FoxSemanticChecker.Mod 397 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Parser := FoxParser,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Streams, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. (* temporary variables for the visitors
  76. they replace variables on a stack during use of the visitor pattern and may only be
  77. - set in AcceptXXX procedures
  78. - set and read in ResolveXXX procedures
  79. *)
  80. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  81. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  82. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  83. currentScope-: SyntaxTree.Scope;
  84. currentIsRealtime: BOOLEAN;
  85. currentIsUnreachable: BOOLEAN;
  86. currentIsCellNet: BOOLEAN;
  87. currentIsBodyProcedure: BOOLEAN;
  88. currentIsExclusive: BOOLEAN;
  89. global: SyntaxTree.ModuleScope;
  90. withEntries: WithEntry;
  91. activeCellsStatement: BOOLEAN;
  92. replacements*: Replacement;
  93. cellsAreObjects: BOOLEAN;
  94. variableAccessed: BOOLEAN;
  95. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope);
  96. BEGIN
  97. SELF.diagnostics := diagnostics;
  98. SELF.useDarwinCCalls := useDarwinCCalls;
  99. SELF.cooperative := cooperative;
  100. SELF.system := system;
  101. SELF.symbolFileFormat := symbolFileFormat;
  102. error := FALSE;
  103. NEW(typeFixes);
  104. NEW(pointerFixes);
  105. resolvedType := NIL;
  106. resolvedExpression := NIL;
  107. resolvedStatement := NIL;
  108. currentScope := NIL;
  109. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  110. SELF.importCache := importCache;
  111. arrayBaseImported := FALSE;
  112. SELF.VerboseErrorMessage := verboseErrorMessage;
  113. global := NIL;
  114. phase := UndefinedPhase;
  115. currentIsRealtime := FALSE;
  116. currentIsUnreachable := FALSE;
  117. currentIsCellNet := FALSE;
  118. currentIsBodyProcedure := FALSE;
  119. currentIsExclusive := FALSE;
  120. withEntries := NIL;
  121. SELF.cellsAreObjects := system.cellsAreObjects;
  122. END InitChecker;
  123. (** report error **)
  124. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  125. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  126. BEGIN
  127. IF diagnostics # NIL THEN
  128. Basic.GetErrorMessage(code,message,errorMessage);
  129. ASSERT(currentScope # NIL);
  130. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  131. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  132. END;
  133. error := TRUE;
  134. END Error;
  135. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  136. VAR errModule: SyntaxTree.Module;
  137. BEGIN
  138. IF diagnostics # NIL THEN
  139. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  140. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  141. END;
  142. END Warning;
  143. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  144. VAR errorMessage: ARRAY 256 OF CHAR;
  145. BEGIN
  146. IF diagnostics # NIL THEN
  147. Basic.Concat(errorMessage,msg," ", msg2);
  148. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  149. END;
  150. error := TRUE;
  151. END ErrorSS;
  152. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  153. VAR msg, msg2: ARRAY 256 OF CHAR;
  154. BEGIN
  155. IF diagnostics # NIL THEN
  156. COPY(msg1, msg);
  157. Strings.Append(msg, " = ");
  158. Basic.GetString(s, msg2);
  159. Strings.Append(msg, msg2);
  160. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  161. END;
  162. END InfoSS;
  163. (*** symbol lookup ***)
  164. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  165. **)
  166. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  167. VAR
  168. scope,baseScope: SyntaxTree.Scope;
  169. symbol, s: SyntaxTree.Symbol;
  170. ownerRecord,base: SyntaxTree.RecordType;
  171. BEGIN
  172. scope := inScope;
  173. symbol := NIL;
  174. WHILE (scope # NIL) & (symbol = NIL) DO
  175. symbol := scope.FindSymbol(name);
  176. s := NIL;
  177. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  178. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  179. END;
  180. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  181. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  182. ELSE
  183. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  184. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  185. symbol.MarkUsed;
  186. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  187. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  188. base := RecordBase(ownerRecord);
  189. IF (base # NIL) THEN
  190. baseScope := base.recordScope;
  191. symbol := Find(baseScope,name,FALSE);
  192. ELSE
  193. symbol := NIL;
  194. END;
  195. ELSE
  196. symbol := NIL;
  197. END;
  198. END;
  199. END;
  200. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  201. END;
  202. IF (symbol # NIL) THEN
  203. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  204. ASSERT(phase = DeclarationPhase);
  205. ResolveSymbol(symbol)
  206. END;
  207. symbol.MarkUsed;
  208. END;
  209. RETURN symbol
  210. END Find;
  211. (*** types ***)
  212. (** find type declaration with name qualifiedIdentifier and return resolved type
  213. - check qualified identifier prefix, set scope to module scope if appropriate
  214. - check suffix in scope
  215. **)
  216. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  217. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  218. BEGIN
  219. result := NIL;
  220. prevScope := currentScope;
  221. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  222. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  223. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  224. IF symbol(SyntaxTree.Import).module = NIL THEN
  225. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  226. result := SyntaxTree.invalidType;
  227. symbol := NIL;
  228. ELSE
  229. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  230. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  231. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  232. IF VerboseErrorMessage THEN
  233. Printout.Info("scope", currentScope);
  234. Printout.Info("symbol", symbol);
  235. END;
  236. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  237. END;
  238. END;
  239. ELSE
  240. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  241. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  242. symbol := NIL;
  243. END;
  244. ELSE
  245. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  246. IF symbol = NIL THEN
  247. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  248. IF VerboseErrorMessage THEN
  249. Printout.Info("Qualident",qualifiedIdentifier);
  250. Printout.Info("in scope",currentScope) ;
  251. END;
  252. END;
  253. END;
  254. IF symbol = NIL THEN (* error already handled *)
  255. typeDeclaration := NIL;
  256. result := SyntaxTree.invalidType;
  257. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  258. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSE
  262. currentScope := symbol.scope;
  263. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  264. result := ResolveType(typeDeclaration.declaredType);
  265. symbol.MarkUsed;
  266. ASSERT(result # NIL);
  267. END;
  268. currentScope := prevScope;
  269. RETURN result
  270. END ResolveNamedType;
  271. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  272. If node is currently being resolved then emit a cyclic definition error.
  273. Return TRUE only if node is fully resolved.
  274. **)
  275. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  276. VAR result: BOOLEAN;
  277. BEGIN
  278. IF SyntaxTree.Resolved IN x.state THEN
  279. result := FALSE
  280. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  281. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  282. result := FALSE;
  283. ELSE
  284. result := TRUE;
  285. x.SetState(SyntaxTree.BeingResolved)
  286. END;
  287. RETURN result
  288. END TypeNeedsResolution;
  289. (** Return invalid type if x is currently being resolved, return x otherwise**)
  290. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  291. BEGIN
  292. IF SyntaxTree.Resolved IN x.state THEN
  293. RETURN x
  294. ELSE
  295. RETURN SyntaxTree.invalidType
  296. END;
  297. END ResolvedType;
  298. PROCEDURE VisitType(x: SyntaxTree.Type);
  299. BEGIN
  300. ASSERT(x = SyntaxTree.invalidType);
  301. END VisitType;
  302. (** resolve basic type **)
  303. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  304. BEGIN
  305. IF TypeNeedsResolution(x) THEN
  306. x.SetState(SyntaxTree.Resolved);
  307. END;
  308. resolvedType := ResolvedType(x)
  309. END VisitBasicType;
  310. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  311. BEGIN
  312. VisitBasicType(x);
  313. END VisitByteType;
  314. (** resolve character type **)
  315. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  316. BEGIN
  317. VisitBasicType(x);
  318. END VisitCharacterType;
  319. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitBooleanType;
  323. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitSetType;
  327. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitAddressType;
  331. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitSizeType;
  335. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitAnyType;
  339. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitObjectType;
  343. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitNilType;
  347. (** resolve integer type **)
  348. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  349. BEGIN
  350. VisitBasicType(x);
  351. END VisitIntegerType;
  352. (** resolve real type **)
  353. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  354. BEGIN
  355. VisitBasicType(x);
  356. END VisitFloatType;
  357. (** resolve complex type **)
  358. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  359. BEGIN
  360. VisitBasicType(x);
  361. END VisitComplexType;
  362. (**
  363. resolve string type: nothing to be done
  364. **)
  365. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  366. BEGIN
  367. IF TypeNeedsResolution(x) THEN
  368. x.SetState(SyntaxTree.Resolved);
  369. END;
  370. resolvedType := ResolvedType(x)
  371. END VisitStringType;
  372. (**
  373. check enumeration scope: enter symbols and check for duplicate names
  374. **)
  375. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  376. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  377. BEGIN
  378. prevScope := currentScope;
  379. currentScope := x;
  380. e := x.firstConstant;
  381. WHILE (e # NIL) DO
  382. Register(e,x,FALSE);
  383. IF SymbolNeedsResolution(e) THEN
  384. IF e.value # NIL THEN
  385. value := ConstantExpression(e.value);
  386. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  387. ELSE
  388. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  389. value.SetType(x.ownerEnumeration);
  390. END;
  391. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  392. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  393. IF nextHighest > highest THEN highest := nextHighest END;
  394. END;
  395. e.SetValue(value);
  396. CheckSymbolVisibility(e);
  397. e.SetType(x.ownerEnumeration);
  398. e.SetState(SyntaxTree.Resolved);
  399. END;
  400. e := e.nextConstant;
  401. END;
  402. currentScope := prevScope;
  403. END CheckEnumerationScope;
  404. (**
  405. resolve enumeration type: check enumeration scope
  406. **)
  407. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  408. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  409. lowest, highest: LONGINT;
  410. BEGIN
  411. IF TypeNeedsResolution(x) THEN
  412. IF x.enumerationBase # NIL THEN
  413. position := x.enumerationBase.position;
  414. baseType := ResolveType(x.enumerationBase);
  415. resolved := baseType.resolved;
  416. baseScope := NIL;
  417. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  418. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  419. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  420. ELSE
  421. enumerationBase := resolved(SyntaxTree.EnumerationType);
  422. lowest := enumerationBase.rangeHighest+1;
  423. END;
  424. x.SetEnumerationBase(baseType);
  425. ELSE lowest := 0;
  426. END;
  427. highest := lowest-1;
  428. CheckEnumerationScope(x.enumerationScope, highest);
  429. x.SetRange(lowest, highest);
  430. x.SetState(SyntaxTree.Resolved);
  431. END;
  432. resolvedType := ResolvedType(x);
  433. END VisitEnumerationType;
  434. (**
  435. resolve range type: nothing to be done
  436. **)
  437. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  438. BEGIN
  439. IF TypeNeedsResolution(x) THEN
  440. x.SetState(SyntaxTree.Resolved);
  441. END;
  442. resolvedType := ResolvedType(x)
  443. END VisitRangeType;
  444. (**
  445. resolve qualified type
  446. - find and resolve named type and set resolved type
  447. **)
  448. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  449. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  450. BEGIN
  451. IF TypeNeedsResolution(x) THEN
  452. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  453. x.SetResolved(type.resolved);
  454. x.SetState(SyntaxTree.Resolved);
  455. x.SetTypeDeclaration (typeDeclaration);
  456. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  457. x.SetResolved(SyntaxTree.invalidType);
  458. END;
  459. resolvedType := x;
  460. END VisitQualifiedType;
  461. (**
  462. resolve array type
  463. - check base type
  464. - array of math array forbidden
  465. - static array of open array forbidden
  466. **)
  467. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  468. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  469. BEGIN
  470. IF TypeNeedsResolution(x) THEN
  471. x.SetArrayBase(ResolveType(x.arrayBase));
  472. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  473. arrayBase := x.arrayBase.resolved;
  474. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  475. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  476. pointerType.SetPointerBase(arrayBase);
  477. pointerType.SetHidden(TRUE);
  478. x.SetArrayBase(pointerType);
  479. END;
  480. IF x.length # NIL THEN
  481. variableAccessed := FALSE;
  482. e := ResolveExpression(x.length);
  483. IF (e.resolved = NIL) THEN
  484. IF variableAccessed THEN
  485. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  486. END;
  487. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  488. ELSE
  489. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  490. END;
  491. END;
  492. IF arrayBase IS SyntaxTree.ArrayType THEN
  493. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  494. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  495. END;
  496. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  497. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  498. END;
  499. x.SetHasPointers(arrayBase.hasPointers);
  500. x.SetState(SyntaxTree.Resolved);
  501. END;
  502. resolvedType := ResolvedType(x);
  503. END VisitArrayType;
  504. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  505. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  506. BEGIN
  507. module := currentScope.ownerModule;
  508. IF module.name=name THEN
  509. (* do nothing *)
  510. ELSE
  511. moduleScope := module.moduleScope;
  512. import := moduleScope.FindImport(name);
  513. IF import = NIL THEN
  514. import := SyntaxTree.NewImport(position,name,name,TRUE);
  515. moduleScope.AddImport(import);
  516. Register(import,moduleScope,FALSE);
  517. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  518. VisitImport(import);
  519. ELSIF import.direct=FALSE THEN
  520. import.SetScope(module.moduleScope);
  521. import.SetDirect(TRUE);
  522. IF moduleScope.FindSymbol(import.name) = NIL THEN
  523. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  524. duplicate.SetContext(import.context);
  525. duplicate.SetModule(import.module);
  526. Register(duplicate,moduleScope,TRUE);
  527. VisitImport(duplicate);
  528. END;
  529. END;
  530. import.MarkUsed
  531. END;
  532. END ImportModule;
  533. (**
  534. resolve math array type
  535. - check base type
  536. - open math array of array forbidden
  537. - math array of tensor forbidden
  538. - static array of open array forbidden
  539. **)
  540. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  541. VAR arrayBase: SyntaxTree.Type;
  542. BEGIN
  543. IF TypeNeedsResolution(x) THEN
  544. x.SetArrayBase(ResolveType(x.arrayBase));
  545. IF x.length # NIL THEN
  546. x.SetLength(ConstantIntegerGeq0(x.length));
  547. END;
  548. arrayBase := x.arrayBase;
  549. IF arrayBase # NIL THEN
  550. arrayBase := arrayBase.resolved;
  551. IF arrayBase = SyntaxTree.invalidType THEN
  552. (* error already handled *)
  553. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  554. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  555. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  556. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  557. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  558. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  559. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  560. END;
  561. END;
  562. IF x.form = SyntaxTree.Static THEN
  563. x.SetIncrement(system.SizeOf(arrayBase));
  564. END;
  565. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  566. END;
  567. x.SetState(SyntaxTree.Resolved);
  568. END;
  569. resolvedType := ResolvedType(x);
  570. END VisitMathArrayType;
  571. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  572. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  573. (1) Rec = RECORD ... END; Ptr <---> Rec
  574. Ptr = POINTER TO Rec; ^ |
  575. | |
  576. TypeDesc TypeDesc
  577. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  578. ^ /
  579. | /
  580. TypeDesc <-- /
  581. *)
  582. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  583. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  584. BEGIN
  585. Strings.IntToStr(x.position,number);
  586. COPY(prefix,name);
  587. Strings.Append(name,"@");
  588. Strings.Append(name,number);
  589. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  590. typeDeclaration.SetDeclaredType(x);
  591. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  592. x.SetTypeDeclaration(typeDeclaration);
  593. currentScope.AddTypeDeclaration(typeDeclaration);
  594. typeDeclaration.SetScope(currentScope);
  595. END AnonymousTypeDeclaration;
  596. (**
  597. deferred pointer type resolving
  598. - resolve base type
  599. - check that base type is a record or array type
  600. - if error then set base type to invalid type
  601. **)
  602. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  603. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  604. BEGIN
  605. ASSERT(type.pointerBase # NIL);
  606. position := type.pointerBase.position;
  607. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  608. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  609. (* not for pointers, a type is needed for the records only
  610. IF type.typeDeclaration = NIL THEN
  611. AnonymousTypeDeclaration(type);
  612. END;
  613. *)
  614. END;
  615. resolved := ResolveType(type.pointerBase);
  616. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  617. type.SetPointerBase(resolved);
  618. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  619. recordType := resolved.resolved(SyntaxTree.RecordType);
  620. IF recordType.isObject & (recordType.baseType # NIL) THEN
  621. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  622. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  623. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  624. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  625. END;
  626. END;
  627. END;
  628. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  629. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  630. END;
  631. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  632. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  633. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  634. ELS
  635. *)
  636. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  637. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  638. END;
  639. END;
  640. ELSE
  641. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  642. type.SetPointerBase(SyntaxTree.invalidType)
  643. END
  644. END FixPointerType;
  645. (**
  646. resolve pointer type
  647. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  648. **)
  649. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  650. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  651. modifiers: SyntaxTree.Modifier; position: LONGINT;
  652. BEGIN
  653. IF TypeNeedsResolution(x) THEN
  654. modifiers := x.modifiers;
  655. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  656. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  657. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  658. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  659. (* inheritance cycle check
  660. example:
  661. A=POINTER TO RECORD(B) END;
  662. B=POINTER TO RECORD(A) END;
  663. *)
  664. IF x.pointerBase IS SyntaxTree.RecordType THEN
  665. recordType := x.pointerBase(SyntaxTree.RecordType);
  666. recordBaseType := ResolveType(recordType.baseType);
  667. recordType.SetBaseType(recordBaseType);
  668. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  669. END;
  670. CheckModifiers(modifiers, TRUE);
  671. typeFixes.Add(x,currentScope);
  672. x.SetState(SyntaxTree.Resolved);
  673. END;
  674. resolvedType := ResolvedType(x)
  675. END VisitPointerType;
  676. (**
  677. resolve port type
  678. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  679. **)
  680. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  681. VAR value: LONGINT;
  682. BEGIN
  683. IF TypeNeedsResolution(x) THEN
  684. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  685. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  686. x.SetSize(value)
  687. ELSE
  688. x.SetSize(system.SizeOf(system.longintType));
  689. END;
  690. x.SetState(SyntaxTree.Resolved);
  691. END;
  692. resolvedType := ResolvedType(x)
  693. END VisitPortType;
  694. (**
  695. deferred procedure type resolving
  696. - resolve return type
  697. - traverse and resolve parameters
  698. **)
  699. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  700. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  701. BEGIN
  702. resolved := ResolveType(procedureType.returnType);
  703. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  704. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  705. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  706. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  707. END;
  708. procedureType.SetReturnType(resolved);
  709. IF (resolved # NIL) THEN
  710. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  711. parameter.SetType(procedureType.returnType);
  712. parameter.SetAccess(SyntaxTree.Hidden);
  713. parameter.SetUntraced(procedureType.hasUntracedReturn);
  714. VisitParameter(parameter);
  715. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  716. END;
  717. (* process parameters *)
  718. parameter :=procedureType.firstParameter;
  719. WHILE (parameter # NIL) DO
  720. VisitParameter(parameter);
  721. parameter := parameter.nextParameter;
  722. END;
  723. END FixProcedureType;
  724. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  725. VAR prev,this: SyntaxTree.Modifier;
  726. BEGIN
  727. this := modifiers;prev := NIL;
  728. WHILE (this # NIL) & (this.identifier # name) DO
  729. prev := this; this := this.nextModifier;
  730. END;
  731. IF this # NIL THEN
  732. IF this.expression # NIL THEN
  733. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  734. END;
  735. this.Resolved;
  736. position := this.position;
  737. RETURN TRUE
  738. ELSE
  739. RETURN FALSE
  740. END;
  741. END HasFlag;
  742. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  743. VAR prev,this: SyntaxTree.Modifier;
  744. BEGIN
  745. this := modifiers;prev := NIL;
  746. WHILE (this # NIL) & (this.identifier # name) DO
  747. prev := this; this := this.nextModifier;
  748. END;
  749. IF this # NIL THEN
  750. IF this.expression = NIL THEN
  751. Error(this.position,Diagnostics.Invalid,"expected expression value");
  752. ELSE
  753. this.SetExpression(ConstantExpression(this.expression));
  754. IF CheckIntegerValue(this.expression,value) THEN END;
  755. END;
  756. this.Resolved;
  757. position := this.position;
  758. RETURN TRUE
  759. ELSE RETURN FALSE
  760. END;
  761. END HasValue;
  762. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  763. VAR this: SyntaxTree.Modifier;
  764. BEGIN
  765. this := modifiers;
  766. WHILE this # NIL DO
  767. IF ~this.resolved THEN
  768. IF checkUse THEN
  769. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  770. ELSE
  771. this.SetExpression(ResolveExpression(this.expression));
  772. this.Resolved;
  773. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  774. END;
  775. END;
  776. this := this.nextModifier
  777. END;
  778. END CheckModifiers;
  779. (**
  780. resolve procedure type
  781. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  782. **)
  783. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  784. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  785. BEGIN
  786. IF TypeNeedsResolution(procedureType) THEN
  787. modifiers := procedureType.modifiers;
  788. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  789. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  790. procedureType.SetInterrupt(TRUE);
  791. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  792. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  793. IF useDarwinCCalls THEN (*fld*)
  794. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  795. ELSE
  796. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  797. END
  798. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  799. procedureType.SetNoReturn(TRUE);
  800. END;
  801. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  802. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  803. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  804. CheckModifiers(modifiers, TRUE);
  805. modifiers := procedureType.returnTypeModifiers;
  806. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  807. CheckModifiers(modifiers, TRUE);
  808. typeFixes.Add(procedureType,currentScope);
  809. procedureType.SetHasPointers(procedureType.isDelegate);
  810. procedureType.SetState(SyntaxTree.Resolved);
  811. END;
  812. resolvedType := ResolvedType(procedureType)
  813. END VisitProcedureType;
  814. (** check and resolve record type
  815. - check base type: must be record, math array or array-structured object type
  816. - check declarations
  817. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  818. **)
  819. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  820. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  821. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  822. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  823. hasPointers: BOOLEAN;
  824. modifiers: SyntaxTree.Modifier;
  825. value: LONGINT;
  826. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  827. BEGIN
  828. type := type.resolved;
  829. IF (type IS SyntaxTree.PointerType) &
  830. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  831. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  832. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  833. RETURN TRUE
  834. ELSE
  835. RETURN FALSE
  836. END;
  837. END IsPointerToRecord;
  838. BEGIN
  839. IF TypeNeedsResolution(x) THEN
  840. hasPointers := FALSE;
  841. modifiers := x.modifiers;
  842. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  843. CheckModifiers(modifiers, TRUE);
  844. IF x.baseType # NIL THEN
  845. position := x.baseType.position;
  846. baseType := ResolveType(x.baseType);
  847. resolved := baseType.resolved;
  848. hasPointers := hasPointers OR resolved.hasPointers;
  849. IF x.isObject THEN (* object *)
  850. ASSERT(x.pointerType # NIL);
  851. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  852. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  853. baseType := NIL
  854. ELSIF IsPointerToRecord(resolved,recordType) THEN
  855. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  856. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  857. ELSE
  858. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  859. END;
  860. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  861. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  862. ELSIF IsPointerToRecord(resolved,recordType) THEN
  863. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  864. ELSIF resolved IS SyntaxTree.RecordType THEN
  865. ELSE
  866. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  867. END;
  868. ELSE
  869. IF resolved IS SyntaxTree.RecordType THEN
  870. ELSE
  871. Error(position, Diagnostics.Invalid,"record does not extend record")
  872. END;
  873. END;
  874. x.SetBaseType(baseType);
  875. IF x.Level() > 15 THEN
  876. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  877. (* note:
  878. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  879. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  880. inheritance history of a type.
  881. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  882. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  883. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  884. *)
  885. END;
  886. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  887. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  888. END;
  889. END;
  890. Declarations(x.recordScope);
  891. ResolveArrayStructure(x);
  892. (* computation of sizes and offsets skipped -> done in backend / system *)
  893. recordBase := x.GetBaseRecord();
  894. IF recordBase = NIL THEN numberMethods := 0
  895. ELSE numberMethods := recordBase.recordScope.numberMethods
  896. END;
  897. isRealtime := TRUE;
  898. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  899. WHILE symbol # NIL DO
  900. IF symbol IS SyntaxTree.Variable THEN
  901. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  902. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  903. END;
  904. IF symbol IS SyntaxTree.Procedure THEN
  905. procedure := symbol(SyntaxTree.Procedure);
  906. IF procedure.super # NIL THEN
  907. procedure.SetMethodNumber(procedure.super.methodNumber)
  908. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  909. procedure.SetMethodNumber(numberMethods);
  910. INC(numberMethods);
  911. END;
  912. END;
  913. symbol := symbol.nextSymbol;
  914. END;
  915. IF isRealtime THEN x.SetRealtime(TRUE) END;
  916. x.recordScope.SetNumberMethods(numberMethods);
  917. (* TODO: is this needed anymore? *)
  918. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  919. Error(x.position,Diagnostics.Invalid,"object extends a record")
  920. END;
  921. IF (x.typeDeclaration = NIL) THEN
  922. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  923. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  924. (*
  925. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  926. AnonymousTypeDeclaration(x,name);
  927. *)
  928. ELSE
  929. AnonymousTypeDeclaration(x,"Anonymous");
  930. END;
  931. END;
  932. x.SetHasPointers(hasPointers);
  933. x.SetState(SyntaxTree.Resolved);
  934. END;
  935. resolvedType := ResolvedType(x);
  936. END VisitRecordType;
  937. (** check and resolve cell type
  938. - check base type: must be cell
  939. - check declarations
  940. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  941. **)
  942. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  943. VAR
  944. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  945. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  946. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  947. recordBase: SyntaxTree.RecordType;
  948. numberMethods, int: LONGINT;
  949. real: LONGREAL;
  950. bool: BOOLEAN;
  951. set: SET;
  952. variable: SyntaxTree.Variable;
  953. v: SyntaxTree.Expression;
  954. str: Scanner.StringType;
  955. atype: SyntaxTree.ArrayType;
  956. prev: SyntaxTree.Scope;
  957. BEGIN
  958. IF TypeNeedsResolution(x) THEN
  959. recordBase := NIL;
  960. IF cellsAreObjects THEN
  961. IF x.baseType = NIL THEN
  962. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  963. ImportModule(qualifiedIdentifier.prefix, x.position);
  964. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  965. x.SetBaseType(ResolveType(x.baseType));
  966. recordBase := x.GetBaseRecord();
  967. IF recordBase = NIL THEN
  968. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  969. END;
  970. ELSE
  971. x.SetBaseType(ResolveType(x.baseType));
  972. END;
  973. ELSE
  974. x.SetBaseType(ResolveType(x.baseType));
  975. END;
  976. IF recordBase = NIL THEN numberMethods := 0
  977. ELSE numberMethods := recordBase.recordScope.numberMethods
  978. END;
  979. modifier := x.modifiers;
  980. (*IF ~x.isCellNet THEN*)
  981. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  982. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  983. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  984. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  985. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  986. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  987. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  988. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  989. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  990. symbol := system.activeCellsCapabilities;
  991. WHILE symbol # NIL DO
  992. IF HasFlag(modifier, symbol.name, position) THEN END;
  993. symbol := symbol.nextSymbol;
  994. END;
  995. modifier := x.modifiers;
  996. WHILE (modifier # NIL) DO
  997. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  998. IF modifier.expression # NIL THEN
  999. v := ConstantExpression(modifier.expression);
  1000. property.SetValue(v);
  1001. IF IsIntegerValue(modifier.expression, int) THEN
  1002. (*property.SetValue(modifier.expression);*)
  1003. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1004. property.SetType(system.longintType);
  1005. ELSIF IsRealValue(modifier.expression, real) THEN
  1006. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1007. property.SetType(system.longrealType);
  1008. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1009. property.SetType(system.booleanType);
  1010. ELSIF IsSetValue(modifier.expression, set) THEN
  1011. property.SetType(system.setType);
  1012. ELSIF IsStringValue(modifier.expression, str) THEN
  1013. (*property.SetValue(modifier.expression);*)
  1014. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1015. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1016. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1017. property.SetType(atype);
  1018. ELSE
  1019. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1020. END;
  1021. ELSE (* flag property *)
  1022. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1023. property.SetType(system.booleanType);
  1024. END;
  1025. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1026. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1027. x.AddProperty(property);
  1028. modifier := modifier.nextModifier;
  1029. END;
  1030. (*ELSE
  1031. (* no: this should not be allowed on cell net types or check against global scope ...
  1032. IF HasValue(modifier, Global.NameFrequencyDivider, position,value) THEN
  1033. IF parameter # NIL THEN Error(position, Diagnostics.Invalid,"forbiddern frequency divider in non-terminal cellnet")
  1034. END;
  1035. END
  1036. *)
  1037. END;*)
  1038. CheckModifiers(modifier, FALSE);
  1039. Declarations(x.cellScope);
  1040. (* process parameters *)
  1041. prev := currentScope;
  1042. currentScope := x.cellScope;
  1043. parameter :=x.firstParameter;
  1044. WHILE (parameter # NIL) DO
  1045. VisitParameter(parameter);
  1046. type := parameter.type.resolved;
  1047. IF ~(type IS SyntaxTree.PortType) THEN
  1048. WHILE IsStaticArray(type, type, len) DO
  1049. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1050. END;
  1051. WHILE IsDynamicArray(type, type) DO
  1052. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1053. END;
  1054. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1055. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1056. END;
  1057. END;
  1058. parameter := parameter.nextParameter;
  1059. END;
  1060. currentScope := prev;
  1061. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1062. WHILE symbol # NIL DO
  1063. IF symbol IS SyntaxTree.Variable THEN
  1064. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1065. END;
  1066. symbol := symbol.nextSymbol;
  1067. END;
  1068. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1069. IF (x.typeDeclaration = NIL) THEN
  1070. AnonymousTypeDeclaration(x,"Anonymous");
  1071. END;
  1072. x.SetState(SyntaxTree.Resolved);
  1073. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1074. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1075. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1076. Warning(x.position, "Non-empty body for an engine?");
  1077. END;
  1078. END;
  1079. resolvedType := ResolvedType(x);
  1080. END VisitCellType;
  1081. (* check if an object is an array-structured object type
  1082. - determine the array structure
  1083. - collect operators from top to bottom in the inheritance hierarchy
  1084. - check if LEN operator is declared
  1085. - determine number of possible index operators
  1086. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1087. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1088. *)
  1089. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1090. VAR
  1091. indexOperatorCount, i: LONGINT;
  1092. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1093. isTensor: BOOLEAN;
  1094. BEGIN
  1095. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1096. (* determine array structure *)
  1097. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1098. END;
  1099. IF recordType.HasArrayStructure() THEN
  1100. (* the object is an ASOT *)
  1101. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1102. (* reset array access operators *)
  1103. arrayAccessOperators.len := NIL;
  1104. arrayAccessOperators.generalRead := NIL;
  1105. arrayAccessOperators.generalWrite := NIL;
  1106. IF isTensor THEN
  1107. (* all operators of dimensionalities 1 to max *)
  1108. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1109. ELSE
  1110. (* all operators of certain dimensionality *)
  1111. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1112. END;
  1113. NEW(arrayAccessOperators.read, indexOperatorCount);
  1114. NEW(arrayAccessOperators.write, indexOperatorCount);
  1115. FOR i := 0 TO indexOperatorCount - 1 DO
  1116. arrayAccessOperators.read[i] := NIL;
  1117. arrayAccessOperators.write[i] := NIL
  1118. END;
  1119. (* collect access operators in the record scope *)
  1120. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1121. IF arrayAccessOperators.len = NIL THEN
  1122. (* TODO: think about making this operator optional for static array structures *)
  1123. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1124. END;
  1125. (* show error messages *)
  1126. IF isTensor THEN
  1127. (* require ARRAY [*] OF RANGE *)
  1128. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1129. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1130. ELSE
  1131. (* forbid ARRAY [*] OF RANGE *)
  1132. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1133. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1134. (* require RANGE, RANGE, ... RANGE *)
  1135. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1136. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1137. END;
  1138. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1139. ELSE
  1140. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1141. IF recordType.recordScope.firstOperator # NIL THEN
  1142. RETURN;
  1143. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1144. END
  1145. END
  1146. END ResolveArrayStructure;
  1147. (** collect array access operators in a record scope **)
  1148. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1149. VAR
  1150. baseType: SyntaxTree.Type;
  1151. operator: SyntaxTree.Operator;
  1152. isReadOperator, isGeneralOperator: BOOLEAN;
  1153. indexListSize, indexListKind, hashValue: LONGINT;
  1154. BEGIN
  1155. (* if a parent record scope exists, collect the operators there first *)
  1156. baseType := recordScope.ownerRecord.baseType;
  1157. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1158. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1159. END;
  1160. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1161. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1162. END;
  1163. (* go through all operators in the current record scope *)
  1164. operator := recordScope.firstOperator;
  1165. WHILE operator # NIL DO
  1166. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1167. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1168. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1169. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1170. IF isGeneralOperator THEN
  1171. IF isReadOperator THEN
  1172. arrayAccessOperators.generalRead := operator
  1173. ELSE
  1174. arrayAccessOperators.generalWrite := operator
  1175. END
  1176. ELSE
  1177. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1178. IF isReadOperator THEN
  1179. arrayAccessOperators.read[hashValue] := operator
  1180. ELSE
  1181. arrayAccessOperators.write[hashValue] := operator
  1182. END
  1183. END
  1184. END
  1185. ELSE
  1186. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1187. END;
  1188. operator := operator.nextOperator
  1189. END
  1190. END CollectArrayAccessOperators;
  1191. (** the hash value of an index operator **)
  1192. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1193. VAR result: LONGINT;
  1194. BEGIN
  1195. IF isTensor THEN
  1196. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1197. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1198. ELSE
  1199. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1200. END
  1201. ELSE
  1202. result := indexListKind
  1203. END;
  1204. RETURN result
  1205. END IndexOperatorHash;
  1206. (** 2 to the power of exponent **)
  1207. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1208. VAR result, i: LONGINT;
  1209. BEGIN
  1210. result := 1;
  1211. FOR i := 1 TO exponent DO
  1212. result := result * 2;
  1213. END;
  1214. RETURN result
  1215. END TwoToThePowerOf;
  1216. (** check if a LEN operator has a correct signature. i.e.
  1217. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1218. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1219. **)
  1220. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1221. VAR
  1222. procedureType: SyntaxTree.ProcedureType;
  1223. returnedArrayType: SyntaxTree.MathArrayType;
  1224. result: BOOLEAN;
  1225. BEGIN
  1226. result := FALSE;
  1227. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1228. IF (procedureType.numberParameters = 0) THEN
  1229. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1230. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1231. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1232. IF returnedArrayType.form = SyntaxTree.Open THEN
  1233. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1234. result := TRUE
  1235. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1236. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1237. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1238. result := TRUE
  1239. END
  1240. END
  1241. END
  1242. END
  1243. END;
  1244. IF result THEN
  1245. (* export symbol automatically *)
  1246. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1247. ELSE
  1248. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1249. END;
  1250. RETURN result
  1251. END CheckLenOperator;
  1252. (** check if an index operator has a correct signature. i.e.
  1253. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1254. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1255. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1256. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1257. - determine if it is a read or write operator (existance of return type)
  1258. - check index parameters
  1259. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1260. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1261. [LONGINT] -> binary 0 -> 0
  1262. [RANGE] -> binary 1 -> 1
  1263. [LONGINT, LONGINT] -> binary 00 -> 0
  1264. [LONGINT, RANGE] -> binary 01 -> 1
  1265. [RANGE, LONGINT] -> binary 10 -> 2
  1266. [RANGE, RANGE] -> binary 11 -> 3
  1267. etc.
  1268. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1269. - for read operators, check if return type matches the type of data that is read
  1270. - for write operators, check if last parameter type matches the type of data that is written
  1271. **)
  1272. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1273. VAR
  1274. elementType, otherElementType, dataType: SyntaxTree.Type;
  1275. procedureType: SyntaxTree.ProcedureType;
  1276. mathArrayType: SyntaxTree.MathArrayType;
  1277. parameter: SyntaxTree.Parameter;
  1278. parameterCount, rangeCount, i: LONGINT;
  1279. hasTypeError: BOOLEAN;
  1280. BEGIN
  1281. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1282. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1283. (* determine if it is a read or write operator *)
  1284. isReadOperator := (procedureType.returnType # NIL);
  1285. IF isReadOperator THEN
  1286. indexListSize := parameterCount;
  1287. ELSE
  1288. indexListSize := parameterCount - 1;
  1289. END;
  1290. IF indexListSize < 1 THEN
  1291. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1292. RETURN FALSE
  1293. END;
  1294. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1295. (* general operator *)
  1296. isGeneralOperator := TRUE;
  1297. IF indexListSize > 1 THEN
  1298. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1299. RETURN FALSE
  1300. END;
  1301. (* ARRAY [*] OF RANGE*)
  1302. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1303. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1304. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1305. RETURN FALSE
  1306. END;
  1307. parameter := procedureType.firstParameter.nextParameter
  1308. ELSE
  1309. (* fixed-dim. operator *)
  1310. isGeneralOperator := FALSE;
  1311. (* check number of index parameters *)
  1312. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1313. (* for tensors, limited to a certain size *)
  1314. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1315. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1316. RETURN FALSE
  1317. END
  1318. ELSE
  1319. (* for non-tensors, depends on dimensionality *)
  1320. IF indexListSize # arrayStructure.Dimensionality() THEN
  1321. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1322. RETURN FALSE
  1323. END
  1324. END;
  1325. (* go through all index parameters
  1326. - count the number of ranges
  1327. - determine the index list kind number
  1328. *)
  1329. indexListKind := 0;
  1330. rangeCount := 0;
  1331. parameter := procedureType.firstParameter;
  1332. FOR i := 1 TO indexListSize DO
  1333. indexListKind := indexListKind * 2;
  1334. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1335. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1336. INC(indexListKind);
  1337. INC(rangeCount)
  1338. ELSE
  1339. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1340. RETURN FALSE
  1341. END;
  1342. parameter := parameter.nextParameter
  1343. END;
  1344. END;
  1345. (*
  1346. - for read operators: check type of last parameter
  1347. - for write operators: check return type
  1348. *)
  1349. IF isReadOperator THEN
  1350. dataType := procedureType.returnType (* the return type *)
  1351. ELSE
  1352. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1353. END;
  1354. elementType := arrayStructure.ElementType();
  1355. hasTypeError := FALSE;
  1356. IF isGeneralOperator THEN
  1357. (* ARRAY [?] OF <Element> *)
  1358. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1359. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1360. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1361. hasTypeError := TRUE
  1362. END
  1363. ELSE
  1364. hasTypeError := TRUE
  1365. END
  1366. ELSE
  1367. IF rangeCount = 0 THEN
  1368. (* <Element> *)
  1369. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1370. ELSE
  1371. (* ARRAY [*, *, ..., *] OF <Element> *)
  1372. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1373. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1374. IF mathArrayType.IsFullyDynamic() THEN
  1375. IF mathArrayType.Dimensionality() = rangeCount THEN
  1376. otherElementType := mathArrayType.ElementType();
  1377. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1378. ELSE
  1379. hasTypeError := TRUE
  1380. END
  1381. ELSE
  1382. hasTypeError := TRUE
  1383. END
  1384. ELSE
  1385. hasTypeError := TRUE
  1386. END
  1387. END
  1388. END;
  1389. IF hasTypeError THEN
  1390. IF isReadOperator THEN
  1391. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1392. ELSE
  1393. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1394. END;
  1395. RETURN FALSE
  1396. END;
  1397. (* export symbol automatically *)
  1398. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1399. RETURN TRUE
  1400. END CheckIndexOperator;
  1401. (** resolve all pending types (late resolving).
  1402. - type fixes are resolved at the end of the declaration phase
  1403. - type fixes may imply new type fixes that are also entered at the end of the list
  1404. **)
  1405. PROCEDURE FixTypes;
  1406. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1407. BEGIN
  1408. prevScope := currentScope;
  1409. p := typeFixes.Get(currentScope);
  1410. WHILE p # NIL DO
  1411. ASSERT(currentScope # NIL);
  1412. ASSERT(p IS SyntaxTree.Type);
  1413. IF p IS SyntaxTree.PointerType THEN
  1414. FixPointerType(p(SyntaxTree.PointerType))
  1415. ELSIF p IS SyntaxTree.ProcedureType THEN
  1416. FixProcedureType(p(SyntaxTree.ProcedureType))
  1417. ELSE
  1418. HALT(100);
  1419. END;
  1420. p := typeFixes.Get(currentScope);
  1421. END;
  1422. currentScope :=prevScope;
  1423. END FixTypes;
  1424. (**
  1425. resolve type x
  1426. - if x is nil then return nil
  1427. - if x cannot be resolved then the result is invalidType else the result is x
  1428. - the resolved type is entered into x.resolved
  1429. **)
  1430. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1431. VAR prev,resolved: SyntaxTree.Type;
  1432. BEGIN
  1433. prev := resolvedType;
  1434. resolvedType := SyntaxTree.invalidType;
  1435. IF x = NIL THEN resolvedType := NIL
  1436. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1437. END;
  1438. resolved := resolvedType;
  1439. resolvedType := prev;
  1440. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1441. RETURN resolved
  1442. END ResolveType;
  1443. (*** compatibility rules ***)
  1444. (**
  1445. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1446. **)
  1447. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1448. VAR result: SyntaxTree.Type;
  1449. BEGIN
  1450. result := SyntaxTree.invalidType;
  1451. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1452. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1453. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1454. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1455. ELSE result := type.resolved
  1456. END;
  1457. RETURN result
  1458. END RegularType;
  1459. (** returns signature compatibility of procedure types this and to
  1460. - if not compatible then error is reported
  1461. - compatibility means type equality
  1462. **)
  1463. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1464. VAR result: BOOLEAN;
  1465. BEGIN
  1466. result := SameType(to,this);
  1467. IF ~result THEN
  1468. Error(position,Diagnostics.Invalid,"signature incompatible");
  1469. IF VerboseErrorMessage THEN
  1470. Printout.Info("this",this);
  1471. Printout.Info("to",to);
  1472. END;
  1473. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1474. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1475. END;
  1476. RETURN result
  1477. END SignatureCompatible;
  1478. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1479. - for var parameters compatibility means same type except for
  1480. - formal is of open array of system byte
  1481. - formal is of record type
  1482. - formal is of open array type
  1483. - formal is of open math array type
  1484. - for value parameters compatibllity means assignment compatibility except for
  1485. - formal is of open array type
  1486. if compatible the return true else report error and return false
  1487. **)
  1488. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1489. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1490. BEGIN
  1491. formalType := RegularType(formal.position,formal.type);
  1492. actualType := RegularType(actual.position,actual.type);
  1493. error := FALSE;
  1494. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1495. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1496. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1497. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1498. END;
  1499. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1500. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1501. ELSIF ~IsVariable(actual) THEN
  1502. result := FALSE; error := TRUE;
  1503. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1504. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1505. ELSE
  1506. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1507. END;
  1508. IF VerboseErrorMessage THEN
  1509. Printout.Info("actual",actual);
  1510. Printout.Info("formal",formal);
  1511. END;
  1512. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1513. result := CompatibleTo(system,actualType,formalType);
  1514. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1515. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1516. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1517. IF IsArrayStructuredObjectType(actualType) THEN
  1518. actualType := MathArrayStructureOfType(actualType)
  1519. END;
  1520. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1521. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1522. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1523. END;
  1524. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1525. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1526. END;
  1527. ELSE
  1528. result := SameType(actualType,formalType)
  1529. END
  1530. ELSE
  1531. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1532. actualType := system.characterType;
  1533. END;
  1534. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1535. result := TRUE; (* special rule for WINAPI parameters *)
  1536. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1537. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1538. ELSE
  1539. result := CompatibleTo(system,actualType,formalType);
  1540. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1541. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1542. END;
  1543. END;
  1544. END;
  1545. IF ~result & ~error THEN
  1546. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1547. IF VerboseErrorMessage THEN
  1548. Printout.Info("actual",actual);
  1549. Printout.Info("formal",formal);
  1550. END;
  1551. END;
  1552. RETURN result
  1553. END ParameterCompatible;
  1554. (** check compatibility for expressions of the form left := right
  1555. - if compatible then return true else error report and return false
  1556. - check if left is variable
  1557. - check compatiblity
  1558. **)
  1559. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1560. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1561. BEGIN
  1562. result := FALSE;
  1563. leftType := RegularType(left.position,left.type);
  1564. rightType := RegularType(right.position,right.type);
  1565. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1566. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1567. END;
  1568. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1569. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1570. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1571. (* error already handled *)
  1572. result := TRUE;
  1573. ELSIF ~IsVariable(left) THEN
  1574. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1575. IF VerboseErrorMessage THEN
  1576. Printout.Info("left",left);
  1577. Printout.Info("right",right);
  1578. END;
  1579. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1580. result := TRUE;
  1581. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1582. result := TRUE
  1583. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1584. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1585. IF VerboseErrorMessage THEN
  1586. Printout.Info("left",left);
  1587. Printout.Info("right",right);
  1588. END;
  1589. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1590. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1591. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1592. ELSE
  1593. result := TRUE
  1594. END;
  1595. RETURN result
  1596. END AssignmentCompatible;
  1597. (*** values ***)
  1598. (** check and resolve integer value **)
  1599. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1600. VAR hugeint: HUGEINT;
  1601. BEGIN
  1602. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1603. value.SetType(Global.GetIntegerType(system,hugeint));
  1604. resolvedExpression := value
  1605. END VisitIntegerValue;
  1606. (** check and resolve real value **)
  1607. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1608. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1609. BEGIN
  1610. subtype := value(SyntaxTree.RealValue).subtype;
  1611. IF subtype = Scanner.Real THEN
  1612. type := system.realType
  1613. ELSIF subtype = Scanner.Longreal THEN
  1614. type := system.longrealType
  1615. ELSE
  1616. HALT(100)
  1617. END;
  1618. value.SetType(type);
  1619. resolvedExpression := value
  1620. END VisitRealValue;
  1621. (** check and resolve complex value **)
  1622. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1623. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1624. BEGIN
  1625. subtype := value(SyntaxTree.ComplexValue).subtype;
  1626. IF subtype = Scanner.Real THEN
  1627. type := system.complexType
  1628. ELSIF subtype = Scanner.Longreal THEN
  1629. type := system.longcomplexType
  1630. ELSE
  1631. HALT(100)
  1632. END;
  1633. value.SetType(type);
  1634. resolvedExpression := value
  1635. END VisitComplexValue;
  1636. (** check and resolve set value **)
  1637. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1638. BEGIN
  1639. value.SetType(system.setType);
  1640. resolvedExpression := value
  1641. END VisitSetValue;
  1642. (** check and resolve set value **)
  1643. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1644. BEGIN
  1645. value.SetType(SyntaxTree.invalidType);
  1646. resolvedExpression := value
  1647. END VisitMathArrayValue;
  1648. (** check and resolve boolean value **)
  1649. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1650. BEGIN
  1651. value.SetType(system.booleanType);
  1652. resolvedExpression := value
  1653. END VisitBooleanValue;
  1654. (** check and resolve string value **)
  1655. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1656. BEGIN
  1657. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1658. resolvedExpression := value
  1659. END VisitStringValue;
  1660. (** check and resolve character value **)
  1661. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1662. BEGIN
  1663. value.SetType(system.characterType);
  1664. resolvedExpression := value
  1665. END VisitCharacterValue;
  1666. (** check and resolve nil value **)
  1667. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1668. BEGIN
  1669. value.SetType(system.nilType);
  1670. resolvedExpression := value
  1671. END VisitNilValue;
  1672. (** check and resolve enumerator value **)
  1673. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1674. BEGIN
  1675. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1676. ASSERT(value.type # NIL);
  1677. resolvedExpression := value
  1678. END VisitEnumerationValue;
  1679. (*** expressions ***)
  1680. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1681. - check all elements on integer type
  1682. - if element range is constant, then check lower and upper bound
  1683. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1684. if an error occurs then report error and return invalidExpression
  1685. **)
  1686. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1687. VAR
  1688. i: LONGINT;
  1689. element: SyntaxTree.Expression;
  1690. constant: BOOLEAN;
  1691. elements: SyntaxTree.ExpressionList;
  1692. s: SET;
  1693. result: SyntaxTree.Expression;
  1694. value: SyntaxTree.Value;
  1695. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1696. VAR
  1697. left, right: SyntaxTree.Expression;
  1698. elementResult: SyntaxTree.Expression;
  1699. leftInteger, rightInteger, temp: LONGINT;
  1700. BEGIN
  1701. (* set context of range *)
  1702. IF element IS SyntaxTree.RangeExpression THEN
  1703. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1704. END;
  1705. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1706. IF elementResult = SyntaxTree.invalidExpression THEN
  1707. (* error already reported *)
  1708. constant := FALSE
  1709. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1710. (* the element is a range expression *)
  1711. (* extract left and right hand side of range *)
  1712. left := elementResult(SyntaxTree.RangeExpression).first;
  1713. right := elementResult(SyntaxTree.RangeExpression).last;
  1714. (* guaranteed by VisitRangeExpression: *)
  1715. ASSERT((left # NIL) & (right # NIL));
  1716. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1717. ELSE
  1718. (* the element is not a range expression *)
  1719. (* check type and add conversion if needed *)
  1720. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1721. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1722. ELSE
  1723. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1724. elementResult := SyntaxTree.invalidExpression;
  1725. constant := FALSE
  1726. END;
  1727. left := elementResult;
  1728. right := elementResult
  1729. END;
  1730. IF elementResult # SyntaxTree.invalidExpression THEN
  1731. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1732. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1733. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1734. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1735. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1736. END
  1737. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1738. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1739. ELSE
  1740. IF leftInteger > rightInteger THEN
  1741. temp := leftInteger; leftInteger := rightInteger; rightInteger := temp
  1742. END;
  1743. IF leftInteger > MAX(SET) THEN leftInteger := MAX(SET) END;
  1744. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END; (*!!!!!!!!! this is a hack !!!!!!! *)
  1745. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1746. extends the range x..31 to x..63 !!!!!! *)
  1747. s := s + {leftInteger..rightInteger};
  1748. (* same but more explicit:
  1749. WHILE (leftInteger <= rightInteger) DO
  1750. INCL(s,leftInteger);
  1751. INC(leftInteger);
  1752. END;
  1753. *)
  1754. END;
  1755. ELSE
  1756. constant := FALSE;
  1757. END
  1758. END;
  1759. RETURN elementResult
  1760. END CheckElement;
  1761. BEGIN
  1762. result := set; constant := TRUE; s := {}; elements := set.elements;
  1763. IF elements # NIL THEN
  1764. FOR i := 0 TO elements.Length()-1 DO
  1765. element := elements.GetExpression(i);
  1766. element := CheckElement(element);
  1767. IF element = SyntaxTree.invalidExpression THEN
  1768. result := SyntaxTree.invalidExpression
  1769. END;
  1770. elements.SetExpression(i,element);
  1771. END;
  1772. END;
  1773. IF constant THEN
  1774. value := SyntaxTree.NewSetValue(set.position,s);
  1775. value.SetType(system.setType);
  1776. result.SetResolved(value);
  1777. END;
  1778. (* optimization possible
  1779. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1780. left this to the programmer...
  1781. *)
  1782. result.SetType(system.setType);
  1783. resolvedExpression := result;
  1784. END VisitSet;
  1785. (*
  1786. old variant: quite generic but needs better conversion handling, do this?
  1787. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1788. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1789. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1790. BEGIN
  1791. type := NIL;
  1792. numberElements := x.elements.Length();
  1793. FOR i := 0 TO numberElements-1 DO
  1794. expression := x.elements.GetExpression(i);
  1795. position := expression.position;
  1796. expression := ResolveExpression(x.elements.GetExpression(i));
  1797. x.elements.SetExpression(i,de);
  1798. IF type = NIL THEN
  1799. type := expression.type;
  1800. ELSIF CompatibleTo(system,expression.type,type) THEN
  1801. (* ok *)
  1802. ELSIF CompatibleTo(system,type,expression.type) THEN
  1803. type := expression.type
  1804. ELSE
  1805. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1806. type := SyntaxTree.invalidType;
  1807. END;
  1808. END;
  1809. isValue := TRUE;
  1810. FOR i := 0 TO numberElements-1 DO
  1811. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1812. x.elements.SetExpression(i,expression);
  1813. isValue := isValue & (expression.resolved # NIL);
  1814. END;
  1815. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1816. arrayType.SetArrayBase(type);
  1817. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1818. IF isValue THEN
  1819. value := SyntaxTree.NewMathArrayValue(position);
  1820. value.SetElements(x.elements);
  1821. x.SetResolved(value);
  1822. END;
  1823. x.SetType(arrayType);
  1824. resolvedExpression := x;
  1825. END VisitMathArrayExpression;
  1826. *)
  1827. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1828. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1829. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1830. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1831. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1832. BEGIN
  1833. numberElements := x.elements.Length();
  1834. FOR i := 0 TO numberElements-1 DO
  1835. expression := x.elements.GetExpression(i);
  1836. IF expression IS SyntaxTree.MathArrayExpression THEN
  1837. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1838. ELSE
  1839. position := expression.position;
  1840. expression := ResolveExpression(x.elements.GetExpression(i));
  1841. x.elements.SetExpression(i,expression);
  1842. IF type = NIL THEN
  1843. type := expression.type;
  1844. ELSIF CompatibleTo(system,expression.type,type) THEN
  1845. (* ok *)
  1846. ELSIF CompatibleTo(system,type,expression.type) THEN
  1847. type := expression.type
  1848. ELSE
  1849. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1850. type := SyntaxTree.invalidType;
  1851. END;
  1852. END;
  1853. END;
  1854. END RecursivelyFindType;
  1855. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1856. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1857. BEGIN
  1858. numberElements := x.elements.Length();
  1859. FOR i := 0 TO numberElements-1 DO
  1860. expression := x.elements.GetExpression(i);
  1861. IF expression IS SyntaxTree.MathArrayExpression THEN
  1862. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1863. ELSE
  1864. position := expression.position;
  1865. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1866. x.elements.SetExpression(i,expression);
  1867. isValue := isValue & (expression.resolved # NIL);
  1868. END;
  1869. END;
  1870. END RecursivelySetExpression;
  1871. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1872. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1873. arrayType: SyntaxTree.MathArrayType;
  1874. BEGIN
  1875. numberElements := x.elements.Length();
  1876. baseType := NIL;
  1877. gsize := 0;
  1878. FOR i := 0 TO numberElements-1 DO
  1879. expression := x.elements.GetExpression(i);
  1880. IF expression IS SyntaxTree.MathArrayExpression THEN
  1881. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1882. IF i=0 THEN
  1883. gsize := size;
  1884. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1885. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1886. ELSE expression.SetType(baseType)
  1887. END;
  1888. ELSIF baseType = NIL THEN baseType := type;
  1889. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1890. END;
  1891. END;
  1892. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1893. arrayType.SetArrayBase(baseType);
  1894. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1895. RETURN ResolveType(arrayType);
  1896. END RecursivelySetType;
  1897. BEGIN
  1898. type := NIL;
  1899. RecursivelyFindType(x);
  1900. isValue := TRUE;
  1901. RecursivelySetExpression(x);
  1902. arrayType := RecursivelySetType(x);
  1903. x.SetType(arrayType);
  1904. IF isValue THEN
  1905. value := SyntaxTree.NewMathArrayValue(x.position);
  1906. value.SetArray(x);
  1907. x.SetResolved(value);
  1908. value.SetType(arrayType);
  1909. END;
  1910. x.SetType(arrayType);
  1911. resolvedExpression := x;
  1912. END VisitMathArrayExpression;
  1913. (** check and resolve unary expression **)
  1914. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1915. VAR
  1916. left: SyntaxTree.Expression;
  1917. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1918. bool: BOOLEAN;
  1919. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1920. value: SyntaxTree.Value;
  1921. BEGIN
  1922. type := SyntaxTree.invalidType;
  1923. left := ResolveExpression(unaryExpression.left);
  1924. unaryExpression.SetLeft(left);
  1925. operator := unaryExpression.operator;
  1926. result := unaryExpression;
  1927. IF ~system.operatorDefined[operator] THEN
  1928. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1929. RETURN
  1930. ELSIF left.type = NIL THEN
  1931. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1932. resolvedExpression := SyntaxTree.invalidExpression;
  1933. RETURN
  1934. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1935. RETURN
  1936. END;
  1937. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1938. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1939. END;
  1940. IF operatorCall # NIL THEN
  1941. result := operatorCall;
  1942. type := operatorCall.type;
  1943. (* admissible operators
  1944. Minus number, set
  1945. Not boolean
  1946. *)
  1947. ELSE
  1948. CASE unaryExpression.operator OF
  1949. |Scanner.Minus:
  1950. IF IsIntegerType(left.type.resolved) THEN
  1951. IF left.resolved # NIL THEN
  1952. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1953. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1954. result.SetResolved(value);
  1955. type := Global.GetIntegerType(system,int);
  1956. value.SetType(type);
  1957. ELSE
  1958. type := left.type
  1959. END
  1960. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1961. IF IsRealValue(left,real) THEN
  1962. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  1963. result.SetResolved(value);
  1964. type := left.type;
  1965. value.SetType(type);
  1966. ELSE
  1967. type := left.type;
  1968. END;
  1969. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  1970. IF IsSetValue(left,set) THEN
  1971. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  1972. result.SetResolved(value);
  1973. type := left.type;
  1974. value.SetType(type);
  1975. ELSE
  1976. type := left.type;
  1977. END;
  1978. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  1979. IF IsComplexValue(left, real, imaginary) THEN
  1980. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  1981. result.SetResolved(value);
  1982. type := left.type;
  1983. value.SetType(type);
  1984. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  1985. ELSE
  1986. type := left.type;
  1987. END
  1988. ELSE
  1989. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  1990. END;
  1991. |Scanner.Not:
  1992. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  1993. IF IsBooleanValue(left,bool) THEN
  1994. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  1995. result.SetResolved(value);
  1996. type := system.booleanType;
  1997. value.SetType(type);
  1998. ELSE
  1999. type := system.booleanType;
  2000. END;
  2001. ELSE
  2002. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2003. END;
  2004. |Scanner.Plus:
  2005. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2006. result := left; type := left.type;
  2007. ELSE
  2008. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2009. END;
  2010. (* ADDRESS OF *)
  2011. |Scanner.Address:
  2012. IF HasAddress(left) THEN
  2013. type := system.addressType;
  2014. ELSE
  2015. type := SyntaxTree.invalidType;
  2016. Error(left.position,Diagnostics.Invalid,"has no address");
  2017. Printout.Info("par", left);
  2018. END;
  2019. (* SIZE OF *)
  2020. |Scanner.Size:
  2021. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2022. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2023. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2024. value := SyntaxTree.NewIntegerValue(left.position, int);
  2025. result.SetResolved(value);
  2026. type := Global.GetIntegerType(system,int);
  2027. value.SetType(type)
  2028. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2029. ELSE
  2030. (* for variables, system sizeof could represent the physically occupied size
  2031. determined via the type descriptor, implement that ? *)
  2032. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2033. END
  2034. (* ALIAS OF *)
  2035. |Scanner.Alias:
  2036. type := left.type.resolved;
  2037. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2038. type := SyntaxTree.invalidType;
  2039. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2040. END;
  2041. ELSE
  2042. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2043. END;
  2044. END;
  2045. result.SetType(type);
  2046. resolvedExpression := result
  2047. END VisitUnaryExpression;
  2048. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2049. VAR
  2050. result: SyntaxTree.Expression;
  2051. array: SyntaxTree.MathArrayExpression;
  2052. value: SyntaxTree.MathArrayValue;
  2053. isValue: BOOLEAN;
  2054. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2055. BEGIN
  2056. type := type.resolved;
  2057. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2058. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2059. END;
  2060. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2061. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2062. END;
  2063. RETURN type
  2064. END BaseType;
  2065. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2066. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2067. BEGIN
  2068. numberElements := x.elements.Length();
  2069. FOR i := 0 TO numberElements-1 DO
  2070. expression := x.elements.GetExpression(i);
  2071. IF expression IS SyntaxTree.MathArrayExpression THEN
  2072. array := SyntaxTree.NewMathArrayExpression(position);
  2073. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2074. expression := array;
  2075. ELSE
  2076. position := expression.position;
  2077. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2078. isValue := isValue & (expression.resolved # NIL);
  2079. END;
  2080. to.elements.AddExpression(expression);
  2081. END;
  2082. END RecursivelyConvert;
  2083. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2084. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2085. arrayType: SyntaxTree.MathArrayType;
  2086. BEGIN
  2087. numberElements := x.elements.Length();
  2088. baseType := NIL;
  2089. gsize := 0;
  2090. FOR i := 0 TO numberElements-1 DO
  2091. expression := x.elements.GetExpression(i);
  2092. IF expression IS SyntaxTree.MathArrayExpression THEN
  2093. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2094. IF i=0 THEN
  2095. gsize := size;
  2096. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2097. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2098. ELSE expression.SetType(baseType)
  2099. END;
  2100. ELSIF baseType = NIL THEN baseType := type;
  2101. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2102. END;
  2103. END;
  2104. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2105. arrayType.SetArrayBase(baseType);
  2106. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2107. RETURN ResolveType(arrayType);
  2108. END RecursivelySetType;
  2109. BEGIN
  2110. result := SyntaxTree.invalidExpression;
  2111. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2112. result := expression (* do not convert *)
  2113. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2114. isValue := TRUE;
  2115. type := BaseType(type);
  2116. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2117. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2118. value := SyntaxTree.NewMathArrayValue(array.position);
  2119. value.SetArray(array);
  2120. value.SetType(RecursivelySetType(array));
  2121. result := value;
  2122. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2123. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2124. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2125. IF result = NIL THEN
  2126. result := SyntaxTree.invalidExpression;
  2127. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2128. IF VerboseErrorMessage THEN
  2129. Printout.Info("expression",expression);
  2130. Printout.Info("type",type);
  2131. END;
  2132. END;
  2133. END;
  2134. RETURN result
  2135. END MathArrayConversion;
  2136. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2137. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2138. BEGIN
  2139. result := expression; type := type.resolved;
  2140. IF (expression IS SyntaxTree.IntegerValue) THEN
  2141. int := expression(SyntaxTree.IntegerValue).hvalue;
  2142. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2143. int := Global.ConvertSigned(int,system.SizeOf(type));
  2144. result := SyntaxTree.NewIntegerValue(position,int);
  2145. result.SetType(type);
  2146. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2147. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2148. result := SyntaxTree.NewIntegerValue(position,int);
  2149. result.SetType(type);
  2150. ELSIF (type IS SyntaxTree.FloatType) THEN
  2151. result := SyntaxTree.NewRealValue(expression.position,int);
  2152. result.SetType(type);
  2153. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2154. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2155. result.SetType(type);
  2156. ELSIF (type IS SyntaxTree.SetType) THEN
  2157. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2158. result.SetType(type);
  2159. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2160. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2161. result.SetType(type);
  2162. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2163. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2164. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2165. END;
  2166. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2167. result.SetType(type);
  2168. ELSIF (type IS SyntaxTree.PortType) THEN
  2169. result := ConvertValue(position, expression, system.integerType);
  2170. ELSE
  2171. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2172. result := SyntaxTree.invalidExpression;
  2173. IF VerboseErrorMessage THEN
  2174. Printout.Info("expression",expression);
  2175. Printout.Info("type",type);
  2176. END;
  2177. END;
  2178. ELSIF IsRealValue(expression,real) THEN
  2179. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2180. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2181. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2182. result.SetType(type);
  2183. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2184. int := ENTIERH(real);
  2185. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2186. result.SetType(type);
  2187. ELSIF (type IS SyntaxTree.FloatType) THEN
  2188. result := SyntaxTree.NewRealValue(position,real);
  2189. result.SetType(type);
  2190. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2191. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2192. result.SetType(type);
  2193. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2194. ELSIF (type IS SyntaxTree.PortType) THEN
  2195. result := ConvertValue(position, expression, system.integerType);
  2196. ELSE
  2197. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2198. result := SyntaxTree.invalidExpression;
  2199. END
  2200. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2201. IF (type IS SyntaxTree.ComplexType) THEN
  2202. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2203. result.SetType(type);
  2204. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2205. ELSE
  2206. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2207. result := SyntaxTree.invalidExpression;
  2208. END
  2209. ELSIF IsSetValue(expression,set) THEN
  2210. IF (type IS SyntaxTree.IntegerType) THEN
  2211. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2212. result.SetType(type);
  2213. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2214. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2215. result.SetType(type);
  2216. ELSIF (type IS SyntaxTree.PortType) THEN
  2217. result := ConvertValue(position, expression, system.integerType);
  2218. ELSE
  2219. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2220. result := SyntaxTree.invalidExpression;
  2221. END;
  2222. ELSIF IsStringValue(expression,string) THEN
  2223. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2224. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2225. result.SetType(type);
  2226. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2227. ELSE
  2228. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2229. result := SyntaxTree.invalidExpression;
  2230. END;
  2231. ELSIF IsCharacterValue(expression,char) THEN
  2232. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2233. string[0] := char; string[1] := 0X;
  2234. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2235. result := SyntaxTree.NewStringValue(expression.position,string);
  2236. result.SetType(type);
  2237. ELSIF (type IS SyntaxTree.ByteType) THEN
  2238. (* do not simply set the new type as this could invalidate types of constants *)
  2239. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2240. result.SetType(type)
  2241. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2242. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2243. result.SetType(type);
  2244. ELSIF (type IS SyntaxTree.SetType) THEN
  2245. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2246. result.SetType(type);
  2247. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2248. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2249. result.SetType(type);
  2250. ELSIF (type IS SyntaxTree.PortType) THEN
  2251. result := ConvertValue(position, expression, system.integerType);
  2252. ELSE
  2253. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2254. result := SyntaxTree.invalidExpression;
  2255. END;
  2256. ELSIF expression IS SyntaxTree.NilValue THEN
  2257. IF type IS SyntaxTree.AddressType THEN
  2258. result := SyntaxTree.NewIntegerValue(position,0);
  2259. result.SetType(type);
  2260. ELSE
  2261. result := expression;
  2262. END;
  2263. (* nothing to be done *)
  2264. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2265. result := MathArrayConversion(position, expression,type);
  2266. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2267. int := expression(SyntaxTree.EnumerationValue).value;
  2268. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2269. int := Global.ConvertSigned(int,system.SizeOf(type));
  2270. result := SyntaxTree.NewIntegerValue(position,int);
  2271. result.SetType(type);
  2272. ELSE
  2273. result := expression;
  2274. END;
  2275. (* nothing to be done *)
  2276. ELSE
  2277. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2278. IF VerboseErrorMessage THEN
  2279. Printout.Info("expression",expression);
  2280. Printout.Info("type",type);
  2281. END;
  2282. result := SyntaxTree.invalidExpression;
  2283. END;
  2284. RETURN result
  2285. END ConvertValue;
  2286. (**
  2287. return a conversion of an expression to a given type
  2288. - if expression is already of same type then return expression
  2289. - if incompatible conversion then report error and return invalidExpression
  2290. **)
  2291. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2292. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2293. BEGIN
  2294. type := type.resolved;
  2295. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2296. result := expression;
  2297. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2298. ELSIF expression = NIL THEN (* NIL expression *)
  2299. ELSIF expression.type = NIL THEN
  2300. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2301. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2302. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2303. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2304. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2305. ELSIF expression.resolved # NIL THEN (* value *)
  2306. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2307. IF value IS SyntaxTree.Value THEN
  2308. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2309. result.SetResolved(value(SyntaxTree.Value));
  2310. result.SetType(value.type);
  2311. ELSE
  2312. result := value
  2313. END;
  2314. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2315. expressionList := SyntaxTree.NewExpressionList();
  2316. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2317. typeDeclaration.SetDeclaredType(type);
  2318. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2319. typeSymbol.SetType(typeDeclaration.type);
  2320. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2321. expressionList.AddExpression(expression);
  2322. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2323. result.SetType(type);
  2324. ELSIF IsArrayStructuredObjectType(type) THEN
  2325. (* no type can be converted to an array-structured object type *)
  2326. HALT(100)
  2327. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2328. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2329. result := MathArrayConversion(position, expression,type);
  2330. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2331. expression := ConvertToMathArray(expression);
  2332. type := MathArrayStructureOfType(type);
  2333. result := MathArrayConversion(position, expression, type)
  2334. ELSE
  2335. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2336. END;
  2337. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2338. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2339. OR ~(type IS SyntaxTree.ArrayType) THEN
  2340. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2341. END;
  2342. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2343. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2344. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2345. (*skip, no conversion*)
  2346. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2347. (* skip, no conversion *)
  2348. ELSE
  2349. ASSERT(~(type IS SyntaxTree.RangeType));
  2350. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2351. ASSERT(type # NIL);
  2352. END;
  2353. RETURN result
  2354. END NewConversion;
  2355. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2356. BEGIN
  2357. IF CompatibleTo(system,expression.type, type) THEN
  2358. RETURN NewConversion(position, expression, type, NIL);
  2359. ELSE
  2360. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2361. RETURN SyntaxTree.invalidExpression
  2362. END;
  2363. END CompatibleConversion;
  2364. (**
  2365. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2366. **)
  2367. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2368. VAR leftType,rightType: SyntaxTree.Type;
  2369. BEGIN
  2370. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2371. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2372. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2373. (* error already handled *)
  2374. ELSE
  2375. leftType := left.type.resolved; rightType := right.type.resolved;
  2376. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2377. right := NewConversion(right.position, right, leftType, NIL);
  2378. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2379. left := NewConversion(left.position,left,rightType,NIL);
  2380. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2381. right := NewConversion(right.position, right, leftType, NIL);
  2382. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2383. left := NewConversion(left.position,left,rightType,NIL);
  2384. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2385. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2386. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2387. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2388. ELSIF
  2389. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2390. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2391. (* must be the case LONGREAL / COMPLEX ) *)
  2392. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2393. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2394. ELSE
  2395. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2396. END;
  2397. END;
  2398. END ConvertOperands;
  2399. (** find and return best operator matching to parameter list (nil, if none)
  2400. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2401. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2402. **)
  2403. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2404. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2405. identifier: SyntaxTree.Identifier;
  2406. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2407. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2408. BEGIN
  2409. operator := scope.firstOperator;
  2410. WHILE(operator # NIL) DO
  2411. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2412. procedureType := operator.type(SyntaxTree.ProcedureType);
  2413. distance := Distance(system, procedureType,actualParameters);
  2414. IF (distance < Infinity) THEN
  2415. IF returnType # NIL THEN
  2416. IF procedureType.returnType = NIL THEN
  2417. distance := Infinity
  2418. ELSE
  2419. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2420. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2421. END;
  2422. END;
  2423. END;
  2424. (*
  2425. IF distance < Infinity THEN
  2426. TRACE(distance, operator);
  2427. Printout.Info("potential operator",operator);
  2428. ELSE
  2429. Printout.Info("operator not possible",operator);
  2430. END;
  2431. *)
  2432. IF distance < bestDistance THEN
  2433. bestDistance := distance;
  2434. bestOperator := operator;
  2435. END;
  2436. END;
  2437. operator := operator.nextOperator;
  2438. END;
  2439. (*
  2440. Printout.Info("taken operator",bestOperator);
  2441. *)
  2442. END FindInScope;
  2443. BEGIN
  2444. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2445. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2446. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2447. import := currentScope.ownerModule.moduleScope.firstImport;
  2448. WHILE (bestDistance > 0) & (import # NIL) DO
  2449. IF import.module # NIL THEN
  2450. identifier := Global.GetIdentifier(operator,import.module.case);
  2451. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2452. END;
  2453. import := import.nextImport;
  2454. END;
  2455. RETURN bestOperator
  2456. END FindOperator;
  2457. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2458. BEGIN
  2459. currentScope := scope;
  2460. END SetCurrentScope;
  2461. (**
  2462. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2463. - handle LEN and DIM operator for array-structured object types
  2464. - find operator, if found then
  2465. - if in other module then add import designator
  2466. - create symbol designator for operator
  2467. - if error then return invalidExpression, if no operator then return NIL
  2468. **)
  2469. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2470. VAR
  2471. operator: SyntaxTree.Operator;
  2472. import: SyntaxTree.Import;
  2473. expression, result: SyntaxTree.Expression;
  2474. designator: SyntaxTree.Designator;
  2475. actualParameters, tempList: SyntaxTree.ExpressionList;
  2476. recordType: SyntaxTree.RecordType;
  2477. castReturnType : SyntaxTree.MathArrayType;
  2478. BEGIN
  2479. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2480. result := SyntaxTree.invalidExpression
  2481. ELSIF leftExpression = NIL THEN
  2482. result := NIL
  2483. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2484. (* LEN or DIM operator on array-structured object type *)
  2485. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2486. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2487. IF recordType.arrayAccessOperators.len = NIL THEN
  2488. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2489. result := SyntaxTree.invalidExpression
  2490. ELSE
  2491. ASSERT(leftExpression IS SyntaxTree.Designator);
  2492. designator := leftExpression(SyntaxTree.Designator);
  2493. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2494. ASSERT(expression IS SyntaxTree.Designator);
  2495. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2496. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2497. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2498. result := designator
  2499. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2500. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2501. tempList := SyntaxTree.NewExpressionList();
  2502. tempList.AddExpression(rightExpression);
  2503. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2504. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2505. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2506. tempList := SyntaxTree.NewExpressionList();
  2507. tempList.AddExpression(designator);
  2508. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2509. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2510. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2511. END
  2512. END;
  2513. ELSE
  2514. (* import OCArrayBase if needed *)
  2515. IF ~arrayBaseImported THEN
  2516. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2517. (* operators on complex numbers *)
  2518. ImportModule(Global.ArrayBaseName,position);
  2519. arrayBaseImported := TRUE;
  2520. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2521. IF op = Global.Dim THEN
  2522. (* not existing in OCArrayBase *)
  2523. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2524. (* not existing in OCArrayBase *)
  2525. ELSE
  2526. ImportModule(Global.ArrayBaseName,position);
  2527. arrayBaseImported := TRUE;
  2528. END
  2529. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2530. ImportModule(Global.ArrayBaseName,position);
  2531. arrayBaseImported := TRUE
  2532. END;
  2533. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2534. (* LEN(RANGE) *)
  2535. ImportModule(Global.ArrayBaseName,position);
  2536. arrayBaseImported := TRUE;
  2537. END;
  2538. END;
  2539. actualParameters := SyntaxTree.NewExpressionList();
  2540. actualParameters.AddExpression(leftExpression);
  2541. IF rightExpression # NIL THEN
  2542. actualParameters.AddExpression(rightExpression)
  2543. END;
  2544. operator := FindOperator(system,op,actualParameters,resultType);
  2545. IF operator # NIL THEN
  2546. designator := NIL;
  2547. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2548. import := currentScope.ownerModule.moduleScope.firstImport;
  2549. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2550. import := import.nextImport;
  2551. END;
  2552. expression := NewSymbolDesignator(position,NIL,import);
  2553. designator := expression(SyntaxTree.Designator);
  2554. END;
  2555. expression := NewSymbolDesignator(position,designator,operator);
  2556. designator := expression(SyntaxTree.Designator);
  2557. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2558. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2559. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2560. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2561. result.SetType(castReturnType);
  2562. END;
  2563. ELSE
  2564. result := NIL;
  2565. END;
  2566. END;
  2567. RETURN result
  2568. END NewOperatorCall;
  2569. (** check and resolve binary expression **)
  2570. (*! clean up *)
  2571. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2572. VAR left,right,result: SyntaxTree.Expression;
  2573. leftType, rightType: SyntaxTree.Type;
  2574. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2575. cl,cr: CHAR;
  2576. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2577. type: SyntaxTree.Type;
  2578. value: SyntaxTree.Value;
  2579. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2580. integerConstantFolding: BOOLEAN;
  2581. list: SyntaxTree.ExpressionList;
  2582. PROCEDURE NewBool(v: BOOLEAN);
  2583. BEGIN
  2584. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2585. value.SetType(system.booleanType);
  2586. result.SetResolved(value);
  2587. type := system.booleanType
  2588. END NewBool;
  2589. PROCEDURE NewSet(v: SET);
  2590. BEGIN
  2591. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2592. value.SetType(system.setType);
  2593. result.SetResolved(value);
  2594. type := system.setType;
  2595. END NewSet;
  2596. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2597. BEGIN
  2598. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2599. (* type cast to "larger" type only if the value is still in the range *)
  2600. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2601. value.SetType(t);
  2602. END;
  2603. result.SetResolved(value);
  2604. type := value.type;
  2605. END NewInteger;
  2606. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2607. BEGIN
  2608. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2609. value.SetType(t);
  2610. result.SetResolved(value);
  2611. type := t;
  2612. END NewReal;
  2613. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2614. BEGIN
  2615. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2616. value.SetType(t);
  2617. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2618. result.SetResolved(value);
  2619. type := t;
  2620. END NewComplex;
  2621. BEGIN
  2622. type := SyntaxTree.invalidType;
  2623. left := ResolveExpression(binaryExpression.left);
  2624. right := ResolveExpression(binaryExpression.right);
  2625. binaryExpression.SetLeft(left);
  2626. binaryExpression.SetRight(right);
  2627. result := binaryExpression;
  2628. operator := binaryExpression.operator;
  2629. IF ~system.operatorDefined[operator] THEN
  2630. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2631. result := SyntaxTree.invalidExpression;
  2632. RETURN
  2633. END;
  2634. IF left.type = NIL THEN
  2635. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2636. result := SyntaxTree.invalidExpression;
  2637. RETURN;
  2638. END;
  2639. IF right.type = NIL THEN
  2640. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2641. result := SyntaxTree.invalidExpression;
  2642. RETURN;
  2643. END;
  2644. leftType := left.type.resolved; rightType := right.type.resolved;
  2645. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2646. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2647. END;
  2648. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2649. list := SyntaxTree.NewExpressionList();
  2650. list.AddExpression(right);
  2651. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2652. END;
  2653. IF operatorCall # NIL THEN
  2654. result := operatorCall;
  2655. type := operatorCall.type;
  2656. (* admissible operators:
  2657. Times, Plus, Minus numeric numeric numeric
  2658. set set set
  2659. Slash numeric numeric real /complex
  2660. set set set
  2661. Div , Mod integer integer integer
  2662. And, Or bool bool bool
  2663. Equal, Unequal basic basic bool
  2664. pointer pointer bool
  2665. object object bool
  2666. record record bool
  2667. string string bool
  2668. enumerator enumerator bool
  2669. Less, LessEqual,
  2670. Greater, GreaterEqual integer/real integer/real bool
  2671. enumerator enumerator bool
  2672. In integer set bool
  2673. Is pointer type bool
  2674. object type bool
  2675. record type bool
  2676. Upto: special abbreviation for a..b
  2677. *)
  2678. ELSIF (left.type = NIL) THEN
  2679. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2680. D.Str("nil type in "); D.Type(left); D.Ln;
  2681. result := SyntaxTree.invalidExpression;
  2682. ELSIF (right.type = NIL) THEN
  2683. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2684. result := SyntaxTree.invalidExpression;
  2685. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2686. result := SyntaxTree.invalidExpression;
  2687. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2688. HALT(100);
  2689. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2690. type := system.booleanType;
  2691. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2692. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2693. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2694. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2695. IF VerboseErrorMessage THEN
  2696. Printout.Info("left",left);
  2697. Printout.Info("right",right);
  2698. END;
  2699. ELSIF IsUnsafePointer(left.type) THEN
  2700. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2701. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2702. NewBool(TRUE)
  2703. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2704. NewBool(TRUE);
  2705. ELSIF IsUnextensibleRecord(left) THEN
  2706. NewBool(FALSE)
  2707. END
  2708. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2709. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2710. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2711. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2712. ELSIF operator = Scanner.In THEN (* left IN right *)
  2713. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2714. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2715. NewBool(il IN sr);
  2716. ELSE
  2717. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2718. left := NewConversion(left.position, left, system.longintType,NIL);
  2719. binaryExpression.SetLeft(left)
  2720. END;
  2721. type := system.booleanType;
  2722. END
  2723. ELSE
  2724. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2725. END
  2726. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2727. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2728. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2729. END;
  2730. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2731. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2732. END
  2733. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2734. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2735. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2736. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2737. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2738. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2739. ConvertOperands(left, right);
  2740. binaryExpression.SetLeft(left);
  2741. binaryExpression.SetRight(right);
  2742. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2743. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2744. END;
  2745. type := system.booleanType;
  2746. ELSE
  2747. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2748. END
  2749. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2750. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2751. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2752. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2753. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2754. CASE operator OF
  2755. |Scanner.Equal: NewBool(strl^=strr^);
  2756. |Scanner.Unequal:NewBool(strl^#strr^);
  2757. |Scanner.Less: NewBool(strl^<strr^);
  2758. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2759. |Scanner.Greater: NewBool(strl^>strr^);
  2760. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2761. ELSE
  2762. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2763. END;
  2764. END;
  2765. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2766. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2767. type := system.booleanType
  2768. ELSE
  2769. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2770. END;
  2771. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2772. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2773. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2774. type := system.booleanType;
  2775. ELSE
  2776. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2777. END
  2778. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2779. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2780. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2781. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2782. type := system.booleanType
  2783. ELSE
  2784. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2785. END;
  2786. ELSE
  2787. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2788. END;
  2789. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2790. type := system.booleanType;
  2791. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2792. type := system.booleanType;
  2793. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2794. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2795. THEN
  2796. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2797. IF (leftType # rightType) THEN
  2798. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2799. ConvertOperands(left,right); (* operands must be of the same type here *)
  2800. END;
  2801. binaryExpression.SetLeft(left);
  2802. binaryExpression.SetRight(right);
  2803. leftType := left.type.resolved;
  2804. rightType := right.type.resolved;
  2805. END;
  2806. type := leftType;
  2807. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2808. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2809. IF VerboseErrorMessage THEN
  2810. Printout.Info("left",left);
  2811. Printout.Info("right",right);
  2812. END;
  2813. ELSIF IsIntegerType(leftType) THEN
  2814. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2815. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2816. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2817. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2818. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2819. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2820. END;
  2821. END;
  2822. (* constant folding *)
  2823. (* bootstrap64
  2824. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2825. CASE operator OF
  2826. |Scanner.Plus: NewInteger(il+ir,left.type);
  2827. |Scanner.Minus: NewInteger(il-ir,left.type);
  2828. |Scanner.Times: NewInteger(il*ir,left.type);
  2829. |Scanner.Slash:
  2830. IF ir # 0 THEN
  2831. NewReal(il/ir, system.realType);
  2832. END;
  2833. |Scanner.Mod:
  2834. IF ir > 0 THEN
  2835. NewInteger(il MOD ir,left.type);
  2836. END;
  2837. |Scanner.Div:
  2838. IF ir > 0 THEN
  2839. NewInteger(il DIV ir,left.type);
  2840. END;
  2841. |Scanner.Equal: NewBool(il=ir);
  2842. |Scanner.Unequal:NewBool(il#ir);
  2843. |Scanner.Less: NewBool(il<ir);
  2844. |Scanner.LessEqual: NewBool(il<=ir);
  2845. |Scanner.Greater: NewBool(il>ir);
  2846. |Scanner.GreaterEqual: NewBool(il>=ir);
  2847. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2848. END;
  2849. ELS*)
  2850. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2851. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2852. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2853. CASE operator OF
  2854. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2855. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2856. |Scanner.Times: NewInteger(hl*hr,left.type);
  2857. |Scanner.Slash:
  2858. IF hr = 0 THEN
  2859. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2860. ELSE
  2861. IF type.sizeInBits = 64 THEN
  2862. NewReal(hl/hr,system.longrealType);
  2863. ELSE
  2864. NewReal(hl/hr,system.realType)
  2865. END
  2866. END;
  2867. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2868. |Scanner.Mod:
  2869. IF hr = 0 THEN
  2870. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2871. ELSE
  2872. NewInteger(hl MOD hr, left.type);
  2873. (* bootstrap64
  2874. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2875. *)
  2876. END;
  2877. |Scanner.Div:
  2878. IF hr = 0 THEN
  2879. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2880. ELSE
  2881. NewInteger(hl DIV hr, left.type);
  2882. (* bootstrap64
  2883. NewInteger(Machine.DivH(hl,hr),left.type);
  2884. *)
  2885. END;
  2886. (* *)
  2887. |Scanner.Equal: NewBool(hl=hr);
  2888. |Scanner.Unequal: NewBool(hl#hr);
  2889. |Scanner.Less: NewBool(hl<hr);
  2890. |Scanner.LessEqual: NewBool(hl<=hr);
  2891. |Scanner.Greater: NewBool(hl>hr);
  2892. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2893. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2894. END;
  2895. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2896. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2897. type := left.type
  2898. ELSIF (operator = Scanner.Slash) THEN
  2899. left := NewConversion(left.position,left,system.realType,NIL);
  2900. right := NewConversion(right.position,right,system.realType,NIL);
  2901. binaryExpression.SetLeft(left);
  2902. binaryExpression.SetRight(right);
  2903. type := system.realType
  2904. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2905. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2906. type := system.booleanType
  2907. ELSE
  2908. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2909. END;
  2910. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2911. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2912. CASE operator OF
  2913. |Scanner.Plus: NewReal(rl+rr,leftType);
  2914. |Scanner.Minus: NewReal(rl-rr,leftType);
  2915. |Scanner.Times:NewReal(rl*rr,leftType);
  2916. |Scanner.Slash:
  2917. IF rr = 0 THEN
  2918. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2919. ELSE
  2920. NewReal(rl/rr,leftType);
  2921. END
  2922. |Scanner.Equal: NewBool(rl=rr);
  2923. |Scanner.Unequal: NewBool(rl#rr);
  2924. |Scanner.Less: NewBool(rl<rr);
  2925. |Scanner.LessEqual: NewBool(rl<=rr);
  2926. |Scanner.Greater: NewBool(rl>rr);
  2927. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2928. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2929. END;
  2930. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2931. type := left.type
  2932. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2933. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2934. type := system.booleanType
  2935. ELSE
  2936. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2937. IF VerboseErrorMessage THEN
  2938. Printout.Info("left",left);
  2939. Printout.Info("right",right);
  2940. END;
  2941. END;
  2942. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2943. CASE operator OF
  2944. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2945. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2946. ELSE
  2947. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2948. IF VerboseErrorMessage THEN
  2949. Printout.Info("left", left);
  2950. Printout.Info("right", right)
  2951. END;
  2952. END;
  2953. IF ~error THEN
  2954. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2955. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2956. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2957. (* do constant folding *)
  2958. CASE operator OF
  2959. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2960. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2961. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  2962. |Scanner.Slash:
  2963. divisor := c * c + d * d;
  2964. ASSERT(divisor # 0);
  2965. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  2966. |Scanner.Equal: NewBool((a = c) & (b = d))
  2967. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  2968. END
  2969. END
  2970. END
  2971. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  2972. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  2973. CASE operator OF
  2974. |Scanner.And: NewBool(bl & br);
  2975. |Scanner.Or: NewBool(bl OR br);
  2976. |Scanner.Equal: NewBool(bl = br);
  2977. |Scanner.Unequal: NewBool(bl # br);
  2978. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  2979. END;
  2980. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  2981. type := system.booleanType
  2982. ELSE
  2983. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  2984. END;
  2985. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  2986. (* constant folding *)
  2987. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  2988. IF operator = Scanner.Equal THEN
  2989. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  2990. ELSIF operator = Scanner.Unequal THEN
  2991. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  2992. END;
  2993. END;
  2994. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2995. type := system.booleanType;
  2996. ELSE
  2997. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  2998. END;
  2999. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3000. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3001. CASE operator OF
  3002. |Scanner.Plus: NewSet(sl + sr);
  3003. |Scanner.Minus: NewSet(sl - sr);
  3004. |Scanner.Times: NewSet(sl * sr);
  3005. |Scanner.Slash: NewSet(sl / sr);
  3006. |Scanner.Equal: NewBool(sl=sr);
  3007. |Scanner.Unequal: NewBool(sl#sr);
  3008. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3009. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3010. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3011. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3012. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3013. END;
  3014. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3015. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3016. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3017. THEN
  3018. type := system.booleanType
  3019. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3020. type := left.type
  3021. ELSE
  3022. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3023. END;
  3024. ELSIF IsCharacterType(left.type) THEN
  3025. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3026. CASE operator OF
  3027. |Scanner.Equal: NewBool(cl=cr);
  3028. |Scanner.Unequal: NewBool(cl#cr);
  3029. |Scanner.Less: NewBool(cl<cr);
  3030. |Scanner.LessEqual: NewBool(cl<=cr);
  3031. |Scanner.Greater: NewBool(cl>cr);
  3032. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3033. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3034. END;
  3035. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3036. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3037. type := system.booleanType
  3038. ELSE
  3039. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3040. END;
  3041. ELSE
  3042. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3043. END;
  3044. ELSE
  3045. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3046. END;
  3047. IF type = SyntaxTree.invalidType THEN
  3048. result := SyntaxTree.invalidExpression
  3049. ELSE
  3050. result.SetType(type)
  3051. END;
  3052. resolvedExpression := result
  3053. END VisitBinaryExpression;
  3054. (** resolve a range expression of the from <<first .. last BY step>>
  3055. - depending on the context different things are checked:
  3056. ArrayIndex:
  3057. - components must be integers
  3058. - replace missing lower bound with 0
  3059. - replace missing upper bound with MAX(LONGINT)
  3060. - replace missing step size with 1
  3061. SetElement:
  3062. - components must be integers
  3063. - replace missing lower bound with 0
  3064. - replace missing upper bound with MAX(SET)
  3065. - must not have step size
  3066. CaseGuard:
  3067. - components must be constant
  3068. - components must be integers or characters
  3069. - must have lower and upper bound present
  3070. - components are made compatible
  3071. - must not have step size
  3072. - if error: return invalidExpression
  3073. **)
  3074. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3075. VAR
  3076. hasError: BOOLEAN;
  3077. first, last, step: SyntaxTree.Expression;
  3078. BEGIN
  3079. hasError := FALSE;
  3080. first := x.first;
  3081. last := x.last;
  3082. step := x.step;
  3083. (* check lower bound *)
  3084. IF x.context = SyntaxTree.CaseGuard THEN
  3085. IF first = NIL THEN
  3086. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3087. hasError := TRUE
  3088. ELSE
  3089. first := ResolveExpression(first);
  3090. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3091. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3092. hasError := TRUE
  3093. ELSE
  3094. IF first IS SyntaxTree.StringValue THEN
  3095. (* add conversion from string to character *)
  3096. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3097. END
  3098. END;
  3099. (* check if expression is constant *)
  3100. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3101. (* error already reported *)
  3102. hasError := TRUE
  3103. END
  3104. END
  3105. ELSE (* ArrayIndex, SetElement *)
  3106. IF first = NIL THEN
  3107. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3108. END;
  3109. first := ResolveExpression(first);
  3110. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3111. first := NewConversion(first.position, first, system.longintType, NIL)
  3112. ELSE
  3113. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3114. hasError := TRUE
  3115. END
  3116. END;
  3117. (* check upper bound *)
  3118. IF x.context = SyntaxTree.CaseGuard THEN
  3119. IF last = NIL THEN
  3120. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3121. hasError := TRUE
  3122. ELSE
  3123. last := ResolveExpression(last);
  3124. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3125. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3126. hasError := TRUE
  3127. ELSE
  3128. IF last IS SyntaxTree.StringValue THEN
  3129. (* add conversion from string to character *)
  3130. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3131. END
  3132. END;
  3133. (* check if expression is constant *)
  3134. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3135. (* error already reported *)
  3136. hasError := TRUE
  3137. ELSE
  3138. (* try to make lower and upper bound compatible *)
  3139. ConvertOperands(first, last);
  3140. IF first.type.resolved # last.type.resolved THEN
  3141. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3142. hasError := TRUE
  3143. END
  3144. END
  3145. END
  3146. ELSE (* ArrayIndex, SetElement *)
  3147. IF last = NIL THEN
  3148. IF x.context = SyntaxTree.ArrayIndex THEN
  3149. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3150. ELSE
  3151. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3152. END
  3153. END;
  3154. last := ResolveExpression(last);
  3155. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3156. last := NewConversion(last.position, last, system.longintType, NIL)
  3157. ELSE
  3158. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3159. hasError := TRUE
  3160. END
  3161. END;
  3162. (* check step size *)
  3163. IF x.context = SyntaxTree.ArrayIndex THEN
  3164. IF step = NIL THEN
  3165. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3166. END;
  3167. step := ResolveExpression(step);
  3168. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3169. step := NewConversion(step.position, step, system.longintType, NIL)
  3170. ELSE
  3171. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3172. hasError := TRUE
  3173. END
  3174. ELSE (* SetElement, CaseGuard *)
  3175. IF step # NIL THEN
  3176. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3177. hasError := TRUE
  3178. END
  3179. END;
  3180. IF hasError THEN
  3181. resolvedExpression := SyntaxTree.invalidExpression
  3182. ELSE
  3183. x.SetFirst(first);
  3184. x.SetLast(last);
  3185. x.SetStep(step);
  3186. x.SetType(system.rangeType);
  3187. resolvedExpression := x;
  3188. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3189. END
  3190. END VisitRangeExpression;
  3191. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3192. BEGIN
  3193. x.SetType(NIL);
  3194. resolvedExpression := x;
  3195. END VisitTensorRangeExpression;
  3196. (** resolve the expression d and return result as designator
  3197. - resolve expression
  3198. - if expression is a designator then return designator else error message and return invalidDesignator
  3199. **)
  3200. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3201. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3202. BEGIN
  3203. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3204. resolved := ResolveExpression(d);
  3205. IF resolved = SyntaxTree.invalidExpression THEN
  3206. (* error should already have been reported *)
  3207. result := SyntaxTree.invalidDesignator;
  3208. ELSIF resolved IS SyntaxTree.Designator THEN
  3209. result := resolved(SyntaxTree.Designator);
  3210. ELSE
  3211. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3212. result := SyntaxTree.invalidDesignator;
  3213. END;
  3214. (* result.type might be nil. *)
  3215. RETURN result
  3216. END ResolveDesignator;
  3217. (**
  3218. symbol designator generated in this module
  3219. nothing to be resolved
  3220. **)
  3221. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3222. BEGIN
  3223. resolvedExpression := x;
  3224. END VisitSymbolDesignator;
  3225. (**
  3226. self designator generated in this module
  3227. nothing to be resolved
  3228. **)
  3229. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3230. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3231. BEGIN
  3232. (* check if in record scope *)
  3233. scope := currentScope;
  3234. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3235. scope := scope.outerScope;
  3236. END;
  3237. IF scope = NIL THEN (* in module scope *)
  3238. x.SetType(system.anyType);
  3239. ELSIF scope IS SyntaxTree.CellScope THEN
  3240. cell := scope(SyntaxTree.CellScope).ownerCell;
  3241. x.SetType(cell);
  3242. ELSE (* in record scope *)
  3243. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3244. IF (record # NIL) & (record.pointerType # NIL) THEN
  3245. type := ResolveType(record.pointerType);
  3246. x.SetType(type);
  3247. ELSE
  3248. x.SetType(record);
  3249. END;
  3250. END;
  3251. resolvedExpression := x;
  3252. END VisitSelfDesignator;
  3253. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3254. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3255. BEGIN
  3256. scope := currentScope;
  3257. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3258. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3259. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3260. returnType := procedureType.returnType;
  3261. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3262. THEN
  3263. x.SetType(returnType);
  3264. ELSE
  3265. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3266. x.SetType(SyntaxTree.invalidType);
  3267. END;
  3268. ELSE
  3269. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3270. x.SetType(SyntaxTree.invalidType);
  3271. END;
  3272. x.SetAssignable(TRUE);
  3273. resolvedExpression := x;
  3274. END VisitResultDesignator;
  3275. (**
  3276. return symbol designator as an expression
  3277. - if symbol is a constant then return the constant value expression
  3278. - else
  3279. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3280. - if symbol is a guarded variable then return a TypeGuardDesignator
  3281. - else return a symbol designator
  3282. **)
  3283. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3284. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3285. guardType: SyntaxTree.Type;
  3286. BEGIN
  3287. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3288. result := SyntaxTree.invalidExpression;
  3289. ASSERT(symbol # NIL);
  3290. (*
  3291. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3292. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3293. Error(position,Diagnostics.Invalid,"type not allowed here");
  3294. ELS *)
  3295. (* not needed any more as values are stored in the expression
  3296. IF symbol IS SyntaxTree.Constant THEN
  3297. result := symbol(SyntaxTree.Constant).value
  3298. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3299. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3300. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3301. ELSE
  3302. result := symbol(SyntaxTree.Constant).value
  3303. END;
  3304. ELSE
  3305. *)
  3306. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3307. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3308. THEN
  3309. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3310. IF IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects THEN
  3311. left := NewDereferenceDesignator(position,left);
  3312. left.SetHidden(TRUE);
  3313. END;
  3314. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3315. scope := currentScope;
  3316. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3317. scope := scope.outerScope;
  3318. END;
  3319. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3320. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3321. END;
  3322. END;
  3323. 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);
  3324. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3325. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3326. ELSE
  3327. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3328. END;
  3329. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3330. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3331. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3332. result.SetType(symbol.type);
  3333. result.SetAssignable(assignable);
  3334. symbol.MarkUsed;
  3335. IF symbol IS SyntaxTree.Constant THEN
  3336. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3337. END;
  3338. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3339. variableAccessed := TRUE
  3340. END;
  3341. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3342. IF GetGuard(symbol,guardType) THEN
  3343. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3344. END;
  3345. END;
  3346. ASSERT(result.type # NIL);
  3347. RETURN result
  3348. END NewSymbolDesignator;
  3349. (** check and resolve an identifier designator "identifier"
  3350. - if identifier = self then return SelfDesignator
  3351. - else find symbol in current scope
  3352. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3353. **)
  3354. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3355. VAR symbol: SyntaxTree.Symbol;
  3356. BEGIN
  3357. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3358. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3359. IF symbol # NIL THEN
  3360. ResolveSymbol(symbol);
  3361. ASSERT(symbol.type # NIL);
  3362. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3363. ELSE
  3364. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3365. IF VerboseErrorMessage THEN
  3366. Printout.Info("undeclared identifier designator",identifierDesignator);
  3367. END;
  3368. resolvedExpression := SyntaxTree.invalidDesignator;
  3369. END;
  3370. END VisitIdentifierDesignator;
  3371. (** check and resolve a selector designator of the form left.designator
  3372. - if left is a pointer type then do auto dereferenciation
  3373. - left denotes a search scope:
  3374. - if left type is import type then set search scope to respective module
  3375. - if left type is enumeration type then set search scope to respective enumeration scope
  3376. - elsif left type is record type then set search scope to record scope
  3377. - search symbol in computed scope
  3378. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3379. **)
  3380. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3381. VAR
  3382. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3383. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3384. BEGIN
  3385. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3386. left := ResolveDesignator(selectorDesignator.left);
  3387. result := SyntaxTree.invalidDesignator;
  3388. IF left # NIL THEN
  3389. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3390. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3391. END;
  3392. scope := NIL;
  3393. IF left.type = NIL THEN
  3394. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3395. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3396. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3397. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3398. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3399. module := symbol(SyntaxTree.Import).module;
  3400. IF module # NIL THEN
  3401. scope := module.moduleScope
  3402. ELSE
  3403. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3404. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3405. END;
  3406. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3407. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3408. ASSERT(scope # NIL)
  3409. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3410. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3411. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3412. IF type IS SyntaxTree.EnumerationType THEN
  3413. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3414. ELSE
  3415. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3416. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3417. END;
  3418. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3419. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3420. ELSE
  3421. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3422. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3423. END;
  3424. symbol := NIL;
  3425. IF scope # NIL THEN
  3426. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3427. IF symbol # NIL THEN
  3428. ResolveSymbol(symbol);
  3429. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3430. symbol.MarkUsed
  3431. ELSE
  3432. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3433. IF VerboseErrorMessage THEN
  3434. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3435. Printout.Info("scope", scope);
  3436. Printout.Info("left", left);
  3437. Printout.Info("undeclared identifier",selectorDesignator);
  3438. Printout.Info("left resolved designator",left);
  3439. END
  3440. END;
  3441. END;
  3442. END;
  3443. resolvedExpression := result;
  3444. END VisitSelectorDesignator;
  3445. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3446. VAR len,idx: LONGINT;
  3447. BEGIN
  3448. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3449. IF idx < 0 THEN
  3450. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3451. ELSE
  3452. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3453. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3454. END;
  3455. END;
  3456. END;
  3457. END IndexCheck;
  3458. (*
  3459. - if index designator has not type, use newBaseType as its type
  3460. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3461. - special rule: if static array of dynamic array occurs, make it all dynamic
  3462. index designator type: new base type: new index designator type:
  3463. NIL z z
  3464. ARRAY [x, y] z ARRAY [x, y] OF z
  3465. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3466. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3467. *)
  3468. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3469. VAR
  3470. mathArrayType: SyntaxTree.MathArrayType;
  3471. makeDynamic: BOOLEAN;
  3472. BEGIN
  3473. IF indexDesignator.type = NIL THEN
  3474. indexDesignator.SetType(newBaseType)
  3475. ELSE
  3476. (* index designator must be a of math array type *)
  3477. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3478. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3479. (* determine if all arrays have to be made dynamic *)
  3480. makeDynamic :=
  3481. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3482. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3483. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3484. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3485. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3486. END;
  3487. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3488. mathArrayType.SetArrayBase(newBaseType)
  3489. END
  3490. END SetIndexBaseType;
  3491. (** check and append index list element to index designator of math array
  3492. - check validity of single index or array range
  3493. - compute new type
  3494. - if range then create new array type (calculate length of resulting array)
  3495. - otherwise take sourceArray.arrayBase as new type
  3496. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3497. **)
  3498. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3499. VAR
  3500. targetArray: SyntaxTree.MathArrayType;
  3501. first, last, step: SyntaxTree.Expression;
  3502. firstValue, lastValue, stepValue, length: LONGINT;
  3503. rangeExpression: SyntaxTree.RangeExpression;
  3504. BEGIN
  3505. IF indexListItem.type = SyntaxTree.invalidType THEN
  3506. (* error already handled *)
  3507. indexDesignator.parameters.AddExpression(indexListItem)
  3508. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3509. indexDesignator.HasRange;
  3510. indexDesignator.HasTensorRange;
  3511. indexDesignator.parameters.AddExpression(indexListItem);
  3512. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3513. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3514. IndexCheck(indexListItem, sourceArray.length);
  3515. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3516. indexDesignator.parameters.AddExpression(indexListItem)
  3517. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3518. indexDesignator.HasRange;
  3519. (* if the range is given as an array range expression, check the validity of its components *)
  3520. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3521. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3522. first := rangeExpression.first;
  3523. last := rangeExpression.last;
  3524. step := rangeExpression.step;
  3525. (* perform static checks on range components *)
  3526. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3527. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3528. END;
  3529. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3530. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3531. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3532. END
  3533. END;
  3534. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3535. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3536. END;
  3537. (* add conversions to size type *)
  3538. (* TODO: needed? *)
  3539. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3540. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3541. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3542. END;
  3543. IF indexDesignator.hasTensorRange THEN
  3544. (* the index designator's base type is a tensor: leave it as is *)
  3545. ELSE
  3546. (* append a new math array to the index designator's base type *)
  3547. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3548. IF ~error THEN
  3549. (*
  3550. (* optimization: calculate length of target array for static ranges *)
  3551. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3552. IF IsStaticallyOpenRange(rangeExpression) THEN
  3553. (* range is open ('*'): reuse source array length as target array length *)
  3554. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3555. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3556. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3557. IF lastValue = MAX(LONGINT) THEN
  3558. IF IsIntegerValue(sourceArray.length, length) THEN
  3559. lastValue := length - 1;
  3560. isStaticTargetArrayLength := TRUE
  3561. ELSE
  3562. isStaticTargetArrayLength := FALSE
  3563. END
  3564. ELSE
  3565. isStaticTargetArrayLength := TRUE
  3566. END;
  3567. IF isStaticTargetArrayLength THEN
  3568. (* calculate static target array length *)
  3569. IF firstValue > lastValue THEN
  3570. length := 0
  3571. ELSE
  3572. length := 1 + lastValue - firstValue;
  3573. IF length MOD stepValue = 0 THEN
  3574. length := length DIV stepValue
  3575. ELSE
  3576. length := length DIV stepValue + 1
  3577. END
  3578. END;
  3579. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3580. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3581. ASSERT(targetArray.form = SyntaxTree.Static)
  3582. END
  3583. END
  3584. END
  3585. *)
  3586. END;
  3587. SetIndexBaseType(indexDesignator, targetArray)
  3588. END;
  3589. indexDesignator.parameters.AddExpression(indexListItem)
  3590. ELSE
  3591. Error(position, Diagnostics.Invalid,"invalid index list item");
  3592. END;
  3593. END AppendMathIndex;
  3594. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3595. VAR parameters: SyntaxTree.ExpressionList;
  3596. BEGIN
  3597. parameters := index.parameters;
  3598. IF (expression.type = NIL) THEN
  3599. Error(position,Diagnostics.Invalid,"invalid index");
  3600. ELSIF IsIntegerType(expression.type.resolved) THEN
  3601. IF over IS SyntaxTree.ArrayType THEN
  3602. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3603. ELSIF over IS SyntaxTree.StringType THEN
  3604. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3605. END;
  3606. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3607. parameters.AddExpression(expression);
  3608. ELSE
  3609. Error(position,Diagnostics.Invalid,"invalid index");
  3610. END;
  3611. END AppendIndex;
  3612. (** convert an expression to math array type
  3613. if expression is of math array type: return expression itself
  3614. if expression is of array-structured object type: return an index operator call on it
  3615. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3616. otherwise: return invalid expression
  3617. **)
  3618. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3619. VAR
  3620. result: SyntaxTree.Expression;
  3621. mathArrayType: SyntaxTree.MathArrayType;
  3622. BEGIN
  3623. IF expression.type = NIL THEN
  3624. result := SyntaxTree.invalidExpression
  3625. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3626. (* expression of math array type *)
  3627. result := expression
  3628. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3629. (* expression of array-structured object type *)
  3630. mathArrayType := MathArrayStructureOfType(expression.type);
  3631. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3632. ELSE
  3633. result := SyntaxTree.invalidExpression
  3634. END;
  3635. RETURN result
  3636. END ConvertToMathArray;
  3637. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3638. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3639. VAR
  3640. result: SyntaxTree.ExpressionList;
  3641. i: LONGINT;
  3642. BEGIN
  3643. result := SyntaxTree.NewExpressionList();
  3644. FOR i := 1 TO itemCount DO
  3645. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3646. END;
  3647. RETURN result
  3648. END ListOfOpenRanges;
  3649. (** create a procedure call designator for an index operator call on an array-structured object type
  3650. - use given index list as actual parameters
  3651. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3652. **)
  3653. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3654. VAR
  3655. operator: SyntaxTree.Operator;
  3656. expression: SyntaxTree.Expression;
  3657. actualParameters, tempList: SyntaxTree.ExpressionList;
  3658. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3659. result, tempDesignator: SyntaxTree.Designator;
  3660. recordType: SyntaxTree.RecordType;
  3661. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3662. i, hashValue, indexListSize, indexListKind: LONGINT;
  3663. castReturnType: SyntaxTree.MathArrayType;
  3664. BEGIN
  3665. ASSERT(IsArrayStructuredObjectType(left.type));
  3666. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3667. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3668. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3669. indexListSize := indexList.Length();
  3670. indexListKind := 0;
  3671. containsNonRange := FALSE;
  3672. FOR i := 0 TO indexList.Length() - 1 DO
  3673. indexListKind := indexListKind * 2;
  3674. expression := indexList.GetExpression(i);
  3675. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3676. INC(indexListKind)
  3677. ELSE
  3678. containsNonRange := TRUE
  3679. END
  3680. END;
  3681. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3682. (* select applicable index operator
  3683. - try to look up optimal index operator
  3684. - if not present, use operator on ranges
  3685. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3686. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3687. *)
  3688. usesGeneralOperator := FALSE;
  3689. IF rhs # NIL THEN
  3690. (* write operator *)
  3691. IF hashValue = -1 THEN
  3692. operator := NIL
  3693. ELSE
  3694. operator := recordType.arrayAccessOperators.write[hashValue];
  3695. END;
  3696. IF operator = NIL THEN
  3697. usesPureRangeOperator := TRUE;
  3698. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3699. operator := recordType.arrayAccessOperators.generalWrite;
  3700. usesGeneralOperator := TRUE
  3701. ELSE
  3702. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3703. operator := recordType.arrayAccessOperators.write[hashValue];
  3704. END
  3705. END
  3706. ELSE
  3707. (* read operator *)
  3708. IF hashValue = -1 THEN
  3709. operator := NIL
  3710. ELSE
  3711. operator := recordType.arrayAccessOperators.read[hashValue];
  3712. END;
  3713. IF operator = NIL THEN
  3714. usesPureRangeOperator := TRUE;
  3715. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3716. operator := recordType.arrayAccessOperators.generalRead;
  3717. usesGeneralOperator := TRUE
  3718. ELSE
  3719. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3720. operator := recordType.arrayAccessOperators.read[hashValue];
  3721. END
  3722. END
  3723. END;
  3724. IF operator = NIL THEN
  3725. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3726. result := SyntaxTree.invalidDesignator;
  3727. ELSE
  3728. (* determine if reshaping is needed *)
  3729. needsReshaping := containsNonRange & usesPureRangeOperator;
  3730. (* import OCArrayBase if reshaping is needed *)
  3731. IF needsReshaping & ~arrayBaseImported THEN
  3732. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3733. arrayBaseImported := TRUE
  3734. END;
  3735. (* add the index list item to the list of actual parameters
  3736. - for general operators: add a single inline array containing the index list items as parameter
  3737. - otherwise: add all index list items as individual parameters
  3738. *)
  3739. actualParameters := SyntaxTree.NewExpressionList();
  3740. IF usesGeneralOperator THEN
  3741. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3742. END;
  3743. FOR i := 0 TO indexListSize - 1 DO
  3744. expression := indexList.GetExpression(i);
  3745. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3746. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3747. tempList := SyntaxTree.NewExpressionList();
  3748. tempList.AddExpression(expression);
  3749. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3750. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3751. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3752. END;
  3753. IF usesGeneralOperator THEN
  3754. tempMathArrayExpression.elements.AddExpression(expression);
  3755. ELSE
  3756. actualParameters.AddExpression(expression)
  3757. END
  3758. END;
  3759. IF usesGeneralOperator THEN
  3760. actualParameters.AddExpression(tempMathArrayExpression)
  3761. END;
  3762. IF rhs # NIL THEN
  3763. (* add actual parameter for RHS *)
  3764. IF needsReshaping THEN
  3765. (* reshape using OCArrayBase.ExpandDimensions *)
  3766. tempList := SyntaxTree.NewExpressionList();
  3767. (* source array *)
  3768. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3769. tempList.AddExpression(rhs);
  3770. ELSE
  3771. (* convert scalar to one-dimensional array *)
  3772. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3773. tempMathArrayExpression.elements.AddExpression(rhs);
  3774. tempList.AddExpression(tempMathArrayExpression)
  3775. END;
  3776. (* list of kept dimensions *)
  3777. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3778. FOR i := 0 TO indexListSize - 1 DO
  3779. expression := indexList.GetExpression(i);
  3780. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3781. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3782. ELSE
  3783. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3784. END
  3785. END;
  3786. tempList.AddExpression(tempMathArrayExpression);
  3787. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3788. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3789. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3790. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3791. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3792. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3793. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3794. expression.SetType(castReturnType);
  3795. ELSE
  3796. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3797. END;
  3798. actualParameters.AddExpression(expression)
  3799. ELSE
  3800. actualParameters.AddExpression(rhs)
  3801. END
  3802. END;
  3803. (* add dereference operator and create procedure call designator *)
  3804. ASSERT(left IS SyntaxTree.Designator);
  3805. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3806. ASSERT(expression IS SyntaxTree.Designator);
  3807. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3808. IF (rhs = NIL) & needsReshaping THEN
  3809. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3810. tempList := SyntaxTree.NewExpressionList();
  3811. FOR i := 0 TO indexList.Length() - 1 DO
  3812. expression := indexList.GetExpression(i);
  3813. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3814. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3815. ELSE
  3816. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3817. END
  3818. END;
  3819. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3820. END;
  3821. IF rhs = NIL THEN
  3822. (* special rule: index read operator calls are considered to be assignable *)
  3823. result.SetAssignable(TRUE)
  3824. END;
  3825. (* put information about this index operator call into the resulting designator *)
  3826. result.SetRelatedAsot(left);
  3827. result.SetRelatedIndexList(indexList)
  3828. END;
  3829. RETURN result
  3830. END NewIndexOperatorCall;
  3831. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3832. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3833. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3834. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3835. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3836. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3837. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3838. CONST trace = FALSE;
  3839. BEGIN
  3840. IF trace THEN
  3841. FOR i := 0 TO actualParameters.Length()-1 DO
  3842. Printout.Info("par", actualParameters.GetExpression(i));
  3843. END;
  3844. END;
  3845. operator := scope.firstOperator;
  3846. WHILE(operator # NIL) DO
  3847. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3848. procedureType := operator.type(SyntaxTree.ProcedureType);
  3849. distance := Distance(system, procedureType,actualParameters);
  3850. IF trace THEN Printout.Info("check op ",operator) END;
  3851. IF distance < bestDistance THEN
  3852. IF trace THEN Printout.Info("taken op",operator) END;
  3853. bestDistance := distance;
  3854. bestOperator := operator;
  3855. END;
  3856. END;
  3857. operator := operator.nextOperator;
  3858. END;
  3859. END FindInScope;
  3860. BEGIN
  3861. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3862. IF oper = 0 THEN (* index *)
  3863. identifier := SyntaxTree.NewIdentifier("[]");
  3864. ELSE
  3865. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3866. END;
  3867. WHILE (recordType # NIL) DO
  3868. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3869. recordType := recordType.GetBaseRecord();
  3870. END;
  3871. RETURN bestOperator
  3872. END FindOperator;
  3873. BEGIN
  3874. type := left.type.resolved;
  3875. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3876. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3877. actualParameters := SyntaxTree.NewExpressionList();
  3878. IF parameters # NIL THEN
  3879. FOR i := 0 TO parameters.Length()-1 DO
  3880. expression := ResolveExpression(parameters.GetExpression(i));
  3881. actualParameters.AddExpression(expression);
  3882. END;
  3883. END;
  3884. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3885. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3886. IF op # NIL THEN
  3887. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3888. ASSERT(expression IS SyntaxTree.Designator);
  3889. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3890. result.SetRelatedAsot(left);
  3891. result.SetRelatedIndexList(parameters);
  3892. (* check if write operator exists, for var parameters *)
  3893. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3894. actualParameters := SyntaxTree.NewExpressionList();
  3895. FOR i := 0 TO parameters.Length()-1 DO
  3896. expression := ResolveExpression(parameters.GetExpression(i));
  3897. actualParameters.AddExpression(expression);
  3898. END;
  3899. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3900. actualParameters.AddExpression(rhs);
  3901. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3902. IF op = NIL THEN rhs := NIL END;
  3903. END;
  3904. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3905. ELSE
  3906. result := NIL;
  3907. END;
  3908. RETURN result;
  3909. END NewObjectOperatorCall;
  3910. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3911. 1. convert bracket designator chains into a single one that contains separators
  3912. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3913. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3914. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3915. - if an array or math array is indexed over, create index designator
  3916. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3917. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3918. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3919. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3920. - if an array-structured object type is indexed over, create procedure call designator
  3921. e.g.: a[x, y] -> a^."[]"(x, y)
  3922. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3923. - a[i, *] = a[i][*]
  3924. - a[*, i] # a[*][i]
  3925. Because:
  3926. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3927. - 'i-th column' = a[*, i]
  3928. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3929. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3930. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3931. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3932. **)
  3933. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3934. VAR
  3935. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3936. indexDesignator: SyntaxTree.IndexDesignator;
  3937. designator: SyntaxTree.Designator;
  3938. type: SyntaxTree.Type;
  3939. recordType: SyntaxTree.RecordType;
  3940. expression, rhs: SyntaxTree.Expression;
  3941. indexList: SyntaxTree.ExpressionList;
  3942. i: LONGINT;
  3943. hasError, done: BOOLEAN;
  3944. PROCEDURE FinalizeIndexDesignator;
  3945. BEGIN
  3946. IF indexDesignator # NIL THEN
  3947. (* the end of a tensor has been reached: *)
  3948. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3949. SetIndexBaseType(indexDesignator, type);
  3950. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3951. designator := indexDesignator;
  3952. type := designator.type.resolved;
  3953. indexDesignator := NIL;
  3954. ASSERT(SyntaxTree.Resolved IN type.state)
  3955. END
  3956. END FinalizeIndexDesignator;
  3957. BEGIN
  3958. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3959. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3960. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3961. (* copy all index list entries including a separator to the left bracket designator *)
  3962. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  3963. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  3964. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  3965. END;
  3966. (* propagate the related RHS *)
  3967. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  3968. (* only resolve left bracket designator and use as final result *)
  3969. resolvedExpression := ResolveExpression(leftBracketDesignator)
  3970. ELSE
  3971. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  3972. designator := ResolveDesignator(bracketDesignator.left);
  3973. type := designator.type.resolved;
  3974. indexDesignator := NIL;
  3975. (*!!! clean up *)
  3976. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  3977. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  3978. IF resolvedExpression = NIL THEN
  3979. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  3980. resolvedExpression := SyntaxTree.invalidDesignator
  3981. END;
  3982. RETURN;
  3983. END;
  3984. i := 0;
  3985. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  3986. expression := bracketDesignator.parameters.GetExpression(i);
  3987. expression := ResolveExpression(expression);
  3988. bracketDesignator.parameters.SetExpression(i, expression);
  3989. IF expression = SyntaxTree.indexListSeparator THEN
  3990. (* finalize an existing index designator if needed *)
  3991. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  3992. INC(i)
  3993. ELSE
  3994. (* do auto-dereferencing if needed *)
  3995. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  3996. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  3997. & (i=0)*)
  3998. THEN
  3999. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4000. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4001. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4002. designator := SyntaxTree.invalidDesignator;
  4003. type := SyntaxTree.invalidType
  4004. ELSE
  4005. FinalizeIndexDesignator;
  4006. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4007. type := designator.type.resolved
  4008. END
  4009. END;
  4010. (* create a new index designator, if needed *)
  4011. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4012. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4013. indexDesignator.SetAssignable(designator.assignable);
  4014. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4015. (* designator := indexDesignator *)
  4016. END;
  4017. IF type = SyntaxTree.invalidType THEN
  4018. (* error already handled *)
  4019. INC(i)
  4020. ELSIF type IS SyntaxTree.ArrayType THEN
  4021. (* indexing over an array *)
  4022. ASSERT(indexDesignator # NIL);
  4023. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4024. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4025. INC(i)
  4026. ELSIF type IS SyntaxTree.StringType THEN
  4027. (* indexing over an array *)
  4028. ASSERT(indexDesignator # NIL);
  4029. AppendIndex(expression.position, indexDesignator, expression, type);
  4030. type := type(SyntaxTree.StringType).baseType.resolved;
  4031. INC(i)
  4032. ELSIF type IS SyntaxTree.MathArrayType THEN
  4033. (* indexing over a math array *)
  4034. ASSERT(indexDesignator # NIL);
  4035. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4036. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4037. INC(i)
  4038. ELSIF IsArrayStructuredObjectType(type) THEN
  4039. (* indexing over ASOTs *)
  4040. FinalizeIndexDesignator;
  4041. ASSERT(type IS SyntaxTree.PointerType);
  4042. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4043. (*
  4044. - collect index list items from bracket designator that belong to ASOT
  4045. - check for errors
  4046. *)
  4047. indexList := SyntaxTree.NewExpressionList();
  4048. hasError := FALSE;
  4049. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4050. (* indexing over tensor ASOT:
  4051. - stop at index list end or separator
  4052. - dimensionality is given by number of index list items
  4053. *)
  4054. done := FALSE;
  4055. WHILE ~done DO
  4056. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4057. done := TRUE;
  4058. ELSE
  4059. expression := bracketDesignator.parameters.GetExpression(i);
  4060. IF expression = SyntaxTree.indexListSeparator THEN
  4061. done := TRUE;
  4062. ELSE
  4063. expression := ResolveExpression(expression);
  4064. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4065. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4066. hasError := TRUE
  4067. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4068. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4069. expression := SyntaxTree.invalidExpression;
  4070. hasError := TRUE
  4071. END;
  4072. indexList.AddExpression(expression)
  4073. END;
  4074. INC(i)
  4075. END
  4076. END
  4077. ELSE
  4078. (* indexing over non-tensor ASOT:
  4079. - ignore separators
  4080. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4081. *)
  4082. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4083. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4084. expression := bracketDesignator.parameters.GetExpression(i);
  4085. ELSE
  4086. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4087. END;
  4088. IF expression # SyntaxTree.indexListSeparator THEN
  4089. expression := ResolveExpression(expression);
  4090. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4091. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4092. expression := SyntaxTree.invalidExpression;
  4093. hasError := TRUE
  4094. END;
  4095. indexList.AddExpression(expression)
  4096. END;
  4097. INC(i)
  4098. END;
  4099. END;
  4100. IF hasError THEN
  4101. designator := SyntaxTree.invalidDesignator;
  4102. type := SyntaxTree.invalidType;
  4103. ELSE
  4104. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4105. and the last entry in the index list belongs to the array-structured object type in question.
  4106. E.g.: for a 2-dimensional array-structured object type:
  4107. - 'lhs := asot[1, 2]' -> read mode
  4108. - 'asot[1, 2] := rhs' -> write mode
  4109. - 'asot[1, 2, 3] := rhs' -> read mode
  4110. *)
  4111. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4112. rhs := bracketDesignator.relatedRhs
  4113. ELSE
  4114. rhs := NIL
  4115. END;
  4116. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4117. type := designator.type
  4118. END
  4119. ELSE
  4120. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4121. designator := SyntaxTree.invalidDesignator;
  4122. type := SyntaxTree.invalidType;
  4123. INC(i)
  4124. END
  4125. END
  4126. END;
  4127. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4128. resolvedExpression := designator
  4129. END
  4130. END VisitBracketDesignator;
  4131. (** check and resolve expression list
  4132. - resolve each expression in an expression list
  4133. - returns true if and only if all statements could have successfully been resolved
  4134. **)
  4135. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4136. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4137. BEGIN
  4138. result := TRUE;
  4139. FOR i := 0 TO expressionList.Length()-1 DO
  4140. expression := ResolveExpression(expressionList.GetExpression(i));
  4141. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4142. expressionList.SetExpression(i,expression);
  4143. END;
  4144. RETURN result
  4145. END ExpressionList;
  4146. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4147. BEGIN
  4148. type := type.resolved;
  4149. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4150. RETURN TRUE
  4151. ELSIF system.CanPassInRegister # NIL THEN
  4152. RETURN system.CanPassInRegister(type);
  4153. ELSE
  4154. RETURN FALSE
  4155. END;
  4156. END CanPassInRegister;
  4157. (** return procedure call designator left(actualParameters)
  4158. - check realtime procedure call in realtime procedure
  4159. - check number of parameters
  4160. - check parameter compatibility
  4161. return invalidDesignator if error
  4162. **)
  4163. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4164. VAR result: SyntaxTree.Designator;
  4165. numberFormalParameters, numberActualParameters: LONGINT;
  4166. formalType: SyntaxTree.ProcedureType;
  4167. formalParameter: SyntaxTree.Parameter;
  4168. actualParameter: SyntaxTree.Expression;
  4169. i: LONGINT;
  4170. BEGIN
  4171. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4172. result := SyntaxTree.invalidDesignator;
  4173. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4174. numberFormalParameters := formalType.numberParameters;
  4175. numberActualParameters := actualParameters.Length();
  4176. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4177. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4178. END;
  4179. IF ~ExpressionList(actualParameters) THEN
  4180. result := SyntaxTree.invalidDesignator
  4181. ELSE
  4182. IF numberActualParameters <= numberFormalParameters THEN
  4183. formalParameter := formalType.firstParameter;
  4184. FOR i := 0 TO numberActualParameters-1 DO
  4185. actualParameter := actualParameters.GetExpression(i);
  4186. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4187. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4188. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4189. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4190. ELSE
  4191. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4192. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4193. END;
  4194. actualParameters.SetExpression(i,actualParameter);
  4195. END;
  4196. formalParameter := formalParameter.nextParameter;
  4197. END;
  4198. WHILE (formalParameter # NIL) DO
  4199. IF formalParameter.defaultValue # NIL THEN
  4200. actualParameters.AddExpression(formalParameter.defaultValue);
  4201. formalParameter := formalParameter.nextParameter
  4202. ELSE
  4203. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4204. formalParameter := NIL;
  4205. END;
  4206. END;
  4207. ELSE
  4208. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4209. END;
  4210. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4211. result.SetAssignable(FALSE);
  4212. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4213. END;
  4214. RETURN result
  4215. END NewProcedureCallDesignator;
  4216. (**
  4217. builtin call designator generated in VisitParameterDesignator
  4218. -> nothing to be resolved
  4219. **)
  4220. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4221. BEGIN
  4222. resolvedExpression := x;
  4223. END VisitTypeGuardDesignator;
  4224. (**
  4225. builtin call designator generated in VisitParameterDesignator
  4226. -> nothing to be resolved
  4227. **)
  4228. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4229. BEGIN
  4230. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4231. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4232. ASSERT(resolvedExpression.type # NIL);
  4233. ELSIF ExpressionList(x.parameters) THEN
  4234. resolvedExpression := x;
  4235. END;
  4236. END VisitBuiltinCallDesignator;
  4237. (**
  4238. procedure call designator generated in VisitParameterDesignator
  4239. -> nothing to be resolved
  4240. **)
  4241. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4242. BEGIN
  4243. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4244. resolvedExpression := x;
  4245. END VisitProcedureCallDesignator;
  4246. (** return true if x is a variable else return false and report error **)
  4247. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4248. VAR result: BOOLEAN;
  4249. BEGIN
  4250. result := TRUE;
  4251. IF x = SyntaxTree.invalidExpression THEN
  4252. result := FALSE;
  4253. ELSIF ~IsVariable(x) THEN
  4254. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4255. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4256. result := FALSE;
  4257. END;
  4258. RETURN result
  4259. END CheckVariable;
  4260. (**
  4261. if expression x is of basic type then return true else report error and return false
  4262. **)
  4263. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4264. VAR result: BOOLEAN;
  4265. BEGIN
  4266. result := FALSE;
  4267. IF x = SyntaxTree.invalidExpression THEN
  4268. ELSIF ~IsBasicType(x.type) THEN
  4269. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4270. result := FALSE
  4271. ELSE result := TRUE
  4272. END;
  4273. RETURN result
  4274. END CheckBasicType;
  4275. (**
  4276. if expression x is of number type then return true else report error and return false
  4277. **)
  4278. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4279. VAR result: BOOLEAN;
  4280. BEGIN
  4281. result := FALSE;
  4282. IF x = SyntaxTree.invalidExpression THEN
  4283. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4284. Error(x.position,Diagnostics.Invalid,"is non number type");
  4285. ELSE result := TRUE
  4286. END;
  4287. RETURN result
  4288. END CheckNumberType;
  4289. (**
  4290. if expression x is of number or size type but not complex then return true else report error and return false
  4291. **)
  4292. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4293. VAR result: BOOLEAN;
  4294. BEGIN
  4295. result := FALSE;
  4296. IF x = SyntaxTree.invalidExpression THEN
  4297. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4298. Error(x.position,Diagnostics.Invalid,"is complex type");
  4299. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4300. Error(x.position,Diagnostics.Invalid,"is non number type");
  4301. ELSE result := TRUE
  4302. END;
  4303. RETURN result
  4304. END CheckNonComplexNumberSizeType;
  4305. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4306. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4307. BEGIN
  4308. result := FALSE; type := x.type.resolved;
  4309. IF x = SyntaxTree.invalidExpression THEN
  4310. 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
  4311. TRACE(type.sizeInBits);
  4312. TRACE(system.addressType.sizeInBits);
  4313. Error(x.position,Diagnostics.Invalid,"is no address type");
  4314. ELSE result := TRUE
  4315. END;
  4316. RETURN result
  4317. END CheckAddressType;
  4318. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4319. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4320. BEGIN
  4321. result := FALSE; type := x.type.resolved;
  4322. IF x = SyntaxTree.invalidExpression THEN
  4323. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4324. Error(x.position,Diagnostics.Invalid,"is no size type");
  4325. ELSE result := TRUE
  4326. END;
  4327. RETURN result
  4328. END CheckSizeType;
  4329. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4330. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4331. BEGIN
  4332. result := FALSE; type := x.type.resolved;
  4333. IF x = SyntaxTree.invalidExpression THEN
  4334. 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
  4335. Error(x.position,Diagnostics.Invalid,"is no object type");
  4336. ELSE result := TRUE
  4337. END;
  4338. RETURN result
  4339. END CheckObjectType;
  4340. (**
  4341. if expression x is of integer type then return true else report error and return false
  4342. **)
  4343. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4344. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4345. BEGIN
  4346. result := FALSE; type := x.type.resolved;
  4347. IF x = SyntaxTree.invalidExpression THEN
  4348. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4349. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4350. ELSE result := TRUE
  4351. END;
  4352. RETURN result
  4353. END CheckIntegerType;
  4354. (**
  4355. if expression x is of character type then return true else report error and return false
  4356. **)
  4357. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4358. VAR result: BOOLEAN;
  4359. BEGIN
  4360. result := FALSE;
  4361. IF x = SyntaxTree.invalidExpression THEN
  4362. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4363. Error(x.position,Diagnostics.Invalid,"is no character type");
  4364. ELSE result := TRUE
  4365. END;
  4366. RETURN result
  4367. END CheckCharacterType;
  4368. (**
  4369. if expression x is of real type then return true else report error and return false
  4370. **)
  4371. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4372. VAR result: BOOLEAN;
  4373. BEGIN
  4374. result := FALSE;
  4375. IF x = SyntaxTree.invalidExpression THEN
  4376. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4377. Error(x.position,Diagnostics.Invalid,"is no float type");
  4378. ELSE result := TRUE
  4379. END;
  4380. RETURN result
  4381. END CheckRealType;
  4382. (**
  4383. if expression x is of range type then return true else report error and return false
  4384. **)
  4385. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4386. VAR result: BOOLEAN;
  4387. BEGIN
  4388. result := FALSE;
  4389. IF x = SyntaxTree.invalidExpression THEN
  4390. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4391. Error(x.position,Diagnostics.Invalid,"is no range type");
  4392. ELSE result := TRUE
  4393. END;
  4394. RETURN result
  4395. END CheckRangeType;
  4396. (**
  4397. if expression x is of boolean type then return true else report error and return false
  4398. **)
  4399. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4400. VAR result: BOOLEAN;
  4401. BEGIN
  4402. result := FALSE;
  4403. IF x = SyntaxTree.invalidExpression THEN
  4404. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4405. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4406. ELSE result := TRUE
  4407. END;
  4408. RETURN result
  4409. END CheckBooleanType;
  4410. (**
  4411. if expression x is of set type then return true else report error and return false
  4412. **)
  4413. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4414. VAR result: BOOLEAN;
  4415. BEGIN
  4416. result := FALSE;
  4417. IF x = SyntaxTree.invalidExpression THEN
  4418. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4419. Error(x.position,Diagnostics.Invalid,"is no set type");
  4420. ELSE result := TRUE
  4421. END;
  4422. RETURN result
  4423. END CheckSetType;
  4424. (**
  4425. if expression x is of string or array of character type then return true else report error and return false
  4426. **)
  4427. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4428. VAR result: BOOLEAN;
  4429. BEGIN
  4430. result := FALSE;
  4431. IF x = SyntaxTree.invalidExpression THEN
  4432. ELSIF ~IsStringType(x.type.resolved) THEN
  4433. Error(x.position,Diagnostics.Invalid,"is no string type");
  4434. ELSE result := TRUE
  4435. END;
  4436. RETURN result
  4437. END CheckStringType;
  4438. (**
  4439. if expression x is a type declaration type return true else report error and return false
  4440. **)
  4441. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4442. VAR result: BOOLEAN;
  4443. BEGIN
  4444. result := FALSE;
  4445. IF x = SyntaxTree.invalidExpression THEN
  4446. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4447. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4448. ELSE result := TRUE
  4449. END;
  4450. RETURN result
  4451. END CheckTypeDeclarationType;
  4452. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4453. VAR result: BOOLEAN;
  4454. BEGIN
  4455. result := FALSE;
  4456. IF x = SyntaxTree.invalidExpression THEN
  4457. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4458. result := TRUE;
  4459. value := x.resolved(SyntaxTree.IntegerValue).value;
  4460. ELSE
  4461. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4462. END;
  4463. RETURN result;
  4464. END CheckIntegerValue;
  4465. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4466. BEGIN
  4467. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4468. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4469. ELSE
  4470. RETURN FALSE
  4471. END;
  4472. END IsUnsignedValue;
  4473. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4474. BEGIN
  4475. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4476. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4477. ELSE
  4478. RETURN FALSE
  4479. END
  4480. END IsAddressValue;
  4481. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4482. BEGIN
  4483. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4484. END IsAddressExpression;
  4485. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4486. BEGIN
  4487. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4488. END IsSizeExpression;
  4489. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4490. VAR result: BOOLEAN;
  4491. BEGIN
  4492. result := FALSE;
  4493. IF x = SyntaxTree.invalidExpression THEN
  4494. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4495. result := TRUE;
  4496. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4497. ELSE
  4498. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4499. END;
  4500. RETURN result;
  4501. END CheckEnumerationValue;
  4502. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4503. VAR result: BOOLEAN;
  4504. BEGIN
  4505. result := FALSE;
  4506. IF x = SyntaxTree.invalidExpression THEN
  4507. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4508. result := TRUE;
  4509. value := x.resolved(SyntaxTree.CharacterValue).value;
  4510. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4511. result := TRUE;
  4512. value := x.resolved(SyntaxTree.StringValue).value[0];
  4513. ELSE
  4514. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4515. END;
  4516. RETURN result;
  4517. END CheckCharacterValue;
  4518. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4519. VAR result: BOOLEAN;
  4520. BEGIN
  4521. result := FALSE;
  4522. IF x = SyntaxTree.invalidExpression THEN
  4523. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4524. value := x.resolved(SyntaxTree.IntegerValue).value;
  4525. IF (value > 0) OR includeZero & (value = 0) THEN
  4526. result := TRUE;
  4527. ELSE
  4528. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4529. END
  4530. ELSE
  4531. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4532. END;
  4533. RETURN result;
  4534. END CheckPositiveIntegerValue;
  4535. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4536. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4537. BEGIN
  4538. result := FALSE;
  4539. IF x = SyntaxTree.invalidExpression THEN
  4540. ELSE
  4541. type := x.type.resolved;
  4542. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4543. portType := type(SyntaxTree.PortType);
  4544. result := TRUE
  4545. ELSE
  4546. Error(x.position,Diagnostics.Invalid,"no port type");
  4547. END;
  4548. END;
  4549. RETURN result
  4550. END CheckPortType;
  4551. (* move to builtin procedure call statement ?
  4552. remove builtin procedure call designator ?
  4553. *)
  4554. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4555. VAR
  4556. numberActualParameters,numberFormalParameters: LONGINT;
  4557. formalParameter: SyntaxTree.Parameter;
  4558. actualParameter: SyntaxTree.Expression;
  4559. procedureType: SyntaxTree.ProcedureType;
  4560. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4561. inPort, outPort: SyntaxTree.PortType;
  4562. constructor: SyntaxTree.Procedure;
  4563. type0,type1,type2: SyntaxTree.Type;
  4564. type,base,parameterType: SyntaxTree.Type;
  4565. arrayType: SyntaxTree.ArrayType;
  4566. i,i0,i1: LONGINT;
  4567. r,r0,r1,im: LONGREAL;
  4568. c: CHAR;
  4569. id: LONGINT;
  4570. b: BOOLEAN;
  4571. first: LONGINT;
  4572. mathArrayType: SyntaxTree.MathArrayType;
  4573. customBuiltin: SyntaxTree.CustomBuiltin;
  4574. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4575. VAR resultB: BOOLEAN;
  4576. BEGIN
  4577. IF numberActualParameters < from THEN
  4578. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4579. result := SyntaxTree.invalidExpression;
  4580. resultB := FALSE;
  4581. ELSIF numberActualParameters > to THEN
  4582. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4583. result := SyntaxTree.invalidExpression;
  4584. resultB := FALSE;
  4585. ELSE
  4586. resultB := TRUE;
  4587. END;
  4588. RETURN resultB
  4589. END CheckArity;
  4590. BEGIN
  4591. type := NIL; result := NIL;
  4592. type0 := NIL; type1 := NIL; type2 := NIL;
  4593. numberActualParameters := actualParameters.Length();
  4594. IF numberActualParameters>0 THEN
  4595. parameter0 := actualParameters.GetExpression(0);
  4596. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4597. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4598. result := SyntaxTree.invalidExpression
  4599. END
  4600. END;
  4601. IF numberActualParameters >1 THEN
  4602. parameter1 := actualParameters.GetExpression(1);
  4603. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4604. ELSE
  4605. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4606. result := SyntaxTree.invalidExpression
  4607. END
  4608. END;
  4609. IF numberActualParameters >2 THEN
  4610. parameter2 := actualParameters.GetExpression(2);
  4611. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4612. ELSE
  4613. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4614. result := SyntaxTree.invalidExpression
  4615. END
  4616. END;
  4617. IF returnType # NIL THEN
  4618. id := Global.New;
  4619. result := NIL;
  4620. ELSE
  4621. id := builtin.id;
  4622. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4623. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4624. END;
  4625. END;
  4626. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4627. ELSIF result # NIL THEN type := result.type (* operator *)
  4628. ELSE
  4629. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4630. result(SyntaxTree.Designator).SetLeft(left);
  4631. IF returnType # NIL THEN
  4632. type := returnType;
  4633. END;
  4634. (* ---- ASSERT ----- *)
  4635. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4636. IF CheckBooleanType(parameter0) THEN
  4637. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4638. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4639. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4640. *)
  4641. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4642. Error(position,Diagnostics.Invalid,"assert failed");
  4643. END;
  4644. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4645. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4646. rules imposed by the architecture / current runtime
  4647. *)
  4648. END;
  4649. END;
  4650. (* ---- COPY ----- *)
  4651. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4652. IF~IsStringType(type0) THEN
  4653. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4654. END;
  4655. IF ~IsStringType(type1) THEN
  4656. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4657. ELSIF CheckVariable(parameter1) THEN
  4658. IF (type0 IS SyntaxTree.StringType) THEN
  4659. arrayType := type1(SyntaxTree.ArrayType);
  4660. IF arrayType.form = SyntaxTree.Static THEN
  4661. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4662. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4663. END;
  4664. END;
  4665. END;
  4666. END;
  4667. (* ---- INC, DEC----- *)
  4668. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4669. IF numberActualParameters = 1 THEN
  4670. parameter1 :=Global.NewIntegerValue(system,position,1);
  4671. actualParameters.AddExpression(parameter1);
  4672. END;
  4673. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4674. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4675. Error(position,Diagnostics.Invalid,"incompatible increment");
  4676. ELSE
  4677. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4678. actualParameters.SetExpression(1,parameter1);
  4679. END;
  4680. END;
  4681. (* ---- EXCL, INCL----- *)
  4682. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4683. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4684. IF IsIntegerValue(parameter1,i0) THEN
  4685. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4686. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4687. END;
  4688. END;
  4689. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4690. actualParameters.SetExpression(1,parameter1);
  4691. END;
  4692. (* ---- HALT, SYSTEM.HALT ----- *)
  4693. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4694. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4695. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4696. rules imposed by the architecture / current runtime
  4697. *)
  4698. END;
  4699. (* ---- WAIT ----- *)
  4700. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4701. IF CheckObjectType(parameter0) THEN
  4702. END;
  4703. (* ---- NEW ----- *)
  4704. ELSIF (id = Global.New) THEN
  4705. IF returnType # NIL THEN
  4706. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4707. ELSE
  4708. first := 1;
  4709. END;
  4710. IF CheckArity(first,Infinity) THEN
  4711. IF currentIsRealtime THEN
  4712. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4713. END;
  4714. (* check constructor *)
  4715. IF (first =0) OR CheckVariable(parameter0) THEN
  4716. IF type0 IS SyntaxTree.PointerType THEN
  4717. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4718. END;
  4719. IF type0 IS SyntaxTree.ArrayType THEN
  4720. arrayType := type0(SyntaxTree.ArrayType);
  4721. IF arrayType.form = SyntaxTree.Static THEN
  4722. i := first
  4723. ELSIF arrayType.form = SyntaxTree.Open THEN
  4724. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4725. ELSE HALT(100)
  4726. END;
  4727. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4728. i := first;
  4729. REPEAT
  4730. actualParameter := actualParameters.GetExpression(i);
  4731. IF CheckSizeType(actualParameter) THEN
  4732. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4733. actualParameters.SetExpression(i,actualParameter);
  4734. END;
  4735. INC(i);
  4736. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4737. END;
  4738. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4739. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4740. IF constructor = NIL THEN
  4741. IF CheckArity(first,first) THEN END;
  4742. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4743. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4744. ELSE
  4745. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4746. numberFormalParameters := procedureType.numberParameters;
  4747. IF numberActualParameters-first <= numberFormalParameters THEN
  4748. formalParameter := procedureType.firstParameter;
  4749. FOR i := first TO numberActualParameters-1 DO
  4750. actualParameter := actualParameters.GetExpression(i);
  4751. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4752. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4753. ELSE
  4754. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4755. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4756. END;
  4757. actualParameters.SetExpression(i,actualParameter);
  4758. END;
  4759. formalParameter := formalParameter.nextParameter;
  4760. END;
  4761. WHILE (formalParameter # NIL) DO
  4762. IF formalParameter.defaultValue # NIL THEN
  4763. actualParameters.AddExpression(formalParameter.defaultValue);
  4764. formalParameter := formalParameter.nextParameter
  4765. ELSE
  4766. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4767. formalParameter := NIL;
  4768. END;
  4769. END;
  4770. ELSE
  4771. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4772. END;
  4773. END;
  4774. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4775. mathArrayType := type0(SyntaxTree.MathArrayType);
  4776. IF mathArrayType.form = SyntaxTree.Static THEN
  4777. Error(position,Diagnostics.Invalid,"new on static array");
  4778. ELSE
  4779. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4780. i0 := first+1; i1 := Infinity;
  4781. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4782. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4783. i1 := i0;
  4784. ELSE HALT(100);
  4785. END;
  4786. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4787. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4788. base := ArrayBase(type0,MAX(LONGINT));
  4789. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4790. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4791. IF ~CompatibleTo(system,type0,parameterType) THEN
  4792. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4793. result := SyntaxTree.invalidExpression;
  4794. ELSE
  4795. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4796. END;
  4797. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4798. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4799. IF ~CompatibleTo(system,type1,parameterType) THEN
  4800. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4801. result := SyntaxTree.invalidExpression;
  4802. ELSE
  4803. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4804. END;
  4805. ELSE
  4806. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4807. i := first;
  4808. REPEAT
  4809. actualParameter := actualParameters.GetExpression(i);
  4810. IF CheckSizeType(actualParameter) THEN
  4811. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4812. actualParameters.SetExpression(i,actualParameter);
  4813. END;
  4814. INC(i);
  4815. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4816. END;
  4817. END;
  4818. END;
  4819. ELSIF type0 IS SyntaxTree.CellType THEN
  4820. IF ~(currentIsCellNet) THEN
  4821. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4822. ELSE
  4823. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4824. IF (constructor = NIL) & CheckArity(1,1) THEN
  4825. (* ok *)
  4826. ELSE
  4827. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4828. numberFormalParameters := procedureType.numberParameters;
  4829. DEC(numberActualParameters);
  4830. IF numberActualParameters <= numberFormalParameters THEN
  4831. formalParameter := procedureType.firstParameter;
  4832. FOR i := first TO numberActualParameters DO
  4833. actualParameter := actualParameters.GetExpression(i);
  4834. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4835. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4836. ELSE
  4837. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4838. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4839. END;
  4840. actualParameters.SetExpression(i,actualParameter);
  4841. END;
  4842. formalParameter := formalParameter.nextParameter;
  4843. END;
  4844. WHILE (formalParameter # NIL) DO
  4845. IF formalParameter.defaultValue # NIL THEN
  4846. actualParameters.AddExpression(formalParameter.defaultValue);
  4847. formalParameter := formalParameter.nextParameter
  4848. ELSE
  4849. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4850. formalParameter := NIL;
  4851. END;
  4852. END;
  4853. ELSE
  4854. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4855. END;
  4856. END;
  4857. END;
  4858. activeCellsStatement := TRUE;
  4859. ELSE
  4860. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4861. END;
  4862. END;
  4863. END;
  4864. (* ---- DISPOSE ----- *)
  4865. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4866. IF ~IsPointerType(parameter0.type) THEN
  4867. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4868. ELSIF ~IsDisposable(parameter0.type) THEN
  4869. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4870. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4871. END
  4872. (* ---- GETPROCEDURE ----- *)
  4873. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4874. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4875. IF CheckVariable(parameter2) THEN
  4876. IF ~GetProcedureAllowed(parameter2.type) THEN
  4877. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4878. END;
  4879. END;
  4880. END;
  4881. (* ---- ABS ----- *)
  4882. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4883. (* note: ABS on complex numbers is done using overloading *)
  4884. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4885. type := type0;
  4886. IF IsIntegerValue(parameter0,i0) THEN
  4887. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4888. type := Global.GetIntegerType(system,ABS(i0));
  4889. ELSIF IsRealValue(parameter0,r) THEN
  4890. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4891. END;
  4892. ELSE
  4893. type := SyntaxTree.invalidType;
  4894. END;
  4895. (* ---- ASH, ASR ----- *)
  4896. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4897. type := type0;
  4898. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4899. (*
  4900. ConvertOperands(parameter0,parameter1); (* same type *)
  4901. *)
  4902. type := parameter0.type;
  4903. IF IsIntegerValue(parameter0,i0) THEN
  4904. IF IsIntegerValue(parameter1,i1) THEN
  4905. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4906. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4907. result := ResolveExpression(result);
  4908. type := Global.GetIntegerType(system,i0);
  4909. END;
  4910. END;
  4911. IF type.resolved.sizeInBits < 32 THEN
  4912. type := system.longintType;
  4913. END;
  4914. (*!compatibility with release, remove when resolved
  4915. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4916. *)
  4917. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4918. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4919. actualParameters.SetExpression(0,parameter0);
  4920. actualParameters.SetExpression(1,parameter1);
  4921. END;
  4922. (* ---- CAP ----- *)
  4923. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4924. type := system.characterType;
  4925. IF CheckCharacterType (parameter0) THEN
  4926. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4927. actualParameters.SetExpression(0,parameter0);
  4928. IF IsCharacterValue(parameter0,c) THEN
  4929. IF (c <= "z") & (c >= "a") THEN
  4930. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4931. ELSE
  4932. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4933. END;
  4934. END;
  4935. END;
  4936. (* ---- CHR ----- *)
  4937. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4938. IF id = Global.Chr THEN
  4939. type := system.characterType
  4940. ELSE
  4941. type := system.characterType32
  4942. END;
  4943. IF CheckIntegerType(parameter0) THEN
  4944. IF IsIntegerValue(parameter0,i0) THEN
  4945. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4946. result := ResolveExpression(result);
  4947. ELSE
  4948. (*
  4949. result := NewConversion(parameter0.position,parameter0,type);
  4950. *)
  4951. END;
  4952. END
  4953. (* ---- ENTIER ----- *)
  4954. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  4955. type := system.longintType;
  4956. IF CheckRealType(parameter0) THEN
  4957. IF IsRealValue(parameter0,r) THEN
  4958. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  4959. type := Global.GetIntegerType(system,ENTIER(r));
  4960. END
  4961. END;
  4962. (* ---- ENTIERH ----- *)
  4963. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  4964. type := system.hugeintType;
  4965. IF CheckRealType(parameter0) THEN
  4966. IF IsRealValue(parameter0,r) THEN
  4967. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  4968. END
  4969. END;
  4970. (* ---- LEN ----- *)
  4971. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  4972. type := system.longintType;
  4973. base := type0;
  4974. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  4975. IF base(SyntaxTree.PointerType).isUnsafe THEN
  4976. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  4977. END;
  4978. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  4979. actualParameters.SetExpression(0,parameter0);
  4980. type0 := parameter0.type.resolved;
  4981. base := type0;
  4982. END;
  4983. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  4984. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  4985. IF i1 < 0 THEN
  4986. Error(position,Diagnostics.Invalid,"invalid dimension");
  4987. base := SyntaxTree.invalidType;
  4988. ELSE
  4989. base := ArrayBase(base,i1);
  4990. IF (base # NIL) & Indexable(base) THEN
  4991. ELSE
  4992. Error(position,Diagnostics.Invalid,"len on no array");
  4993. IF VerboseErrorMessage THEN
  4994. Printout.Info("base",base);
  4995. END;
  4996. base := SyntaxTree.invalidType;
  4997. END;
  4998. END;
  4999. IF numberActualParameters=2 THEN
  5000. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5001. actualParameters.SetExpression(1,parameter1);
  5002. ELSIF base IS SyntaxTree.MathArrayType THEN
  5003. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5004. END;
  5005. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5006. IF base IS SyntaxTree.ArrayType THEN
  5007. arrayType := base(SyntaxTree.ArrayType);
  5008. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5009. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5010. result := Global.NewIntegerValue(system,position,i);
  5011. type := result.type;(* arrayType.length.type;*)
  5012. ASSERT(type # NIL);
  5013. END;
  5014. ELSIF base IS SyntaxTree.MathArrayType THEN
  5015. mathArrayType := base(SyntaxTree.MathArrayType);
  5016. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5017. result := Global.NewIntegerValue(system,position,i);
  5018. type := result.type;
  5019. (*
  5020. type := mathArrayType.length.type;
  5021. *)
  5022. ASSERT(type # NIL);
  5023. END;
  5024. END;
  5025. END;
  5026. ELSE
  5027. type := system.longintType;
  5028. END;
  5029. (* ---- FIRST ---- *)
  5030. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5031. type := system.longintType;
  5032. IF CheckRangeType(parameter0) THEN END;
  5033. result.SetAssignable(parameter0.assignable)
  5034. (* ---- LAST ---- *)
  5035. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5036. type := system.longintType;
  5037. IF CheckRangeType(parameter0) THEN END;
  5038. result.SetAssignable(parameter0.assignable)
  5039. (* ---- STEP ---- *)
  5040. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5041. type := system.longintType;
  5042. IF CheckRangeType(parameter0) THEN END;
  5043. result.SetAssignable(parameter0.assignable)
  5044. (* ---- RE ---- *)
  5045. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5046. IF CheckNumberType(parameter0) THEN
  5047. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5048. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5049. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5050. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5051. type := parameter0.type
  5052. ELSE
  5053. type := system.realType
  5054. END
  5055. END;
  5056. result.SetAssignable(parameter0.assignable)
  5057. (* ---- IM ---- *)
  5058. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5059. IF CheckNumberType(parameter0) THEN
  5060. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5061. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5062. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5063. ELSE
  5064. type := system.realType;
  5065. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5066. END
  5067. END;
  5068. result.SetAssignable(parameter0.assignable)
  5069. (* ---- MAX ----- *)
  5070. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5071. IF numberActualParameters = 1 THEN
  5072. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5073. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5074. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5075. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5076. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5077. *)
  5078. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5079. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5080. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5081. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5082. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5083. END;
  5084. ELSE
  5085. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5086. END
  5087. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5088. ConvertOperands(parameter0,parameter1);
  5089. actualParameters.SetExpression(0,parameter0);
  5090. actualParameters.SetExpression(1,parameter1);
  5091. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5092. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5093. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5094. END;
  5095. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5096. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5097. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5098. END;
  5099. END;
  5100. type := parameter0.type;
  5101. ELSE type := SyntaxTree.invalidType;
  5102. END;
  5103. (* ---- MIN ----- *)
  5104. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5105. IF numberActualParameters = 1 THEN
  5106. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5107. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5108. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5109. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5110. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5111. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5112. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5113. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5114. END;
  5115. ELSE
  5116. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5117. END
  5118. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5119. ConvertOperands(parameter0,parameter1);
  5120. actualParameters.SetExpression(0,parameter0);
  5121. actualParameters.SetExpression(1,parameter1);
  5122. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5123. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5124. ELSE result.SetResolved(parameter1.resolved)
  5125. END;
  5126. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5127. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5128. ELSE result.SetResolved(parameter1.resolved)
  5129. END;
  5130. END;
  5131. type := parameter0.type;
  5132. ELSE type := SyntaxTree.invalidType;
  5133. END;
  5134. (* ---- ODD ----- *)
  5135. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5136. type := system.booleanType;
  5137. IF CheckIntegerType(parameter0) THEN
  5138. IF IsIntegerValue(parameter0,i0) THEN
  5139. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5140. type := system.booleanType;
  5141. END;
  5142. END;
  5143. (* ---- ORD ----- *)
  5144. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5145. IF id = Global.Ord THEN
  5146. type := system.integerType;
  5147. ELSE
  5148. type := system.longintType;
  5149. END;
  5150. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5151. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5152. actualParameters.SetExpression(0,parameter0);
  5153. (* IF CheckCharacterType(parameter0) THEN*)
  5154. IF IsCharacterValue(parameter0,c)THEN
  5155. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5156. type := Global.GetSignedIntegerType(system,ORD(c));
  5157. END;
  5158. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5159. END;
  5160. (* ---- SHORT ----- *)
  5161. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5162. type := type0;
  5163. IF IsSignedIntegerType(type) THEN
  5164. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5165. ELSIF type = system.integerType THEN type := system.shortintType
  5166. ELSIF type = system.longintType THEN type := system.integerType
  5167. ELSIF type = system.hugeintType THEN type:= system.longintType
  5168. ELSE
  5169. CASE type.sizeInBits OF
  5170. 16: type := Global.Integer8
  5171. |32: type := Global.Integer16
  5172. |64: type := Global.Integer32
  5173. END;
  5174. END;
  5175. ELSIF type IS SyntaxTree.FloatType THEN
  5176. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5177. ELSIF type = system.longrealType THEN type := system.realType
  5178. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5179. END;
  5180. ELSIF type IS SyntaxTree.ComplexType THEN
  5181. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5182. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5183. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5184. END;
  5185. ELSE
  5186. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5187. END;
  5188. IF (parameter0.resolved # NIL) THEN
  5189. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5190. IF parameter0 IS SyntaxTree.Value THEN
  5191. result.SetResolved(parameter0(SyntaxTree.Value));
  5192. END;
  5193. END;
  5194. (* ---- LONG ----- *)
  5195. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5196. type := type0;
  5197. IF IsSignedIntegerType(type) THEN
  5198. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5199. ELSIF type = system.longintType THEN type := system.hugeintType
  5200. ELSIF type = system.integerType THEN type := system.longintType
  5201. ELSIF type = system.shortintType THEN type := system.integerType
  5202. ELSE
  5203. CASE type.sizeInBits OF
  5204. 8: type := Global.Integer16
  5205. |16: type := Global.Integer32
  5206. |32: type := Global.Integer64
  5207. END;
  5208. END;
  5209. ELSIF type IS SyntaxTree.FloatType THEN
  5210. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5211. ELSIF type= system.realType THEN type := system.longrealType
  5212. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5213. END;
  5214. ELSIF type IS SyntaxTree.ComplexType THEN
  5215. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5216. ELSIF type = system.complexType THEN type := system.longcomplexType
  5217. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5218. END;
  5219. ELSE
  5220. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5221. END;
  5222. IF (parameter0.resolved # NIL) THEN
  5223. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5224. IF parameter0 IS SyntaxTree.Value THEN
  5225. result.SetResolved(parameter0(SyntaxTree.Value));
  5226. END;
  5227. END;
  5228. (* ---- SIZE OF ----- *)
  5229. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5230. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5231. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5232. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5233. type := system.integerType;
  5234. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5235. ELSE
  5236. (* for variables, system sizeof could represent the physically occupied size
  5237. determined via the type descriptor, implement that ? *)
  5238. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5239. END
  5240. (* ---- SYSTEM.TRACE -----*)
  5241. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5242. FOR i := 0 TO numberActualParameters-1 DO
  5243. parameter0 := actualParameters.GetExpression(i);
  5244. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5245. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5246. END;
  5247. END;
  5248. (* remaining issues can only be tested in backend *)
  5249. (* ---- ADDRESSOF----- *)
  5250. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5251. IF HasAddress(parameter0) THEN
  5252. type := system.addressType;
  5253. ELSE
  5254. type := SyntaxTree.invalidType;
  5255. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5256. END;
  5257. (* ---- BIT ----- *)
  5258. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5259. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5260. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5261. actualParameters.SetExpression(0,parameter0);
  5262. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5263. actualParameters.SetExpression(1,parameter1);
  5264. END;
  5265. type := system.booleanType;
  5266. (* ----- MSK ---- *)
  5267. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5268. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5269. ConvertOperands(parameter0,parameter1);
  5270. actualParameters.SetExpression(0,parameter0);
  5271. actualParameters.SetExpression(1,parameter1);
  5272. END;
  5273. type := parameter0.type;
  5274. (* ---- SYSTEM.GET64 ----- *)
  5275. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5276. IF CheckAddressType(parameter0) THEN
  5277. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5278. actualParameters.SetExpression(0,parameter0);
  5279. END;
  5280. type := system.hugeintType;
  5281. (* ---- SYSTEM.GET32 ----- *)
  5282. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5283. IF CheckAddressType(parameter0) THEN
  5284. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5285. actualParameters.SetExpression(0,parameter0);
  5286. END;
  5287. type := system.longintType;
  5288. (* ---- SYSTEM.GET16 ----- *)
  5289. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5290. IF CheckAddressType(parameter0) THEN
  5291. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5292. actualParameters.SetExpression(0,parameter0);
  5293. END;
  5294. type := system.integerType;
  5295. (* ---- SYSTEM.GET8 ----- *)
  5296. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5297. IF CheckAddressType(parameter0) THEN
  5298. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5299. actualParameters.SetExpression(0,parameter0);
  5300. END;
  5301. type := system.shortintType;
  5302. (* ---- SYSTEM.GetStackPointer ----- *)
  5303. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5304. type := system.addressType;
  5305. (* ---- SYSTEM.GetFramePointer ----- *)
  5306. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5307. type := system.addressType;
  5308. (* ---- SYSTEM.GetActivity ----- *)
  5309. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5310. type := system.objectType;
  5311. (* ---- SYSTEM.SetStackPointer ----- *)
  5312. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5313. IF CheckAddressType(parameter0) THEN
  5314. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5315. actualParameters.SetExpression(0,parameter0);
  5316. END;
  5317. (* ---- SYSTEM.SetFramePointer ----- *)
  5318. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5319. IF CheckAddressType(parameter0) THEN
  5320. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5321. actualParameters.SetExpression(0,parameter0);
  5322. END;
  5323. (* ---- SYSTEM.SetActivity ----- *)
  5324. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5325. IF CheckObjectType(parameter0) THEN
  5326. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5327. actualParameters.SetExpression(0,parameter0);
  5328. END;
  5329. (* ---- LSH, LSL, ROT, ROR ----- *)
  5330. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5331. type := type0;
  5332. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5333. actualParameters.SetExpression(1, parameter1);
  5334. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5335. IF id = Global.Lsh THEN
  5336. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5337. ELSIF id = Global.Rot THEN
  5338. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5339. ELSIF id = Global.Ror THEN
  5340. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5341. END;
  5342. END;
  5343. (* ---- SYSTEM.VAL ----- *)
  5344. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5345. IF CheckTypeDeclarationType(parameter0) THEN
  5346. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5347. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5348. result := SyntaxTree.invalidExpression;
  5349. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5350. ELSE
  5351. IF (parameter1.resolved # NIL) THEN
  5352. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5353. IF parameter0 IS SyntaxTree.Value THEN
  5354. result.SetResolved(parameter0(SyntaxTree.Value));
  5355. END;
  5356. END;
  5357. result.SetAssignable(parameter1.assignable);
  5358. END;
  5359. END;
  5360. (* ---- SYSTEM.GET ----- *)
  5361. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5362. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5363. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5364. actualParameters.SetExpression(0,parameter0);
  5365. END;
  5366. (* ---- SYSTEM.PUT ----- *)
  5367. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5368. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5369. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5370. actualParameters.SetExpression(0,parameter0);
  5371. END;
  5372. (* ---- SYSTEM.PUT64 ----- *)
  5373. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5374. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5375. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5376. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5377. actualParameters.SetExpression(0,parameter0);
  5378. actualParameters.SetExpression(1,parameter1);
  5379. END;
  5380. (* ---- SYSTEM.PUT32 ----- *)
  5381. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5382. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5383. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5384. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5385. actualParameters.SetExpression(0,parameter0);
  5386. actualParameters.SetExpression(1,parameter1);
  5387. END;
  5388. (* ---- SYSTEM.PUT16 ----- *)
  5389. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5390. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5391. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5392. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5393. actualParameters.SetExpression(0,parameter0);
  5394. actualParameters.SetExpression(1,parameter1);
  5395. END;
  5396. (* ---- SYSTEM.PUT8 ----- *)
  5397. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5398. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5399. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5400. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5401. actualParameters.SetExpression(0,parameter0);
  5402. actualParameters.SetExpression(1,parameter1);
  5403. END;
  5404. (* ---- SYSTEM.MOVE ----- *)
  5405. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5406. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5407. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5408. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5409. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5410. actualParameters.SetExpression(0,parameter0);
  5411. actualParameters.SetExpression(1,parameter1);
  5412. actualParameters.SetExpression(2,parameter2);
  5413. END;
  5414. (* ---- SYSTEM.NEW ----- *)
  5415. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5416. IF ~IsPointerType(parameter0.type) THEN
  5417. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5418. ELSIF CheckSizeType(parameter1) THEN
  5419. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5420. actualParameters.SetExpression(1,parameter1);
  5421. END;
  5422. (* ----SYSTEM.REF ---- *)
  5423. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5424. type := system.addressType
  5425. (* ---- INCR ----- *)
  5426. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5427. type := system.sizeType;
  5428. base := type0;
  5429. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5430. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5431. IF i1 < 0 THEN
  5432. Error(position,Diagnostics.Invalid,"invalid dimension");
  5433. base := SyntaxTree.invalidType;
  5434. ELSE
  5435. base := ArrayBase(base,i1);
  5436. IF (base # NIL) & Indexable(base) THEN
  5437. ELSE
  5438. Error(position,Diagnostics.Invalid,"len on no array");
  5439. IF VerboseErrorMessage THEN
  5440. Printout.Info("base",base);
  5441. END;
  5442. base := SyntaxTree.invalidType;
  5443. END;
  5444. END;
  5445. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5446. actualParameters.SetExpression(1,parameter1);
  5447. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5448. mathArrayType := base(SyntaxTree.MathArrayType);
  5449. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5450. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5451. type := system.longintType;
  5452. END;
  5453. END;
  5454. ELSE
  5455. type := system.longintType;
  5456. END;
  5457. (* ---- SUM ----- *)
  5458. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5459. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5460. (* ---- ALL ----- *)
  5461. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5462. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5463. (* ---- DIM ----- *)
  5464. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5465. type := system.sizeType;
  5466. IF type0 IS SyntaxTree.MathArrayType THEN
  5467. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5468. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5469. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5470. END;
  5471. ELSE
  5472. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5473. END;
  5474. (* ---- CAS ----- *)
  5475. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5476. IF type0.IsComposite () THEN
  5477. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5478. result := SyntaxTree.invalidExpression;
  5479. ELSIF ~IsVariable (parameter0) THEN
  5480. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5481. result := SyntaxTree.invalidExpression;
  5482. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5483. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5484. result := SyntaxTree.invalidExpression;
  5485. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5486. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5487. result := SyntaxTree.invalidExpression;
  5488. ELSE
  5489. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5490. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5491. type := type0;
  5492. END;
  5493. (* ---- RESHAPE ----- *)
  5494. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5495. IF type0 IS SyntaxTree.MathArrayType THEN
  5496. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5497. base := ArrayBase(type0,MAX(LONGINT));
  5498. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5499. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5500. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5501. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5502. IF ~CompatibleTo(system,type0,parameterType) THEN
  5503. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5504. result := SyntaxTree.invalidExpression;
  5505. ELSE
  5506. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5507. END;
  5508. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5509. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5510. IF ~CompatibleTo(system,type1,parameterType) THEN
  5511. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5512. result := SyntaxTree.invalidExpression;
  5513. ELSE
  5514. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5515. END;
  5516. ELSE
  5517. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5518. result := SyntaxTree.invalidExpression;
  5519. END;
  5520. (* ---- SYSTEM.TYPECODE ----- *)
  5521. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5522. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5523. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5524. type := type.resolved;
  5525. IF type IS SyntaxTree.PointerType THEN
  5526. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5527. END;
  5528. IF ~(type IS SyntaxTree.RecordType) THEN
  5529. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5530. END;
  5531. ELSE
  5532. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5533. END;
  5534. type := system.addressType;
  5535. (* -------- FLT --------- *)
  5536. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5537. type := system.realType;
  5538. IF IsRealValue(parameter0, r) THEN
  5539. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5540. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5541. i0 := i; i := ABS(i);
  5542. IF i # 0 THEN
  5543. i1 := 23;
  5544. IF i >= 2*800000H THEN
  5545. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5546. ELSIF i < 800000H THEN
  5547. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5548. END;
  5549. i := (i1 + 127)*800000H - 800000H + i;
  5550. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5551. END;
  5552. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5553. END;
  5554. (* ------- CONNECT -------*)
  5555. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5556. IF ~(currentIsCellNet) THEN
  5557. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5558. END;
  5559. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5560. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5561. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5562. END;
  5563. IF numberActualParameters = 3 THEN
  5564. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5565. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5566. END;
  5567. *)
  5568. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5569. actualParameters.SetExpression(2,parameter2);
  5570. END;
  5571. activeCellsStatement := TRUE;
  5572. (* ---------- DELEGATE --------*)
  5573. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5574. IF ~(currentIsCellNet) THEN
  5575. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5576. END;
  5577. IF ~CheckPortType(parameter1, inPort) THEN
  5578. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5579. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5580. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5581. ELSIF (outPort.direction # inPort.direction) THEN
  5582. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5583. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5584. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5585. END;
  5586. activeCellsStatement := TRUE;
  5587. (* --------- RECEIVE ---------*)
  5588. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5589. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5590. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5591. IF inPort.direction # SyntaxTree.InPort THEN
  5592. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5593. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5594. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5595. END;
  5596. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5597. IF ~SameType(parameter2.type, system.integerType) THEN
  5598. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5599. END;
  5600. END;
  5601. END;
  5602. (* --------- SEND ---------*)
  5603. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5604. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5605. IF CheckPortType(parameter0,outPort) THEN
  5606. IF outPort.direction # SyntaxTree.OutPort THEN
  5607. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5608. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5609. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5610. ELSE
  5611. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5612. actualParameters.SetExpression(1,parameter1);
  5613. END;
  5614. END;
  5615. (* ------- custom builtins ----- *)
  5616. ELSIF id = Global.systemSpecial THEN
  5617. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5618. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5619. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5620. type := procedureType.returnType;
  5621. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5622. (* go through all formal parameters *)
  5623. formalParameter := procedureType.firstParameter;
  5624. FOR i := 0 TO actualParameters.Length() - 1 DO
  5625. actualParameter := actualParameters.GetExpression(i);
  5626. IF actualParameter = SyntaxTree.invalidExpression THEN
  5627. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5628. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5629. ELSE
  5630. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5631. END;
  5632. actualParameters.SetExpression(i, actualParameter);
  5633. formalParameter := formalParameter.nextParameter
  5634. END
  5635. END
  5636. ELSE
  5637. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5638. result := SyntaxTree.invalidExpression;
  5639. END;
  5640. END;
  5641. IF result # SyntaxTree.invalidExpression THEN
  5642. type := ResolveType(type);
  5643. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5644. result.SetType(type);
  5645. END;
  5646. RETURN result
  5647. END NewBuiltinCallDesignator;
  5648. (** return type guard designator left(type)
  5649. - check if type can be extended (i.e. is no static record)
  5650. - check if type is a type extension of left.type
  5651. - returns new type guard designator
  5652. returns invalidDesignator = invalidExpression if error
  5653. **)
  5654. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5655. VAR result: SyntaxTree.Designator;
  5656. BEGIN
  5657. result := SyntaxTree.invalidDesignator;
  5658. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5659. Error(position,Diagnostics.Invalid,"no type extension of type");
  5660. IF VerboseErrorMessage THEN
  5661. Printout.Info("left",left);
  5662. Printout.Info("type",type);
  5663. END;
  5664. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5665. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5666. ELSIF IsUnsafePointer(left.type) THEN
  5667. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5668. ELSE
  5669. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5670. result.SetType(type);
  5671. result.SetAssignable(left.assignable);
  5672. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5673. END;
  5674. RETURN result
  5675. END NewTypeGuardDesignator;
  5676. (** check and resolve parameter designator left(expression list)
  5677. - check expression list
  5678. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5679. - elsif left is a procedure type then
  5680. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5681. - else return is a procedure call then return ProcedureCallDesignator
  5682. returns invalidDesignator = invalidExpression if error
  5683. **)
  5684. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5685. VAR
  5686. parameters: SyntaxTree.ExpressionList;
  5687. left: SyntaxTree.Designator;
  5688. result,expression: SyntaxTree.Expression;
  5689. typeDeclaration: SyntaxTree.TypeDeclaration;
  5690. type, expressionType: SyntaxTree.Type;
  5691. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5692. BEGIN
  5693. type := type.resolved;
  5694. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5695. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5696. END;
  5697. RETURN type
  5698. END BaseType;
  5699. BEGIN
  5700. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5701. result := SyntaxTree.invalidDesignator;
  5702. left := ResolveDesignator(designator.left);
  5703. IF left # SyntaxTree.invalidDesignator THEN
  5704. parameters := designator.parameters;
  5705. IF ExpressionList(parameters) THEN
  5706. IF (left.type = NIL) THEN
  5707. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5708. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5709. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5710. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5711. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5712. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5713. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5714. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5715. ELSE
  5716. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5717. END
  5718. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5719. expression := parameters.GetExpression(0);
  5720. type := typeDeclaration.declaredType.resolved;
  5721. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5722. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5723. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5724. OR (expressionType IS SyntaxTree.EnumerationType)
  5725. ) THEN
  5726. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5727. ELSE
  5728. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5729. END;
  5730. ELSE
  5731. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5732. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5733. result := SyntaxTree.invalidDesignator;
  5734. END;
  5735. ELSE
  5736. result := SyntaxTree.invalidDesignator
  5737. END;
  5738. END;
  5739. resolvedExpression := result;
  5740. END VisitParameterDesignator;
  5741. (** check dereference designator left^
  5742. - check if left is pointer type or left is object type
  5743. - return new dereference designator with type = left.baseType.type (if appropriate)
  5744. with error handling
  5745. returns invalidDesignator = invalidExpression if error
  5746. **)
  5747. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5748. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5749. BEGIN
  5750. result := SyntaxTree.invalidDesignator;
  5751. type := left.type;
  5752. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5753. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5754. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5755. result.SetAssignable(TRUE);
  5756. result.SetType(type);
  5757. result.SetHidden(left.isHidden);
  5758. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5759. type := type.resolved;
  5760. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5761. result.SetAssignable(TRUE);
  5762. result.SetType(type);
  5763. result.SetHidden(left.isHidden);
  5764. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5765. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5766. result.SetAssignable(TRUE);
  5767. result.SetType(type);
  5768. result.SetHidden(left.isHidden);
  5769. ELSE
  5770. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5771. IF VerboseErrorMessage THEN
  5772. Printout.Info("pointer", type);
  5773. Printout.Info("scope", currentScope);
  5774. END;
  5775. END;
  5776. RETURN result
  5777. END NewDereferenceDesignator;
  5778. (** check supercall designator left^
  5779. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5780. - return new supercall designator with type = left.type
  5781. with error handling
  5782. **)
  5783. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5784. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5785. objectScope: SyntaxTree.Scope;
  5786. BEGIN
  5787. result := SyntaxTree.invalidDesignator;
  5788. IF left = SyntaxTree.invalidDesignator THEN
  5789. (* error already handled *)
  5790. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5791. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5792. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5793. IF symbol IS SyntaxTree.Procedure THEN
  5794. procedure := symbol(SyntaxTree.Procedure);
  5795. objectScope := currentScope;
  5796. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5797. objectScope := objectScope.outerScope;
  5798. END;
  5799. IF (left.left = NIL) OR ~
  5800. (
  5801. (left.left IS SyntaxTree.SelfDesignator) OR
  5802. (left.left IS SyntaxTree.DereferenceDesignator)
  5803. & (left.left(SyntaxTree.Designator).left # NIL)
  5804. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5805. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5806. IF VerboseErrorMessage THEN
  5807. Printout.Info("left.left",left.left);
  5808. END;
  5809. ELSIF procedure.super # NIL THEN
  5810. result := SyntaxTree.NewSupercallDesignator(position,left);
  5811. result.SetType(left.type.resolved)
  5812. ELSE
  5813. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5814. END;
  5815. ELSE
  5816. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5817. END;
  5818. ELSE
  5819. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5820. END;
  5821. RETURN result
  5822. END NewSupercallDesignator;
  5823. (** check and semantically resolve arrow designator left^
  5824. - if left is procedure type -> result := SupercallDesignator
  5825. - else result := DereferenceDesignator
  5826. returns result via global variable resolvedExpression
  5827. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5828. **)
  5829. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5830. VAR left: SyntaxTree.Designator;
  5831. BEGIN
  5832. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5833. left := ResolveDesignator(arrowDesignator.left);
  5834. IF left # NIL THEN
  5835. IF (left.type = NIL) THEN
  5836. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5837. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5838. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5839. ELSE
  5840. IF IsPointerToObject(left.type) THEN
  5841. Warning(arrowDesignator.position, "forbidden dereference on object");
  5842. END;
  5843. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5844. END
  5845. END
  5846. END VisitArrowDesignator;
  5847. (** check and return expression
  5848. - if expression has no type then resolve expression
  5849. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5850. - return result
  5851. **)
  5852. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5853. VAR result,prev: SyntaxTree.Expression;
  5854. BEGIN
  5855. IF expression = NIL THEN result := NIL
  5856. ELSIF (expression.type = NIL) THEN
  5857. prev := resolvedExpression;
  5858. resolvedExpression := SyntaxTree.invalidExpression;
  5859. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5860. expression.SetType(SyntaxTree.invalidType);
  5861. END;
  5862. expression.Accept(SELF);
  5863. result := resolvedExpression;
  5864. IF currentIsRealtime THEN
  5865. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5866. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5867. END;
  5868. END;
  5869. (* designator modifiers for backends if they support it ...*)
  5870. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5871. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5872. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5873. END;
  5874. resolvedExpression := prev
  5875. ELSE
  5876. result := expression
  5877. END;
  5878. RETURN result
  5879. END ResolveExpression;
  5880. (**
  5881. check expression to be constant expression
  5882. - resolve expression
  5883. - if valid then check that of value type
  5884. report error and return invalidExpression if anything fails
  5885. **)
  5886. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5887. VAR position: LONGINT;
  5888. BEGIN
  5889. position := expression.position;
  5890. expression := ResolveExpression(expression);
  5891. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5892. ELSIF (expression.resolved = NIL) THEN
  5893. Error(position,Diagnostics.Invalid,"expression is not constant");
  5894. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5895. expression := SyntaxTree.invalidExpression;
  5896. END;
  5897. RETURN expression
  5898. END ConstantExpression;
  5899. (** check expression to be constant integer
  5900. - resolve expresssion
  5901. - if valid then check that of integer value type
  5902. report error and return invalidExpression if anything fails
  5903. **)
  5904. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5905. VAR position: LONGINT;
  5906. BEGIN
  5907. position := expression.position;
  5908. expression := ResolveExpression(expression);
  5909. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5910. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5911. expression := SyntaxTree.invalidExpression;
  5912. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5913. END;
  5914. RETURN expression
  5915. END ConstantInteger;
  5916. (** check expression as positive (>=0) constant integer
  5917. - resolve expression
  5918. - if valid then check that integer value
  5919. - if integer value then check that value >= 0
  5920. report error and return invalidExpression if anything fails
  5921. **)
  5922. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5923. VAR position: LONGINT;
  5924. BEGIN
  5925. position := expression.position;
  5926. expression := ConstantExpression(expression);
  5927. IF expression = SyntaxTree.invalidExpression THEN
  5928. (* error already reported *)
  5929. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5930. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5931. expression := SyntaxTree.invalidExpression
  5932. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5933. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5934. END;
  5935. RETURN expression
  5936. END ConstantIntegerGeq0;
  5937. (** check expression as condition
  5938. - resolve expression
  5939. - if valid expression then check that result type is boolean
  5940. report error and return invalidExpression if anything fails
  5941. **)
  5942. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5943. VAR position: LONGINT;
  5944. BEGIN
  5945. position := expression.position;
  5946. expression := ResolveExpression(expression);
  5947. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5948. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  5949. expression := SyntaxTree.invalidExpression;
  5950. Error(position,Diagnostics.Invalid,"expression is not boolean");
  5951. END;
  5952. RETURN expression
  5953. END ResolveCondition;
  5954. (*** symbols ***)
  5955. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  5956. BEGIN
  5957. x.Accept(SELF);
  5958. END ResolveSymbol;
  5959. (** check a symbol
  5960. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  5961. **)
  5962. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  5963. VAR scope: SyntaxTree.Scope;
  5964. BEGIN
  5965. (* visibility *)
  5966. scope := symbol.scope;
  5967. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  5968. scope := scope.outerScope;
  5969. END;
  5970. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  5971. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  5972. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  5973. IF VerboseErrorMessage THEN
  5974. Printout.Info("symbol",symbol);
  5975. END;
  5976. END;
  5977. END;
  5978. END CheckSymbolVisibility;
  5979. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  5980. If node is currently being resolved then emit a cyclic definition error.
  5981. Return TRUE only if node is fully resolved.
  5982. **)
  5983. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  5984. VAR result: BOOLEAN;
  5985. BEGIN
  5986. IF SyntaxTree.Resolved IN x.state THEN
  5987. result := FALSE
  5988. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  5989. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  5990. result := FALSE;
  5991. ELSE
  5992. result := TRUE;
  5993. x.SetState(SyntaxTree.BeingResolved)
  5994. END;
  5995. RETURN result
  5996. END SymbolNeedsResolution;
  5997. (** check and resolve a type declaration symbol = Type
  5998. - set type to declaration type
  5999. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6000. This is so because the type declaration itself does not have a type but it only stands for a type.
  6001. In the implementation of the compiler this made a lot much easier.
  6002. - resolve and set declared type
  6003. - check symbol
  6004. **)
  6005. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6006. VAR prevScope: SyntaxTree.Scope;
  6007. BEGIN
  6008. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6009. IF SymbolNeedsResolution(typeDeclaration) THEN
  6010. prevScope := currentScope;
  6011. currentScope := typeDeclaration.scope;
  6012. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6013. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6014. CheckSymbolVisibility(typeDeclaration);
  6015. typeDeclaration.SetState(SyntaxTree.Resolved);
  6016. currentScope := prevScope;
  6017. END;
  6018. END VisitTypeDeclaration;
  6019. (** check and resolve a constant declaration symbol = (constant) expression
  6020. - check expression
  6021. - set type and value
  6022. - check symbol
  6023. **)
  6024. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6025. VAR
  6026. expression: SyntaxTree.Expression;
  6027. type: SyntaxTree.Type;
  6028. name: Basic.SegmentedName;
  6029. replacement: Replacement;
  6030. stringReader: Streams.StringReader;
  6031. scanner: Scanner.Scanner; parser: Parser.Parser;
  6032. BEGIN
  6033. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6034. IF SymbolNeedsResolution(constant) THEN
  6035. expression := constant.value;
  6036. IF replacements # NIL THEN
  6037. Global.GetSymbolSegmentedName(constant, name);
  6038. replacement := replacements;
  6039. WHILE (replacement # NIL) & (replacement.name # name) DO
  6040. replacement := replacement.next;
  6041. END;
  6042. IF replacement # NIL THEN
  6043. InfoSS(constant.position, "replacing constant", constant.name);
  6044. (*
  6045. NEW(stringReader, Strings.Length(replacement.string^));
  6046. stringReader.Set(replacement.string^);
  6047. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6048. NEW(parser, scanner, diagnostics);
  6049. expression := parser.Expression();
  6050. *)
  6051. expression := replacement.expression;
  6052. replacement.used := TRUE;
  6053. END;
  6054. END;
  6055. constant.SetType(SyntaxTree.invalidType);
  6056. expression := ConstantExpression(expression);
  6057. ASSERT(expression.type # NIL);
  6058. type := expression.type.resolved;
  6059. constant.SetType(type);
  6060. constant.SetValue(expression);
  6061. CheckSymbolVisibility(constant);
  6062. constant.SetState(SyntaxTree.Resolved);
  6063. END;
  6064. END VisitConstant;
  6065. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6066. VAR procedureAlignment: LONGINT;
  6067. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6068. (* least common multiple *)
  6069. VAR a,b: LONGINT;
  6070. BEGIN
  6071. a := a0; b := b0;
  6072. WHILE (a # b) DO
  6073. IF a < b THEN a := a+a0
  6074. ELSE b := b + b0
  6075. END;
  6076. END;
  6077. RETURN a
  6078. END LCM;
  6079. BEGIN
  6080. IF alignment > 1 THEN
  6081. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6082. IF (procedureAlignment > 1) THEN
  6083. alignment := LCM(alignment, procedureAlignment);
  6084. END;
  6085. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6086. END;
  6087. END AdaptStackAlignment;
  6088. (** check and resolve a variable / field
  6089. - check and set type
  6090. - negative check on open array type
  6091. - check symbol
  6092. **)
  6093. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6094. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6095. BEGIN
  6096. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6097. IF SymbolNeedsResolution(variable) THEN
  6098. modifiers := variable.modifiers;
  6099. (*
  6100. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6101. variable.AddFlags(flags);
  6102. *)
  6103. variable.SetType(ResolveType(variable.type));
  6104. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6105. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6106. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6107. END;
  6108. END;
  6109. CheckSymbolVisibility(variable);
  6110. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6111. variable.SetUntraced(TRUE);
  6112. IF ~ContainsPointer(variable.type) THEN
  6113. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6114. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6115. END;
  6116. END;
  6117. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6118. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6119. IF ~PowerOf2(value) THEN
  6120. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6121. ELSE
  6122. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6123. END;
  6124. END;
  6125. variable.SetAlignment(FALSE,value);
  6126. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6127. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6128. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6129. END;
  6130. variable.SetAlignment(TRUE, value);
  6131. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6132. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6133. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6134. END;
  6135. variable.SetFictive(value);
  6136. variable.SetOffset(value*system.dataUnit);
  6137. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6138. END;
  6139. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6140. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6141. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6142. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6143. END;
  6144. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6145. IF variable.initializer # NIL THEN
  6146. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6147. END;
  6148. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6149. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6150. pointerType.SetPointerBase(variable.type);
  6151. pointerType.SetHidden(TRUE);
  6152. variable.SetType(ResolveType(pointerType));
  6153. END;
  6154. variable.SetState(SyntaxTree.Resolved);
  6155. END;
  6156. END VisitVariable;
  6157. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6158. BEGIN
  6159. VisitVariable(property)
  6160. END VisitProperty;
  6161. (** check and resolve a (procedure) parameter
  6162. - check and set type
  6163. - check symbol
  6164. - check parameter kind and set read-only flags if appropriate
  6165. **)
  6166. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6167. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6168. BEGIN
  6169. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6170. IF SymbolNeedsResolution(parameter) THEN
  6171. modifiers := parameter.modifiers;
  6172. parameter.SetType(ResolveType(parameter.type));
  6173. ASSERT(parameter.type.resolved # NIL);
  6174. CheckSymbolVisibility(parameter);
  6175. IF parameter.defaultValue # NIL THEN
  6176. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6177. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6178. ELSE
  6179. expression := ConstantExpression(parameter.defaultValue);
  6180. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6181. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6182. parameter.SetDefaultValue(expression);
  6183. END;
  6184. END;
  6185. END;
  6186. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6187. parameter.SetUntraced(TRUE);
  6188. IF ~ContainsPointer(parameter.type) THEN
  6189. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6190. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6191. END;
  6192. END;
  6193. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6194. parameter.SetMoveable(TRUE);
  6195. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6196. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6197. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6198. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6199. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6200. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6201. END;
  6202. END;
  6203. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6204. parameter.SetState(SyntaxTree.Resolved);
  6205. END;
  6206. END VisitParameter;
  6207. (** check and resolve a procedure (with declaration and implementation scope)
  6208. - check the procedure type
  6209. - check if method (i.e. in record scope), if so then
  6210. - check if (unique) constructor
  6211. - check if (unique) finalizer
  6212. - check if super method available, if so then check signature
  6213. - of not in record scope then negative check on constructor flag
  6214. - of not in record scope then negative check on finalizer flag
  6215. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6216. - check procedure symbol
  6217. **)
  6218. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6219. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6220. procedureType: SyntaxTree.ProcedureType;
  6221. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6222. qualifiedType: SyntaxTree.QualifiedType;
  6223. value: LONGINT;
  6224. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6225. position: LONGINT;
  6226. BEGIN
  6227. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6228. IF IsOberonInline(procedure) THEN
  6229. IF SyntaxTree.Public * procedure.access # {} THEN
  6230. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6231. END;
  6232. procedure.SetInline(FALSE);
  6233. procedure.SetOberonInline(TRUE);
  6234. END;
  6235. IF SymbolNeedsResolution(procedure) THEN
  6236. recentIsRealtime := currentIsRealtime;
  6237. recentIsBodyProcedure := currentIsBodyProcedure;
  6238. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6239. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6240. modifiers := procedureType.modifiers;
  6241. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6242. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6243. IF useDarwinCCalls THEN (*fld*)
  6244. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6245. ELSE
  6246. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6247. END
  6248. END;
  6249. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6250. procedureType.SetInterrupt(TRUE);
  6251. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6252. END;
  6253. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6254. procedureType.SetNoReturn(TRUE);
  6255. END;
  6256. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6257. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6258. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6259. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6260. END;
  6261. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6262. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6263. END;
  6264. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6265. IF ~PowerOf2(value) THEN
  6266. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6267. ELSE
  6268. procedureType.SetStackAlignment(value)
  6269. END;
  6270. END;
  6271. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6272. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6273. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6274. END;
  6275. CheckModifiers(modifiers, TRUE);
  6276. modifiers := procedureType.returnTypeModifiers;
  6277. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6278. CheckModifiers(modifiers, TRUE);
  6279. FixProcedureType(procedureType);
  6280. currentIsRealtime := procedureType.isRealtime;
  6281. currentIsBodyProcedure := procedure.isBodyProcedure;
  6282. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6283. THEN
  6284. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6285. END;
  6286. CheckSymbolVisibility(procedure);
  6287. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6288. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6289. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6290. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6291. END;
  6292. END;
  6293. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6294. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6295. procedureType.SetDelegate(TRUE);
  6296. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6297. IF record.pointerType.typeDeclaration = NIL THEN
  6298. selfParameter.SetType(record.pointerType);
  6299. ELSE
  6300. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6301. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6302. qualifiedType.SetResolved(record.pointerType);
  6303. selfParameter.SetType(qualifiedType);
  6304. END;
  6305. selfParameter.SetAccess(SyntaxTree.Hidden);
  6306. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6307. IF procedure.isConstructor THEN
  6308. (*! constructor is always visible, compatibility to paco
  6309. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6310. *)
  6311. procedure.MarkUsed;
  6312. IF procedureType.returnType # NIL THEN
  6313. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6314. END;
  6315. proc := procedure.scope.firstProcedure;
  6316. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6317. proc := proc.nextProcedure;
  6318. END;
  6319. IF proc # NIL THEN
  6320. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6321. ELSE
  6322. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6323. END;
  6324. END;
  6325. IF procedure.isFinalizer THEN
  6326. procedure.MarkUsed;
  6327. IF procedureType.returnType # NIL THEN
  6328. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6329. END;
  6330. IF procedureType.numberParameters # 0 THEN
  6331. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6332. END;
  6333. proc := procedure.scope.firstProcedure;
  6334. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6335. proc := proc.nextProcedure;
  6336. END;
  6337. IF proc # NIL THEN
  6338. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6339. ELSE
  6340. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6341. END;
  6342. END;
  6343. super := FindSuperProcedure(record.recordScope, procedure);
  6344. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6345. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6346. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6347. END;
  6348. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6349. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6350. END;
  6351. IF super.isFinal THEN
  6352. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6353. END;
  6354. procedure.SetSuper(super);
  6355. super.SetOverwritten(TRUE);
  6356. procedure.SetAccess(procedure.access+super.access);
  6357. procedure.MarkUsed;
  6358. END;
  6359. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6360. THEN
  6361. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6362. END;
  6363. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6364. IF cellsAreObjects THEN
  6365. procedureType.SetDelegate(TRUE);
  6366. END;
  6367. IF procedure.isConstructor THEN
  6368. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6369. END;
  6370. ELSIF procedure.isConstructor THEN
  6371. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6372. END;
  6373. Declarations(procedure.procedureScope);
  6374. (* body resolution part done as late fix of the procedure type *)
  6375. procedure.SetState(SyntaxTree.Resolved);
  6376. currentIsRealtime := recentIsRealtime;
  6377. currentIsBodyProcedure := recentIsBodyProcedure;
  6378. END;
  6379. END VisitProcedure;
  6380. (**
  6381. a builtin procedure is a global item that may not be modified locally
  6382. instead the resolving of builtin procedure calls are done in the esignator
  6383. **)
  6384. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6385. VAR type: SyntaxTree.Type;
  6386. BEGIN
  6387. type := ResolveType(builtinProcedure.type);
  6388. END VisitBuiltin;
  6389. (* nopov *)
  6390. (** check and resolve operator
  6391. - operators are first checked as procedures
  6392. - then additional operator-specific checks are done
  6393. - note that only module-scope operators are checked here
  6394. (operators in a record scope are only allowed in the context of
  6395. array-structured object types and checked in 'ResolveArrayStructure')
  6396. - also note that inter-operator conformity is not checked here
  6397. **)
  6398. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6399. VAR
  6400. procedureType: SyntaxTree.ProcedureType;
  6401. leftType, rightType: SyntaxTree.Type;
  6402. identifierNumber, position: LONGINT;
  6403. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6404. modifiers: SyntaxTree.Modifier;
  6405. (** whether a type is locally defined in the current module scope
  6406. for arrays, the base type must be locally defined **)
  6407. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6408. BEGIN
  6409. IF type = NIL THEN
  6410. RETURN FALSE
  6411. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6412. RETURN TRUE
  6413. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6414. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6415. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6416. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6417. ELSE
  6418. RETURN FALSE
  6419. END
  6420. END IsLocallyDefined;
  6421. BEGIN
  6422. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6423. procedureType := operator.type(SyntaxTree.ProcedureType);
  6424. modifiers := procedureType.modifiers;
  6425. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6426. CheckModifiers(modifiers, TRUE);
  6427. VisitProcedure(operator);
  6428. IF operator.scope IS SyntaxTree.RecordScope THEN
  6429. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6430. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6431. IF identifierNumber = -1 THEN
  6432. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6433. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6434. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6435. ELSE
  6436. IF procedureType.numberParameters < 1 THEN
  6437. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6438. ELSIF procedureType.numberParameters > 2 THEN
  6439. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6440. ELSE
  6441. (* determine operand types *)
  6442. leftType := procedureType.firstParameter.type;
  6443. IF procedureType.numberParameters > 1 THEN
  6444. rightType := procedureType.firstParameter.nextParameter.type
  6445. ELSE
  6446. rightType := NIL
  6447. END;
  6448. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6449. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6450. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6451. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6452. END
  6453. END;
  6454. (* TODO: refine the checks, think about how restrictive the checks should be
  6455. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6456. They might be used for intersection, union, complement of custom object types *)
  6457. (* defaults *)
  6458. hasReturnType := TRUE;
  6459. mustBeUnary := FALSE;
  6460. mustBeBinary := FALSE;
  6461. mustReturnBoolean := FALSE;
  6462. mustReturnInteger := FALSE;
  6463. mustHaveEquitypedOperands := FALSE;
  6464. (* operator-specific exceptions *)
  6465. CASE identifierNumber OF
  6466. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6467. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6468. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6469. mustBeBinary := TRUE
  6470. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6471. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6472. | Scanner.Times: mustBeBinary := TRUE
  6473. | Scanner.TimesTimes: mustBeBinary := TRUE
  6474. | Scanner.DotTimes: mustBeBinary := TRUE
  6475. | Scanner.PlusTimes: mustBeBinary := TRUE
  6476. | Scanner.Slash: mustBeBinary := TRUE
  6477. | Scanner.Backslash: mustBeBinary := TRUE
  6478. | Scanner.DotSlash: mustBeBinary := TRUE
  6479. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6480. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6481. | Scanner.Not: mustBeUnary := TRUE
  6482. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6483. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6484. | Scanner.Transpose: mustBeUnary := TRUE;
  6485. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6486. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6487. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6488. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6489. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6490. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6491. | Global.Abs: mustBeUnary := TRUE;
  6492. | Global.Ash: (* TODO: arity? *)
  6493. | Global.Cap: (* TODO: arity? *)
  6494. | Global.Chr: mustBeUnary := TRUE;
  6495. | Global.Entier: (* TODO: arity? *)
  6496. | Global.EntierH: (* TODO: arity? *)
  6497. | Global.Len: (* unary and binary *)
  6498. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6499. | Global.Max, Global.Min: (* unary and binary *)
  6500. | Global.Odd: (* TODO: arity? *)
  6501. | Global.Sum: (* TODO: arity? *)
  6502. | Global.All: (* TODO: arity? *)
  6503. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6504. | Scanner.Alias:
  6505. | Scanner.GreaterGreater, Scanner.LessLess:
  6506. mustBeBinary := TRUE; hasReturnType := FALSE;
  6507. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6508. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6509. END;
  6510. (* check parameter count *)
  6511. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6512. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6513. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6514. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6515. END;
  6516. (* check parameter types *)
  6517. (* TODO: is this used at all? *)
  6518. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6519. leftType := procedureType.firstParameter.type;
  6520. rightType := procedureType.firstParameter.nextParameter.type;
  6521. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6522. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6523. END
  6524. END;
  6525. (* check return type *)
  6526. IF hasReturnType THEN
  6527. IF procedureType.returnType = NIL THEN
  6528. Error(operator.position, Diagnostics.Invalid, "return type required")
  6529. ELSIF mustReturnBoolean THEN
  6530. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6531. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6532. END
  6533. ELSIF mustReturnInteger THEN
  6534. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6535. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6536. END
  6537. END
  6538. ELSIF procedureType.returnType # NIL THEN
  6539. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6540. END
  6541. END
  6542. END
  6543. END
  6544. END VisitOperator;
  6545. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6546. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6547. BEGIN
  6548. IF error THEN RETURN FALSE END;
  6549. prevScope := currentScope;
  6550. prevDiagnostics := diagnostics;
  6551. diagnostics := NIL; (* suppress error output *)
  6552. currentScope := module.moduleScope;
  6553. VisitImport(x);
  6554. IF ~error THEN
  6555. module.moduleScope.AddImport(x);
  6556. x.SetScope(module.moduleScope);
  6557. END;
  6558. currentScope := prevScope;
  6559. diagnostics := prevDiagnostics;
  6560. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6561. END AddImport;
  6562. (** check and resolve import
  6563. - check for name = SYSTEM
  6564. - check for forbidden self import
  6565. - search through global import cache: already imported?
  6566. - check if already imported indirectly
  6567. - import if necessary -> set module and enter into import cache
  6568. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6569. - after this import this direct import and all indirect imports are stored in the current module's import list
  6570. **)
  6571. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6572. VAR
  6573. module: SyntaxTree.Module;
  6574. moduleScope: SyntaxTree.ModuleScope;
  6575. import,reimport: SyntaxTree.Import;
  6576. filename: FileName;
  6577. prevScope: SyntaxTree.Scope;
  6578. BEGIN
  6579. IF SymbolNeedsResolution(x) THEN
  6580. prevScope := currentScope;
  6581. x.SetType(SyntaxTree.importType);
  6582. moduleScope := currentScope.ownerModule.moduleScope;
  6583. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6584. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6585. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6586. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6587. ELSE
  6588. (* search through global import list: already imported ? *)
  6589. IF (x.module = NIL) & (importCache # NIL) THEN
  6590. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6591. ELSE import := NIL
  6592. END;
  6593. IF x.module # NIL THEN (* already imported indirectly *)
  6594. module := x.module;
  6595. ELSIF import # NIL THEN (* already in module list *)
  6596. module := import.module;
  6597. ASSERT(module # NIL);
  6598. x.SetModule(module);
  6599. ELSE (* must be imported *)
  6600. Global.ModuleFileName(x.moduleName,x.context,filename);
  6601. IF symbolFileFormat # NIL THEN
  6602. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6603. IF module = NIL THEN
  6604. ErrorSS(x.position,"could not import",filename);
  6605. IF VerboseErrorMessage THEN
  6606. Printout.Info("import",x)
  6607. END
  6608. ELSE
  6609. (*
  6610. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6611. (*! should rather be done by importer *)
  6612. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6613. checker.importCache := importCache;
  6614. checker.arrayBaseImported := arrayBaseImported;
  6615. checker.global := global;
  6616. checker.Module(module); (* semantic check *)
  6617. error := error OR checker.error;
  6618. END;
  6619. *)
  6620. (*
  6621. ASSERT(SyntaxTree.Resolved IN module.state);
  6622. *)
  6623. x.SetModule(module);
  6624. IF importCache # NIL THEN
  6625. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6626. import.SetContext(x.context);
  6627. import.SetModule(module);
  6628. importCache.AddImport(import);
  6629. END;
  6630. END;
  6631. ELSE
  6632. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6633. END;
  6634. END;
  6635. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6636. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6637. END;
  6638. import := module.moduleScope.firstImport;
  6639. WHILE(import # NIL) DO
  6640. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6641. ASSERT(currentScope # NIL);
  6642. ASSERT(currentScope.ownerModule # NIL);
  6643. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6644. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6645. Error(x.position,Diagnostics.Invalid,"recursive import");
  6646. ELSE
  6647. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6648. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6649. IF reimport = NIL THEN (* indirect import *)
  6650. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6651. reimport.SetContext(import.context);
  6652. reimport.SetModule(import.module);
  6653. moduleScope.AddImport(reimport);
  6654. reimport.SetScope(moduleScope);
  6655. ELSE
  6656. ASSERT(import.module # NIL);
  6657. reimport.SetModule(import.module); (* direct or indirect import *)
  6658. END;
  6659. END;
  6660. import := import.nextImport;
  6661. END;
  6662. END;
  6663. END;
  6664. currentScope := prevScope;
  6665. (* ELSE nothing to be done *)
  6666. x.SetState(SyntaxTree.Resolved);
  6667. END;
  6668. END VisitImport;
  6669. (*** statements ***)
  6670. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6671. VAR prev,resolved: SyntaxTree.Statement;
  6672. BEGIN
  6673. prev := resolvedStatement;
  6674. resolvedStatement := x;
  6675. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6676. activeCellsStatement := FALSE;
  6677. x.Accept(SELF);
  6678. (* removed this, implementation restriction should be resolved by backend
  6679. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6680. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6681. END;
  6682. *)
  6683. resolved := resolvedStatement;
  6684. resolvedStatement := prev;
  6685. RETURN resolved
  6686. END ResolveStatement;
  6687. (** check and resolve statement sequence
  6688. - check all statements, replace if necessary
  6689. **)
  6690. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6691. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6692. BEGIN
  6693. IF statementSequence # NIL THEN (* else empty *)
  6694. FOR i := 0 TO statementSequence.Length()-1 DO
  6695. statement := statementSequence.GetStatement(i);
  6696. resolved := ResolveStatement(statement);
  6697. IF (resolved # statement) THEN
  6698. statementSequence.SetStatement(i,resolved);
  6699. END;
  6700. END;
  6701. END;
  6702. END StatementSequence;
  6703. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6704. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6705. - check if procedure is callable
  6706. - check return type = NIL (otherwise must be assignment statement)
  6707. **)
  6708. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6709. VAR call: SyntaxTree.Designator;
  6710. BEGIN
  6711. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6712. call := procedureCall.call;
  6713. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6714. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6715. END;
  6716. call := ResolveDesignator(call);
  6717. IF call = SyntaxTree.invalidDesignator THEN
  6718. (* error already handled *)
  6719. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6720. (* inline call in a statement *)
  6721. ELSIF ~IsCallable(call) THEN
  6722. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6723. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6724. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6725. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6726. END;
  6727. procedureCall.SetCall(call);
  6728. (*
  6729. IF call = SyntaxTree.invalidDesignator THEN
  6730. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6731. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6732. IF IsOberonInline(procedure) THEN
  6733. Warning(procedure.position,"call to inline proc");
  6734. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6735. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6736. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6737. resolvedStatement := block;
  6738. RETURN;
  6739. END;
  6740. END;
  6741. *)
  6742. END VisitProcedureCallStatement;
  6743. (** check and resolve assignment LHS := RHS
  6744. - resolve LHS and RHS
  6745. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6746. - check if assignment is compatible
  6747. - check if LHS is variable (i.e. assignable)
  6748. - convert RHS if necessary
  6749. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6750. - assignment between different ASOTs
  6751. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6752. - assignment to ASOT elements:
  6753. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6754. **)
  6755. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6756. VAR
  6757. left: SyntaxTree.Designator;
  6758. right, expression: SyntaxTree.Expression;
  6759. designator: SyntaxTree.Designator;
  6760. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6761. mathArrayType: SyntaxTree.MathArrayType;
  6762. BEGIN
  6763. right := ResolveExpression(assignment.right);
  6764. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6765. left := ResolveDesignator(assignment.left);
  6766. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6767. (* error already handled *)
  6768. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6769. (* LHS is index write operator call on ASOT *)
  6770. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6771. (* necessary ?
  6772. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6773. type := procedureType.firstParameter.type;
  6774. expression := procedureCallDesignator.parameters.GetExpression(0);
  6775. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6776. *)
  6777. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6778. ELSIF CheckVariable(left) THEN
  6779. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6780. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6781. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6782. (* conversion done by procedure call
  6783. (* try to convert to left argument *)
  6784. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6785. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6786. procedureCallDesignator.parameters.SetExpression(1, right);
  6787. END;
  6788. *)
  6789. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6790. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6791. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6792. ELSIF AssignmentCompatible(left, right) THEN
  6793. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6794. mathArrayType := MathArrayStructureOfType(left.type);
  6795. right := NewConversion(right.position, right, mathArrayType, NIL);
  6796. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6797. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6798. ELSE
  6799. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6800. assignment.SetLeft(left);
  6801. assignment.SetRight(right);
  6802. resolvedStatement := assignment
  6803. END
  6804. END
  6805. END
  6806. END VisitAssignment;
  6807. (** check and resolve assignment LHS := RHS
  6808. - resolve LHS and RHS
  6809. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6810. - check if assignment is compatible
  6811. - check if LHS is variable (i.e. assignable)
  6812. - convert RHS if necessary
  6813. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6814. - assignment between different ASOTs
  6815. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6816. - assignment to ASOT elements:
  6817. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6818. **)
  6819. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6820. VAR
  6821. left: SyntaxTree.Designator;
  6822. right: SyntaxTree.Expression;
  6823. inPort, outPort: SyntaxTree.PortType;
  6824. expression: SyntaxTree.Expression;
  6825. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6826. BEGIN
  6827. right := ResolveExpression(communication.right);
  6828. left := ResolveDesignator(communication.left);
  6829. communication.SetLeft(left);
  6830. communication.SetRight(right);
  6831. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6832. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6833. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6834. (* conversion done by procedure call
  6835. (* try to convert to left argument *)
  6836. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6837. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6838. procedureCallDesignator.parameters.SetExpression(1, right);
  6839. END;
  6840. *)
  6841. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6842. ELSE
  6843. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6844. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6845. (* error already handled *)
  6846. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6847. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6848. IF outPort.direction # SyntaxTree.OutPort THEN
  6849. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6850. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6851. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6852. ELSE
  6853. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6854. communication.SetRight(right)
  6855. END;
  6856. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6857. IF CheckVariable(left) THEN
  6858. IF inPort.direction # SyntaxTree.InPort THEN
  6859. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6860. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6861. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6862. END;
  6863. END;
  6864. ELSE
  6865. Error(communication.position, -1, "unsupported stream operation");
  6866. END;
  6867. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6868. IF outPort.direction # SyntaxTree.OutPort THEN
  6869. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6870. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6871. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6872. ELSE
  6873. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6874. communication.SetRight(right)
  6875. END;
  6876. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6877. IF CheckVariable(right) THEN
  6878. IF inPort.direction # SyntaxTree.InPort THEN
  6879. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6880. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6881. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6882. END;
  6883. END;
  6884. ELSE
  6885. Error(communication.position, -1, "unsupported operation");
  6886. END;
  6887. END;
  6888. END VisitCommunicationStatement;
  6889. (** check and resolve if/eslif part
  6890. - check condition
  6891. - check statement sequence
  6892. **)
  6893. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6894. VAR prevUnreachable, b: BOOLEAN;
  6895. BEGIN
  6896. prevUnreachable := currentIsUnreachable;
  6897. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6898. IF IsBooleanValue(ifPart.condition,b) THEN
  6899. IF b=FALSE THEN
  6900. currentIsUnreachable := TRUE
  6901. ELSIF b=TRUE THEN
  6902. true := TRUE
  6903. END;
  6904. END;
  6905. StatementSequence(ifPart.statements);
  6906. currentIsUnreachable := prevUnreachable;
  6907. END IfPart;
  6908. (** check and resolve if statement
  6909. - check if parts and else part statement sequence
  6910. **)
  6911. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6912. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6913. BEGIN
  6914. prevUnreachable := currentIsUnreachable;
  6915. ifPartTrue := FALSE;
  6916. IfPart(ifStatement.ifPart,ifPartTrue);
  6917. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6918. elsif := ifStatement.GetElsifPart(i);
  6919. IfPart(elsif,ifPartTrue);
  6920. END;
  6921. IF ifStatement.elsePart # NIL THEN
  6922. IF ifPartTrue THEN
  6923. currentIsUnreachable := TRUE
  6924. END;
  6925. StatementSequence(ifStatement.elsePart)
  6926. END;
  6927. currentIsUnreachable := prevUnreachable;
  6928. END VisitIfStatement;
  6929. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6930. VAR variable: SyntaxTree.Designator;
  6931. type,variableType: SyntaxTree.Type;
  6932. withEntry: WithEntry;
  6933. BEGIN
  6934. variable := ResolveDesignator(withPart.variable);
  6935. variableType := variable.type.resolved;
  6936. withPart.SetVariable(variable);
  6937. type := ResolveType(withPart.type);
  6938. withPart.SetType(type);
  6939. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6940. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6941. END;
  6942. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6943. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6944. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6945. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6946. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  6947. IF VerboseErrorMessage THEN
  6948. Printout.Info("variable",variable)
  6949. END;
  6950. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  6951. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  6952. IF VerboseErrorMessage THEN
  6953. Printout.Info("variable",variable);
  6954. Printout.Info("type",type);
  6955. END;
  6956. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  6957. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  6958. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  6959. IF VerboseErrorMessage THEN
  6960. Printout.Info("variable",variable);
  6961. Printout.Info("type",type);
  6962. END;
  6963. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  6964. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  6965. ELSE
  6966. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6967. NEW(withEntry);
  6968. withEntry.previous := withEntries;
  6969. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  6970. withEntry.type := type;
  6971. withEntries := withEntry;
  6972. StatementSequence(withPart.statements);
  6973. withEntries := withEntries.previous;
  6974. END;
  6975. END WithPart;
  6976. (** check and resolve with statement WITH variable: type DO ... END;
  6977. - check type and variable
  6978. - check that variable type is type extension of type
  6979. - check that variable is a variable
  6980. - enter new with scope and enter guardedVariable with same name and reference to variable
  6981. - create if statement:
  6982. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  6983. **)
  6984. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  6985. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  6986. BEGIN
  6987. prevScope := currentScope; symbol := NIL;
  6988. FOR i := 0 TO withStatement.WithParts()-1 DO
  6989. WithPart(withStatement.GetWithPart(i),symbol);
  6990. END;
  6991. IF withStatement.elsePart # NIL THEN
  6992. StatementSequence(withStatement.elsePart)
  6993. END;
  6994. currentScope := prevScope;
  6995. END VisitWithStatement;
  6996. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  6997. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  6998. - check 'first' < 'last' and no overlaps between different case labels
  6999. - check statement sequence
  7000. **)
  7001. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7002. VAR
  7003. i: LONGINT;
  7004. position: LONGINT;
  7005. expression, left, right: SyntaxTree.Expression;
  7006. expressionType: SyntaxTree.Type;
  7007. l, r: LONGINT;
  7008. cl, cr: CHAR;
  7009. thiscases: SyntaxTree.CaseConstant;
  7010. BEGIN
  7011. thiscases := NIL;
  7012. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7013. expression := casePart.elements.GetExpression(i);
  7014. position := expression.position;
  7015. (* set context of range *)
  7016. IF expression IS SyntaxTree.RangeExpression THEN
  7017. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7018. END;
  7019. expression := ResolveExpression(expression);
  7020. IF expression = SyntaxTree.invalidExpression THEN
  7021. (* error already reported *)
  7022. expressionType := SyntaxTree.invalidType;
  7023. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7024. (* read out 'first' and 'last' *)
  7025. left := expression(SyntaxTree.RangeExpression).first;
  7026. right := expression(SyntaxTree.RangeExpression).last;
  7027. (* guaranteed by VisitRangeExpression: *)
  7028. ASSERT((left # NIL) & (right # NIL));
  7029. ASSERT(left.type.resolved = right.type.resolved);
  7030. left := CompatibleConversion(left.position, left, type);
  7031. right := CompatibleConversion(right.position, right, type);
  7032. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7033. expression(SyntaxTree.RangeExpression).SetLast(right);
  7034. expressionType := RegularType(position,left.type);
  7035. ELSE
  7036. expression := ConstantExpression(expression);
  7037. expression := CompatibleConversion(expression.position, expression, type);
  7038. (*
  7039. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7040. left := Global.NewCharacterValue(system,expression.position,cl);
  7041. expression := casePart.elements.GetExpression(i);
  7042. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7043. expression := left
  7044. END;
  7045. *)
  7046. casePart.elements.SetExpression(i,expression);
  7047. left := expression; right := expression;
  7048. expressionType := RegularType(position,expression.type)
  7049. END;
  7050. IF (expressionType = SyntaxTree.invalidType) THEN
  7051. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7052. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7053. expression := SyntaxTree.invalidExpression;
  7054. ELSE
  7055. l := 0; r := 0;
  7056. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7057. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7058. l := ORD(cl); r := ORD(cr);
  7059. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7060. ELSE
  7061. expression := SyntaxTree.invalidExpression
  7062. END;
  7063. IF expression # SyntaxTree.invalidExpression THEN
  7064. IF l>r THEN
  7065. Error(position,Diagnostics.Invalid,"empty case label")
  7066. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7067. Error(position,Diagnostics.Invalid,"duplicate case label");
  7068. ELSE
  7069. IF l < min THEN min := l END;
  7070. IF r > max THEN max := r END;
  7071. END;
  7072. END;
  7073. END;
  7074. casePart.elements.SetExpression(i,expression);
  7075. END;
  7076. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7077. casePart.SetConstants(thiscases);
  7078. StatementSequence(casePart.statements);
  7079. END CasePart;
  7080. (** check and resolve case statement CASE variable OF ... END;
  7081. - check variable
  7082. - check case parts
  7083. **)
  7084. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7085. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7086. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7087. BEGIN
  7088. expression := ResolveExpression(caseStatement.variable);
  7089. type := RegularType(expression.position,expression.type);
  7090. IF type = SyntaxTree.invalidType THEN
  7091. expression := SyntaxTree.invalidExpression;
  7092. ELSIF IsIntegerType(type) THEN
  7093. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7094. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7095. (*
  7096. expression := Global.NewCharacterValue(system,expression.position,ch);
  7097. *)
  7098. type := expression.type;
  7099. ELSIF IsCharacterType(type) THEN
  7100. ELSIF IsEnumerationType(type) THEN
  7101. ELSE
  7102. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7103. expression := SyntaxTree.invalidExpression;
  7104. END;
  7105. caseStatement.SetVariable(expression);
  7106. caseList := NIL;
  7107. min := MAX(LONGINT); max := MIN(LONGINT);
  7108. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7109. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7110. END;
  7111. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7112. msg := "huge sparse case table ";
  7113. Strings.AppendInt(msg, max-min);
  7114. Strings.Append(msg,"/");
  7115. Strings.AppendInt(msg, caseStatement.CaseParts());
  7116. Warning(caseStatement.position,msg);
  7117. END;
  7118. caseStatement.SetMinMax(min,max);
  7119. StatementSequence(caseStatement.elsePart);
  7120. IF expression.resolved # NIL THEN
  7121. IF IsCharacterValue(expression,ch) THEN
  7122. l := ORD(ch)
  7123. ELSIF IsIntegerValue(expression,l) THEN
  7124. END;
  7125. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7126. END;
  7127. END VisitCaseStatement;
  7128. (** check and resolve while statement
  7129. - check condition
  7130. - check statement sequence
  7131. **)
  7132. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7133. VAR prevIsUnreachable,b: BOOLEAN;
  7134. BEGIN
  7135. prevIsUnreachable := currentIsUnreachable;
  7136. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7137. IF IsBooleanValue(whileStatement.condition,b) THEN
  7138. IF b=FALSE THEN
  7139. currentIsUnreachable := TRUE
  7140. END;
  7141. END;
  7142. StatementSequence(whileStatement.statements);
  7143. currentIsUnreachable := prevIsUnreachable
  7144. END VisitWhileStatement;
  7145. (** check and resolve repeat statement
  7146. - check condition
  7147. - check statement sequence
  7148. **)
  7149. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7150. BEGIN
  7151. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7152. StatementSequence(repeatStatement.statements);
  7153. END VisitRepeatStatement;
  7154. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7155. VAR withEntry: WithEntry;
  7156. BEGIN
  7157. withEntry := withEntries;
  7158. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7159. withEntry := withEntry.previous
  7160. END;
  7161. IF withEntry = NIL THEN RETURN FALSE
  7162. ELSE
  7163. type := withEntry.type;
  7164. RETURN TRUE
  7165. END;
  7166. END GetGuard;
  7167. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7168. - check that variable is an integer variable
  7169. - check that from is integer typed with compatible type
  7170. - check that to has compatible type
  7171. - check that by is constant integer with compatible type
  7172. **)
  7173. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7174. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7175. BEGIN
  7176. designator := ResolveDesignator(forStatement.variable);
  7177. type := SyntaxTree.invalidType;
  7178. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7179. designator := SyntaxTree.invalidDesignator;
  7180. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7181. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7182. designator := SyntaxTree.invalidDesignator;
  7183. ELSIF CheckVariable(designator) THEN
  7184. type := designator.type;
  7185. END;
  7186. forStatement.SetVariable(designator);
  7187. expression := ResolveExpression(forStatement.from);
  7188. IF expression = SyntaxTree.invalidExpression THEN
  7189. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7190. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7191. expression := SyntaxTree.invalidExpression;
  7192. ELSIF type # SyntaxTree.invalidType THEN
  7193. expression := NewConversion(expression.position,expression,type,NIL)
  7194. END;
  7195. forStatement.SetFrom(expression);
  7196. expression := ResolveExpression(forStatement.to);
  7197. IF expression = SyntaxTree.invalidExpression THEN
  7198. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7199. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7200. expression := SyntaxTree.invalidExpression;
  7201. ELSIF type # SyntaxTree.invalidType THEN
  7202. expression := NewConversion(expression.position,expression,type,NIL)
  7203. END;
  7204. forStatement.SetTo(expression);
  7205. IF forStatement.by # NIL THEN
  7206. expression := ConstantInteger(forStatement.by);
  7207. ELSE
  7208. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7209. END;
  7210. IF expression = SyntaxTree.invalidExpression THEN
  7211. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7212. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7213. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7214. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7215. ELSIF type # SyntaxTree.invalidType THEN
  7216. expression := NewConversion(expression.position,expression,type,NIL)
  7217. END;
  7218. forStatement.SetBy(expression);
  7219. StatementSequence(forStatement.statements);
  7220. END VisitForStatement;
  7221. (** check and resolve loop statement LOOP StatementSequence END
  7222. - check statement sequence
  7223. **)
  7224. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7225. BEGIN
  7226. StatementSequence(loopStatement.statements)
  7227. END VisitLoopStatement;
  7228. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7229. BEGIN
  7230. StatementSequence(exitableBlock.statements);
  7231. END VisitExitableBlock;
  7232. (** check and resolve exit statement EXIT
  7233. - check that exit is within LOOP statement block
  7234. **)
  7235. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7236. VAR outer: SyntaxTree.Statement;
  7237. BEGIN
  7238. outer := exitStatement.outer;
  7239. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7240. outer := outer.outer;
  7241. END;
  7242. IF outer = NIL THEN
  7243. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7244. END;
  7245. END VisitExitStatement;
  7246. (** check and resolve return statement RETURN [expression]
  7247. - check expression (if any)
  7248. - check if in procedure scope
  7249. - if in procedure scope then check expression compatibility
  7250. - if not in procecdure scope then check on return without expression
  7251. **)
  7252. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7253. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7254. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7255. BEGIN
  7256. position := returnStatement.position;
  7257. expression := returnStatement.returnValue;
  7258. IF expression # NIL THEN
  7259. expression := ResolveExpression(expression);
  7260. returnStatement.SetReturnValue(expression);
  7261. END;
  7262. outer := returnStatement.outer;
  7263. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7264. outer := outer.outer
  7265. END;
  7266. IF (outer # NIL) THEN
  7267. scope := outer(SyntaxTree.Body).inScope;
  7268. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7269. IF (expression # NIL) THEN
  7270. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7271. END;
  7272. ELSE
  7273. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7274. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7275. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7276. END;
  7277. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7278. IF returnType # NIL THEN
  7279. returnType := returnType.resolved;
  7280. IF expression = NIL THEN
  7281. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7282. ELSIF expression.type = NIL THEN
  7283. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7284. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7285. Error(position,Diagnostics.Invalid,"return type not compatible");
  7286. IF VerboseErrorMessage THEN
  7287. Printout.Info("returnType",returnType);
  7288. Printout.Info("expression",expression);
  7289. END;
  7290. ELSE
  7291. expression := NewConversion(expression.position,expression,returnType,NIL);
  7292. returnStatement.SetReturnValue(expression);
  7293. END;
  7294. ELSIF expression # NIL THEN
  7295. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7296. END;
  7297. END;
  7298. END;
  7299. END VisitReturnStatement;
  7300. (** check and resolve await statement AWAIT(condition: Expression)
  7301. - check await condition
  7302. **)
  7303. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7304. VAR condition: SyntaxTree.Expression;
  7305. BEGIN
  7306. condition := ResolveCondition(awaitStatement.condition);
  7307. IF currentIsRealtime THEN
  7308. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7309. END;
  7310. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7311. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7312. END;
  7313. awaitStatement.SetCondition(condition);
  7314. END VisitAwaitStatement;
  7315. PROCEDURE CheckSystemImport(position: LONGINT);
  7316. VAR import: SyntaxTree.Import;
  7317. BEGIN
  7318. import := currentScope.ownerModule.moduleScope.firstImport;
  7319. WHILE(import # NIL) DO
  7320. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7321. RETURN;
  7322. END;
  7323. import := import.nextImport;
  7324. END;
  7325. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7326. END CheckSystemImport;
  7327. (** check and resolve code statement: do nothing, must be done by assembler
  7328. **)
  7329. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7330. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7331. BEGIN
  7332. CheckSystemImport(code.position);
  7333. FOR i := 0 TO code.inRules.Length()-1 DO
  7334. statement := code.inRules.GetStatement(i);
  7335. IF statement IS SyntaxTree.Assignment THEN
  7336. WITH statement: SyntaxTree.Assignment DO
  7337. statement.SetRight(ResolveExpression(statement.right));
  7338. END;
  7339. ELSE
  7340. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7341. END;
  7342. END;
  7343. FOR i := 0 TO code.outRules.Length()-1 DO
  7344. statement := code.outRules.GetStatement(i);
  7345. IF statement IS SyntaxTree.Assignment THEN
  7346. WITH statement: SyntaxTree.Assignment DO
  7347. statement.SetLeft(ResolveDesignator(statement.left));
  7348. END;
  7349. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7350. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7351. ELSE
  7352. Printout.Info("out statement ", statement);
  7353. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7354. END;
  7355. END;
  7356. END VisitCode;
  7357. (** check and set flags of a statement block
  7358. - check for multiply occurence of a flag
  7359. - check and set priority only in bodies
  7360. - check for valid names
  7361. **)
  7362. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7363. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7364. flag: LONGINT; recordBody: SyntaxTree.Body;
  7365. PROCEDURE SetProtectedRecord;
  7366. VAR scope: SyntaxTree.Scope;
  7367. BEGIN
  7368. scope := currentScope;
  7369. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7370. scope := scope.outerScope
  7371. END;
  7372. IF scope # NIL THEN
  7373. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7374. END;
  7375. END SetProtectedRecord;
  7376. BEGIN
  7377. flags := {};
  7378. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7379. recordBody := block(SyntaxTree.Body)
  7380. ELSE
  7381. recordBody := NIL
  7382. END;
  7383. blockModifier := block.blockModifiers;
  7384. WHILE(blockModifier # NIL) DO
  7385. name := blockModifier.identifier;
  7386. expression := blockModifier.expression;
  7387. position := blockModifier.position;
  7388. flag := -1;
  7389. IF name=Global.NamePriority THEN
  7390. IF expression = NIL THEN
  7391. Error(position,Diagnostics.Invalid,"missing priority expression");
  7392. ELSIF recordBody = NIL THEN
  7393. Error(position,Diagnostics.Invalid,"priority not on record body");
  7394. ELSIF recordBody.priority # NIL THEN
  7395. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7396. ELSE
  7397. recordBody.SetPriority(expression);
  7398. END;
  7399. ELSIF expression # NIL THEN
  7400. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7401. ELSIF name=Global.NameExclusive THEN
  7402. IF block.isExclusive THEN
  7403. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7404. END;
  7405. block.SetExclusive(TRUE); SetProtectedRecord;
  7406. ELSIF name=Global.NameActive THEN
  7407. IF recordBody = NIL THEN
  7408. Error(position,Diagnostics.Invalid,"active not in record body");
  7409. ELSIF recordBody.isActive THEN
  7410. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7411. ELSE
  7412. recordBody.SetActive(TRUE); SetProtectedRecord;
  7413. END;
  7414. ELSIF name=Global.NameSafe THEN
  7415. IF recordBody = NIL THEN
  7416. Error(position,Diagnostics.Invalid,"safe not in record body");
  7417. ELSIF recordBody.isSafe THEN
  7418. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7419. ELSE
  7420. recordBody.SetSafe(TRUE);
  7421. SetProtectedRecord;
  7422. END;
  7423. ELSIF name=Global.NameRealtime THEN
  7424. IF recordBody = NIL THEN
  7425. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7426. ELSIF recordBody.isRealtime THEN
  7427. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7428. ELSE
  7429. recordBody.SetRealtime(TRUE);
  7430. block.SetRealtime(TRUE);
  7431. END;
  7432. ELSIF name=Global.NameUnchecked THEN
  7433. IF block.isUnchecked THEN
  7434. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7435. ELSE
  7436. block.SetUnchecked(TRUE);
  7437. END;
  7438. ELSIF (name=Global.NameUncooperative) THEN
  7439. IF block.isUncooperative THEN
  7440. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7441. ELSE
  7442. block.SetUncooperative(TRUE);
  7443. END;
  7444. ELSE
  7445. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7446. END;
  7447. blockModifier := blockModifier.nextModifier;
  7448. END;
  7449. END BlockFlags;
  7450. (** check and resolve statement block
  7451. - check flags (exclusive)
  7452. - check statement sequence
  7453. **)
  7454. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7455. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7456. BEGIN
  7457. BlockFlags(statementBlock);
  7458. IF statementBlock.isExclusive THEN
  7459. (* check that not in exclusive block *)
  7460. IF currentIsExclusive THEN
  7461. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7462. ELSIF currentIsRealtime THEN
  7463. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7464. END;
  7465. END;
  7466. recentExclusive := currentIsExclusive;
  7467. recentUnreachable := currentIsUnreachable;
  7468. recentRealtime := currentIsRealtime;
  7469. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7470. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7471. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7472. StatementSequence(statementBlock.statements);
  7473. currentIsRealtime := recentRealtime;
  7474. currentIsExclusive := recentExclusive;
  7475. currentIsUnreachable := recentUnreachable;
  7476. END VisitStatementBlock;
  7477. (** check and resolve body
  7478. - check flags (active, priority, safe)
  7479. - check body and finally part
  7480. **)
  7481. PROCEDURE Body(body: SyntaxTree.Body);
  7482. BEGIN
  7483. VisitStatementBlock(body);
  7484. IF body.isActive THEN
  7485. IF ~currentIsBodyProcedure THEN
  7486. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7487. ELSIF body.priority # NIL THEN
  7488. body.SetPriority(ConstantInteger(body.priority));
  7489. END;
  7490. ELSIF body.isSafe THEN
  7491. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7492. ELSIF body.priority # NIL THEN
  7493. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7494. END;
  7495. IF body.code # NIL THEN
  7496. CheckSystemImport(body.position);
  7497. END;
  7498. StatementSequence(body.finally)
  7499. END Body;
  7500. (*** scopes ***)
  7501. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7502. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7503. VAR duplicateSymbol: BOOLEAN;
  7504. BEGIN
  7505. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7506. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7507. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7508. END;
  7509. scope.EnterSymbol(symbol,duplicateSymbol);
  7510. IF ~allowDuplicate & duplicateSymbol THEN
  7511. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7512. IF VerboseErrorMessage THEN
  7513. Printout.Info("multiply defined identifier",symbol);
  7514. Printout.Info("in scope",scope);
  7515. END;
  7516. END;
  7517. END Register;
  7518. (**
  7519. implementation: check and resolve an implementation part
  7520. **)
  7521. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7522. move implementation checker to a separate object ? *)
  7523. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7524. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7525. BEGIN
  7526. prevIsRealtime := currentIsRealtime;
  7527. prevIsBodyProcedure := currentIsBodyProcedure;
  7528. prevIsCellNet := currentIsCellNet;
  7529. prevScope := currentScope;
  7530. currentScope := scope;
  7531. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7532. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7533. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7534. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7535. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7536. (*
  7537. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7538. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7539. END;
  7540. *)
  7541. END;
  7542. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7543. (* module body, record bodies are wrapped into an artifical procedure *)
  7544. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7545. Body(scope(SyntaxTree.ProcedureScope).body)
  7546. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7547. Body(scope(SyntaxTree.ProcedureScope).body)
  7548. END;
  7549. END;
  7550. currentScope := prevScope;
  7551. currentIsRealtime := prevIsRealtime;
  7552. currentIsBodyProcedure := prevIsBodyProcedure;
  7553. currentIsCellNet := prevIsCellNet;
  7554. END Implementation;
  7555. (** implementation phase:
  7556. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7557. **)
  7558. PROCEDURE Implementations(x: SyntaxTree.Module);
  7559. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7560. BEGIN
  7561. prevPhase := phase;
  7562. phase := InlinePhase;
  7563. scope := x.firstScope;
  7564. WHILE(scope # NIL) DO
  7565. Implementation(scope);
  7566. scope := scope.nextScope;
  7567. END;
  7568. phase := ImplementationPhase;
  7569. scope := x.firstScope;
  7570. WHILE(scope # NIL) DO
  7571. Implementation(scope);
  7572. scope := scope.nextScope;
  7573. END;
  7574. phase := prevPhase;
  7575. END Implementations;
  7576. (** declaration phase:
  7577. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7578. - import lists (for module scopes)
  7579. - parameter list (for procedure scopes)
  7580. - constant declarations
  7581. - type declarations
  7582. - variable declarations
  7583. - procedure declarations
  7584. preformed in two stages:
  7585. - first all symbols are entered into the symbol table (with uniqueness check),
  7586. - then all symbols are resolved
  7587. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7588. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7589. **)
  7590. PROCEDURE Declarations(scope: SyntaxTree.Scope);
  7591. VAR
  7592. constant: SyntaxTree.Constant;
  7593. typeDeclaration: SyntaxTree.TypeDeclaration;
  7594. variable: SyntaxTree.Variable;
  7595. procedure: SyntaxTree.Procedure;
  7596. prevScope: SyntaxTree.Scope;
  7597. parameter: SyntaxTree.Parameter;
  7598. import: SyntaxTree.Import;
  7599. symbol: SyntaxTree.Symbol;
  7600. prevPhase: LONGINT;
  7601. prevError : BOOLEAN;
  7602. type: SyntaxTree.Type;
  7603. atype : SyntaxTree.ArrayType;
  7604. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7605. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7606. BEGIN
  7607. IF type.baseType # NIL THEN
  7608. baseType := type.baseType.resolved;
  7609. IF baseType IS SyntaxTree.PointerType THEN
  7610. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7611. END;
  7612. IF baseType IS SyntaxTree.CellType THEN
  7613. DeclareCell(baseType(SyntaxTree.CellType));
  7614. END;
  7615. END;
  7616. parameter := type.firstParameter;
  7617. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7618. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7619. variable.SetType(parameter.type);
  7620. variable.SetAccess(SyntaxTree.Hidden);
  7621. variable.SetModifiers(parameter.modifiers);
  7622. currentScope.PushVariable(variable);
  7623. (*
  7624. Register(parameter,scope, FALSE);
  7625. *)
  7626. parameter := parameter.nextParameter;
  7627. END;
  7628. property := type.firstProperty;
  7629. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7630. variable := currentScope.FindVariable(property.name);
  7631. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7632. prop := variable(SyntaxTree.Property);
  7633. ELSE (* add, duplicate symbols detection later *)
  7634. prop := SyntaxTree.NewProperty(property.position, property.name);
  7635. currentScope.PushVariable(prop);
  7636. END;
  7637. prop.SetType(property.type);
  7638. prop.SetValue(property.value);
  7639. prop.SetAccess(SyntaxTree.Hidden);
  7640. property := property.nextProperty;
  7641. END;
  7642. END DeclareCell;
  7643. BEGIN
  7644. prevError := error;
  7645. prevPhase := phase;
  7646. phase := DeclarationPhase;
  7647. prevScope := currentScope;
  7648. currentScope := scope;
  7649. error := FALSE;
  7650. (* first enter all symbols in scope *)
  7651. IF scope IS SyntaxTree.ModuleScope THEN
  7652. (* treat imports first for a module scope, , set default context if necessary *)
  7653. import := scope(SyntaxTree.ModuleScope).firstImport;
  7654. WHILE(import # NIL) DO
  7655. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7656. Register(import, currentScope, FALSE);
  7657. import := import.nextImport;
  7658. END;
  7659. import := scope(SyntaxTree.ModuleScope).firstImport;
  7660. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7661. ResolveSymbol(import);
  7662. import := import.nextImport;
  7663. END;
  7664. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7665. (* enter parameters for a procedure scope *)
  7666. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7667. WHILE(parameter # NIL) DO
  7668. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7669. END;
  7670. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7671. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7672. ELSIF scope IS SyntaxTree.CellScope THEN
  7673. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7674. END;
  7675. IF error THEN RETURN END;
  7676. (* constants *)
  7677. constant := scope.firstConstant;
  7678. WHILE (constant # NIL) DO
  7679. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7680. END;
  7681. (* type declarations *)
  7682. typeDeclaration := scope.firstTypeDeclaration;
  7683. WHILE (typeDeclaration # NIL) DO
  7684. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7685. END;
  7686. (* variables *)
  7687. variable := scope.firstVariable;
  7688. WHILE (variable # NIL) DO
  7689. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7690. END;
  7691. (* procedures *)
  7692. procedure := scope.firstProcedure;
  7693. WHILE (procedure # NIL) DO
  7694. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7695. END;
  7696. (* now process all symbols without any presumption on the order *)
  7697. symbol := scope.firstSymbol;
  7698. WHILE(symbol # NIL) DO
  7699. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7700. ResolveSymbol(symbol);
  7701. END;
  7702. symbol := symbol.nextSymbol;
  7703. END;
  7704. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7705. symbol := scope.firstSymbol;
  7706. WHILE symbol # NIL DO
  7707. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7708. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7709. pointerFixes.Add(symbol, currentScope);
  7710. END;
  7711. IF ~symbol.type.resolved.isRealtime THEN
  7712. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7713. END;
  7714. END;
  7715. symbol := symbol.nextSymbol
  7716. END;
  7717. END;
  7718. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7719. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7720. END;
  7721. IF scope.ownerModule # NIL THEN
  7722. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7723. scope.ownerModule.AddScope(scope);
  7724. END;
  7725. phase := prevPhase;
  7726. currentScope := prevScope;
  7727. error := error OR prevError;
  7728. END Declarations;
  7729. (* nopov *)
  7730. (** check if all operators from one module are compatible to the ones in the other module
  7731. - check if there are not multiple operators with the same signature
  7732. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7733. - check for all operators whose signatures are compatible, whether the return types are compatible
  7734. note that:
  7735. - the return type is not considered to be part of the signature
  7736. - two signatures are considered compatible, if all of the operands are compatible
  7737. **)
  7738. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7739. VAR
  7740. thisOperator, thatOperator: SyntaxTree.Operator;
  7741. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7742. thisParameter, thatParameter: SyntaxTree.Parameter;
  7743. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7744. i: LONGINT;
  7745. BEGIN
  7746. currentScope := thisModuleScope;
  7747. hasError := FALSE;
  7748. (* go through all operators in the other module *)
  7749. thatOperator := thatModuleScope.firstOperator;
  7750. WHILE (thatOperator # NIL) & ~hasError DO
  7751. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7752. (* the other operator is accessible *)
  7753. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7754. (* the other operator is not the conversion operator *)
  7755. (* go through all operators in this module *)
  7756. thisOperator := thisModuleScope.firstOperator;
  7757. WHILE (thisOperator # NIL) & ~hasError DO
  7758. IF thisOperator # thatOperator THEN
  7759. (* the operators are not the same *)
  7760. IF thisOperator.name = thatOperator.name THEN
  7761. (* the operators share the same identifier *)
  7762. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7763. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7764. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7765. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7766. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7767. (* both operators have the same paramter count *)
  7768. thisParameter := thisProcedureType.firstParameter;
  7769. thatParameter := thatProcedureType.firstParameter;
  7770. operandsAreEqual := TRUE;
  7771. operandsAreCompatible := TRUE;
  7772. (* go through all parameters *)
  7773. FOR i := 1 TO thisProcedureType.numberParameters DO
  7774. ASSERT(thatParameter # NIL);
  7775. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7776. operandsAreEqual := FALSE;
  7777. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7778. operandsAreCompatible := FALSE
  7779. END
  7780. END;
  7781. thisParameter := thisParameter.nextParameter;
  7782. thatParameter := thatParameter.nextParameter
  7783. END;
  7784. IF operandsAreEqual THEN
  7785. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7786. hasError := TRUE
  7787. ELSIF operandsAreCompatible THEN
  7788. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7789. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7790. hasError := TRUE
  7791. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7792. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7793. hasError := TRUE
  7794. END
  7795. END
  7796. END
  7797. END
  7798. END;
  7799. thisOperator := thisOperator.nextOperator
  7800. END
  7801. END
  7802. END;
  7803. thatOperator := thatOperator.nextOperator
  7804. END
  7805. END CheckInterOperatorConformity;
  7806. (** check module:
  7807. - check module declaration
  7808. - add context, if necessary
  7809. - remove module from import cache, if necessary
  7810. - check declarations
  7811. - resolve all type fixes
  7812. - check implementation (bodies)
  7813. **)
  7814. PROCEDURE Module*(x: SyntaxTree.Module);
  7815. VAR (* nopov *)
  7816. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7817. BEGIN
  7818. prevScope := currentScope;
  7819. prevIsCellNet := currentIsCellNet;
  7820. module := x;
  7821. ASSERT(x # NIL);
  7822. global := system.globalScope[x.case];
  7823. x.moduleScope.SetGlobalScope(global);
  7824. currentScope := global;
  7825. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7826. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7827. RemoveModuleFromCache(importCache,x);
  7828. Declarations(x.moduleScope);
  7829. FixTypes();
  7830. IF module.isCellNet THEN
  7831. currentIsCellNet := TRUE;
  7832. modifier := x.modifiers;
  7833. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7834. CheckModifiers(modifier, FALSE);
  7835. END;
  7836. (* nopov *)
  7837. IF ~error THEN
  7838. (* check if operators conform to each other within this module *)
  7839. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7840. (* go through all imports *)
  7841. import := x.moduleScope.firstImport;
  7842. WHILE import # NIL DO
  7843. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7844. (* check if all operators in this module conform to the ones of the imported module *)
  7845. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7846. END;
  7847. import := import.nextImport
  7848. END;
  7849. END;
  7850. Implementations(x);
  7851. module := NIL;
  7852. currentIsCellNet := prevIsCellNet;
  7853. currentScope := prevScope;
  7854. END Module;
  7855. END Checker;
  7856. Warnings*=OBJECT (SyntaxTree.Visitor)
  7857. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7858. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7859. BEGIN
  7860. SELF.diagnostics := diagnostics
  7861. END InitWarnings;
  7862. (** types *)
  7863. PROCEDURE Type(x: SyntaxTree.Type);
  7864. BEGIN x.Accept(SELF)
  7865. END Type;
  7866. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7867. BEGIN END VisitType;
  7868. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7869. BEGIN END VisitBasicType;
  7870. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7871. BEGIN END VisitCharacterType;
  7872. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7873. BEGIN END VisitIntegerType;
  7874. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7875. BEGIN END VisitFloatType;
  7876. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7877. BEGIN END VisitQualifiedType;
  7878. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7879. BEGIN END VisitStringType;
  7880. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7881. BEGIN END VisitEnumerationType;
  7882. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7883. BEGIN END VisitRangeType;
  7884. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7885. BEGIN
  7886. IF ~(SyntaxTree.Warned IN x.state) THEN
  7887. x.SetState(SyntaxTree.Warned);
  7888. Type(x.arrayBase);
  7889. END;
  7890. END VisitArrayType;
  7891. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7892. BEGIN
  7893. IF ~(SyntaxTree.Warned IN x.state) THEN
  7894. x.SetState(SyntaxTree.Warned);
  7895. Type(x.arrayBase);
  7896. END;
  7897. END VisitMathArrayType;
  7898. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7899. BEGIN
  7900. IF ~(SyntaxTree.Warned IN x.state) THEN
  7901. x.SetState(SyntaxTree.Warned);
  7902. Type(x.pointerBase);
  7903. END;
  7904. END VisitPointerType;
  7905. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7906. BEGIN Scope(x.recordScope) END VisitRecordType;
  7907. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7908. BEGIN Scope(x.cellScope) END VisitCellType;
  7909. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7910. BEGIN END VisitProcedureType;
  7911. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7912. VAR msg: ARRAY 256 OF CHAR;
  7913. BEGIN
  7914. Global.GetSymbolName(x,msg);
  7915. Strings.Append(msg," ");
  7916. Strings.Append(msg,text);
  7917. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7918. END Warning;
  7919. (** symbols *)
  7920. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7921. BEGIN
  7922. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7923. IF ~(x IS SyntaxTree.Parameter) THEN
  7924. Warning(x,"never used");
  7925. END;
  7926. END;
  7927. x.Accept(SELF);
  7928. END Symbol;
  7929. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7930. BEGIN END VisitSymbol;
  7931. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7932. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7933. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7934. BEGIN END VisitConstant;
  7935. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  7936. BEGIN END VisitVariable;
  7937. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  7938. BEGIN END VisitProperty;
  7939. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  7940. BEGIN END VisitParameter;
  7941. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  7942. BEGIN Scope(x.procedureScope) END VisitProcedure;
  7943. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  7944. BEGIN END VisitOperator;
  7945. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  7946. BEGIN END VisitImport;
  7947. PROCEDURE Scope(scope: SyntaxTree.Scope);
  7948. VAR
  7949. symbol: SyntaxTree.Symbol;
  7950. BEGIN
  7951. symbol := scope.firstSymbol;
  7952. WHILE(symbol # NIL) DO
  7953. Symbol(symbol);
  7954. symbol := symbol.nextSymbol;
  7955. END;
  7956. END Scope;
  7957. PROCEDURE Module*(x: SyntaxTree.Module);
  7958. BEGIN
  7959. SELF.module := x;
  7960. Scope(x.moduleScope);
  7961. END Module;
  7962. END Warnings;
  7963. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  7964. BEGIN
  7965. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  7966. END IsOberonInline;
  7967. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  7968. BEGIN
  7969. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  7970. END Resolved;
  7971. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  7972. VAR i: LONGINT;
  7973. BEGIN
  7974. i := 1;
  7975. WHILE i < x DO
  7976. i := i *2
  7977. END;
  7978. RETURN i=x
  7979. END PowerOf2;
  7980. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7981. BEGIN
  7982. RETURN
  7983. (scope # NIL) &
  7984. (scope IS SyntaxTree.ModuleScope)
  7985. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  7986. OR
  7987. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  7988. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7989. END IsCellNetScope;
  7990. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  7991. BEGIN
  7992. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  7993. END IsCellScope;
  7994. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  7995. BEGIN
  7996. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  7997. RETURN (scope # NIL) & IsCellNetScope(scope)
  7998. END InCellNetScope;
  7999. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8000. BEGIN
  8001. ASSERT(size MOD system.dataUnit = 0);
  8002. RETURN size DIV system.dataUnit
  8003. END ToMemoryUnits;
  8004. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8005. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8006. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8007. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8008. BEGIN
  8009. IF t = NIL THEN
  8010. RETURN TRUE
  8011. ELSE
  8012. t := t.resolved;
  8013. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8014. END;
  8015. END TypeAllowed;
  8016. BEGIN
  8017. type := type.resolved;
  8018. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8019. RETURN FALSE
  8020. ELSE
  8021. procedureType := type(SyntaxTree.ProcedureType);
  8022. numberParameters := procedureType.numberParameters;
  8023. RETURN
  8024. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8025. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8026. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8027. END;
  8028. END GetProcedureAllowed;
  8029. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8030. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8031. VAR import: SyntaxTree.Import;
  8032. BEGIN
  8033. import := importCache.ImportByModuleName(x.name,x.context);
  8034. IF import # NIL THEN
  8035. importCache.RemoveImporters(x.name,x.context);
  8036. END;
  8037. END RemoveModuleFromCache;
  8038. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8039. (* to <- this assignment compatibility *)
  8040. VAR result: BOOLEAN;
  8041. BEGIN
  8042. IF this= NIL THEN result := (to=NIL)
  8043. ELSIF to=NIL THEN result := FALSE
  8044. ELSE
  8045. (*! will be replaced by this:
  8046. ELSE result := this.CompatibleTo(to.resolved);
  8047. *)
  8048. this := this.resolved; to := to.resolved;
  8049. IF to=SyntaxTree.invalidType THEN result := FALSE
  8050. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8051. ELSIF to = this THEN
  8052. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8053. ELSIF to IS SyntaxTree.BasicType THEN
  8054. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8055. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8056. result := this.CompatibleTo(to.resolved)
  8057. ELSE
  8058. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8059. END
  8060. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8061. result := to.sizeInBits = this.sizeInBits;
  8062. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8063. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8064. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8065. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8066. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8067. result := TRUE;
  8068. ELSIF to IS SyntaxTree.AnyType THEN
  8069. 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);
  8070. ELSIF to IS SyntaxTree.ObjectType THEN
  8071. 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 *) ;
  8072. ELSIF to IS SyntaxTree.ByteType THEN
  8073. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8074. ELSIF to IS SyntaxTree.CharacterType THEN
  8075. result := IsCharacterType(this)
  8076. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8077. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8078. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this)) THEN
  8079. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8080. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8081. result := TRUE;
  8082. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8083. result := TRUE;
  8084. ELSE
  8085. result := FALSE
  8086. END;
  8087. ELSIF to IS SyntaxTree.PointerType THEN
  8088. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8089. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8090. & (~to.isRealtime OR this.isRealtime);
  8091. ELSIF to IS SyntaxTree.ProcedureType THEN
  8092. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8093. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8094. & (~to.isRealtime OR this.isRealtime)
  8095. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8096. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8097. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8098. ELSIF to IS SyntaxTree.RecordType THEN
  8099. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8100. ELSIF to IS SyntaxTree.ArrayType THEN
  8101. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8102. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8103. ELSIF StaticArrayCompatible(to, this) THEN
  8104. result := TRUE
  8105. ELSE
  8106. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8107. END;
  8108. ELSIF to IS SyntaxTree.MathArrayType THEN
  8109. IF this IS SyntaxTree.MathArrayType THEN
  8110. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8111. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8112. result := TRUE;
  8113. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8114. result := TRUE;
  8115. ELSE
  8116. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8117. END;
  8118. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8119. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8120. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8121. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8122. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8123. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8124. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8125. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8126. ELSE
  8127. result := FALSE
  8128. END;
  8129. (* an array-structured object type is compatible to the type of its array structure *)
  8130. ELSIF IsArrayStructuredObjectType(this) THEN
  8131. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8132. ELSE
  8133. result := FALSE;
  8134. END;
  8135. ELSIF to IS SyntaxTree.StringType THEN
  8136. result := FALSE;
  8137. ELSIF to IS SyntaxTree.EnumerationType THEN
  8138. result := IsEnumerationExtension(this,to);
  8139. ELSIF to IS SyntaxTree.PortType THEN
  8140. result := SameType(to, this)
  8141. ELSE
  8142. Printout.Info("CompatibleTo",to);
  8143. HALT(100); (* implement missing type check *)
  8144. END;
  8145. END;
  8146. RETURN result
  8147. END CompatibleTo;
  8148. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8149. VAR actualBase, formalBase: SyntaxTree.Type;
  8150. BEGIN
  8151. IF SameType(formal,actual) THEN
  8152. RETURN TRUE
  8153. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8154. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8155. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8156. RETURN
  8157. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8158. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8159. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8160. & StaticArrayCompatible(formalBase,actualBase)
  8161. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8162. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8163. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8164. RETURN
  8165. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8166. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8167. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8168. & StaticArrayCompatible(formalBase,actualBase)
  8169. ELSE RETURN FALSE
  8170. END;
  8171. END StaticArrayCompatible;
  8172. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8173. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8174. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8175. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8176. BEGIN
  8177. result := SameType(formal,actual);
  8178. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8179. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8180. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8181. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8182. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8183. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8184. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8185. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8186. & TC(formalBase, actualBase);
  8187. END;
  8188. RETURN result
  8189. END TC;
  8190. BEGIN
  8191. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8192. ELSE
  8193. arrayBase := formalType.arrayBase.resolved;
  8194. IF (actualType IS SyntaxTree.StringType) THEN
  8195. result := arrayBase IS SyntaxTree.CharacterType
  8196. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8197. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8198. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8199. result := TC(formalType, actualType);
  8200. ELSE
  8201. result := (arrayBase IS SyntaxTree.ByteType)
  8202. END;
  8203. END;
  8204. RETURN result
  8205. END OpenArrayCompatible;
  8206. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8207. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8208. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8209. BEGIN
  8210. IF actualType IS SyntaxTree.MathArrayType THEN
  8211. actualArray := actualType(SyntaxTree.MathArrayType);
  8212. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8213. (*
  8214. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8215. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8216. *)
  8217. actualBase := ArrayBase(actualType,Infinity);
  8218. formalBase := ArrayBase(formalType,Infinity);
  8219. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8220. ELSE
  8221. (*
  8222. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8223. *)
  8224. formalBase := Resolved(formalType.arrayBase);
  8225. actualBase := Resolved(actualArray.arrayBase);
  8226. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8227. (*
  8228. ARRAY [k] -> ARRAY [n]
  8229. *)
  8230. result := (formalType.staticLength = actualArray.staticLength)
  8231. ELSE
  8232. result := TRUE
  8233. END;
  8234. IF ~result THEN
  8235. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8236. ELSIF actualBase = NIL THEN result := FALSE
  8237. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8238. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8239. ELSE
  8240. result := SameType(formalBase,actualBase)
  8241. END;
  8242. END;
  8243. ELSE
  8244. result := FALSE
  8245. END;
  8246. RETURN result
  8247. END MathArrayCompatible;
  8248. (**
  8249. Math Array Type distance for assignments / parameter passings of the form
  8250. from -> to
  8251. variants:
  8252. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8253. allowed:
  8254. static -> static (& size match)
  8255. static -> open
  8256. static -> tensor
  8257. open -> open
  8258. open -> tensor
  8259. open -> static
  8260. tensor -> tensor
  8261. tensor -> open
  8262. tensor -> static
  8263. **)
  8264. (*! think about the metric here: is form matching more important than element type matching? *)
  8265. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8266. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8267. BEGIN
  8268. fromBase := Resolved(from.arrayBase);
  8269. toBase := Resolved(to.arrayBase);
  8270. i := Infinity;
  8271. IF from = to THEN
  8272. i := 0;
  8273. ELSIF (from.form = to.form) THEN
  8274. (* static -> static, open -> open, tensor -> tensor *)
  8275. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8276. IF fromBase = toBase THEN i := 0
  8277. ELSIF toBase = NIL THEN i := 1
  8278. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8279. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8280. ELSE
  8281. i := TypeDistance(system,fromBase, toBase, varpar);
  8282. END;
  8283. END;
  8284. ELSIF (to.form = SyntaxTree.Static) THEN
  8285. (* forbidden *)
  8286. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8287. (* static -> tensor, open -> tensor, tensor -> open *)
  8288. IF toBase=fromBase THEN i := 0;
  8289. ELSIF toBase = NIL THEN i := 1;
  8290. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8291. toBase := ArrayBase(toBase,Infinity);
  8292. IF (fromBase=toBase) THEN i := 0
  8293. ELSIF (toBase = NIL) THEN i:= 1
  8294. ELSIF (fromBase = NIL) THEN i := Infinity;
  8295. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8296. END;
  8297. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8298. fromBase := ArrayBase(fromBase,Infinity);
  8299. IF (fromBase=toBase) THEN i := 0
  8300. ELSIF (toBase = NIL) THEN i := 1
  8301. ELSIF (fromBase = NIL) THEN i := Infinity;
  8302. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8303. END;
  8304. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8305. END;
  8306. IF i # Infinity THEN INC(i,2) END;
  8307. ELSIF (from.form = SyntaxTree.Static) THEN
  8308. (* static -> open *)
  8309. IF toBase=fromBase THEN i := 0
  8310. ELSIF toBase = NIL THEN i := 1
  8311. ELSIF fromBase = NIL THEN i := Infinity
  8312. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8313. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8314. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8315. END;
  8316. IF i # Infinity THEN INC(i,1) END;
  8317. ELSE HALT(100); (* unknown case *)
  8318. END;
  8319. RETURN i;
  8320. END MathArrayTypeDistance;
  8321. (** compute and return the distance of two array types
  8322. - return the distance of the base types
  8323. **)
  8324. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8325. VAR i: LONGINT;
  8326. BEGIN
  8327. i := Infinity;
  8328. IF from = to THEN
  8329. i := 0
  8330. ELSE
  8331. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8332. (*
  8333. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8334. i := TypeDistance(from.base, to.base);
  8335. IF i >= 0 THEN INC(i) END
  8336. ELSIF (from.mode = open) & (to.mode = open) THEN
  8337. i := TypeDistance(from.base, to.base);
  8338. *)
  8339. END;
  8340. RETURN i
  8341. END ArrayTypeDistance;
  8342. (** compute the signature distance of a procedure and an actual parameter list
  8343. - if any of the parameters are not compatible, the result is infinite
  8344. - add up and return the distance over all parameters
  8345. **)
  8346. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8347. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8348. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8349. BEGIN
  8350. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8351. result := Infinity
  8352. ELSE
  8353. formalParameter := procedureType.firstParameter;
  8354. i := 0;
  8355. result := 0;
  8356. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8357. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8358. actualParameter := actualParameters.GetExpression(i);
  8359. ASSERT(formalParameter.type # NIL);
  8360. IF (actualParameter.type = NIL) THEN distance := Infinity
  8361. ELSE
  8362. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8363. END;
  8364. IF distance = Infinity THEN
  8365. result := Infinity;
  8366. ELSE
  8367. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8368. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8369. (* already handled varpar *)
  8370. (*
  8371. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8372. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8373. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8374. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8375. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8376. END;
  8377. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8378. result := Infinity
  8379. END;
  8380. *)
  8381. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8382. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8383. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8384. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8385. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8386. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8387. END;
  8388. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8389. result := Infinity
  8390. END;
  8391. ELSE
  8392. result := Infinity
  8393. END;
  8394. ELSE
  8395. INC(result,distance);
  8396. END;
  8397. END;
  8398. formalParameter := formalParameter.nextParameter; INC(i);
  8399. END;
  8400. END;
  8401. ASSERT(result >= 0);
  8402. RETURN result
  8403. END Distance;
  8404. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8405. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8406. BEGIN
  8407. IF right.numberParameters # (procedureType.numberParameters) THEN
  8408. result := Infinity
  8409. ELSE
  8410. formalParameter := procedureType.firstParameter;
  8411. rightParameter := right.firstParameter;
  8412. i := 0;
  8413. result := 0;
  8414. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8415. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8416. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8417. IF distance = Infinity THEN
  8418. result := Infinity;
  8419. ELSE
  8420. INC(result,distance);
  8421. END;
  8422. formalParameter := formalParameter.nextParameter;
  8423. rightParameter := rightParameter.nextParameter;
  8424. END;
  8425. END;
  8426. ASSERT(result >= 0);
  8427. RETURN result
  8428. END ProcedureTypeDistance;
  8429. (** compute and return the distance between two types, used for computation of signature distance
  8430. from -> to
  8431. **)
  8432. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8433. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8434. BEGIN
  8435. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8436. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8437. END;
  8438. i := Infinity;
  8439. IF from = to THEN
  8440. i := 0
  8441. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8442. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8443. i := Infinity;
  8444. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8445. i := 1
  8446. ELSIF (from IS SyntaxTree.StringType) THEN
  8447. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8448. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8449. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8450. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8451. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8452. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8453. i := 1
  8454. ELSIF (from IS SyntaxTree.NilType) THEN
  8455. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8456. (*
  8457. ELSIF (from = NoType) THEN
  8458. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8459. *)
  8460. ELSIF (from IS SyntaxTree.BasicType) THEN
  8461. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8462. IF varpar & (i # 0) THEN i := Infinity END;
  8463. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8464. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8465. ELSIF (from IS SyntaxTree.RecordType) THEN
  8466. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8467. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8468. IF to IS SyntaxTree.MathArrayType THEN
  8469. (*
  8470. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8471. i := Infinity;
  8472. ELSE
  8473. *)
  8474. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8475. (*
  8476. END;
  8477. *)
  8478. END
  8479. ELSIF (from IS SyntaxTree.PointerType) THEN
  8480. ptr := from(SyntaxTree.PointerType);
  8481. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8482. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8483. (* ELSE i := TypeDistance(ptr.base, to); *)
  8484. END
  8485. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8486. IF (to IS SyntaxTree.ProcedureType) THEN
  8487. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8488. END;
  8489. ELSIF (from IS SyntaxTree.PortType) THEN
  8490. IF (to IS SyntaxTree.PortType) THEN
  8491. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8492. i := 0;
  8493. END;
  8494. END;
  8495. (*no procedure test, procedure must be the same*)
  8496. END;
  8497. RETURN i
  8498. END TypeDistance;
  8499. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8500. BEGIN
  8501. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8502. END IsIntegerType;
  8503. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8504. BEGIN
  8505. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8506. END IsAddressType;
  8507. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8508. BEGIN
  8509. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8510. END IsSizeType;
  8511. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8512. BEGIN
  8513. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8514. END IsSignedIntegerType;
  8515. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8516. BEGIN
  8517. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8518. END IsUnsignedIntegerType;
  8519. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8520. VAR result: BOOLEAN;
  8521. BEGIN
  8522. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8523. value := x.resolved(SyntaxTree.IntegerValue).value;
  8524. result := TRUE
  8525. ELSE
  8526. result := FALSE
  8527. END;
  8528. RETURN result
  8529. END IsIntegerValue;
  8530. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8531. VAR result: BOOLEAN;
  8532. BEGIN
  8533. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8534. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8535. result := TRUE
  8536. ELSE
  8537. result := FALSE
  8538. END;
  8539. RETURN result
  8540. END IsEnumerationValue;
  8541. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8542. VAR result: BOOLEAN;
  8543. BEGIN
  8544. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8545. value := x.resolved(SyntaxTree.RealValue).value;
  8546. result := TRUE
  8547. ELSE
  8548. result := FALSE
  8549. END;
  8550. RETURN result
  8551. END IsRealValue;
  8552. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8553. VAR result: BOOLEAN;
  8554. BEGIN
  8555. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8556. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8557. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8558. result := TRUE
  8559. ELSE
  8560. result := FALSE
  8561. END;
  8562. RETURN result
  8563. END IsComplexValue;
  8564. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8565. VAR result: BOOLEAN;
  8566. BEGIN
  8567. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8568. value := x.resolved(SyntaxTree.CharacterValue).value;
  8569. result := TRUE
  8570. ELSE
  8571. result := FALSE
  8572. END;
  8573. RETURN result
  8574. END IsCharacterValue;
  8575. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8576. VAR result: BOOLEAN;
  8577. BEGIN
  8578. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8579. value := x.resolved(SyntaxTree.BooleanValue).value;
  8580. result := TRUE
  8581. ELSE
  8582. result := FALSE
  8583. END;
  8584. RETURN result
  8585. END IsBooleanValue;
  8586. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8587. VAR result: BOOLEAN;
  8588. BEGIN
  8589. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8590. value := x.resolved(SyntaxTree.SetValue).value;
  8591. result := TRUE
  8592. ELSE
  8593. result := FALSE
  8594. END;
  8595. RETURN result
  8596. END IsSetValue;
  8597. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8598. VAR result: BOOLEAN;
  8599. BEGIN
  8600. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8601. value := x.resolved(SyntaxTree.StringValue).value;
  8602. result := TRUE
  8603. ELSE
  8604. result := FALSE
  8605. END;
  8606. RETURN result
  8607. END IsStringValue;
  8608. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8609. BEGIN
  8610. x := x.resolved;
  8611. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8612. END Indexable;
  8613. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8614. BEGIN
  8615. RETURN t1.SameType(t2.resolved);
  8616. END SameType;
  8617. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8618. BEGIN
  8619. IF t IS SyntaxTree.MathArrayType THEN
  8620. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8621. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8622. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8623. DEC(max);
  8624. END;
  8625. ELSIF t IS SyntaxTree.ArrayType THEN
  8626. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8627. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8628. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8629. END;
  8630. END;
  8631. RETURN t;
  8632. END ArrayBase;
  8633. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8634. BEGIN
  8635. type := type.resolved;
  8636. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8637. base := type(SyntaxTree.ArrayType).arrayBase;
  8638. RETURN TRUE;
  8639. END;
  8640. RETURN FALSE;
  8641. END IsOpenArray;
  8642. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8643. BEGIN
  8644. type := type.resolved;
  8645. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8646. base := type(SyntaxTree.ArrayType).arrayBase;
  8647. dim := type(SyntaxTree.ArrayType).staticLength;
  8648. RETURN TRUE
  8649. ELSE
  8650. RETURN FALSE
  8651. END;
  8652. END IsStaticArray;
  8653. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8654. BEGIN
  8655. type := type.resolved;
  8656. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8657. base := type(SyntaxTree.ArrayType).arrayBase;
  8658. RETURN TRUE
  8659. ELSE
  8660. RETURN FALSE
  8661. END;
  8662. END IsDynamicArray;
  8663. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8664. VAR i: LONGINT;
  8665. BEGIN
  8666. i := 0;
  8667. t := t.resolved;
  8668. IF t IS SyntaxTree.MathArrayType THEN
  8669. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8670. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8671. END;
  8672. ELSIF t IS SyntaxTree.ArrayType THEN
  8673. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8674. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8675. END;
  8676. END;
  8677. RETURN i
  8678. END Dimension;
  8679. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8680. BEGIN
  8681. RETURN expression.assignable;
  8682. END IsVariable;
  8683. PROCEDURE IsVariableParameter (symbol: SyntaxTree.Symbol): BOOLEAN;
  8684. BEGIN
  8685. IF (symbol IS SyntaxTree.Parameter) THEN
  8686. WITH symbol: SyntaxTree.Parameter DO
  8687. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8688. END;
  8689. ELSE
  8690. RETURN FALSE
  8691. END;
  8692. END IsVariableParameter;
  8693. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8694. VAR result: BOOLEAN;
  8695. BEGIN
  8696. IF type = NIL THEN result := FALSE
  8697. ELSE
  8698. type := type.resolved;
  8699. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8700. END;
  8701. RETURN result
  8702. END IsPointerType;
  8703. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8704. VAR result: BOOLEAN;
  8705. BEGIN
  8706. IF type = NIL THEN result := FALSE
  8707. ELSE
  8708. type := type.resolved;
  8709. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8710. END;
  8711. RETURN result
  8712. END IsUnsafePointer;
  8713. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8714. BEGIN
  8715. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8716. END IsDisposable;
  8717. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8718. VAR result: BOOLEAN;
  8719. BEGIN
  8720. IF type = NIL THEN result := FALSE
  8721. ELSE
  8722. type := type.resolved;
  8723. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8724. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8725. result := result OR (type IS SyntaxTree.ObjectType);
  8726. END;
  8727. RETURN result
  8728. END IsPointerToRecord;
  8729. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8730. VAR result: BOOLEAN;
  8731. BEGIN
  8732. IF type = NIL THEN result := FALSE
  8733. ELSE
  8734. type := type.resolved;
  8735. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8736. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8737. ;
  8738. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8739. result := result OR (type IS SyntaxTree.ObjectType);
  8740. END;
  8741. RETURN result
  8742. END IsPointerToObject;
  8743. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8744. BEGIN
  8745. IF type # NIL THEN
  8746. RETURN type.resolved.hasPointers
  8747. ELSE
  8748. RETURN FALSE
  8749. END;
  8750. END ContainsPointer;
  8751. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8752. BEGIN
  8753. IF type = NIL THEN RETURN FALSE END;
  8754. type := type.resolved;
  8755. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8756. END IsStringType;
  8757. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8758. BEGIN
  8759. IF type = NIL THEN RETURN FALSE END;
  8760. type := type.resolved;
  8761. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8762. END IsCharacterType;
  8763. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8764. BEGIN
  8765. IF type = NIL THEN RETURN FALSE END;
  8766. type := type.resolved;
  8767. RETURN (type IS SyntaxTree.EnumerationType)
  8768. END IsEnumerationType;
  8769. (** cf. section "Type extension (base type)" in the language report **)
  8770. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8771. VAR result: BOOLEAN;
  8772. BEGIN
  8773. ASSERT(base # NIL); ASSERT(extension # NIL);
  8774. base := base.resolved; extension := extension.resolved;
  8775. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8776. result := TRUE;
  8777. ELSE
  8778. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8779. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8780. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8781. END;
  8782. WHILE (extension # NIL) & (extension # base) DO
  8783. IF extension IS SyntaxTree.RecordType THEN
  8784. extension := extension(SyntaxTree.RecordType).baseType;
  8785. IF (extension # NIL) THEN extension := extension.resolved END;
  8786. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8787. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8788. END;
  8789. ELSE extension := NIL;
  8790. END;
  8791. END;
  8792. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8793. END;
  8794. RETURN result
  8795. END IsTypeExtension;
  8796. (** check if base is the base enumeration type of extension **)
  8797. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8798. BEGIN
  8799. base := base.resolved; extension := extension.resolved;
  8800. WHILE (extension # NIL) & (extension # base) DO
  8801. IF extension IS SyntaxTree.EnumerationType THEN
  8802. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8803. IF extension # NIL THEN extension := extension.resolved END;
  8804. ELSE
  8805. extension := NIL
  8806. END;
  8807. END;
  8808. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8809. END IsEnumerationExtension;
  8810. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8811. BEGIN
  8812. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8813. RETURN TRUE
  8814. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8815. RETURN TRUE
  8816. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8817. RETURN TRUE
  8818. ELSE
  8819. RETURN FALSE
  8820. END
  8821. END IsCallable;
  8822. (** compute and return the distance of two record types
  8823. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8824. **)
  8825. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8826. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8827. BEGIN
  8828. i := 0;
  8829. WHILE (from # NIL) & (from # to) DO
  8830. baseType := from.baseType;
  8831. IF (baseType # NIL) THEN
  8832. baseType := baseType.resolved;
  8833. IF baseType IS SyntaxTree.PointerType THEN
  8834. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8835. END;
  8836. IF baseType IS SyntaxTree.RecordType THEN
  8837. from := baseType(SyntaxTree.RecordType);
  8838. ELSE
  8839. from := NIL;
  8840. END;
  8841. ELSE
  8842. from := NIL
  8843. END;
  8844. INC(i)
  8845. END;
  8846. IF from = NIL THEN i := Infinity END;
  8847. RETURN i
  8848. END RecordTypeDistance;
  8849. (** compute and return the distance of two pointer types **)
  8850. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8851. BEGIN
  8852. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8853. RETURN Infinity;
  8854. ELSE
  8855. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8856. END;
  8857. END PointerTypeDistance;
  8858. (** check if expression contains a symbol designator pointing to a type declaration.
  8859. - if so then enter type declaration into typeDeclaration and return true else return false
  8860. **)
  8861. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8862. VAR result: BOOLEAN;
  8863. BEGIN
  8864. result := FALSE;
  8865. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8866. result := TRUE;
  8867. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8868. END;
  8869. RETURN result
  8870. END IsTypeDesignator;
  8871. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8872. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8873. VAR result: BOOLEAN;
  8874. BEGIN
  8875. type := type.resolved;
  8876. IF type IS SyntaxTree.PointerType THEN
  8877. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8878. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8879. result := TRUE
  8880. ELSE
  8881. result := type IS SyntaxTree.RecordType
  8882. END;
  8883. RETURN result
  8884. END IsExtensibleType;
  8885. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8886. BEGIN
  8887. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8888. (d IS SyntaxTree.SymbolDesignator) &
  8889. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8890. OR
  8891. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8892. END IsUnextensibleRecord;
  8893. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8894. BEGIN
  8895. IF IsUnextensibleRecord(d) THEN
  8896. RETURN FALSE
  8897. ELSE RETURN IsExtensibleType(d.type.resolved)
  8898. END;
  8899. END IsExtensibleDesignator;
  8900. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8901. BEGIN
  8902. type := type.resolved;
  8903. IF (type IS SyntaxTree.PointerType) THEN
  8904. RETURN TRUE
  8905. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8906. RETURN TRUE
  8907. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8908. RETURN TRUE
  8909. ELSIF (type IS SyntaxTree.BasicType) THEN
  8910. RETURN TRUE
  8911. END;
  8912. RETURN FALSE
  8913. END IsBasicType;
  8914. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8915. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8916. BEGIN
  8917. baseType := record.baseType;
  8918. IF (baseType # NIL) THEN
  8919. baseType := baseType.resolved;
  8920. IF (baseType IS SyntaxTree.PointerType) THEN
  8921. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8922. END;
  8923. END;
  8924. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8925. recordType := baseType(SyntaxTree.RecordType);
  8926. ELSE
  8927. recordType := NIL;
  8928. END;
  8929. RETURN recordType
  8930. END RecordBase;
  8931. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8932. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8933. BEGIN
  8934. baseRecord := RecordBase(scope.ownerRecord);
  8935. IF baseRecord = NIL THEN RETURN NIL END;
  8936. scope := baseRecord.recordScope;
  8937. procedureType := procedure.type.resolved;
  8938. IF procedure IS SyntaxTree.Operator THEN
  8939. operator := scope.firstOperator;
  8940. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  8941. (*
  8942. Printout.Info("not same ",procedureType);
  8943. Printout.Info("with ",operator.type);
  8944. *)
  8945. operator := operator.nextOperator;
  8946. END;
  8947. super := operator;
  8948. ELSE
  8949. super := scope.firstProcedure;
  8950. WHILE (super # NIL) & (super.name # procedure.name) DO
  8951. super := super.nextProcedure;
  8952. END;
  8953. END;
  8954. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  8955. RETURN super
  8956. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  8957. RETURN super
  8958. ELSE
  8959. RETURN FindSuperProcedure(scope,procedure);
  8960. END;
  8961. END FindSuperProcedure;
  8962. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  8963. VAR procedure: SyntaxTree.Procedure;
  8964. BEGIN
  8965. procedure := record.recordScope.constructor;
  8966. IF procedure = NIL THEN
  8967. record := RecordBase(record);
  8968. IF record # NIL THEN
  8969. procedure := GetConstructor(record)
  8970. END;
  8971. END;
  8972. RETURN procedure;
  8973. END GetConstructor;
  8974. (* enter a case into a list of cases in a sorted way and check for collision *)
  8975. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  8976. VAR prev,this,new: SyntaxTree.CaseConstant;
  8977. BEGIN
  8978. this := root;
  8979. prev := NIL;
  8980. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  8981. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  8982. RETURN FALSE
  8983. ELSE
  8984. IF (this # NIL) & (this.min = max+1) THEN
  8985. this.min := min
  8986. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  8987. prev.max := min
  8988. ELSE
  8989. NEW(new); new.min := min; new.max := max;
  8990. new.next := this;
  8991. IF prev = NIL THEN
  8992. root := new;
  8993. ELSE
  8994. prev.next := new
  8995. END
  8996. END;
  8997. RETURN TRUE
  8998. END;
  8999. END EnterCase;
  9000. (** generate and return a new checker object, errors are entered into diagnostics **)
  9001. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope): Checker;
  9002. VAR checker: Checker;
  9003. BEGIN
  9004. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache);
  9005. RETURN checker
  9006. END NewChecker;
  9007. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9008. VAR warnings: Warnings;
  9009. BEGIN
  9010. NEW(warnings, diagnostics); RETURN warnings;
  9011. END NewWarnings;
  9012. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9013. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9014. END IsRangeType;
  9015. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9016. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9017. END IsMathArrayType;
  9018. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9019. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9020. END IsArrayType;
  9021. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9022. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9023. END IsComplexType;
  9024. (** if a type is an array-structured object type *)
  9025. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9026. VAR recordType: SyntaxTree.RecordType;
  9027. BEGIN
  9028. IF type = NIL THEN
  9029. RETURN FALSE
  9030. ELSE
  9031. type := type.resolved;
  9032. IF type IS SyntaxTree.PointerType THEN
  9033. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9034. IF type IS SyntaxTree.RecordType THEN
  9035. recordType := type(SyntaxTree.RecordType);
  9036. RETURN recordType.isObject & recordType.HasArrayStructure()
  9037. ELSE
  9038. RETURN FALSE
  9039. END
  9040. ELSE
  9041. RETURN FALSE
  9042. END
  9043. END
  9044. END IsArrayStructuredObjectType;
  9045. (** the math array structure of a type
  9046. - for math arrays: the array itself
  9047. - for pointers: the math array structure of the pointer base
  9048. - for array-structured object types: the underlying structure
  9049. - for non-math arrays and all other types: NIL
  9050. **)
  9051. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9052. VAR
  9053. result: SyntaxTree.MathArrayType;
  9054. BEGIN
  9055. IF type = NIL THEN
  9056. result := NIL
  9057. ELSE
  9058. type := type.resolved;
  9059. IF type IS SyntaxTree.PointerType THEN
  9060. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9061. END;
  9062. IF type IS SyntaxTree.MathArrayType THEN
  9063. result := type(SyntaxTree.MathArrayType)
  9064. ELSIF type IS SyntaxTree.RecordType THEN
  9065. result := type(SyntaxTree.RecordType).arrayStructure
  9066. ELSE
  9067. result := NIL
  9068. END
  9069. END;
  9070. RETURN result
  9071. END MathArrayStructureOfType;
  9072. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9073. VAR
  9074. result: BOOLEAN;
  9075. rangeExpression: SyntaxTree.RangeExpression;
  9076. BEGIN
  9077. IF x IS SyntaxTree.RangeExpression THEN
  9078. rangeExpression := x(SyntaxTree.RangeExpression);
  9079. result := TRUE;
  9080. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9081. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9082. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9083. ELSE
  9084. result := FALSE
  9085. END;
  9086. RETURN result
  9087. END IsStaticRange;
  9088. (** whether a type is a math array of tensor form **)
  9089. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9090. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9091. END IsTensor;
  9092. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9093. BEGIN
  9094. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9095. length := type(SyntaxTree.MathArrayType).staticLength;
  9096. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9097. RETURN TRUE
  9098. ELSE
  9099. RETURN FALSE
  9100. END;
  9101. END IsStaticMathArray;
  9102. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9103. BEGIN
  9104. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9105. END SymbolHasAddress;
  9106. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9107. BEGIN
  9108. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9109. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9110. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9111. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9112. END HasAddress;
  9113. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9114. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9115. BEGIN
  9116. IF (e IS SyntaxTree.Designator) THEN
  9117. d := e(SyntaxTree.Designator);
  9118. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9119. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9120. e := d.left;
  9121. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9122. END;
  9123. IF d # NIL THEN
  9124. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9125. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9126. END;
  9127. END;
  9128. RETURN FALSE;
  9129. END IsLocalVariable;
  9130. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9131. BEGIN
  9132. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9133. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9134. ELSE
  9135. RETURN TRUE
  9136. END;
  9137. END IsStaticProcedure;
  9138. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9139. CONST OptimizeMethodTable = FALSE;
  9140. BEGIN
  9141. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9142. END InMethodTable;
  9143. END FoxSemanticChecker.