FoxSemanticChecker.Mod 400 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967
  1. MODULE FoxSemanticChecker; (* AUTHOR "fof & fn"; PURPOSE "Oberon Compiler: Semantic Checker"; *)
  2. (* (c) fof ETHZ 2009 *)
  3. IMPORT D := Debugging, Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree,
  4. Diagnostics, Global := FoxGlobal, Printout:= FoxPrintout, Formats := FoxFormats, SYSTEM, Strings;
  5. CONST
  6. Trace = FALSE;
  7. Infinity = MAX(LONGINT); (* for type distance *)
  8. InvalidPosition* = Diagnostics.Invalid;
  9. MaxTensorIndexOperatorSize = 4;
  10. UndefinedPhase = 0; DeclarationPhase=1; InlinePhase=2; ImplementationPhase=3;
  11. TYPE
  12. FileName=ARRAY 256 OF CHAR;
  13. LateFix= POINTER TO RECORD (* contains a late fix to be resolved in a later step: type fixes and implementations *)
  14. p: ANY; scope: SyntaxTree.Scope;
  15. next: LateFix;
  16. END;
  17. LateFixList = OBJECT (* fifo queue for items to be resolved later on - deferred fixes *)
  18. VAR first,last: LateFix;
  19. PROCEDURE & Init;
  20. BEGIN first := NIL; last := NIL;
  21. END Init;
  22. (* get and remove element from list *)
  23. PROCEDURE Get(VAR scope: SyntaxTree.Scope): ANY;
  24. VAR p: ANY;
  25. BEGIN
  26. IF first # NIL THEN p := first.p; scope := first.scope; first := first.next ELSE p := NIL; END;
  27. IF first = NIL THEN last := NIL END;
  28. RETURN p;
  29. END Get;
  30. (* add unresolved type to list *)
  31. PROCEDURE Add(p: ANY; scope: SyntaxTree.Scope);
  32. VAR next: LateFix;
  33. BEGIN
  34. ASSERT(scope # NIL);
  35. NEW(next); next.p := p; next.scope := scope;
  36. next.next := NIL;
  37. IF first = NIL THEN first := next; last := next;
  38. ELSE last.next := next; last := next
  39. END;
  40. END Add;
  41. END LateFixList;
  42. WithEntry = POINTER TO RECORD
  43. previous: WithEntry;
  44. symbol: SyntaxTree.Symbol;
  45. type: SyntaxTree.Type;
  46. END;
  47. Replacement*= POINTER TO RECORD
  48. name*: Basic.SegmentedName;
  49. expression*: SyntaxTree.Expression;
  50. used*: BOOLEAN;
  51. next*: Replacement;
  52. END;
  53. (** checker object: used to check and resolve a module
  54. - resolves types
  55. - resolves expressions
  56. - resolves designators
  57. - resolves declarations
  58. - resolves statements
  59. - resolves implementations (bodies)
  60. **)
  61. Checker*= OBJECT (SyntaxTree.Visitor)
  62. VAR
  63. module: SyntaxTree.Module;
  64. diagnostics: Diagnostics.Diagnostics;
  65. useDarwinCCalls: BOOLEAN;
  66. cooperative: BOOLEAN;
  67. error-: BOOLEAN;
  68. VerboseErrorMessage: BOOLEAN;
  69. typeFixes, pointerFixes: LateFixList;
  70. importCache-: SyntaxTree.ModuleScope; (* contains global imports, did not take ImportList as it was much slower, for whatever reasons *)
  71. arrayBaseImported: BOOLEAN;
  72. phase: LONGINT;
  73. system-: Global.System;
  74. symbolFileFormat-: Formats.SymbolFileFormat;
  75. backendName-: ARRAY 32 OF CHAR;
  76. (* temporary variables for the visitors
  77. they replace variables on a stack during use of the visitor pattern and may only be
  78. - set in AcceptXXX procedures
  79. - set and read in ResolveXXX procedures
  80. *)
  81. resolvedType: SyntaxTree.Type; (** temporary used for type resolution **)
  82. resolvedExpression: SyntaxTree.Expression; (** temporary variable used for expression resolution **)
  83. resolvedStatement: SyntaxTree.Statement; (** used for statement resolution **)
  84. currentScope-: SyntaxTree.Scope;
  85. currentIsRealtime: BOOLEAN;
  86. currentIsUnreachable: BOOLEAN;
  87. currentIsCellNet: BOOLEAN;
  88. currentIsBodyProcedure: BOOLEAN;
  89. currentIsExclusive: BOOLEAN;
  90. global: SyntaxTree.ModuleScope;
  91. withEntries: WithEntry;
  92. activeCellsStatement: BOOLEAN;
  93. replacements*: Replacement;
  94. cellsAreObjects: BOOLEAN;
  95. variableAccessed: BOOLEAN;
  96. PROCEDURE &InitChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR);
  97. BEGIN
  98. SELF.diagnostics := diagnostics;
  99. SELF.useDarwinCCalls := useDarwinCCalls;
  100. SELF.cooperative := cooperative;
  101. SELF.system := system;
  102. SELF.symbolFileFormat := symbolFileFormat;
  103. error := FALSE;
  104. NEW(typeFixes);
  105. NEW(pointerFixes);
  106. resolvedType := NIL;
  107. resolvedExpression := NIL;
  108. resolvedStatement := NIL;
  109. currentScope := NIL;
  110. IF importCache = NIL THEN importCache := SyntaxTree.NewModuleScope() END;
  111. SELF.importCache := importCache;
  112. arrayBaseImported := FALSE;
  113. SELF.VerboseErrorMessage := verboseErrorMessage;
  114. global := NIL;
  115. phase := UndefinedPhase;
  116. currentIsRealtime := FALSE;
  117. currentIsUnreachable := FALSE;
  118. currentIsCellNet := FALSE;
  119. currentIsBodyProcedure := FALSE;
  120. currentIsExclusive := FALSE;
  121. withEntries := NIL;
  122. SELF.cellsAreObjects := system.cellsAreObjects;
  123. COPY(backend, backendName);
  124. END InitChecker;
  125. (** report error **)
  126. PROCEDURE Error(position: LONGINT; code: LONGINT; CONST message: ARRAY OF CHAR);
  127. VAR errorMessage: ARRAY 256 OF CHAR; errModule: SyntaxTree.Module;
  128. BEGIN
  129. IF diagnostics # NIL THEN
  130. Basic.GetErrorMessage(code,message,errorMessage);
  131. ASSERT(currentScope # NIL);
  132. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  133. diagnostics.Error(errModule.sourceName, position, code, errorMessage);
  134. END;
  135. error := TRUE;
  136. END Error;
  137. PROCEDURE Warning(position: LONGINT; CONST message: ARRAY OF CHAR);
  138. VAR errModule: SyntaxTree.Module;
  139. BEGIN
  140. IF diagnostics # NIL THEN
  141. IF module # NIL THEN errModule := module ELSE errModule := currentScope.ownerModule END;
  142. diagnostics.Warning(errModule.sourceName, position, Diagnostics.Invalid, message);
  143. END;
  144. END Warning;
  145. PROCEDURE ErrorSS(position: LONGINT; CONST msg,msg2: ARRAY OF CHAR);
  146. VAR errorMessage: ARRAY 256 OF CHAR;
  147. BEGIN
  148. IF diagnostics # NIL THEN
  149. Basic.Concat(errorMessage,msg," ", msg2);
  150. diagnostics.Error(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, errorMessage);
  151. END;
  152. error := TRUE;
  153. END ErrorSS;
  154. PROCEDURE InfoSS(position: LONGINT; CONST msg1: ARRAY OF CHAR; CONST s: Basic.String);
  155. VAR msg, msg2: ARRAY 256 OF CHAR;
  156. BEGIN
  157. IF diagnostics # NIL THEN
  158. COPY(msg1, msg);
  159. Strings.Append(msg, " = ");
  160. Basic.GetString(s, msg2);
  161. Strings.Append(msg, msg2);
  162. diagnostics.Information(currentScope.ownerModule.sourceName, position, Diagnostics.Invalid, msg);
  163. END;
  164. END InfoSS;
  165. (*** symbol lookup ***)
  166. (** find a symbol in the current scope, traverse to outer scope if traverse=true and no symbol found yet
  167. **)
  168. PROCEDURE Find(inScope: SyntaxTree.Scope; name: SyntaxTree.Identifier; traverse: BOOLEAN): SyntaxTree.Symbol;
  169. VAR
  170. scope,baseScope: SyntaxTree.Scope;
  171. symbol, s: SyntaxTree.Symbol;
  172. ownerRecord,base: SyntaxTree.RecordType;
  173. BEGIN
  174. scope := inScope;
  175. symbol := NIL;
  176. WHILE (scope # NIL) & (symbol = NIL) DO
  177. symbol := scope.FindSymbol(name);
  178. s := NIL;
  179. IF (symbol # NIL) & (symbol.access * SyntaxTree.Public = {}) & (symbol.scope IS SyntaxTree.CellScope) (* hidden copies of parameters *) THEN
  180. s := symbol.scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  181. ELSIF (symbol = NIL) & (scope IS SyntaxTree.CellScope) THEN
  182. symbol := scope(SyntaxTree.CellScope).ownerCell.FindParameter(name);
  183. END;
  184. IF (symbol # NIL) & (symbol IS SyntaxTree.Parameter) & (symbol.scope IS SyntaxTree.CellScope) THEN (* ok, symbol auto-export in scope *)
  185. ELSIF s # NIL THEN (* hidden variable shadows port parameter *)
  186. ELSE
  187. WHILE (symbol # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) & (symbol.access * SyntaxTree.Public = {}) DO
  188. (* found symbol in different module, but is it not exported, can we go on searching in record base scopes ? *)
  189. symbol.MarkUsed;
  190. IF (symbol.scope IS SyntaxTree.RecordScope) THEN
  191. ownerRecord := symbol.scope(SyntaxTree.RecordScope).ownerRecord;
  192. base := RecordBase(ownerRecord);
  193. IF (base # NIL) THEN
  194. baseScope := base.recordScope;
  195. symbol := Find(baseScope,name,FALSE);
  196. ELSE
  197. symbol := NIL;
  198. END;
  199. ELSE
  200. symbol := NIL;
  201. END;
  202. END;
  203. END;
  204. IF traverse THEN scope := scope.outerScope ELSE scope := NIL END;
  205. END;
  206. IF (symbol # NIL) THEN
  207. IF ~(SyntaxTree.Resolved IN symbol.state) THEN
  208. ASSERT(phase = DeclarationPhase);
  209. ResolveSymbol(symbol)
  210. END;
  211. symbol.MarkUsed;
  212. END;
  213. RETURN symbol
  214. END Find;
  215. (*** types ***)
  216. (** find type declaration with name qualifiedIdentifier and return resolved type
  217. - check qualified identifier prefix, set scope to module scope if appropriate
  218. - check suffix in scope
  219. **)
  220. PROCEDURE ResolveNamedType(qualifiedIdentifier: SyntaxTree.QualifiedIdentifier; VAR typeDeclaration: SyntaxTree.TypeDeclaration): SyntaxTree.Type;
  221. VAR prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol; result:SyntaxTree.Type;
  222. BEGIN
  223. result := NIL;
  224. prevScope := currentScope;
  225. IF (qualifiedIdentifier.prefix # SyntaxTree.invalidIdentifier) THEN
  226. symbol := Find(currentScope,qualifiedIdentifier.prefix,TRUE);
  227. IF (symbol # NIL) & (symbol IS SyntaxTree.Import) THEN
  228. IF symbol(SyntaxTree.Import).module = NIL THEN
  229. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"module not loaded");
  230. result := SyntaxTree.invalidType;
  231. symbol := NIL;
  232. ELSE
  233. currentScope := symbol(SyntaxTree.Import).module.moduleScope;
  234. symbol := Find(currentScope,qualifiedIdentifier.suffix,FALSE);
  235. IF (symbol = NIL) OR (symbol.access * SyntaxTree.Public = {}) THEN
  236. IF VerboseErrorMessage THEN
  237. Printout.Info("scope", currentScope);
  238. Printout.Info("symbol", symbol);
  239. END;
  240. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (prefix-suffix)")
  241. END;
  242. END;
  243. ELSE
  244. D.Str0(qualifiedIdentifier.prefix);D.Ln;
  245. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"prefix does not denote a module name");
  246. symbol := NIL;
  247. END;
  248. ELSE
  249. symbol := Find(currentScope,qualifiedIdentifier.suffix,TRUE);
  250. IF symbol = NIL THEN
  251. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"undeclared identifier (qualident suffix)");
  252. IF VerboseErrorMessage THEN
  253. Printout.Info("Qualident",qualifiedIdentifier);
  254. Printout.Info("in scope",currentScope) ;
  255. END;
  256. END;
  257. END;
  258. IF symbol = NIL THEN (* error already handled *)
  259. typeDeclaration := NIL;
  260. result := SyntaxTree.invalidType;
  261. ELSIF ~(symbol IS SyntaxTree.TypeDeclaration) THEN
  262. Error(qualifiedIdentifier.position,Diagnostics.Invalid,"symbol does not denote a type");
  263. typeDeclaration := NIL;
  264. result := SyntaxTree.invalidType;
  265. ELSE
  266. currentScope := symbol.scope;
  267. typeDeclaration := symbol(SyntaxTree.TypeDeclaration);
  268. result := ResolveType(typeDeclaration.declaredType);
  269. symbol.MarkUsed;
  270. ASSERT(result # NIL);
  271. END;
  272. currentScope := prevScope;
  273. RETURN result
  274. END ResolveNamedType;
  275. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  276. If node is currently being resolved then emit a cyclic definition error.
  277. Return TRUE only if node is fully resolved.
  278. **)
  279. PROCEDURE TypeNeedsResolution(x: SyntaxTree.Type): BOOLEAN;
  280. VAR result: BOOLEAN;
  281. BEGIN
  282. IF SyntaxTree.Resolved IN x.state THEN
  283. result := FALSE
  284. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  285. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  286. result := FALSE;
  287. ELSE
  288. result := TRUE;
  289. x.SetState(SyntaxTree.BeingResolved)
  290. END;
  291. RETURN result
  292. END TypeNeedsResolution;
  293. (** Return invalid type if x is currently being resolved, return x otherwise**)
  294. PROCEDURE ResolvedType(x: SyntaxTree.Type): SyntaxTree.Type;
  295. BEGIN
  296. IF SyntaxTree.Resolved IN x.state THEN
  297. RETURN x
  298. ELSE
  299. RETURN SyntaxTree.invalidType
  300. END;
  301. END ResolvedType;
  302. PROCEDURE VisitType(x: SyntaxTree.Type);
  303. BEGIN
  304. ASSERT(x = SyntaxTree.invalidType);
  305. END VisitType;
  306. (** resolve basic type **)
  307. PROCEDURE VisitBasicType(x: SyntaxTree.BasicType);
  308. BEGIN
  309. IF TypeNeedsResolution(x) THEN
  310. x.SetState(SyntaxTree.Resolved);
  311. END;
  312. resolvedType := ResolvedType(x)
  313. END VisitBasicType;
  314. PROCEDURE VisitByteType(x: SyntaxTree.ByteType);
  315. BEGIN
  316. VisitBasicType(x);
  317. END VisitByteType;
  318. (** resolve character type **)
  319. PROCEDURE VisitCharacterType(x: SyntaxTree.CharacterType);
  320. BEGIN
  321. VisitBasicType(x);
  322. END VisitCharacterType;
  323. PROCEDURE VisitBooleanType(x: SyntaxTree.BooleanType);
  324. BEGIN
  325. VisitBasicType(x);
  326. END VisitBooleanType;
  327. PROCEDURE VisitSetType(x: SyntaxTree.SetType);
  328. BEGIN
  329. VisitBasicType(x);
  330. END VisitSetType;
  331. PROCEDURE VisitAddressType(x: SyntaxTree.AddressType);
  332. BEGIN
  333. VisitBasicType(x);
  334. END VisitAddressType;
  335. PROCEDURE VisitSizeType(x: SyntaxTree.SizeType);
  336. BEGIN
  337. VisitBasicType(x);
  338. END VisitSizeType;
  339. PROCEDURE VisitAnyType(x: SyntaxTree.AnyType);
  340. BEGIN
  341. VisitBasicType(x);
  342. END VisitAnyType;
  343. PROCEDURE VisitObjectType(x: SyntaxTree.ObjectType);
  344. BEGIN
  345. VisitBasicType(x);
  346. END VisitObjectType;
  347. PROCEDURE VisitNilType(x: SyntaxTree.NilType);
  348. BEGIN
  349. VisitBasicType(x);
  350. END VisitNilType;
  351. (** resolve integer type **)
  352. PROCEDURE VisitIntegerType(x: SyntaxTree.IntegerType);
  353. BEGIN
  354. VisitBasicType(x);
  355. END VisitIntegerType;
  356. (** resolve real type **)
  357. PROCEDURE VisitFloatType(x: SyntaxTree.FloatType);
  358. BEGIN
  359. VisitBasicType(x);
  360. END VisitFloatType;
  361. (** resolve complex type **)
  362. PROCEDURE VisitComplexType(x: SyntaxTree.ComplexType);
  363. BEGIN
  364. VisitBasicType(x);
  365. END VisitComplexType;
  366. (**
  367. resolve string type: nothing to be done
  368. **)
  369. PROCEDURE VisitStringType(x: SyntaxTree.StringType);
  370. BEGIN
  371. IF TypeNeedsResolution(x) THEN
  372. x.SetState(SyntaxTree.Resolved);
  373. END;
  374. resolvedType := ResolvedType(x)
  375. END VisitStringType;
  376. (**
  377. check enumeration scope: enter symbols and check for duplicate names
  378. **)
  379. PROCEDURE CheckEnumerationScope(x: SyntaxTree.EnumerationScope; VAR highest: LONGINT);
  380. VAR e: SyntaxTree.Constant; value: SyntaxTree.Expression; nextHighest: LONGINT; prevScope: SyntaxTree.Scope;
  381. BEGIN
  382. prevScope := currentScope;
  383. currentScope := x;
  384. e := x.firstConstant;
  385. WHILE (e # NIL) DO
  386. Register(e,x,FALSE);
  387. IF SymbolNeedsResolution(e) THEN
  388. IF e.value # NIL THEN
  389. value := ConstantExpression(e.value);
  390. value := NewConversion(e.position,value,x.ownerEnumeration,NIL);
  391. ELSE
  392. value := SyntaxTree.NewEnumerationValue(e.position,highest+1);
  393. value.SetType(x.ownerEnumeration);
  394. END;
  395. IF (value.resolved # NIL) & (value.resolved IS SyntaxTree.EnumerationValue) THEN
  396. nextHighest := value.resolved(SyntaxTree.EnumerationValue).value;
  397. IF nextHighest > highest THEN highest := nextHighest END;
  398. END;
  399. e.SetValue(value);
  400. CheckSymbolVisibility(e);
  401. e.SetType(x.ownerEnumeration);
  402. e.SetState(SyntaxTree.Resolved);
  403. END;
  404. e := e.nextConstant;
  405. END;
  406. currentScope := prevScope;
  407. END CheckEnumerationScope;
  408. (**
  409. resolve enumeration type: check enumeration scope
  410. **)
  411. PROCEDURE VisitEnumerationType(x: SyntaxTree.EnumerationType);
  412. VAR position: LONGINT; baseScope: SyntaxTree.EnumerationScope; baseType,resolved: SyntaxTree.Type; enumerationBase: SyntaxTree.EnumerationType;
  413. lowest, highest: LONGINT;
  414. BEGIN
  415. IF TypeNeedsResolution(x) THEN
  416. IF x.enumerationBase # NIL THEN
  417. position := x.enumerationBase.position;
  418. baseType := ResolveType(x.enumerationBase);
  419. resolved := baseType.resolved;
  420. baseScope := NIL;
  421. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  422. ELSIF ~(resolved IS SyntaxTree.EnumerationType) THEN
  423. Error(position,Diagnostics.Invalid,"base type is no enumeration type");
  424. ELSE
  425. enumerationBase := resolved(SyntaxTree.EnumerationType);
  426. lowest := enumerationBase.rangeHighest+1;
  427. END;
  428. x.SetEnumerationBase(baseType);
  429. ELSE lowest := 0;
  430. END;
  431. highest := lowest-1;
  432. CheckEnumerationScope(x.enumerationScope, highest);
  433. x.SetRange(lowest, highest);
  434. x.SetState(SyntaxTree.Resolved);
  435. END;
  436. resolvedType := ResolvedType(x);
  437. END VisitEnumerationType;
  438. (**
  439. resolve range type: nothing to be done
  440. **)
  441. PROCEDURE VisitRangeType(x: SyntaxTree.RangeType);
  442. BEGIN
  443. IF TypeNeedsResolution(x) THEN
  444. x.SetState(SyntaxTree.Resolved);
  445. END;
  446. resolvedType := ResolvedType(x)
  447. END VisitRangeType;
  448. (**
  449. resolve qualified type
  450. - find and resolve named type and set resolved type
  451. **)
  452. PROCEDURE VisitQualifiedType(x: SyntaxTree.QualifiedType);
  453. VAR type: SyntaxTree.Type; typeDeclaration: SyntaxTree.TypeDeclaration;
  454. BEGIN
  455. IF TypeNeedsResolution(x) THEN
  456. type := ResolveNamedType(x.qualifiedIdentifier, typeDeclaration);
  457. x.SetResolved(type.resolved);
  458. x.SetState(SyntaxTree.Resolved);
  459. x.SetTypeDeclaration (typeDeclaration);
  460. ELSIF ~(SyntaxTree.Resolved IN x.state) THEN
  461. x.SetResolved(SyntaxTree.invalidType);
  462. END;
  463. resolvedType := x;
  464. END VisitQualifiedType;
  465. (**
  466. resolve array type
  467. - check base type
  468. - array of math array forbidden
  469. - static array of open array forbidden
  470. **)
  471. PROCEDURE VisitArrayType(x: SyntaxTree.ArrayType);
  472. VAR arrayBase: SyntaxTree.Type; e: SyntaxTree.Expression; pointerType: SyntaxTree.PointerType;
  473. BEGIN
  474. IF TypeNeedsResolution(x) THEN
  475. x.SetArrayBase(ResolveType(x.arrayBase));
  476. IF x.arrayBase.resolved.isRealtime THEN x.SetRealtime(TRUE) END;
  477. arrayBase := x.arrayBase.resolved;
  478. IF (arrayBase IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  479. pointerType := SyntaxTree.NewPointerType(x.position, x.scope);
  480. pointerType.SetPointerBase(arrayBase);
  481. pointerType.SetHidden(TRUE);
  482. IF x.arrayBase IS SyntaxTree.QualifiedType THEN
  483. x.arrayBase(SyntaxTree.QualifiedType).SetResolved(pointerType)
  484. ELSE
  485. x.SetArrayBase(pointerType);
  486. END;
  487. END;
  488. IF x.length # NIL THEN
  489. variableAccessed := FALSE;
  490. e := ResolveExpression(x.length);
  491. IF (e.resolved = NIL) THEN
  492. IF variableAccessed THEN
  493. Error(e.position, Diagnostics.Invalid, "forbidden variable access");
  494. END;
  495. x.SetLength(e); x.SetForm(SyntaxTree.SemiDynamic);
  496. ELSE
  497. x.SetLength(ConstantIntegerGeq0(e (*x.length*)));
  498. END;
  499. END;
  500. IF arrayBase IS SyntaxTree.ArrayType THEN
  501. IF (x.form = SyntaxTree.Static) & (arrayBase(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  502. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array");
  503. END;
  504. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  505. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  506. END;
  507. x.SetHasPointers(arrayBase.hasPointers);
  508. x.SetState(SyntaxTree.Resolved);
  509. END;
  510. resolvedType := ResolvedType(x);
  511. END VisitArrayType;
  512. PROCEDURE ImportModule(name: SyntaxTree.Identifier; position: LONGINT);
  513. VAR module: SyntaxTree.Module; import, duplicate: SyntaxTree.Import; moduleScope: SyntaxTree.ModuleScope;
  514. BEGIN
  515. module := currentScope.ownerModule;
  516. IF module.name=name THEN
  517. (* do nothing *)
  518. ELSE
  519. moduleScope := module.moduleScope;
  520. import := moduleScope.FindImport(name);
  521. IF import = NIL THEN
  522. import := SyntaxTree.NewImport(position,name,name,TRUE);
  523. moduleScope.AddImport(import);
  524. Register(import,moduleScope,FALSE);
  525. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(SELF.module.context) END;
  526. VisitImport(import);
  527. ELSIF import.direct=FALSE THEN
  528. import.SetScope(module.moduleScope);
  529. import.SetDirect(TRUE);
  530. IF moduleScope.FindSymbol(import.name) = NIL THEN
  531. duplicate := SyntaxTree.NewImport(InvalidPosition,import.name, import.name,FALSE);
  532. duplicate.SetContext(import.context);
  533. duplicate.SetModule(import.module);
  534. Register(duplicate,moduleScope,TRUE);
  535. VisitImport(duplicate);
  536. END;
  537. END;
  538. import.MarkUsed
  539. END;
  540. END ImportModule;
  541. (**
  542. resolve math array type
  543. - check base type
  544. - open math array of array forbidden
  545. - math array of tensor forbidden
  546. - static array of open array forbidden
  547. **)
  548. PROCEDURE VisitMathArrayType(x: SyntaxTree.MathArrayType);
  549. VAR arrayBase: SyntaxTree.Type;
  550. BEGIN
  551. IF TypeNeedsResolution(x) THEN
  552. x.SetArrayBase(ResolveType(x.arrayBase));
  553. IF x.length # NIL THEN
  554. x.SetLength(ConstantIntegerGeq0(x.length));
  555. END;
  556. arrayBase := x.arrayBase;
  557. IF arrayBase # NIL THEN
  558. arrayBase := arrayBase.resolved;
  559. IF arrayBase = SyntaxTree.invalidType THEN
  560. (* error already handled *)
  561. ELSIF arrayBase IS SyntaxTree.ArrayType THEN
  562. Error(x.position,Diagnostics.Invalid,"forbidden array mixed form");
  563. ELSIF arrayBase IS SyntaxTree.MathArrayType THEN
  564. IF (x.form = SyntaxTree.Tensor) OR (arrayBase(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  565. Error(x.position,Diagnostics.Invalid,"forbidden Tensor Array mix")
  566. ELSIF (x.form=SyntaxTree.Static) & (arrayBase(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  567. Error(x.position,Diagnostics.Invalid,"forbidden static array of dynamic array")
  568. END;
  569. END;
  570. IF x.form = SyntaxTree.Static THEN
  571. x.SetIncrement(system.SizeOf(arrayBase));
  572. END;
  573. x.SetHasPointers((x.form # SyntaxTree.Static) OR arrayBase.hasPointers);
  574. END;
  575. x.SetState(SyntaxTree.Resolved);
  576. END;
  577. resolvedType := ResolvedType(x);
  578. END VisitMathArrayType;
  579. (* anonymous type declarations are used for variables that use an anonymous type. They are not used for records that are
  580. pointed to by a pointer to record. The following diagram shows the possible cases for records and pointer to records.
  581. (1) Rec = RECORD ... END; Ptr <---> Rec
  582. Ptr = POINTER TO Rec; ^ |
  583. | |
  584. TypeDesc TypeDesc
  585. (2) Obj = POINTER TO RECORD .. END; Obj <---> Record
  586. ^ /
  587. | /
  588. TypeDesc <-- /
  589. *)
  590. PROCEDURE AnonymousTypeDeclaration(x: SyntaxTree.Type; CONST prefix: ARRAY OF CHAR);
  591. VAR typeDeclaration: SyntaxTree.TypeDeclaration; name,number: Scanner.IdentifierString;
  592. BEGIN
  593. Strings.IntToStr(x.position,number);
  594. COPY(prefix,name);
  595. Strings.Append(name,"@");
  596. Strings.Append(name,number);
  597. typeDeclaration := SyntaxTree.NewTypeDeclaration(x.position,SyntaxTree.NewIdentifier(name));
  598. typeDeclaration.SetDeclaredType(x);
  599. typeDeclaration.SetAccess(SyntaxTree.Hidden);
  600. x.SetTypeDeclaration(typeDeclaration);
  601. currentScope.AddTypeDeclaration(typeDeclaration);
  602. typeDeclaration.SetScope(currentScope);
  603. END AnonymousTypeDeclaration;
  604. (**
  605. deferred pointer type resolving
  606. - resolve base type
  607. - check that base type is a record or array type
  608. - if error then set base type to invalid type
  609. **)
  610. PROCEDURE FixPointerType(type: SyntaxTree.PointerType);
  611. VAR resolved, base: SyntaxTree.Type; position: LONGINT; recordType: SyntaxTree.RecordType;
  612. BEGIN
  613. ASSERT(type.pointerBase # NIL);
  614. position := type.pointerBase.position;
  615. IF (type.pointerBase IS SyntaxTree.RecordType) THEN (* direct POINTER TO RECORD *)
  616. type.pointerBase(SyntaxTree.RecordType).SetPointerType(type);
  617. (* not for pointers, a type is needed for the records only
  618. IF type.typeDeclaration = NIL THEN
  619. AnonymousTypeDeclaration(type);
  620. END;
  621. *)
  622. END;
  623. resolved := ResolveType(type.pointerBase);
  624. IF (resolved.resolved IS SyntaxTree.RecordType) OR (resolved.resolved IS SyntaxTree.ArrayType) OR (resolved.resolved IS SyntaxTree.CellType) THEN
  625. type.SetPointerBase(resolved);
  626. IF (resolved.resolved IS SyntaxTree.RecordType) THEN
  627. recordType := resolved.resolved(SyntaxTree.RecordType);
  628. IF recordType.isObject & (recordType.baseType # NIL) THEN
  629. IF type.isRealtime & ~recordType.baseType.resolved.isRealtime THEN
  630. Error(position,Diagnostics.Invalid,"base type of object must be a realtime object");
  631. ELSIF ~type.isRealtime & recordType.baseType.resolved.isRealtime THEN
  632. Error(position,Diagnostics.Invalid,"extensions of realtime objects must be explicitly declared as realtime objects");
  633. END;
  634. END;
  635. END;
  636. IF type.isRealtime & ~resolved.resolved.isRealtime THEN
  637. Error(position,Diagnostics.Invalid,"realtime object contains references to non-realtime objects");
  638. END;
  639. IF type.isUnsafe & (resolved.resolved IS SyntaxTree.ArrayType) THEN
  640. (*IF ~IsOpenArray(resolved.resolved, base) THEN
  641. Error(position,Diagnostics.Invalid,"forbidden unsafe at static array");
  642. ELS
  643. *)
  644. IF IsOpenArray(resolved.resolved(SyntaxTree.ArrayType).arrayBase, base) THEN
  645. Error(position,Diagnostics.Invalid,"forbidden unsafe at multidimensional array");
  646. END;
  647. END;
  648. ELSE
  649. Error(position,Diagnostics.Invalid,"forbidden pointer base type");
  650. type.SetPointerBase(SyntaxTree.invalidType)
  651. END
  652. END FixPointerType;
  653. (**
  654. resolve pointer type
  655. - enter pointer type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  656. **)
  657. PROCEDURE VisitPointerType(x: SyntaxTree.PointerType);
  658. VAR recordType: SyntaxTree.RecordType; recordBaseType: SyntaxTree.Type;
  659. modifiers: SyntaxTree.Modifier; position: LONGINT;
  660. BEGIN
  661. IF TypeNeedsResolution(x) THEN
  662. modifiers := x.modifiers;
  663. x.SetRealtime(HasFlag(modifiers,Global.NameRealtime, position));
  664. x.SetPlain(HasFlag(modifiers,Global.NamePlain,position));
  665. x.SetDisposable(HasFlag(modifiers,Global.NameDisposable, position));
  666. x.SetUnsafe(HasFlag(modifiers,Global.NameUnsafe,position));
  667. (* inheritance cycle check
  668. example:
  669. A=POINTER TO RECORD(B) END;
  670. B=POINTER TO RECORD(A) END;
  671. *)
  672. IF x.pointerBase IS SyntaxTree.RecordType THEN
  673. recordType := x.pointerBase(SyntaxTree.RecordType);
  674. recordBaseType := ResolveType(recordType.baseType);
  675. recordType.SetBaseType(recordBaseType);
  676. recordType.SetProtected(HasFlag(modifiers, Global.NameExclusive, position));
  677. END;
  678. CheckModifiers(modifiers, TRUE);
  679. typeFixes.Add(x,currentScope);
  680. x.SetState(SyntaxTree.Resolved);
  681. END;
  682. resolvedType := ResolvedType(x)
  683. END VisitPointerType;
  684. (**
  685. resolve port type
  686. - enter port type to list of deferred fixes (to avoid infinite loops in the declaration phase)
  687. **)
  688. PROCEDURE VisitPortType(x: SyntaxTree.PortType);
  689. VAR value: LONGINT;
  690. BEGIN
  691. IF TypeNeedsResolution(x) THEN
  692. x.SetCellsAreObjects(cellsAreObjects);
  693. x.SetSizeExpression(ResolveExpression(x.sizeExpression));
  694. IF (x.sizeExpression # NIL) & CheckPositiveIntegerValue(x.sizeExpression,value,FALSE) THEN
  695. x.SetSize(value)
  696. ELSE
  697. x.SetSize(system.SizeOf(system.longintType));
  698. END;
  699. x.SetState(SyntaxTree.Resolved);
  700. END;
  701. resolvedType := ResolvedType(x)
  702. END VisitPortType;
  703. (**
  704. deferred procedure type resolving
  705. - resolve return type
  706. - traverse and resolve parameters
  707. **)
  708. PROCEDURE FixProcedureType(procedureType: SyntaxTree.ProcedureType);
  709. VAR resolved: SyntaxTree.Type; parameter: SyntaxTree.Parameter;
  710. BEGIN
  711. resolved := ResolveType(procedureType.returnType);
  712. IF (resolved # NIL) & (resolved.resolved IS SyntaxTree.ArrayType) & (resolved.resolved(SyntaxTree.ArrayType).length = NIL) THEN
  713. Error(procedureType.position,Diagnostics.Invalid,"forbidden open array return type");
  714. ELSIF (resolved # NIL) & (procedureType.noReturn) THEN
  715. Error(procedureType.position,Diagnostics.Invalid,"procedure with return type does not return");
  716. END;
  717. procedureType.SetReturnType(resolved);
  718. IF (resolved # NIL) THEN
  719. parameter := SyntaxTree.NewParameter(procedureType.position,procedureType,Global.ResultName, SyntaxTree.VarParameter);
  720. parameter.SetType(procedureType.returnType);
  721. parameter.SetAccess(SyntaxTree.Hidden);
  722. parameter.SetUntraced(procedureType.hasUntracedReturn);
  723. VisitParameter(parameter);
  724. procedureType.SetReturnParameter(parameter); (* return parameter serves as a cache only *)
  725. END;
  726. (* process parameters *)
  727. parameter :=procedureType.firstParameter;
  728. WHILE (parameter # NIL) DO
  729. VisitParameter(parameter);
  730. parameter := parameter.nextParameter;
  731. END;
  732. END FixProcedureType;
  733. PROCEDURE HasFlag(VAR modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT): BOOLEAN;
  734. VAR prev,this: SyntaxTree.Modifier;
  735. BEGIN
  736. this := modifiers;prev := NIL;
  737. WHILE (this # NIL) & (this.identifier # name) DO
  738. prev := this; this := this.nextModifier;
  739. END;
  740. IF this # NIL THEN
  741. IF this.expression # NIL THEN
  742. Error(this.position,Diagnostics.Invalid,"unexpected expression");
  743. END;
  744. this.Resolved;
  745. position := this.position;
  746. RETURN TRUE
  747. ELSE
  748. RETURN FALSE
  749. END;
  750. END HasFlag;
  751. PROCEDURE HasValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: LONGINT): BOOLEAN;
  752. VAR prev,this: SyntaxTree.Modifier;
  753. BEGIN
  754. this := modifiers;prev := NIL;
  755. WHILE (this # NIL) & (this.identifier # name) DO
  756. prev := this; this := this.nextModifier;
  757. END;
  758. IF this # NIL THEN
  759. IF this.expression = NIL THEN
  760. Error(this.position,Diagnostics.Invalid,"expected expression value");
  761. ELSE
  762. this.SetExpression(ConstantExpression(this.expression));
  763. IF CheckIntegerValue(this.expression,value) THEN END;
  764. END;
  765. this.Resolved;
  766. position := this.position;
  767. RETURN TRUE
  768. ELSE RETURN FALSE
  769. END;
  770. END HasValue;
  771. PROCEDURE HasStringValue(modifiers: SyntaxTree.Modifier; name: SyntaxTree.Identifier; VAR position: LONGINT; VAR value: ARRAY OF CHAR): BOOLEAN;
  772. VAR prev,this: SyntaxTree.Modifier;
  773. BEGIN
  774. this := modifiers;prev := NIL;
  775. WHILE (this # NIL) & (this.identifier # name) DO
  776. prev := this; this := this.nextModifier;
  777. END;
  778. IF this # NIL THEN
  779. IF this.expression = NIL THEN
  780. Error(this.position,Diagnostics.Invalid,"expected expression value");
  781. ELSE
  782. this.SetExpression(ConstantExpression(this.expression));
  783. IF CheckStringValue(this.expression,value) THEN END;
  784. END;
  785. this.Resolved;
  786. position := this.position;
  787. RETURN TRUE
  788. ELSE RETURN FALSE
  789. END;
  790. END HasStringValue;
  791. PROCEDURE SkipImplementation*(x: SyntaxTree.CellType): BOOLEAN;
  792. VAR svalue: ARRAY 32 OF CHAR; position: LONGINT;
  793. BEGIN
  794. IF cellsAreObjects THEN RETURN FALSE END;
  795. IF (backendName = "TRM") & x.isCellNet THEN RETURN TRUE END;
  796. IF HasStringValue(x.modifiers,Global.NameBackend,position,svalue) THEN
  797. IF svalue[0] = "~" THEN
  798. Strings.TrimLeft(svalue, "~");
  799. IF svalue = backendName THEN
  800. RETURN TRUE;
  801. END;
  802. ELSIF svalue # backendName THEN
  803. RETURN TRUE;
  804. END;
  805. END;
  806. IF x.baseType # NIL THEN
  807. RETURN SkipImplementation(x.baseType.resolved(SyntaxTree.CellType));
  808. END;
  809. RETURN FALSE;
  810. END SkipImplementation;
  811. PROCEDURE CheckModifiers(modifiers: SyntaxTree.Modifier; checkUse: BOOLEAN);
  812. VAR this: SyntaxTree.Modifier;
  813. BEGIN
  814. this := modifiers;
  815. WHILE this # NIL DO
  816. IF ~this.resolved THEN
  817. IF checkUse THEN
  818. Error(this.position,Diagnostics.Invalid,"unexpected modifier");
  819. ELSE
  820. this.SetExpression(ResolveExpression(this.expression));
  821. this.Resolved;
  822. (*! sanity check for "unqualified" modifiers, as for example used in ActiveCells Engine parameterization *)
  823. END;
  824. END;
  825. this := this.nextModifier
  826. END;
  827. END CheckModifiers;
  828. (**
  829. resolve procedure type
  830. - enter procedure to list of deferred fixes (to avoid infinite loops in the declaration phase)
  831. **)
  832. PROCEDURE VisitProcedureType(procedureType: SyntaxTree.ProcedureType);
  833. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT;
  834. BEGIN
  835. IF TypeNeedsResolution(procedureType) THEN
  836. modifiers := procedureType.modifiers;
  837. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  838. ELSIF HasFlag(modifiers, Global.NameInterrupt,position) THEN
  839. procedureType.SetInterrupt(TRUE);
  840. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  841. ELSIF HasFlag(modifiers,Global.NameC,position) THEN
  842. IF useDarwinCCalls THEN (*fld*)
  843. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  844. ELSE
  845. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  846. END
  847. ELSIF HasFlag(modifiers, Global.NameNoReturn,position) THEN
  848. procedureType.SetNoReturn(TRUE);
  849. END;
  850. IF HasValue(modifiers,Global.NameStackAligned,position,value) THEN procedureType.SetStackAlignment(value) END;
  851. IF HasFlag(modifiers, Global.NameDelegate,position) THEN procedureType.SetDelegate(TRUE) END;
  852. IF HasFlag(modifiers, Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  853. CheckModifiers(modifiers, TRUE);
  854. modifiers := procedureType.returnTypeModifiers;
  855. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  856. CheckModifiers(modifiers, TRUE);
  857. typeFixes.Add(procedureType,currentScope);
  858. procedureType.SetHasPointers(procedureType.isDelegate);
  859. procedureType.SetState(SyntaxTree.Resolved);
  860. END;
  861. resolvedType := ResolvedType(procedureType)
  862. END VisitProcedureType;
  863. (** check and resolve record type
  864. - check base type: must be record, math array or array-structured object type
  865. - check declarations
  866. - every record type is guaranteed to have a type declaration in the module scope (anonymous or not)
  867. **)
  868. PROCEDURE VisitRecordType(x: SyntaxTree.RecordType);
  869. VAR resolved, baseType: SyntaxTree.Type; position: LONGINT;
  870. numberMethods: LONGINT; recordBase, recordType: SyntaxTree.RecordType; procedure: SyntaxTree.Procedure;
  871. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN;
  872. hasPointers: BOOLEAN;
  873. modifiers: SyntaxTree.Modifier;
  874. value: LONGINT;
  875. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type; VAR recordType: SyntaxTree.RecordType): BOOLEAN;
  876. BEGIN
  877. type := type.resolved;
  878. IF (type IS SyntaxTree.PointerType) &
  879. (type(SyntaxTree.PointerType).pointerBase.resolved # NIL) &
  880. (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) THEN
  881. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  882. RETURN TRUE
  883. ELSE
  884. RETURN FALSE
  885. END;
  886. END IsPointerToRecord;
  887. BEGIN
  888. IF TypeNeedsResolution(x) THEN
  889. hasPointers := FALSE;
  890. modifiers := x.modifiers;
  891. IF HasValue(modifiers,Global.NameAligned,position,value) THEN x.SetAlignmentInBits(value*system.dataUnit) END;
  892. CheckModifiers(modifiers, TRUE);
  893. IF x.baseType # NIL THEN
  894. position := x.baseType.position;
  895. baseType := ResolveType(x.baseType);
  896. resolved := baseType.resolved;
  897. hasPointers := hasPointers OR resolved.hasPointers;
  898. IF x.isObject THEN (* object *)
  899. ASSERT(x.pointerType # NIL);
  900. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  901. ELSIF resolved IS SyntaxTree.ObjectType THEN (* the type denoted by the <<OBJECT>> alone *)
  902. baseType := NIL
  903. ELSIF IsPointerToRecord(resolved,recordType) THEN
  904. IF ~recordType.isObject THEN Warning(position, "deprecated extension of record to object"); END;
  905. ELSIF resolved IS SyntaxTree.MathArrayType THEN
  906. ELSE
  907. Error(position, Diagnostics.Invalid,"object does not extend pointer to record, object or math array ")
  908. END;
  909. ELSIF x.pointerType # NIL THEN (* record with type declaration POINTER TO RECORD *)
  910. IF resolved = SyntaxTree.invalidType THEN (* error already handled *)
  911. ELSIF IsPointerToRecord(resolved,recordType) THEN
  912. IF recordType.isObject THEN Error(position, Diagnostics.Invalid,"pointer to record extends object") END;
  913. ELSIF resolved IS SyntaxTree.RecordType THEN
  914. ELSE
  915. Error(position, Diagnostics.Invalid,"pointer to record does not extend pointer to record or record")
  916. END;
  917. ELSE
  918. IF resolved IS SyntaxTree.RecordType THEN
  919. ELSE
  920. Error(position, Diagnostics.Invalid,"record does not extend record")
  921. END;
  922. END;
  923. x.SetBaseType(baseType);
  924. IF x.Level() > 15 THEN
  925. Error(position,Diagnostics.Invalid,"record/object inheritance level too high");
  926. (* note:
  927. the restriction to inheritance with a maximum level of 15 is caused by the implementation of the
  928. runtime structures: type tests are very efficient and rely on the fact that each type descriptor contains the whole
  929. inheritance history of a type.
  930. Example: let inhertitance oe given by B(A), C(B), D(C) etc.
  931. Then the type descriptor of G contains: A|B|C|D|E|F|G|0|0|0...
  932. while the type decriptor of D contains: A|B|C|D|0|0|0|0|0|0...
  933. *)
  934. END;
  935. IF (x.pointerType # NIL) & (resolved IS SyntaxTree.PointerType) & (x.pointerType.isDisposable # resolved(SyntaxTree.PointerType).isDisposable) THEN
  936. Error(position,Diagnostics.Invalid,"invalid inheritance of disposable types");
  937. END;
  938. END;
  939. Declarations(x.recordScope, FALSE);
  940. ResolveArrayStructure(x);
  941. (* computation of sizes and offsets skipped -> done in backend / system *)
  942. recordBase := x.GetBaseRecord();
  943. IF recordBase = NIL THEN numberMethods := 0
  944. ELSE numberMethods := recordBase.recordScope.numberMethods
  945. END;
  946. isRealtime := TRUE;
  947. symbol := x.recordScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  948. WHILE symbol # NIL DO
  949. IF symbol IS SyntaxTree.Variable THEN
  950. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  951. hasPointers := hasPointers OR symbol.type.resolved.hasPointers & ~symbol(SyntaxTree.Variable).untraced;
  952. END;
  953. IF symbol IS SyntaxTree.Procedure THEN
  954. procedure := symbol(SyntaxTree.Procedure);
  955. IF procedure.super # NIL THEN
  956. procedure.SetMethodNumber(procedure.super.methodNumber)
  957. ELSIF InMethodTable(procedure) THEN (* not a static method *)
  958. procedure.SetMethodNumber(numberMethods);
  959. INC(numberMethods);
  960. END;
  961. END;
  962. symbol := symbol.nextSymbol;
  963. END;
  964. IF isRealtime THEN x.SetRealtime(TRUE) END;
  965. x.recordScope.SetNumberMethods(numberMethods);
  966. (* TODO: is this needed anymore? *)
  967. IF (x.isObject) & (x.baseType # NIL) & (x.baseType.resolved IS SyntaxTree.RecordType) THEN
  968. Error(x.position,Diagnostics.Invalid,"object extends a record")
  969. END;
  970. IF (x.typeDeclaration = NIL) THEN
  971. IF (x.pointerType # NIL) & (x.pointerType.resolved.typeDeclaration # NIL) THEN
  972. x.SetTypeDeclaration(x.pointerType.resolved.typeDeclaration);
  973. (*
  974. x.pointerType.resolved.typeDeclaration.name.GetString(name);
  975. AnonymousTypeDeclaration(x,name);
  976. *)
  977. ELSE
  978. AnonymousTypeDeclaration(x,"Anonymous");
  979. END;
  980. END;
  981. x.SetHasPointers(hasPointers);
  982. x.SetState(SyntaxTree.Resolved);
  983. END;
  984. resolvedType := ResolvedType(x);
  985. END VisitRecordType;
  986. (** check and resolve cell type
  987. - check base type: must be cell
  988. - check declarations
  989. - every cell type is guaranteed to have a type declaration in the module scope (anonymous or not)
  990. **)
  991. PROCEDURE VisitCellType(x: SyntaxTree.CellType);
  992. VAR
  993. symbol: SyntaxTree.Symbol; isRealtime: BOOLEAN; parameter: SyntaxTree.Parameter; type: SyntaxTree.Type; len: LONGINT;
  994. modifier: SyntaxTree.Modifier; position,value: LONGINT; isEngine: BOOLEAN; property: SyntaxTree.Property;
  995. qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  996. recordBase: SyntaxTree.RecordType;
  997. numberMethods, int: LONGINT;
  998. real: LONGREAL;
  999. bool: BOOLEAN;
  1000. set: SET;
  1001. v: SyntaxTree.Expression;
  1002. str: Scanner.StringType;
  1003. atype: SyntaxTree.ArrayType;
  1004. prev: SyntaxTree.Scope;
  1005. skip: BOOLEAN;
  1006. svalue: ARRAY 32 OF CHAR;
  1007. BEGIN
  1008. IF TypeNeedsResolution(x) THEN
  1009. recordBase := NIL;
  1010. IF cellsAreObjects THEN
  1011. IF x.baseType = NIL THEN
  1012. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(x.position, SyntaxTree.NewIdentifier("ActiveCellsRuntime"), SyntaxTree.NewIdentifier("Cell"));
  1013. ImportModule(qualifiedIdentifier.prefix, x.position);
  1014. x.SetBaseType(SyntaxTree.NewQualifiedType(x.position, currentScope, qualifiedIdentifier));
  1015. x.SetBaseType(ResolveType(x.baseType));
  1016. recordBase := x.GetBaseRecord();
  1017. IF recordBase = NIL THEN
  1018. Error(x.position,Diagnostics.Invalid,"ActiveCellsRuntime.Cell not present");
  1019. END;
  1020. ELSE
  1021. x.SetBaseType(ResolveType(x.baseType));
  1022. END;
  1023. ELSE
  1024. x.SetBaseType(ResolveType(x.baseType));
  1025. END;
  1026. IF recordBase = NIL THEN numberMethods := 0
  1027. ELSE numberMethods := recordBase.recordScope.numberMethods
  1028. END;
  1029. modifier := x.modifiers;
  1030. (*IF ~x.isCellNet THEN*)
  1031. IF HasValue(modifier,Global.NameDataMemorySize,position,value) THEN END;
  1032. IF HasValue(modifier,Global.NameCodeMemorySize,position,value) THEN END;
  1033. IF HasFlag(modifier, Global.NameEngine, position) THEN isEngine := TRUE ELSE isEngine := FALSE END;
  1034. IF HasFlag(modifier, Global.NameVector,position) THEN END;
  1035. IF HasFlag(modifier, Global.NameFloatingPoint, position) THEN END;
  1036. IF HasFlag(modifier, Global.NameNoMul,position) THEN END;
  1037. IF HasFlag(modifier, Global.NameNonBlockingIO,position) THEN END;
  1038. IF HasFlag(modifier, Global.NameTRM, position) THEN END;
  1039. IF HasFlag(modifier, Global.NameTRMS, position) THEN END;
  1040. symbol := system.activeCellsCapabilities;
  1041. WHILE symbol # NIL DO
  1042. IF HasFlag(modifier, symbol.name, position) THEN END;
  1043. symbol := symbol.nextSymbol;
  1044. END;
  1045. modifier := x.modifiers;
  1046. WHILE (modifier # NIL) DO
  1047. property := SyntaxTree.NewProperty(modifier.position, modifier.identifier);
  1048. IF modifier.expression # NIL THEN
  1049. v := ConstantExpression(modifier.expression);
  1050. property.SetValue(v);
  1051. IF IsIntegerValue(modifier.expression, int) THEN
  1052. (*property.SetValue(modifier.expression);*)
  1053. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longintType, NIL));
  1054. property.SetType(system.longintType);
  1055. ELSIF IsRealValue(modifier.expression, real) THEN
  1056. modifier.SetExpression(NewConversion(modifier.position, modifier.expression, system.longrealType, NIL));
  1057. property.SetType(system.longrealType);
  1058. ELSIF IsBooleanValue(modifier.expression, bool) THEN
  1059. property.SetType(system.booleanType);
  1060. ELSIF IsSetValue(modifier.expression, set) THEN
  1061. property.SetType(system.setType);
  1062. ELSIF IsStringValue(modifier.expression, str) THEN
  1063. (*property.SetValue(modifier.expression);*)
  1064. atype := SyntaxTree.NewArrayType(-1, NIL, SyntaxTree.Static);
  1065. atype.SetArrayBase(modifier.expression.type(SyntaxTree.StringType).baseType);
  1066. atype.SetLength(Global.NewIntegerValue(system,-1, (* type(SyntaxTree.StringType).length *) 256 (*! check if this is a good idea *) ));
  1067. property.SetType(atype);
  1068. ELSE
  1069. Error(modifier.position, Diagnostics.Invalid, "unsupported property type");
  1070. END;
  1071. ELSE (* flag property *)
  1072. (*property.SetValue(SyntaxTree.NewBooleanValue(position,TRUE));*)
  1073. property.SetType(system.booleanType);
  1074. END;
  1075. (* property.SetScope(x.cellScope); *) (* not required, will be done during entry *)
  1076. (* property.SetState(SyntaxTree.Resolved); *) (* not required, will be done during entry *)
  1077. x.AddProperty(property);
  1078. modifier := modifier.nextModifier;
  1079. END;
  1080. CheckModifiers(modifier, FALSE);
  1081. Declarations(x.cellScope, SkipImplementation(x));
  1082. (* process parameters *)
  1083. prev := currentScope;
  1084. currentScope := x.cellScope;
  1085. parameter :=x.firstParameter;
  1086. WHILE (parameter # NIL) DO
  1087. VisitParameter(parameter);
  1088. type := parameter.type.resolved;
  1089. IF ~(type IS SyntaxTree.PortType) THEN
  1090. WHILE IsStaticArray(type, type, len) DO
  1091. IF IsDynamicArray(type, type) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1092. END;
  1093. WHILE IsDynamicArray(type, type) DO
  1094. IF IsStaticArray(type, type, len) THEN Error(parameter.position, Diagnostics.Invalid, "invalid mixture of dynamic and static array of ports") END;
  1095. END;
  1096. IF (* ~IsStaticArray(type,type,len) OR*) ~(type IS SyntaxTree.PortType) THEN
  1097. Error(parameter.position, Diagnostics.Invalid, "invalid type, must be port or static array of port ");
  1098. END;
  1099. END;
  1100. parameter := parameter.nextParameter;
  1101. END;
  1102. currentScope := prev;
  1103. symbol := x.cellScope.firstSymbol; (* sorted symbols, important to go through procedures in a sorted way here !*)
  1104. WHILE symbol # NIL DO
  1105. IF symbol IS SyntaxTree.Variable THEN
  1106. isRealtime := isRealtime & symbol.type.resolved.isRealtime;
  1107. END;
  1108. symbol := symbol.nextSymbol;
  1109. END;
  1110. IF isRealtime THEN x.SetRealtime(TRUE) END;
  1111. IF (x.typeDeclaration = NIL) THEN
  1112. AnonymousTypeDeclaration(x,"Anonymous");
  1113. END;
  1114. x.SetState(SyntaxTree.Resolved);
  1115. IF (x.cellScope.bodyProcedure = NIL) & (~isEngine)THEN
  1116. Error(x.position, Diagnostics.Invalid, "Forbidden empty Body.");
  1117. ELSIF (x.cellScope.bodyProcedure # NIL) & (isEngine)THEN
  1118. Warning(x.position, "Non-empty body for an engine?");
  1119. END;
  1120. END;
  1121. resolvedType := ResolvedType(x);
  1122. END VisitCellType;
  1123. (* check if an object is an array-structured object type
  1124. - determine the array structure
  1125. - collect operators from top to bottom in the inheritance hierarchy
  1126. - check if LEN operator is declared
  1127. - determine number of possible index operators
  1128. - for non-tensors, check if index operators on ranges (RANGE, RANGE, ... RANGE) are present
  1129. - for tensors, check if general index operators (ARRAY [*] OF RANGE) are present
  1130. *)
  1131. PROCEDURE ResolveArrayStructure*(recordType: SyntaxTree.RecordType);
  1132. VAR
  1133. indexOperatorCount, i: LONGINT;
  1134. arrayAccessOperators: SyntaxTree.ArrayAccessOperators;
  1135. isTensor: BOOLEAN;
  1136. BEGIN
  1137. IF recordType.isObject & (recordType.baseType # NIL) THEN
  1138. (* determine array structure *)
  1139. recordType.SetArrayStructure(MathArrayStructureOfType(recordType.baseType.resolved))
  1140. END;
  1141. IF recordType.HasArrayStructure() THEN
  1142. (* the object is an ASOT *)
  1143. isTensor := recordType.arrayStructure.form = SyntaxTree.Tensor;
  1144. (* reset array access operators *)
  1145. arrayAccessOperators.len := NIL;
  1146. arrayAccessOperators.generalRead := NIL;
  1147. arrayAccessOperators.generalWrite := NIL;
  1148. IF isTensor THEN
  1149. (* all operators of dimensionalities 1 to max *)
  1150. indexOperatorCount := TwoToThePowerOf(MaxTensorIndexOperatorSize + 1) - 2
  1151. ELSE
  1152. (* all operators of certain dimensionality *)
  1153. indexOperatorCount := TwoToThePowerOf(recordType.arrayStructure.Dimensionality())
  1154. END;
  1155. NEW(arrayAccessOperators.read, indexOperatorCount);
  1156. NEW(arrayAccessOperators.write, indexOperatorCount);
  1157. FOR i := 0 TO indexOperatorCount - 1 DO
  1158. arrayAccessOperators.read[i] := NIL;
  1159. arrayAccessOperators.write[i] := NIL
  1160. END;
  1161. (* collect access operators in the record scope *)
  1162. CollectArrayAccessOperators(recordType.recordScope, recordType.arrayStructure, arrayAccessOperators);
  1163. IF arrayAccessOperators.len = NIL THEN
  1164. (* TODO: think about making this operator optional for static array structures *)
  1165. Error(recordType.position, Diagnostics.Invalid, "LEN operator missing")
  1166. END;
  1167. (* show error messages *)
  1168. IF isTensor THEN
  1169. (* require ARRAY [*] OF RANGE *)
  1170. IF arrayAccessOperators.generalRead = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator missing") END;
  1171. IF arrayAccessOperators.generalWrite = NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator missing") END;
  1172. ELSE
  1173. (* forbid ARRAY [*] OF RANGE *)
  1174. IF arrayAccessOperators.generalRead # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general read operator not applicable") END;
  1175. IF arrayAccessOperators.generalWrite # NIL THEN Error(recordType.position, Diagnostics.Invalid, "general write operator not applicable") END;
  1176. (* require RANGE, RANGE, ... RANGE *)
  1177. IF arrayAccessOperators.read[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "read operator on ranges missing") END;
  1178. IF arrayAccessOperators.write[indexOperatorCount - 1] = NIL THEN Error(recordType.position, Diagnostics.Invalid, "write operator on ranges missing") END;
  1179. END;
  1180. recordType.SetArrayAccessOperators(arrayAccessOperators)
  1181. ELSE
  1182. (* make sure record scopes of non-ASOT object types do not contain operator declarations *)
  1183. IF recordType.recordScope.firstOperator # NIL THEN
  1184. RETURN;
  1185. Error(recordType.recordScope.firstOperator.position, Diagnostics.Invalid, "operator declared for record type without array structure")
  1186. END
  1187. END
  1188. END ResolveArrayStructure;
  1189. (** collect array access operators in a record scope **)
  1190. PROCEDURE CollectArrayAccessOperators(recordScope: SyntaxTree.RecordScope; arrayStructure: SyntaxTree.MathArrayType; VAR arrayAccessOperators: SyntaxTree.ArrayAccessOperators);
  1191. VAR
  1192. baseType: SyntaxTree.Type;
  1193. operator: SyntaxTree.Operator;
  1194. isReadOperator, isGeneralOperator: BOOLEAN;
  1195. indexListSize, indexListKind, hashValue: LONGINT;
  1196. BEGIN
  1197. (* if a parent record scope exists, collect the operators there first *)
  1198. baseType := recordScope.ownerRecord.baseType;
  1199. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.PointerType) THEN
  1200. baseType := baseType.resolved(SyntaxTree.PointerType).pointerBase.resolved
  1201. END;
  1202. IF (baseType # NIL) & (baseType.resolved IS SyntaxTree.RecordType) THEN
  1203. CollectArrayAccessOperators(baseType(SyntaxTree.RecordType).recordScope, arrayStructure, arrayAccessOperators);
  1204. END;
  1205. (* go through all operators in the current record scope *)
  1206. operator := recordScope.firstOperator;
  1207. WHILE operator # NIL DO
  1208. IF operator.name=SyntaxTree.NewIdentifier("LEN") THEN
  1209. IF CheckLenOperator(operator, arrayStructure) THEN arrayAccessOperators.len := operator END
  1210. ELSIF operator.name = SyntaxTree.NewIdentifier("[]") THEN
  1211. IF CheckIndexOperator(operator, arrayStructure, isReadOperator, isGeneralOperator, indexListSize, indexListKind) THEN
  1212. IF isGeneralOperator THEN
  1213. IF isReadOperator THEN
  1214. arrayAccessOperators.generalRead := operator
  1215. ELSE
  1216. arrayAccessOperators.generalWrite := operator
  1217. END
  1218. ELSE
  1219. hashValue := IndexOperatorHash(indexListSize, indexListKind, arrayStructure.form = SyntaxTree.Tensor);
  1220. IF isReadOperator THEN
  1221. arrayAccessOperators.read[hashValue] := operator
  1222. ELSE
  1223. arrayAccessOperators.write[hashValue] := operator
  1224. END
  1225. END
  1226. END
  1227. ELSE
  1228. Error(operator.position, Diagnostics.Invalid, 'invalid operator')
  1229. END;
  1230. operator := operator.nextOperator
  1231. END
  1232. END CollectArrayAccessOperators;
  1233. (** the hash value of an index operator **)
  1234. PROCEDURE IndexOperatorHash(indexListSize, indexListKind: LONGINT; isTensor: BOOLEAN): LONGINT;
  1235. VAR result: LONGINT;
  1236. BEGIN
  1237. IF isTensor THEN
  1238. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1239. result := -1 (* no fixed-dim. index operator may exist for this scenario: thus, no hash value *)
  1240. ELSE
  1241. result := TwoToThePowerOf(indexListSize) - 2 + indexListKind
  1242. END
  1243. ELSE
  1244. result := indexListKind
  1245. END;
  1246. RETURN result
  1247. END IndexOperatorHash;
  1248. (** 2 to the power of exponent **)
  1249. PROCEDURE TwoToThePowerOf(exponent: LONGINT): LONGINT;
  1250. VAR result, i: LONGINT;
  1251. BEGIN
  1252. result := 1;
  1253. FOR i := 1 TO exponent DO
  1254. result := result * 2;
  1255. END;
  1256. RETURN result
  1257. END TwoToThePowerOf;
  1258. (** check if a LEN operator has a correct signature. i.e.
  1259. for non-tensors: 'OPERATOR "LEN"(): ARRAY [<Dimensionality>] OF LONGINT;'
  1260. for tensors (or non-tensors): 'OPERATOR "LEN"(): ARRAY [*] OF LONGINT;'
  1261. **)
  1262. PROCEDURE CheckLenOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType): BOOLEAN;
  1263. VAR
  1264. procedureType: SyntaxTree.ProcedureType;
  1265. returnedArrayType: SyntaxTree.MathArrayType;
  1266. result: BOOLEAN;
  1267. BEGIN
  1268. result := FALSE;
  1269. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1270. IF (procedureType.numberParameters = 0) THEN
  1271. IF (procedureType.returnType # NIL) & (procedureType.returnType.resolved IS SyntaxTree.MathArrayType) THEN
  1272. returnedArrayType := procedureType.returnType.resolved(SyntaxTree.MathArrayType);
  1273. IF system.longintType.SameType(returnedArrayType.arrayBase.resolved) THEN
  1274. IF returnedArrayType.form = SyntaxTree.Open THEN
  1275. (* ARRAY [*] OF LONGINT: acceptable for both tensors and non-tensors *)
  1276. result := TRUE
  1277. ELSIF arrayStructure.form # SyntaxTree.Tensor THEN
  1278. (* ARRAY [<Dimensionality>] OF LONGINT: only acceptable for non-tensors *)
  1279. IF (returnedArrayType.form = SyntaxTree.Static) & (returnedArrayType.staticLength = arrayStructure.Dimensionality()) THEN
  1280. result := TRUE
  1281. END
  1282. END
  1283. END
  1284. END
  1285. END;
  1286. IF result THEN
  1287. (* export symbol automatically *)
  1288. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal)
  1289. ELSE
  1290. Error(operator.position, Diagnostics.Invalid, "LEN operator with invalid signature");
  1291. END;
  1292. RETURN result
  1293. END CheckLenOperator;
  1294. (** check if an index operator has a correct signature. i.e.
  1295. - for read operators: 'OPERATOR "[]"(<IndexParameters>): <DataType>;'
  1296. - for write operators: 'OPERATOR "[]"(<IndexParameters>; rhs: <DataType>);'
  1297. - for general operators: <IndexParameters> = ARRAY [*] OF RANGE
  1298. - for fixed-dim. operators: <IndexParameters> = i0: <LONGINT/RANGE>; i1: <LONGINT/RANGE>; ...; in: <LONGINT/RANGE>
  1299. - determine if it is a read or write operator (existance of return type)
  1300. - check index parameters
  1301. - for fixed-dim. operators, determine the size of the index lists, the operator handles
  1302. - for fixed-dim. operators, determine the kind of the index list this operator handles. index lists kinds are calculated as follows:
  1303. [LONGINT] -> binary 0 -> 0
  1304. [RANGE] -> binary 1 -> 1
  1305. [LONGINT, LONGINT] -> binary 00 -> 0
  1306. [LONGINT, RANGE] -> binary 01 -> 1
  1307. [RANGE, LONGINT] -> binary 10 -> 2
  1308. [RANGE, RANGE] -> binary 11 -> 3
  1309. etc.
  1310. - for fixed-dim. operators and non-tensors, check if number of index parameters equals the ASOT's dimensionality
  1311. - for read operators, check if return type matches the type of data that is read
  1312. - for write operators, check if last parameter type matches the type of data that is written
  1313. **)
  1314. PROCEDURE CheckIndexOperator(operator: SyntaxTree.Operator; arrayStructure: SyntaxTree.MathArrayType; VAR isReadOperator, isGeneralOperator: BOOLEAN; VAR indexListSize, indexListKind: LONGINT): BOOLEAN;
  1315. VAR
  1316. elementType, otherElementType, dataType: SyntaxTree.Type;
  1317. procedureType: SyntaxTree.ProcedureType;
  1318. mathArrayType: SyntaxTree.MathArrayType;
  1319. parameter: SyntaxTree.Parameter;
  1320. parameterCount, rangeCount, i: LONGINT;
  1321. hasTypeError: BOOLEAN;
  1322. BEGIN
  1323. procedureType := operator.type.resolved(SyntaxTree.ProcedureType);
  1324. parameterCount := procedureType.numberParameters; (* true parameter count *)
  1325. (* determine if it is a read or write operator *)
  1326. isReadOperator := (procedureType.returnType # NIL);
  1327. IF isReadOperator THEN
  1328. indexListSize := parameterCount;
  1329. ELSE
  1330. indexListSize := parameterCount - 1;
  1331. END;
  1332. IF indexListSize < 1 THEN
  1333. Error(operator.position, Diagnostics.Invalid, "index operator with too few parameters");
  1334. RETURN FALSE
  1335. END;
  1336. IF procedureType.firstParameter.type.resolved IS SyntaxTree.MathArrayType THEN
  1337. (* general operator *)
  1338. isGeneralOperator := TRUE;
  1339. IF indexListSize > 1 THEN
  1340. Error(operator.position, Diagnostics.Invalid, "index operator with too many parameters");
  1341. RETURN FALSE
  1342. END;
  1343. (* ARRAY [*] OF RANGE*)
  1344. mathArrayType := procedureType.firstParameter.type.resolved(SyntaxTree.MathArrayType);
  1345. IF ~((mathArrayType.arrayBase.resolved IS SyntaxTree.RangeType) & (mathArrayType.form = SyntaxTree.Open)) THEN
  1346. Error(operator.position, Diagnostics.Invalid, "index parameter not dynamic math array of range");
  1347. RETURN FALSE
  1348. END;
  1349. parameter := procedureType.firstParameter.nextParameter
  1350. ELSE
  1351. (* fixed-dim. operator *)
  1352. isGeneralOperator := FALSE;
  1353. (* check number of index parameters *)
  1354. IF arrayStructure.form = SyntaxTree.Tensor THEN
  1355. (* for tensors, limited to a certain size *)
  1356. IF indexListSize > MaxTensorIndexOperatorSize THEN
  1357. Error(operator.position, Diagnostics.Invalid, "too many index parameters for tensor");
  1358. RETURN FALSE
  1359. END
  1360. ELSE
  1361. (* for non-tensors, depends on dimensionality *)
  1362. IF indexListSize # arrayStructure.Dimensionality() THEN
  1363. Error(operator.position, Diagnostics.Invalid, "index parameter count does not match dimensionality");
  1364. RETURN FALSE
  1365. END
  1366. END;
  1367. (* go through all index parameters
  1368. - count the number of ranges
  1369. - determine the index list kind number
  1370. *)
  1371. indexListKind := 0;
  1372. rangeCount := 0;
  1373. parameter := procedureType.firstParameter;
  1374. FOR i := 1 TO indexListSize DO
  1375. indexListKind := indexListKind * 2;
  1376. IF parameter.type.resolved IS SyntaxTree.IntegerType THEN
  1377. ELSIF parameter.type.resolved IS SyntaxTree.RangeType THEN
  1378. INC(indexListKind);
  1379. INC(rangeCount)
  1380. ELSE
  1381. Error(parameter.position, Diagnostics.Invalid, "integer or range expected");
  1382. RETURN FALSE
  1383. END;
  1384. parameter := parameter.nextParameter
  1385. END;
  1386. END;
  1387. (*
  1388. - for read operators: check type of last parameter
  1389. - for write operators: check return type
  1390. *)
  1391. IF isReadOperator THEN
  1392. dataType := procedureType.returnType (* the return type *)
  1393. ELSE
  1394. dataType := parameter.type (* the type of the last non-hidden parameter *)
  1395. END;
  1396. elementType := arrayStructure.ElementType();
  1397. hasTypeError := FALSE;
  1398. IF isGeneralOperator THEN
  1399. (* ARRAY [?] OF <Element> *)
  1400. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1401. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1402. IF ~((mathArrayType.arrayBase.resolved = elementType.resolved) & (mathArrayType.form = SyntaxTree.Tensor)) THEN
  1403. hasTypeError := TRUE
  1404. END
  1405. ELSE
  1406. hasTypeError := TRUE
  1407. END
  1408. ELSE
  1409. IF rangeCount = 0 THEN
  1410. (* <Element> *)
  1411. IF dataType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1412. ELSE
  1413. (* ARRAY [*, *, ..., *] OF <Element> *)
  1414. IF dataType.resolved IS SyntaxTree.MathArrayType THEN
  1415. mathArrayType := dataType.resolved(SyntaxTree.MathArrayType);
  1416. IF mathArrayType.IsFullyDynamic() THEN
  1417. IF mathArrayType.Dimensionality() = rangeCount THEN
  1418. otherElementType := mathArrayType.ElementType();
  1419. IF otherElementType.resolved # elementType.resolved THEN hasTypeError := TRUE END
  1420. ELSE
  1421. hasTypeError := TRUE
  1422. END
  1423. ELSE
  1424. hasTypeError := TRUE
  1425. END
  1426. ELSE
  1427. hasTypeError := TRUE
  1428. END
  1429. END
  1430. END;
  1431. IF hasTypeError THEN
  1432. IF isReadOperator THEN
  1433. Error(operator.position, Diagnostics.Invalid, "return type does not match")
  1434. ELSE
  1435. Error(parameter.position, Diagnostics.Invalid, "type of last parameter does not match")
  1436. END;
  1437. RETURN FALSE
  1438. END;
  1439. (* export symbol automatically *)
  1440. operator.SetAccess(SyntaxTree.Public + SyntaxTree.Protected + SyntaxTree.Internal);
  1441. RETURN TRUE
  1442. END CheckIndexOperator;
  1443. (** resolve all pending types (late resolving).
  1444. - type fixes are resolved at the end of the declaration phase
  1445. - type fixes may imply new type fixes that are also entered at the end of the list
  1446. **)
  1447. PROCEDURE FixTypes;
  1448. VAR p: ANY; prevScope: SyntaxTree.Scope;
  1449. BEGIN
  1450. prevScope := currentScope;
  1451. p := typeFixes.Get(currentScope);
  1452. WHILE p # NIL DO
  1453. ASSERT(currentScope # NIL);
  1454. ASSERT(p IS SyntaxTree.Type);
  1455. IF p IS SyntaxTree.PointerType THEN
  1456. FixPointerType(p(SyntaxTree.PointerType))
  1457. ELSIF p IS SyntaxTree.ProcedureType THEN
  1458. FixProcedureType(p(SyntaxTree.ProcedureType))
  1459. ELSE
  1460. HALT(100);
  1461. END;
  1462. p := typeFixes.Get(currentScope);
  1463. END;
  1464. currentScope :=prevScope;
  1465. END FixTypes;
  1466. (**
  1467. resolve type x
  1468. - if x is nil then return nil
  1469. - if x cannot be resolved then the result is invalidType else the result is x
  1470. - the resolved type is entered into x.resolved
  1471. **)
  1472. PROCEDURE ResolveType(x: SyntaxTree.Type): SyntaxTree.Type;
  1473. VAR prev,resolved: SyntaxTree.Type;
  1474. BEGIN
  1475. prev := resolvedType;
  1476. resolvedType := SyntaxTree.invalidType;
  1477. IF x = NIL THEN resolvedType := NIL
  1478. ELSE x.Accept(SELF); ASSERT(resolvedType # NIL); (* in error cases it must be invalidType *)
  1479. END;
  1480. resolved := resolvedType;
  1481. resolvedType := prev;
  1482. ASSERT((resolved = NIL) OR (resolved.resolved # NIL));
  1483. RETURN resolved
  1484. END ResolveType;
  1485. (*** compatibility rules ***)
  1486. (**
  1487. return a regular type: if type is invalid, NIL, importType or typeDeclarationType then return invalidType else return type
  1488. **)
  1489. PROCEDURE RegularType(position: LONGINT; type: SyntaxTree.Type): SyntaxTree.Type;
  1490. VAR result: SyntaxTree.Type;
  1491. BEGIN
  1492. result := SyntaxTree.invalidType;
  1493. IF type = NIL THEN Error(position,Diagnostics.Invalid,"expression of type NIL");
  1494. ELSIF type = SyntaxTree.invalidType THEN (* error already handled *)
  1495. ELSIF type.resolved = SyntaxTree.importType THEN Error(position,Diagnostics.Invalid,"expression is an import");
  1496. ELSIF type.resolved = SyntaxTree.typeDeclarationType THEN Error(position,Diagnostics.Invalid,"expression is a type");
  1497. ELSE result := type.resolved
  1498. END;
  1499. RETURN result
  1500. END RegularType;
  1501. (** returns signature compatibility of procedure types this and to
  1502. - if not compatible then error is reported
  1503. - compatibility means type equality
  1504. **)
  1505. PROCEDURE SignatureCompatible(position: LONGINT; this, to: SyntaxTree.ProcedureType): BOOLEAN;
  1506. VAR result: BOOLEAN;
  1507. BEGIN
  1508. result := SameType(to,this);
  1509. IF ~result THEN
  1510. Error(position,Diagnostics.Invalid,"signature incompatible");
  1511. IF VerboseErrorMessage THEN
  1512. Printout.Info("this",this);
  1513. Printout.Info("to",to);
  1514. END;
  1515. ELSIF (to(SyntaxTree.ProcedureType).isRealtime) & ~(this(SyntaxTree.ProcedureType).isRealtime) THEN
  1516. Error(position,Diagnostics.Invalid,"signature incompatible: realtime flag must be inherited");
  1517. END;
  1518. RETURN result
  1519. END SignatureCompatible;
  1520. (** check parameter compatibility for expressions of the form P(actual) where P = PROCEDURE([VAR|CONST] formal)
  1521. - for var parameters compatibility means same type except for
  1522. - formal is of open array of system byte
  1523. - formal is of record type
  1524. - formal is of open array type
  1525. - formal is of open math array type
  1526. - for value parameters compatibllity means assignment compatibility except for
  1527. - formal is of open array type
  1528. if compatible the return true else report error and return false
  1529. **)
  1530. PROCEDURE ParameterCompatible(formal: SyntaxTree.Parameter; actual: SyntaxTree.Expression): BOOLEAN;
  1531. VAR formalType, actualType: SyntaxTree.Type; result,error: BOOLEAN;
  1532. BEGIN
  1533. formalType := RegularType(formal.position,formal.type);
  1534. actualType := RegularType(actual.position,actual.type);
  1535. error := FALSE;
  1536. IF actualType = SyntaxTree.invalidType THEN (* error already handled *)
  1537. ELSIF (formal.kind = SyntaxTree.VarParameter) THEN
  1538. IF (actual IS SyntaxTree.SymbolDesignator) & (actual(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable) THEN
  1539. actual(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Variable).UsedAsReference;
  1540. END;
  1541. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & (actualType IS SyntaxTree.NilType) THEN
  1542. result := TRUE; (* special rule for WINAPI parameters, needed to be able to pass NIL address to var parameters *)
  1543. ELSIF ~IsVariable(actual) THEN
  1544. result := FALSE; error := TRUE;
  1545. IF actual IS SyntaxTree.ProcedureCallDesignator THEN
  1546. Error(actual.position,Diagnostics.Invalid,"not a variable: no operator for writing");
  1547. ELSE
  1548. Error(actual.position,Diagnostics.Invalid,"is not a variable");
  1549. END;
  1550. IF VerboseErrorMessage THEN
  1551. Printout.Info("actual",actual);
  1552. Printout.Info("formal",formal);
  1553. END;
  1554. ELSIF (formalType IS SyntaxTree.ByteType) OR (formalType IS SyntaxTree.RecordType) & (~formalType(SyntaxTree.RecordType).isObject) THEN
  1555. result := CompatibleTo(system,actualType,formalType);
  1556. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1557. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1558. ELSIF (formalType IS SyntaxTree.MathArrayType) THEN
  1559. IF IsArrayStructuredObjectType(actualType) THEN
  1560. actualType := MathArrayStructureOfType(actualType)
  1561. END;
  1562. result := MathArrayCompatible(formalType(SyntaxTree.MathArrayType),actualType);
  1563. IF result & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1564. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1565. END;
  1566. IF result & (actualType(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (formalType(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  1567. Error(actual.position,Diagnostics.Invalid,"incompatible tensor (use a range expression)");
  1568. END;
  1569. ELSE
  1570. result := SameType(actualType,formalType)
  1571. END
  1572. ELSE
  1573. IF (formalType IS SyntaxTree.CharacterType) & (actualType IS SyntaxTree.StringType) & (actualType(SyntaxTree.StringType).length = 2) THEN
  1574. actualType := system.characterType;
  1575. END;
  1576. IF (formal.ownerType(SyntaxTree.ProcedureType).callingConvention = SyntaxTree.WinAPICallingConvention) & ((actualType IS SyntaxTree.NilType) OR (actualType IS SyntaxTree.AnyType)) THEN
  1577. result := TRUE; (* special rule for WINAPI parameters *)
  1578. ELSIF (formalType IS SyntaxTree.ArrayType) & (formalType(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  1579. result := OpenArrayCompatible(formalType(SyntaxTree.ArrayType),actualType);
  1580. ELSE
  1581. result := CompatibleTo(system,actualType,formalType);
  1582. IF result & (formalType IS SyntaxTree.MathArrayType) & (formalType(SyntaxTree.MathArrayType).form = SyntaxTree.Static) & (actualType(SyntaxTree.MathArrayType).form # SyntaxTree.Static) THEN
  1583. Error(actual.position,Diagnostics.Invalid,"incompatible non-static actual type");
  1584. END;
  1585. END;
  1586. END;
  1587. IF ~result & ~error THEN
  1588. Error(actual.position,Diagnostics.Invalid,"incompatible parameter");
  1589. IF VerboseErrorMessage THEN
  1590. Printout.Info("actual",actual);
  1591. Printout.Info("formal",formal);
  1592. END;
  1593. END;
  1594. RETURN result
  1595. END ParameterCompatible;
  1596. (** check compatibility for expressions of the form left := right
  1597. - if compatible then return true else error report and return false
  1598. - check if left is variable
  1599. - check compatiblity
  1600. **)
  1601. PROCEDURE AssignmentCompatible(left: SyntaxTree.Designator; right: SyntaxTree.Expression): BOOLEAN;
  1602. VAR leftType,rightType: SyntaxTree.Type; VAR result: BOOLEAN;
  1603. BEGIN
  1604. result := FALSE;
  1605. leftType := RegularType(left.position,left.type);
  1606. rightType := RegularType(right.position,right.type);
  1607. IF (leftType IS SyntaxTree.CharacterType) & (rightType IS SyntaxTree.StringType) & (rightType(SyntaxTree.StringType).length = 2) THEN
  1608. rightType := system.characterType; (* conversion character "x" -> string "x" *)
  1609. END;
  1610. (* special rule: a type is assignment compatible to an ASOT if it is assignment compatible to its structure *)
  1611. IF IsArrayStructuredObjectType(leftType) THEN leftType := MathArrayStructureOfType(leftType) END;
  1612. IF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN
  1613. (* error already handled *)
  1614. result := TRUE;
  1615. ELSIF ~IsVariable(left) THEN
  1616. Error(left.position,Diagnostics.Invalid,"is not a variable");
  1617. IF VerboseErrorMessage THEN
  1618. Printout.Info("left",left);
  1619. Printout.Info("right",right);
  1620. END;
  1621. ELSIF (leftType IS SyntaxTree.AddressType) & IsAddressValue(right) THEN
  1622. result := TRUE;
  1623. ELSIF IsUnsignedIntegerType(leftType) & IsUnsignedValue(right, leftType.sizeInBits) THEN
  1624. result := TRUE
  1625. ELSIF ~CompatibleTo(system,rightType,leftType) THEN
  1626. Error(left.position,Diagnostics.Invalid,"incompatible assignment");
  1627. IF VerboseErrorMessage THEN
  1628. Printout.Info("left",left);
  1629. Printout.Info("right",right);
  1630. END;
  1631. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) &
  1632. (right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).scope IS SyntaxTree.ProcedureScope) THEN
  1633. Error(right.position,Diagnostics.Invalid,"forbidden assignment of a nested procedure");
  1634. ELSE
  1635. result := TRUE
  1636. END;
  1637. RETURN result
  1638. END AssignmentCompatible;
  1639. (*** values ***)
  1640. (** check and resolve integer value **)
  1641. PROCEDURE VisitIntegerValue(value: SyntaxTree.IntegerValue);
  1642. VAR hugeint: HUGEINT;
  1643. BEGIN
  1644. hugeint := value(SyntaxTree.IntegerValue).hvalue;
  1645. value.SetType(Global.GetIntegerType(system,hugeint));
  1646. resolvedExpression := value
  1647. END VisitIntegerValue;
  1648. (** check and resolve real value **)
  1649. PROCEDURE VisitRealValue(value: SyntaxTree.RealValue);
  1650. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1651. BEGIN
  1652. subtype := value(SyntaxTree.RealValue).subtype;
  1653. IF subtype = Scanner.Real THEN
  1654. type := system.realType
  1655. ELSIF subtype = Scanner.Longreal THEN
  1656. type := system.longrealType
  1657. ELSE
  1658. HALT(100)
  1659. END;
  1660. value.SetType(type);
  1661. resolvedExpression := value
  1662. END VisitRealValue;
  1663. (** check and resolve complex value **)
  1664. PROCEDURE VisitComplexValue(value: SyntaxTree.ComplexValue);
  1665. VAR subtype: LONGINT; type: SyntaxTree.Type;
  1666. BEGIN
  1667. subtype := value(SyntaxTree.ComplexValue).subtype;
  1668. IF subtype = Scanner.Real THEN
  1669. type := system.complexType
  1670. ELSIF subtype = Scanner.Longreal THEN
  1671. type := system.longcomplexType
  1672. ELSE
  1673. HALT(100)
  1674. END;
  1675. value.SetType(type);
  1676. resolvedExpression := value
  1677. END VisitComplexValue;
  1678. (** check and resolve set value **)
  1679. PROCEDURE VisitSetValue(value: SyntaxTree.SetValue);
  1680. BEGIN
  1681. value.SetType(system.setType);
  1682. resolvedExpression := value
  1683. END VisitSetValue;
  1684. (** check and resolve set value **)
  1685. PROCEDURE VisitMathArrayValue(value: SyntaxTree.MathArrayValue);
  1686. BEGIN
  1687. value.SetType(SyntaxTree.invalidType);
  1688. resolvedExpression := value
  1689. END VisitMathArrayValue;
  1690. (** check and resolve boolean value **)
  1691. PROCEDURE VisitBooleanValue(value: SyntaxTree.BooleanValue);
  1692. BEGIN
  1693. value.SetType(system.booleanType);
  1694. resolvedExpression := value
  1695. END VisitBooleanValue;
  1696. (** check and resolve string value **)
  1697. PROCEDURE VisitStringValue(value: SyntaxTree.StringValue);
  1698. BEGIN
  1699. value.SetType(ResolveType(SyntaxTree.NewStringType(value.position,system.characterType,value.length)));
  1700. resolvedExpression := value
  1701. END VisitStringValue;
  1702. (** check and resolve character value **)
  1703. PROCEDURE VisitCharacterValue(value: SyntaxTree.CharacterValue);
  1704. BEGIN
  1705. value.SetType(system.characterType);
  1706. resolvedExpression := value
  1707. END VisitCharacterValue;
  1708. (** check and resolve nil value **)
  1709. PROCEDURE VisitNilValue(value: SyntaxTree.NilValue);
  1710. BEGIN
  1711. value.SetType(system.nilType);
  1712. resolvedExpression := value
  1713. END VisitNilValue;
  1714. (** check and resolve enumerator value **)
  1715. PROCEDURE VisitEnumerationValue(value: SyntaxTree.EnumerationValue);
  1716. BEGIN
  1717. value.SetType(currentScope(SyntaxTree.EnumerationScope).ownerEnumeration);
  1718. ASSERT(value.type # NIL);
  1719. resolvedExpression := value
  1720. END VisitEnumerationValue;
  1721. (*** expressions ***)
  1722. (** check and resolve a Set expression of the form {Expression, Expression, ...}
  1723. - check all elements on integer type
  1724. - if element range is constant, then check lower and upper bound
  1725. - if all elements constant then return constant set value else return set expression (via global variable resolvedExpression)
  1726. if an error occurs then report error and return invalidExpression
  1727. **)
  1728. PROCEDURE VisitSet(set: SyntaxTree.Set);
  1729. VAR
  1730. i: LONGINT;
  1731. element: SyntaxTree.Expression;
  1732. constant: BOOLEAN;
  1733. elements: SyntaxTree.ExpressionList;
  1734. s: SET;
  1735. result: SyntaxTree.Expression;
  1736. value: SyntaxTree.Value;
  1737. PROCEDURE CheckElement(element: SyntaxTree.Expression): SyntaxTree.Expression;
  1738. VAR
  1739. left, right: SyntaxTree.Expression;
  1740. elementResult: SyntaxTree.Expression;
  1741. leftInteger, rightInteger, temp: LONGINT;
  1742. BEGIN
  1743. (* set context of range *)
  1744. IF element IS SyntaxTree.RangeExpression THEN
  1745. element(SyntaxTree.RangeExpression).SetContext(SyntaxTree.SetElement)
  1746. END;
  1747. elementResult := ResolveExpression(element); (* implies checking of subexpressions in binary expressions *)
  1748. IF elementResult = SyntaxTree.invalidExpression THEN
  1749. (* error already reported *)
  1750. constant := FALSE
  1751. ELSIF elementResult IS SyntaxTree.RangeExpression THEN
  1752. (* the element is a range expression *)
  1753. (* extract left and right hand side of range *)
  1754. left := elementResult(SyntaxTree.RangeExpression).first;
  1755. right := elementResult(SyntaxTree.RangeExpression).last;
  1756. (* guaranteed by VisitRangeExpression: *)
  1757. ASSERT((left # NIL) & (right # NIL));
  1758. ASSERT(system.longintType.SameType(left.type.resolved) & system.longintType.SameType(right.type.resolved));
  1759. ELSE
  1760. (* the element is not a range expression *)
  1761. (* check type and add conversion if needed *)
  1762. IF IsSizeType(elementResult.type.resolved, system.addressSize) THEN
  1763. elementResult := NewConversion(elementResult.position, elementResult, system.sizeType, NIL)
  1764. ELSE
  1765. Error(elementResult.position, Diagnostics.Invalid, "non integer element in set");
  1766. elementResult := SyntaxTree.invalidExpression;
  1767. constant := FALSE
  1768. END;
  1769. left := elementResult;
  1770. right := elementResult
  1771. END;
  1772. IF elementResult # SyntaxTree.invalidExpression THEN
  1773. IF IsIntegerValue(left,leftInteger) & IsIntegerValue(right,rightInteger) THEN
  1774. IF (leftInteger<0) OR (leftInteger >= system.setType.sizeInBits) THEN
  1775. Error(left.position,Diagnostics.Invalid,"not allowed set integer value");
  1776. IF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1777. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1778. END
  1779. ELSIF (rightInteger<0) OR (rightInteger >= system.setType.sizeInBits) THEN
  1780. Error(right.position,Diagnostics.Invalid,"not allowed set integer value");
  1781. ELSE
  1782. IF (leftInteger > MAX(SET)) OR (rightInteger <0) THEN
  1783. s := {};
  1784. ELSE
  1785. IF rightInteger > MAX(SET) THEN rightInteger := MAX(SET) END;
  1786. IF leftInteger < 0 THEN leftInteger := 0 END;
  1787. (*!!!!!!!!! this is a hack !!!!!!! *)
  1788. (*! in case of MAX(SET) =31 and --bits=64 some kind of sign extension
  1789. extends the range x..31 to x..63 !!!!!! *)
  1790. s := s + {leftInteger..rightInteger};
  1791. END;
  1792. END;
  1793. ELSE
  1794. constant := FALSE;
  1795. END
  1796. END;
  1797. RETURN elementResult
  1798. END CheckElement;
  1799. BEGIN
  1800. result := set; constant := TRUE; s := {}; elements := set.elements;
  1801. IF elements # NIL THEN
  1802. FOR i := 0 TO elements.Length()-1 DO
  1803. element := elements.GetExpression(i);
  1804. element := CheckElement(element);
  1805. IF element = SyntaxTree.invalidExpression THEN
  1806. result := SyntaxTree.invalidExpression
  1807. END;
  1808. elements.SetExpression(i,element);
  1809. END;
  1810. END;
  1811. IF constant THEN
  1812. value := SyntaxTree.NewSetValue(set.position,s);
  1813. value.SetType(system.setType);
  1814. result.SetResolved(value);
  1815. END;
  1816. (* optimization possible
  1817. convert {a,b,1,2,3,4,c,d} into {a,b,c,d} + {1,2,3,4}
  1818. left this to the programmer...
  1819. *)
  1820. result.SetType(system.setType);
  1821. resolvedExpression := result;
  1822. END VisitSet;
  1823. (*
  1824. old variant: quite generic but needs better conversion handling, do this?
  1825. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1826. VAR type: SyntaxTree.Type; position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; isValue: BOOLEAN;
  1827. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.MathArrayType;
  1828. BEGIN
  1829. type := NIL;
  1830. numberElements := x.elements.Length();
  1831. FOR i := 0 TO numberElements-1 DO
  1832. expression := x.elements.GetExpression(i);
  1833. position := expression.position;
  1834. expression := ResolveExpression(x.elements.GetExpression(i));
  1835. x.elements.SetExpression(i,de);
  1836. IF type = NIL THEN
  1837. type := expression.type;
  1838. ELSIF CompatibleTo(system,expression.type,type) THEN
  1839. (* ok *)
  1840. ELSIF CompatibleTo(system,type,expression.type) THEN
  1841. type := expression.type
  1842. ELSE
  1843. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1844. type := SyntaxTree.invalidType;
  1845. END;
  1846. END;
  1847. isValue := TRUE;
  1848. FOR i := 0 TO numberElements-1 DO
  1849. expression := NewConversion(position,x.elements.GetExpression(i),type);
  1850. x.elements.SetExpression(i,expression);
  1851. isValue := isValue & (expression.resolved # NIL);
  1852. END;
  1853. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1854. arrayType.SetArrayBase(type);
  1855. arrayType.SetLength(Global.NewIntegerValue(system,NewIntegerValue(system,rElements));
  1856. IF isValue THEN
  1857. value := SyntaxTree.NewMathArrayValue(position);
  1858. value.SetElements(x.elements);
  1859. x.SetResolved(value);
  1860. END;
  1861. x.SetType(arrayType);
  1862. resolvedExpression := x;
  1863. END VisitMathArrayExpression;
  1864. *)
  1865. PROCEDURE VisitMathArrayExpression(x: SyntaxTree.MathArrayExpression);
  1866. VAR type: SyntaxTree.Type; isValue: BOOLEAN;
  1867. value: SyntaxTree.MathArrayValue; arrayType: SyntaxTree.Type;
  1868. PROCEDURE RecursivelyFindType(x: SyntaxTree.MathArrayExpression);
  1869. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1870. BEGIN
  1871. numberElements := x.elements.Length();
  1872. FOR i := 0 TO numberElements-1 DO
  1873. expression := x.elements.GetExpression(i);
  1874. IF expression IS SyntaxTree.MathArrayExpression THEN
  1875. RecursivelyFindType(expression(SyntaxTree.MathArrayExpression))
  1876. ELSE
  1877. position := expression.position;
  1878. expression := ResolveExpression(x.elements.GetExpression(i));
  1879. x.elements.SetExpression(i,expression);
  1880. IF type = NIL THEN
  1881. type := expression.type;
  1882. ELSIF CompatibleTo(system,expression.type,type) THEN
  1883. (* ok *)
  1884. ELSIF CompatibleTo(system,type,expression.type) THEN
  1885. type := expression.type
  1886. ELSE
  1887. Error(expression.position,Diagnostics.Invalid, "incompatible element types");
  1888. type := SyntaxTree.invalidType;
  1889. END;
  1890. END;
  1891. END;
  1892. END RecursivelyFindType;
  1893. PROCEDURE RecursivelySetExpression(x: SyntaxTree.MathArrayExpression);
  1894. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression;
  1895. BEGIN
  1896. numberElements := x.elements.Length();
  1897. FOR i := 0 TO numberElements-1 DO
  1898. expression := x.elements.GetExpression(i);
  1899. IF expression IS SyntaxTree.MathArrayExpression THEN
  1900. RecursivelySetExpression(expression(SyntaxTree.MathArrayExpression));
  1901. ELSE
  1902. position := expression.position;
  1903. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  1904. x.elements.SetExpression(i,expression);
  1905. isValue := isValue & (expression.resolved # NIL);
  1906. END;
  1907. END;
  1908. END RecursivelySetExpression;
  1909. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  1910. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  1911. arrayType: SyntaxTree.MathArrayType;
  1912. BEGIN
  1913. numberElements := x.elements.Length();
  1914. baseType := NIL;
  1915. gsize := 0;
  1916. FOR i := 0 TO numberElements-1 DO
  1917. expression := x.elements.GetExpression(i);
  1918. IF expression IS SyntaxTree.MathArrayExpression THEN
  1919. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  1920. IF i=0 THEN
  1921. gsize := size;
  1922. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  1923. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1924. ELSE expression.SetType(baseType)
  1925. END;
  1926. ELSIF baseType = NIL THEN baseType := type;
  1927. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  1928. END;
  1929. END;
  1930. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  1931. arrayType.SetArrayBase(baseType);
  1932. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  1933. RETURN ResolveType(arrayType);
  1934. END RecursivelySetType;
  1935. BEGIN
  1936. type := NIL;
  1937. RecursivelyFindType(x);
  1938. isValue := TRUE;
  1939. RecursivelySetExpression(x);
  1940. arrayType := RecursivelySetType(x);
  1941. x.SetType(arrayType);
  1942. IF isValue THEN
  1943. value := SyntaxTree.NewMathArrayValue(x.position);
  1944. value.SetArray(x);
  1945. x.SetResolved(value);
  1946. value.SetType(arrayType);
  1947. END;
  1948. x.SetType(arrayType);
  1949. resolvedExpression := x;
  1950. END VisitMathArrayExpression;
  1951. (** check and resolve unary expression **)
  1952. PROCEDURE VisitUnaryExpression(unaryExpression: SyntaxTree.UnaryExpression);
  1953. VAR
  1954. left: SyntaxTree.Expression;
  1955. int: HUGEINT; real, imaginary: LONGREAL; set: SET; operator: LONGINT;
  1956. bool: BOOLEAN;
  1957. result: SyntaxTree.Expression; type: SyntaxTree.Type; operatorCall: SyntaxTree.Expression;
  1958. value: SyntaxTree.Value;
  1959. BEGIN
  1960. type := SyntaxTree.invalidType;
  1961. left := ResolveExpression(unaryExpression.left);
  1962. unaryExpression.SetLeft(left);
  1963. operator := unaryExpression.operator;
  1964. result := unaryExpression;
  1965. IF ~system.operatorDefined[operator] THEN
  1966. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  1967. RETURN
  1968. ELSIF left.type = NIL THEN
  1969. Error(left.position,Diagnostics.Invalid,"Invalid Nil Argument in Unary Expression");
  1970. resolvedExpression := SyntaxTree.invalidExpression;
  1971. RETURN
  1972. ELSIF left = SyntaxTree.invalidExpression THEN (* error already handled *)
  1973. RETURN
  1974. END;
  1975. IF ~(left.type.resolved IS SyntaxTree.BasicType) OR (left.type.resolved IS SyntaxTree.ComplexType) THEN
  1976. operatorCall := NewOperatorCall(unaryExpression.position, operator,left,NIL,NIL);
  1977. END;
  1978. IF operatorCall # NIL THEN
  1979. result := operatorCall;
  1980. type := operatorCall.type;
  1981. (* admissible operators
  1982. Minus number, set
  1983. Not boolean
  1984. *)
  1985. ELSE
  1986. CASE unaryExpression.operator OF
  1987. |Scanner.Minus:
  1988. IF IsIntegerType(left.type.resolved) THEN
  1989. IF left.resolved # NIL THEN
  1990. int := -left.resolved(SyntaxTree.IntegerValue).hvalue;
  1991. value := SyntaxTree.NewIntegerValue(unaryExpression.position,int);
  1992. result.SetResolved(value);
  1993. type := Global.GetIntegerType(system,int);
  1994. value.SetType(type);
  1995. ELSE
  1996. type := left.type
  1997. END
  1998. ELSIF left.type.resolved IS SyntaxTree.FloatType THEN
  1999. IF IsRealValue(left,real) THEN
  2000. value := SyntaxTree.NewRealValue(unaryExpression.position,-real);
  2001. result.SetResolved(value);
  2002. type := left.type;
  2003. value.SetType(type);
  2004. ELSE
  2005. type := left.type;
  2006. END;
  2007. ELSIF left.type.resolved IS SyntaxTree.SetType THEN
  2008. IF IsSetValue(left,set) THEN
  2009. value := SyntaxTree.NewSetValue(unaryExpression.position,-set);
  2010. result.SetResolved(value);
  2011. type := left.type;
  2012. value.SetType(type);
  2013. ELSE
  2014. type := left.type;
  2015. END;
  2016. ELSIF left.type.resolved IS SyntaxTree.ComplexType THEN
  2017. IF IsComplexValue(left, real, imaginary) THEN
  2018. value := SyntaxTree.NewComplexValue(unaryExpression.position,-real, -imaginary);
  2019. result.SetResolved(value);
  2020. type := left.type;
  2021. value.SetType(type);
  2022. value(SyntaxTree.ComplexValue).SetSubtype(left.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2023. ELSE
  2024. type := left.type;
  2025. END
  2026. ELSE
  2027. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2028. END;
  2029. |Scanner.Not:
  2030. IF left.type.resolved IS SyntaxTree.BooleanType THEN
  2031. IF IsBooleanValue(left,bool) THEN
  2032. value := SyntaxTree.NewBooleanValue(unaryExpression.position,~bool);
  2033. result.SetResolved(value);
  2034. type := system.booleanType;
  2035. value.SetType(type);
  2036. ELSE
  2037. type := system.booleanType;
  2038. END;
  2039. ELSE
  2040. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2041. END;
  2042. |Scanner.Plus:
  2043. IF (left.type.resolved IS SyntaxTree.NumberType) THEN
  2044. result := left; type := left.type;
  2045. ELSE
  2046. Error(left.position,Diagnostics.Invalid,"unary operator not applicable");
  2047. END;
  2048. (* ADDRESS OF *)
  2049. |Scanner.Address:
  2050. IF HasAddress(left) THEN
  2051. type := system.addressType;
  2052. ELSE
  2053. type := SyntaxTree.invalidType;
  2054. Error(left.position,Diagnostics.Invalid,"has no address");
  2055. Printout.Info("par", left);
  2056. END;
  2057. (* SIZE OF *)
  2058. |Scanner.Size:
  2059. IF (left.type = SyntaxTree.typeDeclarationType) THEN
  2060. type := left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  2061. int := system.SizeOf(type.resolved) DIV 8 (* in bytes *);
  2062. value := SyntaxTree.NewIntegerValue(left.position, int);
  2063. result.SetResolved(value);
  2064. type := Global.GetIntegerType(system,int);
  2065. value.SetType(type)
  2066. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  2067. ELSE
  2068. (* for variables, system sizeof could represent the physically occupied size
  2069. determined via the type descriptor, implement that ? *)
  2070. Error(left.position,Diagnostics.Invalid,"is not a type symbol");
  2071. END
  2072. (* ALIAS OF *)
  2073. |Scanner.Alias:
  2074. type := left.type.resolved;
  2075. IF ~(type IS SyntaxTree.MathArrayType) THEN
  2076. type := SyntaxTree.invalidType;
  2077. Error(left.position, Diagnostics.Invalid,"alias on non math array type");
  2078. END;
  2079. ELSE
  2080. Error(left.position,Diagnostics.Invalid,"unary operator not defined");
  2081. END;
  2082. END;
  2083. result.SetType(type);
  2084. resolvedExpression := result
  2085. END VisitUnaryExpression;
  2086. PROCEDURE MathArrayConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2087. VAR
  2088. result: SyntaxTree.Expression;
  2089. array: SyntaxTree.MathArrayExpression;
  2090. value: SyntaxTree.MathArrayValue;
  2091. isValue: BOOLEAN;
  2092. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  2093. BEGIN
  2094. type := type.resolved;
  2095. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  2096. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  2097. END;
  2098. WHILE (type # NIL) & (type IS SyntaxTree.ArrayType) DO
  2099. type := Resolved(type(SyntaxTree.ArrayType).arrayBase);
  2100. END;
  2101. RETURN type
  2102. END BaseType;
  2103. PROCEDURE RecursivelyConvert(x, to: SyntaxTree.MathArrayExpression);
  2104. VAR position,numberElements,i: LONGINT; expression: SyntaxTree.Expression; array: SyntaxTree.MathArrayExpression;
  2105. BEGIN
  2106. numberElements := x.elements.Length();
  2107. FOR i := 0 TO numberElements-1 DO
  2108. expression := x.elements.GetExpression(i);
  2109. IF expression IS SyntaxTree.MathArrayExpression THEN
  2110. array := SyntaxTree.NewMathArrayExpression(position);
  2111. RecursivelyConvert(expression(SyntaxTree.MathArrayExpression), array);
  2112. expression := array;
  2113. ELSE
  2114. position := expression.position;
  2115. expression := NewConversion(position,x.elements.GetExpression(i),type,NIL);
  2116. isValue := isValue & (expression.resolved # NIL);
  2117. END;
  2118. to.elements.AddExpression(expression);
  2119. END;
  2120. END RecursivelyConvert;
  2121. PROCEDURE RecursivelySetType(x: SyntaxTree.MathArrayExpression): SyntaxTree.Type;
  2122. VAR numberElements,i,size,gsize: LONGINT; baseType: SyntaxTree.Type;expression: SyntaxTree.Expression;
  2123. arrayType: SyntaxTree.MathArrayType;
  2124. BEGIN
  2125. numberElements := x.elements.Length();
  2126. baseType := NIL;
  2127. gsize := 0;
  2128. FOR i := 0 TO numberElements-1 DO
  2129. expression := x.elements.GetExpression(i);
  2130. IF expression IS SyntaxTree.MathArrayExpression THEN
  2131. size := expression(SyntaxTree.MathArrayExpression).elements.Length();
  2132. IF i=0 THEN
  2133. gsize := size;
  2134. baseType := RecursivelySetType(expression(SyntaxTree.MathArrayExpression));
  2135. ELSIF (baseType = type) OR (gsize # size) THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2136. ELSE expression.SetType(baseType)
  2137. END;
  2138. ELSIF baseType = NIL THEN baseType := type;
  2139. ELSIF baseType # type THEN Error(expression.position,Diagnostics.Invalid, "invalid array dimensions");
  2140. END;
  2141. END;
  2142. arrayType := SyntaxTree.NewMathArrayType(x.position,NIL, SyntaxTree.Static);
  2143. arrayType.SetArrayBase(baseType);
  2144. arrayType.SetLength(Global.NewIntegerValue(system,x.position,numberElements));
  2145. RETURN ResolveType(arrayType);
  2146. END RecursivelySetType;
  2147. BEGIN
  2148. result := SyntaxTree.invalidExpression;
  2149. IF (BaseType(type)=NIL) OR (BaseType(expression.type.resolved).SameType(BaseType(type))) THEN
  2150. result := expression (* do not convert *)
  2151. ELSIF (expression.resolved # NIL) & (BaseType(type) IS SyntaxTree.BasicType) THEN (* compliance has already been checked *)
  2152. isValue := TRUE;
  2153. type := BaseType(type);
  2154. array := SyntaxTree.NewMathArrayExpression(expression.position);
  2155. RecursivelyConvert(expression(SyntaxTree.MathArrayValue).array(SyntaxTree.MathArrayExpression), array);
  2156. value := SyntaxTree.NewMathArrayValue(array.position);
  2157. value.SetArray(array);
  2158. value.SetType(RecursivelySetType(array));
  2159. result := value;
  2160. IF ~isValue THEN Error(position, Diagnostics.Invalid, "incompatible array conversion") END;
  2161. ELSE (* should the search for operators be restricted to the ArrayBase module here ? *)
  2162. result := NewOperatorCall(position,Global.Conversion,expression,NIL,type);
  2163. IF result = NIL THEN
  2164. result := SyntaxTree.invalidExpression;
  2165. Error(position,Diagnostics.Invalid,"incompatible conversion");
  2166. IF VerboseErrorMessage THEN
  2167. Printout.Info("expression",expression);
  2168. Printout.Info("type",type);
  2169. END;
  2170. END;
  2171. END;
  2172. RETURN result
  2173. END MathArrayConversion;
  2174. PROCEDURE ConvertValue(position: LONGINT; expression: SyntaxTree.Value; type: SyntaxTree.Type): SyntaxTree.Expression;
  2175. VAR result: SyntaxTree.Expression; int: HUGEINT; real, imaginary: LONGREAL; set: SET; char: CHAR; string: Scanner.StringType;
  2176. BEGIN
  2177. result := expression; type := type.resolved;
  2178. IF (expression IS SyntaxTree.IntegerValue) THEN
  2179. int := expression(SyntaxTree.IntegerValue).hvalue;
  2180. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2181. int := Global.ConvertSigned(int,system.SizeOf(type));
  2182. result := SyntaxTree.NewIntegerValue(position,int);
  2183. result.SetType(type);
  2184. ELSIF (type IS SyntaxTree.AddressType) OR IsUnsafePointer(type) THEN
  2185. int := Global.ConvertUnsigned(int,system.SizeOf(type));
  2186. result := SyntaxTree.NewIntegerValue(position,int);
  2187. result.SetType(type);
  2188. ELSIF (type IS SyntaxTree.FloatType) THEN
  2189. result := SyntaxTree.NewRealValue(expression.position,int);
  2190. result.SetType(type);
  2191. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2192. result := SyntaxTree.NewComplexValue(expression.position, int, 0);
  2193. result.SetType(type);
  2194. ELSIF (type IS SyntaxTree.SetType) THEN
  2195. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,int));
  2196. result.SetType(type);
  2197. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN
  2198. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,int));
  2199. result.SetType(type);
  2200. ELSIF (type IS SyntaxTree.EnumerationType) THEN
  2201. IF (int > MAX(LONGINT)) OR (int < MIN(LONGINT)) THEN
  2202. Error(position,Diagnostics.Invalid,"huge integer value incompatible to enumeration");
  2203. END;
  2204. result := SyntaxTree.NewEnumerationValue(expression.position,SHORT(int));
  2205. result.SetType(type);
  2206. ELSIF (type IS SyntaxTree.PortType) THEN
  2207. result := ConvertValue(position, expression, system.integerType);
  2208. ELSE
  2209. Error(position,Diagnostics.Invalid,"integer value cannot be converted");
  2210. result := SyntaxTree.invalidExpression;
  2211. IF VerboseErrorMessage THEN
  2212. Printout.Info("expression",expression);
  2213. Printout.Info("type",type);
  2214. END;
  2215. END;
  2216. ELSIF IsRealValue(expression,real) THEN
  2217. IF (type IS SyntaxTree.IntegerType) & (type.sizeInBits < 64) THEN
  2218. int := Global.ConvertSigned(ENTIER(real),system.SizeOf(type));
  2219. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2220. result.SetType(type);
  2221. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2222. int := ENTIERH(real);
  2223. result := SyntaxTree.NewIntegerValue(expression.position,int);
  2224. result.SetType(type);
  2225. ELSIF (type IS SyntaxTree.FloatType) THEN
  2226. result := SyntaxTree.NewRealValue(position,real);
  2227. result.SetType(type);
  2228. ELSIF (type IS SyntaxTree.ComplexType) THEN
  2229. result := SyntaxTree.NewComplexValue(expression.position, real, 0);
  2230. result.SetType(type);
  2231. result(SyntaxTree.ComplexValue).UpdateSubtype;
  2232. ELSIF (type IS SyntaxTree.PortType) THEN
  2233. result := ConvertValue(position, expression, system.integerType);
  2234. ELSE
  2235. Error(position,Diagnostics.Invalid,"real value cannot be converted");
  2236. result := SyntaxTree.invalidExpression;
  2237. END
  2238. ELSIF IsComplexValue(expression, real, imaginary) THEN
  2239. IF (type IS SyntaxTree.ComplexType) THEN
  2240. result := SyntaxTree.NewComplexValue(expression.position, real, imaginary);
  2241. result.SetType(type);
  2242. result(SyntaxTree.ComplexValue).SetSubtype(expression.resolved(SyntaxTree.ComplexValue).subtype) (* reuse subtype *)
  2243. ELSE
  2244. Error(position,Diagnostics.Invalid,"complex value cannot be converted");
  2245. result := SyntaxTree.invalidExpression;
  2246. END
  2247. ELSIF IsSetValue(expression,set) THEN
  2248. IF (type IS SyntaxTree.IntegerType) THEN
  2249. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,set));
  2250. result.SetType(type);
  2251. ELSIF (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) THEN (* for example: possible via ch = CHR(SYSTEM.VAL(LONGINT,set)) *)
  2252. result := SyntaxTree.NewCharacterValue(expression.position,SYSTEM.VAL(CHAR,set));
  2253. result.SetType(type);
  2254. ELSIF (type IS SyntaxTree.PortType) THEN
  2255. result := ConvertValue(position, expression, system.integerType);
  2256. ELSE
  2257. Error(position,Diagnostics.Invalid,"set value cannot be converted");
  2258. result := SyntaxTree.invalidExpression;
  2259. END;
  2260. ELSIF IsStringValue(expression,string) THEN
  2261. IF ((type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType)) & (string[1]=0X) THEN
  2262. result := SyntaxTree.NewCharacterValue(expression.position,string[0]);
  2263. result.SetType(type);
  2264. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN (* nothing to be done *)
  2265. ELSE
  2266. Error(position,Diagnostics.Invalid,"string value cannot be converted");
  2267. result := SyntaxTree.invalidExpression;
  2268. END;
  2269. ELSIF IsCharacterValue(expression,char) THEN
  2270. IF (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN
  2271. string[0] := char; string[1] := 0X;
  2272. type := SyntaxTree.NewStringType(InvalidPosition,system.characterType,2);
  2273. result := SyntaxTree.NewStringValue(expression.position,string);
  2274. result.SetType(type);
  2275. ELSIF (type IS SyntaxTree.ByteType) THEN
  2276. (* do not simply set the new type as this could invalidate types of constants *)
  2277. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2278. result.SetType(type)
  2279. ELSIF (type IS SyntaxTree.IntegerType) THEN
  2280. result := SyntaxTree.NewIntegerValue(expression.position,SYSTEM.VAL(LONGINT,char));
  2281. result.SetType(type);
  2282. ELSIF (type IS SyntaxTree.SetType) THEN
  2283. result := SyntaxTree.NewSetValue(expression.position,SYSTEM.VAL(SET,char));
  2284. result.SetType(type);
  2285. ELSIF (type IS SyntaxTree.CharacterType) THEN
  2286. result := SyntaxTree.NewCharacterValue(expression.position,char);
  2287. result.SetType(type);
  2288. ELSIF (type IS SyntaxTree.PortType) THEN
  2289. result := ConvertValue(position, expression, system.integerType);
  2290. ELSE
  2291. Error(position,Diagnostics.Invalid,"character value cannot be converted");
  2292. result := SyntaxTree.invalidExpression;
  2293. END;
  2294. ELSIF expression IS SyntaxTree.NilValue THEN
  2295. IF type IS SyntaxTree.AddressType THEN
  2296. result := SyntaxTree.NewIntegerValue(position,0);
  2297. result.SetType(type);
  2298. ELSE
  2299. result := expression;
  2300. END;
  2301. (* nothing to be done *)
  2302. ELSIF expression IS SyntaxTree.MathArrayValue THEN
  2303. result := MathArrayConversion(position, expression,type);
  2304. ELSIF expression IS SyntaxTree.EnumerationValue THEN
  2305. int := expression(SyntaxTree.EnumerationValue).value;
  2306. IF (type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.SizeType) THEN
  2307. int := Global.ConvertSigned(int,system.SizeOf(type));
  2308. result := SyntaxTree.NewIntegerValue(position,int);
  2309. result.SetType(type);
  2310. ELSE
  2311. result := expression;
  2312. END;
  2313. (* nothing to be done *)
  2314. ELSE
  2315. Error(position,Diagnostics.Invalid,"expression cannot be converted");
  2316. IF VerboseErrorMessage THEN
  2317. Printout.Info("expression",expression);
  2318. Printout.Info("type",type);
  2319. END;
  2320. result := SyntaxTree.invalidExpression;
  2321. END;
  2322. RETURN result
  2323. END ConvertValue;
  2324. (**
  2325. return a conversion of an expression to a given type
  2326. - if expression is already of same type then return expression
  2327. - if incompatible conversion then report error and return invalidExpression
  2328. **)
  2329. PROCEDURE NewConversion*(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type; reference: SyntaxTree.Expression): SyntaxTree.Expression;
  2330. VAR result: SyntaxTree.Expression; value: SyntaxTree.Expression; expressionList: SyntaxTree.ExpressionList; typeDeclaration: SyntaxTree.TypeDeclaration; typeSymbol: SyntaxTree.Designator;
  2331. BEGIN
  2332. type := type.resolved;
  2333. ASSERT(type # NIL); ASSERT(~(type IS SyntaxTree.QualifiedType));
  2334. result := expression;
  2335. IF expression = SyntaxTree.invalidExpression THEN (* error already handled *)
  2336. ELSIF expression = NIL THEN (* NIL expression *)
  2337. ELSIF expression.type = NIL THEN
  2338. Error(position,Diagnostics.Invalid,"expression of type NIL cannot be converted");
  2339. ELSIF expression.type.SameType(type) THEN (* nothing to be done ! *)
  2340. ELSIF IsPointerType(expression.type) & IsPointerType(type) THEN (* nothing to be done *)
  2341. ELSIF (expression.type.resolved IS SyntaxTree.AnyType) THEN (*! binary symbol file problem: ANY and OBJECT cannot be distinguished *)
  2342. ELSIF (expression.type.resolved IS SyntaxTree.ObjectType) & (type IS SyntaxTree.AnyType) THEN (*! binary symbol file problem *)
  2343. ELSIF expression.resolved # NIL THEN (* value *)
  2344. value := ConvertValue(position,expression.resolved(SyntaxTree.Value),type);
  2345. IF value IS SyntaxTree.Value THEN
  2346. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2347. result.SetResolved(value(SyntaxTree.Value));
  2348. result.SetType(value.type);
  2349. ELSE
  2350. result := value
  2351. END;
  2352. ELSIF (type IS SyntaxTree.ByteType) THEN (* do not convert *)
  2353. expressionList := SyntaxTree.NewExpressionList();
  2354. typeDeclaration := SyntaxTree.NewTypeDeclaration(expression.position,SyntaxTree.NewIdentifier("@byte"));
  2355. typeDeclaration.SetDeclaredType(type);
  2356. typeSymbol := SyntaxTree.NewSymbolDesignator(InvalidPosition,NIL,typeDeclaration);
  2357. typeSymbol.SetType(typeDeclaration.type);
  2358. expressionList.AddExpression(typeSymbol); (* type declaration symbol skipped *)
  2359. expressionList.AddExpression(expression);
  2360. result := SyntaxTree.NewBuiltinCallDesignator(expression.position,Global.systemVal,NIL,expressionList);
  2361. result.SetType(type);
  2362. ELSIF IsArrayStructuredObjectType(type) THEN
  2363. (* no type can be converted to an array-structured object type *)
  2364. HALT(100)
  2365. ELSIF (type IS SyntaxTree.MathArrayType) THEN
  2366. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  2367. result := MathArrayConversion(position, expression,type);
  2368. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  2369. expression := ConvertToMathArray(expression);
  2370. type := MathArrayStructureOfType(type);
  2371. result := MathArrayConversion(position, expression, type)
  2372. ELSE
  2373. Error(expression.position,Diagnostics.Invalid,"cannot convert non array type to array type")
  2374. END;
  2375. ELSIF (expression.type.resolved IS SyntaxTree.MathArrayType) THEN
  2376. IF (expression.type.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static)
  2377. OR ~(type IS SyntaxTree.ArrayType) THEN
  2378. Error(expression.position,Diagnostics.Invalid,"cannot convert array type to non-array type")
  2379. END;
  2380. ELSIF IsPointerType(type) & ~IsPointerType(expression.type.resolved) THEN
  2381. result := SyntaxTree.NewConversion(expression.position,expression,system.addressType,reference);
  2382. ELSIF ~(type IS SyntaxTree.BasicType) & ~(expression.type.resolved IS SyntaxTree.CharacterType) THEN
  2383. (*skip, no conversion*)
  2384. ELSIF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  2385. (* skip, no conversion *)
  2386. ELSE
  2387. ASSERT(~(type IS SyntaxTree.RangeType));
  2388. result := SyntaxTree.NewConversion(expression.position,expression,type,reference);
  2389. ASSERT(type # NIL);
  2390. END;
  2391. RETURN result
  2392. END NewConversion;
  2393. PROCEDURE CompatibleConversion(position: LONGINT; expression: SyntaxTree.Expression; type: SyntaxTree.Type): SyntaxTree.Expression;
  2394. BEGIN
  2395. IF CompatibleTo(system,expression.type, type) THEN
  2396. RETURN NewConversion(position, expression, type, NIL);
  2397. ELSE
  2398. Error(expression.position, Diagnostics.Invalid, "incompatible expression");
  2399. RETURN SyntaxTree.invalidExpression
  2400. END;
  2401. END CompatibleConversion;
  2402. (**
  2403. convert operands left and right to a type that both operands are compatible with, if no such type exists then report error
  2404. **)
  2405. PROCEDURE ConvertOperands(VAR left,right: SyntaxTree.Expression);
  2406. VAR leftType,rightType: SyntaxTree.Type;
  2407. BEGIN
  2408. IF left.type = NIL THEN Error(left.position,Diagnostics.Invalid,"no type")
  2409. ELSIF right.type= NIL THEN Error(right.position,Diagnostics.Invalid,"no type")
  2410. ELSIF (left = SyntaxTree.invalidExpression) OR (right = SyntaxTree.invalidExpression) THEN
  2411. (* error already handled *)
  2412. ELSE
  2413. leftType := left.type.resolved; rightType := right.type.resolved;
  2414. IF (leftType IS SyntaxTree.AddressType) & IsAddressExpression(right) THEN
  2415. right := NewConversion(right.position, right, leftType, NIL);
  2416. ELSIF (rightType IS SyntaxTree.AddressType) & IsAddressExpression(left) THEN
  2417. left := NewConversion(left.position,left,rightType,NIL);
  2418. ELSIF (leftType IS SyntaxTree.SizeType) & IsSizeExpression(right) THEN
  2419. right := NewConversion(right.position, right, leftType, NIL);
  2420. ELSIF (rightType IS SyntaxTree.SizeType) & IsSizeExpression(left) THEN
  2421. left := NewConversion(left.position,left,rightType,NIL);
  2422. ELSIF CompatibleTo(system,leftType,rightType) THEN
  2423. left := NewConversion(left.position,left,right.type.resolved,NIL);
  2424. ELSIF CompatibleTo(system,rightType,leftType) THEN
  2425. right := NewConversion(right.position,right,left.type.resolved,NIL);
  2426. ELSIF
  2427. (leftType IS SyntaxTree.ComplexType) & (rightType IS SyntaxTree.FloatType) OR
  2428. (leftType IS SyntaxTree.FloatType) & (rightType IS SyntaxTree.ComplexType) THEN
  2429. (* must be the case LONGREAL / COMPLEX ) *)
  2430. left := NewConversion(left.position, left, Global.Complex128, NIL);
  2431. right := NewConversion(right.position, right, Global.Complex128, NIL);
  2432. ELSE
  2433. Error(left.position,Diagnostics.Invalid,"incompatible operands");
  2434. END;
  2435. END;
  2436. END ConvertOperands;
  2437. (** find and return best operator matching to parameter list (nil, if none)
  2438. - search current module scope and all (directly or indirectly) imported modules for matching operator
  2439. - take operator with smalles distance, where signature distance is computed in procedure Distance
  2440. **)
  2441. PROCEDURE FindOperator*(system: Global.System; operator: LONGINT; actualParameters: SyntaxTree.ExpressionList; returnType: SyntaxTree.Type): SyntaxTree.Operator;
  2442. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; import: SyntaxTree.Import; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  2443. identifier: SyntaxTree.Identifier;
  2444. PROCEDURE FindInScope(scope: SyntaxTree.ModuleScope; access: SET);
  2445. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  2446. BEGIN
  2447. operator := scope.firstOperator;
  2448. WHILE(operator # NIL) DO
  2449. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  2450. procedureType := operator.type(SyntaxTree.ProcedureType);
  2451. distance := Distance(system, procedureType,actualParameters);
  2452. IF (distance < Infinity) THEN
  2453. IF returnType # NIL THEN
  2454. IF procedureType.returnType = NIL THEN
  2455. distance := Infinity
  2456. ELSE
  2457. i := TypeDistance(system,returnType,procedureType.returnType,TRUE);
  2458. IF i = Infinity THEN distance := Infinity ELSE INC(distance,i) END;
  2459. END;
  2460. END;
  2461. END;
  2462. (*
  2463. IF distance < Infinity THEN
  2464. TRACE(distance, operator);
  2465. Printout.Info("potential operator",operator);
  2466. ELSE
  2467. Printout.Info("operator not possible",operator);
  2468. END;
  2469. *)
  2470. IF distance < bestDistance THEN
  2471. bestDistance := distance;
  2472. bestOperator := operator;
  2473. END;
  2474. END;
  2475. operator := operator.nextOperator;
  2476. END;
  2477. (*
  2478. Printout.Info("taken operator",bestOperator);
  2479. *)
  2480. END FindInScope;
  2481. BEGIN
  2482. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  2483. identifier := Global.GetIdentifier(operator,currentScope.ownerModule.case);
  2484. FindInScope(currentScope.ownerModule.moduleScope,SyntaxTree.ReadOnly);
  2485. import := currentScope.ownerModule.moduleScope.firstImport;
  2486. WHILE (bestDistance > 0) & (import # NIL) DO
  2487. IF import.module # NIL THEN
  2488. identifier := Global.GetIdentifier(operator,import.module.case);
  2489. FindInScope(import.module.moduleScope,SyntaxTree.Public);
  2490. END;
  2491. import := import.nextImport;
  2492. END;
  2493. RETURN bestOperator
  2494. END FindOperator;
  2495. PROCEDURE SetCurrentScope*(scope: SyntaxTree.Scope);
  2496. BEGIN
  2497. currentScope := scope;
  2498. END SetCurrentScope;
  2499. (**
  2500. return a procedure call designator for the best matching operator procedure of the form "op"(leftExpression,rightExpression) (if any)
  2501. - handle LEN and DIM operator for array-structured object types
  2502. - find operator, if found then
  2503. - if in other module then add import designator
  2504. - create symbol designator for operator
  2505. - if error then return invalidExpression, if no operator then return NIL
  2506. **)
  2507. PROCEDURE NewOperatorCall*(position: LONGINT; op: LONGINT; leftExpression, rightExpression: SyntaxTree.Expression; resultType: SyntaxTree.Type): SyntaxTree.Expression;
  2508. VAR
  2509. operator: SyntaxTree.Operator;
  2510. import: SyntaxTree.Import;
  2511. expression, result: SyntaxTree.Expression;
  2512. designator: SyntaxTree.Designator;
  2513. actualParameters, tempList: SyntaxTree.ExpressionList;
  2514. recordType: SyntaxTree.RecordType;
  2515. castReturnType : SyntaxTree.MathArrayType;
  2516. BEGIN
  2517. IF (leftExpression = SyntaxTree.invalidExpression) OR (rightExpression = SyntaxTree.invalidExpression) THEN
  2518. result := SyntaxTree.invalidExpression
  2519. ELSIF leftExpression = NIL THEN
  2520. result := NIL
  2521. ELSIF IsArrayStructuredObjectType(leftExpression.type) & ((op = Global.Len) OR (op = Global.Dim)) THEN
  2522. (* LEN or DIM operator on array-structured object type *)
  2523. ASSERT(leftExpression.type.resolved IS SyntaxTree.PointerType);
  2524. recordType := leftExpression.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  2525. IF recordType.arrayAccessOperators.len = NIL THEN
  2526. Error(position, Diagnostics.Invalid, "call of undeclared LEN operator");
  2527. result := SyntaxTree.invalidExpression
  2528. ELSE
  2529. ASSERT(leftExpression IS SyntaxTree.Designator);
  2530. designator := leftExpression(SyntaxTree.Designator);
  2531. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(position, designator), recordType.arrayAccessOperators.len);
  2532. ASSERT(expression IS SyntaxTree.Designator);
  2533. designator := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), SyntaxTree.NewExpressionList());
  2534. IF (op = Global.Len) & (rightExpression = NIL) THEN
  2535. (* LEN(OBJECT) -> OBJECT^."LEN"() *)
  2536. result := designator
  2537. ELSIF (op = Global.Len) & (rightExpression # NIL) & (rightExpression.type.resolved IS SyntaxTree.IntegerType) THEN
  2538. (* LEN(OBJECT, LONGINT) -> OBJECT^."LEN"()[LONGINT] *)
  2539. tempList := SyntaxTree.NewExpressionList();
  2540. tempList.AddExpression(rightExpression);
  2541. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, designator, tempList))
  2542. ELSIF (op = Global.Dim) & (rightExpression = NIL) THEN
  2543. (* DIM(OBJECT) -> LEN(OBJECT^."LEN"(), 0) *)
  2544. tempList := SyntaxTree.NewExpressionList();
  2545. tempList.AddExpression(designator);
  2546. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0));
  2547. designator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.GetIdentifier(Global.Len, module.case));
  2548. result := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, designator, tempList))
  2549. END
  2550. END;
  2551. ELSE
  2552. (* import OCArrayBase if needed *)
  2553. IF ~arrayBaseImported THEN
  2554. IF (leftExpression # NIL) & IsComplexType(leftExpression.type) THEN
  2555. (* operators on complex numbers *)
  2556. ImportModule(Global.ArrayBaseName,position);
  2557. arrayBaseImported := TRUE;
  2558. ELSIF (leftExpression # NIL) & IsMathArrayType(leftExpression.type) OR (rightExpression # NIL) & IsMathArrayType(rightExpression.type) THEN
  2559. IF op = Global.Dim THEN
  2560. (* not existing in OCArrayBase *)
  2561. ELSIF (op = Global.Len) & (rightExpression # NIL) THEN
  2562. (* not existing in OCArrayBase *)
  2563. ELSE
  2564. ImportModule(Global.ArrayBaseName,position);
  2565. arrayBaseImported := TRUE;
  2566. END
  2567. ELSIF (leftExpression # NIL) & IsArrayStructuredObjectType(leftExpression.type) OR (rightExpression # NIL) & IsArrayStructuredObjectType(rightExpression.type) THEN
  2568. ImportModule(Global.ArrayBaseName,position);
  2569. arrayBaseImported := TRUE
  2570. END;
  2571. IF (op = Global.Len) & (leftExpression # NIL) & IsRangeType(leftExpression.type) & (rightExpression = NIL) THEN
  2572. (* LEN(RANGE) *)
  2573. ImportModule(Global.ArrayBaseName,position);
  2574. arrayBaseImported := TRUE;
  2575. END;
  2576. END;
  2577. actualParameters := SyntaxTree.NewExpressionList();
  2578. actualParameters.AddExpression(leftExpression);
  2579. IF rightExpression # NIL THEN
  2580. actualParameters.AddExpression(rightExpression)
  2581. END;
  2582. operator := FindOperator(system,op,actualParameters,resultType);
  2583. IF operator # NIL THEN
  2584. designator := NIL;
  2585. IF operator.scope.ownerModule # currentScope.ownerModule THEN
  2586. import := currentScope.ownerModule.moduleScope.firstImport;
  2587. WHILE(import # NIL) & (import.module # operator.scope.ownerModule) DO
  2588. import := import.nextImport;
  2589. END;
  2590. expression := NewSymbolDesignator(position,NIL,import);
  2591. designator := expression(SyntaxTree.Designator);
  2592. END;
  2593. expression := NewSymbolDesignator(position,designator,operator);
  2594. designator := expression(SyntaxTree.Designator);
  2595. result := NewProcedureCallDesignator(position,designator,actualParameters);
  2596. IF op = Scanner.Alias THEN (* hard type cast to same type *)
  2597. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  2598. castReturnType.SetArrayBase(ArrayBase(leftExpression.type.resolved,MAX(LONGINT)));
  2599. result.SetType(castReturnType);
  2600. END;
  2601. ELSE
  2602. result := NIL;
  2603. END;
  2604. END;
  2605. RETURN result
  2606. END NewOperatorCall;
  2607. (** check and resolve binary expression **)
  2608. (*! clean up *)
  2609. PROCEDURE VisitBinaryExpression(binaryExpression: SyntaxTree.BinaryExpression);
  2610. VAR left,right,result: SyntaxTree.Expression;
  2611. leftType, rightType: SyntaxTree.Type;
  2612. il,ir: LONGINT; rl,rr,a,b,c,d,divisor: LONGREAL; hl,hr: HUGEINT;bl,br: BOOLEAN; sl,sr: SET; strl,strr: Scanner.StringType;
  2613. cl,cr: CHAR;
  2614. operator: LONGINT; operatorCall: SyntaxTree.Expression;
  2615. type: SyntaxTree.Type;
  2616. value: SyntaxTree.Value;
  2617. leftFirst, leftLast, leftStep, rightFirst, rightLast, rightStep: LONGINT;
  2618. integerConstantFolding: BOOLEAN;
  2619. list: SyntaxTree.ExpressionList;
  2620. PROCEDURE NewBool(v: BOOLEAN);
  2621. BEGIN
  2622. value := SyntaxTree.NewBooleanValue(binaryExpression.position,v);
  2623. value.SetType(system.booleanType);
  2624. result.SetResolved(value);
  2625. type := system.booleanType
  2626. END NewBool;
  2627. PROCEDURE NewSet(v: SET);
  2628. BEGIN
  2629. value := SyntaxTree.NewSetValue(binaryExpression.position,v);
  2630. value.SetType(system.setType);
  2631. result.SetResolved(value);
  2632. type := system.setType;
  2633. END NewSet;
  2634. PROCEDURE NewInteger(v: HUGEINT; t: SyntaxTree.Type);
  2635. BEGIN
  2636. value := Global.NewIntegerValue(system,binaryExpression.position,v);
  2637. (* type cast to "larger" type only if the value is still in the range *)
  2638. IF (t IS SyntaxTree.AddressType) & IsAddressValue(value) THEN
  2639. value.SetType(t);
  2640. END;
  2641. result.SetResolved(value);
  2642. type := value.type;
  2643. END NewInteger;
  2644. PROCEDURE NewReal(v: LONGREAL; t: SyntaxTree.Type);
  2645. BEGIN
  2646. value := SyntaxTree.NewRealValue(binaryExpression.position,v);
  2647. value.SetType(t);
  2648. result.SetResolved(value);
  2649. type := t;
  2650. END NewReal;
  2651. PROCEDURE NewComplex(realValue, imagValue: LONGREAL; t: SyntaxTree.Type);
  2652. BEGIN
  2653. value := SyntaxTree.NewComplexValue(binaryExpression.position, realValue, imagValue);
  2654. value.SetType(t);
  2655. value(SyntaxTree.ComplexValue).UpdateSubtype;
  2656. result.SetResolved(value);
  2657. type := t;
  2658. END NewComplex;
  2659. BEGIN
  2660. type := SyntaxTree.invalidType;
  2661. left := ResolveExpression(binaryExpression.left);
  2662. right := ResolveExpression(binaryExpression.right);
  2663. binaryExpression.SetLeft(left);
  2664. binaryExpression.SetRight(right);
  2665. result := binaryExpression;
  2666. operator := binaryExpression.operator;
  2667. IF ~system.operatorDefined[operator] THEN
  2668. Error(left.position,Diagnostics.Invalid,"Operator Not Defined");
  2669. result := SyntaxTree.invalidExpression;
  2670. RETURN
  2671. END;
  2672. IF left.type = NIL THEN
  2673. Error(left.position,Diagnostics.Invalid,"Expression has no result type");
  2674. result := SyntaxTree.invalidExpression;
  2675. RETURN;
  2676. END;
  2677. IF right.type = NIL THEN
  2678. Error(right.position,Diagnostics.Invalid,"Expression has no result type");
  2679. result := SyntaxTree.invalidExpression;
  2680. RETURN;
  2681. END;
  2682. leftType := left.type.resolved; rightType := right.type.resolved;
  2683. IF ~(leftType IS SyntaxTree.BasicType) OR ~(rightType IS SyntaxTree.BasicType) OR (leftType IS SyntaxTree.ComplexType) OR (rightType IS SyntaxTree.ComplexType) THEN
  2684. operatorCall := NewOperatorCall(binaryExpression.position,operator,left,right,NIL);
  2685. END;
  2686. IF (operatorCall = NIL) & IsPointerToObject(left.type) THEN
  2687. list := SyntaxTree.NewExpressionList();
  2688. list.AddExpression(right);
  2689. operatorCall := NewObjectOperatorCall(binaryExpression.position, left, operator, NIL, right);
  2690. END;
  2691. IF operatorCall # NIL THEN
  2692. result := operatorCall;
  2693. type := operatorCall.type;
  2694. (* admissible operators:
  2695. Times, Plus, Minus numeric numeric numeric
  2696. set set set
  2697. Slash numeric numeric real /complex
  2698. set set set
  2699. Div , Mod integer integer integer
  2700. And, Or bool bool bool
  2701. Equal, Unequal basic basic bool
  2702. pointer pointer bool
  2703. object object bool
  2704. record record bool
  2705. string string bool
  2706. enumerator enumerator bool
  2707. Less, LessEqual,
  2708. Greater, GreaterEqual integer/real integer/real bool
  2709. enumerator enumerator bool
  2710. In integer set bool
  2711. Is pointer type bool
  2712. object type bool
  2713. record type bool
  2714. Upto: special abbreviation for a..b
  2715. *)
  2716. ELSIF (left.type = NIL) THEN
  2717. Error(left.position,Diagnostics.Invalid,"type (left operand) = NIL in binary expression");
  2718. D.Str("nil type in "); D.Type(left); D.Ln;
  2719. result := SyntaxTree.invalidExpression;
  2720. ELSIF (right.type = NIL) THEN
  2721. Error(right.position,Diagnostics.Invalid,"type (right operand) = NIL in binary expression");
  2722. result := SyntaxTree.invalidExpression;
  2723. ELSIF (leftType = SyntaxTree.invalidType) OR (rightType = SyntaxTree.invalidType) THEN (* error already handled *)
  2724. result := SyntaxTree.invalidExpression;
  2725. ELSIF operator = Scanner.Upto THEN (* left .. right: now solved as RangeExpression*)
  2726. HALT(100);
  2727. ELSIF operator = Scanner.Is THEN (* left IS right: now solved as IsExpression *)
  2728. type := system.booleanType;
  2729. IF ~(rightType = SyntaxTree.typeDeclarationType) THEN
  2730. Error(right.position,Diagnostics.Invalid,"is not a type ");
  2731. ELSIF ~IsTypeExtension(leftType, right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved) THEN
  2732. Error(binaryExpression.position,Diagnostics.Invalid,"is not a type extension of ");
  2733. IF VerboseErrorMessage THEN
  2734. Printout.Info("left",left);
  2735. Printout.Info("right",right);
  2736. END;
  2737. ELSIF IsUnsafePointer(left.type) THEN
  2738. Error(binaryExpression.position,Diagnostics.Invalid,"forbidden type test on unsafe pointer");
  2739. ELSIF (leftType.SameType(right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved)) & ~(leftType IS SyntaxTree.PointerType) THEN
  2740. NewBool(TRUE)
  2741. ELSIF right(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved IS SyntaxTree.AnyType THEN
  2742. NewBool(TRUE);
  2743. ELSIF IsUnextensibleRecord(left) THEN
  2744. NewBool(FALSE)
  2745. END
  2746. ELSIF (right IS SyntaxTree.SymbolDesignator) & (right(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2747. Error(right.position,Diagnostics.Invalid,"must not be a type");
  2748. ELSIF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.TypeDeclaration) THEN
  2749. Error(left.position,Diagnostics.Invalid,"must not be a type");
  2750. ELSIF operator = Scanner.In THEN (* left IN right *)
  2751. IF IsIntegerType(leftType) & (rightType IS SyntaxTree.SetType) THEN
  2752. IF IsIntegerValue(left,il) & IsSetValue(right,sr) THEN
  2753. NewBool(il IN sr);
  2754. ELSE
  2755. IF leftType.sizeInBits # system.longintType.sizeInBits THEN
  2756. left := NewConversion(left.position, left, system.longintType,NIL);
  2757. binaryExpression.SetLeft(left)
  2758. END;
  2759. type := system.booleanType;
  2760. END
  2761. ELSE
  2762. Error(binaryExpression.position,Diagnostics.Invalid, "incompatible operands");
  2763. END
  2764. ELSIF (leftType IS SyntaxTree.ProcedureType) OR (rightType IS SyntaxTree.ProcedureType) THEN
  2765. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2766. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2767. END;
  2768. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN type := system.booleanType
  2769. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 1")
  2770. END
  2771. ELSIF IsPointerType(leftType) OR IsPointerType(rightType) THEN
  2772. IF ~CompatibleTo(system,leftType,rightType) & ~CompatibleTo(system,rightType,leftType) THEN
  2773. (* IsPointerType(leftType) OR ~IsPointerType(rightType) THEN *)
  2774. Error(binaryExpression.position,Diagnostics.Invalid,"incompatible operands");
  2775. IF VerboseErrorMessage THEN Printout.Info("leftType",leftType); Printout.Info("right",rightType) END
  2776. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  2777. ConvertOperands(left, right);
  2778. binaryExpression.SetLeft(left);
  2779. binaryExpression.SetRight(right);
  2780. IF (left IS SyntaxTree.NilValue) & (right IS SyntaxTree.NilValue) THEN
  2781. IF operator = Scanner.Equal THEN NewBool(TRUE) ELSE NewBool(FALSE) END;
  2782. END;
  2783. type := system.booleanType;
  2784. ELSE
  2785. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 3");
  2786. END
  2787. ELSIF (left.resolved# NIL) & (left.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2788. ELSIF (right.resolved # NIL) & (right.resolved IS SyntaxTree.NilValue) THEN Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined");
  2789. ELSIF IsStringType(leftType) & IsStringType(rightType) THEN (* string ops*)
  2790. IF IsStringType(leftType) & IsStringType(rightType) THEN (*ok*)
  2791. IF IsStringValue(left,strl) & IsStringValue(right,strr) THEN
  2792. CASE operator OF
  2793. |Scanner.Equal: NewBool(strl^=strr^);
  2794. |Scanner.Unequal:NewBool(strl^#strr^);
  2795. |Scanner.Less: NewBool(strl^<strr^);
  2796. |Scanner.LessEqual: NewBool(strl^<=strr^);
  2797. |Scanner.Greater: NewBool(strl^>strr^);
  2798. |Scanner.GreaterEqual: NewBool(strl^>=strr^);
  2799. ELSE
  2800. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 4");
  2801. END;
  2802. END;
  2803. ELSIF (operator = Scanner.Equal) OR (operator=Scanner.Unequal) OR (operator = Scanner.Less)
  2804. OR (operator = Scanner.LessEqual) OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2805. type := system.booleanType
  2806. ELSE
  2807. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 5");
  2808. END;
  2809. IF (operator = Scanner.Equal) OR (operator=Scanner.Unequal)
  2810. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2811. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2812. type := system.booleanType;
  2813. ELSE
  2814. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 6");
  2815. END
  2816. ELSIF (leftType IS SyntaxTree.EnumerationType) OR (rightType IS SyntaxTree.EnumerationType) THEN
  2817. IF IsEnumerationExtension(left.type,right.type) OR IsEnumerationExtension(right.type,left.type) THEN
  2818. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2819. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2820. type := system.booleanType
  2821. ELSE
  2822. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined for enumerators");
  2823. END;
  2824. ELSE
  2825. Error(binaryExpression.position,Diagnostics.Invalid,"operator not applicable between different enumerators");
  2826. END;
  2827. ELSIF (leftType IS SyntaxTree.PortType) & ((operator = Scanner.Questionmarks) OR (operator = Scanner.ExclamationMarks) OR (operator = Scanner.LessLessQ)) THEN
  2828. type := system.booleanType;
  2829. ELSIF (rightType IS SyntaxTree.PortType) & (operator = Scanner.LessLessQ) THEN
  2830. type := system.booleanType;
  2831. ELSIF (leftType IS SyntaxTree.BasicType) & (rightType IS SyntaxTree.BasicType)
  2832. OR IsCharacterType(leftType) & IsCharacterType(rightType)
  2833. THEN
  2834. integerConstantFolding := IsIntegerValue(left,il) & IsIntegerValue(right,ir);
  2835. IF (leftType # rightType) THEN
  2836. IF ~integerConstantFolding THEN (* no conversions for constant folding on integer values *)
  2837. ConvertOperands(left,right); (* operands must be of the same type here *)
  2838. END;
  2839. binaryExpression.SetLeft(left);
  2840. binaryExpression.SetRight(right);
  2841. leftType := left.type.resolved;
  2842. rightType := right.type.resolved;
  2843. END;
  2844. type := leftType;
  2845. IF ~integerConstantFolding & ~leftType.SameType(rightType) THEN
  2846. Error(binaryExpression.position,Diagnostics.Invalid,"conversion failed ?");
  2847. IF VerboseErrorMessage THEN
  2848. Printout.Info("left",left);
  2849. Printout.Info("right",right);
  2850. END;
  2851. ELSIF IsIntegerType(leftType) THEN
  2852. IF IsIntegerValue(right,ir) (* & (right.type.sizeInBits < 64) *) THEN
  2853. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2854. IF (hr=0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div) OR (operator = Scanner.Slash)) THEN
  2855. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2856. ELSIF (hr<0) & ((operator = Scanner.Mod) OR (operator = Scanner.Div))THEN
  2857. Error(binaryExpression.position,Diagnostics.Invalid,"integer division by negative number");
  2858. END;
  2859. END;
  2860. (* constant folding *)
  2861. (* bootstrap64
  2862. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) & (type.sizeInBits < 64) THEN
  2863. CASE operator OF
  2864. |Scanner.Plus: NewInteger(il+ir,left.type);
  2865. |Scanner.Minus: NewInteger(il-ir,left.type);
  2866. |Scanner.Times: NewInteger(il*ir,left.type);
  2867. |Scanner.Slash:
  2868. IF ir # 0 THEN
  2869. NewReal(il/ir, system.realType);
  2870. END;
  2871. |Scanner.Mod:
  2872. IF ir > 0 THEN
  2873. NewInteger(il MOD ir,left.type);
  2874. END;
  2875. |Scanner.Div:
  2876. IF ir > 0 THEN
  2877. NewInteger(il DIV ir,left.type);
  2878. END;
  2879. |Scanner.Equal: NewBool(il=ir);
  2880. |Scanner.Unequal:NewBool(il#ir);
  2881. |Scanner.Less: NewBool(il<ir);
  2882. |Scanner.LessEqual: NewBool(il<=ir);
  2883. |Scanner.Greater: NewBool(il>ir);
  2884. |Scanner.GreaterEqual: NewBool(il>=ir);
  2885. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 7");
  2886. END;
  2887. ELS*)
  2888. IF IsIntegerValue(left,il) & IsIntegerValue(right,ir) (* bootstrap64 & (type.sizeInBits = 64)*) THEN
  2889. hl := left.resolved(SyntaxTree.IntegerValue).hvalue;
  2890. hr := right.resolved(SyntaxTree.IntegerValue).hvalue;
  2891. CASE operator OF
  2892. |Scanner.Plus: NewInteger(hl+hr,left.type);
  2893. |Scanner.Minus: NewInteger(hl-hr,left.type);
  2894. |Scanner.Times: NewInteger(hl*hr,left.type);
  2895. |Scanner.Slash:
  2896. IF hr = 0 THEN
  2897. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2898. ELSE
  2899. IF type.sizeInBits = 64 THEN
  2900. NewReal(hl/hr,system.longrealType);
  2901. ELSE
  2902. NewReal(hl/hr,system.realType)
  2903. END
  2904. END;
  2905. (* do the bootstrapping for this kind of expression on hugeint values , then enable: *)
  2906. |Scanner.Mod:
  2907. IF hr = 0 THEN
  2908. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2909. ELSE
  2910. NewInteger(hl MOD hr, left.type);
  2911. (* bootstrap64
  2912. NewInteger(hl - Machine.DivH(hl,hr)*hr,left.type);
  2913. *)
  2914. END;
  2915. |Scanner.Div:
  2916. IF hr = 0 THEN
  2917. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2918. ELSE
  2919. NewInteger(hl DIV hr, left.type);
  2920. (* bootstrap64
  2921. NewInteger(Machine.DivH(hl,hr),left.type);
  2922. *)
  2923. END;
  2924. (* *)
  2925. |Scanner.Equal: NewBool(hl=hr);
  2926. |Scanner.Unequal: NewBool(hl#hr);
  2927. |Scanner.Less: NewBool(hl<hr);
  2928. |Scanner.LessEqual: NewBool(hl<=hr);
  2929. |Scanner.Greater: NewBool(hl>hr);
  2930. |Scanner.GreaterEqual:NewBool(hl>=hr);
  2931. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 8");
  2932. END;
  2933. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR
  2934. (operator = Scanner.Mod) OR (operator = Scanner.Div) THEN
  2935. type := left.type
  2936. ELSIF (operator = Scanner.Slash) THEN
  2937. left := NewConversion(left.position,left,system.realType,NIL);
  2938. right := NewConversion(right.position,right,system.realType,NIL);
  2939. binaryExpression.SetLeft(left);
  2940. binaryExpression.SetRight(right);
  2941. type := system.realType
  2942. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2943. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2944. type := system.booleanType
  2945. ELSE
  2946. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 9");
  2947. END;
  2948. ELSIF (leftType IS SyntaxTree.FloatType) THEN
  2949. IF IsRealValue(left,rl) & IsRealValue(right,rr) THEN
  2950. CASE operator OF
  2951. |Scanner.Plus: NewReal(rl+rr,leftType);
  2952. |Scanner.Minus: NewReal(rl-rr,leftType);
  2953. |Scanner.Times:NewReal(rl*rr,leftType);
  2954. |Scanner.Slash:
  2955. IF rr = 0 THEN
  2956. Error(binaryExpression.position,Diagnostics.Invalid,"division by zero");
  2957. ELSE
  2958. NewReal(rl/rr,leftType);
  2959. END
  2960. |Scanner.Equal: NewBool(rl=rr);
  2961. |Scanner.Unequal: NewBool(rl#rr);
  2962. |Scanner.Less: NewBool(rl<rr);
  2963. |Scanner.LessEqual: NewBool(rl<=rr);
  2964. |Scanner.Greater: NewBool(rl>rr);
  2965. |Scanner.GreaterEqual: NewBool(rl>=rr);
  2966. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 10");
  2967. END;
  2968. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  2969. type := left.type
  2970. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  2971. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  2972. type := system.booleanType
  2973. ELSE
  2974. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 11");
  2975. IF VerboseErrorMessage THEN
  2976. Printout.Info("left",left);
  2977. Printout.Info("right",right);
  2978. END;
  2979. END;
  2980. ELSIF (leftType IS SyntaxTree.ComplexType) THEN
  2981. CASE operator OF
  2982. |Scanner.Plus, Scanner.Minus, Scanner.Times, Scanner.Slash: type := left.type
  2983. |Scanner.Equal, Scanner.Unequal: type := system.booleanType
  2984. ELSE
  2985. Error(binaryExpression.position, Diagnostics.Invalid,"operator not defined");
  2986. IF VerboseErrorMessage THEN
  2987. Printout.Info("left", left);
  2988. Printout.Info("right", right)
  2989. END;
  2990. END;
  2991. IF ~error THEN
  2992. IF (operator = Scanner.Slash) & IsComplexValue(right, c, d) & (c = 0) & (d = 0) THEN
  2993. Error(binaryExpression.position, Diagnostics.Invalid,"division by zero")
  2994. ELSIF IsComplexValue(left, a, b) & IsComplexValue(right, c, d) THEN
  2995. (* do constant folding *)
  2996. CASE operator OF
  2997. |Scanner.Plus: NewComplex(a + b, c + d, leftType)
  2998. |Scanner.Minus: NewComplex(a - b, c - d, leftType)
  2999. |Scanner.Times: NewComplex(a * c - b * d, b * c + a * d, leftType)
  3000. |Scanner.Slash:
  3001. divisor := c * c + d * d;
  3002. ASSERT(divisor # 0);
  3003. NewComplex((a * c + b * d) / divisor, (b * c - a * d) / divisor, leftType)
  3004. |Scanner.Equal: NewBool((a = c) & (b = d))
  3005. |Scanner.Unequal: NewBool((a # c) OR (b # d))
  3006. END
  3007. END
  3008. END
  3009. ELSIF (leftType IS SyntaxTree.BooleanType) THEN
  3010. IF IsBooleanValue(left,bl) & IsBooleanValue(right,br) THEN
  3011. CASE operator OF
  3012. |Scanner.And: NewBool(bl & br);
  3013. |Scanner.Or: NewBool(bl OR br);
  3014. |Scanner.Equal: NewBool(bl = br);
  3015. |Scanner.Unequal: NewBool(bl # br);
  3016. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 12");
  3017. END;
  3018. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.And) OR (operator = Scanner.Or) THEN
  3019. type := system.booleanType
  3020. ELSE
  3021. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 13");
  3022. END;
  3023. ELSIF left.type.resolved IS SyntaxTree.RangeType THEN
  3024. (* constant folding *)
  3025. IF IsStaticRange(left, leftFirst, leftLast, leftStep) & IsStaticRange(right, rightFirst, rightLast, rightStep) THEN
  3026. IF operator = Scanner.Equal THEN
  3027. NewBool((leftFirst = rightFirst) & (leftLast = rightLast) & (leftStep = rightStep))
  3028. ELSIF operator = Scanner.Unequal THEN
  3029. NewBool((leftFirst # rightFirst) OR (leftLast # rightLast) OR (leftStep # rightStep))
  3030. END;
  3031. END;
  3032. IF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) THEN
  3033. type := system.booleanType;
  3034. ELSE
  3035. Error(binaryExpression.position, Diagnostics.Invalid, "operator not defined");
  3036. END;
  3037. ELSIF (leftType IS SyntaxTree.SetType) THEN
  3038. IF IsSetValue(left,sl) & IsSetValue(right,sr) THEN
  3039. CASE operator OF
  3040. |Scanner.Plus: NewSet(sl + sr);
  3041. |Scanner.Minus: NewSet(sl - sr);
  3042. |Scanner.Times: NewSet(sl * sr);
  3043. |Scanner.Slash: NewSet(sl / sr);
  3044. |Scanner.Equal: NewBool(sl=sr);
  3045. |Scanner.Unequal: NewBool(sl#sr);
  3046. |Scanner.Less: NewBool( (sl * sr = sl) & (sl#sr));
  3047. |Scanner.LessEqual: NewBool(sl*sr = sl);
  3048. |Scanner.Greater: NewBool( (sl * sr = sr) & (sl # sr));
  3049. |Scanner.GreaterEqual: NewBool(sl*sr = sr);
  3050. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 14");
  3051. END;
  3052. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal)
  3053. OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3054. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) (* implement inclusion *)
  3055. THEN
  3056. type := system.booleanType
  3057. ELSIF (operator = Scanner.Plus) OR (operator = Scanner.Minus) OR (operator = Scanner.Times) OR (operator = Scanner.Slash) THEN
  3058. type := left.type
  3059. ELSE
  3060. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 15");
  3061. END;
  3062. ELSIF IsCharacterType(left.type) THEN
  3063. IF IsCharacterValue(left,cl) & IsCharacterValue(right,cr) THEN
  3064. CASE operator OF
  3065. |Scanner.Equal: NewBool(cl=cr);
  3066. |Scanner.Unequal: NewBool(cl#cr);
  3067. |Scanner.Less: NewBool(cl<cr);
  3068. |Scanner.LessEqual: NewBool(cl<=cr);
  3069. |Scanner.Greater: NewBool(cl>cr);
  3070. |Scanner.GreaterEqual: NewBool(cl>=cr);
  3071. ELSE Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 16");
  3072. END;
  3073. ELSIF (operator = Scanner.Equal) OR (operator = Scanner.Unequal) OR (operator = Scanner.Less) OR (operator = Scanner.LessEqual)
  3074. OR (operator = Scanner.Greater) OR (operator = Scanner.GreaterEqual) THEN
  3075. type := system.booleanType
  3076. ELSE
  3077. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 17");
  3078. END;
  3079. ELSE
  3080. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 18");
  3081. END;
  3082. ELSE
  3083. Error(binaryExpression.position,Diagnostics.Invalid,"operator not defined 19");
  3084. END;
  3085. IF type = SyntaxTree.invalidType THEN
  3086. result := SyntaxTree.invalidExpression
  3087. ELSE
  3088. result.SetType(type)
  3089. END;
  3090. resolvedExpression := result
  3091. END VisitBinaryExpression;
  3092. (** resolve a range expression of the from <<first .. last BY step>>
  3093. - depending on the context different things are checked:
  3094. ArrayIndex:
  3095. - components must be integers
  3096. - replace missing lower bound with 0
  3097. - replace missing upper bound with MAX(LONGINT)
  3098. - replace missing step size with 1
  3099. SetElement:
  3100. - components must be integers
  3101. - replace missing lower bound with 0
  3102. - replace missing upper bound with MAX(SET)
  3103. - must not have step size
  3104. CaseGuard:
  3105. - components must be constant
  3106. - components must be integers or characters
  3107. - must have lower and upper bound present
  3108. - components are made compatible
  3109. - must not have step size
  3110. - if error: return invalidExpression
  3111. **)
  3112. PROCEDURE VisitRangeExpression(x: SyntaxTree.RangeExpression);
  3113. VAR
  3114. hasError: BOOLEAN;
  3115. first, last, step: SyntaxTree.Expression;
  3116. BEGIN
  3117. hasError := FALSE;
  3118. first := x.first;
  3119. last := x.last;
  3120. step := x.step;
  3121. (* check lower bound *)
  3122. IF x.context = SyntaxTree.CaseGuard THEN
  3123. IF first = NIL THEN
  3124. Error(x.position, Diagnostics.Invalid, "missing lower bound");
  3125. hasError := TRUE
  3126. ELSE
  3127. first := ResolveExpression(first);
  3128. IF ~(first.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(first.type.resolved) THEN
  3129. Error(first.position, Diagnostics.Invalid, "lower bound not integer or character");
  3130. hasError := TRUE
  3131. ELSE
  3132. IF first IS SyntaxTree.StringValue THEN
  3133. (* add conversion from string to character *)
  3134. first := ConvertValue(first.position, first(SyntaxTree.Value), system.characterType)
  3135. END
  3136. END;
  3137. (* check if expression is constant *)
  3138. IF ConstantExpression(first) = SyntaxTree.invalidExpression THEN
  3139. (* error already reported *)
  3140. hasError := TRUE
  3141. END
  3142. END
  3143. ELSE (* ArrayIndex, SetElement *)
  3144. IF first = NIL THEN
  3145. first := SyntaxTree.NewIntegerValue(x.position, 0);
  3146. END;
  3147. first := ResolveExpression(first);
  3148. IF first.type.resolved IS SyntaxTree.IntegerType THEN
  3149. first := NewConversion(first.position, first, system.longintType, NIL)
  3150. ELSE
  3151. Error(first.position, Diagnostics.Invalid, "lower bound not integer");
  3152. hasError := TRUE
  3153. END
  3154. END;
  3155. (* check upper bound *)
  3156. IF x.context = SyntaxTree.CaseGuard THEN
  3157. IF last = NIL THEN
  3158. Error(x.position, Diagnostics.Invalid, "missing upper bound");
  3159. hasError := TRUE
  3160. ELSE
  3161. last := ResolveExpression(last);
  3162. IF ~(last.type.resolved IS SyntaxTree.IntegerType) & ~IsCharacterType(last.type.resolved) THEN
  3163. Error(last.position, Diagnostics.Invalid, "lower bound not integer or character");
  3164. hasError := TRUE
  3165. ELSE
  3166. IF last IS SyntaxTree.StringValue THEN
  3167. (* add conversion from string to character *)
  3168. last := ConvertValue(last.position, last(SyntaxTree.Value), system.characterType)
  3169. END
  3170. END;
  3171. (* check if expression is constant *)
  3172. IF ConstantExpression(last) = SyntaxTree.invalidExpression THEN
  3173. (* error already reported *)
  3174. hasError := TRUE
  3175. ELSE
  3176. (* try to make lower and upper bound compatible *)
  3177. ConvertOperands(first, last);
  3178. IF first.type.resolved # last.type.resolved THEN
  3179. Error(x.position, Diagnostics.Invalid, "lower and upper bounds incompatible");
  3180. hasError := TRUE
  3181. END
  3182. END
  3183. END
  3184. ELSE (* ArrayIndex, SetElement *)
  3185. IF last = NIL THEN
  3186. IF x.context = SyntaxTree.ArrayIndex THEN
  3187. last := SyntaxTree.NewIntegerValue(x.position, MAX(LONGINT))
  3188. ELSE
  3189. last := SyntaxTree.NewIntegerValue(x.position, MAX(SET))
  3190. END
  3191. END;
  3192. last := ResolveExpression(last);
  3193. IF last.type.resolved IS SyntaxTree.IntegerType THEN
  3194. last := NewConversion(last.position, last, system.longintType, NIL)
  3195. ELSE
  3196. Error(last.position, Diagnostics.Invalid, "upper bound not integer");
  3197. hasError := TRUE
  3198. END
  3199. END;
  3200. (* check step size *)
  3201. IF x.context = SyntaxTree.ArrayIndex THEN
  3202. IF step = NIL THEN
  3203. step := SyntaxTree.NewIntegerValue(x.position, 1)
  3204. END;
  3205. step := ResolveExpression(step);
  3206. IF step.type.resolved IS SyntaxTree.IntegerType THEN
  3207. step := NewConversion(step.position, step, system.longintType, NIL)
  3208. ELSE
  3209. Error(step.position, Diagnostics.Invalid, "step size not integer");
  3210. hasError := TRUE
  3211. END
  3212. ELSE (* SetElement, CaseGuard *)
  3213. IF step # NIL THEN
  3214. Error(last.position, Diagnostics.Invalid, "step size not allowed in this context");
  3215. hasError := TRUE
  3216. END
  3217. END;
  3218. IF hasError THEN
  3219. resolvedExpression := SyntaxTree.invalidExpression
  3220. ELSE
  3221. x.SetFirst(first);
  3222. x.SetLast(last);
  3223. x.SetStep(step);
  3224. x.SetType(system.rangeType);
  3225. resolvedExpression := x;
  3226. resolvedExpression.SetAssignable(FALSE) (* range expressions may never be assigned to *)
  3227. END
  3228. END VisitRangeExpression;
  3229. PROCEDURE VisitTensorRangeExpression(x: SyntaxTree.TensorRangeExpression);
  3230. BEGIN
  3231. x.SetType(NIL);
  3232. resolvedExpression := x;
  3233. END VisitTensorRangeExpression;
  3234. (** resolve the expression d and return result as designator
  3235. - resolve expression
  3236. - if expression is a designator then return designator else error message and return invalidDesignator
  3237. **)
  3238. PROCEDURE ResolveDesignator*(d: SyntaxTree.Expression): SyntaxTree.Designator;
  3239. VAR result: SyntaxTree.Designator; resolved: SyntaxTree.Expression;
  3240. BEGIN
  3241. IF Trace THEN D.Str("ResolveDesignator"); D.Ln; END;
  3242. resolved := ResolveExpression(d);
  3243. IF resolved = SyntaxTree.invalidExpression THEN
  3244. (* error should already have been reported *)
  3245. result := SyntaxTree.invalidDesignator;
  3246. ELSIF resolved IS SyntaxTree.Designator THEN
  3247. result := resolved(SyntaxTree.Designator);
  3248. ELSE
  3249. Error(d.position,Diagnostics.Invalid,"is no designator ! ");
  3250. result := SyntaxTree.invalidDesignator;
  3251. END;
  3252. (* result.type might be nil. *)
  3253. RETURN result
  3254. END ResolveDesignator;
  3255. (**
  3256. symbol designator generated in this module
  3257. nothing to be resolved
  3258. **)
  3259. PROCEDURE VisitSymbolDesignator(x: SyntaxTree.SymbolDesignator);
  3260. BEGIN
  3261. resolvedExpression := x;
  3262. END VisitSymbolDesignator;
  3263. (**
  3264. self designator generated in this module
  3265. nothing to be resolved
  3266. **)
  3267. PROCEDURE VisitSelfDesignator(x: SyntaxTree.SelfDesignator);
  3268. VAR scope: SyntaxTree.Scope; record: SyntaxTree.RecordType; type: SyntaxTree.Type; cell: SyntaxTree.CellType;
  3269. BEGIN
  3270. (* check if in record scope *)
  3271. scope := currentScope;
  3272. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) &~(scope IS SyntaxTree.CellScope) DO
  3273. scope := scope.outerScope;
  3274. END;
  3275. IF scope = NIL THEN (* in module scope *)
  3276. x.SetType(system.anyType);
  3277. ELSIF scope IS SyntaxTree.CellScope THEN
  3278. cell := scope(SyntaxTree.CellScope).ownerCell;
  3279. x.SetType(cell);
  3280. ELSE (* in record scope *)
  3281. record := scope(SyntaxTree.RecordScope).ownerRecord;
  3282. IF (record # NIL) & (record.pointerType # NIL) THEN
  3283. type := ResolveType(record.pointerType);
  3284. x.SetType(type);
  3285. ELSE
  3286. x.SetType(record);
  3287. END;
  3288. END;
  3289. resolvedExpression := x;
  3290. END VisitSelfDesignator;
  3291. PROCEDURE VisitResultDesignator(x: SyntaxTree.ResultDesignator);
  3292. VAR scope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; procedureType: SyntaxTree.ProcedureType; returnType: SyntaxTree.Type;
  3293. BEGIN
  3294. scope := currentScope;
  3295. IF (scope # NIL) & (scope IS SyntaxTree.ProcedureScope) THEN
  3296. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  3297. procedureType := procedure.type(SyntaxTree.ProcedureType);
  3298. returnType := procedureType.returnType;
  3299. IF IsPointerType(returnType) OR IsArrayType(returnType) OR IsMathArrayType(returnType)
  3300. THEN
  3301. x.SetType(returnType);
  3302. ELSE
  3303. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator (only pointer, array and math array)");
  3304. x.SetType(SyntaxTree.invalidType);
  3305. END;
  3306. ELSE
  3307. Error(x.position,Diagnostics.Invalid,"forbidden access to result designator");
  3308. x.SetType(SyntaxTree.invalidType);
  3309. END;
  3310. x.SetAssignable(TRUE);
  3311. resolvedExpression := x;
  3312. END VisitResultDesignator;
  3313. (**
  3314. return symbol designator as an expression
  3315. - if symbol is a constant then return the constant value expression
  3316. - else
  3317. - if no left designator present then do auto-self if in record scope identifier-> SELF.identiifer
  3318. - if symbol is a guarded variable then return a TypeGuardDesignator
  3319. - else return a symbol designator
  3320. **)
  3321. PROCEDURE NewSymbolDesignator*(position: LONGINT; left: SyntaxTree.Designator; symbol: SyntaxTree.Symbol): SyntaxTree.Expression;
  3322. VAR result: SyntaxTree.Expression; assignable: BOOLEAN; scope: SyntaxTree.Scope;
  3323. guardType: SyntaxTree.Type;
  3324. BEGIN
  3325. IF Trace THEN D.Str("NewSymbolDesignator "); D.Ln; END;
  3326. result := SyntaxTree.invalidExpression;
  3327. ASSERT(symbol # NIL);
  3328. (*
  3329. not necessary any more since a type declaration is of type SyntaxTree.typeDeclarationType now
  3330. IF symbol IS SyntaxTree.TypeDeclaration THEN
  3331. Error(position,Diagnostics.Invalid,"type not allowed here");
  3332. ELS *)
  3333. (* not needed any more as values are stored in the expression
  3334. IF symbol IS SyntaxTree.Constant THEN
  3335. result := symbol(SyntaxTree.Constant).value
  3336. IF symbol(SyntaxTree.Constant).value # NIL THEN
  3337. IF symbol(SyntaxTree.Constant).value IS SyntaxTree.Value THEN
  3338. result := symbol(SyntaxTree.Constant).value(SyntaxTree.Value).Copy(position);
  3339. ELSE
  3340. result := symbol(SyntaxTree.Constant).value
  3341. END;
  3342. ELSE
  3343. *)
  3344. IF (left = NIL) & (symbol.scope IS SyntaxTree.RecordScope)
  3345. OR (left = NIL) & (symbol.scope IS SyntaxTree.CellScope) & cellsAreObjects
  3346. THEN
  3347. left := ResolveDesignator(SyntaxTree.NewSelfDesignator(position)); (* auto self *)
  3348. IF (IsPointerType(left.type) OR (left.type.resolved IS SyntaxTree.CellType) & cellsAreObjects) &~(symbol IS SyntaxTree.Import) THEN
  3349. left := NewDereferenceDesignator(position,left);
  3350. left.SetHidden(TRUE);
  3351. END;
  3352. ELSIF (symbol.scope IS SyntaxTree.ProcedureScope) THEN
  3353. scope := currentScope;
  3354. WHILE (scope # NIL) & (scope # symbol.scope) & ~(scope IS SyntaxTree.RecordScope) DO
  3355. scope := scope.outerScope;
  3356. END;
  3357. IF (scope # NIL) & (scope # symbol.scope) & ~(symbol IS SyntaxTree.Constant) THEN
  3358. Error(position,Diagnostics.Invalid,"forbidden access to symbol in parent procedure scope");
  3359. END;
  3360. END;
  3361. assignable := (left = NIL) OR left.assignable OR (left IS SyntaxTree.DereferenceDesignator) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Import);
  3362. IF (currentScope # NIL) & (symbol.scope.ownerModule # currentScope.ownerModule) THEN
  3363. assignable := assignable & (SyntaxTree.PublicWrite IN symbol.access);
  3364. ELSE
  3365. assignable := assignable & (SyntaxTree.InternalWrite IN symbol.access);
  3366. END;
  3367. assignable := assignable & ((symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter)
  3368. & (symbol(SyntaxTree.Parameter).kind # SyntaxTree.ConstParameter) & ~(symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType));
  3369. result := SyntaxTree.NewSymbolDesignator(position,left,symbol);
  3370. result.SetType(symbol.type);
  3371. result.SetAssignable(assignable);
  3372. symbol.MarkUsed;
  3373. IF symbol IS SyntaxTree.Constant THEN
  3374. result.SetResolved(symbol(SyntaxTree.Constant).value.resolved);
  3375. END;
  3376. IF (symbol IS SyntaxTree.Variable) & ~(symbol IS SyntaxTree.Property) THEN
  3377. variableAccessed := TRUE
  3378. END;
  3379. IF (left = NIL) OR (left IS SyntaxTree.SelfDesignator) OR (left IS SyntaxTree.DereferenceDesignator) & (left(SyntaxTree.DereferenceDesignator).left IS SyntaxTree.SelfDesignator) THEN
  3380. IF GetGuard(symbol,guardType) THEN
  3381. result := NewTypeGuardDesignator(position,result(SyntaxTree.SymbolDesignator),guardType, result);
  3382. END;
  3383. END;
  3384. ASSERT(result.type # NIL);
  3385. RETURN result
  3386. END NewSymbolDesignator;
  3387. (** check and resolve an identifier designator "identifier"
  3388. - if identifier = self then return SelfDesignator
  3389. - else find symbol in current scope
  3390. - if symbol found then return SymbolDesignator, else error message and return invalidDesignator
  3391. **)
  3392. PROCEDURE VisitIdentifierDesignator(identifierDesignator: SyntaxTree.IdentifierDesignator);
  3393. VAR symbol: SyntaxTree.Symbol;
  3394. BEGIN
  3395. IF Trace THEN D.Str("VisitIdentifierDesignator "); D.Ln; END;
  3396. symbol := Find(currentScope,identifierDesignator.identifier,TRUE);
  3397. IF symbol # NIL THEN
  3398. ResolveSymbol(symbol);
  3399. ASSERT(symbol.type # NIL);
  3400. resolvedExpression := NewSymbolDesignator(identifierDesignator.position,NIL,symbol);
  3401. ELSE
  3402. Error(identifierDesignator.position,Basic.UndeclaredIdentifier,"");
  3403. IF VerboseErrorMessage THEN
  3404. Printout.Info("undeclared identifier designator",identifierDesignator);
  3405. END;
  3406. resolvedExpression := SyntaxTree.invalidDesignator;
  3407. END;
  3408. END VisitIdentifierDesignator;
  3409. (** check and resolve a selector designator of the form left.designator
  3410. - if left is a pointer type then do auto dereferenciation
  3411. - left denotes a search scope:
  3412. - if left type is import type then set search scope to respective module
  3413. - if left type is enumeration type then set search scope to respective enumeration scope
  3414. - elsif left type is record type then set search scope to record scope
  3415. - search symbol in computed scope
  3416. returns selector designator (via global variable resolvedExpression) if symbol found, else error message is given and invalidDesignator is returned
  3417. **)
  3418. PROCEDURE VisitSelectorDesignator(selectorDesignator: SyntaxTree.SelectorDesignator);
  3419. VAR
  3420. symbol: SyntaxTree.Symbol; left: SyntaxTree.Designator; scope: SyntaxTree.Scope;
  3421. module: SyntaxTree.Module; result: SyntaxTree.Expression; type: SyntaxTree.Type;
  3422. BEGIN
  3423. IF Trace THEN D.Str("VisitSelectorDesignator"); D.Ln; END;
  3424. left := ResolveDesignator(selectorDesignator.left);
  3425. result := SyntaxTree.invalidDesignator;
  3426. IF left # NIL THEN
  3427. IF (left.type # NIL) & IsPointerType(left.type.resolved) THEN
  3428. left := NewDereferenceDesignator(selectorDesignator.position,left);
  3429. END;
  3430. scope := NIL;
  3431. IF left.type = NIL THEN
  3432. Error(selectorDesignator.position,Diagnostics.Invalid,"field on nil typed designator");
  3433. IF VerboseErrorMessage THEN Printout.Info("nil typed designator",left) END;
  3434. ELSIF left.type.resolved = SyntaxTree.invalidType THEN (* error already handled *)
  3435. ELSIF left.type.resolved = SyntaxTree.importType THEN
  3436. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3437. module := symbol(SyntaxTree.Import).module;
  3438. IF module # NIL THEN
  3439. scope := module.moduleScope
  3440. ELSE
  3441. Error(left.position,Diagnostics.Invalid,"module not loaded");
  3442. IF VerboseErrorMessage THEN Printout.Info("unloaded module",symbol) END;
  3443. END;
  3444. ELSIF left.type.resolved IS SyntaxTree.RecordType THEN
  3445. scope := left.type.resolved(SyntaxTree.RecordType).recordScope;
  3446. ASSERT(scope # NIL)
  3447. ELSIF left.type.resolved = SyntaxTree.typeDeclarationType THEN
  3448. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  3449. type := symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  3450. IF type IS SyntaxTree.EnumerationType THEN
  3451. scope := type(SyntaxTree.EnumerationType).enumerationScope;
  3452. ELSE
  3453. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-enumeration type declaration");
  3454. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3455. END;
  3456. ELSIF left.type.resolved IS SyntaxTree.CellType THEN
  3457. scope := left.type.resolved(SyntaxTree.CellType).cellScope;
  3458. ELSE
  3459. Error(selectorDesignator.position,Diagnostics.Invalid,"field on non-record type designator");
  3460. IF VerboseErrorMessage THEN Printout.Info("non-record type designator",left) END;
  3461. END;
  3462. symbol := NIL;
  3463. IF scope # NIL THEN
  3464. symbol := Find(scope,selectorDesignator.identifier,FALSE (* do not visit nested scopes *));
  3465. IF symbol # NIL THEN
  3466. ResolveSymbol(symbol);
  3467. result := NewSymbolDesignator(selectorDesignator.position,left,symbol);
  3468. symbol.MarkUsed
  3469. ELSE
  3470. Error(selectorDesignator.position,Diagnostics.Invalid,"undeclared identifier (selector)");
  3471. IF VerboseErrorMessage THEN
  3472. D.Str("IDENT = "); D.Str0(selectorDesignator.identifier); D.Ln;
  3473. Printout.Info("scope", scope);
  3474. Printout.Info("left", left);
  3475. Printout.Info("undeclared identifier",selectorDesignator);
  3476. Printout.Info("left resolved designator",left);
  3477. END
  3478. END;
  3479. END;
  3480. END;
  3481. resolvedExpression := result;
  3482. END VisitSelectorDesignator;
  3483. PROCEDURE IndexCheck(index,length: SyntaxTree.Expression);
  3484. VAR len,idx: LONGINT;
  3485. BEGIN
  3486. IF (index # NIL) & IsIntegerValue(index,idx) THEN
  3487. IF idx < 0 THEN
  3488. Error(index.position,Diagnostics.Invalid,"index out of bounds (too small)")
  3489. ELSE
  3490. IF (length # NIL) & IsIntegerValue(length,len) & (idx >= len) THEN
  3491. Error(index.position,Diagnostics.Invalid,"index out of bounds (too large)");
  3492. END;
  3493. END;
  3494. END;
  3495. END IndexCheck;
  3496. (*
  3497. - if index designator has not type, use newBaseType as its type
  3498. - otherwise, replace the element type (last base type of math array chain) with newBaseType
  3499. - special rule: if static array of dynamic array occurs, make it all dynamic
  3500. index designator type: new base type: new index designator type:
  3501. NIL z z
  3502. ARRAY [x, y] z ARRAY [x, y] OF z
  3503. ARRAY [x, y] ARRAY [z] ARRAY [x, y, z]
  3504. ARRAY [x, y] ARRAY [*] ARRAY [*, *, *]
  3505. *)
  3506. PROCEDURE SetIndexBaseType(indexDesignator: SyntaxTree.IndexDesignator; newBaseType: SyntaxTree.Type);
  3507. VAR
  3508. mathArrayType: SyntaxTree.MathArrayType;
  3509. makeDynamic: BOOLEAN;
  3510. BEGIN
  3511. IF indexDesignator.type = NIL THEN
  3512. indexDesignator.SetType(newBaseType)
  3513. ELSE
  3514. (* index designator must be a of math array type *)
  3515. ASSERT(indexDesignator.type.resolved IS SyntaxTree.MathArrayType);
  3516. mathArrayType := indexDesignator.type.resolved(SyntaxTree.MathArrayType);
  3517. (* determine if all arrays have to be made dynamic *)
  3518. makeDynamic :=
  3519. (newBaseType.resolved IS SyntaxTree.MathArrayType) &
  3520. (newBaseType.resolved(SyntaxTree.MathArrayType).form # SyntaxTree.Static);
  3521. WHILE (mathArrayType.arrayBase # NIL) & (mathArrayType.arrayBase IS SyntaxTree.MathArrayType) DO
  3522. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3523. mathArrayType := mathArrayType.arrayBase(SyntaxTree.MathArrayType)
  3524. END;
  3525. IF makeDynamic THEN mathArrayType.SetForm(SyntaxTree.Open) END;
  3526. mathArrayType.SetArrayBase(newBaseType)
  3527. END
  3528. END SetIndexBaseType;
  3529. (** check and append index list element to index designator of math array
  3530. - check validity of single index or array range
  3531. - compute new type
  3532. - if range then create new array type (calculate length of resulting array)
  3533. - otherwise take sourceArray.arrayBase as new type
  3534. - type is not only replaced but might have to be inserted when resolving expressions of the form A[*,i,j,*]
  3535. **)
  3536. PROCEDURE AppendMathIndex(position: LONGINT; indexDesignator: SyntaxTree.IndexDesignator; indexListItem: SyntaxTree.Expression; sourceArray: SyntaxTree.MathArrayType);
  3537. VAR
  3538. targetArray: SyntaxTree.MathArrayType;
  3539. first, last, step: SyntaxTree.Expression;
  3540. firstValue, lastValue, stepValue, length: LONGINT;
  3541. rangeExpression: SyntaxTree.RangeExpression;
  3542. BEGIN
  3543. IF indexListItem.type = SyntaxTree.invalidType THEN
  3544. (* error already handled *)
  3545. indexDesignator.parameters.AddExpression(indexListItem)
  3546. ELSIF indexListItem IS SyntaxTree.TensorRangeExpression THEN
  3547. indexDesignator.HasRange;
  3548. indexDesignator.HasTensorRange;
  3549. indexDesignator.parameters.AddExpression(indexListItem);
  3550. indexDesignator.SetType(SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Tensor))
  3551. ELSIF indexListItem.type.resolved IS SyntaxTree.IntegerType THEN
  3552. IndexCheck(indexListItem, sourceArray.length);
  3553. indexListItem := NewConversion(InvalidPosition, indexListItem, system.sizeType, NIL);
  3554. indexDesignator.parameters.AddExpression(indexListItem)
  3555. ELSIF indexListItem.type.resolved IS SyntaxTree.RangeType THEN
  3556. indexDesignator.HasRange;
  3557. (* if the range is given as an array range expression, check the validity of its components *)
  3558. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3559. rangeExpression := indexListItem(SyntaxTree.RangeExpression);
  3560. first := rangeExpression.first;
  3561. last := rangeExpression.last;
  3562. step := rangeExpression.step;
  3563. (* perform static checks on range components *)
  3564. IF IsIntegerValue(first, firstValue) & (firstValue < 0) THEN
  3565. Error(indexListItem.position, Diagnostics.Invalid,"lower bound of array range too small")
  3566. END;
  3567. IF IsIntegerValue(last, lastValue) & (lastValue # MAX(LONGINT)) THEN
  3568. IF (sourceArray.length # NIL) & IsIntegerValue(sourceArray.length, length) & (lastValue > (length - 1)) THEN
  3569. Error(indexListItem.position, Diagnostics.Invalid,"upper bound of array range too large")
  3570. END
  3571. END;
  3572. IF IsIntegerValue(step, stepValue) & (stepValue < 1) THEN
  3573. Error(indexListItem.position, Diagnostics.Invalid,"invalid step size")
  3574. END;
  3575. (* add conversions to size type *)
  3576. (* TODO: needed? *)
  3577. rangeExpression.SetFirst(NewConversion(InvalidPosition, first, system.sizeType, NIL));
  3578. rangeExpression.SetLast(NewConversion(InvalidPosition, last, system.sizeType, NIL));
  3579. rangeExpression.SetStep(NewConversion(InvalidPosition, step, system.sizeType, NIL));
  3580. END;
  3581. IF indexDesignator.hasTensorRange THEN
  3582. (* the index designator's base type is a tensor: leave it as is *)
  3583. ELSE
  3584. (* append a new math array to the index designator's base type *)
  3585. targetArray := SyntaxTree.NewMathArrayType(position, NIL, SyntaxTree.Open);
  3586. IF ~error THEN
  3587. (*
  3588. (* optimization: calculate length of target array for static ranges *)
  3589. IF indexListItem IS SyntaxTree.RangeExpression THEN
  3590. IF IsStaticallyOpenRange(rangeExpression) THEN
  3591. (* range is open ('*'): reuse source array length as target array length *)
  3592. targetArray.SetLength(sourceArray.length); (* the length may or may not be static *)
  3593. targetArray.SetIncrement(sourceArray.staticIncrementInBits)
  3594. ELSIF IsStaticRange(rangeExpression, firstValue, lastValue, stepValue) THEN
  3595. IF lastValue = MAX(LONGINT) THEN
  3596. IF IsIntegerValue(sourceArray.length, length) THEN
  3597. lastValue := length - 1;
  3598. isStaticTargetArrayLength := TRUE
  3599. ELSE
  3600. isStaticTargetArrayLength := FALSE
  3601. END
  3602. ELSE
  3603. isStaticTargetArrayLength := TRUE
  3604. END;
  3605. IF isStaticTargetArrayLength THEN
  3606. (* calculate static target array length *)
  3607. IF firstValue > lastValue THEN
  3608. length := 0
  3609. ELSE
  3610. length := 1 + lastValue - firstValue;
  3611. IF length MOD stepValue = 0 THEN
  3612. length := length DIV stepValue
  3613. ELSE
  3614. length := length DIV stepValue + 1
  3615. END
  3616. END;
  3617. targetArray.SetLength(Global.NewIntegerValue(system, position, length));
  3618. targetArray.SetIncrement(sourceArray.staticIncrementInBits * stepValue);
  3619. ASSERT(targetArray.form = SyntaxTree.Static)
  3620. END
  3621. END
  3622. END
  3623. *)
  3624. END;
  3625. SetIndexBaseType(indexDesignator, targetArray)
  3626. END;
  3627. indexDesignator.parameters.AddExpression(indexListItem)
  3628. ELSE
  3629. Error(position, Diagnostics.Invalid,"invalid index list item");
  3630. END;
  3631. END AppendMathIndex;
  3632. PROCEDURE AppendIndex(position: LONGINT; index: SyntaxTree.IndexDesignator; expression: SyntaxTree.Expression; over: SyntaxTree.Type);
  3633. VAR parameters: SyntaxTree.ExpressionList;
  3634. BEGIN
  3635. parameters := index.parameters;
  3636. IF (expression.type = NIL) THEN
  3637. Error(position,Diagnostics.Invalid,"invalid index");
  3638. ELSIF IsIntegerType(expression.type.resolved) THEN
  3639. IF over IS SyntaxTree.ArrayType THEN
  3640. IndexCheck(expression,over(SyntaxTree.ArrayType).length);
  3641. ELSIF over IS SyntaxTree.StringType THEN
  3642. IndexCheck(expression,Global.NewIntegerValue(system, position, over(SyntaxTree.StringType).length));
  3643. END;
  3644. expression := NewConversion(InvalidPosition,expression,system.sizeType,NIL);
  3645. parameters.AddExpression(expression);
  3646. ELSE
  3647. Error(position,Diagnostics.Invalid,"invalid index");
  3648. END;
  3649. END AppendIndex;
  3650. (** convert an expression to math array type
  3651. if expression is of math array type: return expression itself
  3652. if expression is of array-structured object type: return an index operator call on it
  3653. e.g. if expression is 3-dim. ASOT: expression -> expression^."[]"( * , * , * )
  3654. otherwise: return invalid expression
  3655. **)
  3656. PROCEDURE ConvertToMathArray(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  3657. VAR
  3658. result: SyntaxTree.Expression;
  3659. mathArrayType: SyntaxTree.MathArrayType;
  3660. BEGIN
  3661. IF expression.type = NIL THEN
  3662. result := SyntaxTree.invalidExpression
  3663. ELSIF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3664. (* expression of math array type *)
  3665. result := expression
  3666. ELSIF IsArrayStructuredObjectType(expression.type) THEN
  3667. (* expression of array-structured object type *)
  3668. mathArrayType := MathArrayStructureOfType(expression.type);
  3669. result := NewIndexOperatorCall(InvalidPosition, expression, ListOfOpenRanges(mathArrayType.Dimensionality()), NIL)
  3670. ELSE
  3671. result := SyntaxTree.invalidExpression
  3672. END;
  3673. RETURN result
  3674. END ConvertToMathArray;
  3675. (** get an expression list containing a certain amount of open ranges, e.g. [*, *, *, *] **)
  3676. PROCEDURE ListOfOpenRanges(itemCount: LONGINT): SyntaxTree.ExpressionList;
  3677. VAR
  3678. result: SyntaxTree.ExpressionList;
  3679. i: LONGINT;
  3680. BEGIN
  3681. result := SyntaxTree.NewExpressionList();
  3682. FOR i := 1 TO itemCount DO
  3683. result.AddExpression(ResolveExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)))
  3684. END;
  3685. RETURN result
  3686. END ListOfOpenRanges;
  3687. (** create a procedure call designator for an index operator call on an array-structured object type
  3688. - use given index list as actual parameters
  3689. - if rhs parameter is not NIL: call write operator, otherwise read operator
  3690. **)
  3691. PROCEDURE NewIndexOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; indexList: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3692. VAR
  3693. operator: SyntaxTree.Operator;
  3694. expression: SyntaxTree.Expression;
  3695. actualParameters, tempList: SyntaxTree.ExpressionList;
  3696. tempMathArrayExpression: SyntaxTree.MathArrayExpression;
  3697. result, tempDesignator: SyntaxTree.Designator;
  3698. recordType: SyntaxTree.RecordType;
  3699. containsNonRange, usesPureRangeOperator, usesGeneralOperator, needsReshaping: BOOLEAN;
  3700. i, hashValue, indexListSize, indexListKind: LONGINT;
  3701. castReturnType: SyntaxTree.MathArrayType;
  3702. BEGIN
  3703. ASSERT(IsArrayStructuredObjectType(left.type));
  3704. ASSERT(left.type.resolved IS SyntaxTree.PointerType);
  3705. recordType := left.type.resolved(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3706. (* determine hash value of optimal index operator and if index list contains non-range item *)
  3707. indexListSize := indexList.Length();
  3708. indexListKind := 0;
  3709. containsNonRange := FALSE;
  3710. FOR i := 0 TO indexList.Length() - 1 DO
  3711. indexListKind := indexListKind * 2;
  3712. expression := indexList.GetExpression(i);
  3713. IF expression.type.resolved IS SyntaxTree.RangeType THEN
  3714. INC(indexListKind)
  3715. ELSE
  3716. containsNonRange := TRUE
  3717. END
  3718. END;
  3719. hashValue := IndexOperatorHash(indexListSize, indexListKind, recordType.arrayStructure.form = SyntaxTree.Tensor);
  3720. (* select applicable index operator
  3721. - try to look up optimal index operator
  3722. - if not present, use operator on ranges
  3723. - for non-tensors, use fixed-dim. operator: (RANGE, RANGE, ... RANGE)
  3724. - for tensors, use general operator: (ARRAY [*] OF RANGE)
  3725. *)
  3726. usesGeneralOperator := FALSE;
  3727. IF rhs # NIL THEN
  3728. (* write operator *)
  3729. IF hashValue = -1 THEN
  3730. operator := NIL
  3731. ELSE
  3732. operator := recordType.arrayAccessOperators.write[hashValue];
  3733. END;
  3734. IF operator = NIL THEN
  3735. usesPureRangeOperator := TRUE;
  3736. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3737. operator := recordType.arrayAccessOperators.generalWrite;
  3738. usesGeneralOperator := TRUE
  3739. ELSE
  3740. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3741. operator := recordType.arrayAccessOperators.write[hashValue];
  3742. END
  3743. END
  3744. ELSE
  3745. (* read operator *)
  3746. IF hashValue = -1 THEN
  3747. operator := NIL
  3748. ELSE
  3749. operator := recordType.arrayAccessOperators.read[hashValue];
  3750. END;
  3751. IF operator = NIL THEN
  3752. usesPureRangeOperator := TRUE;
  3753. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  3754. operator := recordType.arrayAccessOperators.generalRead;
  3755. usesGeneralOperator := TRUE
  3756. ELSE
  3757. hashValue := TwoToThePowerOf(indexListSize) - 1;
  3758. operator := recordType.arrayAccessOperators.read[hashValue];
  3759. END
  3760. END
  3761. END;
  3762. IF operator = NIL THEN
  3763. Error(position, Diagnostics.Invalid, "call of undeclared [] operator");
  3764. result := SyntaxTree.invalidDesignator;
  3765. ELSE
  3766. (* determine if reshaping is needed *)
  3767. needsReshaping := containsNonRange & usesPureRangeOperator;
  3768. (* import OCArrayBase if reshaping is needed *)
  3769. IF needsReshaping & ~arrayBaseImported THEN
  3770. ImportModule(Global.ArrayBaseName, InvalidPosition);
  3771. arrayBaseImported := TRUE
  3772. END;
  3773. (* add the index list item to the list of actual parameters
  3774. - for general operators: add a single inline array containing the index list items as parameter
  3775. - otherwise: add all index list items as individual parameters
  3776. *)
  3777. actualParameters := SyntaxTree.NewExpressionList();
  3778. IF usesGeneralOperator THEN
  3779. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3780. END;
  3781. FOR i := 0 TO indexListSize - 1 DO
  3782. expression := indexList.GetExpression(i);
  3783. IF (expression.type.resolved IS SyntaxTree.IntegerType) & needsReshaping THEN
  3784. (* convert integer to range using OCArrayBase.RangeFromInteger *)
  3785. tempList := SyntaxTree.NewExpressionList();
  3786. tempList.AddExpression(expression);
  3787. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3788. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("RangeFromInteger"));
  3789. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3790. END;
  3791. IF usesGeneralOperator THEN
  3792. tempMathArrayExpression.elements.AddExpression(expression);
  3793. ELSE
  3794. actualParameters.AddExpression(expression)
  3795. END
  3796. END;
  3797. IF usesGeneralOperator THEN
  3798. actualParameters.AddExpression(tempMathArrayExpression)
  3799. END;
  3800. IF rhs # NIL THEN
  3801. (* add actual parameter for RHS *)
  3802. IF needsReshaping THEN
  3803. (* reshape using OCArrayBase.ExpandDimensions *)
  3804. tempList := SyntaxTree.NewExpressionList();
  3805. (* source array *)
  3806. IF rhs.type.resolved IS SyntaxTree.MathArrayType THEN
  3807. tempList.AddExpression(rhs);
  3808. ELSE
  3809. (* convert scalar to one-dimensional array *)
  3810. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3811. tempMathArrayExpression.elements.AddExpression(rhs);
  3812. tempList.AddExpression(tempMathArrayExpression)
  3813. END;
  3814. (* list of kept dimensions *)
  3815. tempMathArrayExpression := SyntaxTree.NewMathArrayExpression(InvalidPosition);
  3816. FOR i := 0 TO indexListSize - 1 DO
  3817. expression := indexList.GetExpression(i);
  3818. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3819. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, FALSE)) (* insert dimension *)
  3820. ELSE
  3821. tempMathArrayExpression.elements.AddExpression(SyntaxTree.NewBooleanValue(InvalidPosition, TRUE)) (* keep dimension *)
  3822. END
  3823. END;
  3824. tempList.AddExpression(tempMathArrayExpression);
  3825. tempDesignator := SyntaxTree.NewIdentifierDesignator(InvalidPosition, Global.ArrayBaseName);
  3826. tempDesignator := SyntaxTree.NewSelectorDesignator(InvalidPosition, tempDesignator, SyntaxTree.NewIdentifier("ExpandDimensions"));
  3827. expression := ResolveExpression(SyntaxTree.NewParameterDesignator(InvalidPosition, tempDesignator, tempList));
  3828. IF expression.type.resolved IS SyntaxTree.MathArrayType THEN
  3829. (* change the base type of the returned tensor from SYSTEM.ALL to the array structure's element type *)
  3830. castReturnType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid, expression.type.scope,SyntaxTree.Tensor);
  3831. castReturnType.SetArrayBase(ArrayBase(rhs.type.resolved,MAX(LONGINT)));
  3832. expression.SetType(castReturnType);
  3833. ELSE
  3834. Error(expression.position, Diagnostics.Invalid, "problem with resolving ArrayBase.ExpandDimensions");
  3835. END;
  3836. actualParameters.AddExpression(expression)
  3837. ELSE
  3838. actualParameters.AddExpression(rhs)
  3839. END
  3840. END;
  3841. (* add dereference operator and create procedure call designator *)
  3842. ASSERT(left IS SyntaxTree.Designator);
  3843. expression := NewSymbolDesignator(InvalidPosition, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)), operator);
  3844. ASSERT(expression IS SyntaxTree.Designator);
  3845. result := NewProcedureCallDesignator(InvalidPosition, expression(SyntaxTree.Designator), actualParameters);
  3846. IF (rhs = NIL) & needsReshaping THEN
  3847. (* reshape using an additional bracket designator with zeros and open ranges at the end; e.g. designator[0, *, *, 0] *)
  3848. tempList := SyntaxTree.NewExpressionList();
  3849. FOR i := 0 TO indexList.Length() - 1 DO
  3850. expression := indexList.GetExpression(i);
  3851. IF expression.type.resolved IS SyntaxTree.IntegerType THEN
  3852. tempList.AddExpression(SyntaxTree.NewIntegerValue(InvalidPosition, 0))
  3853. ELSE
  3854. tempList.AddExpression(SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL))
  3855. END
  3856. END;
  3857. result := ResolveDesignator(SyntaxTree.NewBracketDesignator(InvalidPosition, result, tempList))
  3858. END;
  3859. IF rhs = NIL THEN
  3860. (* special rule: index read operator calls are considered to be assignable *)
  3861. result.SetAssignable(TRUE)
  3862. END;
  3863. (* put information about this index operator call into the resulting designator *)
  3864. result.SetRelatedAsot(left);
  3865. result.SetRelatedIndexList(indexList)
  3866. END;
  3867. RETURN result
  3868. END NewIndexOperatorCall;
  3869. PROCEDURE NewObjectOperatorCall*(position: LONGINT; left: SyntaxTree.Expression; oper: LONGINT; parameters: SyntaxTree.ExpressionList; rhs: SyntaxTree.Expression): SyntaxTree.Designator;
  3870. VAR type: SyntaxTree.Type; expression: SyntaxTree.Expression; op: SyntaxTree.Operator; recordType: SyntaxTree.RecordType;
  3871. actualParameters: SyntaxTree.ExpressionList; i: LONGINT; result: SyntaxTree.Designator;
  3872. PROCEDURE FindOperator(recordType: SyntaxTree.RecordType; identifier: SyntaxTree.Identifier; actualParameters: SyntaxTree.ExpressionList): SyntaxTree.Operator;
  3873. VAR bestOperator: SyntaxTree.Operator; bestDistance: LONGINT; numberParameters: LONGINT; procedureType: SyntaxTree.ProcedureType;
  3874. PROCEDURE FindInScope(scope: SyntaxTree.RecordScope; access: SET);
  3875. VAR operator: SyntaxTree.Operator; distance,i: LONGINT;
  3876. CONST trace = FALSE;
  3877. BEGIN
  3878. IF trace THEN
  3879. FOR i := 0 TO actualParameters.Length()-1 DO
  3880. Printout.Info("par", actualParameters.GetExpression(i));
  3881. END;
  3882. END;
  3883. operator := scope.firstOperator;
  3884. WHILE(operator # NIL) DO
  3885. IF (operator.name=identifier) & (operator.access * access # {}) THEN
  3886. procedureType := operator.type(SyntaxTree.ProcedureType);
  3887. distance := Distance(system, procedureType,actualParameters);
  3888. IF trace THEN Printout.Info("check op ",operator) END;
  3889. IF distance < bestDistance THEN
  3890. IF trace THEN Printout.Info("taken op",operator) END;
  3891. bestDistance := distance;
  3892. bestOperator := operator;
  3893. END;
  3894. END;
  3895. operator := operator.nextOperator;
  3896. END;
  3897. END FindInScope;
  3898. BEGIN
  3899. bestDistance := Infinity; bestOperator := NIL; numberParameters := actualParameters.Length();
  3900. IF oper = 0 THEN (* index *)
  3901. identifier := SyntaxTree.NewIdentifier("[]");
  3902. ELSE
  3903. identifier := Global.GetIdentifier(oper,currentScope.ownerModule.case);
  3904. END;
  3905. WHILE (recordType # NIL) DO
  3906. FindInScope(recordType.recordScope,SyntaxTree.ReadOnly);
  3907. recordType := recordType.GetBaseRecord();
  3908. END;
  3909. RETURN bestOperator
  3910. END FindOperator;
  3911. BEGIN
  3912. type := left.type.resolved;
  3913. IF ~(type IS SyntaxTree.PointerType) THEN RETURN NIL END;
  3914. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  3915. actualParameters := SyntaxTree.NewExpressionList();
  3916. IF parameters # NIL THEN
  3917. FOR i := 0 TO parameters.Length()-1 DO
  3918. expression := ResolveExpression(parameters.GetExpression(i));
  3919. actualParameters.AddExpression(expression);
  3920. END;
  3921. END;
  3922. IF rhs # NIL THEN actualParameters.AddExpression(rhs) END;
  3923. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3924. IF op # NIL THEN
  3925. expression := NewSymbolDesignator(position, NewDereferenceDesignator(InvalidPosition, left(SyntaxTree.Designator)) , op);
  3926. ASSERT(expression IS SyntaxTree.Designator);
  3927. result := NewProcedureCallDesignator(position, expression(SyntaxTree.Designator), actualParameters);
  3928. result.SetRelatedAsot(left);
  3929. result.SetRelatedIndexList(parameters);
  3930. (* check if write operator exists, for var parameters *)
  3931. IF (rhs = NIL) & (op.type(SyntaxTree.ProcedureType).returnType # NIL) THEN
  3932. actualParameters := SyntaxTree.NewExpressionList();
  3933. FOR i := 0 TO parameters.Length()-1 DO
  3934. expression := ResolveExpression(parameters.GetExpression(i));
  3935. actualParameters.AddExpression(expression);
  3936. END;
  3937. rhs := SyntaxTree.NewDesignator(); rhs.SetType(op.type(SyntaxTree.ProcedureType).returnType); (* only a stub to test for existence of operator *)
  3938. actualParameters.AddExpression(rhs);
  3939. op := FindOperator(recordType, SyntaxTree.NewIdentifier("[]"), actualParameters);
  3940. IF op = NIL THEN rhs := NIL END;
  3941. END;
  3942. IF rhs # NIL THEN result.SetAssignable(TRUE) END;
  3943. ELSE
  3944. result := NIL;
  3945. END;
  3946. RETURN result;
  3947. END NewObjectOperatorCall;
  3948. (** check and semantically resolve a bracket designator of the form 'left[expression, ..., expression]'
  3949. 1. convert bracket designator chains into a single one that contains separators
  3950. e.g.: left[a, b, c][d, e][f] -> left[a, b, c, |, d, e, |, f]
  3951. 2. convert single bracket designator into a chain of index- , dereference- and procedure call designators
  3952. e.g.: left[a, b, c, |, d, e, |, f] -> left^[a]^."[]"(b, c, d)[e, f]
  3953. - if an array or math array is indexed over, create index designator
  3954. a[x, |, y] -> a[x][y] (split at separator if 'x' contains range or 'a' is tensor math array)
  3955. a[x, |, y] -> a[x, y] (otherwise, combine into single one)
  3956. - if a pointer is indexed over, splitting and auto-dereferencing takes place:
  3957. a[x, y] -> a[x]^[y] (a: ARRAY OF POINTER TO ARRAY OF INTEGER)
  3958. - if an array-structured object type is indexed over, create procedure call designator
  3959. e.g.: a[x, y] -> a^."[]"(x, y)
  3960. Note 1: for math arrays, there can be a difference between a[x, y] and [y, x]:
  3961. - a[i, *] = a[i][*]
  3962. - a[*, i] # a[*][i]
  3963. Because:
  3964. - 'i-th row' = a[*][i] = a[*][i, *] = a[i, *] = a[i] = a[i][*] = a[i][*][*] = a[i][*][*][*]
  3965. - 'i-th column' = a[*, i]
  3966. Note 2: math arrays of arrays (and vice versa) are forbidden by the type system.
  3967. However, pointers are permitted: e.g. ARRAY [10] OF POINTER TO ARRAY is a valid type.
  3968. Note 3: while this compiler tries to combine multiple bracket designators into a single index designator,
  3969. older Oberon compilers did this the other way around: a[x, y, z] -> A[x][y][z].
  3970. **)
  3971. PROCEDURE VisitBracketDesignator(bracketDesignator: SyntaxTree.BracketDesignator);
  3972. VAR
  3973. leftBracketDesignator: SyntaxTree.BracketDesignator;
  3974. indexDesignator: SyntaxTree.IndexDesignator;
  3975. designator: SyntaxTree.Designator;
  3976. type: SyntaxTree.Type;
  3977. recordType: SyntaxTree.RecordType;
  3978. expression, rhs: SyntaxTree.Expression;
  3979. indexList: SyntaxTree.ExpressionList;
  3980. i: LONGINT;
  3981. hasError, done: BOOLEAN;
  3982. PROCEDURE FinalizeIndexDesignator;
  3983. BEGIN
  3984. IF indexDesignator # NIL THEN
  3985. (* the end of a tensor has been reached: *)
  3986. IF IsTensor(type) THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  3987. SetIndexBaseType(indexDesignator, type);
  3988. indexDesignator.SetType(ResolveType(indexDesignator.type));
  3989. designator := indexDesignator;
  3990. type := designator.type.resolved;
  3991. indexDesignator := NIL;
  3992. ASSERT(SyntaxTree.Resolved IN type.state)
  3993. END
  3994. END FinalizeIndexDesignator;
  3995. BEGIN
  3996. IF Trace THEN D.Str("VisitBracketDesignator"); D.Ln; END;
  3997. IF bracketDesignator.left IS SyntaxTree.BracketDesignator THEN
  3998. leftBracketDesignator := bracketDesignator.left(SyntaxTree.BracketDesignator);
  3999. (* copy all index list entries including a separator to the left bracket designator *)
  4000. leftBracketDesignator.parameters.AddExpression(SyntaxTree.indexListSeparator);
  4001. FOR i := 0 TO bracketDesignator.parameters.Length() - 1 DO
  4002. leftBracketDesignator.parameters.AddExpression(bracketDesignator.parameters.GetExpression(i))
  4003. END;
  4004. (* propagate the related RHS *)
  4005. leftBracketDesignator.SetRelatedRhs(bracketDesignator.relatedRhs); (* for 'left[a][b] := rhs;' *)
  4006. (* only resolve left bracket designator and use as final result *)
  4007. resolvedExpression := ResolveExpression(leftBracketDesignator)
  4008. ELSE
  4009. ASSERT(~(bracketDesignator.left IS SyntaxTree.BracketDesignator));
  4010. designator := ResolveDesignator(bracketDesignator.left);
  4011. type := designator.type.resolved;
  4012. indexDesignator := NIL;
  4013. (*!!! clean up *)
  4014. IF (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType) & ~IsArrayStructuredObjectType(type) THEN
  4015. resolvedExpression := NewObjectOperatorCall(bracketDesignator.position, designator, 0, bracketDesignator.parameters,bracketDesignator.relatedRhs);
  4016. IF resolvedExpression = NIL THEN
  4017. Error(bracketDesignator.position,Diagnostics.Invalid,"undefined operator");
  4018. resolvedExpression := SyntaxTree.invalidDesignator
  4019. END;
  4020. RETURN;
  4021. END;
  4022. i := 0;
  4023. WHILE i <= bracketDesignator.parameters.Length() - 1 DO
  4024. expression := bracketDesignator.parameters.GetExpression(i);
  4025. expression := ResolveExpression(expression);
  4026. bracketDesignator.parameters.SetExpression(i, expression);
  4027. IF expression = SyntaxTree.indexListSeparator THEN
  4028. (* finalize an existing index designator if needed *)
  4029. IF IsTensor(type) OR (indexDesignator # NIL) & (indexDesignator.hasRange) THEN FinalizeIndexDesignator END;
  4030. INC(i)
  4031. ELSE
  4032. (* do auto-dereferencing if needed *)
  4033. IF (type IS SyntaxTree.PointerType) & ~IsArrayStructuredObjectType(type)
  4034. (*OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) & cellsAreObjects
  4035. & (i=0)*)
  4036. THEN
  4037. (* expression of the form A[x,...] over ARRAY [...] OF POINTER TO ARRAY OF ... *)
  4038. IF (indexDesignator # NIL) & indexDesignator.hasRange THEN
  4039. Error(expression.position, Diagnostics.Invalid, "forbidden range valued indexer over pointer to array");
  4040. designator := SyntaxTree.invalidDesignator;
  4041. type := SyntaxTree.invalidType
  4042. ELSE
  4043. FinalizeIndexDesignator;
  4044. designator := NewDereferenceDesignator(bracketDesignator.position, designator);
  4045. type := designator.type.resolved
  4046. END
  4047. END;
  4048. (* create a new index designator, if needed *)
  4049. IF (indexDesignator = NIL) & ((type IS SyntaxTree.ArrayType) OR (type IS SyntaxTree.MathArrayType) OR (type IS SyntaxTree.StringType)) THEN
  4050. indexDesignator := SyntaxTree.NewIndexDesignator(bracketDesignator.position, designator);
  4051. indexDesignator.SetAssignable(designator.assignable);
  4052. indexDesignator.SetType(NIL); (* type will be re-set when index designator is finalized *)
  4053. (* designator := indexDesignator *)
  4054. END;
  4055. IF type = SyntaxTree.invalidType THEN
  4056. (* error already handled *)
  4057. INC(i)
  4058. ELSIF type IS SyntaxTree.ArrayType THEN
  4059. (* indexing over an array *)
  4060. ASSERT(indexDesignator # NIL);
  4061. AppendIndex(expression.position, indexDesignator, expression, type(SyntaxTree.ArrayType));
  4062. type := type(SyntaxTree.ArrayType).arrayBase.resolved;
  4063. INC(i)
  4064. ELSIF type IS SyntaxTree.StringType THEN
  4065. (* indexing over an array *)
  4066. ASSERT(indexDesignator # NIL);
  4067. AppendIndex(expression.position, indexDesignator, expression, type);
  4068. type := type(SyntaxTree.StringType).baseType.resolved;
  4069. INC(i)
  4070. ELSIF type IS SyntaxTree.MathArrayType THEN
  4071. (* indexing over a math array *)
  4072. ASSERT(indexDesignator # NIL);
  4073. AppendMathIndex(expression.position, indexDesignator, expression, type(SyntaxTree.MathArrayType));
  4074. IF type(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN type := type(SyntaxTree.MathArrayType).arrayBase.resolved END;
  4075. INC(i)
  4076. ELSIF IsArrayStructuredObjectType(type) THEN
  4077. (* indexing over ASOTs *)
  4078. FinalizeIndexDesignator;
  4079. ASSERT(type IS SyntaxTree.PointerType);
  4080. recordType := type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType);
  4081. (*
  4082. - collect index list items from bracket designator that belong to ASOT
  4083. - check for errors
  4084. *)
  4085. indexList := SyntaxTree.NewExpressionList();
  4086. hasError := FALSE;
  4087. IF recordType.arrayStructure.form = SyntaxTree.Tensor THEN
  4088. (* indexing over tensor ASOT:
  4089. - stop at index list end or separator
  4090. - dimensionality is given by number of index list items
  4091. *)
  4092. done := FALSE;
  4093. WHILE ~done DO
  4094. IF i > bracketDesignator.parameters.Length() - 1 THEN
  4095. done := TRUE;
  4096. ELSE
  4097. expression := bracketDesignator.parameters.GetExpression(i);
  4098. IF expression = SyntaxTree.indexListSeparator THEN
  4099. done := TRUE;
  4100. ELSE
  4101. expression := ResolveExpression(expression);
  4102. IF expression IS SyntaxTree.TensorRangeExpression THEN
  4103. Error(expression.position, Diagnostics.Invalid, "tensor range expression not supported for tensor ASOTs");
  4104. hasError := TRUE
  4105. ELSIF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4106. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4107. expression := SyntaxTree.invalidExpression;
  4108. hasError := TRUE
  4109. END;
  4110. indexList.AddExpression(expression)
  4111. END;
  4112. INC(i)
  4113. END
  4114. END
  4115. ELSE
  4116. (* indexing over non-tensor ASOT:
  4117. - ignore separators
  4118. - make sure that the number of index items matches the ASOT's dimensionality by appending open ranges ('*')
  4119. *)
  4120. WHILE indexList.Length() < recordType.arrayStructure.Dimensionality() DO
  4121. IF i <= bracketDesignator.parameters.Length() - 1 THEN
  4122. expression := bracketDesignator.parameters.GetExpression(i);
  4123. ELSE
  4124. expression := SyntaxTree.NewRangeExpression(InvalidPosition, NIL, NIL, NIL)
  4125. END;
  4126. IF expression # SyntaxTree.indexListSeparator THEN
  4127. expression := ResolveExpression(expression);
  4128. IF ~(expression.type.resolved IS SyntaxTree.IntegerType) & ~(expression.type.resolved IS SyntaxTree.RangeType) THEN
  4129. Error(expression.position, Diagnostics.Invalid, "integer or range expected");
  4130. expression := SyntaxTree.invalidExpression;
  4131. hasError := TRUE
  4132. END;
  4133. indexList.AddExpression(expression)
  4134. END;
  4135. INC(i)
  4136. END;
  4137. END;
  4138. IF hasError THEN
  4139. designator := SyntaxTree.invalidDesignator;
  4140. type := SyntaxTree.invalidType;
  4141. ELSE
  4142. (* determine if read or write mode applies: write mode applies if there is a related RHS
  4143. and the last entry in the index list belongs to the array-structured object type in question.
  4144. E.g.: for a 2-dimensional array-structured object type:
  4145. - 'lhs := asot[1, 2]' -> read mode
  4146. - 'asot[1, 2] := rhs' -> write mode
  4147. - 'asot[1, 2, 3] := rhs' -> read mode
  4148. *)
  4149. IF (bracketDesignator.relatedRhs # NIL) & (i > bracketDesignator.parameters.Length() - 1) THEN
  4150. rhs := bracketDesignator.relatedRhs
  4151. ELSE
  4152. rhs := NIL
  4153. END;
  4154. designator := NewIndexOperatorCall(bracketDesignator.position, designator, indexList, rhs);
  4155. type := designator.type
  4156. END
  4157. ELSE
  4158. Error(expression.position, Diagnostics.Invalid,"indexing over non-array type");
  4159. designator := SyntaxTree.invalidDesignator;
  4160. type := SyntaxTree.invalidType;
  4161. INC(i)
  4162. END
  4163. END
  4164. END;
  4165. IF type # SyntaxTree.invalidType THEN FinalizeIndexDesignator END;
  4166. resolvedExpression := designator
  4167. END
  4168. END VisitBracketDesignator;
  4169. (** check and resolve expression list
  4170. - resolve each expression in an expression list
  4171. - returns true if and only if all statements could have successfully been resolved
  4172. **)
  4173. PROCEDURE ExpressionList(expressionList: SyntaxTree.ExpressionList): BOOLEAN;
  4174. VAR i: LONGINT; expression: SyntaxTree.Expression; result: BOOLEAN;
  4175. BEGIN
  4176. result := TRUE;
  4177. FOR i := 0 TO expressionList.Length()-1 DO
  4178. expression := ResolveExpression(expressionList.GetExpression(i));
  4179. IF expression = SyntaxTree.invalidExpression THEN result := FALSE END;
  4180. expressionList.SetExpression(i,expression);
  4181. END;
  4182. RETURN result
  4183. END ExpressionList;
  4184. PROCEDURE CanPassInRegister*(type: SyntaxTree.Type): BOOLEAN;
  4185. BEGIN
  4186. type := type.resolved;
  4187. IF (type IS SyntaxTree.BasicType) & ~type.IsPointer() & ~type.IsComposite() THEN
  4188. RETURN TRUE
  4189. ELSIF system.CanPassInRegister # NIL THEN
  4190. RETURN system.CanPassInRegister(type);
  4191. ELSE
  4192. RETURN FALSE
  4193. END;
  4194. END CanPassInRegister;
  4195. (** return procedure call designator left(actualParameters)
  4196. - check realtime procedure call in realtime procedure
  4197. - check number of parameters
  4198. - check parameter compatibility
  4199. return invalidDesignator if error
  4200. **)
  4201. PROCEDURE NewProcedureCallDesignator(position: LONGINT; left: SyntaxTree.Designator; actualParameters:SyntaxTree.ExpressionList): SyntaxTree.Designator;
  4202. VAR result: SyntaxTree.Designator;
  4203. numberFormalParameters, numberActualParameters: LONGINT;
  4204. formalType: SyntaxTree.ProcedureType;
  4205. formalParameter: SyntaxTree.Parameter;
  4206. actualParameter: SyntaxTree.Expression;
  4207. i: LONGINT;
  4208. BEGIN
  4209. IF Trace THEN D.Str("ProcedureCallDesignator"); D.Ln; END;
  4210. result := SyntaxTree.invalidDesignator;
  4211. formalType := left.type.resolved(SyntaxTree.ProcedureType); (* type checked in VisitParameterDesignator *)
  4212. numberFormalParameters := formalType.numberParameters;
  4213. numberActualParameters := actualParameters.Length();
  4214. IF (currentIsRealtime) & ~(formalType.isRealtime) THEN
  4215. Error(position,Diagnostics.Invalid,"forbidden call of non-realtime procedure in realtime block");
  4216. END;
  4217. IF ~ExpressionList(actualParameters) THEN
  4218. result := SyntaxTree.invalidDesignator
  4219. ELSE
  4220. IF numberActualParameters <= numberFormalParameters THEN
  4221. formalParameter := formalType.firstParameter;
  4222. FOR i := 0 TO numberActualParameters-1 DO
  4223. actualParameter := actualParameters.GetExpression(i);
  4224. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4225. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4226. ELSIF (currentIsRealtime) & ~actualParameter.type.resolved.isRealtime THEN
  4227. Error(position,Diagnostics.Invalid,"non-realtime actual parameter in context of realtime procedure");
  4228. ELSE
  4229. IF ~formalParameter.type.SameType(actualParameter.type.resolved) THEN
  4230. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4231. END;
  4232. actualParameters.SetExpression(i,actualParameter);
  4233. END;
  4234. formalParameter := formalParameter.nextParameter;
  4235. END;
  4236. WHILE (formalParameter # NIL) DO
  4237. IF formalParameter.defaultValue # NIL THEN
  4238. actualParameters.AddExpression(formalParameter.defaultValue);
  4239. formalParameter := formalParameter.nextParameter
  4240. ELSE
  4241. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4242. formalParameter := NIL;
  4243. END;
  4244. END;
  4245. ELSE
  4246. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4247. END;
  4248. result := SyntaxTree.NewProcedureCallDesignator(position,left,actualParameters);
  4249. result.SetAssignable(FALSE);
  4250. result.SetType(left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4251. END;
  4252. RETURN result
  4253. END NewProcedureCallDesignator;
  4254. (**
  4255. builtin call designator generated in VisitParameterDesignator
  4256. -> nothing to be resolved
  4257. **)
  4258. PROCEDURE VisitTypeGuardDesignator(x: SyntaxTree.TypeGuardDesignator);
  4259. BEGIN
  4260. resolvedExpression := x;
  4261. END VisitTypeGuardDesignator;
  4262. (**
  4263. builtin call designator generated in VisitParameterDesignator
  4264. -> nothing to be resolved
  4265. **)
  4266. PROCEDURE VisitBuiltinCallDesignator(x: SyntaxTree.BuiltinCallDesignator);
  4267. BEGIN
  4268. IF (x.returnType # NIL) & ExpressionList(x.parameters) THEN
  4269. resolvedExpression := NewBuiltinCallDesignator(x.position,NIL, x.parameters,NIL, ResolveType(x.returnType));
  4270. ASSERT(resolvedExpression.type # NIL);
  4271. ELSIF ExpressionList(x.parameters) THEN
  4272. resolvedExpression := x;
  4273. END;
  4274. END VisitBuiltinCallDesignator;
  4275. (**
  4276. procedure call designator generated in VisitParameterDesignator
  4277. -> nothing to be resolved
  4278. **)
  4279. PROCEDURE VisitProcedureCallDesignator(x: SyntaxTree.ProcedureCallDesignator);
  4280. BEGIN
  4281. x.SetType(x.left.type.resolved(SyntaxTree.ProcedureType).returnType);
  4282. resolvedExpression := x;
  4283. END VisitProcedureCallDesignator;
  4284. (** return true if x is a variable else return false and report error **)
  4285. PROCEDURE CheckVariable(x: SyntaxTree.Expression): BOOLEAN;
  4286. VAR result: BOOLEAN;
  4287. BEGIN
  4288. result := TRUE;
  4289. IF x = SyntaxTree.invalidExpression THEN
  4290. result := FALSE;
  4291. ELSIF ~IsVariable(x) THEN
  4292. Error(x.position,Diagnostics.Invalid,"non variable expression");
  4293. IF VerboseErrorMessage THEN Printout.Info("non variable",x) END;
  4294. result := FALSE;
  4295. END;
  4296. RETURN result
  4297. END CheckVariable;
  4298. (**
  4299. if expression x is of basic type then return true else report error and return false
  4300. **)
  4301. PROCEDURE CheckBasicType(x: SyntaxTree.Expression): BOOLEAN;
  4302. VAR result: BOOLEAN;
  4303. BEGIN
  4304. result := FALSE;
  4305. IF x = SyntaxTree.invalidExpression THEN
  4306. ELSIF ~IsBasicType(x.type) THEN
  4307. Error(x.position,Diagnostics.Invalid,"is no basic type");
  4308. result := FALSE
  4309. ELSE result := TRUE
  4310. END;
  4311. RETURN result
  4312. END CheckBasicType;
  4313. (**
  4314. if expression x is of number type then return true else report error and return false
  4315. **)
  4316. PROCEDURE CheckNumberType(x: SyntaxTree.Expression): BOOLEAN;
  4317. VAR result: BOOLEAN;
  4318. BEGIN
  4319. result := FALSE;
  4320. IF x = SyntaxTree.invalidExpression THEN
  4321. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) THEN
  4322. Error(x.position,Diagnostics.Invalid,"is non number type");
  4323. ELSE result := TRUE
  4324. END;
  4325. RETURN result
  4326. END CheckNumberType;
  4327. (**
  4328. if expression x is of number or size type but not complex then return true else report error and return false
  4329. **)
  4330. PROCEDURE CheckNonComplexNumberSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4331. VAR result: BOOLEAN;
  4332. BEGIN
  4333. result := FALSE;
  4334. IF x = SyntaxTree.invalidExpression THEN
  4335. ELSIF x.type.resolved IS SyntaxTree.ComplexType THEN
  4336. Error(x.position,Diagnostics.Invalid,"is complex type");
  4337. ELSIF ~(x.type.resolved IS SyntaxTree.NumberType) & ~(x.type.resolved IS SyntaxTree.SizeType) THEN
  4338. Error(x.position,Diagnostics.Invalid,"is non number type");
  4339. ELSE result := TRUE
  4340. END;
  4341. RETURN result
  4342. END CheckNonComplexNumberSizeType;
  4343. PROCEDURE CheckAddressType(x: SyntaxTree.Expression): BOOLEAN;
  4344. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4345. BEGIN
  4346. result := FALSE; type := x.type.resolved;
  4347. IF x = SyntaxTree.invalidExpression THEN
  4348. ELSIF ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.addressType.sizeInBits)) & ~IsAddressValue(x) & ~IsUnsafePointer(type) THEN
  4349. TRACE(type.sizeInBits);
  4350. TRACE(system.addressType.sizeInBits);
  4351. Error(x.position,Diagnostics.Invalid,"is no address type");
  4352. ELSE result := TRUE
  4353. END;
  4354. RETURN result
  4355. END CheckAddressType;
  4356. PROCEDURE CheckSizeType(x: SyntaxTree.Expression): BOOLEAN;
  4357. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4358. BEGIN
  4359. result := FALSE; type := x.type.resolved;
  4360. IF x = SyntaxTree.invalidExpression THEN
  4361. ELSIF ~(type IS SyntaxTree.SizeType) & ~( (type IS SyntaxTree.IntegerType) & (type.sizeInBits <= system.sizeType.sizeInBits)) THEN
  4362. Error(x.position,Diagnostics.Invalid,"is no size type");
  4363. ELSE result := TRUE
  4364. END;
  4365. RETURN result
  4366. END CheckSizeType;
  4367. PROCEDURE CheckObjectType(x: SyntaxTree.Expression): BOOLEAN;
  4368. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4369. BEGIN
  4370. result := FALSE; type := x.type.resolved;
  4371. IF x = SyntaxTree.invalidExpression THEN
  4372. ELSIF ~(type IS SyntaxTree.NilType) & ~(type IS SyntaxTree.ObjectType) & (~(type IS SyntaxTree.PointerType) OR ~(type(SyntaxTree.PointerType).pointerBase IS SyntaxTree.RecordType) OR ~type(SyntaxTree.PointerType).pointerBase(SyntaxTree.RecordType).isObject) THEN
  4373. Error(x.position,Diagnostics.Invalid,"is no object type");
  4374. ELSE result := TRUE
  4375. END;
  4376. RETURN result
  4377. END CheckObjectType;
  4378. (**
  4379. if expression x is of integer type then return true else report error and return false
  4380. **)
  4381. PROCEDURE CheckIntegerType(x: SyntaxTree.Expression): BOOLEAN;
  4382. VAR result: BOOLEAN; type: SyntaxTree.Type;
  4383. BEGIN
  4384. result := FALSE; type := x.type.resolved;
  4385. IF x = SyntaxTree.invalidExpression THEN
  4386. ELSIF ~(type IS SyntaxTree.IntegerType) & ~(type IS SyntaxTree.ByteType) & ~(type IS SyntaxTree.AddressType) & ~(type IS SyntaxTree.SizeType) THEN
  4387. Error(x.position,Diagnostics.Invalid,"is no integer type");
  4388. ELSE result := TRUE
  4389. END;
  4390. RETURN result
  4391. END CheckIntegerType;
  4392. (**
  4393. if expression x is of character type then return true else report error and return false
  4394. **)
  4395. PROCEDURE CheckCharacterType(x: SyntaxTree.Expression): BOOLEAN;
  4396. VAR result: BOOLEAN;
  4397. BEGIN
  4398. result := FALSE;
  4399. IF x = SyntaxTree.invalidExpression THEN
  4400. ELSIF ~(x.type.resolved IS SyntaxTree.CharacterType) & ~(x.type.resolved IS SyntaxTree.ByteType) & ~IsCharacterType(x.type.resolved) THEN
  4401. Error(x.position,Diagnostics.Invalid,"is no character type");
  4402. ELSE result := TRUE
  4403. END;
  4404. RETURN result
  4405. END CheckCharacterType;
  4406. (**
  4407. if expression x is of real type then return true else report error and return false
  4408. **)
  4409. PROCEDURE CheckRealType(x: SyntaxTree.Expression): BOOLEAN;
  4410. VAR result: BOOLEAN;
  4411. BEGIN
  4412. result := FALSE;
  4413. IF x = SyntaxTree.invalidExpression THEN
  4414. ELSIF ~(x.type.resolved IS SyntaxTree.FloatType) THEN
  4415. Error(x.position,Diagnostics.Invalid,"is no float type");
  4416. ELSE result := TRUE
  4417. END;
  4418. RETURN result
  4419. END CheckRealType;
  4420. (**
  4421. if expression x is of range type then return true else report error and return false
  4422. **)
  4423. PROCEDURE CheckRangeType(x: SyntaxTree.Expression): BOOLEAN;
  4424. VAR result: BOOLEAN;
  4425. BEGIN
  4426. result := FALSE;
  4427. IF x = SyntaxTree.invalidExpression THEN
  4428. ELSIF ~(x.type.resolved IS SyntaxTree.RangeType) THEN
  4429. Error(x.position,Diagnostics.Invalid,"is no range type");
  4430. ELSE result := TRUE
  4431. END;
  4432. RETURN result
  4433. END CheckRangeType;
  4434. (**
  4435. if expression x is of boolean type then return true else report error and return false
  4436. **)
  4437. PROCEDURE CheckBooleanType(x: SyntaxTree.Expression): BOOLEAN;
  4438. VAR result: BOOLEAN;
  4439. BEGIN
  4440. result := FALSE;
  4441. IF x = SyntaxTree.invalidExpression THEN
  4442. ELSIF ~(x.type.resolved IS SyntaxTree.BooleanType) THEN
  4443. Error(x.position,Diagnostics.Invalid,"is no boolean type");
  4444. ELSE result := TRUE
  4445. END;
  4446. RETURN result
  4447. END CheckBooleanType;
  4448. (**
  4449. if expression x is of set type then return true else report error and return false
  4450. **)
  4451. PROCEDURE CheckSetType(x: SyntaxTree.Expression): BOOLEAN;
  4452. VAR result: BOOLEAN;
  4453. BEGIN
  4454. result := FALSE;
  4455. IF x = SyntaxTree.invalidExpression THEN
  4456. ELSIF ~(x.type.resolved IS SyntaxTree.SetType) THEN
  4457. Error(x.position,Diagnostics.Invalid,"is no set type");
  4458. ELSE result := TRUE
  4459. END;
  4460. RETURN result
  4461. END CheckSetType;
  4462. (**
  4463. if expression x is of string or array of character type then return true else report error and return false
  4464. **)
  4465. PROCEDURE CheckStringType(x: SyntaxTree.Expression): BOOLEAN;
  4466. VAR result: BOOLEAN;
  4467. BEGIN
  4468. result := FALSE;
  4469. IF x = SyntaxTree.invalidExpression THEN
  4470. ELSIF ~IsStringType(x.type.resolved) THEN
  4471. Error(x.position,Diagnostics.Invalid,"is no string type");
  4472. ELSE result := TRUE
  4473. END;
  4474. RETURN result
  4475. END CheckStringType;
  4476. (**
  4477. if expression x is a type declaration type return true else report error and return false
  4478. **)
  4479. PROCEDURE CheckTypeDeclarationType(x: SyntaxTree.Expression): BOOLEAN;
  4480. VAR result: BOOLEAN;
  4481. BEGIN
  4482. result := FALSE;
  4483. IF x = SyntaxTree.invalidExpression THEN
  4484. ELSIF (x.type.resolved # SyntaxTree.typeDeclarationType) THEN
  4485. Error(x.position,Diagnostics.Invalid,"is not a type declaration");
  4486. ELSE result := TRUE
  4487. END;
  4488. RETURN result
  4489. END CheckTypeDeclarationType;
  4490. PROCEDURE CheckIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4491. VAR result: BOOLEAN;
  4492. BEGIN
  4493. result := FALSE;
  4494. IF x = SyntaxTree.invalidExpression THEN
  4495. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4496. result := TRUE;
  4497. value := x.resolved(SyntaxTree.IntegerValue).value;
  4498. ELSE
  4499. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4500. END;
  4501. RETURN result;
  4502. END CheckIntegerValue;
  4503. PROCEDURE CheckStringValue(x: SyntaxTree.Expression; VAR value: ARRAY OF CHAR): BOOLEAN;
  4504. VAR result: BOOLEAN;
  4505. BEGIN
  4506. result := FALSE;
  4507. IF x = SyntaxTree.invalidExpression THEN
  4508. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  4509. result := TRUE;
  4510. COPY(x.resolved(SyntaxTree.StringValue).value^, value);
  4511. ELSE
  4512. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4513. END;
  4514. RETURN result;
  4515. END CheckStringValue;
  4516. PROCEDURE IsUnsignedValue(x: SyntaxTree.Expression; maxSizeInBits: LONGINT): BOOLEAN;
  4517. BEGIN
  4518. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4519. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, maxSizeInBits)
  4520. ELSE
  4521. RETURN FALSE
  4522. END;
  4523. END IsUnsignedValue;
  4524. PROCEDURE IsAddressValue(x: SyntaxTree.Expression): BOOLEAN;
  4525. BEGIN
  4526. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4527. RETURN Global.IsUnsignedInteger(x.resolved(SyntaxTree.IntegerValue).hvalue, system.addressType.sizeInBits)
  4528. ELSE
  4529. RETURN FALSE
  4530. END
  4531. END IsAddressValue;
  4532. PROCEDURE IsAddressExpression(x: SyntaxTree.Expression): BOOLEAN;
  4533. BEGIN
  4534. RETURN IsAddressType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4535. END IsAddressExpression;
  4536. PROCEDURE IsSizeExpression(x: SyntaxTree.Expression): BOOLEAN;
  4537. BEGIN
  4538. RETURN IsSizeType(x.type.resolved, system.addressSize) OR IsAddressValue(x)
  4539. END IsSizeExpression;
  4540. PROCEDURE CheckEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  4541. VAR result: BOOLEAN;
  4542. BEGIN
  4543. result := FALSE;
  4544. IF x = SyntaxTree.invalidExpression THEN
  4545. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  4546. result := TRUE;
  4547. value := x.resolved(SyntaxTree.EnumerationValue).value;
  4548. ELSE
  4549. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4550. END;
  4551. RETURN result;
  4552. END CheckEnumerationValue;
  4553. PROCEDURE CheckCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  4554. VAR result: BOOLEAN;
  4555. BEGIN
  4556. result := FALSE;
  4557. IF x = SyntaxTree.invalidExpression THEN
  4558. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  4559. result := TRUE;
  4560. value := x.resolved(SyntaxTree.CharacterValue).value;
  4561. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) & (x.resolved(SyntaxTree.StringValue).length =2) THEN
  4562. result := TRUE;
  4563. value := x.resolved(SyntaxTree.StringValue).value[0];
  4564. ELSE
  4565. Error(x.position,Diagnostics.Invalid,"expression is not a character constant");
  4566. END;
  4567. RETURN result;
  4568. END CheckCharacterValue;
  4569. PROCEDURE CheckPositiveIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT; includeZero: BOOLEAN): BOOLEAN;
  4570. VAR result: BOOLEAN;
  4571. BEGIN
  4572. result := FALSE;
  4573. IF x = SyntaxTree.invalidExpression THEN
  4574. ELSIF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  4575. value := x.resolved(SyntaxTree.IntegerValue).value;
  4576. IF (value > 0) OR includeZero & (value = 0) THEN
  4577. result := TRUE;
  4578. ELSE
  4579. Error(x.position,Diagnostics.Invalid,"integer is not positive");
  4580. END
  4581. ELSE
  4582. Error(x.position,Diagnostics.Invalid,"expression is not an integer constant");
  4583. END;
  4584. RETURN result;
  4585. END CheckPositiveIntegerValue;
  4586. PROCEDURE CheckPortType(x: SyntaxTree.Expression; VAR portType: SyntaxTree.PortType): BOOLEAN;
  4587. VAR type: SyntaxTree.Type; result: BOOLEAN;
  4588. BEGIN
  4589. result := FALSE;
  4590. IF x = SyntaxTree.invalidExpression THEN
  4591. ELSE
  4592. type := x.type.resolved;
  4593. IF (type # NIL) & (type IS SyntaxTree.PortType) THEN
  4594. portType := type(SyntaxTree.PortType);
  4595. result := TRUE
  4596. ELSE
  4597. Error(x.position,Diagnostics.Invalid,"no port type");
  4598. END;
  4599. END;
  4600. RETURN result
  4601. END CheckPortType;
  4602. (* move to builtin procedure call statement ?
  4603. remove builtin procedure call designator ?
  4604. *)
  4605. PROCEDURE NewBuiltinCallDesignator(position: LONGINT; builtin: SyntaxTree.Builtin; actualParameters:SyntaxTree.ExpressionList; left: SyntaxTree.Designator; returnType: SyntaxTree.Type): SyntaxTree.Expression;
  4606. VAR
  4607. numberActualParameters,numberFormalParameters: LONGINT;
  4608. formalParameter: SyntaxTree.Parameter;
  4609. actualParameter: SyntaxTree.Expression;
  4610. procedureType: SyntaxTree.ProcedureType;
  4611. parameter0, parameter1, parameter2, result: SyntaxTree.Expression;
  4612. inPort, outPort: SyntaxTree.PortType;
  4613. constructor: SyntaxTree.Procedure;
  4614. type0,type1,type2: SyntaxTree.Type;
  4615. type,base,parameterType: SyntaxTree.Type;
  4616. arrayType: SyntaxTree.ArrayType;
  4617. i,i0,i1: LONGINT;
  4618. r,r0,r1,im: LONGREAL;
  4619. c: CHAR;
  4620. id: LONGINT;
  4621. b: BOOLEAN;
  4622. first: LONGINT;
  4623. mathArrayType: SyntaxTree.MathArrayType;
  4624. customBuiltin: SyntaxTree.CustomBuiltin;
  4625. PROCEDURE CheckArity(from,to: LONGINT): BOOLEAN;
  4626. VAR resultB: BOOLEAN;
  4627. BEGIN
  4628. IF numberActualParameters < from THEN
  4629. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4630. result := SyntaxTree.invalidExpression;
  4631. resultB := FALSE;
  4632. ELSIF numberActualParameters > to THEN
  4633. Error(position,Diagnostics.Invalid,"more actual than formal parameters");
  4634. result := SyntaxTree.invalidExpression;
  4635. resultB := FALSE;
  4636. ELSE
  4637. resultB := TRUE;
  4638. END;
  4639. RETURN resultB
  4640. END CheckArity;
  4641. BEGIN
  4642. type := NIL; result := NIL;
  4643. type0 := NIL; type1 := NIL; type2 := NIL;
  4644. numberActualParameters := actualParameters.Length();
  4645. IF numberActualParameters>0 THEN
  4646. parameter0 := actualParameters.GetExpression(0);
  4647. IF parameter0.type # NIL THEN type0 := parameter0.type.resolved ELSE
  4648. Error(parameter0.position,Diagnostics.Invalid,"forbidden type-less argument");
  4649. result := SyntaxTree.invalidExpression
  4650. END
  4651. END;
  4652. IF numberActualParameters >1 THEN
  4653. parameter1 := actualParameters.GetExpression(1);
  4654. IF parameter1.type # NIL THEN type1 := parameter1.type.resolved
  4655. ELSE
  4656. Error(parameter1.position,Diagnostics.Invalid,"forbidden type-less argument");
  4657. result := SyntaxTree.invalidExpression
  4658. END
  4659. END;
  4660. IF numberActualParameters >2 THEN
  4661. parameter2 := actualParameters.GetExpression(2);
  4662. IF parameter2.type # NIL THEN type2 := parameter2.type.resolved
  4663. ELSE
  4664. Error(parameter2.position,Diagnostics.Invalid,"forbidden type-less argument");
  4665. result := SyntaxTree.invalidExpression
  4666. END
  4667. END;
  4668. IF returnType # NIL THEN
  4669. id := Global.New;
  4670. result := NIL;
  4671. ELSE
  4672. id := builtin.id;
  4673. IF system.operatorDefined[id] THEN (* try to find overloaded operator *)
  4674. result := NewOperatorCall(position,builtin.id,parameter0,parameter1,NIL);
  4675. END;
  4676. END;
  4677. IF result = SyntaxTree.invalidExpression THEN (* error already handled *)
  4678. ELSIF result # NIL THEN type := result.type (* operator *)
  4679. ELSE
  4680. result := SyntaxTree.NewBuiltinCallDesignator(position,id,left,actualParameters);
  4681. result(SyntaxTree.Designator).SetLeft(left);
  4682. IF returnType # NIL THEN
  4683. type := returnType;
  4684. END;
  4685. (* ---- ASSERT ----- *)
  4686. IF (id = Global.Assert) & CheckArity(1,2) THEN
  4687. IF CheckBooleanType(parameter0) THEN
  4688. (* mk: Commented this out because Oberon 07 uses Assert(FALSE, trap) instead of HALT
  4689. fof: commented in again as ASSERT is crucial for compilation tests, Oberon07 obviously needs a HALT statement
  4690. misusing ASSERT does not make the language clearer nor odes it make the compiler simpler!
  4691. *)
  4692. IF IsBooleanValue(parameter0,b) & ~b & ~(currentIsUnreachable) THEN
  4693. Error(position,Diagnostics.Invalid,"assert failed");
  4694. END;
  4695. IF (numberActualParameters > 1) & CheckIntegerValue(parameter1,i1) THEN
  4696. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4697. rules imposed by the architecture / current runtime
  4698. *)
  4699. END;
  4700. END;
  4701. (* ---- COPY ----- *)
  4702. ELSIF (id = Global.Copy) & CheckArity(2,2) THEN
  4703. IF~IsStringType(type0) THEN
  4704. Error(parameter0.position,Diagnostics.Invalid,"no string type");
  4705. END;
  4706. IF ~IsStringType(type1) THEN
  4707. Error(parameter1.position,Diagnostics.Invalid,"no string type");
  4708. ELSIF CheckVariable(parameter1) THEN
  4709. IF (type0 IS SyntaxTree.StringType) THEN
  4710. arrayType := type1(SyntaxTree.ArrayType);
  4711. IF arrayType.form = SyntaxTree.Static THEN
  4712. IF arrayType.staticLength < type0(SyntaxTree.StringType).length THEN
  4713. Error(position,Diagnostics.Invalid,"destination length smaller than source length")
  4714. END;
  4715. END;
  4716. END;
  4717. END;
  4718. (* ---- INC, DEC----- *)
  4719. ELSIF ((id = Global.Dec) OR (id = Global.Inc)) & CheckArity(1,2) THEN
  4720. IF numberActualParameters = 1 THEN
  4721. parameter1 :=Global.NewIntegerValue(system,position,1);
  4722. actualParameters.AddExpression(parameter1);
  4723. END;
  4724. IF CheckVariable(parameter0) & CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4725. IF ~CompatibleTo(system,parameter1.type,parameter0.type) THEN
  4726. Error(position,Diagnostics.Invalid,"incompatible increment");
  4727. ELSE
  4728. parameter1 := NewConversion(0,parameter1,parameter0.type,NIL);
  4729. actualParameters.SetExpression(1,parameter1);
  4730. END;
  4731. END;
  4732. (* ---- EXCL, INCL----- *)
  4733. ELSIF ((id = Global.Excl) OR (id = Global.Incl)) & CheckArity(2,2) THEN
  4734. IF CheckVariable(parameter0) & CheckSetType(parameter0) & CheckSizeType(parameter1) THEN
  4735. IF IsIntegerValue(parameter1,i0) THEN
  4736. IF (i0 < 0) OR (i0>= system.setType.sizeInBits) THEN
  4737. Error(position,Diagnostics.Invalid,"parameter out of SET range")
  4738. END;
  4739. END;
  4740. parameter1 := NewConversion(0,parameter1,system.longintType,NIL);
  4741. actualParameters.SetExpression(1,parameter1);
  4742. END;
  4743. (* ---- HALT, SYSTEM.HALT ----- *)
  4744. ELSIF ((id = Global.Halt) OR (id = Global.systemHalt)) & CheckArity(1,1) THEN
  4745. IF CheckPositiveIntegerValue(parameter0,i0,FALSE) THEN
  4746. (* modified: any integer parameter value is allowed, it is in the responsibility of the programmer to adhere to
  4747. rules imposed by the architecture / current runtime
  4748. *)
  4749. END;
  4750. (* ---- WAIT ----- *)
  4751. ELSIF cooperative & (id = Global.Wait) & CheckArity(1,1) THEN
  4752. IF CheckObjectType(parameter0) THEN
  4753. END;
  4754. (* ---- NEW ----- *)
  4755. ELSIF (id = Global.New) THEN
  4756. IF returnType # NIL THEN
  4757. first := 0; type2 := type1; type1 := type0 ; type0:= returnType.resolved;
  4758. ELSE
  4759. first := 1;
  4760. END;
  4761. IF CheckArity(first,Infinity) THEN
  4762. IF currentIsRealtime THEN
  4763. Error(position,Diagnostics.Invalid,"forbidden new in realtime block");
  4764. END;
  4765. (* check constructor *)
  4766. IF (first =0) OR CheckVariable(parameter0) THEN
  4767. IF type0 IS SyntaxTree.PointerType THEN
  4768. type0 := type0(SyntaxTree.PointerType).pointerBase.resolved;
  4769. ELSIF type0 IS SyntaxTree.CellType THEN
  4770. ELSE
  4771. Error(position, Diagnostics.Invalid, "forbidden new on value type");
  4772. END;
  4773. IF type0 IS SyntaxTree.ArrayType THEN
  4774. arrayType := type0(SyntaxTree.ArrayType);
  4775. IF arrayType.form = SyntaxTree.Static THEN
  4776. i := first
  4777. ELSIF arrayType.form = SyntaxTree.Open THEN
  4778. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4779. ELSE HALT(100)
  4780. END;
  4781. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4782. i := first;
  4783. REPEAT
  4784. actualParameter := actualParameters.GetExpression(i);
  4785. IF CheckSizeType(actualParameter) THEN
  4786. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4787. actualParameters.SetExpression(i,actualParameter);
  4788. END;
  4789. INC(i);
  4790. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4791. END;
  4792. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4793. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4794. IF constructor = NIL THEN
  4795. IF CheckArity(first,first) THEN END;
  4796. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4797. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4798. ELSE
  4799. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4800. numberFormalParameters := procedureType.numberParameters;
  4801. IF numberActualParameters-first <= numberFormalParameters THEN
  4802. formalParameter := procedureType.firstParameter;
  4803. FOR i := first TO numberActualParameters-1 DO
  4804. actualParameter := actualParameters.GetExpression(i);
  4805. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4806. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4807. ELSE
  4808. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4809. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4810. END;
  4811. actualParameters.SetExpression(i,actualParameter);
  4812. END;
  4813. formalParameter := formalParameter.nextParameter;
  4814. END;
  4815. WHILE (formalParameter # NIL) DO
  4816. IF formalParameter.defaultValue # NIL THEN
  4817. actualParameters.AddExpression(formalParameter.defaultValue);
  4818. formalParameter := formalParameter.nextParameter
  4819. ELSE
  4820. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4821. formalParameter := NIL;
  4822. END;
  4823. END;
  4824. ELSE
  4825. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4826. END;
  4827. END;
  4828. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4829. mathArrayType := type0(SyntaxTree.MathArrayType);
  4830. IF mathArrayType.form = SyntaxTree.Static THEN
  4831. Error(position,Diagnostics.Invalid,"new on static array");
  4832. ELSE
  4833. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4834. i0 := first+1; i1 := Infinity;
  4835. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4836. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4837. i1 := i0;
  4838. ELSE HALT(100);
  4839. END;
  4840. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4841. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4842. base := ArrayBase(type0,MAX(LONGINT));
  4843. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4844. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4845. IF ~CompatibleTo(system,type0,parameterType) THEN
  4846. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4847. result := SyntaxTree.invalidExpression;
  4848. ELSE
  4849. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4850. END;
  4851. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4852. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4853. IF ~CompatibleTo(system,type1,parameterType) THEN
  4854. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4855. result := SyntaxTree.invalidExpression;
  4856. ELSE
  4857. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4858. END;
  4859. ELSE
  4860. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4861. i := first;
  4862. REPEAT
  4863. actualParameter := actualParameters.GetExpression(i);
  4864. IF CheckSizeType(actualParameter) THEN
  4865. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4866. actualParameters.SetExpression(i,actualParameter);
  4867. END;
  4868. INC(i);
  4869. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4870. END;
  4871. END;
  4872. END;
  4873. ELSIF type0 IS SyntaxTree.CellType THEN
  4874. IF ~(currentIsCellNet) THEN
  4875. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4876. ELSE
  4877. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4878. IF (constructor = NIL) & CheckArity(1,1) THEN
  4879. (* ok *)
  4880. ELSE
  4881. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4882. numberFormalParameters := procedureType.numberParameters;
  4883. DEC(numberActualParameters);
  4884. IF numberActualParameters <= numberFormalParameters THEN
  4885. formalParameter := procedureType.firstParameter;
  4886. FOR i := first TO numberActualParameters DO
  4887. actualParameter := actualParameters.GetExpression(i);
  4888. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4889. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4890. ELSE
  4891. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4892. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4893. END;
  4894. actualParameters.SetExpression(i,actualParameter);
  4895. END;
  4896. formalParameter := formalParameter.nextParameter;
  4897. END;
  4898. WHILE (formalParameter # NIL) DO
  4899. IF formalParameter.defaultValue # NIL THEN
  4900. actualParameters.AddExpression(formalParameter.defaultValue);
  4901. formalParameter := formalParameter.nextParameter
  4902. ELSE
  4903. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4904. formalParameter := NIL;
  4905. END;
  4906. END;
  4907. ELSE
  4908. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4909. END;
  4910. END;
  4911. END;
  4912. activeCellsStatement := TRUE;
  4913. ELSE
  4914. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4915. END;
  4916. END;
  4917. END;
  4918. (* ---- DISPOSE ----- *)
  4919. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4920. IF ~IsPointerType(parameter0.type) THEN
  4921. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4922. ELSIF ~IsDisposable(parameter0.type) THEN
  4923. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4924. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4925. END
  4926. (* ---- GETPROCEDURE ----- *)
  4927. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4928. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4929. IF CheckVariable(parameter2) THEN
  4930. IF ~GetProcedureAllowed(parameter2.type) THEN
  4931. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4932. END;
  4933. END;
  4934. END;
  4935. (* ---- ABS ----- *)
  4936. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4937. (* note: ABS on complex numbers is done using overloading *)
  4938. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4939. type := type0;
  4940. IF IsIntegerValue(parameter0,i0) THEN
  4941. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4942. type := Global.GetIntegerType(system,ABS(i0));
  4943. ELSIF IsRealValue(parameter0,r) THEN
  4944. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4945. END;
  4946. ELSE
  4947. type := SyntaxTree.invalidType;
  4948. END;
  4949. (* ---- ASH, ASR ----- *)
  4950. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4951. type := type0;
  4952. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4953. (*
  4954. ConvertOperands(parameter0,parameter1); (* same type *)
  4955. *)
  4956. type := parameter0.type;
  4957. IF IsIntegerValue(parameter0,i0) THEN
  4958. IF IsIntegerValue(parameter1,i1) THEN
  4959. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4960. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4961. result := ResolveExpression(result);
  4962. type := Global.GetIntegerType(system,i0);
  4963. END;
  4964. END;
  4965. IF type.resolved.sizeInBits < 32 THEN
  4966. type := system.longintType;
  4967. END;
  4968. (*!compatibility with release, remove when resolved
  4969. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4970. *)
  4971. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4972. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4973. actualParameters.SetExpression(0,parameter0);
  4974. actualParameters.SetExpression(1,parameter1);
  4975. END;
  4976. (* ---- CAP ----- *)
  4977. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4978. type := system.characterType;
  4979. IF CheckCharacterType (parameter0) THEN
  4980. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4981. actualParameters.SetExpression(0,parameter0);
  4982. IF IsCharacterValue(parameter0,c) THEN
  4983. IF (c <= "z") & (c >= "a") THEN
  4984. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4985. ELSE
  4986. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4987. END;
  4988. END;
  4989. END;
  4990. (* ---- CHR ----- *)
  4991. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4992. IF id = Global.Chr THEN
  4993. type := system.characterType
  4994. ELSE
  4995. type := system.characterType32
  4996. END;
  4997. IF CheckIntegerType(parameter0) THEN
  4998. IF IsIntegerValue(parameter0,i0) THEN
  4999. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  5000. result := ResolveExpression(result);
  5001. ELSE
  5002. (*
  5003. result := NewConversion(parameter0.position,parameter0,type);
  5004. *)
  5005. END;
  5006. END
  5007. (* ---- ENTIER ----- *)
  5008. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5009. type := system.longintType;
  5010. IF CheckRealType(parameter0) THEN
  5011. IF IsRealValue(parameter0,r) THEN
  5012. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5013. type := Global.GetIntegerType(system,ENTIER(r));
  5014. END
  5015. END;
  5016. (* ---- ENTIERH ----- *)
  5017. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5018. type := system.hugeintType;
  5019. IF CheckRealType(parameter0) THEN
  5020. IF IsRealValue(parameter0,r) THEN
  5021. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5022. END
  5023. END;
  5024. (* ---- LEN ----- *)
  5025. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5026. type := system.longintType;
  5027. base := type0;
  5028. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5029. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5030. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5031. END;
  5032. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5033. actualParameters.SetExpression(0,parameter0);
  5034. type0 := parameter0.type.resolved;
  5035. base := type0;
  5036. END;
  5037. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5038. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5039. IF i1 < 0 THEN
  5040. Error(position,Diagnostics.Invalid,"invalid dimension");
  5041. base := SyntaxTree.invalidType;
  5042. ELSE
  5043. base := ArrayBase(base,i1);
  5044. IF (base # NIL) & Indexable(base) THEN
  5045. ELSE
  5046. Error(position,Diagnostics.Invalid,"len on no array");
  5047. IF VerboseErrorMessage THEN
  5048. Printout.Info("base",base);
  5049. END;
  5050. base := SyntaxTree.invalidType;
  5051. END;
  5052. END;
  5053. IF numberActualParameters=2 THEN
  5054. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5055. actualParameters.SetExpression(1,parameter1);
  5056. ELSIF base IS SyntaxTree.MathArrayType THEN
  5057. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5058. END;
  5059. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5060. IF base IS SyntaxTree.ArrayType THEN
  5061. arrayType := base(SyntaxTree.ArrayType);
  5062. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5063. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5064. result := Global.NewIntegerValue(system,position,i);
  5065. type := result.type;(* arrayType.length.type;*)
  5066. ASSERT(type # NIL);
  5067. END;
  5068. ELSIF base IS SyntaxTree.MathArrayType THEN
  5069. mathArrayType := base(SyntaxTree.MathArrayType);
  5070. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5071. result := Global.NewIntegerValue(system,position,i);
  5072. type := result.type;
  5073. (*
  5074. type := mathArrayType.length.type;
  5075. *)
  5076. ASSERT(type # NIL);
  5077. END;
  5078. END;
  5079. END;
  5080. ELSE
  5081. type := system.longintType;
  5082. END;
  5083. (* ---- FIRST ---- *)
  5084. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5085. type := system.longintType;
  5086. IF CheckRangeType(parameter0) THEN END;
  5087. result.SetAssignable(parameter0.assignable)
  5088. (* ---- LAST ---- *)
  5089. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5090. type := system.longintType;
  5091. IF CheckRangeType(parameter0) THEN END;
  5092. result.SetAssignable(parameter0.assignable)
  5093. (* ---- STEP ---- *)
  5094. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5095. type := system.longintType;
  5096. IF CheckRangeType(parameter0) THEN END;
  5097. result.SetAssignable(parameter0.assignable)
  5098. (* ---- RE ---- *)
  5099. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5100. IF CheckNumberType(parameter0) THEN
  5101. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5102. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5103. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5104. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5105. type := parameter0.type
  5106. ELSE
  5107. type := system.realType
  5108. END
  5109. END;
  5110. result.SetAssignable(parameter0.assignable)
  5111. (* ---- IM ---- *)
  5112. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5113. IF CheckNumberType(parameter0) THEN
  5114. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5115. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5116. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5117. ELSE
  5118. type := system.realType;
  5119. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5120. END
  5121. END;
  5122. result.SetAssignable(parameter0.assignable)
  5123. (* ---- MAX ----- *)
  5124. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5125. IF numberActualParameters = 1 THEN
  5126. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5127. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5128. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5129. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5130. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5131. *)
  5132. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5133. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5134. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5135. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5136. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5137. END;
  5138. ELSE
  5139. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5140. END
  5141. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5142. ConvertOperands(parameter0,parameter1);
  5143. actualParameters.SetExpression(0,parameter0);
  5144. actualParameters.SetExpression(1,parameter1);
  5145. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5146. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5147. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5148. END;
  5149. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5150. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5151. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5152. END;
  5153. END;
  5154. type := parameter0.type;
  5155. ELSE type := SyntaxTree.invalidType;
  5156. END;
  5157. (* ---- MIN ----- *)
  5158. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5159. IF numberActualParameters = 1 THEN
  5160. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5161. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5162. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5163. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5164. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5165. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5166. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5167. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5168. END;
  5169. ELSE
  5170. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5171. END
  5172. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5173. ConvertOperands(parameter0,parameter1);
  5174. actualParameters.SetExpression(0,parameter0);
  5175. actualParameters.SetExpression(1,parameter1);
  5176. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5177. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5178. ELSE result.SetResolved(parameter1.resolved)
  5179. END;
  5180. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5181. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5182. ELSE result.SetResolved(parameter1.resolved)
  5183. END;
  5184. END;
  5185. type := parameter0.type;
  5186. ELSE type := SyntaxTree.invalidType;
  5187. END;
  5188. (* ---- ODD ----- *)
  5189. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5190. type := system.booleanType;
  5191. IF CheckIntegerType(parameter0) THEN
  5192. IF IsIntegerValue(parameter0,i0) THEN
  5193. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5194. type := system.booleanType;
  5195. END;
  5196. END;
  5197. (* ---- ORD ----- *)
  5198. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5199. IF id = Global.Ord THEN
  5200. type := system.integerType;
  5201. ELSE
  5202. type := system.longintType;
  5203. END;
  5204. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5205. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5206. actualParameters.SetExpression(0,parameter0);
  5207. (* IF CheckCharacterType(parameter0) THEN*)
  5208. IF IsCharacterValue(parameter0,c)THEN
  5209. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5210. type := Global.GetSignedIntegerType(system,ORD(c));
  5211. END;
  5212. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5213. END;
  5214. (* ---- SHORT ----- *)
  5215. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5216. type := type0;
  5217. IF IsSignedIntegerType(type) THEN
  5218. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5219. ELSIF type = system.integerType THEN type := system.shortintType
  5220. ELSIF type = system.longintType THEN type := system.integerType
  5221. ELSIF type = system.hugeintType THEN type:= system.longintType
  5222. ELSE
  5223. CASE type.sizeInBits OF
  5224. 16: type := Global.Integer8
  5225. |32: type := Global.Integer16
  5226. |64: type := Global.Integer32
  5227. END;
  5228. END;
  5229. ELSIF type IS SyntaxTree.FloatType THEN
  5230. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5231. ELSIF type = system.longrealType THEN type := system.realType
  5232. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5233. END;
  5234. ELSIF type IS SyntaxTree.ComplexType THEN
  5235. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5236. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5237. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5238. END;
  5239. ELSE
  5240. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5241. END;
  5242. IF (parameter0.resolved # NIL) THEN
  5243. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5244. IF parameter0 IS SyntaxTree.Value THEN
  5245. result.SetResolved(parameter0(SyntaxTree.Value));
  5246. END;
  5247. END;
  5248. (* ---- LONG ----- *)
  5249. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5250. type := type0;
  5251. IF IsSignedIntegerType(type) THEN
  5252. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5253. ELSIF type = system.longintType THEN type := system.hugeintType
  5254. ELSIF type = system.integerType THEN type := system.longintType
  5255. ELSIF type = system.shortintType THEN type := system.integerType
  5256. ELSE
  5257. CASE type.sizeInBits OF
  5258. 8: type := Global.Integer16
  5259. |16: type := Global.Integer32
  5260. |32: type := Global.Integer64
  5261. END;
  5262. END;
  5263. ELSIF type IS SyntaxTree.FloatType THEN
  5264. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5265. ELSIF type= system.realType THEN type := system.longrealType
  5266. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5267. END;
  5268. ELSIF type IS SyntaxTree.ComplexType THEN
  5269. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5270. ELSIF type = system.complexType THEN type := system.longcomplexType
  5271. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5272. END;
  5273. ELSE
  5274. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5275. END;
  5276. IF (parameter0.resolved # NIL) THEN
  5277. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5278. IF parameter0 IS SyntaxTree.Value THEN
  5279. result.SetResolved(parameter0(SyntaxTree.Value));
  5280. END;
  5281. END;
  5282. (* ---- SIZE OF ----- *)
  5283. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5284. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5285. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5286. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5287. type := system.integerType;
  5288. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5289. ELSE
  5290. (* for variables, system sizeof could represent the physically occupied size
  5291. determined via the type descriptor, implement that ? *)
  5292. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5293. END
  5294. (* ---- SYSTEM.TRACE -----*)
  5295. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5296. FOR i := 0 TO numberActualParameters-1 DO
  5297. parameter0 := actualParameters.GetExpression(i);
  5298. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5299. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5300. END;
  5301. END;
  5302. (* remaining issues can only be tested in backend *)
  5303. (* ---- ADDRESSOF----- *)
  5304. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5305. IF HasAddress(parameter0) THEN
  5306. type := system.addressType;
  5307. ELSE
  5308. type := SyntaxTree.invalidType;
  5309. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5310. END;
  5311. (* ---- BIT ----- *)
  5312. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5313. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5314. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5315. actualParameters.SetExpression(0,parameter0);
  5316. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5317. actualParameters.SetExpression(1,parameter1);
  5318. END;
  5319. type := system.booleanType;
  5320. (* ----- MSK ---- *)
  5321. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5322. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5323. ConvertOperands(parameter0,parameter1);
  5324. actualParameters.SetExpression(0,parameter0);
  5325. actualParameters.SetExpression(1,parameter1);
  5326. END;
  5327. type := parameter0.type;
  5328. (* ---- SYSTEM.GET64 ----- *)
  5329. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5330. IF CheckAddressType(parameter0) THEN
  5331. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5332. actualParameters.SetExpression(0,parameter0);
  5333. END;
  5334. type := system.hugeintType;
  5335. (* ---- SYSTEM.GET32 ----- *)
  5336. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5337. IF CheckAddressType(parameter0) THEN
  5338. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5339. actualParameters.SetExpression(0,parameter0);
  5340. END;
  5341. type := system.longintType;
  5342. (* ---- SYSTEM.GET16 ----- *)
  5343. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5344. IF CheckAddressType(parameter0) THEN
  5345. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5346. actualParameters.SetExpression(0,parameter0);
  5347. END;
  5348. type := system.integerType;
  5349. (* ---- SYSTEM.GET8 ----- *)
  5350. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5351. IF CheckAddressType(parameter0) THEN
  5352. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5353. actualParameters.SetExpression(0,parameter0);
  5354. END;
  5355. type := system.shortintType;
  5356. (* ---- SYSTEM.GetStackPointer ----- *)
  5357. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5358. type := system.addressType;
  5359. (* ---- SYSTEM.GetFramePointer ----- *)
  5360. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5361. type := system.addressType;
  5362. (* ---- SYSTEM.GetActivity ----- *)
  5363. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5364. type := system.objectType;
  5365. (* ---- SYSTEM.SetStackPointer ----- *)
  5366. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5367. IF CheckAddressType(parameter0) THEN
  5368. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5369. actualParameters.SetExpression(0,parameter0);
  5370. END;
  5371. (* ---- SYSTEM.SetFramePointer ----- *)
  5372. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5373. IF CheckAddressType(parameter0) THEN
  5374. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5375. actualParameters.SetExpression(0,parameter0);
  5376. END;
  5377. (* ---- SYSTEM.SetActivity ----- *)
  5378. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5379. IF CheckObjectType(parameter0) THEN
  5380. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5381. actualParameters.SetExpression(0,parameter0);
  5382. END;
  5383. (* ---- LSH, LSL, ROT, ROR ----- *)
  5384. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5385. type := type0;
  5386. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5387. actualParameters.SetExpression(1, parameter1);
  5388. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5389. IF id = Global.Lsh THEN
  5390. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5391. ELSIF id = Global.Rot THEN
  5392. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5393. ELSIF id = Global.Ror THEN
  5394. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5395. END;
  5396. END;
  5397. (* ---- SYSTEM.VAL ----- *)
  5398. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5399. IF CheckTypeDeclarationType(parameter0) THEN
  5400. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5401. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5402. result := SyntaxTree.invalidExpression;
  5403. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5404. ELSE
  5405. IF (parameter1.resolved # NIL) THEN
  5406. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5407. IF parameter0 IS SyntaxTree.Value THEN
  5408. result.SetResolved(parameter0(SyntaxTree.Value));
  5409. END;
  5410. END;
  5411. result.SetAssignable(parameter1.assignable);
  5412. END;
  5413. END;
  5414. (* ---- SYSTEM.GET ----- *)
  5415. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5416. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5417. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5418. actualParameters.SetExpression(0,parameter0);
  5419. END;
  5420. (* ---- SYSTEM.PUT ----- *)
  5421. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5422. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5423. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5424. actualParameters.SetExpression(0,parameter0);
  5425. END;
  5426. (* ---- SYSTEM.PUT64 ----- *)
  5427. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5428. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5429. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5430. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5431. actualParameters.SetExpression(0,parameter0);
  5432. actualParameters.SetExpression(1,parameter1);
  5433. END;
  5434. (* ---- SYSTEM.PUT32 ----- *)
  5435. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5436. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5437. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5438. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5439. actualParameters.SetExpression(0,parameter0);
  5440. actualParameters.SetExpression(1,parameter1);
  5441. END;
  5442. (* ---- SYSTEM.PUT16 ----- *)
  5443. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5444. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5445. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5446. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5447. actualParameters.SetExpression(0,parameter0);
  5448. actualParameters.SetExpression(1,parameter1);
  5449. END;
  5450. (* ---- SYSTEM.PUT8 ----- *)
  5451. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5452. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5453. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5454. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5455. actualParameters.SetExpression(0,parameter0);
  5456. actualParameters.SetExpression(1,parameter1);
  5457. END;
  5458. (* ---- SYSTEM.MOVE ----- *)
  5459. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5460. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5461. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5462. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5463. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5464. actualParameters.SetExpression(0,parameter0);
  5465. actualParameters.SetExpression(1,parameter1);
  5466. actualParameters.SetExpression(2,parameter2);
  5467. END;
  5468. (* ---- SYSTEM.NEW ----- *)
  5469. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5470. IF ~IsPointerType(parameter0.type) THEN
  5471. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5472. ELSIF CheckSizeType(parameter1) THEN
  5473. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5474. actualParameters.SetExpression(1,parameter1);
  5475. END;
  5476. (* ----SYSTEM.REF ---- *)
  5477. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5478. type := system.addressType
  5479. (* ---- INCR ----- *)
  5480. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5481. type := system.sizeType;
  5482. base := type0;
  5483. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5484. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5485. IF i1 < 0 THEN
  5486. Error(position,Diagnostics.Invalid,"invalid dimension");
  5487. base := SyntaxTree.invalidType;
  5488. ELSE
  5489. base := ArrayBase(base,i1);
  5490. IF (base # NIL) & Indexable(base) THEN
  5491. ELSE
  5492. Error(position,Diagnostics.Invalid,"len on no array");
  5493. IF VerboseErrorMessage THEN
  5494. Printout.Info("base",base);
  5495. END;
  5496. base := SyntaxTree.invalidType;
  5497. END;
  5498. END;
  5499. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5500. actualParameters.SetExpression(1,parameter1);
  5501. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5502. mathArrayType := base(SyntaxTree.MathArrayType);
  5503. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5504. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5505. type := system.longintType;
  5506. END;
  5507. END;
  5508. ELSE
  5509. type := system.longintType;
  5510. END;
  5511. (* ---- SUM ----- *)
  5512. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5513. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5514. (* ---- ALL ----- *)
  5515. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5516. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5517. (* ---- DIM ----- *)
  5518. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5519. type := system.sizeType;
  5520. IF type0 IS SyntaxTree.MathArrayType THEN
  5521. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5522. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5523. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5524. END;
  5525. ELSE
  5526. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5527. END;
  5528. (* ---- CAS ----- *)
  5529. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5530. IF type0.IsComposite () THEN
  5531. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5532. result := SyntaxTree.invalidExpression;
  5533. ELSIF ~IsVariable (parameter0) THEN
  5534. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5535. result := SyntaxTree.invalidExpression;
  5536. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5537. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5538. result := SyntaxTree.invalidExpression;
  5539. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5540. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5541. result := SyntaxTree.invalidExpression;
  5542. ELSE
  5543. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5544. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5545. type := type0;
  5546. END;
  5547. (* ---- RESHAPE ----- *)
  5548. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5549. IF type0 IS SyntaxTree.MathArrayType THEN
  5550. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5551. base := ArrayBase(type0,MAX(LONGINT));
  5552. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5553. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5554. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5555. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5556. IF ~CompatibleTo(system,type0,parameterType) THEN
  5557. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5558. result := SyntaxTree.invalidExpression;
  5559. ELSE
  5560. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5561. END;
  5562. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5563. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5564. IF ~CompatibleTo(system,type1,parameterType) THEN
  5565. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5566. result := SyntaxTree.invalidExpression;
  5567. ELSE
  5568. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5569. END;
  5570. ELSE
  5571. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5572. result := SyntaxTree.invalidExpression;
  5573. END;
  5574. (* ---- SYSTEM.TYPECODE ----- *)
  5575. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5576. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5577. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5578. type := type.resolved;
  5579. IF type IS SyntaxTree.PointerType THEN
  5580. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5581. END;
  5582. IF ~(type IS SyntaxTree.RecordType) THEN
  5583. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5584. END;
  5585. ELSE
  5586. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5587. END;
  5588. type := system.addressType;
  5589. (* -------- FLT --------- *)
  5590. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5591. type := system.realType;
  5592. IF IsRealValue(parameter0, r) THEN
  5593. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5594. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5595. i0 := i; i := ABS(i);
  5596. IF i # 0 THEN
  5597. i1 := 23;
  5598. IF i >= 2*800000H THEN
  5599. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5600. ELSIF i < 800000H THEN
  5601. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5602. END;
  5603. i := (i1 + 127)*800000H - 800000H + i;
  5604. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5605. END;
  5606. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5607. END;
  5608. (* ------- CONNECT -------*)
  5609. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5610. IF ~(currentIsCellNet) THEN
  5611. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5612. END;
  5613. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5614. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5615. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5616. END;
  5617. IF numberActualParameters = 3 THEN
  5618. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5619. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5620. END;
  5621. *)
  5622. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5623. actualParameters.SetExpression(2,parameter2);
  5624. END;
  5625. activeCellsStatement := TRUE;
  5626. (* ---------- DELEGATE --------*)
  5627. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5628. IF ~(currentIsCellNet) THEN
  5629. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5630. END;
  5631. IF ~CheckPortType(parameter1, inPort) THEN
  5632. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5633. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5634. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5635. ELSIF (outPort.direction # inPort.direction) THEN
  5636. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5637. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5638. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5639. END;
  5640. activeCellsStatement := TRUE;
  5641. (* --------- RECEIVE ---------*)
  5642. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5643. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5644. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5645. IF inPort.direction # SyntaxTree.InPort THEN
  5646. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5647. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5648. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5649. END;
  5650. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5651. IF ~SameType(parameter2.type, system.integerType) THEN
  5652. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5653. END;
  5654. END;
  5655. END;
  5656. (* --------- SEND ---------*)
  5657. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5658. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5659. IF CheckPortType(parameter0,outPort) THEN
  5660. IF outPort.direction # SyntaxTree.OutPort THEN
  5661. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5662. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5663. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5664. ELSE
  5665. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5666. actualParameters.SetExpression(1,parameter1);
  5667. END;
  5668. END;
  5669. (* ------- custom builtins ----- *)
  5670. ELSIF id = Global.systemSpecial THEN
  5671. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5672. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5673. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5674. type := procedureType.returnType;
  5675. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5676. (* go through all formal parameters *)
  5677. formalParameter := procedureType.firstParameter;
  5678. FOR i := 0 TO actualParameters.Length() - 1 DO
  5679. actualParameter := actualParameters.GetExpression(i);
  5680. IF actualParameter = SyntaxTree.invalidExpression THEN
  5681. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5682. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5683. ELSE
  5684. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5685. END;
  5686. actualParameters.SetExpression(i, actualParameter);
  5687. formalParameter := formalParameter.nextParameter
  5688. END
  5689. END
  5690. ELSE
  5691. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5692. result := SyntaxTree.invalidExpression;
  5693. END;
  5694. END;
  5695. IF result # SyntaxTree.invalidExpression THEN
  5696. type := ResolveType(type);
  5697. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5698. result.SetType(type);
  5699. END;
  5700. RETURN result
  5701. END NewBuiltinCallDesignator;
  5702. (** return type guard designator left(type)
  5703. - check if type can be extended (i.e. is no static record)
  5704. - check if type is a type extension of left.type
  5705. - returns new type guard designator
  5706. returns invalidDesignator = invalidExpression if error
  5707. **)
  5708. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5709. VAR result: SyntaxTree.Designator;
  5710. BEGIN
  5711. result := SyntaxTree.invalidDesignator;
  5712. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5713. Error(position,Diagnostics.Invalid,"no type extension of type");
  5714. IF VerboseErrorMessage THEN
  5715. Printout.Info("left",left);
  5716. Printout.Info("type",type);
  5717. END;
  5718. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5719. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5720. ELSIF IsUnsafePointer(left.type) THEN
  5721. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5722. ELSE
  5723. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5724. result.SetType(type);
  5725. result.SetAssignable(left.assignable);
  5726. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5727. END;
  5728. RETURN result
  5729. END NewTypeGuardDesignator;
  5730. (** check and resolve parameter designator left(expression list)
  5731. - check expression list
  5732. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5733. - elsif left is a procedure type then
  5734. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5735. - else return is a procedure call then return ProcedureCallDesignator
  5736. returns invalidDesignator = invalidExpression if error
  5737. **)
  5738. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5739. VAR
  5740. parameters: SyntaxTree.ExpressionList;
  5741. left: SyntaxTree.Designator;
  5742. result,expression: SyntaxTree.Expression;
  5743. typeDeclaration: SyntaxTree.TypeDeclaration;
  5744. type, expressionType: SyntaxTree.Type;
  5745. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5746. BEGIN
  5747. type := type.resolved;
  5748. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5749. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5750. END;
  5751. RETURN type
  5752. END BaseType;
  5753. BEGIN
  5754. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5755. result := SyntaxTree.invalidDesignator;
  5756. left := ResolveDesignator(designator.left);
  5757. IF left # SyntaxTree.invalidDesignator THEN
  5758. parameters := designator.parameters;
  5759. IF ExpressionList(parameters) THEN
  5760. IF (left.type = NIL) THEN
  5761. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5762. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5763. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5764. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5765. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5766. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5767. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5768. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5769. ELSE
  5770. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5771. END
  5772. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5773. expression := parameters.GetExpression(0);
  5774. type := typeDeclaration.declaredType.resolved;
  5775. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5776. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5777. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5778. OR (expressionType IS SyntaxTree.EnumerationType)
  5779. ) THEN
  5780. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5781. ELSE
  5782. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5783. END;
  5784. ELSE
  5785. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5786. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5787. result := SyntaxTree.invalidDesignator;
  5788. END;
  5789. ELSE
  5790. result := SyntaxTree.invalidDesignator
  5791. END;
  5792. END;
  5793. resolvedExpression := result;
  5794. END VisitParameterDesignator;
  5795. (** check dereference designator left^
  5796. - check if left is pointer type or left is object type
  5797. - return new dereference designator with type = left.baseType.type (if appropriate)
  5798. with error handling
  5799. returns invalidDesignator = invalidExpression if error
  5800. **)
  5801. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5802. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5803. BEGIN
  5804. result := SyntaxTree.invalidDesignator;
  5805. type := left.type;
  5806. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5807. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5808. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5809. result.SetAssignable(TRUE);
  5810. result.SetType(type);
  5811. result.SetHidden(left.isHidden);
  5812. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5813. type := type.resolved;
  5814. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5815. result.SetAssignable(TRUE);
  5816. result.SetType(type);
  5817. result.SetHidden(left.isHidden);
  5818. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5819. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5820. result.SetAssignable(TRUE);
  5821. result.SetType(type);
  5822. result.SetHidden(left.isHidden);
  5823. ELSE
  5824. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5825. IF VerboseErrorMessage THEN
  5826. Printout.Info("pointer", type);
  5827. Printout.Info("scope", currentScope);
  5828. END;
  5829. END;
  5830. RETURN result
  5831. END NewDereferenceDesignator;
  5832. (** check supercall designator left^
  5833. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5834. - return new supercall designator with type = left.type
  5835. with error handling
  5836. **)
  5837. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5838. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5839. objectScope: SyntaxTree.Scope;
  5840. BEGIN
  5841. result := SyntaxTree.invalidDesignator;
  5842. IF left = SyntaxTree.invalidDesignator THEN
  5843. (* error already handled *)
  5844. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5845. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5846. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5847. IF symbol IS SyntaxTree.Procedure THEN
  5848. procedure := symbol(SyntaxTree.Procedure);
  5849. objectScope := currentScope;
  5850. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5851. objectScope := objectScope.outerScope;
  5852. END;
  5853. IF (left.left = NIL) OR ~
  5854. (
  5855. (left.left IS SyntaxTree.SelfDesignator) OR
  5856. (left.left IS SyntaxTree.DereferenceDesignator)
  5857. & (left.left(SyntaxTree.Designator).left # NIL)
  5858. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5859. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5860. IF VerboseErrorMessage THEN
  5861. Printout.Info("left.left",left.left);
  5862. END;
  5863. ELSIF procedure.super # NIL THEN
  5864. result := SyntaxTree.NewSupercallDesignator(position,left);
  5865. result.SetType(left.type.resolved)
  5866. ELSE
  5867. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5868. END;
  5869. ELSE
  5870. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5871. END;
  5872. ELSE
  5873. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5874. END;
  5875. RETURN result
  5876. END NewSupercallDesignator;
  5877. (** check and semantically resolve arrow designator left^
  5878. - if left is procedure type -> result := SupercallDesignator
  5879. - else result := DereferenceDesignator
  5880. returns result via global variable resolvedExpression
  5881. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5882. **)
  5883. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5884. VAR left: SyntaxTree.Designator;
  5885. BEGIN
  5886. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5887. left := ResolveDesignator(arrowDesignator.left);
  5888. IF left # NIL THEN
  5889. IF (left.type = NIL) THEN
  5890. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5891. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5892. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5893. ELSE
  5894. IF IsPointerToObject(left.type) THEN
  5895. Warning(arrowDesignator.position, "forbidden dereference on object");
  5896. END;
  5897. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5898. END
  5899. END
  5900. END VisitArrowDesignator;
  5901. (** check and return expression
  5902. - if expression has no type then resolve expression
  5903. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5904. - return result
  5905. **)
  5906. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5907. VAR result,prev: SyntaxTree.Expression;
  5908. BEGIN
  5909. IF expression = NIL THEN result := NIL
  5910. ELSIF (expression.type = NIL) THEN
  5911. prev := resolvedExpression;
  5912. resolvedExpression := SyntaxTree.invalidExpression;
  5913. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5914. expression.SetType(SyntaxTree.invalidType);
  5915. END;
  5916. expression.Accept(SELF);
  5917. result := resolvedExpression;
  5918. IF currentIsRealtime THEN
  5919. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5920. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5921. END;
  5922. END;
  5923. (* designator modifiers for backends if they support it ...*)
  5924. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5925. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5926. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5927. END;
  5928. resolvedExpression := prev
  5929. ELSE
  5930. result := expression
  5931. END;
  5932. RETURN result
  5933. END ResolveExpression;
  5934. (**
  5935. check expression to be constant expression
  5936. - resolve expression
  5937. - if valid then check that of value type
  5938. report error and return invalidExpression if anything fails
  5939. **)
  5940. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5941. VAR position: LONGINT;
  5942. BEGIN
  5943. position := expression.position;
  5944. expression := ResolveExpression(expression);
  5945. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5946. ELSIF (expression.resolved = NIL) THEN
  5947. Error(position,Diagnostics.Invalid,"expression is not constant");
  5948. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5949. expression := SyntaxTree.invalidExpression;
  5950. END;
  5951. RETURN expression
  5952. END ConstantExpression;
  5953. (** check expression to be constant integer
  5954. - resolve expresssion
  5955. - if valid then check that of integer value type
  5956. report error and return invalidExpression if anything fails
  5957. **)
  5958. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5959. VAR position: LONGINT;
  5960. BEGIN
  5961. position := expression.position;
  5962. expression := ResolveExpression(expression);
  5963. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5964. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5965. expression := SyntaxTree.invalidExpression;
  5966. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5967. END;
  5968. RETURN expression
  5969. END ConstantInteger;
  5970. (** check expression as positive (>=0) constant integer
  5971. - resolve expression
  5972. - if valid then check that integer value
  5973. - if integer value then check that value >= 0
  5974. report error and return invalidExpression if anything fails
  5975. **)
  5976. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5977. VAR position: LONGINT;
  5978. BEGIN
  5979. position := expression.position;
  5980. expression := ConstantExpression(expression);
  5981. IF expression = SyntaxTree.invalidExpression THEN
  5982. (* error already reported *)
  5983. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5984. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5985. expression := SyntaxTree.invalidExpression
  5986. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5987. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5988. END;
  5989. RETURN expression
  5990. END ConstantIntegerGeq0;
  5991. (** check expression as condition
  5992. - resolve expression
  5993. - if valid expression then check that result type is boolean
  5994. report error and return invalidExpression if anything fails
  5995. **)
  5996. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5997. VAR position: LONGINT;
  5998. BEGIN
  5999. position := expression.position;
  6000. expression := ResolveExpression(expression);
  6001. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  6002. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6003. expression := SyntaxTree.invalidExpression;
  6004. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6005. END;
  6006. RETURN expression
  6007. END ResolveCondition;
  6008. (*** symbols ***)
  6009. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6010. BEGIN
  6011. x.Accept(SELF);
  6012. END ResolveSymbol;
  6013. (** check a symbol
  6014. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6015. **)
  6016. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6017. VAR scope: SyntaxTree.Scope;
  6018. BEGIN
  6019. (* visibility *)
  6020. scope := symbol.scope;
  6021. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6022. scope := scope.outerScope;
  6023. END;
  6024. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6025. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6026. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6027. IF VerboseErrorMessage THEN
  6028. Printout.Info("symbol",symbol);
  6029. END;
  6030. END;
  6031. END;
  6032. END CheckSymbolVisibility;
  6033. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6034. If node is currently being resolved then emit a cyclic definition error.
  6035. Return TRUE only if node is fully resolved.
  6036. **)
  6037. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6038. VAR result: BOOLEAN;
  6039. BEGIN
  6040. IF SyntaxTree.Resolved IN x.state THEN
  6041. result := FALSE
  6042. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6043. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6044. result := FALSE;
  6045. ELSE
  6046. result := TRUE;
  6047. x.SetState(SyntaxTree.BeingResolved)
  6048. END;
  6049. RETURN result
  6050. END SymbolNeedsResolution;
  6051. (** check and resolve a type declaration symbol = Type
  6052. - set type to declaration type
  6053. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6054. This is so because the type declaration itself does not have a type but it only stands for a type.
  6055. In the implementation of the compiler this made a lot much easier.
  6056. - resolve and set declared type
  6057. - check symbol
  6058. **)
  6059. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6060. VAR prevScope: SyntaxTree.Scope;
  6061. BEGIN
  6062. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6063. IF SymbolNeedsResolution(typeDeclaration) THEN
  6064. prevScope := currentScope;
  6065. currentScope := typeDeclaration.scope;
  6066. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6067. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6068. CheckSymbolVisibility(typeDeclaration);
  6069. typeDeclaration.SetState(SyntaxTree.Resolved);
  6070. currentScope := prevScope;
  6071. END;
  6072. END VisitTypeDeclaration;
  6073. (** check and resolve a constant declaration symbol = (constant) expression
  6074. - check expression
  6075. - set type and value
  6076. - check symbol
  6077. **)
  6078. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6079. VAR
  6080. expression: SyntaxTree.Expression;
  6081. type: SyntaxTree.Type;
  6082. name: Basic.SegmentedName;
  6083. replacement: Replacement;
  6084. BEGIN
  6085. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6086. IF SymbolNeedsResolution(constant) THEN
  6087. expression := constant.value;
  6088. IF replacements # NIL THEN
  6089. Global.GetSymbolSegmentedName(constant, name);
  6090. replacement := replacements;
  6091. WHILE (replacement # NIL) & (replacement.name # name) DO
  6092. replacement := replacement.next;
  6093. END;
  6094. IF replacement # NIL THEN
  6095. InfoSS(constant.position, "replacing constant", constant.name);
  6096. (*
  6097. NEW(stringReader, Strings.Length(replacement.string^));
  6098. stringReader.Set(replacement.string^);
  6099. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6100. NEW(parser, scanner, diagnostics);
  6101. expression := parser.Expression();
  6102. *)
  6103. expression := replacement.expression;
  6104. replacement.used := TRUE;
  6105. END;
  6106. END;
  6107. constant.SetType(SyntaxTree.invalidType);
  6108. expression := ConstantExpression(expression);
  6109. ASSERT(expression.type # NIL);
  6110. type := expression.type.resolved;
  6111. constant.SetType(type);
  6112. constant.SetValue(expression);
  6113. CheckSymbolVisibility(constant);
  6114. constant.SetState(SyntaxTree.Resolved);
  6115. END;
  6116. END VisitConstant;
  6117. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6118. VAR procedureAlignment: LONGINT;
  6119. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6120. (* least common multiple *)
  6121. VAR a,b: LONGINT;
  6122. BEGIN
  6123. a := a0; b := b0;
  6124. WHILE (a # b) DO
  6125. IF a < b THEN a := a+a0
  6126. ELSE b := b + b0
  6127. END;
  6128. END;
  6129. RETURN a
  6130. END LCM;
  6131. BEGIN
  6132. IF alignment > 1 THEN
  6133. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6134. IF (procedureAlignment > 1) THEN
  6135. alignment := LCM(alignment, procedureAlignment);
  6136. END;
  6137. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6138. END;
  6139. END AdaptStackAlignment;
  6140. (** check and resolve a variable / field
  6141. - check and set type
  6142. - negative check on open array type
  6143. - check symbol
  6144. **)
  6145. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6146. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6147. BEGIN
  6148. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6149. IF SymbolNeedsResolution(variable) THEN
  6150. modifiers := variable.modifiers;
  6151. (*
  6152. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6153. variable.AddFlags(flags);
  6154. *)
  6155. variable.SetType(ResolveType(variable.type));
  6156. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6157. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6158. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6159. END;
  6160. END;
  6161. CheckSymbolVisibility(variable);
  6162. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6163. variable.SetUntraced(TRUE);
  6164. IF ~ContainsPointer(variable.type) THEN
  6165. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6166. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6167. END;
  6168. END;
  6169. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6170. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6171. IF ~PowerOf2(value) THEN
  6172. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6173. ELSE
  6174. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6175. END;
  6176. END;
  6177. variable.SetAlignment(FALSE,value);
  6178. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6179. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6180. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6181. END;
  6182. variable.SetAlignment(TRUE, value);
  6183. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6184. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6185. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6186. END;
  6187. variable.SetFictive(value);
  6188. variable.SetOffset(value*system.dataUnit);
  6189. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6190. END;
  6191. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6192. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6193. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6194. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6195. END;
  6196. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6197. IF variable.initializer # NIL THEN
  6198. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6199. END;
  6200. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6201. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6202. pointerType.SetPointerBase(variable.type);
  6203. pointerType.SetHidden(TRUE);
  6204. variable.SetType(ResolveType(pointerType));
  6205. END;
  6206. variable.SetState(SyntaxTree.Resolved);
  6207. END;
  6208. END VisitVariable;
  6209. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6210. BEGIN
  6211. VisitVariable(property)
  6212. END VisitProperty;
  6213. (** check and resolve a (procedure) parameter
  6214. - check and set type
  6215. - check symbol
  6216. - check parameter kind and set read-only flags if appropriate
  6217. **)
  6218. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6219. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6220. BEGIN
  6221. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6222. IF SymbolNeedsResolution(parameter) THEN
  6223. modifiers := parameter.modifiers;
  6224. parameter.SetType(ResolveType(parameter.type));
  6225. ASSERT(parameter.type.resolved # NIL);
  6226. CheckSymbolVisibility(parameter);
  6227. IF parameter.defaultValue # NIL THEN
  6228. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6229. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6230. ELSE
  6231. expression := ConstantExpression(parameter.defaultValue);
  6232. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6233. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6234. parameter.SetDefaultValue(expression);
  6235. END;
  6236. END;
  6237. END;
  6238. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6239. parameter.SetUntraced(TRUE);
  6240. IF ~ContainsPointer(parameter.type) THEN
  6241. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6242. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6243. END;
  6244. END;
  6245. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6246. parameter.SetMoveable(TRUE);
  6247. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6248. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6249. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6250. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6251. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6252. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6253. END;
  6254. END;
  6255. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6256. parameter.SetState(SyntaxTree.Resolved);
  6257. END;
  6258. END VisitParameter;
  6259. (** check and resolve a procedure (with declaration and implementation scope)
  6260. - check the procedure type
  6261. - check if method (i.e. in record scope), if so then
  6262. - check if (unique) constructor
  6263. - check if (unique) finalizer
  6264. - check if super method available, if so then check signature
  6265. - of not in record scope then negative check on constructor flag
  6266. - of not in record scope then negative check on finalizer flag
  6267. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6268. - check procedure symbol
  6269. **)
  6270. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6271. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6272. procedureType: SyntaxTree.ProcedureType;
  6273. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6274. qualifiedType: SyntaxTree.QualifiedType;
  6275. value: LONGINT;
  6276. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6277. position: LONGINT;
  6278. BEGIN
  6279. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6280. IF IsOberonInline(procedure) THEN
  6281. IF SyntaxTree.Public * procedure.access # {} THEN
  6282. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6283. END;
  6284. procedure.SetInline(FALSE);
  6285. procedure.SetOberonInline(TRUE);
  6286. END;
  6287. IF SymbolNeedsResolution(procedure) THEN
  6288. recentIsRealtime := currentIsRealtime;
  6289. recentIsBodyProcedure := currentIsBodyProcedure;
  6290. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6291. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6292. modifiers := procedureType.modifiers;
  6293. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6294. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6295. IF useDarwinCCalls THEN (*fld*)
  6296. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6297. ELSE
  6298. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6299. END
  6300. END;
  6301. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6302. procedureType.SetInterrupt(TRUE);
  6303. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6304. END;
  6305. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6306. procedureType.SetNoReturn(TRUE);
  6307. END;
  6308. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6309. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6310. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6311. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6312. END;
  6313. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6314. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6315. END;
  6316. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6317. IF ~PowerOf2(value) THEN
  6318. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6319. ELSE
  6320. procedureType.SetStackAlignment(value)
  6321. END;
  6322. END;
  6323. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6324. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6325. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6326. END;
  6327. CheckModifiers(modifiers, TRUE);
  6328. modifiers := procedureType.returnTypeModifiers;
  6329. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6330. CheckModifiers(modifiers, TRUE);
  6331. FixProcedureType(procedureType);
  6332. currentIsRealtime := procedureType.isRealtime;
  6333. currentIsBodyProcedure := procedure.isBodyProcedure;
  6334. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6335. THEN
  6336. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6337. END;
  6338. CheckSymbolVisibility(procedure);
  6339. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6340. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6341. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6342. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6343. END;
  6344. END;
  6345. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6346. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6347. procedureType.SetDelegate(TRUE);
  6348. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6349. IF record.pointerType.typeDeclaration = NIL THEN
  6350. selfParameter.SetType(record.pointerType);
  6351. ELSE
  6352. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6353. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6354. qualifiedType.SetResolved(record.pointerType);
  6355. selfParameter.SetType(qualifiedType);
  6356. END;
  6357. selfParameter.SetAccess(SyntaxTree.Hidden);
  6358. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6359. IF procedure.isConstructor THEN
  6360. (*! constructor is always visible, compatibility to paco
  6361. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6362. *)
  6363. procedure.MarkUsed;
  6364. IF procedureType.returnType # NIL THEN
  6365. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6366. END;
  6367. proc := procedure.scope.firstProcedure;
  6368. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6369. proc := proc.nextProcedure;
  6370. END;
  6371. IF proc # NIL THEN
  6372. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6373. ELSE
  6374. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6375. END;
  6376. END;
  6377. IF procedure.isFinalizer THEN
  6378. procedure.MarkUsed;
  6379. IF procedureType.returnType # NIL THEN
  6380. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6381. END;
  6382. IF procedureType.numberParameters # 0 THEN
  6383. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6384. END;
  6385. proc := procedure.scope.firstProcedure;
  6386. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6387. proc := proc.nextProcedure;
  6388. END;
  6389. IF proc # NIL THEN
  6390. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6391. ELSE
  6392. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6393. END;
  6394. END;
  6395. super := FindSuperProcedure(record.recordScope, procedure);
  6396. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6397. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6398. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6399. END;
  6400. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6401. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6402. END;
  6403. IF super.isFinal THEN
  6404. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6405. END;
  6406. procedure.SetSuper(super);
  6407. super.SetOverwritten(TRUE);
  6408. procedure.SetAccess(procedure.access+super.access);
  6409. procedure.MarkUsed;
  6410. END;
  6411. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6412. THEN
  6413. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6414. END;
  6415. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6416. IF cellsAreObjects THEN
  6417. procedureType.SetDelegate(TRUE);
  6418. END;
  6419. IF procedure.isConstructor THEN
  6420. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6421. END;
  6422. ELSIF procedure.isConstructor THEN
  6423. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6424. END;
  6425. Declarations(procedure.procedureScope, FALSE);
  6426. (* body resolution part done as late fix of the procedure type *)
  6427. procedure.SetState(SyntaxTree.Resolved);
  6428. currentIsRealtime := recentIsRealtime;
  6429. currentIsBodyProcedure := recentIsBodyProcedure;
  6430. END;
  6431. END VisitProcedure;
  6432. (**
  6433. a builtin procedure is a global item that may not be modified locally
  6434. instead the resolving of builtin procedure calls are done in the esignator
  6435. **)
  6436. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6437. VAR type: SyntaxTree.Type;
  6438. BEGIN
  6439. type := ResolveType(builtinProcedure.type);
  6440. END VisitBuiltin;
  6441. (* nopov *)
  6442. (** check and resolve operator
  6443. - operators are first checked as procedures
  6444. - then additional operator-specific checks are done
  6445. - note that only module-scope operators are checked here
  6446. (operators in a record scope are only allowed in the context of
  6447. array-structured object types and checked in 'ResolveArrayStructure')
  6448. - also note that inter-operator conformity is not checked here
  6449. **)
  6450. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6451. VAR
  6452. procedureType: SyntaxTree.ProcedureType;
  6453. leftType, rightType: SyntaxTree.Type;
  6454. identifierNumber, position: LONGINT;
  6455. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6456. modifiers: SyntaxTree.Modifier;
  6457. (** whether a type is locally defined in the current module scope
  6458. for arrays, the base type must be locally defined **)
  6459. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6460. BEGIN
  6461. IF type = NIL THEN
  6462. RETURN FALSE
  6463. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6464. RETURN TRUE
  6465. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6466. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6467. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6468. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6469. ELSE
  6470. RETURN FALSE
  6471. END
  6472. END IsLocallyDefined;
  6473. BEGIN
  6474. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6475. procedureType := operator.type(SyntaxTree.ProcedureType);
  6476. modifiers := procedureType.modifiers;
  6477. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6478. CheckModifiers(modifiers, TRUE);
  6479. VisitProcedure(operator);
  6480. IF operator.scope IS SyntaxTree.RecordScope THEN
  6481. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6482. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6483. IF identifierNumber = -1 THEN
  6484. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6485. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6486. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6487. ELSE
  6488. IF procedureType.numberParameters < 1 THEN
  6489. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6490. ELSIF procedureType.numberParameters > 2 THEN
  6491. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6492. ELSE
  6493. (* determine operand types *)
  6494. leftType := procedureType.firstParameter.type;
  6495. IF procedureType.numberParameters > 1 THEN
  6496. rightType := procedureType.firstParameter.nextParameter.type
  6497. ELSE
  6498. rightType := NIL
  6499. END;
  6500. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6501. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6502. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6503. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6504. END
  6505. END;
  6506. (* TODO: refine the checks, think about how restrictive the checks should be
  6507. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6508. They might be used for intersection, union, complement of custom object types *)
  6509. (* defaults *)
  6510. hasReturnType := TRUE;
  6511. mustBeUnary := FALSE;
  6512. mustBeBinary := FALSE;
  6513. mustReturnBoolean := FALSE;
  6514. mustReturnInteger := FALSE;
  6515. mustHaveEquitypedOperands := FALSE;
  6516. (* operator-specific exceptions *)
  6517. CASE identifierNumber OF
  6518. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6519. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6520. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6521. mustBeBinary := TRUE
  6522. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6523. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6524. | Scanner.Times: mustBeBinary := TRUE
  6525. | Scanner.TimesTimes: mustBeBinary := TRUE
  6526. | Scanner.DotTimes: mustBeBinary := TRUE
  6527. | Scanner.PlusTimes: mustBeBinary := TRUE
  6528. | Scanner.Slash: mustBeBinary := TRUE
  6529. | Scanner.Backslash: mustBeBinary := TRUE
  6530. | Scanner.DotSlash: mustBeBinary := TRUE
  6531. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6532. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6533. | Scanner.Not: mustBeUnary := TRUE
  6534. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6535. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6536. | Scanner.Transpose: mustBeUnary := TRUE;
  6537. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6538. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6539. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6540. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6541. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6542. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6543. | Global.Abs: mustBeUnary := TRUE;
  6544. | Global.Ash: (* TODO: arity? *)
  6545. | Global.Cap: (* TODO: arity? *)
  6546. | Global.Chr: mustBeUnary := TRUE;
  6547. | Global.Entier: (* TODO: arity? *)
  6548. | Global.EntierH: (* TODO: arity? *)
  6549. | Global.Len: (* unary and binary *)
  6550. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6551. | Global.Max, Global.Min: (* unary and binary *)
  6552. | Global.Odd: (* TODO: arity? *)
  6553. | Global.Sum: (* TODO: arity? *)
  6554. | Global.All: (* TODO: arity? *)
  6555. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6556. | Scanner.Alias:
  6557. | Scanner.GreaterGreater, Scanner.LessLess:
  6558. mustBeBinary := TRUE; hasReturnType := FALSE;
  6559. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6560. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6561. END;
  6562. (* check parameter count *)
  6563. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6564. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6565. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6566. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6567. END;
  6568. (* check parameter types *)
  6569. (* TODO: is this used at all? *)
  6570. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6571. leftType := procedureType.firstParameter.type;
  6572. rightType := procedureType.firstParameter.nextParameter.type;
  6573. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6574. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6575. END
  6576. END;
  6577. (* check return type *)
  6578. IF hasReturnType THEN
  6579. IF procedureType.returnType = NIL THEN
  6580. Error(operator.position, Diagnostics.Invalid, "return type required")
  6581. ELSIF mustReturnBoolean THEN
  6582. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6583. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6584. END
  6585. ELSIF mustReturnInteger THEN
  6586. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6587. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6588. END
  6589. END
  6590. ELSIF procedureType.returnType # NIL THEN
  6591. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6592. END
  6593. END
  6594. END
  6595. END
  6596. END VisitOperator;
  6597. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6598. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6599. BEGIN
  6600. IF error THEN RETURN FALSE END;
  6601. prevScope := currentScope;
  6602. prevDiagnostics := diagnostics;
  6603. diagnostics := NIL; (* suppress error output *)
  6604. currentScope := module.moduleScope;
  6605. VisitImport(x);
  6606. IF ~error THEN
  6607. module.moduleScope.AddImport(x);
  6608. x.SetScope(module.moduleScope);
  6609. END;
  6610. currentScope := prevScope;
  6611. diagnostics := prevDiagnostics;
  6612. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6613. END AddImport;
  6614. (** check and resolve import
  6615. - check for name = SYSTEM
  6616. - check for forbidden self import
  6617. - search through global import cache: already imported?
  6618. - check if already imported indirectly
  6619. - import if necessary -> set module and enter into import cache
  6620. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6621. - after this import this direct import and all indirect imports are stored in the current module's import list
  6622. **)
  6623. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6624. VAR
  6625. module: SyntaxTree.Module;
  6626. moduleScope: SyntaxTree.ModuleScope;
  6627. import,reimport: SyntaxTree.Import;
  6628. filename: FileName;
  6629. prevScope: SyntaxTree.Scope;
  6630. BEGIN
  6631. IF SymbolNeedsResolution(x) THEN
  6632. prevScope := currentScope;
  6633. x.SetType(SyntaxTree.importType);
  6634. moduleScope := currentScope.ownerModule.moduleScope;
  6635. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6636. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6637. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6638. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6639. ELSE
  6640. (* search through global import list: already imported ? *)
  6641. IF (x.module = NIL) & (importCache # NIL) THEN
  6642. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6643. ELSE import := NIL
  6644. END;
  6645. IF x.module # NIL THEN (* already imported indirectly *)
  6646. module := x.module;
  6647. ELSIF import # NIL THEN (* already in module list *)
  6648. module := import.module;
  6649. ASSERT(module # NIL);
  6650. x.SetModule(module);
  6651. ELSE (* must be imported *)
  6652. Global.ModuleFileName(x.moduleName,x.context,filename);
  6653. IF symbolFileFormat # NIL THEN
  6654. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6655. IF module = NIL THEN
  6656. ErrorSS(x.position,"could not import",filename);
  6657. IF VerboseErrorMessage THEN
  6658. Printout.Info("import",x)
  6659. END
  6660. ELSE
  6661. (*
  6662. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6663. (*! should rather be done by importer *)
  6664. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6665. checker.importCache := importCache;
  6666. checker.arrayBaseImported := arrayBaseImported;
  6667. checker.global := global;
  6668. checker.Module(module); (* semantic check *)
  6669. error := error OR checker.error;
  6670. END;
  6671. *)
  6672. (*
  6673. ASSERT(SyntaxTree.Resolved IN module.state);
  6674. *)
  6675. x.SetModule(module);
  6676. IF importCache # NIL THEN
  6677. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6678. import.SetContext(x.context);
  6679. import.SetModule(module);
  6680. importCache.AddImport(import);
  6681. END;
  6682. END;
  6683. ELSE
  6684. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6685. END;
  6686. END;
  6687. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6688. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6689. END;
  6690. import := module.moduleScope.firstImport;
  6691. WHILE(import # NIL) DO
  6692. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6693. ASSERT(currentScope # NIL);
  6694. ASSERT(currentScope.ownerModule # NIL);
  6695. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6696. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6697. Error(x.position,Diagnostics.Invalid,"recursive import");
  6698. ELSE
  6699. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6700. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6701. IF reimport = NIL THEN (* indirect import *)
  6702. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6703. reimport.SetContext(import.context);
  6704. reimport.SetModule(import.module);
  6705. moduleScope.AddImport(reimport);
  6706. reimport.SetScope(moduleScope);
  6707. ELSE
  6708. ASSERT(import.module # NIL);
  6709. reimport.SetModule(import.module); (* direct or indirect import *)
  6710. END;
  6711. END;
  6712. import := import.nextImport;
  6713. END;
  6714. END;
  6715. END;
  6716. currentScope := prevScope;
  6717. (* ELSE nothing to be done *)
  6718. x.SetState(SyntaxTree.Resolved);
  6719. END;
  6720. END VisitImport;
  6721. (*** statements ***)
  6722. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6723. VAR prev,resolved: SyntaxTree.Statement;
  6724. BEGIN
  6725. prev := resolvedStatement;
  6726. resolvedStatement := x;
  6727. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6728. activeCellsStatement := FALSE;
  6729. x.Accept(SELF);
  6730. (* removed this, implementation restriction should be resolved by backend
  6731. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6732. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6733. END;
  6734. *)
  6735. resolved := resolvedStatement;
  6736. resolvedStatement := prev;
  6737. RETURN resolved
  6738. END ResolveStatement;
  6739. (** check and resolve statement sequence
  6740. - check all statements, replace if necessary
  6741. **)
  6742. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6743. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6744. BEGIN
  6745. IF statementSequence # NIL THEN (* else empty *)
  6746. FOR i := 0 TO statementSequence.Length()-1 DO
  6747. statement := statementSequence.GetStatement(i);
  6748. resolved := ResolveStatement(statement);
  6749. IF (resolved # statement) THEN
  6750. statementSequence.SetStatement(i,resolved);
  6751. END;
  6752. END;
  6753. END;
  6754. END StatementSequence;
  6755. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6756. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6757. - check if procedure is callable
  6758. - check return type = NIL (otherwise must be assignment statement)
  6759. **)
  6760. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6761. VAR call: SyntaxTree.Designator;
  6762. BEGIN
  6763. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6764. call := procedureCall.call;
  6765. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6766. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6767. END;
  6768. call := ResolveDesignator(call);
  6769. IF call = SyntaxTree.invalidDesignator THEN
  6770. (* error already handled *)
  6771. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6772. (* inline call in a statement *)
  6773. ELSIF ~IsCallable(call) THEN
  6774. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6775. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6776. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6777. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6778. END;
  6779. procedureCall.SetCall(call);
  6780. (*
  6781. IF call = SyntaxTree.invalidDesignator THEN
  6782. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6783. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6784. IF IsOberonInline(procedure) THEN
  6785. Warning(procedure.position,"call to inline proc");
  6786. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6787. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6788. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6789. resolvedStatement := block;
  6790. RETURN;
  6791. END;
  6792. END;
  6793. *)
  6794. END VisitProcedureCallStatement;
  6795. (** check and resolve assignment LHS := RHS
  6796. - resolve LHS and RHS
  6797. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6798. - check if assignment is compatible
  6799. - check if LHS is variable (i.e. assignable)
  6800. - convert RHS if necessary
  6801. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6802. - assignment between different ASOTs
  6803. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6804. - assignment to ASOT elements:
  6805. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6806. **)
  6807. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6808. VAR
  6809. left: SyntaxTree.Designator;
  6810. right, expression: SyntaxTree.Expression;
  6811. designator: SyntaxTree.Designator;
  6812. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6813. mathArrayType: SyntaxTree.MathArrayType;
  6814. BEGIN
  6815. right := ResolveExpression(assignment.right);
  6816. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6817. left := ResolveDesignator(assignment.left);
  6818. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6819. (* error already handled *)
  6820. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6821. (* LHS is index write operator call on ASOT *)
  6822. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6823. (* necessary ?
  6824. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6825. type := procedureType.firstParameter.type;
  6826. expression := procedureCallDesignator.parameters.GetExpression(0);
  6827. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6828. *)
  6829. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6830. ELSIF CheckVariable(left) THEN
  6831. expression := NewOperatorCall(assignment.position, Scanner.Becomes, 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(assignment.position, procedureCallDesignator, assignment.outer);
  6842. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6843. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6844. ELSIF AssignmentCompatible(left, right) THEN
  6845. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6846. mathArrayType := MathArrayStructureOfType(left.type);
  6847. right := NewConversion(right.position, right, mathArrayType, NIL);
  6848. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6849. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6850. ELSE
  6851. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6852. assignment.SetLeft(left);
  6853. assignment.SetRight(right);
  6854. resolvedStatement := assignment
  6855. END
  6856. END
  6857. END
  6858. END VisitAssignment;
  6859. (** check and resolve assignment LHS := RHS
  6860. - resolve LHS and RHS
  6861. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6862. - check if assignment is compatible
  6863. - check if LHS is variable (i.e. assignable)
  6864. - convert RHS if necessary
  6865. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6866. - assignment between different ASOTs
  6867. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6868. - assignment to ASOT elements:
  6869. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6870. **)
  6871. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6872. VAR
  6873. left: SyntaxTree.Designator;
  6874. right: SyntaxTree.Expression;
  6875. inPort, outPort: SyntaxTree.PortType;
  6876. expression: SyntaxTree.Expression;
  6877. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6878. BEGIN
  6879. right := ResolveExpression(communication.right);
  6880. left := ResolveDesignator(communication.left);
  6881. communication.SetLeft(left);
  6882. communication.SetRight(right);
  6883. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6884. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6885. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6886. (* conversion done by procedure call
  6887. (* try to convert to left argument *)
  6888. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6889. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6890. procedureCallDesignator.parameters.SetExpression(1, right);
  6891. END;
  6892. *)
  6893. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6894. ELSE
  6895. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6896. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6897. (* error already handled *)
  6898. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6899. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6900. IF outPort.direction # SyntaxTree.OutPort THEN
  6901. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6902. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6903. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6904. ELSE
  6905. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6906. communication.SetRight(right)
  6907. END;
  6908. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6909. IF CheckVariable(left) THEN
  6910. IF inPort.direction # SyntaxTree.InPort THEN
  6911. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6912. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6913. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6914. END;
  6915. END;
  6916. ELSE
  6917. Error(communication.position, -1, "unsupported stream operation");
  6918. END;
  6919. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6920. IF outPort.direction # SyntaxTree.OutPort THEN
  6921. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6922. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6923. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6924. ELSE
  6925. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6926. communication.SetRight(right)
  6927. END;
  6928. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6929. IF CheckVariable(right) THEN
  6930. IF inPort.direction # SyntaxTree.InPort THEN
  6931. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6932. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6933. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6934. END;
  6935. END;
  6936. ELSE
  6937. Error(communication.position, -1, "unsupported operation");
  6938. END;
  6939. END;
  6940. END VisitCommunicationStatement;
  6941. (** check and resolve if/eslif part
  6942. - check condition
  6943. - check statement sequence
  6944. **)
  6945. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6946. VAR prevUnreachable, b: BOOLEAN;
  6947. BEGIN
  6948. prevUnreachable := currentIsUnreachable;
  6949. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6950. IF IsBooleanValue(ifPart.condition,b) THEN
  6951. IF b=FALSE THEN
  6952. currentIsUnreachable := TRUE
  6953. ELSIF b=TRUE THEN
  6954. true := TRUE
  6955. END;
  6956. END;
  6957. StatementSequence(ifPart.statements);
  6958. currentIsUnreachable := prevUnreachable;
  6959. END IfPart;
  6960. (** check and resolve if statement
  6961. - check if parts and else part statement sequence
  6962. **)
  6963. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6964. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6965. BEGIN
  6966. prevUnreachable := currentIsUnreachable;
  6967. ifPartTrue := FALSE;
  6968. IfPart(ifStatement.ifPart,ifPartTrue);
  6969. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6970. elsif := ifStatement.GetElsifPart(i);
  6971. IfPart(elsif,ifPartTrue);
  6972. END;
  6973. IF ifStatement.elsePart # NIL THEN
  6974. IF ifPartTrue THEN
  6975. currentIsUnreachable := TRUE
  6976. END;
  6977. StatementSequence(ifStatement.elsePart)
  6978. END;
  6979. currentIsUnreachable := prevUnreachable;
  6980. END VisitIfStatement;
  6981. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6982. VAR variable: SyntaxTree.Designator;
  6983. type,variableType: SyntaxTree.Type;
  6984. withEntry: WithEntry;
  6985. BEGIN
  6986. variable := ResolveDesignator(withPart.variable);
  6987. variableType := variable.type.resolved;
  6988. withPart.SetVariable(variable);
  6989. type := ResolveType(withPart.type);
  6990. withPart.SetType(type);
  6991. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6992. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6993. END;
  6994. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6995. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6996. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6997. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6998. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  6999. IF VerboseErrorMessage THEN
  7000. Printout.Info("variable",variable)
  7001. END;
  7002. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7003. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7004. IF VerboseErrorMessage THEN
  7005. Printout.Info("variable",variable);
  7006. Printout.Info("type",type);
  7007. END;
  7008. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7009. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7010. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7011. IF VerboseErrorMessage THEN
  7012. Printout.Info("variable",variable);
  7013. Printout.Info("type",type);
  7014. END;
  7015. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7016. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7017. ELSE
  7018. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7019. NEW(withEntry);
  7020. withEntry.previous := withEntries;
  7021. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7022. withEntry.type := type;
  7023. withEntries := withEntry;
  7024. StatementSequence(withPart.statements);
  7025. withEntries := withEntries.previous;
  7026. END;
  7027. END WithPart;
  7028. (** check and resolve with statement WITH variable: type DO ... END;
  7029. - check type and variable
  7030. - check that variable type is type extension of type
  7031. - check that variable is a variable
  7032. - enter new with scope and enter guardedVariable with same name and reference to variable
  7033. - create if statement:
  7034. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7035. **)
  7036. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7037. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7038. BEGIN
  7039. prevScope := currentScope; symbol := NIL;
  7040. FOR i := 0 TO withStatement.WithParts()-1 DO
  7041. WithPart(withStatement.GetWithPart(i),symbol);
  7042. END;
  7043. IF withStatement.elsePart # NIL THEN
  7044. StatementSequence(withStatement.elsePart)
  7045. END;
  7046. currentScope := prevScope;
  7047. END VisitWithStatement;
  7048. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7049. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7050. - check 'first' < 'last' and no overlaps between different case labels
  7051. - check statement sequence
  7052. **)
  7053. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7054. VAR
  7055. i: LONGINT;
  7056. position: LONGINT;
  7057. expression, left, right: SyntaxTree.Expression;
  7058. expressionType: SyntaxTree.Type;
  7059. l, r: LONGINT;
  7060. cl, cr: CHAR;
  7061. thiscases: SyntaxTree.CaseConstant;
  7062. BEGIN
  7063. thiscases := NIL;
  7064. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7065. expression := casePart.elements.GetExpression(i);
  7066. position := expression.position;
  7067. (* set context of range *)
  7068. IF expression IS SyntaxTree.RangeExpression THEN
  7069. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7070. END;
  7071. expression := ResolveExpression(expression);
  7072. IF expression = SyntaxTree.invalidExpression THEN
  7073. (* error already reported *)
  7074. expressionType := SyntaxTree.invalidType;
  7075. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7076. (* read out 'first' and 'last' *)
  7077. left := expression(SyntaxTree.RangeExpression).first;
  7078. right := expression(SyntaxTree.RangeExpression).last;
  7079. (* guaranteed by VisitRangeExpression: *)
  7080. ASSERT((left # NIL) & (right # NIL));
  7081. ASSERT(left.type.resolved = right.type.resolved);
  7082. left := CompatibleConversion(left.position, left, type);
  7083. right := CompatibleConversion(right.position, right, type);
  7084. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7085. expression(SyntaxTree.RangeExpression).SetLast(right);
  7086. expressionType := RegularType(position,left.type);
  7087. ELSE
  7088. expression := ConstantExpression(expression);
  7089. expression := CompatibleConversion(expression.position, expression, type);
  7090. (*
  7091. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7092. left := Global.NewCharacterValue(system,expression.position,cl);
  7093. expression := casePart.elements.GetExpression(i);
  7094. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7095. expression := left
  7096. END;
  7097. *)
  7098. casePart.elements.SetExpression(i,expression);
  7099. left := expression; right := expression;
  7100. expressionType := RegularType(position,expression.type)
  7101. END;
  7102. IF (expressionType = SyntaxTree.invalidType) THEN
  7103. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7104. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7105. expression := SyntaxTree.invalidExpression;
  7106. ELSE
  7107. l := 0; r := 0;
  7108. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7109. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7110. l := ORD(cl); r := ORD(cr);
  7111. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7112. ELSE
  7113. expression := SyntaxTree.invalidExpression
  7114. END;
  7115. IF expression # SyntaxTree.invalidExpression THEN
  7116. IF l>r THEN
  7117. Error(position,Diagnostics.Invalid,"empty case label")
  7118. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7119. Error(position,Diagnostics.Invalid,"duplicate case label");
  7120. ELSE
  7121. IF l < min THEN min := l END;
  7122. IF r > max THEN max := r END;
  7123. END;
  7124. END;
  7125. END;
  7126. casePart.elements.SetExpression(i,expression);
  7127. END;
  7128. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7129. casePart.SetConstants(thiscases);
  7130. StatementSequence(casePart.statements);
  7131. END CasePart;
  7132. (** check and resolve case statement CASE variable OF ... END;
  7133. - check variable
  7134. - check case parts
  7135. **)
  7136. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7137. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7138. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7139. BEGIN
  7140. expression := ResolveExpression(caseStatement.variable);
  7141. type := RegularType(expression.position,expression.type);
  7142. IF type = SyntaxTree.invalidType THEN
  7143. expression := SyntaxTree.invalidExpression;
  7144. ELSIF IsIntegerType(type) THEN
  7145. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7146. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7147. (*
  7148. expression := Global.NewCharacterValue(system,expression.position,ch);
  7149. *)
  7150. type := expression.type;
  7151. ELSIF IsCharacterType(type) THEN
  7152. ELSIF IsEnumerationType(type) THEN
  7153. ELSE
  7154. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7155. expression := SyntaxTree.invalidExpression;
  7156. END;
  7157. caseStatement.SetVariable(expression);
  7158. caseList := NIL;
  7159. min := MAX(LONGINT); max := MIN(LONGINT);
  7160. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7161. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7162. END;
  7163. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7164. msg := "huge sparse case table ";
  7165. Strings.AppendInt(msg, max-min);
  7166. Strings.Append(msg,"/");
  7167. Strings.AppendInt(msg, caseStatement.CaseParts());
  7168. Warning(caseStatement.position,msg);
  7169. END;
  7170. caseStatement.SetMinMax(min,max);
  7171. StatementSequence(caseStatement.elsePart);
  7172. IF expression.resolved # NIL THEN
  7173. IF IsCharacterValue(expression,ch) THEN
  7174. l := ORD(ch)
  7175. ELSIF IsIntegerValue(expression,l) THEN
  7176. END;
  7177. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7178. END;
  7179. END VisitCaseStatement;
  7180. (** check and resolve while statement
  7181. - check condition
  7182. - check statement sequence
  7183. **)
  7184. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7185. VAR prevIsUnreachable,b: BOOLEAN;
  7186. BEGIN
  7187. prevIsUnreachable := currentIsUnreachable;
  7188. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7189. IF IsBooleanValue(whileStatement.condition,b) THEN
  7190. IF b=FALSE THEN
  7191. currentIsUnreachable := TRUE
  7192. END;
  7193. END;
  7194. StatementSequence(whileStatement.statements);
  7195. currentIsUnreachable := prevIsUnreachable
  7196. END VisitWhileStatement;
  7197. (** check and resolve repeat statement
  7198. - check condition
  7199. - check statement sequence
  7200. **)
  7201. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7202. BEGIN
  7203. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7204. StatementSequence(repeatStatement.statements);
  7205. END VisitRepeatStatement;
  7206. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7207. VAR withEntry: WithEntry;
  7208. BEGIN
  7209. withEntry := withEntries;
  7210. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7211. withEntry := withEntry.previous
  7212. END;
  7213. IF withEntry = NIL THEN RETURN FALSE
  7214. ELSE
  7215. type := withEntry.type;
  7216. RETURN TRUE
  7217. END;
  7218. END GetGuard;
  7219. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7220. - check that variable is an integer variable
  7221. - check that from is integer typed with compatible type
  7222. - check that to has compatible type
  7223. - check that by is constant integer with compatible type
  7224. **)
  7225. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7226. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7227. BEGIN
  7228. designator := ResolveDesignator(forStatement.variable);
  7229. type := SyntaxTree.invalidType;
  7230. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7231. designator := SyntaxTree.invalidDesignator;
  7232. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7233. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7234. designator := SyntaxTree.invalidDesignator;
  7235. ELSIF CheckVariable(designator) THEN
  7236. type := designator.type;
  7237. END;
  7238. forStatement.SetVariable(designator);
  7239. expression := ResolveExpression(forStatement.from);
  7240. IF expression = SyntaxTree.invalidExpression THEN
  7241. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7242. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7243. expression := SyntaxTree.invalidExpression;
  7244. ELSIF type # SyntaxTree.invalidType THEN
  7245. expression := NewConversion(expression.position,expression,type,NIL)
  7246. END;
  7247. forStatement.SetFrom(expression);
  7248. expression := ResolveExpression(forStatement.to);
  7249. IF expression = SyntaxTree.invalidExpression THEN
  7250. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7251. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7252. expression := SyntaxTree.invalidExpression;
  7253. ELSIF type # SyntaxTree.invalidType THEN
  7254. expression := NewConversion(expression.position,expression,type,NIL)
  7255. END;
  7256. forStatement.SetTo(expression);
  7257. IF forStatement.by # NIL THEN
  7258. expression := ConstantInteger(forStatement.by);
  7259. ELSE
  7260. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7261. END;
  7262. IF expression = SyntaxTree.invalidExpression THEN
  7263. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7264. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7265. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7266. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7267. ELSIF type # SyntaxTree.invalidType THEN
  7268. expression := NewConversion(expression.position,expression,type,NIL)
  7269. END;
  7270. forStatement.SetBy(expression);
  7271. StatementSequence(forStatement.statements);
  7272. END VisitForStatement;
  7273. (** check and resolve loop statement LOOP StatementSequence END
  7274. - check statement sequence
  7275. **)
  7276. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7277. BEGIN
  7278. StatementSequence(loopStatement.statements)
  7279. END VisitLoopStatement;
  7280. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7281. BEGIN
  7282. StatementSequence(exitableBlock.statements);
  7283. END VisitExitableBlock;
  7284. (** check and resolve exit statement EXIT
  7285. - check that exit is within LOOP statement block
  7286. **)
  7287. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7288. VAR outer: SyntaxTree.Statement;
  7289. BEGIN
  7290. outer := exitStatement.outer;
  7291. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7292. outer := outer.outer;
  7293. END;
  7294. IF outer = NIL THEN
  7295. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7296. END;
  7297. END VisitExitStatement;
  7298. (** check and resolve return statement RETURN [expression]
  7299. - check expression (if any)
  7300. - check if in procedure scope
  7301. - if in procedure scope then check expression compatibility
  7302. - if not in procecdure scope then check on return without expression
  7303. **)
  7304. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7305. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7306. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7307. BEGIN
  7308. position := returnStatement.position;
  7309. expression := returnStatement.returnValue;
  7310. IF expression # NIL THEN
  7311. expression := ResolveExpression(expression);
  7312. returnStatement.SetReturnValue(expression);
  7313. END;
  7314. outer := returnStatement.outer;
  7315. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7316. outer := outer.outer
  7317. END;
  7318. IF (outer # NIL) THEN
  7319. scope := outer(SyntaxTree.Body).inScope;
  7320. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7321. IF (expression # NIL) THEN
  7322. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7323. END;
  7324. ELSE
  7325. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7326. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7327. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7328. END;
  7329. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7330. IF returnType # NIL THEN
  7331. returnType := returnType.resolved;
  7332. IF expression = NIL THEN
  7333. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7334. ELSIF expression.type = NIL THEN
  7335. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7336. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7337. Error(position,Diagnostics.Invalid,"return type not compatible");
  7338. IF VerboseErrorMessage THEN
  7339. Printout.Info("returnType",returnType);
  7340. Printout.Info("expression",expression);
  7341. END;
  7342. ELSE
  7343. expression := NewConversion(expression.position,expression,returnType,NIL);
  7344. returnStatement.SetReturnValue(expression);
  7345. END;
  7346. ELSIF expression # NIL THEN
  7347. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7348. END;
  7349. END;
  7350. END;
  7351. END VisitReturnStatement;
  7352. (** check and resolve await statement AWAIT(condition: Expression)
  7353. - check await condition
  7354. **)
  7355. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7356. VAR condition: SyntaxTree.Expression;
  7357. BEGIN
  7358. condition := ResolveCondition(awaitStatement.condition);
  7359. IF currentIsRealtime THEN
  7360. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7361. END;
  7362. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7363. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7364. END;
  7365. awaitStatement.SetCondition(condition);
  7366. END VisitAwaitStatement;
  7367. PROCEDURE CheckSystemImport(position: LONGINT);
  7368. VAR import: SyntaxTree.Import;
  7369. BEGIN
  7370. import := currentScope.ownerModule.moduleScope.firstImport;
  7371. WHILE(import # NIL) DO
  7372. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7373. RETURN;
  7374. END;
  7375. import := import.nextImport;
  7376. END;
  7377. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7378. END CheckSystemImport;
  7379. (** check and resolve code statement: do nothing, must be done by assembler
  7380. **)
  7381. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7382. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7383. BEGIN
  7384. CheckSystemImport(code.position);
  7385. FOR i := 0 TO code.inRules.Length()-1 DO
  7386. statement := code.inRules.GetStatement(i);
  7387. IF statement IS SyntaxTree.Assignment THEN
  7388. WITH statement: SyntaxTree.Assignment DO
  7389. statement.SetRight(ResolveExpression(statement.right));
  7390. END;
  7391. ELSE
  7392. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7393. END;
  7394. END;
  7395. FOR i := 0 TO code.outRules.Length()-1 DO
  7396. statement := code.outRules.GetStatement(i);
  7397. IF statement IS SyntaxTree.Assignment THEN
  7398. WITH statement: SyntaxTree.Assignment DO
  7399. statement.SetLeft(ResolveDesignator(statement.left));
  7400. END;
  7401. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7402. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7403. ELSE
  7404. Printout.Info("out statement ", statement);
  7405. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7406. END;
  7407. END;
  7408. END VisitCode;
  7409. (** check and set flags of a statement block
  7410. - check for multiply occurence of a flag
  7411. - check and set priority only in bodies
  7412. - check for valid names
  7413. **)
  7414. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7415. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7416. flag: LONGINT; recordBody: SyntaxTree.Body;
  7417. PROCEDURE SetProtectedRecord;
  7418. VAR scope: SyntaxTree.Scope;
  7419. BEGIN
  7420. scope := currentScope;
  7421. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7422. scope := scope.outerScope
  7423. END;
  7424. IF scope # NIL THEN
  7425. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7426. END;
  7427. END SetProtectedRecord;
  7428. BEGIN
  7429. flags := {};
  7430. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7431. recordBody := block(SyntaxTree.Body)
  7432. ELSE
  7433. recordBody := NIL
  7434. END;
  7435. blockModifier := block.blockModifiers;
  7436. WHILE(blockModifier # NIL) DO
  7437. name := blockModifier.identifier;
  7438. expression := blockModifier.expression;
  7439. position := blockModifier.position;
  7440. flag := -1;
  7441. IF name=Global.NamePriority THEN
  7442. IF expression = NIL THEN
  7443. Error(position,Diagnostics.Invalid,"missing priority expression");
  7444. ELSIF recordBody = NIL THEN
  7445. Error(position,Diagnostics.Invalid,"priority not on record body");
  7446. ELSIF recordBody.priority # NIL THEN
  7447. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7448. ELSE
  7449. recordBody.SetPriority(expression);
  7450. END;
  7451. ELSIF expression # NIL THEN
  7452. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7453. ELSIF name=Global.NameExclusive THEN
  7454. IF block.isExclusive THEN
  7455. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7456. END;
  7457. block.SetExclusive(TRUE); SetProtectedRecord;
  7458. ELSIF name=Global.NameActive THEN
  7459. IF recordBody = NIL THEN
  7460. Error(position,Diagnostics.Invalid,"active not in record body");
  7461. ELSIF recordBody.isActive THEN
  7462. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7463. ELSE
  7464. recordBody.SetActive(TRUE); SetProtectedRecord;
  7465. END;
  7466. ELSIF name=Global.NameSafe THEN
  7467. IF recordBody = NIL THEN
  7468. Error(position,Diagnostics.Invalid,"safe not in record body");
  7469. ELSIF recordBody.isSafe THEN
  7470. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7471. ELSE
  7472. recordBody.SetSafe(TRUE);
  7473. SetProtectedRecord;
  7474. END;
  7475. ELSIF name=Global.NameRealtime THEN
  7476. IF recordBody = NIL THEN
  7477. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7478. ELSIF recordBody.isRealtime THEN
  7479. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7480. ELSE
  7481. recordBody.SetRealtime(TRUE);
  7482. block.SetRealtime(TRUE);
  7483. END;
  7484. ELSIF name=Global.NameUnchecked THEN
  7485. IF block.isUnchecked THEN
  7486. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7487. ELSE
  7488. block.SetUnchecked(TRUE);
  7489. END;
  7490. ELSIF (name=Global.NameUncooperative) THEN
  7491. IF block.isUncooperative THEN
  7492. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7493. ELSE
  7494. block.SetUncooperative(TRUE);
  7495. END;
  7496. ELSE
  7497. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7498. END;
  7499. blockModifier := blockModifier.nextModifier;
  7500. END;
  7501. END BlockFlags;
  7502. (** check and resolve statement block
  7503. - check flags (exclusive)
  7504. - check statement sequence
  7505. **)
  7506. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7507. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7508. BEGIN
  7509. BlockFlags(statementBlock);
  7510. IF statementBlock.isExclusive THEN
  7511. (* check that not in exclusive block *)
  7512. IF currentIsExclusive THEN
  7513. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7514. ELSIF currentIsRealtime THEN
  7515. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7516. END;
  7517. END;
  7518. recentExclusive := currentIsExclusive;
  7519. recentUnreachable := currentIsUnreachable;
  7520. recentRealtime := currentIsRealtime;
  7521. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7522. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7523. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7524. StatementSequence(statementBlock.statements);
  7525. currentIsRealtime := recentRealtime;
  7526. currentIsExclusive := recentExclusive;
  7527. currentIsUnreachable := recentUnreachable;
  7528. END VisitStatementBlock;
  7529. (** check and resolve body
  7530. - check flags (active, priority, safe)
  7531. - check body and finally part
  7532. **)
  7533. PROCEDURE Body(body: SyntaxTree.Body);
  7534. BEGIN
  7535. VisitStatementBlock(body);
  7536. IF body.isActive THEN
  7537. IF ~currentIsBodyProcedure THEN
  7538. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7539. ELSIF body.priority # NIL THEN
  7540. body.SetPriority(ConstantInteger(body.priority));
  7541. END;
  7542. ELSIF body.isSafe THEN
  7543. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7544. ELSIF body.priority # NIL THEN
  7545. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7546. END;
  7547. IF body.code # NIL THEN
  7548. CheckSystemImport(body.position);
  7549. END;
  7550. StatementSequence(body.finally)
  7551. END Body;
  7552. (*** scopes ***)
  7553. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7554. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7555. VAR duplicateSymbol: BOOLEAN;
  7556. BEGIN
  7557. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7558. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7559. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7560. END;
  7561. scope.EnterSymbol(symbol,duplicateSymbol);
  7562. IF ~allowDuplicate & duplicateSymbol THEN
  7563. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7564. IF VerboseErrorMessage THEN
  7565. Printout.Info("multiply defined identifier",symbol);
  7566. Printout.Info("in scope",scope);
  7567. END;
  7568. END;
  7569. END Register;
  7570. (**
  7571. implementation: check and resolve an implementation part
  7572. **)
  7573. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7574. move implementation checker to a separate object ? *)
  7575. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7576. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7577. BEGIN
  7578. prevIsRealtime := currentIsRealtime;
  7579. prevIsBodyProcedure := currentIsBodyProcedure;
  7580. prevIsCellNet := currentIsCellNet;
  7581. prevScope := currentScope;
  7582. currentScope := scope;
  7583. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7584. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7585. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7586. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7587. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7588. (*
  7589. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7590. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7591. END;
  7592. *)
  7593. END;
  7594. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7595. (* module body, record bodies are wrapped into an artifical procedure *)
  7596. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7597. Body(scope(SyntaxTree.ProcedureScope).body)
  7598. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7599. Body(scope(SyntaxTree.ProcedureScope).body)
  7600. END;
  7601. END;
  7602. currentScope := prevScope;
  7603. currentIsRealtime := prevIsRealtime;
  7604. currentIsBodyProcedure := prevIsBodyProcedure;
  7605. currentIsCellNet := prevIsCellNet;
  7606. END Implementation;
  7607. (** implementation phase:
  7608. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7609. **)
  7610. PROCEDURE Implementations(x: SyntaxTree.Module);
  7611. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7612. BEGIN
  7613. prevPhase := phase;
  7614. phase := InlinePhase;
  7615. scope := x.firstScope;
  7616. WHILE(scope # NIL) DO
  7617. Implementation(scope);
  7618. scope := scope.nextScope;
  7619. END;
  7620. phase := ImplementationPhase;
  7621. scope := x.firstScope;
  7622. WHILE(scope # NIL) DO
  7623. Implementation(scope);
  7624. scope := scope.nextScope;
  7625. END;
  7626. phase := prevPhase;
  7627. END Implementations;
  7628. (** declaration phase:
  7629. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7630. - import lists (for module scopes)
  7631. - parameter list (for procedure scopes)
  7632. - constant declarations
  7633. - type declarations
  7634. - variable declarations
  7635. - procedure declarations
  7636. preformed in two stages:
  7637. - first all symbols are entered into the symbol table (with uniqueness check),
  7638. - then all symbols are resolved
  7639. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7640. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7641. **)
  7642. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7643. VAR
  7644. constant: SyntaxTree.Constant;
  7645. typeDeclaration: SyntaxTree.TypeDeclaration;
  7646. variable: SyntaxTree.Variable;
  7647. procedure: SyntaxTree.Procedure;
  7648. prevScope: SyntaxTree.Scope;
  7649. parameter: SyntaxTree.Parameter;
  7650. import: SyntaxTree.Import;
  7651. symbol: SyntaxTree.Symbol;
  7652. prevPhase: LONGINT;
  7653. prevError : BOOLEAN;
  7654. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7655. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7656. BEGIN
  7657. IF type.baseType # NIL THEN
  7658. baseType := type.baseType.resolved;
  7659. IF baseType IS SyntaxTree.PointerType THEN
  7660. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7661. END;
  7662. IF baseType IS SyntaxTree.CellType THEN
  7663. DeclareCell(baseType(SyntaxTree.CellType));
  7664. END;
  7665. END;
  7666. parameter := type.firstParameter;
  7667. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7668. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7669. variable.SetType(parameter.type);
  7670. variable.SetAccess(SyntaxTree.Hidden);
  7671. variable.SetModifiers(parameter.modifiers);
  7672. currentScope.PushVariable(variable);
  7673. (*
  7674. Register(parameter,scope, FALSE);
  7675. *)
  7676. parameter := parameter.nextParameter;
  7677. END;
  7678. property := type.firstProperty;
  7679. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7680. variable := currentScope.FindVariable(property.name);
  7681. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7682. prop := variable(SyntaxTree.Property);
  7683. ELSE (* add, duplicate symbols detection later *)
  7684. prop := SyntaxTree.NewProperty(property.position, property.name);
  7685. currentScope.PushVariable(prop);
  7686. END;
  7687. prop.SetType(property.type);
  7688. prop.SetValue(property.value);
  7689. prop.SetAccess(SyntaxTree.Hidden);
  7690. property := property.nextProperty;
  7691. END;
  7692. END DeclareCell;
  7693. BEGIN
  7694. prevError := error;
  7695. prevPhase := phase;
  7696. phase := DeclarationPhase;
  7697. prevScope := currentScope;
  7698. currentScope := scope;
  7699. error := FALSE;
  7700. (* first enter all symbols in scope *)
  7701. IF scope IS SyntaxTree.ModuleScope THEN
  7702. (* treat imports first for a module scope, , set default context if necessary *)
  7703. import := scope(SyntaxTree.ModuleScope).firstImport;
  7704. WHILE(import # NIL) DO
  7705. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7706. Register(import, currentScope, FALSE);
  7707. import := import.nextImport;
  7708. END;
  7709. import := scope(SyntaxTree.ModuleScope).firstImport;
  7710. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7711. ResolveSymbol(import);
  7712. import := import.nextImport;
  7713. END;
  7714. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7715. (* enter parameters for a procedure scope *)
  7716. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7717. WHILE(parameter # NIL) DO
  7718. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7719. END;
  7720. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7721. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7722. ELSIF scope IS SyntaxTree.CellScope THEN
  7723. IF~skipImplementation THEN
  7724. import := scope(SyntaxTree.CellScope).firstImport;
  7725. WHILE(import # NIL) DO
  7726. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7727. Register(import, currentScope, FALSE);
  7728. import := import.nextImport;
  7729. END;
  7730. import := scope(SyntaxTree.CellScope).firstImport;
  7731. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7732. ResolveSymbol(import);
  7733. import := import.nextImport;
  7734. END;
  7735. END;
  7736. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7737. END;
  7738. IF error THEN RETURN END;
  7739. (* constants *)
  7740. constant := scope.firstConstant;
  7741. WHILE (constant # NIL) DO
  7742. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7743. END;
  7744. (* type declarations *)
  7745. typeDeclaration := scope.firstTypeDeclaration;
  7746. WHILE (typeDeclaration # NIL) DO
  7747. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7748. END;
  7749. (* variables *)
  7750. variable := scope.firstVariable;
  7751. WHILE (variable # NIL) DO
  7752. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7753. END;
  7754. (* procedures *)
  7755. procedure := scope.firstProcedure;
  7756. WHILE (procedure # NIL) DO
  7757. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7758. END;
  7759. IF ~skipImplementation THEN
  7760. (* now process all symbols without any presumption on the order *)
  7761. symbol := scope.firstSymbol;
  7762. WHILE(symbol # NIL) DO
  7763. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7764. ResolveSymbol(symbol);
  7765. END;
  7766. symbol := symbol.nextSymbol;
  7767. END;
  7768. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7769. symbol := scope.firstSymbol;
  7770. WHILE symbol # NIL DO
  7771. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7772. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7773. pointerFixes.Add(symbol, currentScope);
  7774. END;
  7775. IF ~symbol.type.resolved.isRealtime THEN
  7776. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7777. END;
  7778. END;
  7779. symbol := symbol.nextSymbol
  7780. END;
  7781. END;
  7782. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7783. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7784. END;
  7785. IF (scope.ownerModule # NIL) THEN
  7786. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7787. scope.ownerModule.AddScope(scope);
  7788. END;
  7789. END;
  7790. phase := prevPhase;
  7791. currentScope := prevScope;
  7792. error := error OR prevError;
  7793. END Declarations;
  7794. (* nopov *)
  7795. (** check if all operators from one module are compatible to the ones in the other module
  7796. - check if there are not multiple operators with the same signature
  7797. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7798. - check for all operators whose signatures are compatible, whether the return types are compatible
  7799. note that:
  7800. - the return type is not considered to be part of the signature
  7801. - two signatures are considered compatible, if all of the operands are compatible
  7802. **)
  7803. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7804. VAR
  7805. thisOperator, thatOperator: SyntaxTree.Operator;
  7806. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7807. thisParameter, thatParameter: SyntaxTree.Parameter;
  7808. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7809. i: LONGINT;
  7810. BEGIN
  7811. currentScope := thisModuleScope;
  7812. hasError := FALSE;
  7813. (* go through all operators in the other module *)
  7814. thatOperator := thatModuleScope.firstOperator;
  7815. WHILE (thatOperator # NIL) & ~hasError DO
  7816. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7817. (* the other operator is accessible *)
  7818. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7819. (* the other operator is not the conversion operator *)
  7820. (* go through all operators in this module *)
  7821. thisOperator := thisModuleScope.firstOperator;
  7822. WHILE (thisOperator # NIL) & ~hasError DO
  7823. IF thisOperator # thatOperator THEN
  7824. (* the operators are not the same *)
  7825. IF thisOperator.name = thatOperator.name THEN
  7826. (* the operators share the same identifier *)
  7827. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7828. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7829. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7830. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7831. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7832. (* both operators have the same paramter count *)
  7833. thisParameter := thisProcedureType.firstParameter;
  7834. thatParameter := thatProcedureType.firstParameter;
  7835. operandsAreEqual := TRUE;
  7836. operandsAreCompatible := TRUE;
  7837. (* go through all parameters *)
  7838. FOR i := 1 TO thisProcedureType.numberParameters DO
  7839. ASSERT(thatParameter # NIL);
  7840. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7841. operandsAreEqual := FALSE;
  7842. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7843. operandsAreCompatible := FALSE
  7844. END
  7845. END;
  7846. thisParameter := thisParameter.nextParameter;
  7847. thatParameter := thatParameter.nextParameter
  7848. END;
  7849. IF operandsAreEqual THEN
  7850. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7851. hasError := TRUE
  7852. ELSIF operandsAreCompatible THEN
  7853. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7854. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7855. hasError := TRUE
  7856. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7857. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7858. hasError := TRUE
  7859. END
  7860. END
  7861. END
  7862. END
  7863. END;
  7864. thisOperator := thisOperator.nextOperator
  7865. END
  7866. END
  7867. END;
  7868. thatOperator := thatOperator.nextOperator
  7869. END
  7870. END CheckInterOperatorConformity;
  7871. (** check module:
  7872. - check module declaration
  7873. - add context, if necessary
  7874. - remove module from import cache, if necessary
  7875. - check declarations
  7876. - resolve all type fixes
  7877. - check implementation (bodies)
  7878. **)
  7879. PROCEDURE Module*(x: SyntaxTree.Module);
  7880. VAR (* nopov *)
  7881. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7882. BEGIN
  7883. prevScope := currentScope;
  7884. prevIsCellNet := currentIsCellNet;
  7885. module := x;
  7886. ASSERT(x # NIL);
  7887. global := system.globalScope[x.case];
  7888. x.moduleScope.SetGlobalScope(global);
  7889. currentScope := global;
  7890. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7891. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7892. RemoveModuleFromCache(importCache,x);
  7893. Declarations(x.moduleScope, FALSE);
  7894. FixTypes();
  7895. IF module.isCellNet THEN
  7896. currentIsCellNet := TRUE;
  7897. modifier := x.modifiers;
  7898. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7899. CheckModifiers(modifier, FALSE);
  7900. END;
  7901. (* nopov *)
  7902. IF ~error THEN
  7903. (* check if operators conform to each other within this module *)
  7904. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7905. (* go through all imports *)
  7906. import := x.moduleScope.firstImport;
  7907. WHILE import # NIL DO
  7908. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7909. (* check if all operators in this module conform to the ones of the imported module *)
  7910. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7911. END;
  7912. import := import.nextImport
  7913. END;
  7914. END;
  7915. Implementations(x);
  7916. module := NIL;
  7917. currentIsCellNet := prevIsCellNet;
  7918. currentScope := prevScope;
  7919. END Module;
  7920. END Checker;
  7921. Warnings*=OBJECT (SyntaxTree.Visitor)
  7922. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7923. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7924. BEGIN
  7925. SELF.diagnostics := diagnostics
  7926. END InitWarnings;
  7927. (** types *)
  7928. PROCEDURE Type(x: SyntaxTree.Type);
  7929. BEGIN x.Accept(SELF)
  7930. END Type;
  7931. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7932. BEGIN END VisitType;
  7933. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7934. BEGIN END VisitBasicType;
  7935. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7936. BEGIN END VisitCharacterType;
  7937. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7938. BEGIN END VisitIntegerType;
  7939. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7940. BEGIN END VisitFloatType;
  7941. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7942. BEGIN END VisitQualifiedType;
  7943. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7944. BEGIN END VisitStringType;
  7945. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7946. BEGIN END VisitEnumerationType;
  7947. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7948. BEGIN END VisitRangeType;
  7949. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7950. BEGIN
  7951. IF ~(SyntaxTree.Warned IN x.state) THEN
  7952. x.SetState(SyntaxTree.Warned);
  7953. Type(x.arrayBase);
  7954. END;
  7955. END VisitArrayType;
  7956. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7957. BEGIN
  7958. IF ~(SyntaxTree.Warned IN x.state) THEN
  7959. x.SetState(SyntaxTree.Warned);
  7960. Type(x.arrayBase);
  7961. END;
  7962. END VisitMathArrayType;
  7963. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7964. BEGIN
  7965. IF ~(SyntaxTree.Warned IN x.state) THEN
  7966. x.SetState(SyntaxTree.Warned);
  7967. Type(x.pointerBase);
  7968. END;
  7969. END VisitPointerType;
  7970. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7971. BEGIN Scope(x.recordScope) END VisitRecordType;
  7972. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7973. BEGIN Scope(x.cellScope) END VisitCellType;
  7974. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7975. BEGIN END VisitProcedureType;
  7976. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7977. VAR msg: ARRAY 256 OF CHAR;
  7978. BEGIN
  7979. Global.GetSymbolName(x,msg);
  7980. Strings.Append(msg," ");
  7981. Strings.Append(msg,text);
  7982. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7983. END Warning;
  7984. (** symbols *)
  7985. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7986. BEGIN
  7987. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7988. IF ~(x IS SyntaxTree.Parameter) THEN
  7989. Warning(x,"never used");
  7990. END;
  7991. END;
  7992. x.Accept(SELF);
  7993. END Symbol;
  7994. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7995. BEGIN END VisitSymbol;
  7996. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7997. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7998. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7999. BEGIN END VisitConstant;
  8000. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  8001. BEGIN END VisitVariable;
  8002. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8003. BEGIN END VisitProperty;
  8004. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8005. BEGIN END VisitParameter;
  8006. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8007. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8008. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8009. BEGIN END VisitOperator;
  8010. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8011. BEGIN END VisitImport;
  8012. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8013. VAR
  8014. symbol: SyntaxTree.Symbol;
  8015. BEGIN
  8016. symbol := scope.firstSymbol;
  8017. WHILE(symbol # NIL) DO
  8018. Symbol(symbol);
  8019. symbol := symbol.nextSymbol;
  8020. END;
  8021. END Scope;
  8022. PROCEDURE Module*(x: SyntaxTree.Module);
  8023. BEGIN
  8024. SELF.module := x;
  8025. Scope(x.moduleScope);
  8026. END Module;
  8027. END Warnings;
  8028. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8029. BEGIN
  8030. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8031. END IsOberonInline;
  8032. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8033. BEGIN
  8034. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8035. END Resolved;
  8036. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8037. VAR i: LONGINT;
  8038. BEGIN
  8039. i := 1;
  8040. WHILE i < x DO
  8041. i := i *2
  8042. END;
  8043. RETURN i=x
  8044. END PowerOf2;
  8045. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8046. BEGIN
  8047. RETURN
  8048. (scope # NIL) &
  8049. (scope IS SyntaxTree.ModuleScope)
  8050. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8051. OR
  8052. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8053. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8054. END IsCellNetScope;
  8055. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8056. BEGIN
  8057. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8058. END IsCellScope;
  8059. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8060. BEGIN
  8061. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8062. RETURN (scope # NIL) & IsCellNetScope(scope)
  8063. END InCellNetScope;
  8064. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8065. BEGIN
  8066. ASSERT(size MOD system.dataUnit = 0);
  8067. RETURN size DIV system.dataUnit
  8068. END ToMemoryUnits;
  8069. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8070. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8071. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8072. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8073. BEGIN
  8074. IF t = NIL THEN
  8075. RETURN TRUE
  8076. ELSE
  8077. t := t.resolved;
  8078. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8079. END;
  8080. END TypeAllowed;
  8081. BEGIN
  8082. type := type.resolved;
  8083. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8084. RETURN FALSE
  8085. ELSE
  8086. procedureType := type(SyntaxTree.ProcedureType);
  8087. numberParameters := procedureType.numberParameters;
  8088. RETURN
  8089. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8090. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8091. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8092. END;
  8093. END GetProcedureAllowed;
  8094. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8095. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8096. VAR import: SyntaxTree.Import;
  8097. BEGIN
  8098. import := importCache.ImportByModuleName(x.name,x.context);
  8099. IF import # NIL THEN
  8100. importCache.RemoveImporters(x.name,x.context);
  8101. END;
  8102. END RemoveModuleFromCache;
  8103. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8104. (* to <- this assignment compatibility *)
  8105. VAR result: BOOLEAN;
  8106. BEGIN
  8107. IF this= NIL THEN result := (to=NIL)
  8108. ELSIF to=NIL THEN result := FALSE
  8109. ELSE
  8110. (*! will be replaced by this:
  8111. ELSE result := this.CompatibleTo(to.resolved);
  8112. *)
  8113. this := this.resolved; to := to.resolved;
  8114. IF to=SyntaxTree.invalidType THEN result := FALSE
  8115. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8116. ELSIF to = this THEN
  8117. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8118. ELSIF to IS SyntaxTree.BasicType THEN
  8119. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8120. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8121. result := this.CompatibleTo(to.resolved)
  8122. ELSE
  8123. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8124. END
  8125. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8126. result := to.sizeInBits = this.sizeInBits;
  8127. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8128. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8129. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8130. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8131. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8132. result := TRUE;
  8133. ELSIF to IS SyntaxTree.AnyType THEN
  8134. 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);
  8135. ELSIF to IS SyntaxTree.ObjectType THEN
  8136. 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 *) ;
  8137. ELSIF to IS SyntaxTree.ByteType THEN
  8138. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8139. ELSIF to IS SyntaxTree.CharacterType THEN
  8140. result := IsCharacterType(this)
  8141. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8142. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8143. ELSIF (to IS SyntaxTree.AddressType) & ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.SizeType) OR IsPointerType(this) OR (this IS SyntaxTree.ProcedureType)) THEN
  8144. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8145. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8146. result := TRUE;
  8147. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8148. result := TRUE;
  8149. ELSE
  8150. result := FALSE
  8151. END;
  8152. ELSIF to IS SyntaxTree.PointerType THEN
  8153. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8154. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8155. & (~to.isRealtime OR this.isRealtime);
  8156. ELSIF to IS SyntaxTree.ProcedureType THEN
  8157. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8158. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8159. & (~to.isRealtime OR this.isRealtime)
  8160. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8161. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8162. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8163. ELSIF to IS SyntaxTree.RecordType THEN
  8164. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8165. ELSIF to IS SyntaxTree.ArrayType THEN
  8166. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8167. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8168. ELSIF StaticArrayCompatible(to, this) THEN
  8169. result := TRUE
  8170. ELSE
  8171. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8172. END;
  8173. ELSIF to IS SyntaxTree.MathArrayType THEN
  8174. IF this IS SyntaxTree.MathArrayType THEN
  8175. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8176. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8177. result := TRUE;
  8178. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8179. result := TRUE;
  8180. ELSE
  8181. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8182. END;
  8183. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8184. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8185. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8186. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8187. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8188. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8189. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8190. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8191. ELSE
  8192. result := FALSE
  8193. END;
  8194. (* an array-structured object type is compatible to the type of its array structure *)
  8195. ELSIF IsArrayStructuredObjectType(this) THEN
  8196. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8197. ELSE
  8198. result := FALSE;
  8199. END;
  8200. ELSIF to IS SyntaxTree.StringType THEN
  8201. result := FALSE;
  8202. ELSIF to IS SyntaxTree.EnumerationType THEN
  8203. result := IsEnumerationExtension(this,to);
  8204. ELSIF to IS SyntaxTree.PortType THEN
  8205. result := SameType(to, this)
  8206. ELSE
  8207. Printout.Info("CompatibleTo",to);
  8208. HALT(100); (* implement missing type check *)
  8209. END;
  8210. END;
  8211. RETURN result
  8212. END CompatibleTo;
  8213. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8214. VAR actualBase, formalBase: SyntaxTree.Type;
  8215. BEGIN
  8216. IF SameType(formal,actual) THEN
  8217. RETURN TRUE
  8218. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8219. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8220. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8221. RETURN
  8222. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8223. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8224. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8225. & StaticArrayCompatible(formalBase,actualBase)
  8226. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8227. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8228. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8229. RETURN
  8230. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8231. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8232. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8233. & StaticArrayCompatible(formalBase,actualBase)
  8234. ELSE RETURN FALSE
  8235. END;
  8236. END StaticArrayCompatible;
  8237. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8238. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8239. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8240. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8241. BEGIN
  8242. result := SameType(formal,actual);
  8243. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8244. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8245. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8246. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8247. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8248. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8249. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8250. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8251. & TC(formalBase, actualBase);
  8252. END;
  8253. RETURN result
  8254. END TC;
  8255. BEGIN
  8256. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8257. ELSE
  8258. arrayBase := formalType.arrayBase.resolved;
  8259. IF (actualType IS SyntaxTree.StringType) THEN
  8260. result := arrayBase IS SyntaxTree.CharacterType
  8261. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8262. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8263. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8264. result := TC(formalType, actualType);
  8265. ELSE
  8266. result := (arrayBase IS SyntaxTree.ByteType)
  8267. END;
  8268. END;
  8269. RETURN result
  8270. END OpenArrayCompatible;
  8271. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8272. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8273. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8274. BEGIN
  8275. IF actualType IS SyntaxTree.MathArrayType THEN
  8276. actualArray := actualType(SyntaxTree.MathArrayType);
  8277. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8278. (*
  8279. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8280. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8281. *)
  8282. actualBase := ArrayBase(actualType,Infinity);
  8283. formalBase := ArrayBase(formalType,Infinity);
  8284. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8285. ELSE
  8286. (*
  8287. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8288. *)
  8289. formalBase := Resolved(formalType.arrayBase);
  8290. actualBase := Resolved(actualArray.arrayBase);
  8291. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8292. (*
  8293. ARRAY [k] -> ARRAY [n]
  8294. *)
  8295. result := (formalType.staticLength = actualArray.staticLength)
  8296. ELSE
  8297. result := TRUE
  8298. END;
  8299. IF ~result THEN
  8300. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8301. ELSIF actualBase = NIL THEN result := FALSE
  8302. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8303. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8304. ELSE
  8305. result := SameType(formalBase,actualBase)
  8306. END;
  8307. END;
  8308. ELSE
  8309. result := FALSE
  8310. END;
  8311. RETURN result
  8312. END MathArrayCompatible;
  8313. (**
  8314. Math Array Type distance for assignments / parameter passings of the form
  8315. from -> to
  8316. variants:
  8317. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8318. allowed:
  8319. static -> static (& size match)
  8320. static -> open
  8321. static -> tensor
  8322. open -> open
  8323. open -> tensor
  8324. open -> static
  8325. tensor -> tensor
  8326. tensor -> open
  8327. tensor -> static
  8328. **)
  8329. (*! think about the metric here: is form matching more important than element type matching? *)
  8330. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8331. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8332. BEGIN
  8333. fromBase := Resolved(from.arrayBase);
  8334. toBase := Resolved(to.arrayBase);
  8335. i := Infinity;
  8336. IF from = to THEN
  8337. i := 0;
  8338. ELSIF (from.form = to.form) THEN
  8339. (* static -> static, open -> open, tensor -> tensor *)
  8340. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8341. IF fromBase = toBase THEN i := 0
  8342. ELSIF toBase = NIL THEN i := 1
  8343. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8344. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8345. ELSE
  8346. i := TypeDistance(system,fromBase, toBase, varpar);
  8347. END;
  8348. END;
  8349. ELSIF (to.form = SyntaxTree.Static) THEN
  8350. (* forbidden *)
  8351. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8352. (* static -> tensor, open -> tensor, tensor -> open *)
  8353. IF toBase=fromBase THEN i := 0;
  8354. ELSIF toBase = NIL THEN i := 1;
  8355. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8356. toBase := ArrayBase(toBase,Infinity);
  8357. IF (fromBase=toBase) THEN i := 0
  8358. ELSIF (toBase = NIL) THEN i:= 1
  8359. ELSIF (fromBase = NIL) THEN i := Infinity;
  8360. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8361. END;
  8362. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8363. fromBase := ArrayBase(fromBase,Infinity);
  8364. IF (fromBase=toBase) THEN i := 0
  8365. ELSIF (toBase = NIL) THEN i := 1
  8366. ELSIF (fromBase = NIL) THEN i := Infinity;
  8367. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8368. END;
  8369. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8370. END;
  8371. IF i # Infinity THEN INC(i,2) END;
  8372. ELSIF (from.form = SyntaxTree.Static) THEN
  8373. (* static -> open *)
  8374. IF toBase=fromBase THEN i := 0
  8375. ELSIF toBase = NIL THEN i := 1
  8376. ELSIF fromBase = NIL THEN i := Infinity
  8377. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8378. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8379. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8380. END;
  8381. IF i # Infinity THEN INC(i,1) END;
  8382. ELSE HALT(100); (* unknown case *)
  8383. END;
  8384. RETURN i;
  8385. END MathArrayTypeDistance;
  8386. (** compute and return the distance of two array types
  8387. - return the distance of the base types
  8388. **)
  8389. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8390. VAR i: LONGINT;
  8391. BEGIN
  8392. i := Infinity;
  8393. IF from = to THEN
  8394. i := 0
  8395. ELSE
  8396. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8397. (*
  8398. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8399. i := TypeDistance(from.base, to.base);
  8400. IF i >= 0 THEN INC(i) END
  8401. ELSIF (from.mode = open) & (to.mode = open) THEN
  8402. i := TypeDistance(from.base, to.base);
  8403. *)
  8404. END;
  8405. RETURN i
  8406. END ArrayTypeDistance;
  8407. (** compute the signature distance of a procedure and an actual parameter list
  8408. - if any of the parameters are not compatible, the result is infinite
  8409. - add up and return the distance over all parameters
  8410. **)
  8411. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8412. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8413. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8414. BEGIN
  8415. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8416. result := Infinity
  8417. ELSE
  8418. formalParameter := procedureType.firstParameter;
  8419. i := 0;
  8420. result := 0;
  8421. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8422. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8423. actualParameter := actualParameters.GetExpression(i);
  8424. ASSERT(formalParameter.type # NIL);
  8425. IF (actualParameter.type = NIL) THEN distance := Infinity
  8426. ELSE
  8427. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8428. END;
  8429. IF distance = Infinity THEN
  8430. result := Infinity;
  8431. ELSE
  8432. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8433. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8434. (* already handled varpar *)
  8435. (*
  8436. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8437. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8438. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8439. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8440. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8441. END;
  8442. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8443. result := Infinity
  8444. END;
  8445. *)
  8446. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8447. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8448. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8449. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8450. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8451. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8452. END;
  8453. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8454. result := Infinity
  8455. END;
  8456. ELSE
  8457. result := Infinity
  8458. END;
  8459. ELSE
  8460. INC(result,distance);
  8461. END;
  8462. END;
  8463. formalParameter := formalParameter.nextParameter; INC(i);
  8464. END;
  8465. END;
  8466. ASSERT(result >= 0);
  8467. RETURN result
  8468. END Distance;
  8469. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8470. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8471. BEGIN
  8472. IF right.numberParameters # (procedureType.numberParameters) THEN
  8473. result := Infinity
  8474. ELSE
  8475. formalParameter := procedureType.firstParameter;
  8476. rightParameter := right.firstParameter;
  8477. i := 0;
  8478. result := 0;
  8479. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8480. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8481. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8482. IF distance = Infinity THEN
  8483. result := Infinity;
  8484. ELSE
  8485. INC(result,distance);
  8486. END;
  8487. formalParameter := formalParameter.nextParameter;
  8488. rightParameter := rightParameter.nextParameter;
  8489. END;
  8490. END;
  8491. ASSERT(result >= 0);
  8492. RETURN result
  8493. END ProcedureTypeDistance;
  8494. (** compute and return the distance between two types, used for computation of signature distance
  8495. from -> to
  8496. **)
  8497. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8498. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8499. BEGIN
  8500. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8501. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8502. END;
  8503. i := Infinity;
  8504. IF from = to THEN
  8505. i := 0
  8506. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8507. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8508. i := Infinity;
  8509. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8510. i := 1
  8511. ELSIF (from IS SyntaxTree.StringType) THEN
  8512. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8513. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8514. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8515. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8516. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8517. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8518. i := 1
  8519. ELSIF (from IS SyntaxTree.NilType) THEN
  8520. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8521. (*
  8522. ELSIF (from = NoType) THEN
  8523. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8524. *)
  8525. ELSIF (from IS SyntaxTree.BasicType) THEN
  8526. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8527. IF varpar & (i # 0) THEN i := Infinity END;
  8528. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8529. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8530. ELSIF (from IS SyntaxTree.RecordType) THEN
  8531. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8532. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8533. IF to IS SyntaxTree.MathArrayType THEN
  8534. (*
  8535. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8536. i := Infinity;
  8537. ELSE
  8538. *)
  8539. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8540. (*
  8541. END;
  8542. *)
  8543. END
  8544. ELSIF (from IS SyntaxTree.PointerType) THEN
  8545. ptr := from(SyntaxTree.PointerType);
  8546. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8547. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8548. (* ELSE i := TypeDistance(ptr.base, to); *)
  8549. END
  8550. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8551. IF (to IS SyntaxTree.ProcedureType) THEN
  8552. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8553. END;
  8554. ELSIF (from IS SyntaxTree.PortType) THEN
  8555. IF (to IS SyntaxTree.PortType) THEN
  8556. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8557. i := 0;
  8558. END;
  8559. END;
  8560. (*no procedure test, procedure must be the same*)
  8561. END;
  8562. RETURN i
  8563. END TypeDistance;
  8564. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8565. BEGIN
  8566. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8567. END IsIntegerType;
  8568. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8569. BEGIN
  8570. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8571. END IsAddressType;
  8572. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8573. BEGIN
  8574. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8575. END IsSizeType;
  8576. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8577. BEGIN
  8578. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8579. END IsSignedIntegerType;
  8580. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8581. BEGIN
  8582. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8583. END IsUnsignedIntegerType;
  8584. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8585. VAR result: BOOLEAN;
  8586. BEGIN
  8587. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8588. value := x.resolved(SyntaxTree.IntegerValue).value;
  8589. result := TRUE
  8590. ELSE
  8591. result := FALSE
  8592. END;
  8593. RETURN result
  8594. END IsIntegerValue;
  8595. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8596. VAR result: BOOLEAN;
  8597. BEGIN
  8598. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8599. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8600. result := TRUE
  8601. ELSE
  8602. result := FALSE
  8603. END;
  8604. RETURN result
  8605. END IsEnumerationValue;
  8606. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8607. VAR result: BOOLEAN;
  8608. BEGIN
  8609. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8610. value := x.resolved(SyntaxTree.RealValue).value;
  8611. result := TRUE
  8612. ELSE
  8613. result := FALSE
  8614. END;
  8615. RETURN result
  8616. END IsRealValue;
  8617. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8618. VAR result: BOOLEAN;
  8619. BEGIN
  8620. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8621. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8622. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8623. result := TRUE
  8624. ELSE
  8625. result := FALSE
  8626. END;
  8627. RETURN result
  8628. END IsComplexValue;
  8629. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8630. VAR result: BOOLEAN;
  8631. BEGIN
  8632. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8633. value := x.resolved(SyntaxTree.CharacterValue).value;
  8634. result := TRUE
  8635. ELSE
  8636. result := FALSE
  8637. END;
  8638. RETURN result
  8639. END IsCharacterValue;
  8640. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8641. VAR result: BOOLEAN;
  8642. BEGIN
  8643. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8644. value := x.resolved(SyntaxTree.BooleanValue).value;
  8645. result := TRUE
  8646. ELSE
  8647. result := FALSE
  8648. END;
  8649. RETURN result
  8650. END IsBooleanValue;
  8651. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8652. VAR result: BOOLEAN;
  8653. BEGIN
  8654. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8655. value := x.resolved(SyntaxTree.SetValue).value;
  8656. result := TRUE
  8657. ELSE
  8658. result := FALSE
  8659. END;
  8660. RETURN result
  8661. END IsSetValue;
  8662. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8663. VAR result: BOOLEAN;
  8664. BEGIN
  8665. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8666. value := x.resolved(SyntaxTree.StringValue).value;
  8667. result := TRUE
  8668. ELSE
  8669. result := FALSE
  8670. END;
  8671. RETURN result
  8672. END IsStringValue;
  8673. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8674. BEGIN
  8675. x := x.resolved;
  8676. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8677. END Indexable;
  8678. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8679. BEGIN
  8680. RETURN t1.SameType(t2.resolved);
  8681. END SameType;
  8682. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8683. BEGIN
  8684. IF t IS SyntaxTree.MathArrayType THEN
  8685. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8686. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8687. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8688. DEC(max);
  8689. END;
  8690. ELSIF t IS SyntaxTree.ArrayType THEN
  8691. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8692. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8693. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8694. END;
  8695. END;
  8696. RETURN t;
  8697. END ArrayBase;
  8698. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8699. BEGIN
  8700. type := type.resolved;
  8701. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8702. base := type(SyntaxTree.ArrayType).arrayBase;
  8703. RETURN TRUE;
  8704. END;
  8705. RETURN FALSE;
  8706. END IsOpenArray;
  8707. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8708. BEGIN
  8709. type := type.resolved;
  8710. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8711. base := type(SyntaxTree.ArrayType).arrayBase;
  8712. dim := type(SyntaxTree.ArrayType).staticLength;
  8713. RETURN TRUE
  8714. ELSE
  8715. RETURN FALSE
  8716. END;
  8717. END IsStaticArray;
  8718. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8719. BEGIN
  8720. type := type.resolved;
  8721. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8722. base := type(SyntaxTree.ArrayType).arrayBase;
  8723. RETURN TRUE
  8724. ELSE
  8725. RETURN FALSE
  8726. END;
  8727. END IsDynamicArray;
  8728. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8729. VAR i: LONGINT;
  8730. BEGIN
  8731. i := 0;
  8732. t := t.resolved;
  8733. IF t IS SyntaxTree.MathArrayType THEN
  8734. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8735. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8736. END;
  8737. ELSIF t IS SyntaxTree.ArrayType THEN
  8738. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8739. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8740. END;
  8741. END;
  8742. RETURN i
  8743. END Dimension;
  8744. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8745. BEGIN
  8746. RETURN expression.assignable;
  8747. END IsVariable;
  8748. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8749. BEGIN
  8750. IF (symbol IS SyntaxTree.Parameter) THEN
  8751. WITH symbol: SyntaxTree.Parameter DO
  8752. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8753. END;
  8754. ELSE
  8755. RETURN FALSE
  8756. END;
  8757. END IsVariableParameter;
  8758. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8759. VAR result: BOOLEAN;
  8760. BEGIN
  8761. IF type = NIL THEN result := FALSE
  8762. ELSE
  8763. type := type.resolved;
  8764. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8765. END;
  8766. RETURN result
  8767. END IsPointerType;
  8768. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8769. VAR result: BOOLEAN;
  8770. BEGIN
  8771. IF type = NIL THEN result := FALSE
  8772. ELSE
  8773. type := type.resolved;
  8774. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8775. END;
  8776. RETURN result
  8777. END IsUnsafePointer;
  8778. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8779. BEGIN
  8780. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8781. END IsDisposable;
  8782. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8783. VAR result: BOOLEAN;
  8784. BEGIN
  8785. IF type = NIL THEN result := FALSE
  8786. ELSE
  8787. type := type.resolved;
  8788. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8789. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8790. result := result OR (type IS SyntaxTree.ObjectType);
  8791. END;
  8792. RETURN result
  8793. END IsPointerToRecord;
  8794. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8795. VAR result: BOOLEAN;
  8796. BEGIN
  8797. IF type = NIL THEN result := FALSE
  8798. ELSE
  8799. type := type.resolved;
  8800. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8801. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8802. ;
  8803. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8804. result := result OR (type IS SyntaxTree.ObjectType);
  8805. END;
  8806. RETURN result
  8807. END IsPointerToObject;
  8808. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8809. BEGIN
  8810. IF type # NIL THEN
  8811. RETURN type.resolved.hasPointers
  8812. ELSE
  8813. RETURN FALSE
  8814. END;
  8815. END ContainsPointer;
  8816. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8817. BEGIN
  8818. IF type = NIL THEN RETURN FALSE END;
  8819. type := type.resolved;
  8820. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8821. END IsStringType;
  8822. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8823. BEGIN
  8824. IF type = NIL THEN RETURN FALSE END;
  8825. type := type.resolved;
  8826. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8827. END IsCharacterType;
  8828. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8829. BEGIN
  8830. IF type = NIL THEN RETURN FALSE END;
  8831. type := type.resolved;
  8832. RETURN (type IS SyntaxTree.EnumerationType)
  8833. END IsEnumerationType;
  8834. (** cf. section "Type extension (base type)" in the language report **)
  8835. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8836. VAR result: BOOLEAN;
  8837. BEGIN
  8838. ASSERT(base # NIL); ASSERT(extension # NIL);
  8839. base := base.resolved; extension := extension.resolved;
  8840. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8841. result := TRUE;
  8842. ELSE
  8843. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8844. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8845. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8846. END;
  8847. WHILE (extension # NIL) & (extension # base) DO
  8848. IF extension IS SyntaxTree.RecordType THEN
  8849. extension := extension(SyntaxTree.RecordType).baseType;
  8850. IF (extension # NIL) THEN extension := extension.resolved END;
  8851. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8852. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8853. END;
  8854. ELSE extension := NIL;
  8855. END;
  8856. END;
  8857. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8858. END;
  8859. RETURN result
  8860. END IsTypeExtension;
  8861. (** check if base is the base enumeration type of extension **)
  8862. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8863. BEGIN
  8864. base := base.resolved; extension := extension.resolved;
  8865. WHILE (extension # NIL) & (extension # base) DO
  8866. IF extension IS SyntaxTree.EnumerationType THEN
  8867. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8868. IF extension # NIL THEN extension := extension.resolved END;
  8869. ELSE
  8870. extension := NIL
  8871. END;
  8872. END;
  8873. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8874. END IsEnumerationExtension;
  8875. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8876. BEGIN
  8877. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8878. RETURN TRUE
  8879. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8880. RETURN TRUE
  8881. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8882. RETURN TRUE
  8883. ELSE
  8884. RETURN FALSE
  8885. END
  8886. END IsCallable;
  8887. (** compute and return the distance of two record types
  8888. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8889. **)
  8890. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8891. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8892. BEGIN
  8893. i := 0;
  8894. WHILE (from # NIL) & (from # to) DO
  8895. baseType := from.baseType;
  8896. IF (baseType # NIL) THEN
  8897. baseType := baseType.resolved;
  8898. IF baseType IS SyntaxTree.PointerType THEN
  8899. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8900. END;
  8901. IF baseType IS SyntaxTree.RecordType THEN
  8902. from := baseType(SyntaxTree.RecordType);
  8903. ELSE
  8904. from := NIL;
  8905. END;
  8906. ELSE
  8907. from := NIL
  8908. END;
  8909. INC(i)
  8910. END;
  8911. IF from = NIL THEN i := Infinity END;
  8912. RETURN i
  8913. END RecordTypeDistance;
  8914. (** compute and return the distance of two pointer types **)
  8915. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8916. BEGIN
  8917. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8918. RETURN Infinity;
  8919. ELSE
  8920. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8921. END;
  8922. END PointerTypeDistance;
  8923. (** check if expression contains a symbol designator pointing to a type declaration.
  8924. - if so then enter type declaration into typeDeclaration and return true else return false
  8925. **)
  8926. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8927. VAR result: BOOLEAN;
  8928. BEGIN
  8929. result := FALSE;
  8930. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8931. result := TRUE;
  8932. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8933. END;
  8934. RETURN result
  8935. END IsTypeDesignator;
  8936. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8937. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8938. VAR result: BOOLEAN;
  8939. BEGIN
  8940. type := type.resolved;
  8941. IF type IS SyntaxTree.PointerType THEN
  8942. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8943. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8944. result := TRUE
  8945. ELSE
  8946. result := type IS SyntaxTree.RecordType
  8947. END;
  8948. RETURN result
  8949. END IsExtensibleType;
  8950. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8951. BEGIN
  8952. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8953. (d IS SyntaxTree.SymbolDesignator) &
  8954. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8955. OR
  8956. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8957. END IsUnextensibleRecord;
  8958. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8959. BEGIN
  8960. IF IsUnextensibleRecord(d) THEN
  8961. RETURN FALSE
  8962. ELSE RETURN IsExtensibleType(d.type.resolved)
  8963. END;
  8964. END IsExtensibleDesignator;
  8965. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8966. BEGIN
  8967. type := type.resolved;
  8968. IF (type IS SyntaxTree.PointerType) THEN
  8969. RETURN TRUE
  8970. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8971. RETURN TRUE
  8972. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8973. RETURN TRUE
  8974. ELSIF (type IS SyntaxTree.BasicType) THEN
  8975. RETURN TRUE
  8976. END;
  8977. RETURN FALSE
  8978. END IsBasicType;
  8979. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8980. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8981. BEGIN
  8982. baseType := record.baseType;
  8983. IF (baseType # NIL) THEN
  8984. baseType := baseType.resolved;
  8985. IF (baseType IS SyntaxTree.PointerType) THEN
  8986. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8987. END;
  8988. END;
  8989. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8990. recordType := baseType(SyntaxTree.RecordType);
  8991. ELSE
  8992. recordType := NIL;
  8993. END;
  8994. RETURN recordType
  8995. END RecordBase;
  8996. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8997. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8998. BEGIN
  8999. baseRecord := RecordBase(scope.ownerRecord);
  9000. IF baseRecord = NIL THEN RETURN NIL END;
  9001. scope := baseRecord.recordScope;
  9002. procedureType := procedure.type.resolved;
  9003. IF procedure IS SyntaxTree.Operator THEN
  9004. operator := scope.firstOperator;
  9005. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9006. (*
  9007. Printout.Info("not same ",procedureType);
  9008. Printout.Info("with ",operator.type);
  9009. *)
  9010. operator := operator.nextOperator;
  9011. END;
  9012. super := operator;
  9013. ELSE
  9014. super := scope.firstProcedure;
  9015. WHILE (super # NIL) & (super.name # procedure.name) DO
  9016. super := super.nextProcedure;
  9017. END;
  9018. END;
  9019. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9020. RETURN super
  9021. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9022. RETURN super
  9023. ELSE
  9024. RETURN FindSuperProcedure(scope,procedure);
  9025. END;
  9026. END FindSuperProcedure;
  9027. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9028. VAR procedure: SyntaxTree.Procedure;
  9029. BEGIN
  9030. procedure := record.recordScope.constructor;
  9031. IF procedure = NIL THEN
  9032. record := RecordBase(record);
  9033. IF record # NIL THEN
  9034. procedure := GetConstructor(record)
  9035. END;
  9036. END;
  9037. RETURN procedure;
  9038. END GetConstructor;
  9039. (* enter a case into a list of cases in a sorted way and check for collision *)
  9040. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9041. VAR prev,this,new: SyntaxTree.CaseConstant;
  9042. BEGIN
  9043. this := root;
  9044. prev := NIL;
  9045. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9046. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9047. RETURN FALSE
  9048. ELSE
  9049. IF (this # NIL) & (this.min = max+1) THEN
  9050. this.min := min
  9051. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9052. prev.max := min
  9053. ELSE
  9054. NEW(new); new.min := min; new.max := max;
  9055. new.next := this;
  9056. IF prev = NIL THEN
  9057. root := new;
  9058. ELSE
  9059. prev.next := new
  9060. END
  9061. END;
  9062. RETURN TRUE
  9063. END;
  9064. END EnterCase;
  9065. (** generate and return a new checker object, errors are entered into diagnostics **)
  9066. PROCEDURE NewChecker*(diagnostics: Diagnostics.Diagnostics; verboseErrorMessage,useDarwinCCalls,cooperative: BOOLEAN; system: Global.System; symbolFileFormat: Formats.SymbolFileFormat; VAR importCache: SyntaxTree.ModuleScope; CONST backend: ARRAY OF CHAR): Checker;
  9067. VAR checker: Checker;
  9068. BEGIN
  9069. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9070. RETURN checker
  9071. END NewChecker;
  9072. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9073. VAR warnings: Warnings;
  9074. BEGIN
  9075. NEW(warnings, diagnostics); RETURN warnings;
  9076. END NewWarnings;
  9077. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9078. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9079. END IsRangeType;
  9080. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9081. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9082. END IsMathArrayType;
  9083. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9084. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9085. END IsArrayType;
  9086. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9087. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9088. END IsComplexType;
  9089. (** if a type is an array-structured object type *)
  9090. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9091. VAR recordType: SyntaxTree.RecordType;
  9092. BEGIN
  9093. IF type = NIL THEN
  9094. RETURN FALSE
  9095. ELSE
  9096. type := type.resolved;
  9097. IF type IS SyntaxTree.PointerType THEN
  9098. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9099. IF type IS SyntaxTree.RecordType THEN
  9100. recordType := type(SyntaxTree.RecordType);
  9101. RETURN recordType.isObject & recordType.HasArrayStructure()
  9102. ELSE
  9103. RETURN FALSE
  9104. END
  9105. ELSE
  9106. RETURN FALSE
  9107. END
  9108. END
  9109. END IsArrayStructuredObjectType;
  9110. (** the math array structure of a type
  9111. - for math arrays: the array itself
  9112. - for pointers: the math array structure of the pointer base
  9113. - for array-structured object types: the underlying structure
  9114. - for non-math arrays and all other types: NIL
  9115. **)
  9116. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9117. VAR
  9118. result: SyntaxTree.MathArrayType;
  9119. BEGIN
  9120. IF type = NIL THEN
  9121. result := NIL
  9122. ELSE
  9123. type := type.resolved;
  9124. IF type IS SyntaxTree.PointerType THEN
  9125. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9126. END;
  9127. IF type IS SyntaxTree.MathArrayType THEN
  9128. result := type(SyntaxTree.MathArrayType)
  9129. ELSIF type IS SyntaxTree.RecordType THEN
  9130. result := type(SyntaxTree.RecordType).arrayStructure
  9131. ELSE
  9132. result := NIL
  9133. END
  9134. END;
  9135. RETURN result
  9136. END MathArrayStructureOfType;
  9137. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9138. VAR
  9139. result: BOOLEAN;
  9140. rangeExpression: SyntaxTree.RangeExpression;
  9141. BEGIN
  9142. IF x IS SyntaxTree.RangeExpression THEN
  9143. rangeExpression := x(SyntaxTree.RangeExpression);
  9144. result := TRUE;
  9145. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9146. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9147. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9148. ELSE
  9149. result := FALSE
  9150. END;
  9151. RETURN result
  9152. END IsStaticRange;
  9153. (** whether a type is a math array of tensor form **)
  9154. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9155. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9156. END IsTensor;
  9157. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9158. BEGIN
  9159. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9160. length := type(SyntaxTree.MathArrayType).staticLength;
  9161. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9162. RETURN TRUE
  9163. ELSE
  9164. RETURN FALSE
  9165. END;
  9166. END IsStaticMathArray;
  9167. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9168. BEGIN
  9169. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9170. END SymbolHasAddress;
  9171. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9172. BEGIN
  9173. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9174. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9175. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9176. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9177. END HasAddress;
  9178. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9179. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9180. BEGIN
  9181. IF (e IS SyntaxTree.Designator) THEN
  9182. d := e(SyntaxTree.Designator);
  9183. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9184. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9185. e := d.left;
  9186. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9187. END;
  9188. IF d # NIL THEN
  9189. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9190. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9191. END;
  9192. END;
  9193. RETURN FALSE;
  9194. END IsLocalVariable;
  9195. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9196. BEGIN
  9197. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9198. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9199. ELSE
  9200. RETURN TRUE
  9201. END;
  9202. END IsStaticProcedure;
  9203. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9204. CONST OptimizeMethodTable = FALSE;
  9205. BEGIN
  9206. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9207. END InMethodTable;
  9208. END FoxSemanticChecker.
  9209. SystemTools.FreeDownTo FoxSemanticChecker ~