FoxSemanticChecker.Mod 400 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964
  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. END;
  4770. IF type0 IS SyntaxTree.ArrayType THEN
  4771. arrayType := type0(SyntaxTree.ArrayType);
  4772. IF arrayType.form = SyntaxTree.Static THEN
  4773. i := first
  4774. ELSIF arrayType.form = SyntaxTree.Open THEN
  4775. i := Dimension(arrayType,{SyntaxTree.Open})+first;
  4776. ELSE HALT(100)
  4777. END;
  4778. IF CheckArity(i,i) & (numberActualParameters>1) THEN
  4779. i := first;
  4780. REPEAT
  4781. actualParameter := actualParameters.GetExpression(i);
  4782. IF CheckSizeType(actualParameter) THEN
  4783. actualParameter := NewConversion(0,actualParameter,system.longintType,NIL);
  4784. actualParameters.SetExpression(i,actualParameter);
  4785. END;
  4786. INC(i);
  4787. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4788. END;
  4789. ELSIF (type0 IS SyntaxTree.RecordType) THEN
  4790. constructor := GetConstructor(type0(SyntaxTree.RecordType));
  4791. IF constructor = NIL THEN
  4792. IF CheckArity(first,first) THEN END;
  4793. ELSIF (constructor.scope.ownerModule # currentScope.ownerModule) & ~(SyntaxTree.PublicRead IN constructor.access) THEN
  4794. Error(position,Diagnostics.Invalid,"new on object with hidden constructor");
  4795. ELSE
  4796. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4797. numberFormalParameters := procedureType.numberParameters;
  4798. IF numberActualParameters-first <= numberFormalParameters THEN
  4799. formalParameter := procedureType.firstParameter;
  4800. FOR i := first TO numberActualParameters-1 DO
  4801. actualParameter := actualParameters.GetExpression(i);
  4802. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4803. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4804. ELSE
  4805. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4806. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4807. END;
  4808. actualParameters.SetExpression(i,actualParameter);
  4809. END;
  4810. formalParameter := formalParameter.nextParameter;
  4811. END;
  4812. WHILE (formalParameter # NIL) DO
  4813. IF formalParameter.defaultValue # NIL THEN
  4814. actualParameters.AddExpression(formalParameter.defaultValue);
  4815. formalParameter := formalParameter.nextParameter
  4816. ELSE
  4817. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4818. formalParameter := NIL;
  4819. END;
  4820. END;
  4821. ELSE
  4822. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4823. END;
  4824. END;
  4825. ELSIF type0 IS SyntaxTree.MathArrayType THEN
  4826. mathArrayType := type0(SyntaxTree.MathArrayType);
  4827. IF mathArrayType.form = SyntaxTree.Static THEN
  4828. Error(position,Diagnostics.Invalid,"new on static array");
  4829. ELSE
  4830. IF mathArrayType.form = SyntaxTree.Tensor THEN
  4831. i0 := first+1; i1 := Infinity;
  4832. ELSIF mathArrayType.form = SyntaxTree.Open THEN
  4833. i0 := Dimension(mathArrayType,{SyntaxTree.Open})+first;
  4834. i1 := i0;
  4835. ELSE HALT(100);
  4836. END;
  4837. IF type1 IS SyntaxTree.MathArrayType THEN (* NEW(a, array) *)
  4838. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  4839. base := ArrayBase(type0,MAX(LONGINT));
  4840. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  4841. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  4842. IF ~CompatibleTo(system,type0,parameterType) THEN
  4843. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in new");
  4844. result := SyntaxTree.invalidExpression;
  4845. ELSE
  4846. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  4847. END;
  4848. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  4849. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  4850. IF ~CompatibleTo(system,type1,parameterType) THEN
  4851. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  4852. result := SyntaxTree.invalidExpression;
  4853. ELSE
  4854. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  4855. END;
  4856. ELSE
  4857. IF CheckArity(i0,i1) & (numberActualParameters >first) THEN
  4858. i := first;
  4859. REPEAT
  4860. actualParameter := actualParameters.GetExpression(i);
  4861. IF CheckSizeType(actualParameter) THEN
  4862. actualParameter := NewConversion(0,actualParameter,system.sizeType,NIL);
  4863. actualParameters.SetExpression(i,actualParameter);
  4864. END;
  4865. INC(i);
  4866. UNTIL ~CheckSizeType(actualParameter) OR (actualParameter.resolved # NIL) & ~CheckPositiveIntegerValue(actualParameter,i0,TRUE) OR (i=numberActualParameters);
  4867. END;
  4868. END;
  4869. END;
  4870. ELSIF type0 IS SyntaxTree.CellType THEN
  4871. IF ~(currentIsCellNet) THEN
  4872. Error(position,Diagnostics.Invalid,"cell allocation outside activeCells ");
  4873. ELSE
  4874. constructor := type0(SyntaxTree.CellType).cellScope.constructor;
  4875. IF (constructor = NIL) & CheckArity(1,1) THEN
  4876. (* ok *)
  4877. ELSE
  4878. procedureType := constructor.type(SyntaxTree.ProcedureType);
  4879. numberFormalParameters := procedureType.numberParameters;
  4880. DEC(numberActualParameters);
  4881. IF numberActualParameters <= numberFormalParameters THEN
  4882. formalParameter := procedureType.firstParameter;
  4883. FOR i := first TO numberActualParameters DO
  4884. actualParameter := actualParameters.GetExpression(i);
  4885. IF (actualParameter = SyntaxTree.invalidExpression) THEN
  4886. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  4887. ELSE
  4888. IF formalParameter.type.resolved # actualParameter.type.resolved THEN
  4889. actualParameter := NewConversion(actualParameter.position,actualParameter,formalParameter.type,NIL);
  4890. END;
  4891. actualParameters.SetExpression(i,actualParameter);
  4892. END;
  4893. formalParameter := formalParameter.nextParameter;
  4894. END;
  4895. WHILE (formalParameter # NIL) DO
  4896. IF formalParameter.defaultValue # NIL THEN
  4897. actualParameters.AddExpression(formalParameter.defaultValue);
  4898. formalParameter := formalParameter.nextParameter
  4899. ELSE
  4900. Error(position,Diagnostics.Invalid,"less actual than formal parameters");
  4901. formalParameter := NIL;
  4902. END;
  4903. END;
  4904. ELSE
  4905. Error(position,Diagnostics.Invalid,"more actual than formal parameters")
  4906. END;
  4907. END;
  4908. END;
  4909. activeCellsStatement := TRUE;
  4910. ELSE
  4911. Error(position,Diagnostics.Invalid,"cannot be allocated");
  4912. END;
  4913. END;
  4914. END;
  4915. (* ---- DISPOSE ----- *)
  4916. ELSIF (id = Global.Dispose) & CheckArity(1,1) THEN
  4917. IF ~IsPointerType(parameter0.type) THEN
  4918. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  4919. ELSIF ~IsDisposable(parameter0.type) THEN
  4920. Error(parameter0.position,Diagnostics.Invalid,"is not disposable")
  4921. ELSIF CheckVariable(parameter0) THEN (* ok *)
  4922. END
  4923. (* ---- GETPROCEDURE ----- *)
  4924. ELSIF (id = Global.GetProcedure) & CheckArity(3,3) THEN
  4925. IF CheckStringType(parameter0) & CheckStringType(parameter1) THEN
  4926. IF CheckVariable(parameter2) THEN
  4927. IF ~GetProcedureAllowed(parameter2.type) THEN
  4928. Error(parameter2.position,Diagnostics.Invalid,"GETPROCEDURE not allowed on this type");
  4929. END;
  4930. END;
  4931. END;
  4932. (* ---- ABS ----- *)
  4933. ELSIF (id = Global.Abs) & CheckArity(1,1) THEN
  4934. (* note: ABS on complex numbers is done using overloading *)
  4935. IF CheckNonComplexNumberSizeType(parameter0) THEN
  4936. type := type0;
  4937. IF IsIntegerValue(parameter0,i0) THEN
  4938. result.SetResolved(SyntaxTree.NewIntegerValue(position,ABS(i0)));
  4939. type := Global.GetIntegerType(system,ABS(i0));
  4940. ELSIF IsRealValue(parameter0,r) THEN
  4941. result.SetResolved(SyntaxTree.NewRealValue(position,ABS(r)));
  4942. END;
  4943. ELSE
  4944. type := SyntaxTree.invalidType;
  4945. END;
  4946. (* ---- ASH, ASR ----- *)
  4947. ELSIF ((id = Global.Ash) OR (id= Global.Asr)) & CheckArity(2,2) THEN
  4948. type := type0;
  4949. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  4950. (*
  4951. ConvertOperands(parameter0,parameter1); (* same type *)
  4952. *)
  4953. type := parameter0.type;
  4954. IF IsIntegerValue(parameter0,i0) THEN
  4955. IF IsIntegerValue(parameter1,i1) THEN
  4956. IF id = Global.Ash THEN i0 := ASH(i0,i1) ELSE i0 := ASR(i0,i1) END;
  4957. result.SetResolved(SyntaxTree.NewIntegerValue(position,i0));
  4958. result := ResolveExpression(result);
  4959. type := Global.GetIntegerType(system,i0);
  4960. END;
  4961. END;
  4962. IF type.resolved.sizeInBits < 32 THEN
  4963. type := system.longintType;
  4964. END;
  4965. (*!compatibility with release, remove when resolved
  4966. critical in release : SHORT(ASH(..))), ASH(ORD(..))
  4967. *)
  4968. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  4969. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4970. actualParameters.SetExpression(0,parameter0);
  4971. actualParameters.SetExpression(1,parameter1);
  4972. END;
  4973. (* ---- CAP ----- *)
  4974. ELSIF (id = Global.Cap) & CheckArity(1,1) THEN
  4975. type := system.characterType;
  4976. IF CheckCharacterType (parameter0) THEN
  4977. parameter0 := NewConversion(parameter0.position,parameter0,type,NIL);
  4978. actualParameters.SetExpression(0,parameter0);
  4979. IF IsCharacterValue(parameter0,c) THEN
  4980. IF (c <= "z") & (c >= "a") THEN
  4981. result.SetResolved(SyntaxTree.NewCharacterValue(position,CAP(c)))
  4982. ELSE
  4983. result.SetResolved(SyntaxTree.NewCharacterValue(position,c))
  4984. END;
  4985. END;
  4986. END;
  4987. (* ---- CHR ----- *)
  4988. ELSIF ((id = Global.Chr) OR (id = Global.Chr32)) & CheckArity(1,1) THEN
  4989. IF id = Global.Chr THEN
  4990. type := system.characterType
  4991. ELSE
  4992. type := system.characterType32
  4993. END;
  4994. IF CheckIntegerType(parameter0) THEN
  4995. IF IsIntegerValue(parameter0,i0) THEN
  4996. result.SetResolved(SyntaxTree.NewCharacterValue(position,CHR(i0)));
  4997. result := ResolveExpression(result);
  4998. ELSE
  4999. (*
  5000. result := NewConversion(parameter0.position,parameter0,type);
  5001. *)
  5002. END;
  5003. END
  5004. (* ---- ENTIER ----- *)
  5005. ELSIF (id = Global.Entier) & CheckArity(1,1) THEN
  5006. type := system.longintType;
  5007. IF CheckRealType(parameter0) THEN
  5008. IF IsRealValue(parameter0,r) THEN
  5009. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIER(r)));
  5010. type := Global.GetIntegerType(system,ENTIER(r));
  5011. END
  5012. END;
  5013. (* ---- ENTIERH ----- *)
  5014. ELSIF (id = Global.EntierH) & CheckArity(1,1) THEN
  5015. type := system.hugeintType;
  5016. IF CheckRealType(parameter0) THEN
  5017. IF IsRealValue(parameter0,r) THEN
  5018. result.SetResolved(SyntaxTree.NewIntegerValue(position,ENTIERH(r)));
  5019. END
  5020. END;
  5021. (* ---- LEN ----- *)
  5022. ELSIF (id = Global.Len) & CheckArity(1,2) THEN
  5023. type := system.longintType;
  5024. base := type0;
  5025. IF (base IS SyntaxTree.PointerType) & (parameter0 IS SyntaxTree.Designator) THEN
  5026. IF base(SyntaxTree.PointerType).isUnsafe THEN
  5027. Error(position, Diagnostics.Invalid, "forbidden len on unsafe pointer");
  5028. END;
  5029. parameter0 := NewDereferenceDesignator(position,parameter0(SyntaxTree.Designator));
  5030. actualParameters.SetExpression(0,parameter0);
  5031. type0 := parameter0.type.resolved;
  5032. base := type0;
  5033. END;
  5034. IF (numberActualParameters=1) OR (numberActualParameters =2) & CheckIntegerType(parameter1) THEN
  5035. IF ~(numberActualParameters=2) OR ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5036. IF i1 < 0 THEN
  5037. Error(position,Diagnostics.Invalid,"invalid dimension");
  5038. base := SyntaxTree.invalidType;
  5039. ELSE
  5040. base := ArrayBase(base,i1);
  5041. IF (base # NIL) & Indexable(base) THEN
  5042. ELSE
  5043. Error(position,Diagnostics.Invalid,"len on no array");
  5044. IF VerboseErrorMessage THEN
  5045. Printout.Info("base",base);
  5046. END;
  5047. base := SyntaxTree.invalidType;
  5048. END;
  5049. END;
  5050. IF numberActualParameters=2 THEN
  5051. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5052. actualParameters.SetExpression(1,parameter1);
  5053. ELSIF base IS SyntaxTree.MathArrayType THEN
  5054. Error(position,Diagnostics.Invalid,"missing dimension specification");
  5055. END;
  5056. IF (numberActualParameters=1) OR (numberActualParameters =2) & IsIntegerValue(parameter1,i1) THEN
  5057. IF base IS SyntaxTree.ArrayType THEN
  5058. arrayType := base(SyntaxTree.ArrayType);
  5059. IF (arrayType.length # NIL) & (arrayType.length.resolved # NIL) & IsIntegerValue(arrayType.length,i) THEN
  5060. (* do not use length directly such as in result := length as this mide have side-effects when result types get converted *)
  5061. result := Global.NewIntegerValue(system,position,i);
  5062. type := result.type;(* arrayType.length.type;*)
  5063. ASSERT(type # NIL);
  5064. END;
  5065. ELSIF base IS SyntaxTree.MathArrayType THEN
  5066. mathArrayType := base(SyntaxTree.MathArrayType);
  5067. IF (mathArrayType.length # NIL) & (mathArrayType.length.resolved # NIL) & IsIntegerValue(mathArrayType.length,i) THEN
  5068. result := Global.NewIntegerValue(system,position,i);
  5069. type := result.type;
  5070. (*
  5071. type := mathArrayType.length.type;
  5072. *)
  5073. ASSERT(type # NIL);
  5074. END;
  5075. END;
  5076. END;
  5077. ELSE
  5078. type := system.longintType;
  5079. END;
  5080. (* ---- FIRST ---- *)
  5081. ELSIF (id = Global.First) & CheckArity(1,1) THEN
  5082. type := system.longintType;
  5083. IF CheckRangeType(parameter0) THEN END;
  5084. result.SetAssignable(parameter0.assignable)
  5085. (* ---- LAST ---- *)
  5086. ELSIF (id = Global.Last) & CheckArity(1,1) THEN
  5087. type := system.longintType;
  5088. IF CheckRangeType(parameter0) THEN END;
  5089. result.SetAssignable(parameter0.assignable)
  5090. (* ---- STEP ---- *)
  5091. ELSIF (id = Global.Step) & CheckArity(1,1) THEN
  5092. type := system.longintType;
  5093. IF CheckRangeType(parameter0) THEN END;
  5094. result.SetAssignable(parameter0.assignable)
  5095. (* ---- RE ---- *)
  5096. ELSIF (id = Global.Re) & CheckArity(1,1) THEN
  5097. IF CheckNumberType(parameter0) THEN
  5098. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5099. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5100. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, r)) END
  5101. ELSIF parameter0.type.resolved IS SyntaxTree.FloatType THEN
  5102. type := parameter0.type
  5103. ELSE
  5104. type := system.realType
  5105. END
  5106. END;
  5107. result.SetAssignable(parameter0.assignable)
  5108. (* ---- IM ---- *)
  5109. ELSIF (id = Global.Im) & CheckArity(1,1) THEN
  5110. IF CheckNumberType(parameter0) THEN
  5111. IF parameter0.type.resolved IS SyntaxTree.ComplexType THEN
  5112. type := parameter0.type.resolved(SyntaxTree.ComplexType).componentType;
  5113. IF IsComplexValue(parameter0, r, im) THEN result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, im)) END
  5114. ELSE
  5115. type := system.realType;
  5116. result.SetResolved(SyntaxTree.NewRealValue(parameter0.position, 0))
  5117. END
  5118. END;
  5119. result.SetAssignable(parameter0.assignable)
  5120. (* ---- MAX ----- *)
  5121. ELSIF (id = Global.Max) & CheckArity(1,2) THEN
  5122. IF numberActualParameters = 1 THEN
  5123. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5124. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5125. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MAX(CHAR)));
  5126. (*!! ELSIF type = Global.Char16 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFH));
  5127. ELSIF type = Global.Char32 THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0FFFFFFFFH));
  5128. *)
  5129. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.IntegerType))));
  5130. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MaxFloat(system,type(SyntaxTree.FloatType))));
  5131. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type)-1)); type := system.shortintType;
  5132. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MaxInteger(system,type(SyntaxTree.BasicType))));
  5133. ELSE Error(Diagnostics.Invalid,parameter0.position,"builtin function not applicable to this type");
  5134. END;
  5135. ELSE
  5136. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5137. END
  5138. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5139. ConvertOperands(parameter0,parameter1);
  5140. actualParameters.SetExpression(0,parameter0);
  5141. actualParameters.SetExpression(1,parameter1);
  5142. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5143. IF r0 > r1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5144. ELSE result.SetResolved(parameter0(SyntaxTree.Value))
  5145. END;
  5146. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5147. IF i0 > i1 THEN result.SetResolved(parameter0(SyntaxTree.Value))
  5148. ELSE result.SetResolved(parameter1(SyntaxTree.Value))
  5149. END;
  5150. END;
  5151. type := parameter0.type;
  5152. ELSE type := SyntaxTree.invalidType;
  5153. END;
  5154. (* ---- MIN ----- *)
  5155. ELSIF (id = Global.Min) & CheckArity(1,2) THEN
  5156. IF numberActualParameters = 1 THEN
  5157. IF parameter0.type = SyntaxTree.typeDeclarationType THEN
  5158. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType.resolved;
  5159. IF type IS SyntaxTree.CharacterType THEN result.SetResolved(SyntaxTree.NewCharacterValue(position,MIN(CHAR)));
  5160. ELSIF type IS SyntaxTree.IntegerType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,Global.MinInteger(system,type(SyntaxTree.IntegerType))));
  5161. ELSIF type IS SyntaxTree.FloatType THEN result.SetResolved(SyntaxTree.NewRealValue(position,Global.MinFloat(system,type(SyntaxTree.FloatType))));
  5162. ELSIF type IS SyntaxTree.SetType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position,0)); type := system.shortintType;
  5163. ELSIF type IS SyntaxTree.SizeType THEN result.SetResolved(SyntaxTree.NewIntegerValue(position, Global.MinInteger(system,type(SyntaxTree.BasicType))));
  5164. ELSE Error(parameter0.position,Diagnostics.Invalid,"builtin function not applicable to this type");
  5165. END;
  5166. ELSE
  5167. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5168. END
  5169. ELSIF CheckNonComplexNumberSizeType(parameter0) & CheckNonComplexNumberSizeType(parameter1) THEN
  5170. ConvertOperands(parameter0,parameter1);
  5171. actualParameters.SetExpression(0,parameter0);
  5172. actualParameters.SetExpression(1,parameter1);
  5173. IF IsRealValue(parameter0,r0) & IsRealValue(parameter1,r1) THEN
  5174. IF r0 < r1 THEN result.SetResolved(parameter0.resolved)
  5175. ELSE result.SetResolved(parameter1.resolved)
  5176. END;
  5177. ELSIF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5178. IF i0 < i1 THEN result.SetResolved(parameter0.resolved)
  5179. ELSE result.SetResolved(parameter1.resolved)
  5180. END;
  5181. END;
  5182. type := parameter0.type;
  5183. ELSE type := SyntaxTree.invalidType;
  5184. END;
  5185. (* ---- ODD ----- *)
  5186. ELSIF (id = Global.Odd) & CheckArity(1,1) THEN
  5187. type := system.booleanType;
  5188. IF CheckIntegerType(parameter0) THEN
  5189. IF IsIntegerValue(parameter0,i0) THEN
  5190. result.SetResolved(SyntaxTree.NewBooleanValue(position,ODD(i0)));
  5191. type := system.booleanType;
  5192. END;
  5193. END;
  5194. (* ---- ORD ----- *)
  5195. ELSIF ((id = Global.Ord) OR (id = Global.Ord32)) & CheckArity(1,1) THEN
  5196. IF id = Global.Ord THEN
  5197. type := system.integerType;
  5198. ELSE
  5199. type := system.longintType;
  5200. END;
  5201. IF CompatibleTo(system, parameter0.type, system.characterType) THEN
  5202. parameter0 := NewConversion(parameter0.position, parameter0, system.characterType,NIL);
  5203. actualParameters.SetExpression(0,parameter0);
  5204. (* IF CheckCharacterType(parameter0) THEN*)
  5205. IF IsCharacterValue(parameter0,c)THEN
  5206. result.SetResolved(Global.NewIntegerValue(system,position,ORD(c)));
  5207. type := Global.GetSignedIntegerType(system,ORD(c));
  5208. END;
  5209. ELSE Error(parameter0.position, Diagnostics.Invalid, "incompatible parameter");
  5210. END;
  5211. (* ---- SHORT ----- *)
  5212. ELSIF (id = Global.Short) & CheckArity(1,1) THEN
  5213. type := type0;
  5214. IF IsSignedIntegerType(type) THEN
  5215. IF (type.sizeInBits = 8) OR (type = system.shortintType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5216. ELSIF type = system.integerType THEN type := system.shortintType
  5217. ELSIF type = system.longintType THEN type := system.integerType
  5218. ELSIF type = system.hugeintType THEN type:= system.longintType
  5219. ELSE
  5220. CASE type.sizeInBits OF
  5221. 16: type := Global.Integer8
  5222. |32: type := Global.Integer16
  5223. |64: type := Global.Integer32
  5224. END;
  5225. END;
  5226. ELSIF type IS SyntaxTree.FloatType THEN
  5227. IF (type.sizeInBits = 32) OR (type = system.realType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5228. ELSIF type = system.longrealType THEN type := system.realType
  5229. ELSIF type.sizeInBits = 64 THEN type := Global.Float32
  5230. END;
  5231. ELSIF type IS SyntaxTree.ComplexType THEN
  5232. IF (type.sizeInBits = 64) OR (type = system.complexType) THEN Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5233. ELSIF (type = system.longcomplexType) THEN type := system.complexType
  5234. ELSIF type.sizeInBits = 128 THEN type := Global.Complex64
  5235. END;
  5236. ELSE
  5237. Error(parameter0.position,Diagnostics.Invalid,"short not applicable")
  5238. END;
  5239. IF (parameter0.resolved # NIL) THEN
  5240. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5241. IF parameter0 IS SyntaxTree.Value THEN
  5242. result.SetResolved(parameter0(SyntaxTree.Value));
  5243. END;
  5244. END;
  5245. (* ---- LONG ----- *)
  5246. ELSIF (id = Global.Long) & CheckArity(1,1) THEN
  5247. type := type0;
  5248. IF IsSignedIntegerType(type) THEN
  5249. IF (type.sizeInBits = 64) OR (type = system.hugeintType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5250. ELSIF type = system.longintType THEN type := system.hugeintType
  5251. ELSIF type = system.integerType THEN type := system.longintType
  5252. ELSIF type = system.shortintType THEN type := system.integerType
  5253. ELSE
  5254. CASE type.sizeInBits OF
  5255. 8: type := Global.Integer16
  5256. |16: type := Global.Integer32
  5257. |32: type := Global.Integer64
  5258. END;
  5259. END;
  5260. ELSIF type IS SyntaxTree.FloatType THEN
  5261. IF (type.sizeInBits = 64) OR (type = system.longrealType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5262. ELSIF type= system.realType THEN type := system.longrealType
  5263. ELSIF type.sizeInBits = 32 THEN type := Global.Float64
  5264. END;
  5265. ELSIF type IS SyntaxTree.ComplexType THEN
  5266. IF (type.sizeInBits = 128) OR (type = system.longcomplexType) THEN Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5267. ELSIF type = system.complexType THEN type := system.longcomplexType
  5268. ELSIF type.sizeInBits = 64 THEN type := Global.Complex128
  5269. END;
  5270. ELSE
  5271. Error(parameter0.position,Diagnostics.Invalid,"long not applicable")
  5272. END;
  5273. IF (parameter0.resolved # NIL) THEN
  5274. parameter0 := ConvertValue(parameter0.position,parameter0.resolved,type);
  5275. IF parameter0 IS SyntaxTree.Value THEN
  5276. result.SetResolved(parameter0(SyntaxTree.Value));
  5277. END;
  5278. END;
  5279. (* ---- SIZE OF ----- *)
  5280. ELSIF (id = Global.systemSize) & CheckArity(1,1) THEN
  5281. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5282. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5283. result.SetResolved(SyntaxTree.NewIntegerValue(position,system.SizeOf(type.resolved) DIV 8 (* in bytes *) ));
  5284. type := system.integerType;
  5285. (* was Int16 in paco but should be systemSize (conflict with current release) *)
  5286. ELSE
  5287. (* for variables, system sizeof could represent the physically occupied size
  5288. determined via the type descriptor, implement that ? *)
  5289. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5290. END
  5291. (* ---- SYSTEM.TRACE -----*)
  5292. ELSIF (id = Global.systemTrace) & CheckArity(1,MAX(LONGINT)) THEN
  5293. FOR i := 0 TO numberActualParameters-1 DO
  5294. parameter0 := actualParameters.GetExpression(i);
  5295. IF ~IsBasicType(parameter0.type) & ~IsStringType(parameter0.type) THEN
  5296. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter");
  5297. END;
  5298. END;
  5299. (* remaining issues can only be tested in backend *)
  5300. (* ---- ADDRESSOF----- *)
  5301. ELSIF (id = Global.systemAdr) & CheckArity(1,1) THEN
  5302. IF HasAddress(parameter0) THEN
  5303. type := system.addressType;
  5304. ELSE
  5305. type := SyntaxTree.invalidType;
  5306. Error(parameter0.position,Diagnostics.Invalid,"has no address");
  5307. END;
  5308. (* ---- BIT ----- *)
  5309. ELSIF (id = Global.systemBit) & CheckArity(2,2) THEN
  5310. IF CheckAddressType(parameter0) & CheckSizeType(parameter1) THEN
  5311. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5312. actualParameters.SetExpression(0,parameter0);
  5313. parameter1 := NewConversion(parameter1.position,parameter1,system.addressType,NIL);
  5314. actualParameters.SetExpression(1,parameter1);
  5315. END;
  5316. type := system.booleanType;
  5317. (* ----- MSK ---- *)
  5318. ELSIF (id = Global.systemMsk) & CheckArity(2,2) THEN
  5319. IF CheckIntegerType(parameter0) & CheckIntegerType(parameter1) THEN
  5320. ConvertOperands(parameter0,parameter1);
  5321. actualParameters.SetExpression(0,parameter0);
  5322. actualParameters.SetExpression(1,parameter1);
  5323. END;
  5324. type := parameter0.type;
  5325. (* ---- SYSTEM.GET64 ----- *)
  5326. ELSIF (id = Global.systemGet64) & CheckArity(1,1) THEN
  5327. IF CheckAddressType(parameter0) THEN
  5328. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5329. actualParameters.SetExpression(0,parameter0);
  5330. END;
  5331. type := system.hugeintType;
  5332. (* ---- SYSTEM.GET32 ----- *)
  5333. ELSIF (id = Global.systemGet32) & CheckArity(1,1) THEN
  5334. IF CheckAddressType(parameter0) THEN
  5335. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5336. actualParameters.SetExpression(0,parameter0);
  5337. END;
  5338. type := system.longintType;
  5339. (* ---- SYSTEM.GET16 ----- *)
  5340. ELSIF (id = Global.systemGet16) & CheckArity(1,1) THEN
  5341. IF CheckAddressType(parameter0) THEN
  5342. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5343. actualParameters.SetExpression(0,parameter0);
  5344. END;
  5345. type := system.integerType;
  5346. (* ---- SYSTEM.GET8 ----- *)
  5347. ELSIF (id = Global.systemGet8) & CheckArity(1,1) THEN
  5348. IF CheckAddressType(parameter0) THEN
  5349. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5350. actualParameters.SetExpression(0,parameter0);
  5351. END;
  5352. type := system.shortintType;
  5353. (* ---- SYSTEM.GetStackPointer ----- *)
  5354. ELSIF (id = Global.systemGetStackPointer) & CheckArity(0,0) THEN
  5355. type := system.addressType;
  5356. (* ---- SYSTEM.GetFramePointer ----- *)
  5357. ELSIF (id = Global.systemGetFramePointer) & CheckArity(0,0) THEN
  5358. type := system.addressType;
  5359. (* ---- SYSTEM.GetActivity ----- *)
  5360. ELSIF cooperative & (id = Global.systemGetActivity) & CheckArity(0,0) THEN
  5361. type := system.objectType;
  5362. (* ---- SYSTEM.SetStackPointer ----- *)
  5363. ELSIF (id = Global.systemSetStackPointer) & CheckArity(1,1) THEN
  5364. IF CheckAddressType(parameter0) THEN
  5365. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5366. actualParameters.SetExpression(0,parameter0);
  5367. END;
  5368. (* ---- SYSTEM.SetFramePointer ----- *)
  5369. ELSIF (id = Global.systemSetFramePointer) & CheckArity(1,1) THEN
  5370. IF CheckAddressType(parameter0) THEN
  5371. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5372. actualParameters.SetExpression(0,parameter0);
  5373. END;
  5374. (* ---- SYSTEM.SetActivity ----- *)
  5375. ELSIF cooperative & (id = Global.systemSetActivity) & CheckArity(1,1) THEN
  5376. IF CheckObjectType(parameter0) THEN
  5377. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5378. actualParameters.SetExpression(0,parameter0);
  5379. END;
  5380. (* ---- LSH, LSL, ROT, ROR ----- *)
  5381. ELSIF ((id = Global.Lsh) OR (id = Global.Rot) OR (id= Global.Ror)) & CheckArity(2,2) THEN
  5382. type := type0;
  5383. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5384. actualParameters.SetExpression(1, parameter1);
  5385. IF IsIntegerValue(parameter0,i0) & IsIntegerValue(parameter1,i1) THEN
  5386. IF id = Global.Lsh THEN
  5387. result.SetResolved(SyntaxTree.NewIntegerValue(position,LSH(i0,i1)));
  5388. ELSIF id = Global.Rot THEN
  5389. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROT(i0,i1)));
  5390. ELSIF id = Global.Ror THEN
  5391. result.SetResolved(SyntaxTree.NewIntegerValue(position,ROR(i0,i1)));
  5392. END;
  5393. END;
  5394. (* ---- SYSTEM.VAL ----- *)
  5395. ELSIF (id = Global.systemVal) & CheckArity(2,2) THEN
  5396. IF CheckTypeDeclarationType(parameter0) THEN
  5397. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5398. IF (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form # SyntaxTree.Static) THEN
  5399. result := SyntaxTree.invalidExpression;
  5400. Error(parameter0.position,Diagnostics.Invalid,"is no basic type");
  5401. ELSE
  5402. IF (parameter1.resolved # NIL) THEN
  5403. parameter0 := ConvertValue(parameter1.position,parameter1.resolved,type);
  5404. IF parameter0 IS SyntaxTree.Value THEN
  5405. result.SetResolved(parameter0(SyntaxTree.Value));
  5406. END;
  5407. END;
  5408. result.SetAssignable(parameter1.assignable);
  5409. END;
  5410. END;
  5411. (* ---- SYSTEM.GET ----- *)
  5412. ELSIF (id = Global.systemGet) & CheckArity(2,2) THEN
  5413. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) & CheckVariable(parameter1) THEN
  5414. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5415. actualParameters.SetExpression(0,parameter0);
  5416. END;
  5417. (* ---- SYSTEM.PUT ----- *)
  5418. ELSIF (id = Global.systemPut) & CheckArity(2,2) THEN
  5419. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5420. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5421. actualParameters.SetExpression(0,parameter0);
  5422. END;
  5423. (* ---- SYSTEM.PUT64 ----- *)
  5424. ELSIF (id = Global.systemPut64) & CheckArity(2,2) THEN
  5425. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5426. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5427. parameter1 := NewConversion(parameter1.position,parameter1,system.hugeintType,NIL);
  5428. actualParameters.SetExpression(0,parameter0);
  5429. actualParameters.SetExpression(1,parameter1);
  5430. END;
  5431. (* ---- SYSTEM.PUT32 ----- *)
  5432. ELSIF (id = Global.systemPut32) & CheckArity(2,2) THEN
  5433. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5434. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5435. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5436. actualParameters.SetExpression(0,parameter0);
  5437. actualParameters.SetExpression(1,parameter1);
  5438. END;
  5439. (* ---- SYSTEM.PUT16 ----- *)
  5440. ELSIF (id = Global.systemPut16) & CheckArity(2,2) THEN
  5441. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5442. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5443. parameter1 := NewConversion(parameter1.position,parameter1,system.integerType,NIL);
  5444. actualParameters.SetExpression(0,parameter0);
  5445. actualParameters.SetExpression(1,parameter1);
  5446. END;
  5447. (* ---- SYSTEM.PUT8 ----- *)
  5448. ELSIF (id = Global.systemPut8) & CheckArity(2,2) THEN
  5449. IF CheckAddressType(parameter0) & CheckBasicType(parameter1) THEN
  5450. parameter0 := NewConversion(parameter0.position,parameter0,system.addressType,NIL);
  5451. parameter1 := NewConversion(parameter1.position,parameter1,system.shortintType,NIL);
  5452. actualParameters.SetExpression(0,parameter0);
  5453. actualParameters.SetExpression(1,parameter1);
  5454. END;
  5455. (* ---- SYSTEM.MOVE ----- *)
  5456. ELSIF (id = Global.systemMove) & CheckArity(3,3) THEN
  5457. IF CheckAddressType(parameter0) & CheckAddressType(parameter1) & CheckAddressType(parameter2) THEN
  5458. parameter0 := NewConversion(0,parameter0,system.addressType,NIL);
  5459. parameter1 := NewConversion(0,parameter1,system.addressType,NIL);
  5460. parameter2 := NewConversion(0,parameter2,system.addressType,NIL);
  5461. actualParameters.SetExpression(0,parameter0);
  5462. actualParameters.SetExpression(1,parameter1);
  5463. actualParameters.SetExpression(2,parameter2);
  5464. END;
  5465. (* ---- SYSTEM.NEW ----- *)
  5466. ELSIF (id = Global.systemNew) & CheckArity(2,2) THEN
  5467. IF ~IsPointerType(parameter0.type) THEN
  5468. Error(parameter0.position,Diagnostics.Invalid,"is not a pointer")
  5469. ELSIF CheckSizeType(parameter1) THEN
  5470. parameter1 := NewConversion(Diagnostics.Invalid, parameter1, system.sizeType,NIL);
  5471. actualParameters.SetExpression(1,parameter1);
  5472. END;
  5473. (* ----SYSTEM.REF ---- *)
  5474. ELSIF (id = Global.systemRef) & CheckArity(1,1) & CheckStringType(parameter0) THEN
  5475. type := system.addressType
  5476. (* ---- INCR ----- *)
  5477. ELSIF (id = Global.Incr) & CheckArity(1,2) THEN
  5478. type := system.sizeType;
  5479. base := type0;
  5480. IF (numberActualParameters =2) & CheckSizeType(parameter1) THEN
  5481. IF ~IsIntegerValue(parameter1,i1) THEN i1 := 0 END;
  5482. IF i1 < 0 THEN
  5483. Error(position,Diagnostics.Invalid,"invalid dimension");
  5484. base := SyntaxTree.invalidType;
  5485. ELSE
  5486. base := ArrayBase(base,i1);
  5487. IF (base # NIL) & Indexable(base) THEN
  5488. ELSE
  5489. Error(position,Diagnostics.Invalid,"len on no array");
  5490. IF VerboseErrorMessage THEN
  5491. Printout.Info("base",base);
  5492. END;
  5493. base := SyntaxTree.invalidType;
  5494. END;
  5495. END;
  5496. parameter1 := NewConversion(parameter1.position,parameter1,system.longintType,NIL);
  5497. actualParameters.SetExpression(1,parameter1);
  5498. IF (numberActualParameters =2) & (parameter1 IS SyntaxTree.IntegerValue) THEN
  5499. mathArrayType := base(SyntaxTree.MathArrayType);
  5500. IF (mathArrayType.form = SyntaxTree.Static) THEN
  5501. result := SyntaxTree.NewIntegerValue(position,ToMemoryUnits(system,mathArrayType.staticIncrementInBits));
  5502. type := system.longintType;
  5503. END;
  5504. END;
  5505. ELSE
  5506. type := system.longintType;
  5507. END;
  5508. (* ---- SUM ----- *)
  5509. ELSIF (id = Global.Sum) & CheckArity(1,2) THEN (* can only be found by overloading *)
  5510. Error(position, Diagnostics.Invalid, "sum operator not applicable");
  5511. (* ---- ALL ----- *)
  5512. ELSIF (id = Global.All) & CheckArity(2,4) THEN (* can only be found by overloading *)
  5513. Error(position, Diagnostics.Invalid, "all operator not applicable");
  5514. (* ---- DIM ----- *)
  5515. ELSIF (id = Global.Dim) & CheckArity(1,1) THEN
  5516. type := system.sizeType;
  5517. IF type0 IS SyntaxTree.MathArrayType THEN
  5518. IF type0(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor THEN
  5519. i := Dimension(type0,{SyntaxTree.Open,SyntaxTree.Static});
  5520. result.SetResolved(SyntaxTree.NewIntegerValue(position,i));
  5521. END;
  5522. ELSE
  5523. Error(position,Diagnostics.Invalid,"dimension on non math array type");
  5524. END;
  5525. (* ---- CAS ----- *)
  5526. ELSIF (id = Global.Cas) & CheckArity(3,3) THEN
  5527. IF type0.IsComposite () THEN
  5528. Error(position,Diagnostics.Invalid,"first parameter of composite type");
  5529. result := SyntaxTree.invalidExpression;
  5530. ELSIF ~IsVariable (parameter0) THEN
  5531. Error(position,Diagnostics.Invalid,"first parameter not assignable");
  5532. result := SyntaxTree.invalidExpression;
  5533. ELSIF ~CompatibleTo(system,type1,type0) THEN
  5534. Error(position,Diagnostics.Invalid,"second parameter incompatible");
  5535. result := SyntaxTree.invalidExpression;
  5536. ELSIF ~CompatibleTo(system,type2,type0) THEN
  5537. Error(position,Diagnostics.Invalid,"third parameter incompatible");
  5538. result := SyntaxTree.invalidExpression;
  5539. ELSE
  5540. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,type0,NIL); actualParameters.SetExpression(1,parameter1);
  5541. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,type0,NIL); actualParameters.SetExpression(2,parameter2);
  5542. type := type0;
  5543. END;
  5544. (* ---- RESHAPE ----- *)
  5545. ELSIF (id = Global.Reshape) & CheckArity(2,2) THEN
  5546. IF type0 IS SyntaxTree.MathArrayType THEN
  5547. (* use type checking facilities of procedure calls: artificially build parameters here and call checker *)
  5548. base := ArrayBase(type0,MAX(LONGINT));
  5549. type := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5550. type(SyntaxTree.MathArrayType).SetArrayBase(base);
  5551. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Tensor);
  5552. parameterType(SyntaxTree.MathArrayType).SetArrayBase(base);
  5553. IF ~CompatibleTo(system,type0,parameterType) THEN
  5554. Error(parameter0.position,Diagnostics.Invalid,"incompatible parameter in reshape");
  5555. result := SyntaxTree.invalidExpression;
  5556. ELSE
  5557. parameter0 := NewConversion(Diagnostics.Invalid,parameter0,parameterType,NIL); actualParameters.SetExpression(0,parameter0);
  5558. END;
  5559. parameterType := SyntaxTree.NewMathArrayType(Diagnostics.Invalid,currentScope,SyntaxTree.Open);
  5560. parameterType(SyntaxTree.MathArrayType).SetArrayBase(system.longintType);
  5561. IF ~CompatibleTo(system,type1,parameterType) THEN
  5562. Error(parameter1.position,Diagnostics.Invalid,"parameter incompatible to math array of longint");
  5563. result := SyntaxTree.invalidExpression;
  5564. ELSE
  5565. parameter1 := NewConversion(Diagnostics.Invalid,parameter1,parameterType,NIL); actualParameters.SetExpression(1,parameter1);
  5566. END;
  5567. ELSE
  5568. Error(position, Diagnostics.Invalid,"reshape on non math array type");
  5569. result := SyntaxTree.invalidExpression;
  5570. END;
  5571. (* ---- SYSTEM.TYPECODE ----- *)
  5572. ELSIF (id = Global.systemTypeCode) & CheckArity(1,1) THEN
  5573. IF (parameter0.type = SyntaxTree.typeDeclarationType) THEN
  5574. type := parameter0(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration).declaredType;
  5575. type := type.resolved;
  5576. IF type IS SyntaxTree.PointerType THEN
  5577. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  5578. END;
  5579. IF ~(type IS SyntaxTree.RecordType) THEN
  5580. Error(parameter0.position,Diagnostics.Invalid,"must be type with type descriptor");
  5581. END;
  5582. ELSE
  5583. Error(parameter0.position,Diagnostics.Invalid,"is not a type symbol");
  5584. END;
  5585. type := system.addressType;
  5586. (* -------- FLT --------- *)
  5587. ELSIF (id = Global.Flt) & CheckArity(1,1) THEN
  5588. type := system.realType;
  5589. IF IsRealValue(parameter0, r) THEN
  5590. result.SetResolved(SyntaxTree.NewRealValue(position, r));
  5591. ELSIF CheckIntegerType(parameter0) & IsIntegerValue(parameter0, i) THEN
  5592. i0 := i; i := ABS(i);
  5593. IF i # 0 THEN
  5594. i1 := 23;
  5595. IF i >= 2*800000H THEN
  5596. REPEAT i := i DIV 2; INC(i1) UNTIL i < 2*800000H;
  5597. ELSIF i < 800000H THEN
  5598. REPEAT i := 2 * i; DEC(i1) UNTIL i >= 800000H;
  5599. END;
  5600. i := (i1 + 127)*800000H - 800000H + i;
  5601. IF i0 < 0 THEN i := SYSTEM.VAL(LONGINT, SYSTEM.VAL(SET, i) + {31}); END;
  5602. END;
  5603. result.SetResolved(SyntaxTree.NewRealValue(position, SYSTEM.VAL(REAL, i)));
  5604. END;
  5605. (* ------- CONNECT -------*)
  5606. ELSIF (id = Global.Connect) & (CheckArity(2,3)) THEN
  5607. IF ~(currentIsCellNet) THEN
  5608. Error(position,Diagnostics.Invalid,"connection outside activeCells body block");
  5609. END;
  5610. IF CheckPortType(parameter0, outPort) & CheckPortType(parameter1, inPort) THEN
  5611. IF (outPort.direction # SyntaxTree.OutPort) THEN Error(parameter0.position,Diagnostics.Invalid,"not an out-port") END;
  5612. IF (inPort.direction # SyntaxTree.InPort) THEN Error(parameter1.position,Diagnostics.Invalid,"not an in-port") END;
  5613. END;
  5614. IF numberActualParameters = 3 THEN
  5615. (*IF ~cellsAreObjects & ~IsIntegerValue(parameter2,i0) & (i0>=0) THEN
  5616. Error(position,Diagnostics.Invalid,"incompatible channel size parameter");
  5617. END;
  5618. *)
  5619. parameter2 := NewConversion(Diagnostics.Invalid,parameter2,system.longintType,NIL);
  5620. actualParameters.SetExpression(2,parameter2);
  5621. END;
  5622. activeCellsStatement := TRUE;
  5623. (* ---------- DELEGATE --------*)
  5624. ELSIF (id = Global.Delegate) & (CheckArity(2,2)) THEN
  5625. IF ~(currentIsCellNet) THEN
  5626. Error(position,Diagnostics.Invalid,"connection delegation outside activeCells body block");
  5627. END;
  5628. IF ~CheckPortType(parameter1, inPort) THEN
  5629. Error(parameter0.position,Diagnostics.Invalid,"not a port")
  5630. ELSIF ~CheckPortType(parameter0, outPort) THEN
  5631. Error(parameter1.position,Diagnostics.Invalid,"not a port")
  5632. ELSIF (outPort.direction # inPort.direction) THEN
  5633. Error(parameter0.position,Diagnostics.Invalid,"invalid port direction");
  5634. ELSIF outPort.sizeInBits # inPort.sizeInBits THEN
  5635. Error(position,Diagnostics.Invalid,"incompatible port sizes");
  5636. END;
  5637. activeCellsStatement := TRUE;
  5638. (* --------- RECEIVE ---------*)
  5639. ELSIF (id = Global.Receive) & CheckArity(2,3) THEN
  5640. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5641. IF CheckPortType(parameter0,inPort) & CheckVariable(parameter1) THEN
  5642. IF inPort.direction # SyntaxTree.InPort THEN
  5643. Error(parameter0.position,Diagnostics.Invalid,"not an in-port")
  5644. ELSIF inPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5645. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5646. END;
  5647. IF (numberActualParameters=3) & CheckVariable(parameter2) THEN
  5648. IF ~SameType(parameter2.type, system.integerType) THEN
  5649. Error(parameter2.position,Diagnostics.Invalid,"incompatible to integer type");
  5650. END;
  5651. END;
  5652. END;
  5653. (* --------- SEND ---------*)
  5654. ELSIF (id = Global.Send) & CheckArity(2,2) THEN
  5655. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,position) END;
  5656. IF CheckPortType(parameter0,outPort) THEN
  5657. IF outPort.direction # SyntaxTree.OutPort THEN
  5658. Error(parameter1.position,Diagnostics.Invalid,"not an out-port")
  5659. ELSIF outPort.sizeInBits # system.SizeOf(parameter1.type) THEN
  5660. Error(parameter1.position,Diagnostics.Invalid,"incompatible to port type");
  5661. ELSE
  5662. parameter1 := NewConversion(position,parameter1,parameter0.type.resolved,NIL);
  5663. actualParameters.SetExpression(1,parameter1);
  5664. END;
  5665. END;
  5666. (* ------- custom builtins ----- *)
  5667. ELSIF id = Global.systemSpecial THEN
  5668. customBuiltin := builtin(SyntaxTree.CustomBuiltin);
  5669. ASSERT(customBuiltin.type IS SyntaxTree.ProcedureType);
  5670. procedureType := customBuiltin.type(SyntaxTree.ProcedureType);
  5671. type := procedureType.returnType;
  5672. IF CheckArity(procedureType.numberParameters, procedureType.numberParameters) THEN (* check parameter count *)
  5673. (* go through all formal parameters *)
  5674. formalParameter := procedureType.firstParameter;
  5675. FOR i := 0 TO actualParameters.Length() - 1 DO
  5676. actualParameter := actualParameters.GetExpression(i);
  5677. IF actualParameter = SyntaxTree.invalidExpression THEN
  5678. ELSIF ~ParameterCompatible(formalParameter,actualParameter) THEN
  5679. Error(position,Diagnostics.Invalid, "incompatible parameter")
  5680. ELSE
  5681. actualParameter := NewConversion(actualParameter.position, actualParameter, formalParameter.type, NIL)
  5682. END;
  5683. actualParameters.SetExpression(i, actualParameter);
  5684. formalParameter := formalParameter.nextParameter
  5685. END
  5686. END
  5687. ELSE
  5688. Error(position,Diagnostics.Invalid,"builtin not implemented");
  5689. result := SyntaxTree.invalidExpression;
  5690. END;
  5691. END;
  5692. IF result # SyntaxTree.invalidExpression THEN
  5693. type := ResolveType(type);
  5694. IF result.resolved # NIL THEN result.resolved.SetType(type) END;
  5695. result.SetType(type);
  5696. END;
  5697. RETURN result
  5698. END NewBuiltinCallDesignator;
  5699. (** return type guard designator left(type)
  5700. - check if type can be extended (i.e. is no static record)
  5701. - check if type is a type extension of left.type
  5702. - returns new type guard designator
  5703. returns invalidDesignator = invalidExpression if error
  5704. **)
  5705. PROCEDURE NewTypeGuardDesignator(position: LONGINT; left: SyntaxTree.Designator; type: SyntaxTree.Type; typeExpression: SyntaxTree.Expression): SyntaxTree.Designator;
  5706. VAR result: SyntaxTree.Designator;
  5707. BEGIN
  5708. result := SyntaxTree.invalidDesignator;
  5709. IF ~IsTypeExtension(left.type.resolved,type.resolved) THEN
  5710. Error(position,Diagnostics.Invalid,"no type extension of type");
  5711. IF VerboseErrorMessage THEN
  5712. Printout.Info("left",left);
  5713. Printout.Info("type",type);
  5714. END;
  5715. ELSIF ~(left.type.resolved = type.resolved) & ~IsExtensibleDesignator(left) THEN (* left is not extensible *)
  5716. Error(position,Diagnostics.Invalid,"variable cannot be extended");
  5717. ELSIF IsUnsafePointer(left.type) THEN
  5718. Error(position,Diagnostics.Invalid,"forbidden type guard on unsafe pointer");
  5719. ELSE
  5720. result := SyntaxTree.NewTypeGuardDesignator(position,left,type);
  5721. result.SetType(type);
  5722. result.SetAssignable(left.assignable);
  5723. result(SyntaxTree.TypeGuardDesignator).SetTypeExpression(typeExpression);
  5724. END;
  5725. RETURN result
  5726. END NewTypeGuardDesignator;
  5727. (** check and resolve parameter designator left(expression list)
  5728. - check expression list
  5729. - if one parameter and left is extensible type and parameter contains type declaration then return TypeGuardDesignator
  5730. - elsif left is a procedure type then
  5731. - if left is a built-in procedure then return NewBuiltinCallDesignator
  5732. - else return is a procedure call then return ProcedureCallDesignator
  5733. returns invalidDesignator = invalidExpression if error
  5734. **)
  5735. PROCEDURE VisitParameterDesignator(designator: SyntaxTree.ParameterDesignator);
  5736. VAR
  5737. parameters: SyntaxTree.ExpressionList;
  5738. left: SyntaxTree.Designator;
  5739. result,expression: SyntaxTree.Expression;
  5740. typeDeclaration: SyntaxTree.TypeDeclaration;
  5741. type, expressionType: SyntaxTree.Type;
  5742. PROCEDURE BaseType(type: SyntaxTree.Type): SyntaxTree.Type;
  5743. BEGIN
  5744. type := type.resolved;
  5745. WHILE (type # NIL) & (type IS SyntaxTree.MathArrayType) DO
  5746. type := Resolved(type(SyntaxTree.MathArrayType).arrayBase);
  5747. END;
  5748. RETURN type
  5749. END BaseType;
  5750. BEGIN
  5751. IF Trace THEN D.Str("VisitParameterDesignator"); D.Ln; END;
  5752. result := SyntaxTree.invalidDesignator;
  5753. left := ResolveDesignator(designator.left);
  5754. IF left # SyntaxTree.invalidDesignator THEN
  5755. parameters := designator.parameters;
  5756. IF ExpressionList(parameters) THEN
  5757. IF (left.type = NIL) THEN
  5758. Error(left.position,Diagnostics.Invalid,"object is not a procedure or cannot be extended");
  5759. ELSIF IsExtensibleDesignator(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) THEN
  5760. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5761. ELSIF IsUnextensibleRecord(left) & (parameters.Length()=1) & IsTypeDesignator(parameters.GetExpression(0),typeDeclaration) & (typeDeclaration.declaredType.resolved = left.type.resolved) THEN
  5762. result := NewTypeGuardDesignator(designator.position,left,typeDeclaration.declaredType, parameters.GetExpression(0))
  5763. ELSIF (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5764. IF (left IS SyntaxTree.SymbolDesignator) & (left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Builtin) THEN
  5765. result := NewBuiltinCallDesignator(designator.position,left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Builtin),parameters,left,NIL);
  5766. ELSE
  5767. result := NewProcedureCallDesignator(designator.position,left,parameters)
  5768. END
  5769. ELSIF IsTypeDesignator(left,typeDeclaration) & (parameters.Length()=1) THEN
  5770. expression := parameters.GetExpression(0);
  5771. type := typeDeclaration.declaredType.resolved;
  5772. expressionType := BaseType(expression.type); (* type or base type of math array, if applicable *)
  5773. IF ((type IS SyntaxTree.NumberType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType)) &
  5774. ((expressionType IS SyntaxTree.NumberType) OR (expressionType IS SyntaxTree.AddressType) OR (expressionType IS SyntaxTree.SizeType)
  5775. OR (expressionType IS SyntaxTree.EnumerationType)
  5776. ) THEN
  5777. result := NewConversion(designator.position,expression,typeDeclaration.declaredType,left)
  5778. ELSE
  5779. Error(left.position,Diagnostics.Invalid,"invalid type in explicit conversion");
  5780. END;
  5781. ELSE
  5782. Error(left.position,Diagnostics.Invalid,"called object is not a procedure or cannot be extended");
  5783. IF VerboseErrorMessage THEN Printout.Info("designator",designator); Printout.Info("left",left) END;
  5784. result := SyntaxTree.invalidDesignator;
  5785. END;
  5786. ELSE
  5787. result := SyntaxTree.invalidDesignator
  5788. END;
  5789. END;
  5790. resolvedExpression := result;
  5791. END VisitParameterDesignator;
  5792. (** check dereference designator left^
  5793. - check if left is pointer type or left is object type
  5794. - return new dereference designator with type = left.baseType.type (if appropriate)
  5795. with error handling
  5796. returns invalidDesignator = invalidExpression if error
  5797. **)
  5798. PROCEDURE NewDereferenceDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5799. VAR type: SyntaxTree.Type; result: SyntaxTree.Designator;
  5800. BEGIN
  5801. result := SyntaxTree.invalidDesignator;
  5802. type := left.type;
  5803. IF (type # NIL) & ((type.resolved IS SyntaxTree.PointerType)) THEN
  5804. type := type.resolved(SyntaxTree.PointerType).pointerBase;
  5805. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5806. result.SetAssignable(TRUE);
  5807. result.SetType(type);
  5808. result.SetHidden(left.isHidden);
  5809. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.ArrayType) & (type.resolved(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  5810. type := type.resolved;
  5811. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5812. result.SetAssignable(TRUE);
  5813. result.SetType(type);
  5814. result.SetHidden(left.isHidden);
  5815. ELSIF (type # NIL) & (type.resolved IS SyntaxTree.CellType) THEN
  5816. result := SyntaxTree.NewDereferenceDesignator(position,left);
  5817. result.SetAssignable(TRUE);
  5818. result.SetType(type);
  5819. result.SetHidden(left.isHidden);
  5820. ELSE
  5821. Error(position,Diagnostics.Invalid,"dereference on no pointer");
  5822. IF VerboseErrorMessage THEN
  5823. Printout.Info("pointer", type);
  5824. Printout.Info("scope", currentScope);
  5825. END;
  5826. END;
  5827. RETURN result
  5828. END NewDereferenceDesignator;
  5829. (** check supercall designator left^
  5830. - if left is symbol designator with procedure symbol containing non-nil supermethod then
  5831. - return new supercall designator with type = left.type
  5832. with error handling
  5833. **)
  5834. PROCEDURE NewSupercallDesignator(position: LONGINT; left: SyntaxTree.Designator): SyntaxTree.Designator;
  5835. VAR result: SyntaxTree.Designator; symbol: SyntaxTree.Symbol; procedure: SyntaxTree.Procedure;
  5836. objectScope: SyntaxTree.Scope;
  5837. BEGIN
  5838. result := SyntaxTree.invalidDesignator;
  5839. IF left = SyntaxTree.invalidDesignator THEN
  5840. (* error already handled *)
  5841. ELSIF left IS SyntaxTree.SymbolDesignator THEN
  5842. symbol := left(SyntaxTree.SymbolDesignator).symbol;
  5843. ASSERT(symbol # SyntaxTree.invalidSymbol);
  5844. IF symbol IS SyntaxTree.Procedure THEN
  5845. procedure := symbol(SyntaxTree.Procedure);
  5846. objectScope := currentScope;
  5847. WHILE (objectScope # NIL) & ~(objectScope IS SyntaxTree.RecordScope) DO
  5848. objectScope := objectScope.outerScope;
  5849. END;
  5850. IF (left.left = NIL) OR ~
  5851. (
  5852. (left.left IS SyntaxTree.SelfDesignator) OR
  5853. (left.left IS SyntaxTree.DereferenceDesignator)
  5854. & (left.left(SyntaxTree.Designator).left # NIL)
  5855. & (left.left(SyntaxTree.Designator).left IS SyntaxTree.SelfDesignator)) OR (procedure.scope # objectScope) THEN
  5856. Error(position,Diagnostics.Invalid,"procedure not in immediate object scope");
  5857. IF VerboseErrorMessage THEN
  5858. Printout.Info("left.left",left.left);
  5859. END;
  5860. ELSIF procedure.super # NIL THEN
  5861. result := SyntaxTree.NewSupercallDesignator(position,left);
  5862. result.SetType(left.type.resolved)
  5863. ELSE
  5864. Error(position,Diagnostics.Invalid,"no supermethod for this procedure");
  5865. END;
  5866. ELSE
  5867. Error(position,Diagnostics.Invalid,"symbol is not a procedure");
  5868. END;
  5869. ELSE
  5870. Error(position,Diagnostics.Invalid,"is no symbol designator");
  5871. END;
  5872. RETURN result
  5873. END NewSupercallDesignator;
  5874. (** check and semantically resolve arrow designator left^
  5875. - if left is procedure type -> result := SupercallDesignator
  5876. - else result := DereferenceDesignator
  5877. returns result via global variable resolvedExpression
  5878. error handling deferred to procedures SupercallDesignator and DereferenceDesignator
  5879. **)
  5880. PROCEDURE VisitArrowDesignator(arrowDesignator: SyntaxTree.ArrowDesignator);
  5881. VAR left: SyntaxTree.Designator;
  5882. BEGIN
  5883. IF Trace THEN D.Str("VisitArrowDesignator"); D.Ln; END;
  5884. left := ResolveDesignator(arrowDesignator.left);
  5885. IF left # NIL THEN
  5886. IF (left.type = NIL) THEN
  5887. Error(arrowDesignator.position,Diagnostics.Invalid,"Invalid arrow designator");
  5888. ELSIF (left.type.resolved # NIL) & (left.type.resolved IS SyntaxTree.ProcedureType) THEN
  5889. resolvedExpression := NewSupercallDesignator(arrowDesignator.position,left);
  5890. ELSE
  5891. IF IsPointerToObject(left.type) THEN
  5892. Warning(arrowDesignator.position, "forbidden dereference on object");
  5893. END;
  5894. resolvedExpression := NewDereferenceDesignator(arrowDesignator.position,left)
  5895. END
  5896. END
  5897. END VisitArrowDesignator;
  5898. (** check and return expression
  5899. - if expression has no type then resolve expression
  5900. - resulting expression is exchanged via global variable "resolvedExpression" which makes this mechanism thread-unsafe
  5901. - return result
  5902. **)
  5903. PROCEDURE ResolveExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5904. VAR result,prev: SyntaxTree.Expression;
  5905. BEGIN
  5906. IF expression = NIL THEN result := NIL
  5907. ELSIF (expression.type = NIL) THEN
  5908. prev := resolvedExpression;
  5909. resolvedExpression := SyntaxTree.invalidExpression;
  5910. IF ~(expression IS SyntaxTree.BuiltinCallDesignator) THEN
  5911. expression.SetType(SyntaxTree.invalidType);
  5912. END;
  5913. expression.Accept(SELF);
  5914. result := resolvedExpression;
  5915. IF currentIsRealtime THEN
  5916. IF (result.type # NIL) & ~result.type.resolved.isRealtime THEN
  5917. Error(expression.position,Diagnostics.Invalid,"forbidden non-realtime expression in realtime procedure");
  5918. END;
  5919. END;
  5920. (* designator modifiers for backends if they support it ...*)
  5921. IF (expression IS SyntaxTree.Designator) & (expression(SyntaxTree.Designator).modifiers # NIL) & (result IS SyntaxTree.Designator) THEN
  5922. result(SyntaxTree.Designator).SetModifiers(expression(SyntaxTree.Designator).modifiers);
  5923. CheckModifiers(result(SyntaxTree.Designator).modifiers, FALSE);
  5924. END;
  5925. resolvedExpression := prev
  5926. ELSE
  5927. result := expression
  5928. END;
  5929. RETURN result
  5930. END ResolveExpression;
  5931. (**
  5932. check expression to be constant expression
  5933. - resolve expression
  5934. - if valid then check that of value type
  5935. report error and return invalidExpression if anything fails
  5936. **)
  5937. PROCEDURE ConstantExpression(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5938. VAR position: LONGINT;
  5939. BEGIN
  5940. position := expression.position;
  5941. expression := ResolveExpression(expression);
  5942. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5943. ELSIF (expression.resolved = NIL) THEN
  5944. Error(position,Diagnostics.Invalid,"expression is not constant");
  5945. IF VerboseErrorMessage THEN Printout.Info("expression",expression); END;
  5946. expression := SyntaxTree.invalidExpression;
  5947. END;
  5948. RETURN expression
  5949. END ConstantExpression;
  5950. (** check expression to be constant integer
  5951. - resolve expresssion
  5952. - if valid then check that of integer value type
  5953. report error and return invalidExpression if anything fails
  5954. **)
  5955. PROCEDURE ConstantInteger(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5956. VAR position: LONGINT;
  5957. BEGIN
  5958. position := expression.position;
  5959. expression := ResolveExpression(expression);
  5960. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5961. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5962. expression := SyntaxTree.invalidExpression;
  5963. Error(position,Diagnostics.Invalid,"expression is not a constant integer");
  5964. END;
  5965. RETURN expression
  5966. END ConstantInteger;
  5967. (** check expression as positive (>=0) constant integer
  5968. - resolve expression
  5969. - if valid then check that integer value
  5970. - if integer value then check that value >= 0
  5971. report error and return invalidExpression if anything fails
  5972. **)
  5973. PROCEDURE ConstantIntegerGeq0(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5974. VAR position: LONGINT;
  5975. BEGIN
  5976. position := expression.position;
  5977. expression := ConstantExpression(expression);
  5978. IF expression = SyntaxTree.invalidExpression THEN
  5979. (* error already reported *)
  5980. ELSIF (expression.resolved = NIL) OR ~(expression.resolved IS SyntaxTree.IntegerValue) THEN
  5981. Error(position,Diagnostics.Invalid,"expression is not integer valued");
  5982. expression := SyntaxTree.invalidExpression
  5983. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue <0) THEN
  5984. Error(position,Diagnostics.Invalid,"integer is not greater or equal zero");
  5985. END;
  5986. RETURN expression
  5987. END ConstantIntegerGeq0;
  5988. (** check expression as condition
  5989. - resolve expression
  5990. - if valid expression then check that result type is boolean
  5991. report error and return invalidExpression if anything fails
  5992. **)
  5993. PROCEDURE ResolveCondition(expression: SyntaxTree.Expression): SyntaxTree.Expression;
  5994. VAR position: LONGINT;
  5995. BEGIN
  5996. position := expression.position;
  5997. expression := ResolveExpression(expression);
  5998. IF expression = SyntaxTree.invalidExpression THEN (* error already reported *)
  5999. ELSIF (expression.type = NIL) OR ~(expression.type.resolved IS SyntaxTree.BooleanType) THEN
  6000. expression := SyntaxTree.invalidExpression;
  6001. Error(position,Diagnostics.Invalid,"expression is not boolean");
  6002. END;
  6003. RETURN expression
  6004. END ResolveCondition;
  6005. (*** symbols ***)
  6006. PROCEDURE ResolveSymbol(x: SyntaxTree.Symbol);
  6007. BEGIN
  6008. x.Accept(SELF);
  6009. END ResolveSymbol;
  6010. (** check a symbol
  6011. - check visibility flags (symbols within procedure scope (direct or indirect) cannot be exported)
  6012. **)
  6013. PROCEDURE CheckSymbolVisibility(symbol: SyntaxTree.Symbol);
  6014. VAR scope: SyntaxTree.Scope;
  6015. BEGIN
  6016. (* visibility *)
  6017. scope := symbol.scope;
  6018. WHILE (scope # NIL) & ~(scope IS SyntaxTree.ProcedureScope) DO
  6019. scope := scope.outerScope;
  6020. END;
  6021. IF (scope # NIL) THEN (* symbol (directly or indirectly) in procedure scope *)
  6022. IF (symbol.access * SyntaxTree.Public # {}) & (~(symbol IS SyntaxTree.Procedure) OR ~symbol(SyntaxTree.Procedure).isBodyProcedure & ~symbol(SyntaxTree.Procedure).isConstructor & ~symbol(SyntaxTree.Procedure).isFinalizer) THEN
  6023. Error(symbol.position,Diagnostics.Invalid,"cannot be exported");
  6024. IF VerboseErrorMessage THEN
  6025. Printout.Info("symbol",symbol);
  6026. END;
  6027. END;
  6028. END;
  6029. END CheckSymbolVisibility;
  6030. (** Check if a node has already been resolved. If not then mark as currently being resolved.
  6031. If node is currently being resolved then emit a cyclic definition error.
  6032. Return TRUE only if node is fully resolved.
  6033. **)
  6034. PROCEDURE SymbolNeedsResolution(x: SyntaxTree.Symbol): BOOLEAN;
  6035. VAR result: BOOLEAN;
  6036. BEGIN
  6037. IF SyntaxTree.Resolved IN x.state THEN
  6038. result := FALSE
  6039. ELSIF SyntaxTree.BeingResolved IN x.state THEN
  6040. Error(x.position,Diagnostics.Invalid,"cyclic definition");
  6041. result := FALSE;
  6042. ELSE
  6043. result := TRUE;
  6044. x.SetState(SyntaxTree.BeingResolved)
  6045. END;
  6046. RETURN result
  6047. END SymbolNeedsResolution;
  6048. (** check and resolve a type declaration symbol = Type
  6049. - set type to declaration type
  6050. -> the type of a type declaration is NOT the declared type but the "declaration" type.
  6051. This is so because the type declaration itself does not have a type but it only stands for a type.
  6052. In the implementation of the compiler this made a lot much easier.
  6053. - resolve and set declared type
  6054. - check symbol
  6055. **)
  6056. PROCEDURE VisitTypeDeclaration(typeDeclaration: SyntaxTree.TypeDeclaration);
  6057. VAR prevScope: SyntaxTree.Scope;
  6058. BEGIN
  6059. IF Trace THEN D.Str("VisitTypeDeclaration "); D.Str0(typeDeclaration.name); D.Ln; END;
  6060. IF SymbolNeedsResolution(typeDeclaration) THEN
  6061. prevScope := currentScope;
  6062. currentScope := typeDeclaration.scope;
  6063. typeDeclaration.SetType(SyntaxTree.typeDeclarationType);
  6064. typeDeclaration.SetDeclaredType(ResolveType(typeDeclaration.declaredType));
  6065. CheckSymbolVisibility(typeDeclaration);
  6066. typeDeclaration.SetState(SyntaxTree.Resolved);
  6067. currentScope := prevScope;
  6068. END;
  6069. END VisitTypeDeclaration;
  6070. (** check and resolve a constant declaration symbol = (constant) expression
  6071. - check expression
  6072. - set type and value
  6073. - check symbol
  6074. **)
  6075. PROCEDURE VisitConstant(constant: SyntaxTree.Constant);
  6076. VAR
  6077. expression: SyntaxTree.Expression;
  6078. type: SyntaxTree.Type;
  6079. name: Basic.SegmentedName;
  6080. replacement: Replacement;
  6081. BEGIN
  6082. IF Trace THEN D.Str("VisitConstant "); D.Str0(constant.name); D.Ln; END;
  6083. IF SymbolNeedsResolution(constant) THEN
  6084. expression := constant.value;
  6085. IF replacements # NIL THEN
  6086. Global.GetSymbolSegmentedName(constant, name);
  6087. replacement := replacements;
  6088. WHILE (replacement # NIL) & (replacement.name # name) DO
  6089. replacement := replacement.next;
  6090. END;
  6091. IF replacement # NIL THEN
  6092. InfoSS(constant.position, "replacing constant", constant.name);
  6093. (*
  6094. NEW(stringReader, Strings.Length(replacement.string^));
  6095. stringReader.Set(replacement.string^);
  6096. NEW(scanner, replacement.string^, stringReader,0, diagnostics);
  6097. NEW(parser, scanner, diagnostics);
  6098. expression := parser.Expression();
  6099. *)
  6100. expression := replacement.expression;
  6101. replacement.used := TRUE;
  6102. END;
  6103. END;
  6104. constant.SetType(SyntaxTree.invalidType);
  6105. expression := ConstantExpression(expression);
  6106. ASSERT(expression.type # NIL);
  6107. type := expression.type.resolved;
  6108. constant.SetType(type);
  6109. constant.SetValue(expression);
  6110. CheckSymbolVisibility(constant);
  6111. constant.SetState(SyntaxTree.Resolved);
  6112. END;
  6113. END VisitConstant;
  6114. PROCEDURE AdaptStackAlignment(procedure: SyntaxTree.Procedure; alignment: LONGINT);
  6115. VAR procedureAlignment: LONGINT;
  6116. PROCEDURE LCM(a0,b0: LONGINT): LONGINT;
  6117. (* least common multiple *)
  6118. VAR a,b: LONGINT;
  6119. BEGIN
  6120. a := a0; b := b0;
  6121. WHILE (a # b) DO
  6122. IF a < b THEN a := a+a0
  6123. ELSE b := b + b0
  6124. END;
  6125. END;
  6126. RETURN a
  6127. END LCM;
  6128. BEGIN
  6129. IF alignment > 1 THEN
  6130. procedureAlignment := procedure.type(SyntaxTree.ProcedureType).stackAlignment;
  6131. IF (procedureAlignment > 1) THEN
  6132. alignment := LCM(alignment, procedureAlignment);
  6133. END;
  6134. procedure.type(SyntaxTree.ProcedureType).SetStackAlignment(alignment);
  6135. END;
  6136. END AdaptStackAlignment;
  6137. (** check and resolve a variable / field
  6138. - check and set type
  6139. - negative check on open array type
  6140. - check symbol
  6141. **)
  6142. PROCEDURE VisitVariable(variable: SyntaxTree.Variable);
  6143. VAR modifiers: SyntaxTree.Modifier; value,position: LONGINT; pointerType: SyntaxTree.PointerType;
  6144. BEGIN
  6145. IF Trace THEN D.Str("VisitVariable "); D.Str0(variable.name); D.Ln; END;
  6146. IF SymbolNeedsResolution(variable) THEN
  6147. modifiers := variable.modifiers;
  6148. (*
  6149. flags := Flags(variable.modifiers,{SyntaxTree.UntracedFlag, SyntaxTree.AlignedFlag, SyntaxTree.FixedFlag});
  6150. variable.AddFlags(flags);
  6151. *)
  6152. variable.SetType(ResolveType(variable.type));
  6153. IF variable.type.resolved IS SyntaxTree.ArrayType THEN
  6154. IF variable.type.resolved(SyntaxTree.ArrayType).length = NIL THEN
  6155. Error(variable.position,Diagnostics.Invalid,"forbidden open array variable");
  6156. END;
  6157. END;
  6158. CheckSymbolVisibility(variable);
  6159. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6160. variable.SetUntraced(TRUE);
  6161. IF ~ContainsPointer(variable.type) THEN
  6162. IF VerboseErrorMessage THEN Printout.Info("variable",variable); Printout.Info("variable.type",variable.type.resolved); END;
  6163. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6164. END;
  6165. END;
  6166. IF HasValue(modifiers, Global.NameAligned,position, value) THEN
  6167. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6168. IF ~PowerOf2(value) THEN
  6169. Error(position,Diagnostics.Invalid,"forbidden alignment - must be power of two");
  6170. ELSE
  6171. AdaptStackAlignment(variable.scope(SyntaxTree.ProcedureScope).ownerProcedure, value);
  6172. END;
  6173. END;
  6174. variable.SetAlignment(FALSE,value);
  6175. ELSIF HasValue(modifiers, Global.NameFixed,position, value) THEN
  6176. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6177. Error(position,Diagnostics.Invalid,"fixed position not possible in procedure");
  6178. END;
  6179. variable.SetAlignment(TRUE, value);
  6180. ELSIF HasValue(modifiers, Global.NameFictive, position, value) THEN
  6181. IF (variable.scope IS SyntaxTree.ProcedureScope) THEN
  6182. Error(position, Diagnostics.Invalid,"fictive offset not possible in procedure");
  6183. END;
  6184. variable.SetFictive(value);
  6185. variable.SetOffset(value*system.dataUnit);
  6186. IF ContainsPointer(variable.type) THEN variable.SetUntraced(TRUE) END;
  6187. END;
  6188. IF HasFlag(modifiers, Global.NameRegister, position) THEN variable.SetUseRegister(TRUE) END;
  6189. IF variable.type.resolved IS SyntaxTree.CellType THEN
  6190. IF HasValue(modifiers, Global.NameCodeMemorySize, position, value) THEN END;
  6191. IF HasValue(modifiers, Global.NameDataMemorySize, position, value) THEN END;
  6192. END;
  6193. CheckModifiers(modifiers, ~InCellNetScope(variable.scope) & ~(variable.type.resolved IS SyntaxTree.CellType) & ~(variable.type.resolved IS SyntaxTree.PortType));
  6194. IF variable.initializer # NIL THEN
  6195. variable.SetInitializer (CompatibleConversion (variable.initializer.position, ConstantExpression(variable.initializer), variable.type));
  6196. END;
  6197. IF (variable.type.resolved IS SyntaxTree.CellType) (*& (cellsAreObjects)*) THEN
  6198. pointerType := SyntaxTree.NewPointerType(variable.position, variable.scope);
  6199. pointerType.SetPointerBase(variable.type);
  6200. pointerType.SetHidden(TRUE);
  6201. variable.SetType(ResolveType(pointerType));
  6202. END;
  6203. variable.SetState(SyntaxTree.Resolved);
  6204. END;
  6205. END VisitVariable;
  6206. PROCEDURE VisitProperty(property: SyntaxTree.Property);
  6207. BEGIN
  6208. VisitVariable(property)
  6209. END VisitProperty;
  6210. (** check and resolve a (procedure) parameter
  6211. - check and set type
  6212. - check symbol
  6213. - check parameter kind and set read-only flags if appropriate
  6214. **)
  6215. PROCEDURE VisitParameter(parameter: SyntaxTree.Parameter);
  6216. VAR modifiers: SyntaxTree.Modifier; expression: SyntaxTree.Expression; position: LONGINT;
  6217. BEGIN
  6218. IF Trace THEN D.Str("VisitParameter "); D.Str0(parameter.name); D.Ln; END;
  6219. IF SymbolNeedsResolution(parameter) THEN
  6220. modifiers := parameter.modifiers;
  6221. parameter.SetType(ResolveType(parameter.type));
  6222. ASSERT(parameter.type.resolved # NIL);
  6223. CheckSymbolVisibility(parameter);
  6224. IF parameter.defaultValue # NIL THEN
  6225. IF parameter.kind # SyntaxTree.ValueParameter THEN
  6226. Error(parameter.position,Diagnostics.Invalid,"forbidden default value on non-value parameter");
  6227. ELSE
  6228. expression := ConstantExpression(parameter.defaultValue);
  6229. IF CompatibleTo(system,expression.type, parameter.type) THEN
  6230. expression := NewConversion(expression.position, expression, parameter.type, NIL);
  6231. parameter.SetDefaultValue(expression);
  6232. END;
  6233. END;
  6234. END;
  6235. IF HasFlag(modifiers, Global.NameUntraced,position) THEN
  6236. parameter.SetUntraced(TRUE);
  6237. IF ~ContainsPointer(parameter.type) THEN
  6238. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6239. Error(position,Diagnostics.Invalid,"untraced flag on non-pointer variable");
  6240. END;
  6241. END;
  6242. IF HasFlag(modifiers, Global.NameMovable,position) THEN
  6243. parameter.SetMoveable(TRUE);
  6244. IF ~(parameter.type.resolved IS SyntaxTree.AddressType) THEN
  6245. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6246. Error(position,Diagnostics.Invalid,"illegal movable flag on non-address variable");
  6247. ELSIF parameter.kind = SyntaxTree.VarParameter THEN
  6248. IF VerboseErrorMessage THEN Printout.Info("parameter",parameter); Printout.Info("parameter.type",parameter.type.resolved); END;
  6249. Error(position,Diagnostics.Invalid,"unnecessary movable flag on variable variable");
  6250. END;
  6251. END;
  6252. CheckModifiers(modifiers, ~InCellNetScope(parameter.scope) & ~(parameter.type.resolved IS SyntaxTree.CellType) & ~(parameter.type.resolved IS SyntaxTree.PortType));
  6253. parameter.SetState(SyntaxTree.Resolved);
  6254. END;
  6255. END VisitParameter;
  6256. (** check and resolve a procedure (with declaration and implementation scope)
  6257. - check the procedure type
  6258. - check if method (i.e. in record scope), if so then
  6259. - check if (unique) constructor
  6260. - check if (unique) finalizer
  6261. - check if super method available, if so then check signature
  6262. - of not in record scope then negative check on constructor flag
  6263. - of not in record scope then negative check on finalizer flag
  6264. - check declarations (including a delayed implementation check, cf procedure Declarations)
  6265. - check procedure symbol
  6266. **)
  6267. PROCEDURE VisitProcedure(procedure: SyntaxTree.Procedure);
  6268. VAR super,proc: SyntaxTree.Procedure; record: SyntaxTree.RecordType;
  6269. procedureType: SyntaxTree.ProcedureType;
  6270. selfParameter: SyntaxTree.Parameter; qualifiedIdentifier: SyntaxTree.QualifiedIdentifier;
  6271. qualifiedType: SyntaxTree.QualifiedType;
  6272. value: LONGINT;
  6273. modifiers: SyntaxTree.Modifier; recentIsRealtime, recentIsBodyProcedure: BOOLEAN;
  6274. position: LONGINT;
  6275. BEGIN
  6276. IF Trace THEN D.Str("VisitProcedure "); D.Str0(procedure.name); D.Ln; END;
  6277. IF IsOberonInline(procedure) THEN
  6278. IF SyntaxTree.Public * procedure.access # {} THEN
  6279. Error(procedure.position, Diagnostics.Invalid, "Export of Oberon Inline Not Yet Implemented")
  6280. END;
  6281. procedure.SetInline(FALSE);
  6282. procedure.SetOberonInline(TRUE);
  6283. END;
  6284. IF SymbolNeedsResolution(procedure) THEN
  6285. recentIsRealtime := currentIsRealtime;
  6286. recentIsBodyProcedure := currentIsBodyProcedure;
  6287. IF Trace THEN D.Str("undefined"); D.Ln; END;
  6288. procedureType := procedure.type(SyntaxTree.ProcedureType);
  6289. modifiers := procedureType.modifiers;
  6290. IF HasFlag(modifiers, Global.NameWinAPI,position) THEN procedureType.SetCallingConvention(SyntaxTree.WinAPICallingConvention)
  6291. ELSIF HasFlag(modifiers, Global.NameC,position) THEN
  6292. IF useDarwinCCalls THEN (*fld*)
  6293. procedureType.SetCallingConvention(SyntaxTree.DarwinCCallingConvention)
  6294. ELSE
  6295. procedureType.SetCallingConvention(SyntaxTree.CCallingConvention)
  6296. END
  6297. END;
  6298. IF HasFlag(modifiers, Global.NameInterrupt, position) THEN
  6299. procedureType.SetInterrupt(TRUE);
  6300. procedureType.SetCallingConvention(SyntaxTree.InterruptCallingConvention)
  6301. END;
  6302. IF HasFlag(modifiers, Global.NameNoReturn, position) THEN
  6303. procedureType.SetNoReturn(TRUE);
  6304. END;
  6305. IF HasValue(modifiers, Global.NamePcOffset, position, value) THEN procedureType.SetPcOffset(value) END;
  6306. IF HasFlag(modifiers,Global.NameNoPAF,position) THEN procedureType.SetNoPAF(TRUE) END;
  6307. IF HasFlag(modifiers, Global.NameEntry,position) THEN procedure.SetEntry(TRUE)
  6308. ELSIF (procedure.scope IS SyntaxTree.ModuleScope) & HasFlag(modifiers, Global.NameExit, position) THEN procedure.SetExit(TRUE)
  6309. END;
  6310. IF HasValue(modifiers,Global.NameAligned,position,value) THEN procedure.SetAlignment(FALSE,value)
  6311. ELSIF HasValue(modifiers,Global.NameFixed,position,value) THEN procedure.SetAlignment(TRUE,value)
  6312. END;
  6313. IF HasValue(modifiers,Global.NameStackAligned, position, value) THEN
  6314. IF ~PowerOf2(value) THEN
  6315. Error(position,Diagnostics.Invalid,"forbidden stack alignment - must be power of two");
  6316. ELSE
  6317. procedureType.SetStackAlignment(value)
  6318. END;
  6319. END;
  6320. IF HasFlag(modifiers,Global.NameRealtime,position) THEN procedureType.SetRealtime(TRUE) END;
  6321. IF HasFlag(modifiers,Global.NameFinal,position) THEN procedure.SetFinal(TRUE)
  6322. ELSIF HasFlag(modifiers,Global.NameAbstract,position) THEN procedure.SetAbstract(TRUE)
  6323. END;
  6324. CheckModifiers(modifiers, TRUE);
  6325. modifiers := procedureType.returnTypeModifiers;
  6326. procedureType.SetUntracedReturn(HasFlag(modifiers, Global.NameUntraced, position));
  6327. CheckModifiers(modifiers, TRUE);
  6328. FixProcedureType(procedureType);
  6329. currentIsRealtime := procedureType.isRealtime;
  6330. currentIsBodyProcedure := procedure.isBodyProcedure;
  6331. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6332. THEN
  6333. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6334. END;
  6335. CheckSymbolVisibility(procedure);
  6336. IF procedure.scope IS SyntaxTree.ProcedureScope THEN
  6337. procedure.SetLevel(procedure.scope(SyntaxTree.ProcedureScope).ownerProcedure.level+1);
  6338. IF ~system.GenerateParameterOffsets(procedure,TRUE) THEN
  6339. Error(procedure.position,Diagnostics.Invalid,"problem during parameter offset generation");
  6340. END;
  6341. END;
  6342. IF procedure.scope IS SyntaxTree.RecordScope THEN
  6343. record := procedure.scope(SyntaxTree.RecordScope).ownerRecord;
  6344. procedureType.SetDelegate(TRUE);
  6345. selfParameter := SyntaxTree.NewParameter(procedure.position,procedureType,Global.SelfParameterName,SyntaxTree.ValueParameter);
  6346. IF record.pointerType.typeDeclaration = NIL THEN
  6347. selfParameter.SetType(record.pointerType);
  6348. ELSE
  6349. qualifiedIdentifier := SyntaxTree.NewQualifiedIdentifier(procedure.position,SyntaxTree.invalidIdentifier,record.pointerType.typeDeclaration.name);
  6350. qualifiedType := SyntaxTree.NewQualifiedType(procedure.position,procedure.scope,qualifiedIdentifier);
  6351. qualifiedType.SetResolved(record.pointerType);
  6352. selfParameter.SetType(qualifiedType);
  6353. END;
  6354. selfParameter.SetAccess(SyntaxTree.Hidden);
  6355. (*! constructor information is redundant, we can remove "isConstructor" and repplace by constructor procedure reference *)
  6356. IF procedure.isConstructor THEN
  6357. (*! constructor is always visible, compatibility to paco
  6358. procedure.SetAccess(SyntaxTree.Public+SyntaxTree.Protected+SyntaxTree.Internal);
  6359. *)
  6360. procedure.MarkUsed;
  6361. IF procedureType.returnType # NIL THEN
  6362. Error(procedure.position,Diagnostics.Invalid,"constructor with forbidden return type");
  6363. END;
  6364. proc := procedure.scope.firstProcedure;
  6365. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isConstructor)) DO
  6366. proc := proc.nextProcedure;
  6367. END;
  6368. IF proc # NIL THEN
  6369. Error(procedure.position,Diagnostics.Invalid,"duplicate constructor")
  6370. ELSE
  6371. procedure.scope(SyntaxTree.RecordScope).SetConstructor(procedure);
  6372. END;
  6373. END;
  6374. IF procedure.isFinalizer THEN
  6375. procedure.MarkUsed;
  6376. IF procedureType.returnType # NIL THEN
  6377. Error(procedure.position,Diagnostics.Invalid,"finalizer with forbidden return type");
  6378. END;
  6379. IF procedureType.numberParameters # 0 THEN
  6380. Error(procedure.position,Diagnostics.Invalid,"finalizer with formal parameters");
  6381. END;
  6382. proc := procedure.scope.firstProcedure;
  6383. WHILE (proc # NIL) & ((proc = procedure) OR ~(proc.isFinalizer)) DO
  6384. proc := proc.nextProcedure;
  6385. END;
  6386. IF proc # NIL THEN
  6387. Error(procedure.position,Diagnostics.Invalid,"duplicate finalizer")
  6388. ELSE
  6389. procedure.scope(SyntaxTree.RecordScope).SetFinalizer(procedure);
  6390. END;
  6391. END;
  6392. super := FindSuperProcedure(record.recordScope, procedure);
  6393. IF (super # NIL) & SignatureCompatible(procedure.position,procedureType,super.type.resolved(SyntaxTree.ProcedureType)) THEN
  6394. IF (super.isConstructor) & ~(procedure.isConstructor) THEN
  6395. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-constructor extends constructor");
  6396. END;
  6397. IF (super.isFinalizer) & ~(procedure.isFinalizer) THEN
  6398. Error(procedure.position,Diagnostics.Invalid,"incompatible signature: non-finalizer extends finalizer");
  6399. END;
  6400. IF super.isFinal THEN
  6401. Error(procedure.position,Diagnostics.Invalid,"forbidden method extending final method");
  6402. END;
  6403. procedure.SetSuper(super);
  6404. super.SetOverwritten(TRUE);
  6405. procedure.SetAccess(procedure.access+super.access);
  6406. procedure.MarkUsed;
  6407. END;
  6408. IF ~system.GenerateParameterOffsets(procedure,FALSE) (* assume that this is no nested procedure, is fixed later otherwise *)
  6409. THEN
  6410. Error(procedure.position,Diagnostics.Invalid,"problems during parameter offset computation");
  6411. END;
  6412. ELSIF procedure.scope IS SyntaxTree.CellScope THEN (* allowed to be constructor *)
  6413. IF cellsAreObjects THEN
  6414. procedureType.SetDelegate(TRUE);
  6415. END;
  6416. IF procedure.isConstructor THEN
  6417. procedure.scope(SyntaxTree.CellScope).SetConstructor(procedure);
  6418. END;
  6419. ELSIF procedure.isConstructor THEN
  6420. Error(procedure.position,Diagnostics.Invalid,"procedure illegaly marked as initializer - not in object scope");
  6421. END;
  6422. Declarations(procedure.procedureScope, FALSE);
  6423. (* body resolution part done as late fix of the procedure type *)
  6424. procedure.SetState(SyntaxTree.Resolved);
  6425. currentIsRealtime := recentIsRealtime;
  6426. currentIsBodyProcedure := recentIsBodyProcedure;
  6427. END;
  6428. END VisitProcedure;
  6429. (**
  6430. a builtin procedure is a global item that may not be modified locally
  6431. instead the resolving of builtin procedure calls are done in the esignator
  6432. **)
  6433. PROCEDURE VisitBuiltin(builtinProcedure: SyntaxTree.Builtin);
  6434. VAR type: SyntaxTree.Type;
  6435. BEGIN
  6436. type := ResolveType(builtinProcedure.type);
  6437. END VisitBuiltin;
  6438. (* nopov *)
  6439. (** check and resolve operator
  6440. - operators are first checked as procedures
  6441. - then additional operator-specific checks are done
  6442. - note that only module-scope operators are checked here
  6443. (operators in a record scope are only allowed in the context of
  6444. array-structured object types and checked in 'ResolveArrayStructure')
  6445. - also note that inter-operator conformity is not checked here
  6446. **)
  6447. PROCEDURE VisitOperator(operator: SyntaxTree.Operator);
  6448. VAR
  6449. procedureType: SyntaxTree.ProcedureType;
  6450. leftType, rightType: SyntaxTree.Type;
  6451. identifierNumber, position: LONGINT;
  6452. hasReturnType, mustBeUnary, mustBeBinary, mustReturnBoolean, mustReturnInteger, mustHaveEquitypedOperands: BOOLEAN;
  6453. modifiers: SyntaxTree.Modifier;
  6454. (** whether a type is locally defined in the current module scope
  6455. for arrays, the base type must be locally defined **)
  6456. PROCEDURE IsLocallyDefined(type: SyntaxTree.Type): BOOLEAN;
  6457. BEGIN
  6458. IF type = NIL THEN
  6459. RETURN FALSE
  6460. ELSIF (type.typeDeclaration # NIL) & (type.typeDeclaration.scope.ownerModule = currentScope.ownerModule) THEN
  6461. RETURN TRUE
  6462. ELSIF (type.resolved IS SyntaxTree.ArrayType) THEN
  6463. RETURN IsLocallyDefined(type.resolved(SyntaxTree.ArrayType).arrayBase)
  6464. ELSIF (type.resolved IS SyntaxTree.MathArrayType) THEN
  6465. RETURN IsLocallyDefined(type.resolved(SyntaxTree.MathArrayType).arrayBase)
  6466. ELSE
  6467. RETURN FALSE
  6468. END
  6469. END IsLocallyDefined;
  6470. BEGIN
  6471. ASSERT(operator.type IS SyntaxTree.ProcedureType);
  6472. procedureType := operator.type(SyntaxTree.ProcedureType);
  6473. modifiers := procedureType.modifiers;
  6474. IF HasFlag(modifiers, Global.NameDynamic, position) THEN operator.SetDynamic(TRUE) END;
  6475. CheckModifiers(modifiers, TRUE);
  6476. VisitProcedure(operator);
  6477. IF operator.scope IS SyntaxTree.RecordScope THEN
  6478. ELSIF operator.scope IS SyntaxTree.ModuleScope THEN
  6479. identifierNumber := Global.GetSymbol(operator.scope.ownerModule.case, operator.name);
  6480. IF identifierNumber = -1 THEN
  6481. Error(operator.position, Diagnostics.Invalid, "operator with unknown identifier")
  6482. ELSIF ~system.operatorDefined[identifierNumber] THEN
  6483. Error(operator.position, Diagnostics.Invalid, "identifier may not be used for operator")
  6484. ELSE
  6485. IF procedureType.numberParameters < 1 THEN
  6486. Error(operator.position, Diagnostics.Invalid, "operator without operand");
  6487. ELSIF procedureType.numberParameters > 2 THEN
  6488. Error(operator.position, Diagnostics.Invalid, "operator with more than two operands");
  6489. ELSE
  6490. (* determine operand types *)
  6491. leftType := procedureType.firstParameter.type;
  6492. IF procedureType.numberParameters > 1 THEN
  6493. rightType := procedureType.firstParameter.nextParameter.type
  6494. ELSE
  6495. rightType := NIL
  6496. END;
  6497. (* check whether at least one of the operand types is declared in the current module (this check is skipped for the module FoxArrayBase) *)
  6498. IF currentScope.ownerModule.name # Global.ArrayBaseName THEN
  6499. IF ~(IsLocallyDefined(leftType) OR IsLocallyDefined(rightType)) THEN
  6500. Error(operator.position, Diagnostics.Invalid, "none of the operands is declared in the same module")
  6501. END
  6502. END;
  6503. (* TODO: refine the checks, think about how restrictive the checks should be
  6504. requiring operators such as "&", "OR", "~" to return Booleans, makes overloading for them almost pointless.
  6505. They might be used for intersection, union, complement of custom object types *)
  6506. (* defaults *)
  6507. hasReturnType := TRUE;
  6508. mustBeUnary := FALSE;
  6509. mustBeBinary := FALSE;
  6510. mustReturnBoolean := FALSE;
  6511. mustReturnInteger := FALSE;
  6512. mustHaveEquitypedOperands := FALSE;
  6513. (* operator-specific exceptions *)
  6514. CASE identifierNumber OF
  6515. | Scanner.Equal, Scanner.Unequal, Scanner.Less, Scanner.LessEqual, Scanner.Greater, Scanner.GreaterEqual:
  6516. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6517. | Scanner.DotEqual, Scanner.DotUnequal, Scanner.DotLess, Scanner.DotLessEqual, Scanner.DotGreater, Scanner.DotGreaterEqual:
  6518. mustBeBinary := TRUE
  6519. | Scanner.In: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6520. | Scanner.Is: mustBeBinary := TRUE; mustReturnBoolean := TRUE
  6521. | Scanner.Times: mustBeBinary := TRUE
  6522. | Scanner.TimesTimes: mustBeBinary := TRUE
  6523. | Scanner.DotTimes: mustBeBinary := TRUE
  6524. | Scanner.PlusTimes: mustBeBinary := TRUE
  6525. | Scanner.Slash: mustBeBinary := TRUE
  6526. | Scanner.Backslash: mustBeBinary := TRUE
  6527. | Scanner.DotSlash: mustBeBinary := TRUE
  6528. | Scanner.Div, Scanner.Mod: mustBeBinary := TRUE;
  6529. | Scanner.And, Scanner.Or: mustBeBinary := TRUE;
  6530. | Scanner.Not: mustBeUnary := TRUE
  6531. | Scanner.Plus, Scanner.Minus: (* unary and binary *)
  6532. | Scanner.Becomes: mustBeBinary := TRUE; hasReturnType := FALSE;
  6533. | Scanner.Transpose: mustBeUnary := TRUE;
  6534. | Global.Conversion: mustBeUnary := TRUE; (* TODO: get rid of return type? *)
  6535. | Global.DotTimesPlus: mustBeBinary := TRUE;
  6536. | Global.AtMulDec, Global.AtMulInc: mustBeBinary := TRUE;
  6537. | Global.DecMul, Global.IncMul: mustBeBinary := TRUE;
  6538. | Global.Dec, Global.Inc: hasReturnType := FALSE; (* unary and binary *)
  6539. | Global.Excl, Global.Incl:hasReturnType := FALSE;
  6540. | Global.Abs: mustBeUnary := TRUE;
  6541. | Global.Ash: (* TODO: arity? *)
  6542. | Global.Cap: (* TODO: arity? *)
  6543. | Global.Chr: mustBeUnary := TRUE;
  6544. | Global.Entier: (* TODO: arity? *)
  6545. | Global.EntierH: (* TODO: arity? *)
  6546. | Global.Len: (* unary and binary *)
  6547. | Global.Short, Global.Long: mustBeUnary := TRUE;
  6548. | Global.Max, Global.Min: (* unary and binary *)
  6549. | Global.Odd: (* TODO: arity? *)
  6550. | Global.Sum: (* TODO: arity? *)
  6551. | Global.All: (* TODO: arity? *)
  6552. | Global.Dim: mustBeUnary := TRUE; mustReturnInteger := TRUE;
  6553. | Scanner.Alias:
  6554. | Scanner.GreaterGreater, Scanner.LessLess:
  6555. mustBeBinary := TRUE; hasReturnType := FALSE;
  6556. | Scanner.GreaterGreaterQ, Scanner.LessLessQ:
  6557. mustBeBinary := TRUE; mustReturnBoolean := TRUE;
  6558. END;
  6559. (* check parameter count *)
  6560. IF mustBeUnary & (procedureType.numberParameters # 1) THEN
  6561. Error(operator.position, Diagnostics.Invalid,"operator is not unary")
  6562. ELSIF mustBeBinary & (procedureType.numberParameters # 2) THEN
  6563. Error(operator.position, Diagnostics.Invalid,"operator is not binary")
  6564. END;
  6565. (* check parameter types *)
  6566. (* TODO: is this used at all? *)
  6567. IF mustHaveEquitypedOperands & (procedureType.numberParameters = 2) THEN
  6568. leftType := procedureType.firstParameter.type;
  6569. rightType := procedureType.firstParameter.nextParameter.type;
  6570. IF ~leftType.resolved.SameType(rightType.resolved) THEN
  6571. Error(operator.position,Diagnostics.Invalid, "the two operands are not of the same type")
  6572. END
  6573. END;
  6574. (* check return type *)
  6575. IF hasReturnType THEN
  6576. IF procedureType.returnType = NIL THEN
  6577. Error(operator.position, Diagnostics.Invalid, "return type required")
  6578. ELSIF mustReturnBoolean THEN
  6579. IF ~(procedureType.returnType.resolved IS SyntaxTree.BooleanType) THEN
  6580. Error(operator.position, Diagnostics.Invalid,"return type is not Boolean")
  6581. END
  6582. ELSIF mustReturnInteger THEN
  6583. IF ~(procedureType.returnType.resolved IS SyntaxTree.IntegerType) THEN
  6584. Error(operator.position, Diagnostics.Invalid,"return type is not integer")
  6585. END
  6586. END
  6587. ELSIF procedureType.returnType # NIL THEN
  6588. Error(operator.position, Diagnostics.Invalid, "return type not allowed")
  6589. END
  6590. END
  6591. END
  6592. END
  6593. END VisitOperator;
  6594. PROCEDURE AddImport*(module: SyntaxTree.Module; x: SyntaxTree.Import): BOOLEAN;
  6595. VAR prevScope: SyntaxTree.Scope; prevDiagnostics: Diagnostics.Diagnostics;
  6596. BEGIN
  6597. IF error THEN RETURN FALSE END;
  6598. prevScope := currentScope;
  6599. prevDiagnostics := diagnostics;
  6600. diagnostics := NIL; (* suppress error output *)
  6601. currentScope := module.moduleScope;
  6602. VisitImport(x);
  6603. IF ~error THEN
  6604. module.moduleScope.AddImport(x);
  6605. x.SetScope(module.moduleScope);
  6606. END;
  6607. currentScope := prevScope;
  6608. diagnostics := prevDiagnostics;
  6609. IF error THEN error := FALSE; RETURN FALSE ELSE RETURN TRUE END;
  6610. END AddImport;
  6611. (** check and resolve import
  6612. - check for name = SYSTEM
  6613. - check for forbidden self import
  6614. - search through global import cache: already imported?
  6615. - check if already imported indirectly
  6616. - import if necessary -> set module and enter into import cache
  6617. - enter re-imports into list of imported modules as non-direct import (if not in direct import list)
  6618. - after this import this direct import and all indirect imports are stored in the current module's import list
  6619. **)
  6620. PROCEDURE VisitImport(x: SyntaxTree.Import);
  6621. VAR
  6622. module: SyntaxTree.Module;
  6623. moduleScope: SyntaxTree.ModuleScope;
  6624. import,reimport: SyntaxTree.Import;
  6625. filename: FileName;
  6626. prevScope: SyntaxTree.Scope;
  6627. BEGIN
  6628. IF SymbolNeedsResolution(x) THEN
  6629. prevScope := currentScope;
  6630. x.SetType(SyntaxTree.importType);
  6631. moduleScope := currentScope.ownerModule.moduleScope;
  6632. IF (x.moduleName=Global.SystemName) THEN x.SetModule(system.systemModule[Scanner.Uppercase])
  6633. ELSIF (x.moduleName=Global.systemName) THEN x.SetModule(system.systemModule[Scanner.Lowercase])
  6634. ELSIF (x.moduleName=currentScope.ownerModule.name) & (x.context=currentScope.ownerModule.context) THEN
  6635. Error(x.position,Diagnostics.Invalid,"forbidden self import");
  6636. ELSE
  6637. (* search through global import list: already imported ? *)
  6638. IF (x.module = NIL) & (importCache # NIL) THEN
  6639. import := importCache.ImportByModuleName(x.moduleName,x.context);
  6640. ELSE import := NIL
  6641. END;
  6642. IF x.module # NIL THEN (* already imported indirectly *)
  6643. module := x.module;
  6644. ELSIF import # NIL THEN (* already in module list *)
  6645. module := import.module;
  6646. ASSERT(module # NIL);
  6647. x.SetModule(module);
  6648. ELSE (* must be imported *)
  6649. Global.ModuleFileName(x.moduleName,x.context,filename);
  6650. IF symbolFileFormat # NIL THEN
  6651. module := symbolFileFormat.Import(filename,importCache); (* includes module parsing *)
  6652. IF module = NIL THEN
  6653. ErrorSS(x.position,"could not import",filename);
  6654. IF VerboseErrorMessage THEN
  6655. Printout.Info("import",x)
  6656. END
  6657. ELSE
  6658. (*
  6659. IF ~(SyntaxTree.Resolved IN module.state) THEN
  6660. (*! should rather be done by importer *)
  6661. checker := NewChecker(diagnostics,VerboseErrorMessage,system,symbolFileFormat,importCache);
  6662. checker.importCache := importCache;
  6663. checker.arrayBaseImported := arrayBaseImported;
  6664. checker.global := global;
  6665. checker.Module(module); (* semantic check *)
  6666. error := error OR checker.error;
  6667. END;
  6668. *)
  6669. (*
  6670. ASSERT(SyntaxTree.Resolved IN module.state);
  6671. *)
  6672. x.SetModule(module);
  6673. IF importCache # NIL THEN
  6674. import := SyntaxTree.NewImport(InvalidPosition,x.moduleName,x.moduleName,FALSE);
  6675. import.SetContext(x.context);
  6676. import.SetModule(module);
  6677. importCache.AddImport(import);
  6678. END;
  6679. END;
  6680. ELSE
  6681. ErrorSS(x.position,"no symbol file specified: cannot import",filename);
  6682. END;
  6683. END;
  6684. IF module # NIL THEN (* enter reimports into list of imported modules *)
  6685. IF SELF.module = NIL THEN (* happens in recursive imports *)
  6686. END;
  6687. import := module.moduleScope.firstImport;
  6688. WHILE(import # NIL) DO
  6689. ASSERT(import.moduleName # SyntaxTree.invalidIdentifier);
  6690. ASSERT(currentScope # NIL);
  6691. ASSERT(currentScope.ownerModule # NIL);
  6692. ASSERT(import.context # SyntaxTree.invalidIdentifier);
  6693. IF (import.moduleName=currentScope.ownerModule.name) & (import.context=currentScope.ownerModule.context) THEN
  6694. Error(x.position,Diagnostics.Invalid,"recursive import");
  6695. ELSE
  6696. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(x.context) END;
  6697. reimport := moduleScope.ImportByModuleName(import.moduleName,import.context);
  6698. IF reimport = NIL THEN (* indirect import *)
  6699. reimport := SyntaxTree.NewImport(InvalidPosition,import.moduleName,import.moduleName,FALSE);
  6700. reimport.SetContext(import.context);
  6701. reimport.SetModule(import.module);
  6702. moduleScope.AddImport(reimport);
  6703. reimport.SetScope(moduleScope);
  6704. ELSE
  6705. ASSERT(import.module # NIL);
  6706. reimport.SetModule(import.module); (* direct or indirect import *)
  6707. END;
  6708. END;
  6709. import := import.nextImport;
  6710. END;
  6711. END;
  6712. END;
  6713. currentScope := prevScope;
  6714. (* ELSE nothing to be done *)
  6715. x.SetState(SyntaxTree.Resolved);
  6716. END;
  6717. END VisitImport;
  6718. (*** statements ***)
  6719. PROCEDURE ResolveStatement(x: SyntaxTree.Statement): SyntaxTree.Statement;
  6720. VAR prev,resolved: SyntaxTree.Statement;
  6721. BEGIN
  6722. prev := resolvedStatement;
  6723. resolvedStatement := x;
  6724. IF currentIsUnreachable THEN x.SetUnreachable(TRUE) END;
  6725. activeCellsStatement := FALSE;
  6726. x.Accept(SELF);
  6727. (* removed this, implementation restriction should be resolved by backend
  6728. IF (inCellNetBody) & (activeCellsStatement = FALSE) THEN
  6729. Error(x.position, Diagnostics.Invalid, "non-activeCells statement in activeCells block - not yet implemented");
  6730. END;
  6731. *)
  6732. resolved := resolvedStatement;
  6733. resolvedStatement := prev;
  6734. RETURN resolved
  6735. END ResolveStatement;
  6736. (** check and resolve statement sequence
  6737. - check all statements, replace if necessary
  6738. **)
  6739. PROCEDURE StatementSequence(statementSequence: SyntaxTree.StatementSequence);
  6740. VAR i: LONGINT; statement,resolved: SyntaxTree.Statement;
  6741. BEGIN
  6742. IF statementSequence # NIL THEN (* else empty *)
  6743. FOR i := 0 TO statementSequence.Length()-1 DO
  6744. statement := statementSequence.GetStatement(i);
  6745. resolved := ResolveStatement(statement);
  6746. IF (resolved # statement) THEN
  6747. statementSequence.SetStatement(i,resolved);
  6748. END;
  6749. END;
  6750. END;
  6751. END StatementSequence;
  6752. (** check and resolve procedure call statement procedureCall() or procedureCall;
  6753. - check if call is a procedure call designator, if not (procedure type symbol) try to make one out of it
  6754. - check if procedure is callable
  6755. - check return type = NIL (otherwise must be assignment statement)
  6756. **)
  6757. PROCEDURE VisitProcedureCallStatement(procedureCall: SyntaxTree.ProcedureCallStatement);
  6758. VAR call: SyntaxTree.Designator;
  6759. BEGIN
  6760. IF Trace THEN D.Str("VisitProcedureCallStatement"); D.Ln; END;
  6761. call := procedureCall.call;
  6762. IF (call # NIL) & ~(call IS SyntaxTree.ParameterDesignator) & ~(call IS SyntaxTree.ProcedureCallDesignator) & ~(call IS SyntaxTree.BuiltinCallDesignator) THEN
  6763. call := SyntaxTree.NewParameterDesignator(call.position,call,SyntaxTree.NewExpressionList());
  6764. END;
  6765. call := ResolveDesignator(call);
  6766. IF call = SyntaxTree.invalidDesignator THEN
  6767. (* error already handled *)
  6768. ELSIF call IS SyntaxTree.StatementDesignator THEN
  6769. (* inline call in a statement *)
  6770. ELSIF ~IsCallable(call) THEN
  6771. Error(procedureCall.position,Diagnostics.Invalid,"called object is not a procedure");
  6772. ELSIF (call.type # NIL) & (call.left # NIL) & (call.left.type.resolved(SyntaxTree.ProcedureType).callingConvention # SyntaxTree.WinAPICallingConvention) THEN
  6773. Error(procedureCall.position,Diagnostics.Invalid,"calling procedure with non-void return type");
  6774. IF VerboseErrorMessage THEN Printout.Info("call ",call) END;
  6775. END;
  6776. procedureCall.SetCall(call);
  6777. (*
  6778. IF call = SyntaxTree.invalidDesignator THEN
  6779. ELSIF (call.left IS SyntaxTree.SymbolDesignator) & (call.left(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Procedure) THEN
  6780. procedure := call.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure);
  6781. IF IsOberonInline(procedure) THEN
  6782. Warning(procedure.position,"call to inline proc");
  6783. block := SyntaxTree.NewStatementBlock(call.position, NIL (*! todo *));
  6784. block.SetStatementSequence(SyntaxTree.CloneStatementSequence(procedure.procedureScope.body.statements));
  6785. ReplaceParameters(block, procedure.type(SyntaxTree.ProcedureType).firstParameter, call(SyntaxTree.ProcedureCallDesignator).parameters);
  6786. resolvedStatement := block;
  6787. RETURN;
  6788. END;
  6789. END;
  6790. *)
  6791. END VisitProcedureCallStatement;
  6792. (** check and resolve assignment LHS := RHS
  6793. - resolve LHS and RHS
  6794. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6795. - check if assignment is compatible
  6796. - check if LHS is variable (i.e. assignable)
  6797. - convert RHS if necessary
  6798. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6799. - assignment between different ASOTs
  6800. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6801. - assignment to ASOT elements:
  6802. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6803. **)
  6804. PROCEDURE VisitAssignment(assignment: SyntaxTree.Assignment);
  6805. VAR
  6806. left: SyntaxTree.Designator;
  6807. right, expression: SyntaxTree.Expression;
  6808. designator: SyntaxTree.Designator;
  6809. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6810. mathArrayType: SyntaxTree.MathArrayType;
  6811. BEGIN
  6812. right := ResolveExpression(assignment.right);
  6813. assignment.left.SetRelatedRhs(right); (* store a reference to the RHS in the assignement's LHS*)
  6814. left := ResolveDesignator(assignment.left);
  6815. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6816. (* error already handled *)
  6817. ELSIF (left IS SyntaxTree.ProcedureCallDesignator) & (left.type = NIL) & (left.relatedAsot # NIL) THEN
  6818. (* LHS is index write operator call on ASOT *)
  6819. procedureCallDesignator := left(SyntaxTree.ProcedureCallDesignator);
  6820. (* necessary ?
  6821. procedureType := procedureCallDesignator.left(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Procedure).type(SyntaxTree.ProcedureType);
  6822. type := procedureType.firstParameter.type;
  6823. expression := procedureCallDesignator.parameters.GetExpression(0);
  6824. procedureCallDesignator.parameters.SetExpression(0,NewConversion(0,expression,type,NIL));
  6825. *)
  6826. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6827. ELSIF CheckVariable(left) THEN
  6828. expression := NewOperatorCall(assignment.position, Scanner.Becomes, left, right, NIL);
  6829. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6830. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6831. (* conversion done by procedure call
  6832. (* try to convert to left argument *)
  6833. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6834. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6835. procedureCallDesignator.parameters.SetExpression(1, right);
  6836. END;
  6837. *)
  6838. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, procedureCallDesignator, assignment.outer);
  6839. ELSIF (expression # NIL) & (expression IS SyntaxTree.StatementDesignator) THEN
  6840. resolvedStatement := expression(SyntaxTree.StatementDesignator).statement;
  6841. ELSIF AssignmentCompatible(left, right) THEN
  6842. IF IsArrayStructuredObjectType(left.type) & (left.type.resolved # right.type.resolved) THEN
  6843. mathArrayType := MathArrayStructureOfType(left.type);
  6844. right := NewConversion(right.position, right, mathArrayType, NIL);
  6845. designator := NewIndexOperatorCall(InvalidPosition, left, ListOfOpenRanges(mathArrayType.Dimensionality()), right);
  6846. resolvedStatement := SyntaxTree.NewProcedureCallStatement(assignment.position, designator, assignment.outer)
  6847. ELSE
  6848. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6849. assignment.SetLeft(left);
  6850. assignment.SetRight(right);
  6851. resolvedStatement := assignment
  6852. END
  6853. END
  6854. END
  6855. END VisitAssignment;
  6856. (** check and resolve assignment LHS := RHS
  6857. - resolve LHS and RHS
  6858. - check if assignment operator is found. if yes, return operator call instead of assignment instruction
  6859. - check if assignment is compatible
  6860. - check if LHS is variable (i.e. assignable)
  6861. - convert RHS if necessary
  6862. - for the following two cases, return index write operator call on ASOT instead of assignment instruction:
  6863. - assignment between different ASOTs
  6864. asot := asot2; -> asot^."[]"( *, *, ..., *, asot2);
  6865. - assignment to ASOT elements:
  6866. asot[indexList] := rhs; -> asot^."[]"(indexList, rhs);
  6867. **)
  6868. PROCEDURE VisitCommunicationStatement(communication: SyntaxTree.CommunicationStatement);
  6869. VAR
  6870. left: SyntaxTree.Designator;
  6871. right: SyntaxTree.Expression;
  6872. inPort, outPort: SyntaxTree.PortType;
  6873. expression: SyntaxTree.Expression;
  6874. procedureCallDesignator: SyntaxTree.ProcedureCallDesignator;
  6875. BEGIN
  6876. right := ResolveExpression(communication.right);
  6877. left := ResolveDesignator(communication.left);
  6878. communication.SetLeft(left);
  6879. communication.SetRight(right);
  6880. expression := NewOperatorCall(communication.position, communication.op, left, right, NIL);
  6881. IF (expression # NIL) & (expression IS SyntaxTree.ProcedureCallDesignator) THEN
  6882. procedureCallDesignator := expression(SyntaxTree.ProcedureCallDesignator);
  6883. (* conversion done by procedure call
  6884. (* try to convert to left argument *)
  6885. IF (left.type.resolved IS SyntaxTree.MathArrayType) & (right.type.resolved IS SyntaxTree.MathArrayType) & AssignmentCompatible(left, right) THEN
  6886. right := NewConversion(right.position, right, left.type.resolved, NIL);
  6887. procedureCallDesignator.parameters.SetExpression(1, right);
  6888. END;
  6889. *)
  6890. resolvedStatement := SyntaxTree.NewProcedureCallStatement(communication.position, procedureCallDesignator, communication.outer);
  6891. ELSE
  6892. IF ~cellsAreObjects THEN ImportModule(Global.NameChannelModule,communication.position) END;
  6893. IF (left = SyntaxTree.invalidDesignator) OR (right = SyntaxTree.invalidExpression) THEN
  6894. (* error already handled *)
  6895. ELSIF communication.op = Scanner.LessLess THEN (* left is dest *)
  6896. IF (left.type.resolved IS SyntaxTree.PortType) & CheckPortType(left, outPort) THEN (* send *)
  6897. IF outPort.direction # SyntaxTree.OutPort THEN
  6898. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6899. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6900. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6901. ELSE
  6902. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6903. communication.SetRight(right)
  6904. END;
  6905. ELSIF (right.type.resolved IS SyntaxTree.PortType) & CheckPortType(right, inPort) THEN (* receive *)
  6906. IF CheckVariable(left) THEN
  6907. IF inPort.direction # SyntaxTree.InPort THEN
  6908. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6909. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6910. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6911. END;
  6912. END;
  6913. ELSE
  6914. Error(communication.position, -1, "unsupported stream operation");
  6915. END;
  6916. ELSIF (communication.op = Scanner.ExclamationMark) & CheckPortType(left,outPort) THEN
  6917. IF outPort.direction # SyntaxTree.OutPort THEN
  6918. Error(left.position,Diagnostics.Invalid,"not an out-port")
  6919. ELSIF outPort.sizeInBits < system.SizeOf(right.type) THEN
  6920. Error(left.position,Diagnostics.Invalid,"incompatible to port type");
  6921. ELSE
  6922. right := NewConversion(communication.position,right,left.type.resolved,NIL);
  6923. communication.SetRight(right)
  6924. END;
  6925. ELSIF (communication.op = Scanner.Questionmark) & CheckPortType(left,inPort) THEN
  6926. IF CheckVariable(right) THEN
  6927. IF inPort.direction # SyntaxTree.InPort THEN
  6928. Error(left.position,Diagnostics.Invalid,"not an in-port")
  6929. ELSIF inPort.sizeInBits # system.SizeOf(right.type) THEN
  6930. Error(right.position,Diagnostics.Invalid,"incompatible to port type");
  6931. END;
  6932. END;
  6933. ELSE
  6934. Error(communication.position, -1, "unsupported operation");
  6935. END;
  6936. END;
  6937. END VisitCommunicationStatement;
  6938. (** check and resolve if/eslif part
  6939. - check condition
  6940. - check statement sequence
  6941. **)
  6942. PROCEDURE IfPart(ifPart: SyntaxTree.IfPart; VAR true: BOOLEAN);
  6943. VAR prevUnreachable, b: BOOLEAN;
  6944. BEGIN
  6945. prevUnreachable := currentIsUnreachable;
  6946. ifPart.SetCondition(ResolveCondition(ifPart.condition));
  6947. IF IsBooleanValue(ifPart.condition,b) THEN
  6948. IF b=FALSE THEN
  6949. currentIsUnreachable := TRUE
  6950. ELSIF b=TRUE THEN
  6951. true := TRUE
  6952. END;
  6953. END;
  6954. StatementSequence(ifPart.statements);
  6955. currentIsUnreachable := prevUnreachable;
  6956. END IfPart;
  6957. (** check and resolve if statement
  6958. - check if parts and else part statement sequence
  6959. **)
  6960. PROCEDURE VisitIfStatement(ifStatement: SyntaxTree.IfStatement);
  6961. VAR elsif: SyntaxTree.IfPart; i: LONGINT; ifPartTrue, prevUnreachable: BOOLEAN;
  6962. BEGIN
  6963. prevUnreachable := currentIsUnreachable;
  6964. ifPartTrue := FALSE;
  6965. IfPart(ifStatement.ifPart,ifPartTrue);
  6966. FOR i := 0 TO ifStatement.ElsifParts()-1 DO
  6967. elsif := ifStatement.GetElsifPart(i);
  6968. IfPart(elsif,ifPartTrue);
  6969. END;
  6970. IF ifStatement.elsePart # NIL THEN
  6971. IF ifPartTrue THEN
  6972. currentIsUnreachable := TRUE
  6973. END;
  6974. StatementSequence(ifStatement.elsePart)
  6975. END;
  6976. currentIsUnreachable := prevUnreachable;
  6977. END VisitIfStatement;
  6978. PROCEDURE WithPart(withPart: SyntaxTree.WithPart; VAR symbol: SyntaxTree.Symbol);
  6979. VAR variable: SyntaxTree.Designator;
  6980. type,variableType: SyntaxTree.Type;
  6981. withEntry: WithEntry;
  6982. BEGIN
  6983. variable := ResolveDesignator(withPart.variable);
  6984. variableType := variable.type.resolved;
  6985. withPart.SetVariable(variable);
  6986. type := ResolveType(withPart.type);
  6987. withPart.SetType(type);
  6988. WHILE variable IS SyntaxTree.TypeGuardDesignator DO
  6989. variable := variable(SyntaxTree.TypeGuardDesignator).left(SyntaxTree.Designator);
  6990. END;
  6991. IF (type.resolved = SyntaxTree.invalidType) OR (variableType = SyntaxTree.invalidType) THEN (* error already reported *)
  6992. ELSIF ~(type.resolved = variableType) & ~IsExtensibleDesignator(variable) THEN
  6993. Error(variable.position,Diagnostics.Invalid,"is not extensible designator");
  6994. ELSIF ~(variable IS SyntaxTree.SymbolDesignator) (* OR (variable(SyntaxTree.SymbolDesignator).left # NIL) needed ?? *) THEN
  6995. Error(variable.position,Diagnostics.Invalid,"is no local variable ");
  6996. IF VerboseErrorMessage THEN
  6997. Printout.Info("variable",variable)
  6998. END;
  6999. ELSIF ~IsTypeExtension(variableType, type.resolved) THEN
  7000. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no type extension of ");
  7001. IF VerboseErrorMessage THEN
  7002. Printout.Info("variable",variable);
  7003. Printout.Info("type",type);
  7004. END;
  7005. ELSIF ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  7006. & ~(variable(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) THEN
  7007. Error(variable.position,Diagnostics.Invalid,"withguarded symbol is no variable ");
  7008. IF VerboseErrorMessage THEN
  7009. Printout.Info("variable",variable);
  7010. Printout.Info("type",type);
  7011. END;
  7012. ELSIF (symbol # NIL) & (symbol # variable(SyntaxTree.SymbolDesignator).symbol) THEN
  7013. Error(variable.position,Diagnostics.Invalid,"invalid change of withguarded symbol");
  7014. ELSE
  7015. symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7016. NEW(withEntry);
  7017. withEntry.previous := withEntries;
  7018. withEntry.symbol := variable(SyntaxTree.SymbolDesignator).symbol;
  7019. withEntry.type := type;
  7020. withEntries := withEntry;
  7021. StatementSequence(withPart.statements);
  7022. withEntries := withEntries.previous;
  7023. END;
  7024. END WithPart;
  7025. (** check and resolve with statement WITH variable: type DO ... END;
  7026. - check type and variable
  7027. - check that variable type is type extension of type
  7028. - check that variable is a variable
  7029. - enter new with scope and enter guardedVariable with same name and reference to variable
  7030. - create if statement:
  7031. WITH variable: type DO ... END; --> IF ~(variable IS type) THEN HALT(withTrap) ELSE ... END;
  7032. **)
  7033. PROCEDURE VisitWithStatement(withStatement: SyntaxTree.WithStatement);
  7034. VAR i: LONGINT; prevScope: SyntaxTree.Scope; symbol: SyntaxTree.Symbol;
  7035. BEGIN
  7036. prevScope := currentScope; symbol := NIL;
  7037. FOR i := 0 TO withStatement.WithParts()-1 DO
  7038. WithPart(withStatement.GetWithPart(i),symbol);
  7039. END;
  7040. IF withStatement.elsePart # NIL THEN
  7041. StatementSequence(withStatement.elsePart)
  7042. END;
  7043. currentScope := prevScope;
  7044. END VisitWithStatement;
  7045. (** check and resolve case part <<a, b, c..d: StatementSequence>>
  7046. - check expression to be constant or case range expression <<first .. last>> with constants 'first' and 'last' and compatible to type
  7047. - check 'first' < 'last' and no overlaps between different case labels
  7048. - check statement sequence
  7049. **)
  7050. PROCEDURE CasePart(casePart: SyntaxTree.CasePart; type: SyntaxTree.Type; VAR allcases: SyntaxTree.CaseConstant; VAR min,max: LONGINT);
  7051. VAR
  7052. i: LONGINT;
  7053. position: LONGINT;
  7054. expression, left, right: SyntaxTree.Expression;
  7055. expressionType: SyntaxTree.Type;
  7056. l, r: LONGINT;
  7057. cl, cr: CHAR;
  7058. thiscases: SyntaxTree.CaseConstant;
  7059. BEGIN
  7060. thiscases := NIL;
  7061. FOR i := 0 TO casePart.elements.Length() - 1 DO
  7062. expression := casePart.elements.GetExpression(i);
  7063. position := expression.position;
  7064. (* set context of range *)
  7065. IF expression IS SyntaxTree.RangeExpression THEN
  7066. expression(SyntaxTree.RangeExpression).SetContext(SyntaxTree.CaseGuard)
  7067. END;
  7068. expression := ResolveExpression(expression);
  7069. IF expression = SyntaxTree.invalidExpression THEN
  7070. (* error already reported *)
  7071. expressionType := SyntaxTree.invalidType;
  7072. ELSIF (expression IS SyntaxTree.RangeExpression) THEN
  7073. (* read out 'first' and 'last' *)
  7074. left := expression(SyntaxTree.RangeExpression).first;
  7075. right := expression(SyntaxTree.RangeExpression).last;
  7076. (* guaranteed by VisitRangeExpression: *)
  7077. ASSERT((left # NIL) & (right # NIL));
  7078. ASSERT(left.type.resolved = right.type.resolved);
  7079. left := CompatibleConversion(left.position, left, type);
  7080. right := CompatibleConversion(right.position, right, type);
  7081. expression(SyntaxTree.RangeExpression).SetFirst(left);
  7082. expression(SyntaxTree.RangeExpression).SetLast(right);
  7083. expressionType := RegularType(position,left.type);
  7084. ELSE
  7085. expression := ConstantExpression(expression);
  7086. expression := CompatibleConversion(expression.position, expression, type);
  7087. (*
  7088. IF IsStringType(expression.type) (*& IsCharacterValue(expression,cl) *) THEN
  7089. left := Global.NewCharacterValue(system,expression.position,cl);
  7090. expression := casePart.elements.GetExpression(i);
  7091. expression.SetResolved(left(SyntaxTree.CharacterValue));
  7092. expression := left
  7093. END;
  7094. *)
  7095. casePart.elements.SetExpression(i,expression);
  7096. left := expression; right := expression;
  7097. expressionType := RegularType(position,expression.type)
  7098. END;
  7099. IF (expressionType = SyntaxTree.invalidType) THEN
  7100. ELSIF ~CompatibleTo(system,expressionType,type) THEN
  7101. Error(position,Diagnostics.Invalid,"inadmissible case label");
  7102. expression := SyntaxTree.invalidExpression;
  7103. ELSE
  7104. l := 0; r := 0;
  7105. IF IsIntegerValue(left,l) & CheckIntegerValue(right,r) THEN
  7106. ELSIF IsCharacterValue(left,cl) & CheckCharacterValue(right,cr) THEN
  7107. l := ORD(cl); r := ORD(cr);
  7108. ELSIF IsEnumerationValue(left,l) & CheckEnumerationValue(right,r) THEN
  7109. ELSE
  7110. expression := SyntaxTree.invalidExpression
  7111. END;
  7112. IF expression # SyntaxTree.invalidExpression THEN
  7113. IF l>r THEN
  7114. Error(position,Diagnostics.Invalid,"empty case label")
  7115. ELSIF ~EnterCase(thiscases,l,r) OR ~EnterCase(allcases,l,r) THEN
  7116. Error(position,Diagnostics.Invalid,"duplicate case label");
  7117. ELSE
  7118. IF l < min THEN min := l END;
  7119. IF r > max THEN max := r END;
  7120. END;
  7121. END;
  7122. END;
  7123. casePart.elements.SetExpression(i,expression);
  7124. END;
  7125. (*! Coalesce(caseConstants); sort and find succeeeding numbers !!! *)
  7126. casePart.SetConstants(thiscases);
  7127. StatementSequence(casePart.statements);
  7128. END CasePart;
  7129. (** check and resolve case statement CASE variable OF ... END;
  7130. - check variable
  7131. - check case parts
  7132. **)
  7133. PROCEDURE VisitCaseStatement(caseStatement: SyntaxTree.CaseStatement);
  7134. VAR expression: SyntaxTree.Expression; i: LONGINT; type: SyntaxTree.Type; caseList: SyntaxTree.CaseConstant;
  7135. ch: CHAR; l: LONGINT; min,max: LONGINT; msg: ARRAY 64 OF CHAR;
  7136. BEGIN
  7137. expression := ResolveExpression(caseStatement.variable);
  7138. type := RegularType(expression.position,expression.type);
  7139. IF type = SyntaxTree.invalidType THEN
  7140. expression := SyntaxTree.invalidExpression;
  7141. ELSIF IsIntegerType(type) THEN
  7142. ELSIF IsStringType(expression.type) (* & IsCharacterValue(expression,ch) *) THEN
  7143. expression := NewConversion(expression.position, expression, system.characterType,NIL);
  7144. (*
  7145. expression := Global.NewCharacterValue(system,expression.position,ch);
  7146. *)
  7147. type := expression.type;
  7148. ELSIF IsCharacterType(type) THEN
  7149. ELSIF IsEnumerationType(type) THEN
  7150. ELSE
  7151. Error(caseStatement.variable.position,Diagnostics.Invalid,"variable must be integer or character type");
  7152. expression := SyntaxTree.invalidExpression;
  7153. END;
  7154. caseStatement.SetVariable(expression);
  7155. caseList := NIL;
  7156. min := MAX(LONGINT); max := MIN(LONGINT);
  7157. FOR i := 0 TO caseStatement.CaseParts()-1 DO
  7158. CasePart(caseStatement.GetCasePart(i),type,caseList,min,max);
  7159. END;
  7160. IF (max - min > 1024) & (100* caseStatement.CaseParts() DIV (max-min) < 10) (* less than ten percent used in a huge case table *) THEN
  7161. msg := "huge sparse case table ";
  7162. Strings.AppendInt(msg, max-min);
  7163. Strings.Append(msg,"/");
  7164. Strings.AppendInt(msg, caseStatement.CaseParts());
  7165. Warning(caseStatement.position,msg);
  7166. END;
  7167. caseStatement.SetMinMax(min,max);
  7168. StatementSequence(caseStatement.elsePart);
  7169. IF expression.resolved # NIL THEN
  7170. IF IsCharacterValue(expression,ch) THEN
  7171. l := ORD(ch)
  7172. ELSIF IsIntegerValue(expression,l) THEN
  7173. END;
  7174. IF EnterCase(caseList,l,l) & (caseStatement.elsePart = NIL) THEN Error(caseStatement.position,Diagnostics.Invalid,"no matching case label") END;
  7175. END;
  7176. END VisitCaseStatement;
  7177. (** check and resolve while statement
  7178. - check condition
  7179. - check statement sequence
  7180. **)
  7181. PROCEDURE VisitWhileStatement(whileStatement: SyntaxTree.WhileStatement);
  7182. VAR prevIsUnreachable,b: BOOLEAN;
  7183. BEGIN
  7184. prevIsUnreachable := currentIsUnreachable;
  7185. whileStatement.SetCondition(ResolveCondition(whileStatement.condition));
  7186. IF IsBooleanValue(whileStatement.condition,b) THEN
  7187. IF b=FALSE THEN
  7188. currentIsUnreachable := TRUE
  7189. END;
  7190. END;
  7191. StatementSequence(whileStatement.statements);
  7192. currentIsUnreachable := prevIsUnreachable
  7193. END VisitWhileStatement;
  7194. (** check and resolve repeat statement
  7195. - check condition
  7196. - check statement sequence
  7197. **)
  7198. PROCEDURE VisitRepeatStatement(repeatStatement: SyntaxTree.RepeatStatement);
  7199. BEGIN
  7200. repeatStatement.SetCondition(ResolveCondition(repeatStatement.condition));
  7201. StatementSequence(repeatStatement.statements);
  7202. END VisitRepeatStatement;
  7203. PROCEDURE GetGuard(symbol: SyntaxTree.Symbol; VAR type: SyntaxTree.Type): BOOLEAN;
  7204. VAR withEntry: WithEntry;
  7205. BEGIN
  7206. withEntry := withEntries;
  7207. WHILE (withEntry # NIL) & (withEntry.symbol # symbol) DO
  7208. withEntry := withEntry.previous
  7209. END;
  7210. IF withEntry = NIL THEN RETURN FALSE
  7211. ELSE
  7212. type := withEntry.type;
  7213. RETURN TRUE
  7214. END;
  7215. END GetGuard;
  7216. (** check and resolve for statement FOR variable := from TO to BY by DO StatementSequence END;
  7217. - check that variable is an integer variable
  7218. - check that from is integer typed with compatible type
  7219. - check that to has compatible type
  7220. - check that by is constant integer with compatible type
  7221. **)
  7222. PROCEDURE VisitForStatement(forStatement: SyntaxTree.ForStatement);
  7223. VAR expression: SyntaxTree.Expression; designator: SyntaxTree.Designator; type: SyntaxTree.Type;
  7224. BEGIN
  7225. designator := ResolveDesignator(forStatement.variable);
  7226. type := SyntaxTree.invalidType;
  7227. IF designator.type = SyntaxTree.invalidType THEN (* error already handled *)
  7228. designator := SyntaxTree.invalidDesignator;
  7229. ELSIF ~IsIntegerType(designator.type.resolved) THEN
  7230. Error(designator.position,Diagnostics.Invalid,"control variable of non-integer type");
  7231. designator := SyntaxTree.invalidDesignator;
  7232. ELSIF CheckVariable(designator) THEN
  7233. type := designator.type;
  7234. END;
  7235. forStatement.SetVariable(designator);
  7236. expression := ResolveExpression(forStatement.from);
  7237. IF expression = SyntaxTree.invalidExpression THEN
  7238. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7239. Error(expression.position,Diagnostics.Invalid,"start value of incompatible type");
  7240. expression := SyntaxTree.invalidExpression;
  7241. ELSIF type # SyntaxTree.invalidType THEN
  7242. expression := NewConversion(expression.position,expression,type,NIL)
  7243. END;
  7244. forStatement.SetFrom(expression);
  7245. expression := ResolveExpression(forStatement.to);
  7246. IF expression = SyntaxTree.invalidExpression THEN
  7247. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7248. Error(expression.position,Diagnostics.Invalid,"end value of incompatible type");
  7249. expression := SyntaxTree.invalidExpression;
  7250. ELSIF type # SyntaxTree.invalidType THEN
  7251. expression := NewConversion(expression.position,expression,type,NIL)
  7252. END;
  7253. forStatement.SetTo(expression);
  7254. IF forStatement.by # NIL THEN
  7255. expression := ConstantInteger(forStatement.by);
  7256. ELSE
  7257. expression := Global.NewIntegerValue(system,InvalidPosition,1);
  7258. END;
  7259. IF expression = SyntaxTree.invalidExpression THEN
  7260. ELSIF ~CompatibleTo(system,expression.type.resolved,designator.type.resolved) THEN
  7261. Error(expression.position,Diagnostics.Invalid,"step value of incompatible type");
  7262. ELSIF (expression.resolved(SyntaxTree.IntegerValue).hvalue = 0) THEN
  7263. Error(expression.position,Diagnostics.Invalid,"invalid step value");
  7264. ELSIF type # SyntaxTree.invalidType THEN
  7265. expression := NewConversion(expression.position,expression,type,NIL)
  7266. END;
  7267. forStatement.SetBy(expression);
  7268. StatementSequence(forStatement.statements);
  7269. END VisitForStatement;
  7270. (** check and resolve loop statement LOOP StatementSequence END
  7271. - check statement sequence
  7272. **)
  7273. PROCEDURE VisitLoopStatement(loopStatement: SyntaxTree.LoopStatement);
  7274. BEGIN
  7275. StatementSequence(loopStatement.statements)
  7276. END VisitLoopStatement;
  7277. PROCEDURE VisitExitableBlock(exitableBlock: SyntaxTree.ExitableBlock);
  7278. BEGIN
  7279. StatementSequence(exitableBlock.statements);
  7280. END VisitExitableBlock;
  7281. (** check and resolve exit statement EXIT
  7282. - check that exit is within LOOP statement block
  7283. **)
  7284. PROCEDURE VisitExitStatement(exitStatement: SyntaxTree.ExitStatement);
  7285. VAR outer: SyntaxTree.Statement;
  7286. BEGIN
  7287. outer := exitStatement.outer;
  7288. WHILE(outer # NIL) & ~(outer IS SyntaxTree.ExitableBlock) DO
  7289. outer := outer.outer;
  7290. END;
  7291. IF outer = NIL THEN
  7292. Error(exitStatement.position,Diagnostics.Invalid,"exit statement not within loop statement");
  7293. END;
  7294. END VisitExitStatement;
  7295. (** check and resolve return statement RETURN [expression]
  7296. - check expression (if any)
  7297. - check if in procedure scope
  7298. - if in procedure scope then check expression compatibility
  7299. - if not in procecdure scope then check on return without expression
  7300. **)
  7301. PROCEDURE VisitReturnStatement(returnStatement: SyntaxTree.ReturnStatement);
  7302. VAR expression: SyntaxTree.Expression; position: LONGINT; procedure: SyntaxTree.Procedure;
  7303. returnType: SyntaxTree.Type; outer: SyntaxTree.Statement; scope: SyntaxTree.Scope;
  7304. BEGIN
  7305. position := returnStatement.position;
  7306. expression := returnStatement.returnValue;
  7307. IF expression # NIL THEN
  7308. expression := ResolveExpression(expression);
  7309. returnStatement.SetReturnValue(expression);
  7310. END;
  7311. outer := returnStatement.outer;
  7312. WHILE(outer # NIL) & ~(outer IS SyntaxTree.Body) DO
  7313. outer := outer.outer
  7314. END;
  7315. IF (outer # NIL) THEN
  7316. scope := outer(SyntaxTree.Body).inScope;
  7317. IF ~(scope IS SyntaxTree.ProcedureScope) THEN
  7318. IF (expression # NIL) THEN
  7319. Error(position,Diagnostics.Invalid,"return statement with parameter not in procedure scope");
  7320. END;
  7321. ELSE
  7322. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7323. IF procedure.type(SyntaxTree.ProcedureType).noReturn THEN
  7324. Error(position,Diagnostics.Invalid,"return statement in procedure that does not return");
  7325. END;
  7326. returnType := procedure.type(SyntaxTree.ProcedureType).returnType;
  7327. IF returnType # NIL THEN
  7328. returnType := returnType.resolved;
  7329. IF expression = NIL THEN
  7330. Error(position,Diagnostics.Invalid,"empty return type in procedure providing a return type")
  7331. ELSIF expression.type = NIL THEN
  7332. Error(position, Diagnostics.Invalid,"returned type incompatible: expression has no type");
  7333. ELSIF ~CompatibleTo(system,expression.type.resolved,returnType) THEN
  7334. Error(position,Diagnostics.Invalid,"return type not compatible");
  7335. IF VerboseErrorMessage THEN
  7336. Printout.Info("returnType",returnType);
  7337. Printout.Info("expression",expression);
  7338. END;
  7339. ELSE
  7340. expression := NewConversion(expression.position,expression,returnType,NIL);
  7341. returnStatement.SetReturnValue(expression);
  7342. END;
  7343. ELSIF expression # NIL THEN
  7344. Error(position,Diagnostics.Invalid,"non-empty return type in procedure providing no return type");
  7345. END;
  7346. END;
  7347. END;
  7348. END VisitReturnStatement;
  7349. (** check and resolve await statement AWAIT(condition: Expression)
  7350. - check await condition
  7351. **)
  7352. PROCEDURE VisitAwaitStatement(awaitStatement: SyntaxTree.AwaitStatement);
  7353. VAR condition: SyntaxTree.Expression;
  7354. BEGIN
  7355. condition := ResolveCondition(awaitStatement.condition);
  7356. IF currentIsRealtime THEN
  7357. Error(awaitStatement.position,Diagnostics.Invalid,"forbidden await statement in realtime block");
  7358. END;
  7359. IF (condition.resolved # NIL) & (condition.resolved IS SyntaxTree.BooleanValue) THEN
  7360. Error(awaitStatement.position,Diagnostics.Invalid,"senseless await statement with constant condition");
  7361. END;
  7362. awaitStatement.SetCondition(condition);
  7363. END VisitAwaitStatement;
  7364. PROCEDURE CheckSystemImport(position: LONGINT);
  7365. VAR import: SyntaxTree.Import;
  7366. BEGIN
  7367. import := currentScope.ownerModule.moduleScope.firstImport;
  7368. WHILE(import # NIL) DO
  7369. IF (import.module.name = Global.SystemName) OR (import.module.name = Global.systemName) THEN
  7370. RETURN;
  7371. END;
  7372. import := import.nextImport;
  7373. END;
  7374. Error(position, Diagnostics.Invalid, "forbidden code without system import");
  7375. END CheckSystemImport;
  7376. (** check and resolve code statement: do nothing, must be done by assembler
  7377. **)
  7378. PROCEDURE VisitCode(code: SyntaxTree.Code);
  7379. VAR i: LONGINT; statement: SyntaxTree.Statement;
  7380. BEGIN
  7381. CheckSystemImport(code.position);
  7382. FOR i := 0 TO code.inRules.Length()-1 DO
  7383. statement := code.inRules.GetStatement(i);
  7384. IF statement IS SyntaxTree.Assignment THEN
  7385. WITH statement: SyntaxTree.Assignment DO
  7386. statement.SetRight(ResolveExpression(statement.right));
  7387. END;
  7388. ELSE
  7389. Error(statement.position, Diagnostics.Invalid, "can only be assignment")
  7390. END;
  7391. END;
  7392. FOR i := 0 TO code.outRules.Length()-1 DO
  7393. statement := code.outRules.GetStatement(i);
  7394. IF statement IS SyntaxTree.Assignment THEN
  7395. WITH statement: SyntaxTree.Assignment DO
  7396. statement.SetLeft(ResolveDesignator(statement.left));
  7397. END;
  7398. ELSIF statement IS SyntaxTree.ReturnStatement THEN
  7399. ELSIF statement IS SyntaxTree.StatementBlock THEN
  7400. ELSE
  7401. Printout.Info("out statement ", statement);
  7402. Error(statement.position, Diagnostics.Invalid, "(out) can only be assignment")
  7403. END;
  7404. END;
  7405. END VisitCode;
  7406. (** check and set flags of a statement block
  7407. - check for multiply occurence of a flag
  7408. - check and set priority only in bodies
  7409. - check for valid names
  7410. **)
  7411. PROCEDURE BlockFlags(block: SyntaxTree.StatementBlock);
  7412. VAR blockModifier: SyntaxTree.Modifier; expression: SyntaxTree.Expression; name: SyntaxTree.Identifier; flags: SET; position: LONGINT;
  7413. flag: LONGINT; recordBody: SyntaxTree.Body;
  7414. PROCEDURE SetProtectedRecord;
  7415. VAR scope: SyntaxTree.Scope;
  7416. BEGIN
  7417. scope := currentScope;
  7418. WHILE (scope # NIL) & ~(scope IS SyntaxTree.RecordScope) DO
  7419. scope := scope.outerScope
  7420. END;
  7421. IF scope # NIL THEN
  7422. scope(SyntaxTree.RecordScope).ownerRecord.SetProtected(TRUE);
  7423. END;
  7424. END SetProtectedRecord;
  7425. BEGIN
  7426. flags := {};
  7427. IF (block IS SyntaxTree.Body) & (currentIsBodyProcedure) & ((currentScope.outerScope = NIL) OR ~(currentScope.outerScope IS SyntaxTree.ModuleScope)) THEN
  7428. recordBody := block(SyntaxTree.Body)
  7429. ELSE
  7430. recordBody := NIL
  7431. END;
  7432. blockModifier := block.blockModifiers;
  7433. WHILE(blockModifier # NIL) DO
  7434. name := blockModifier.identifier;
  7435. expression := blockModifier.expression;
  7436. position := blockModifier.position;
  7437. flag := -1;
  7438. IF name=Global.NamePriority THEN
  7439. IF expression = NIL THEN
  7440. Error(position,Diagnostics.Invalid,"missing priority expression");
  7441. ELSIF recordBody = NIL THEN
  7442. Error(position,Diagnostics.Invalid,"priority not on record body");
  7443. ELSIF recordBody.priority # NIL THEN
  7444. Error(position,Diagnostics.Invalid,"duplicate priority expression");
  7445. ELSE
  7446. recordBody.SetPriority(expression);
  7447. END;
  7448. ELSIF expression # NIL THEN
  7449. Error(expression.position,Diagnostics.Invalid,"expression not in connection with priority")
  7450. ELSIF name=Global.NameExclusive THEN
  7451. IF block.isExclusive THEN
  7452. Error(position,Diagnostics.Invalid,"duplicate exclusive flag")
  7453. END;
  7454. block.SetExclusive(TRUE); SetProtectedRecord;
  7455. ELSIF name=Global.NameActive THEN
  7456. IF recordBody = NIL THEN
  7457. Error(position,Diagnostics.Invalid,"active not in record body");
  7458. ELSIF recordBody.isActive THEN
  7459. Error(position,Diagnostics.Invalid,"duplicate active flag")
  7460. ELSE
  7461. recordBody.SetActive(TRUE); SetProtectedRecord;
  7462. END;
  7463. ELSIF name=Global.NameSafe THEN
  7464. IF recordBody = NIL THEN
  7465. Error(position,Diagnostics.Invalid,"safe not in record body");
  7466. ELSIF recordBody.isSafe THEN
  7467. Error(position,Diagnostics.Invalid,"duplicate safe flag")
  7468. ELSE
  7469. recordBody.SetSafe(TRUE);
  7470. SetProtectedRecord;
  7471. END;
  7472. ELSIF name=Global.NameRealtime THEN
  7473. IF recordBody = NIL THEN
  7474. Error(position,Diagnostics.Invalid,"realtime not in record body");
  7475. ELSIF recordBody.isRealtime THEN
  7476. Error(position,Diagnostics.Invalid,"duplicate realtime flag")
  7477. ELSE
  7478. recordBody.SetRealtime(TRUE);
  7479. block.SetRealtime(TRUE);
  7480. END;
  7481. ELSIF name=Global.NameUnchecked THEN
  7482. IF block.isUnchecked THEN
  7483. Error(position,Diagnostics.Invalid,"duplicate unchecked flag")
  7484. ELSE
  7485. block.SetUnchecked(TRUE);
  7486. END;
  7487. ELSIF (name=Global.NameUncooperative) THEN
  7488. IF block.isUncooperative THEN
  7489. Error(position,Diagnostics.Invalid,"duplicate uncooperative flag")
  7490. ELSE
  7491. block.SetUncooperative(TRUE);
  7492. END;
  7493. ELSE
  7494. Error(position,Diagnostics.Invalid,"unknown block modifier");
  7495. END;
  7496. blockModifier := blockModifier.nextModifier;
  7497. END;
  7498. END BlockFlags;
  7499. (** check and resolve statement block
  7500. - check flags (exclusive)
  7501. - check statement sequence
  7502. **)
  7503. PROCEDURE VisitStatementBlock(statementBlock: SyntaxTree.StatementBlock);
  7504. VAR recentExclusive, recentUnreachable, recentRealtime: BOOLEAN;
  7505. BEGIN
  7506. BlockFlags(statementBlock);
  7507. IF statementBlock.isExclusive THEN
  7508. (* check that not in exclusive block *)
  7509. IF currentIsExclusive THEN
  7510. Error (statementBlock.position,Diagnostics.Invalid,"forbidden recursive exclusive")
  7511. ELSIF currentIsRealtime THEN
  7512. Error( statementBlock.position,Diagnostics.Invalid,"forbidden exculsive in realtime block");
  7513. END;
  7514. END;
  7515. recentExclusive := currentIsExclusive;
  7516. recentUnreachable := currentIsUnreachable;
  7517. recentRealtime := currentIsRealtime;
  7518. IF statementBlock.isExclusive THEN currentIsExclusive := TRUE END;
  7519. IF statementBlock.isUnreachable THEN currentIsUnreachable := TRUE END;
  7520. IF statementBlock.isRealtime THEN currentIsRealtime := TRUE END;
  7521. StatementSequence(statementBlock.statements);
  7522. currentIsRealtime := recentRealtime;
  7523. currentIsExclusive := recentExclusive;
  7524. currentIsUnreachable := recentUnreachable;
  7525. END VisitStatementBlock;
  7526. (** check and resolve body
  7527. - check flags (active, priority, safe)
  7528. - check body and finally part
  7529. **)
  7530. PROCEDURE Body(body: SyntaxTree.Body);
  7531. BEGIN
  7532. VisitStatementBlock(body);
  7533. IF body.isActive THEN
  7534. IF ~currentIsBodyProcedure THEN
  7535. Error(body.position,Diagnostics.Invalid,"active flag not in object body");
  7536. ELSIF body.priority # NIL THEN
  7537. body.SetPriority(ConstantInteger(body.priority));
  7538. END;
  7539. ELSIF body.isSafe THEN
  7540. Error(body.position,Diagnostics.Invalid,"safe flag not in active body");
  7541. ELSIF body.priority # NIL THEN
  7542. Error(body.position,Diagnostics.Invalid,"priority flag not in active body");
  7543. END;
  7544. IF body.code # NIL THEN
  7545. CheckSystemImport(body.position);
  7546. END;
  7547. StatementSequence(body.finally)
  7548. END Body;
  7549. (*** scopes ***)
  7550. (** Register a symbol in a scope. Check for duplicate symbols and collision with globally defined symbols. **)
  7551. PROCEDURE Register(symbol: SyntaxTree.Symbol; scope: SyntaxTree.Scope; allowDuplicate: BOOLEAN);
  7552. VAR duplicateSymbol: BOOLEAN;
  7553. BEGIN
  7554. ASSERT(symbol.name # SyntaxTree.invalidIdentifier);
  7555. IF ~allowDuplicate & (global.FindSymbol(symbol.name)#NIL) THEN
  7556. Error(symbol.position,Diagnostics.Invalid,"globally defined keyword")
  7557. END;
  7558. scope.EnterSymbol(symbol,duplicateSymbol);
  7559. IF ~allowDuplicate & duplicateSymbol THEN
  7560. Error(symbol.position,Basic.MultiplyDefinedIdentifier,"");
  7561. IF VerboseErrorMessage THEN
  7562. Printout.Info("multiply defined identifier",symbol);
  7563. Printout.Info("in scope",scope);
  7564. END;
  7565. END;
  7566. END Register;
  7567. (**
  7568. implementation: check and resolve an implementation part
  7569. **)
  7570. (*! can in principle be done in parallel on different checkers: implementations do only depend on declarations)
  7571. move implementation checker to a separate object ? *)
  7572. PROCEDURE Implementation(scope: SyntaxTree.Scope);
  7573. VAR prevScope: SyntaxTree.Scope; procedure: SyntaxTree.Procedure; prevIsRealtime, prevIsBodyProcedure, prevIsCellNet: BOOLEAN;
  7574. BEGIN
  7575. prevIsRealtime := currentIsRealtime;
  7576. prevIsBodyProcedure := currentIsBodyProcedure;
  7577. prevIsCellNet := currentIsCellNet;
  7578. prevScope := currentScope;
  7579. currentScope := scope;
  7580. IF (scope IS SyntaxTree.ProcedureScope) THEN
  7581. procedure := scope(SyntaxTree.ProcedureScope).ownerProcedure;
  7582. currentIsBodyProcedure := currentIsBodyProcedure OR procedure.isBodyProcedure;
  7583. currentIsRealtime := currentIsRealtime OR procedure.type.isRealtime;
  7584. currentIsCellNet := InCellNetScope(procedure.scope) OR cellsAreObjects;
  7585. (*
  7586. IF procedure.isInline & ((scope(SyntaxTree.ProcedureScope).body = NIL) OR (scope(SyntaxTree.ProcedureScope).body # NIL) & (scope(SyntaxTree.ProcedureScope).body.code = NIL)) THEN
  7587. Warning(procedure.position,"unsupported inline procedure - must be assembler code")
  7588. END;
  7589. *)
  7590. END;
  7591. IF (scope IS SyntaxTree.ProcedureScope) & (scope(SyntaxTree.ProcedureScope).body # NIL) (* & ~(scope IS SyntaxTree.RecordScope) *) THEN
  7592. (* module body, record bodies are wrapped into an artifical procedure *)
  7593. IF (phase = InlinePhase) & (IsOberonInline(procedure)) THEN
  7594. Body(scope(SyntaxTree.ProcedureScope).body)
  7595. ELSIF (phase = ImplementationPhase) & ~IsOberonInline(procedure) THEN
  7596. Body(scope(SyntaxTree.ProcedureScope).body)
  7597. END;
  7598. END;
  7599. currentScope := prevScope;
  7600. currentIsRealtime := prevIsRealtime;
  7601. currentIsBodyProcedure := prevIsBodyProcedure;
  7602. currentIsCellNet := prevIsCellNet;
  7603. END Implementation;
  7604. (** implementation phase:
  7605. check and resolve all scopes (implementation phase) that have been entered into a list during the declaration phase
  7606. **)
  7607. PROCEDURE Implementations(x: SyntaxTree.Module);
  7608. VAR scope: SyntaxTree.Scope; prevPhase: LONGINT;
  7609. BEGIN
  7610. prevPhase := phase;
  7611. phase := InlinePhase;
  7612. scope := x.firstScope;
  7613. WHILE(scope # NIL) DO
  7614. Implementation(scope);
  7615. scope := scope.nextScope;
  7616. END;
  7617. phase := ImplementationPhase;
  7618. scope := x.firstScope;
  7619. WHILE(scope # NIL) DO
  7620. Implementation(scope);
  7621. scope := scope.nextScope;
  7622. END;
  7623. phase := prevPhase;
  7624. END Implementations;
  7625. (** declaration phase:
  7626. check and resolve all declarations of a scope (module scope, procedure scope, record scope):
  7627. - import lists (for module scopes)
  7628. - parameter list (for procedure scopes)
  7629. - constant declarations
  7630. - type declarations
  7631. - variable declarations
  7632. - procedure declarations
  7633. preformed in two stages:
  7634. - first all symbols are entered into the symbol table (with uniqueness check),
  7635. - then all symbols are resolved
  7636. after declaration check, bodies are entered into the global list of implementations that remain to be resolved after all declarations.
  7637. Declarations depend on other declarations, this procedure is neither thread safe not would it be wise to try concurrency here
  7638. **)
  7639. PROCEDURE Declarations(scope: SyntaxTree.Scope; skipImplementation: BOOLEAN);
  7640. VAR
  7641. constant: SyntaxTree.Constant;
  7642. typeDeclaration: SyntaxTree.TypeDeclaration;
  7643. variable: SyntaxTree.Variable;
  7644. procedure: SyntaxTree.Procedure;
  7645. prevScope: SyntaxTree.Scope;
  7646. parameter: SyntaxTree.Parameter;
  7647. import: SyntaxTree.Import;
  7648. symbol: SyntaxTree.Symbol;
  7649. prevPhase: LONGINT;
  7650. prevError : BOOLEAN;
  7651. PROCEDURE DeclareCell(type: SyntaxTree.CellType);
  7652. VAR baseType: SyntaxTree.Type; property, prop: SyntaxTree.Property; variable: SyntaxTree.Variable;
  7653. BEGIN
  7654. IF type.baseType # NIL THEN
  7655. baseType := type.baseType.resolved;
  7656. IF baseType IS SyntaxTree.PointerType THEN
  7657. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  7658. END;
  7659. IF baseType IS SyntaxTree.CellType THEN
  7660. DeclareCell(baseType(SyntaxTree.CellType));
  7661. END;
  7662. END;
  7663. parameter := type.firstParameter;
  7664. WHILE(parameter # NIL) DO (* duplicates forbidden *)
  7665. variable := SyntaxTree.NewVariable(parameter.position, parameter.name);
  7666. variable.SetType(parameter.type);
  7667. variable.SetAccess(SyntaxTree.Hidden);
  7668. variable.SetModifiers(parameter.modifiers);
  7669. currentScope.PushVariable(variable);
  7670. (*
  7671. Register(parameter,scope, FALSE);
  7672. *)
  7673. parameter := parameter.nextParameter;
  7674. END;
  7675. property := type.firstProperty;
  7676. WHILE (property # NIL) DO (* duplicates allowed : overwrite *)
  7677. variable := currentScope.FindVariable(property.name);
  7678. IF (variable # NIL) & (variable IS SyntaxTree.Property) THEN (* overwrite *)
  7679. prop := variable(SyntaxTree.Property);
  7680. ELSE (* add, duplicate symbols detection later *)
  7681. prop := SyntaxTree.NewProperty(property.position, property.name);
  7682. currentScope.PushVariable(prop);
  7683. END;
  7684. prop.SetType(property.type);
  7685. prop.SetValue(property.value);
  7686. prop.SetAccess(SyntaxTree.Hidden);
  7687. property := property.nextProperty;
  7688. END;
  7689. END DeclareCell;
  7690. BEGIN
  7691. prevError := error;
  7692. prevPhase := phase;
  7693. phase := DeclarationPhase;
  7694. prevScope := currentScope;
  7695. currentScope := scope;
  7696. error := FALSE;
  7697. (* first enter all symbols in scope *)
  7698. IF scope IS SyntaxTree.ModuleScope THEN
  7699. (* treat imports first for a module scope, , set default context if necessary *)
  7700. import := scope(SyntaxTree.ModuleScope).firstImport;
  7701. WHILE(import # NIL) DO
  7702. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7703. Register(import, currentScope, FALSE);
  7704. import := import.nextImport;
  7705. END;
  7706. import := scope(SyntaxTree.ModuleScope).firstImport;
  7707. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7708. ResolveSymbol(import);
  7709. import := import.nextImport;
  7710. END;
  7711. ELSIF scope IS SyntaxTree.ProcedureScope THEN
  7712. (* enter parameters for a procedure scope *)
  7713. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).firstParameter;
  7714. WHILE(parameter # NIL) DO
  7715. Register(parameter,currentScope, FALSE); parameter := parameter.nextParameter;
  7716. END;
  7717. parameter := scope(SyntaxTree.ProcedureScope).ownerProcedure.type.resolved(SyntaxTree.ProcedureType).returnParameter;
  7718. IF parameter # NIL THEN Register(parameter, currentScope, FALSE); END;
  7719. ELSIF scope IS SyntaxTree.CellScope THEN
  7720. IF~skipImplementation THEN
  7721. import := scope(SyntaxTree.CellScope).firstImport;
  7722. WHILE(import # NIL) DO
  7723. IF import.context = SyntaxTree.invalidIdentifier THEN import.SetContext(scope.ownerModule.context) END;
  7724. Register(import, currentScope, FALSE);
  7725. import := import.nextImport;
  7726. END;
  7727. import := scope(SyntaxTree.CellScope).firstImport;
  7728. WHILE(import # NIL) DO (* 2nd stage to avoid duplicate symbol *)
  7729. ResolveSymbol(import);
  7730. import := import.nextImport;
  7731. END;
  7732. END;
  7733. DeclareCell(scope(SyntaxTree.CellScope).ownerCell);
  7734. END;
  7735. IF error THEN RETURN END;
  7736. (* constants *)
  7737. constant := scope.firstConstant;
  7738. WHILE (constant # NIL) DO
  7739. Register(constant, currentScope, FALSE); constant := constant.nextConstant;
  7740. END;
  7741. (* type declarations *)
  7742. typeDeclaration := scope.firstTypeDeclaration;
  7743. WHILE (typeDeclaration # NIL) DO
  7744. Register(typeDeclaration, currentScope, FALSE); typeDeclaration := typeDeclaration.nextTypeDeclaration;
  7745. END;
  7746. (* variables *)
  7747. variable := scope.firstVariable;
  7748. WHILE (variable # NIL) DO
  7749. Register(variable, currentScope, FALSE); variable := variable.nextVariable;
  7750. END;
  7751. (* procedures *)
  7752. procedure := scope.firstProcedure;
  7753. WHILE (procedure # NIL) DO
  7754. Register(procedure, currentScope, procedure IS SyntaxTree.Operator); procedure := procedure.nextProcedure;
  7755. END;
  7756. IF ~skipImplementation THEN
  7757. (* now process all symbols without any presumption on the order *)
  7758. symbol := scope.firstSymbol;
  7759. WHILE(symbol # NIL) DO
  7760. IF ~(symbol IS SyntaxTree.Parameter) OR (symbol(SyntaxTree.Parameter).ownerType IS SyntaxTree.CellType) THEN
  7761. ResolveSymbol(symbol);
  7762. END;
  7763. symbol := symbol.nextSymbol;
  7764. END;
  7765. IF (scope IS SyntaxTree.ProcedureScope) & scope(SyntaxTree.ProcedureScope).ownerProcedure.type.isRealtime THEN
  7766. symbol := scope.firstSymbol;
  7767. WHILE symbol # NIL DO
  7768. IF (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) THEN
  7769. IF (symbol.type IS SyntaxTree.PointerType) OR (symbol.type IS SyntaxTree.QualifiedType) THEN
  7770. pointerFixes.Add(symbol, currentScope);
  7771. END;
  7772. IF ~symbol.type.resolved.isRealtime THEN
  7773. Error(symbol.position,Diagnostics.Invalid,"symbol has no realtime type");
  7774. END;
  7775. END;
  7776. symbol := symbol.nextSymbol
  7777. END;
  7778. END;
  7779. IF ~error & ~system.GenerateVariableOffsets(scope) THEN
  7780. Error(Diagnostics.Invalid,Diagnostics.Invalid,"problems during offset computation in module");
  7781. END;
  7782. IF (scope.ownerModule # NIL) THEN
  7783. (* add scope to global list of all scopes, very handy for code generation and for checking implementations *)
  7784. scope.ownerModule.AddScope(scope);
  7785. END;
  7786. END;
  7787. phase := prevPhase;
  7788. currentScope := prevScope;
  7789. error := error OR prevError;
  7790. END Declarations;
  7791. (* nopov *)
  7792. (** check if all operators from one module are compatible to the ones in the other module
  7793. - check if there are not multiple operators with the same signature
  7794. (apart from the conversion operator "@Convert": it is the only operator that may be defined multiple times with the same signature)
  7795. - check for all operators whose signatures are compatible, whether the return types are compatible
  7796. note that:
  7797. - the return type is not considered to be part of the signature
  7798. - two signatures are considered compatible, if all of the operands are compatible
  7799. **)
  7800. PROCEDURE CheckInterOperatorConformity(thisModuleScope, thatModuleScope: SyntaxTree.ModuleScope);
  7801. VAR
  7802. thisOperator, thatOperator: SyntaxTree.Operator;
  7803. thisProcedureType, thatProcedureType: SyntaxTree.ProcedureType;
  7804. thisParameter, thatParameter: SyntaxTree.Parameter;
  7805. operandsAreEqual, operandsAreCompatible, hasError: BOOLEAN;
  7806. i: LONGINT;
  7807. BEGIN
  7808. currentScope := thisModuleScope;
  7809. hasError := FALSE;
  7810. (* go through all operators in the other module *)
  7811. thatOperator := thatModuleScope.firstOperator;
  7812. WHILE (thatOperator # NIL) & ~hasError DO
  7813. IF (thisModuleScope = thatModuleScope) OR (SyntaxTree.PublicRead IN thatOperator.access) THEN
  7814. (* the other operator is accessible *)
  7815. IF thatOperator.name # Global.GetIdentifier(Global.Conversion, thatModuleScope.ownerModule.case) THEN
  7816. (* the other operator is not the conversion operator *)
  7817. (* go through all operators in this module *)
  7818. thisOperator := thisModuleScope.firstOperator;
  7819. WHILE (thisOperator # NIL) & ~hasError DO
  7820. IF thisOperator # thatOperator THEN
  7821. (* the operators are not the same *)
  7822. IF thisOperator.name = thatOperator.name THEN
  7823. (* the operators share the same identifier *)
  7824. ASSERT(thisOperator.type IS SyntaxTree.ProcedureType);
  7825. ASSERT(thatOperator.type IS SyntaxTree.ProcedureType);
  7826. thisProcedureType := thisOperator.type(SyntaxTree.ProcedureType);
  7827. thatProcedureType := thatOperator.type(SyntaxTree.ProcedureType);
  7828. IF thisProcedureType.numberParameters = thatProcedureType.numberParameters THEN
  7829. (* both operators have the same paramter count *)
  7830. thisParameter := thisProcedureType.firstParameter;
  7831. thatParameter := thatProcedureType.firstParameter;
  7832. operandsAreEqual := TRUE;
  7833. operandsAreCompatible := TRUE;
  7834. (* go through all parameters *)
  7835. FOR i := 1 TO thisProcedureType.numberParameters DO
  7836. ASSERT(thatParameter # NIL);
  7837. IF ~SameType(thisParameter.type, thatParameter.type) THEN
  7838. operandsAreEqual := FALSE;
  7839. IF ~CompatibleTo(system, thisParameter.type, thatParameter.type) THEN
  7840. operandsAreCompatible := FALSE
  7841. END
  7842. END;
  7843. thisParameter := thisParameter.nextParameter;
  7844. thatParameter := thatParameter.nextParameter
  7845. END;
  7846. IF operandsAreEqual THEN
  7847. Error(thisOperator.position, Diagnostics.Invalid, "operator has the same identifier and operand types as other one");
  7848. hasError := TRUE
  7849. ELSIF operandsAreCompatible THEN
  7850. IF ~CompatibleTo(system, thisProcedureType.returnType, thatProcedureType.returnType) THEN
  7851. Error(thisOperator.position, Diagnostics.Invalid, "operator's return type is not compatible to the one of a more generic operator");
  7852. hasError := TRUE
  7853. ELSIF ~thisOperator.isDynamic & thatOperator.isDynamic THEN
  7854. Error(thisOperator.position, Diagnostics.Invalid, "operator must be dynamic because it is signature-compatible to a dynamic one");
  7855. hasError := TRUE
  7856. END
  7857. END
  7858. END
  7859. END
  7860. END;
  7861. thisOperator := thisOperator.nextOperator
  7862. END
  7863. END
  7864. END;
  7865. thatOperator := thatOperator.nextOperator
  7866. END
  7867. END CheckInterOperatorConformity;
  7868. (** check module:
  7869. - check module declaration
  7870. - add context, if necessary
  7871. - remove module from import cache, if necessary
  7872. - check declarations
  7873. - resolve all type fixes
  7874. - check implementation (bodies)
  7875. **)
  7876. PROCEDURE Module*(x: SyntaxTree.Module);
  7877. VAR (* nopov *)
  7878. import: SyntaxTree.Import; modifier: SyntaxTree.Modifier; value,position: LONGINT; prevIsCellNet: BOOLEAN; prevScope: SyntaxTree.Scope;
  7879. BEGIN
  7880. prevScope := currentScope;
  7881. prevIsCellNet := currentIsCellNet;
  7882. module := x;
  7883. ASSERT(x # NIL);
  7884. global := system.globalScope[x.case];
  7885. x.moduleScope.SetGlobalScope(global);
  7886. currentScope := global;
  7887. IF (x.name = Global.SystemName) OR (x.name = Global.systemName) THEN Error(x.position,Diagnostics.Invalid,"name reserved") END;
  7888. IF x.context = SyntaxTree.invalidIdentifier THEN x.SetContext(Global.A2Name) END;
  7889. RemoveModuleFromCache(importCache,x);
  7890. Declarations(x.moduleScope, FALSE);
  7891. FixTypes();
  7892. IF module.isCellNet THEN
  7893. currentIsCellNet := TRUE;
  7894. modifier := x.modifiers;
  7895. IF HasValue(modifier,Global.NameFrequencyDivider,position,value) THEN END;
  7896. CheckModifiers(modifier, FALSE);
  7897. END;
  7898. (* nopov *)
  7899. IF ~error THEN
  7900. (* check if operators conform to each other within this module *)
  7901. CheckInterOperatorConformity(x.moduleScope, x.moduleScope);
  7902. (* go through all imports *)
  7903. import := x.moduleScope.firstImport;
  7904. WHILE import # NIL DO
  7905. IF (import.module # NIL) & ~Global.IsSystemModule(import.module) THEN (* ignore SYSTEM-module *)
  7906. (* check if all operators in this module conform to the ones of the imported module *)
  7907. CheckInterOperatorConformity(x.moduleScope, import.module.moduleScope)
  7908. END;
  7909. import := import.nextImport
  7910. END;
  7911. END;
  7912. Implementations(x);
  7913. module := NIL;
  7914. currentIsCellNet := prevIsCellNet;
  7915. currentScope := prevScope;
  7916. END Module;
  7917. END Checker;
  7918. Warnings*=OBJECT (SyntaxTree.Visitor)
  7919. VAR diagnostics: Diagnostics.Diagnostics; module: SyntaxTree.Module;
  7920. PROCEDURE &InitWarnings*(diagnostics: Diagnostics.Diagnostics);
  7921. BEGIN
  7922. SELF.diagnostics := diagnostics
  7923. END InitWarnings;
  7924. (** types *)
  7925. PROCEDURE Type(x: SyntaxTree.Type);
  7926. BEGIN x.Accept(SELF)
  7927. END Type;
  7928. PROCEDURE VisitType*(x: SyntaxTree.Type);
  7929. BEGIN END VisitType;
  7930. PROCEDURE VisitBasicType*(x: SyntaxTree.BasicType);
  7931. BEGIN END VisitBasicType;
  7932. PROCEDURE VisitCharacterType*(x: SyntaxTree.CharacterType);
  7933. BEGIN END VisitCharacterType;
  7934. PROCEDURE VisitIntegerType*(x: SyntaxTree.IntegerType);
  7935. BEGIN END VisitIntegerType;
  7936. PROCEDURE VisitFloatType*(x: SyntaxTree.FloatType);
  7937. BEGIN END VisitFloatType;
  7938. PROCEDURE VisitQualifiedType*(x: SyntaxTree.QualifiedType);
  7939. BEGIN END VisitQualifiedType;
  7940. PROCEDURE VisitStringType*(x: SyntaxTree.StringType);
  7941. BEGIN END VisitStringType;
  7942. PROCEDURE VisitEnumerationType*(x: SyntaxTree.EnumerationType);
  7943. BEGIN END VisitEnumerationType;
  7944. PROCEDURE VisitRangeType*(x: SyntaxTree.RangeType);
  7945. BEGIN END VisitRangeType;
  7946. PROCEDURE VisitArrayType*(x: SyntaxTree.ArrayType);
  7947. BEGIN
  7948. IF ~(SyntaxTree.Warned IN x.state) THEN
  7949. x.SetState(SyntaxTree.Warned);
  7950. Type(x.arrayBase);
  7951. END;
  7952. END VisitArrayType;
  7953. PROCEDURE VisitMathArrayType*(x: SyntaxTree.MathArrayType);
  7954. BEGIN
  7955. IF ~(SyntaxTree.Warned IN x.state) THEN
  7956. x.SetState(SyntaxTree.Warned);
  7957. Type(x.arrayBase);
  7958. END;
  7959. END VisitMathArrayType;
  7960. PROCEDURE VisitPointerType*(x: SyntaxTree.PointerType);
  7961. BEGIN
  7962. IF ~(SyntaxTree.Warned IN x.state) THEN
  7963. x.SetState(SyntaxTree.Warned);
  7964. Type(x.pointerBase);
  7965. END;
  7966. END VisitPointerType;
  7967. PROCEDURE VisitRecordType*(x: SyntaxTree.RecordType);
  7968. BEGIN Scope(x.recordScope) END VisitRecordType;
  7969. PROCEDURE VisitCellType*(x: SyntaxTree.CellType);
  7970. BEGIN Scope(x.cellScope) END VisitCellType;
  7971. PROCEDURE VisitProcedureType*(x: SyntaxTree.ProcedureType);
  7972. BEGIN END VisitProcedureType;
  7973. PROCEDURE Warning(x: SyntaxTree.Symbol; CONST text: ARRAY OF CHAR);
  7974. VAR msg: ARRAY 256 OF CHAR;
  7975. BEGIN
  7976. Global.GetSymbolName(x,msg);
  7977. Strings.Append(msg," ");
  7978. Strings.Append(msg,text);
  7979. diagnostics.Warning(module.sourceName,x.position,Diagnostics.Invalid,msg);
  7980. END Warning;
  7981. (** symbols *)
  7982. PROCEDURE Symbol(x: SyntaxTree.Symbol);
  7983. BEGIN
  7984. IF ~x.used & (x.access * SyntaxTree.Public = {}) & (x.access # SyntaxTree.Hidden) THEN
  7985. IF ~(x IS SyntaxTree.Parameter) THEN
  7986. Warning(x,"never used");
  7987. END;
  7988. END;
  7989. x.Accept(SELF);
  7990. END Symbol;
  7991. PROCEDURE VisitSymbol*(x: SyntaxTree.Symbol);
  7992. BEGIN END VisitSymbol;
  7993. PROCEDURE VisitTypeDeclaration*(x: SyntaxTree.TypeDeclaration);
  7994. BEGIN Type(x.declaredType) END VisitTypeDeclaration;
  7995. PROCEDURE VisitConstant*(x: SyntaxTree.Constant);
  7996. BEGIN END VisitConstant;
  7997. PROCEDURE VisitVariable*(x: SyntaxTree.Variable);
  7998. BEGIN END VisitVariable;
  7999. PROCEDURE VisitProperty*(x: SyntaxTree.Property);
  8000. BEGIN END VisitProperty;
  8001. PROCEDURE VisitParameter*(x: SyntaxTree.Parameter);
  8002. BEGIN END VisitParameter;
  8003. PROCEDURE VisitProcedure*(x: SyntaxTree.Procedure);
  8004. BEGIN Scope(x.procedureScope) END VisitProcedure;
  8005. PROCEDURE VisitOperator*(x: SyntaxTree.Operator);
  8006. BEGIN END VisitOperator;
  8007. PROCEDURE VisitImport*(x: SyntaxTree.Import);
  8008. BEGIN END VisitImport;
  8009. PROCEDURE Scope(scope: SyntaxTree.Scope);
  8010. VAR
  8011. symbol: SyntaxTree.Symbol;
  8012. BEGIN
  8013. symbol := scope.firstSymbol;
  8014. WHILE(symbol # NIL) DO
  8015. Symbol(symbol);
  8016. symbol := symbol.nextSymbol;
  8017. END;
  8018. END Scope;
  8019. PROCEDURE Module*(x: SyntaxTree.Module);
  8020. BEGIN
  8021. SELF.module := x;
  8022. Scope(x.moduleScope);
  8023. END Module;
  8024. END Warnings;
  8025. PROCEDURE IsOberonInline(procedure: SyntaxTree.Procedure): BOOLEAN;
  8026. BEGIN
  8027. RETURN procedure.isInline & ((procedure.procedureScope.body = NIL) OR (procedure.procedureScope.body # NIL) & (procedure.procedureScope.body.code = NIL))
  8028. END IsOberonInline;
  8029. PROCEDURE Resolved(x: SyntaxTree.Type): SyntaxTree.Type;
  8030. BEGIN
  8031. IF x = NIL THEN RETURN NIL ELSE RETURN x.resolved END;
  8032. END Resolved;
  8033. PROCEDURE PowerOf2(x: LONGINT): BOOLEAN;
  8034. VAR i: LONGINT;
  8035. BEGIN
  8036. i := 1;
  8037. WHILE i < x DO
  8038. i := i *2
  8039. END;
  8040. RETURN i=x
  8041. END PowerOf2;
  8042. PROCEDURE IsCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8043. BEGIN
  8044. RETURN
  8045. (scope # NIL) &
  8046. (scope IS SyntaxTree.ModuleScope)
  8047. & (scope(SyntaxTree.ModuleScope).ownerModule.isCellNet)
  8048. OR
  8049. (scope # NIL) & (scope IS SyntaxTree.CellScope)
  8050. & (scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8051. END IsCellNetScope;
  8052. PROCEDURE IsCellScope(scope: SyntaxTree.Scope): BOOLEAN;
  8053. BEGIN
  8054. RETURN (scope # NIL) & (scope IS SyntaxTree.CellScope) & ~(scope(SyntaxTree.CellScope).ownerCell.isCellNet)
  8055. END IsCellScope;
  8056. PROCEDURE InCellNetScope(scope: SyntaxTree.Scope): BOOLEAN;
  8057. BEGIN
  8058. WHILE (scope # NIL) & ~IsCellScope(scope) & ~IsCellNetScope(scope) DO scope := scope.outerScope END;
  8059. RETURN (scope # NIL) & IsCellNetScope(scope)
  8060. END InCellNetScope;
  8061. PROCEDURE ToMemoryUnits(system: Global.System; size: LONGINT): LONGINT;
  8062. BEGIN
  8063. ASSERT(size MOD system.dataUnit = 0);
  8064. RETURN size DIV system.dataUnit
  8065. END ToMemoryUnits;
  8066. (* Returns TRUE if the built-in function GETPROCEDURE can be used with this procedure type *)
  8067. PROCEDURE GetProcedureAllowed*(type: SyntaxTree.Type) : BOOLEAN;
  8068. VAR procedureType: SyntaxTree.ProcedureType; numberParameters: LONGINT;
  8069. PROCEDURE TypeAllowed(t : SyntaxTree.Type) : BOOLEAN;
  8070. BEGIN
  8071. IF t = NIL THEN
  8072. RETURN TRUE
  8073. ELSE
  8074. t := t.resolved;
  8075. RETURN (t IS SyntaxTree.RecordType) OR IsPointerToRecord(t) OR (t IS SyntaxTree.AnyType);
  8076. END;
  8077. END TypeAllowed;
  8078. BEGIN
  8079. type := type.resolved;
  8080. IF ~(type IS SyntaxTree.ProcedureType) THEN
  8081. RETURN FALSE
  8082. ELSE
  8083. procedureType := type(SyntaxTree.ProcedureType);
  8084. numberParameters := procedureType.numberParameters;
  8085. RETURN
  8086. (numberParameters = 0) & TypeAllowed(procedureType.returnType) OR
  8087. (numberParameters = 1) & TypeAllowed(procedureType.firstParameter.type) & TypeAllowed(procedureType.returnType) OR
  8088. (numberParameters = 1) & (procedureType.firstParameter.ownerType.resolved IS SyntaxTree.AnyType) & (procedureType.returnType.resolved IS SyntaxTree.AnyType)
  8089. END;
  8090. END GetProcedureAllowed;
  8091. (** check import cache: if module x is in current import cache then remove x and all modules importing x from the cache **)
  8092. PROCEDURE RemoveModuleFromCache*(importCache: SyntaxTree.ModuleScope; x: SyntaxTree.Module);
  8093. VAR import: SyntaxTree.Import;
  8094. BEGIN
  8095. import := importCache.ImportByModuleName(x.name,x.context);
  8096. IF import # NIL THEN
  8097. importCache.RemoveImporters(x.name,x.context);
  8098. END;
  8099. END RemoveModuleFromCache;
  8100. PROCEDURE CompatibleTo(system: Global.System; this,to: SyntaxTree.Type): BOOLEAN;
  8101. (* to <- this assignment compatibility *)
  8102. VAR result: BOOLEAN;
  8103. BEGIN
  8104. IF this= NIL THEN result := (to=NIL)
  8105. ELSIF to=NIL THEN result := FALSE
  8106. ELSE
  8107. (*! will be replaced by this:
  8108. ELSE result := this.CompatibleTo(to.resolved);
  8109. *)
  8110. this := this.resolved; to := to.resolved;
  8111. IF to=SyntaxTree.invalidType THEN result := FALSE
  8112. ELSIF to=SyntaxTree.typeDeclarationType THEN result := FALSE;
  8113. ELSIF to = this THEN
  8114. result := ~(to IS SyntaxTree.ArrayType) OR (to(SyntaxTree.ArrayType).form # SyntaxTree.Open);
  8115. ELSIF to IS SyntaxTree.BasicType THEN
  8116. IF (to IS SyntaxTree.NumberType) & (this IS SyntaxTree.NumberType) THEN
  8117. IF (to IS SyntaxTree.ComplexType) OR (this IS SyntaxTree.ComplexType) THEN
  8118. result := this.CompatibleTo(to.resolved)
  8119. ELSE
  8120. result := Global.BasicTypeDistance(system,this(SyntaxTree.BasicType),to(SyntaxTree.BasicType)) < Infinity;
  8121. END
  8122. ELSIF (to IS SyntaxTree.SetType) & (this IS SyntaxTree.SetType) THEN
  8123. result := to.sizeInBits = this.sizeInBits;
  8124. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.AddressType) THEN
  8125. result := to.sizeInBits >= this.sizeInBits; (* weak compatibility: (unsigned) address may be assigned to signed integer of same (or greater) size *)
  8126. ELSIF (to IS SyntaxTree.IntegerType) & (this IS SyntaxTree.SizeType) THEN
  8127. result := to.sizeInBits >= this.sizeInBits; (* compatibility: (signed) size may be assigned to signed integer of greater or equal size *)
  8128. ELSIF (to IS SyntaxTree.FloatType) & (this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.SizeType) THEN
  8129. result := TRUE;
  8130. ELSIF to IS SyntaxTree.AnyType THEN
  8131. 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);
  8132. ELSIF to IS SyntaxTree.ObjectType THEN
  8133. 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 *) ;
  8134. ELSIF to IS SyntaxTree.ByteType THEN
  8135. result := (this IS SyntaxTree.IntegerType) & (to.sizeInBits = 8) OR IsCharacterType(this)
  8136. ELSIF to IS SyntaxTree.CharacterType THEN
  8137. result := IsCharacterType(this)
  8138. ELSIF (to IS SyntaxTree.SizeType) & ((this IS SyntaxTree.SizeType) OR (this IS SyntaxTree.IntegerType) OR (this IS SyntaxTree.AddressType)) THEN
  8139. result := to.sizeInBits >= this.sizeInBits (*! weak compatibility: signed size type may be assigned with unsigned address type of same size *)
  8140. 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
  8141. result := to.sizeInBits >= this.sizeInBits; (*! weak compatibility: addresses may be assigned with signed integer *)
  8142. ELSIF (to IS SyntaxTree.RangeType) & (this IS SyntaxTree.RangeType) THEN
  8143. result := TRUE;
  8144. ELSIF (to IS SyntaxTree.BooleanType) & (this IS SyntaxTree.BooleanType) THEN
  8145. result := TRUE;
  8146. ELSE
  8147. result := FALSE
  8148. END;
  8149. ELSIF to IS SyntaxTree.PointerType THEN
  8150. result := (this IS SyntaxTree.NilType) OR ((this IS SyntaxTree.AddressType) OR (this IS SyntaxTree.IntegerType)) & to(SyntaxTree.PointerType).isUnsafe OR
  8151. IsPointerType(this) & (IsTypeExtension(to,this) OR to(SyntaxTree.PointerType).isUnsafe OR ((to(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) & SameType(to,this)))
  8152. & (~to.isRealtime OR this.isRealtime);
  8153. ELSIF to IS SyntaxTree.ProcedureType THEN
  8154. result := (this IS SyntaxTree.NilType) OR (this IS SyntaxTree.ProcedureType) & SameType(to(SyntaxTree.ProcedureType),this(SyntaxTree.ProcedureType))
  8155. & (~(this(SyntaxTree.ProcedureType).isDelegate) OR (to(SyntaxTree.ProcedureType).isDelegate))
  8156. & (~to.isRealtime OR this.isRealtime)
  8157. & ((this(SyntaxTree.ProcedureType).stackAlignment <=1) OR (this(SyntaxTree.ProcedureType).stackAlignment <= to(SyntaxTree.ProcedureType).stackAlignment));
  8158. ELSIF (to IS SyntaxTree.RecordType) & to(SyntaxTree.RecordType).isObject THEN
  8159. result := (this IS SyntaxTree.NilType) OR IsTypeExtension(to,this);
  8160. ELSIF to IS SyntaxTree.RecordType THEN
  8161. result := (this IS SyntaxTree.RecordType) & IsTypeExtension(to,this);
  8162. ELSIF to IS SyntaxTree.ArrayType THEN
  8163. IF IsStringType(to) & (this IS SyntaxTree.StringType) THEN
  8164. result := (to(SyntaxTree.ArrayType).form = SyntaxTree.Open) OR (to(SyntaxTree.ArrayType).staticLength >= this(SyntaxTree.StringType).length)
  8165. ELSIF StaticArrayCompatible(to, this) THEN
  8166. result := TRUE
  8167. ELSE
  8168. result := (to(SyntaxTree.ArrayType).staticLength # 0) & SameType(to,this)
  8169. END;
  8170. ELSIF to IS SyntaxTree.MathArrayType THEN
  8171. IF this IS SyntaxTree.MathArrayType THEN
  8172. IF to(SyntaxTree.MathArrayType).arrayBase= NIL THEN
  8173. IF to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor THEN
  8174. result := TRUE;
  8175. ELSIF this(SyntaxTree.MathArrayType).arrayBase = NIL THEN
  8176. result := TRUE;
  8177. ELSE
  8178. result := ~(this(SyntaxTree.MathArrayType).arrayBase.resolved IS SyntaxTree.MathArrayType);
  8179. END;
  8180. (* special case: ARRAY [...] OF SYSTEM.ALL *)
  8181. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) THEN
  8182. (* ARRAY [?] OF <- ARRAY [x,...,x] OF *)
  8183. result := CompatibleTo(system,ArrayBase(this,Infinity),ArrayBase(to,Infinity));
  8184. ELSIF (to(SyntaxTree.MathArrayType).form = SyntaxTree.Open) OR (this(SyntaxTree.MathArrayType).form = SyntaxTree.Open)
  8185. OR (to(SyntaxTree.MathArrayType).staticLength = this(SyntaxTree.MathArrayType).staticLength) THEN
  8186. (* ARRAY [x] OF <- ARRAY [x] OF *)
  8187. result := CompatibleTo(system,this(SyntaxTree.MathArrayType).arrayBase,to(SyntaxTree.MathArrayType).arrayBase);
  8188. ELSE
  8189. result := FALSE
  8190. END;
  8191. (* an array-structured object type is compatible to the type of its array structure *)
  8192. ELSIF IsArrayStructuredObjectType(this) THEN
  8193. result := CompatibleTo(system, to, MathArrayStructureOfType(this))
  8194. ELSE
  8195. result := FALSE;
  8196. END;
  8197. ELSIF to IS SyntaxTree.StringType THEN
  8198. result := FALSE;
  8199. ELSIF to IS SyntaxTree.EnumerationType THEN
  8200. result := IsEnumerationExtension(this,to);
  8201. ELSIF to IS SyntaxTree.PortType THEN
  8202. result := SameType(to, this)
  8203. ELSE
  8204. Printout.Info("CompatibleTo",to);
  8205. HALT(100); (* implement missing type check *)
  8206. END;
  8207. END;
  8208. RETURN result
  8209. END CompatibleTo;
  8210. PROCEDURE StaticArrayCompatible(formal: SyntaxTree.Type; actual: SyntaxTree.Type): BOOLEAN;
  8211. VAR actualBase, formalBase: SyntaxTree.Type;
  8212. BEGIN
  8213. IF SameType(formal,actual) THEN
  8214. RETURN TRUE
  8215. ELSIF (formal IS SyntaxTree.MathArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8216. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8217. formalBase := formal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8218. RETURN
  8219. (formal(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8220. & (actual(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8221. & (actual(SyntaxTree.ArrayType).staticLength = formal(SyntaxTree.MathArrayType).staticLength)
  8222. & StaticArrayCompatible(formalBase,actualBase)
  8223. ELSIF (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8224. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8225. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8226. RETURN
  8227. (formal(SyntaxTree.ArrayType).form = SyntaxTree.Static)
  8228. & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8229. & (actual(SyntaxTree.MathArrayType).staticLength = formal(SyntaxTree.ArrayType).staticLength)
  8230. & StaticArrayCompatible(formalBase,actualBase)
  8231. ELSE RETURN FALSE
  8232. END;
  8233. END StaticArrayCompatible;
  8234. PROCEDURE OpenArrayCompatible(formalType: SyntaxTree.ArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8235. VAR arrayBase: SyntaxTree.Type; result: BOOLEAN;
  8236. PROCEDURE TC(formal,actual: SyntaxTree.Type): BOOLEAN;
  8237. VAR actualBase,formalBase: SyntaxTree.Type; result: BOOLEAN;
  8238. BEGIN
  8239. result := SameType(formal,actual);
  8240. IF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.ArrayType) THEN
  8241. actualBase := actual(SyntaxTree.ArrayType).arrayBase.resolved;
  8242. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8243. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & TC(formalBase,actualBase)
  8244. ELSIF ~result & (formal IS SyntaxTree.ArrayType) & (actual IS SyntaxTree.MathArrayType) THEN
  8245. actualBase := actual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8246. formalBase := formal(SyntaxTree.ArrayType).arrayBase.resolved;
  8247. result := (formal(SyntaxTree.ArrayType).form = SyntaxTree.Open) & (actual(SyntaxTree.MathArrayType).form = SyntaxTree.Static)
  8248. & TC(formalBase, actualBase);
  8249. END;
  8250. RETURN result
  8251. END TC;
  8252. BEGIN
  8253. IF formalType.form # SyntaxTree.Open THEN result := FALSE
  8254. ELSE
  8255. arrayBase := formalType.arrayBase.resolved;
  8256. IF (actualType IS SyntaxTree.StringType) THEN
  8257. result := arrayBase IS SyntaxTree.CharacterType
  8258. ELSIF actualType IS SyntaxTree.ArrayType THEN
  8259. result := (arrayBase IS SyntaxTree.ByteType) OR TC(formalType,actualType)
  8260. ELSIF actualType IS SyntaxTree.MathArrayType THEN
  8261. result := TC(formalType, actualType);
  8262. ELSE
  8263. result := (arrayBase IS SyntaxTree.ByteType)
  8264. END;
  8265. END;
  8266. RETURN result
  8267. END OpenArrayCompatible;
  8268. PROCEDURE MathArrayCompatible(formalType: SyntaxTree.MathArrayType; actualType: SyntaxTree.Type): BOOLEAN;
  8269. (* special compatibility rule for parameters of the form VAR A: ARRAY [x] OF , VAR A: ARRAY [*] OF and VAR A: ARRAY [?] OF *)
  8270. VAR formalBase,actualBase: SyntaxTree.Type; result: BOOLEAN; actualArray: SyntaxTree.MathArrayType;
  8271. BEGIN
  8272. IF actualType IS SyntaxTree.MathArrayType THEN
  8273. actualArray := actualType(SyntaxTree.MathArrayType);
  8274. IF (formalType.form = SyntaxTree.Tensor) OR (actualArray.form = SyntaxTree.Tensor) THEN
  8275. (*
  8276. ARRAY [?] OF -> ARRAY [?|*|k] OF
  8277. ARRAY [?|*|k] OF -> ARRAY [?] OF
  8278. *)
  8279. actualBase := ArrayBase(actualType,Infinity);
  8280. formalBase := ArrayBase(formalType,Infinity);
  8281. result := (formalBase = NIL) OR SameType(formalBase,actualBase);
  8282. ELSE
  8283. (*
  8284. ARRAY [*|k] OF -> ARRAY [*|n] OF
  8285. *)
  8286. formalBase := Resolved(formalType.arrayBase);
  8287. actualBase := Resolved(actualArray.arrayBase);
  8288. IF (formalType.form = SyntaxTree.Static) & (actualArray.form = SyntaxTree.Static) THEN
  8289. (*
  8290. ARRAY [k] -> ARRAY [n]
  8291. *)
  8292. result := (formalType.staticLength = actualArray.staticLength)
  8293. ELSE
  8294. result := TRUE
  8295. END;
  8296. IF ~result THEN
  8297. ELSIF formalBase = NIL THEN result := (actualBase = NIL) OR ~(actualBase IS SyntaxTree.MathArrayType);
  8298. ELSIF actualBase = NIL THEN result := FALSE
  8299. ELSIF formalBase IS SyntaxTree.MathArrayType THEN
  8300. result := MathArrayCompatible(formalBase(SyntaxTree.MathArrayType),actualBase)
  8301. ELSE
  8302. result := SameType(formalBase,actualBase)
  8303. END;
  8304. END;
  8305. ELSE
  8306. result := FALSE
  8307. END;
  8308. RETURN result
  8309. END MathArrayCompatible;
  8310. (**
  8311. Math Array Type distance for assignments / parameter passings of the form
  8312. from -> to
  8313. variants:
  8314. ARRAY [num] | ARRAY [*] | ARRAY [?] -> ARRAY [num] | ARRAY[*] | ARRAY [?]
  8315. allowed:
  8316. static -> static (& size match)
  8317. static -> open
  8318. static -> tensor
  8319. open -> open
  8320. open -> tensor
  8321. open -> static
  8322. tensor -> tensor
  8323. tensor -> open
  8324. tensor -> static
  8325. **)
  8326. (*! think about the metric here: is form matching more important than element type matching? *)
  8327. PROCEDURE MathArrayTypeDistance(system: Global.System; from,to: SyntaxTree.MathArrayType; varpar:BOOLEAN): LONGINT;
  8328. VAR i: LONGINT; fromBase, toBase: SyntaxTree.Type;
  8329. BEGIN
  8330. fromBase := Resolved(from.arrayBase);
  8331. toBase := Resolved(to.arrayBase);
  8332. i := Infinity;
  8333. IF from = to THEN
  8334. i := 0;
  8335. ELSIF (from.form = to.form) THEN
  8336. (* static -> static, open -> open, tensor -> tensor *)
  8337. IF (from.form # SyntaxTree.Static) OR (from.staticLength = to.staticLength) THEN
  8338. IF fromBase = toBase THEN i := 0
  8339. ELSIF toBase = NIL THEN i := 1
  8340. ELSIF (fromBase IS SyntaxTree.MathArrayType) & (toBase IS SyntaxTree.MathArrayType) THEN
  8341. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8342. ELSE
  8343. i := TypeDistance(system,fromBase, toBase, varpar);
  8344. END;
  8345. END;
  8346. ELSIF (to.form = SyntaxTree.Static) THEN
  8347. (* forbidden *)
  8348. ELSIF (from.form = SyntaxTree.Tensor) OR (to.form = SyntaxTree.Tensor) THEN
  8349. (* static -> tensor, open -> tensor, tensor -> open *)
  8350. IF toBase=fromBase THEN i := 0;
  8351. ELSIF toBase = NIL THEN i := 1;
  8352. ELSIF (toBase IS SyntaxTree.MathArrayType) THEN
  8353. toBase := ArrayBase(toBase,Infinity);
  8354. IF (fromBase=toBase) THEN i := 0
  8355. ELSIF (toBase = NIL) THEN i:= 1
  8356. ELSIF (fromBase = NIL) THEN i := Infinity;
  8357. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8358. END;
  8359. ELSIF (fromBase IS SyntaxTree.MathArrayType) THEN
  8360. fromBase := ArrayBase(fromBase,Infinity);
  8361. IF (fromBase=toBase) THEN i := 0
  8362. ELSIF (toBase = NIL) THEN i := 1
  8363. ELSIF (fromBase = NIL) THEN i := Infinity;
  8364. ELSE i := TypeDistance(system,fromBase,toBase,varpar);
  8365. END;
  8366. ELSE i := TypeDistance(system, fromBase, toBase, varpar);
  8367. END;
  8368. IF i # Infinity THEN INC(i,2) END;
  8369. ELSIF (from.form = SyntaxTree.Static) THEN
  8370. (* static -> open *)
  8371. IF toBase=fromBase THEN i := 0
  8372. ELSIF toBase = NIL THEN i := 1
  8373. ELSIF fromBase = NIL THEN i := Infinity
  8374. ELSIF (toBase IS SyntaxTree.MathArrayType) & (fromBase IS SyntaxTree.MathArrayType) THEN
  8375. i := MathArrayTypeDistance(system,fromBase(SyntaxTree.MathArrayType),toBase(SyntaxTree.MathArrayType),varpar);
  8376. ELSE i := TypeDistance(system,fromBase, toBase, varpar);
  8377. END;
  8378. IF i # Infinity THEN INC(i,1) END;
  8379. ELSE HALT(100); (* unknown case *)
  8380. END;
  8381. RETURN i;
  8382. END MathArrayTypeDistance;
  8383. (** compute and return the distance of two array types
  8384. - return the distance of the base types
  8385. **)
  8386. PROCEDURE ArrayTypeDistance(system: Global.System; from, to: SyntaxTree.ArrayType): LONGINT;
  8387. VAR i: LONGINT;
  8388. BEGIN
  8389. i := Infinity;
  8390. IF from = to THEN
  8391. i := 0
  8392. ELSE
  8393. i := TypeDistance(system,from.arrayBase.resolved, to.arrayBase.resolved,FALSE);
  8394. (*
  8395. ELSIF (from.mode = static) & (to.mode IN {open}) THEN
  8396. i := TypeDistance(from.base, to.base);
  8397. IF i >= 0 THEN INC(i) END
  8398. ELSIF (from.mode = open) & (to.mode = open) THEN
  8399. i := TypeDistance(from.base, to.base);
  8400. *)
  8401. END;
  8402. RETURN i
  8403. END ArrayTypeDistance;
  8404. (** compute the signature distance of a procedure and an actual parameter list
  8405. - if any of the parameters are not compatible, the result is infinite
  8406. - add up and return the distance over all parameters
  8407. **)
  8408. PROCEDURE Distance(system: Global.System; procedureType: SyntaxTree.ProcedureType; actualParameters: SyntaxTree.ExpressionList): LONGINT;
  8409. VAR result: LONGINT; formalParameter: SyntaxTree.Parameter; actualParameter: SyntaxTree.Expression;
  8410. distance: LONGINT; baseFormal,baseActual: SyntaxTree.Type; i: LONGINT;
  8411. BEGIN
  8412. IF actualParameters.Length() # (procedureType.numberParameters) THEN
  8413. result := Infinity
  8414. ELSE
  8415. formalParameter := procedureType.firstParameter;
  8416. i := 0;
  8417. result := 0;
  8418. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8419. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8420. actualParameter := actualParameters.GetExpression(i);
  8421. ASSERT(formalParameter.type # NIL);
  8422. IF (actualParameter.type = NIL) THEN distance := Infinity
  8423. ELSE
  8424. distance := TypeDistance(system,actualParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8425. END;
  8426. IF distance = Infinity THEN
  8427. result := Infinity;
  8428. ELSE
  8429. IF (formalParameter.kind = SyntaxTree.VarParameter) & (distance # 0) THEN
  8430. IF (formalParameter.type.resolved IS SyntaxTree.MathArrayType) & (actualParameter.type.resolved IS SyntaxTree.MathArrayType) THEN
  8431. (* already handled varpar *)
  8432. (*
  8433. baseActual := actualParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8434. baseFormal := formalParameter.type.resolved(SyntaxTree.MathArrayType).arrayBase.resolved;
  8435. WHILE(baseActual IS SyntaxTree.MathArrayType) & (baseFormal IS SyntaxTree.MathArrayType) DO
  8436. baseActual := baseActual(SyntaxTree.MathArrayType).arrayBase.resolved;
  8437. baseFormal := baseFormal(SyntaxTree.MathArrayType).arrayBase.resolved;
  8438. END;
  8439. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8440. result := Infinity
  8441. END;
  8442. *)
  8443. ELSIF (formalParameter.type.resolved IS SyntaxTree.ArrayType) & (actualParameter.type.resolved IS SyntaxTree.ArrayType) THEN
  8444. baseActual := actualParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8445. baseFormal := formalParameter.type.resolved(SyntaxTree.ArrayType).arrayBase.resolved;
  8446. WHILE(baseActual IS SyntaxTree.ArrayType) & (baseFormal IS SyntaxTree.ArrayType) DO
  8447. baseActual := baseActual(SyntaxTree.ArrayType).arrayBase.resolved;
  8448. baseFormal := baseFormal(SyntaxTree.ArrayType).arrayBase.resolved;
  8449. END;
  8450. IF TypeDistance(system,baseActual,baseFormal,FALSE) # 0 THEN
  8451. result := Infinity
  8452. END;
  8453. ELSE
  8454. result := Infinity
  8455. END;
  8456. ELSE
  8457. INC(result,distance);
  8458. END;
  8459. END;
  8460. formalParameter := formalParameter.nextParameter; INC(i);
  8461. END;
  8462. END;
  8463. ASSERT(result >= 0);
  8464. RETURN result
  8465. END Distance;
  8466. PROCEDURE ProcedureTypeDistance(system: Global.System; procedureType: SyntaxTree.ProcedureType; right: SyntaxTree.ProcedureType): LONGINT;
  8467. VAR result: LONGINT; formalParameter, rightParameter: SyntaxTree.Parameter; distance: LONGINT; i: LONGINT;
  8468. BEGIN
  8469. IF right.numberParameters # (procedureType.numberParameters) THEN
  8470. result := Infinity
  8471. ELSE
  8472. formalParameter := procedureType.firstParameter;
  8473. rightParameter := right.firstParameter;
  8474. i := 0;
  8475. result := 0;
  8476. (*! taken from paco, seems to not be 100% correct, check (in particular array part -> length of arrays??) *)
  8477. WHILE (formalParameter # NIL) & (result # Infinity) DO
  8478. distance := TypeDistance(system,rightParameter.type.resolved,formalParameter.type.resolved,formalParameter.kind = SyntaxTree.VarParameter);
  8479. IF distance = Infinity THEN
  8480. result := Infinity;
  8481. ELSE
  8482. INC(result,distance);
  8483. END;
  8484. formalParameter := formalParameter.nextParameter;
  8485. rightParameter := rightParameter.nextParameter;
  8486. END;
  8487. END;
  8488. ASSERT(result >= 0);
  8489. RETURN result
  8490. END ProcedureTypeDistance;
  8491. (** compute and return the distance between two types, used for computation of signature distance
  8492. from -> to
  8493. **)
  8494. PROCEDURE TypeDistance(system: Global.System; from, to: SyntaxTree.Type; varpar: BOOLEAN): LONGINT;
  8495. VAR i: LONGINT; ptr: SyntaxTree.PointerType;
  8496. BEGIN
  8497. IF IsArrayStructuredObjectType(from) & (to IS SyntaxTree.MathArrayType) THEN
  8498. RETURN TypeDistance(system, MathArrayStructureOfType(from), to, varpar) + 0; (* TODO: find better value?*)
  8499. END;
  8500. i := Infinity;
  8501. IF from = to THEN
  8502. i := 0
  8503. ELSIF (to = NIL) OR (from=NIL) THEN HALT(100); (* was: SYSTEM.ALL type, removed *)
  8504. ELSIF (from IS SyntaxTree.NilType) OR (to IS SyntaxTree.NilType) THEN
  8505. i := Infinity;
  8506. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.ByteType) THEN
  8507. i := 1
  8508. ELSIF (from IS SyntaxTree.StringType) THEN
  8509. IF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1 END
  8510. ELSIF (from IS SyntaxTree.CharacterType) THEN
  8511. IF (to IS SyntaxTree.CharacterType) & (to.sizeInBits = from.sizeInBits) THEN i := 0
  8512. ELSIF (to IS SyntaxTree.ArrayType) & (to(SyntaxTree.ArrayType).length = NIL) & (to(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType) THEN i := 1
  8513. ELSIF to IS SyntaxTree.ByteType THEN i := 1 END
  8514. ELSIF (from IS SyntaxTree.IntegerType) & (to IS SyntaxTree.ByteType) & (to.sizeInBits = from.sizeInBits) THEN
  8515. i := 1
  8516. ELSIF (from IS SyntaxTree.NilType) THEN
  8517. IF (to IS SyntaxTree.AnyType) OR (to IS SyntaxTree.ObjectType) OR (to IS SyntaxTree.PointerType) OR (to IS SyntaxTree.ProcedureType) THEN i := 1 END
  8518. (*
  8519. ELSIF (from = NoType) THEN
  8520. IF (to IS Delegate) THEN i := 1 END (*special case: procedure -> proctype, not resolved yet*)
  8521. *)
  8522. ELSIF (from IS SyntaxTree.BasicType) THEN
  8523. IF to IS SyntaxTree.BasicType THEN i := Global.BasicTypeDistance(system,from(SyntaxTree.BasicType), to(SyntaxTree.BasicType)) END;
  8524. IF varpar & (i # 0) THEN i := Infinity END;
  8525. ELSIF (from IS SyntaxTree.ArrayType) THEN
  8526. IF to IS SyntaxTree.ArrayType THEN i := ArrayTypeDistance(system,from(SyntaxTree.ArrayType), to(SyntaxTree.ArrayType)) END
  8527. ELSIF (from IS SyntaxTree.RecordType) THEN
  8528. IF to IS SyntaxTree.RecordType THEN i := RecordTypeDistance(from(SyntaxTree.RecordType), to (SyntaxTree.RecordType)) END
  8529. ELSIF (from IS SyntaxTree.MathArrayType) THEN
  8530. IF to IS SyntaxTree.MathArrayType THEN
  8531. (*
  8532. IF varpar & (from(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor) & (to(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) THEN
  8533. i := Infinity;
  8534. ELSE
  8535. *)
  8536. i := MathArrayTypeDistance(system,from(SyntaxTree.MathArrayType), to(SyntaxTree.MathArrayType),varpar)
  8537. (*
  8538. END;
  8539. *)
  8540. END
  8541. ELSIF (from IS SyntaxTree.PointerType) THEN
  8542. ptr := from(SyntaxTree.PointerType);
  8543. IF (to IS SyntaxTree.AnyType) THEN i := 1
  8544. ELSIF to IS SyntaxTree.PointerType THEN i := PointerTypeDistance(ptr, to(SyntaxTree.PointerType))
  8545. (* ELSE i := TypeDistance(ptr.base, to); *)
  8546. END
  8547. ELSIF (from IS SyntaxTree.ProcedureType) THEN
  8548. IF (to IS SyntaxTree.ProcedureType) THEN
  8549. i := ProcedureTypeDistance(system, from(SyntaxTree.ProcedureType), to(SyntaxTree.ProcedureType));
  8550. END;
  8551. ELSIF (from IS SyntaxTree.PortType) THEN
  8552. IF (to IS SyntaxTree.PortType) THEN
  8553. IF (to.sizeInBits = from.sizeInBits) & (to(SyntaxTree.PortType).direction = from(SyntaxTree.PortType).direction) THEN
  8554. i := 0;
  8555. END;
  8556. END;
  8557. (*no procedure test, procedure must be the same*)
  8558. END;
  8559. RETURN i
  8560. END TypeDistance;
  8561. PROCEDURE IsIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8562. BEGIN
  8563. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8564. END IsIntegerType;
  8565. PROCEDURE IsAddressType*(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8566. BEGIN
  8567. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.AddressType) OR (type IS SyntaxTree.SizeType))
  8568. END IsAddressType;
  8569. PROCEDURE IsSizeType(type: SyntaxTree.Type; addressWidth: LONGINT): BOOLEAN;
  8570. BEGIN
  8571. RETURN (type # NIL) & ((type IS SyntaxTree.IntegerType) & (type(SyntaxTree.IntegerType).sizeInBits <= addressWidth) OR (type IS SyntaxTree.SizeType))
  8572. END IsSizeType;
  8573. PROCEDURE IsSignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8574. BEGIN
  8575. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & type(SyntaxTree.IntegerType).signed
  8576. END IsSignedIntegerType;
  8577. PROCEDURE IsUnsignedIntegerType*(type: SyntaxTree.Type): BOOLEAN;
  8578. BEGIN
  8579. RETURN (type # NIL) & (type IS SyntaxTree.IntegerType) & ~type(SyntaxTree.IntegerType).signed
  8580. END IsUnsignedIntegerType;
  8581. PROCEDURE IsIntegerValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8582. VAR result: BOOLEAN;
  8583. BEGIN
  8584. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.IntegerValue) THEN
  8585. value := x.resolved(SyntaxTree.IntegerValue).value;
  8586. result := TRUE
  8587. ELSE
  8588. result := FALSE
  8589. END;
  8590. RETURN result
  8591. END IsIntegerValue;
  8592. PROCEDURE IsEnumerationValue(x: SyntaxTree.Expression; VAR value: LONGINT): BOOLEAN;
  8593. VAR result: BOOLEAN;
  8594. BEGIN
  8595. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.EnumerationValue) THEN
  8596. value := x.resolved(SyntaxTree.EnumerationValue).value;
  8597. result := TRUE
  8598. ELSE
  8599. result := FALSE
  8600. END;
  8601. RETURN result
  8602. END IsEnumerationValue;
  8603. PROCEDURE IsRealValue(x: SyntaxTree.Expression; VAR value: LONGREAL): BOOLEAN;
  8604. VAR result: BOOLEAN;
  8605. BEGIN
  8606. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.RealValue) THEN
  8607. value := x.resolved(SyntaxTree.RealValue).value;
  8608. result := TRUE
  8609. ELSE
  8610. result := FALSE
  8611. END;
  8612. RETURN result
  8613. END IsRealValue;
  8614. PROCEDURE IsComplexValue(x: SyntaxTree.Expression; VAR realValue, imagValue: LONGREAL): BOOLEAN;
  8615. VAR result: BOOLEAN;
  8616. BEGIN
  8617. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.ComplexValue) THEN
  8618. realValue := x.resolved(SyntaxTree.ComplexValue).realValue;
  8619. imagValue := x.resolved(SyntaxTree.ComplexValue).imagValue;
  8620. result := TRUE
  8621. ELSE
  8622. result := FALSE
  8623. END;
  8624. RETURN result
  8625. END IsComplexValue;
  8626. PROCEDURE IsCharacterValue(x: SyntaxTree.Expression; VAR value: CHAR): BOOLEAN;
  8627. VAR result: BOOLEAN;
  8628. BEGIN
  8629. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.CharacterValue) THEN
  8630. value := x.resolved(SyntaxTree.CharacterValue).value;
  8631. result := TRUE
  8632. ELSE
  8633. result := FALSE
  8634. END;
  8635. RETURN result
  8636. END IsCharacterValue;
  8637. PROCEDURE IsBooleanValue*(x: SyntaxTree.Expression; VAR value: BOOLEAN): BOOLEAN;
  8638. VAR result: BOOLEAN;
  8639. BEGIN
  8640. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.BooleanValue) THEN
  8641. value := x.resolved(SyntaxTree.BooleanValue).value;
  8642. result := TRUE
  8643. ELSE
  8644. result := FALSE
  8645. END;
  8646. RETURN result
  8647. END IsBooleanValue;
  8648. PROCEDURE IsSetValue(x: SyntaxTree.Expression; VAR value: SET): BOOLEAN;
  8649. VAR result: BOOLEAN;
  8650. BEGIN
  8651. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.SetValue) THEN
  8652. value := x.resolved(SyntaxTree.SetValue).value;
  8653. result := TRUE
  8654. ELSE
  8655. result := FALSE
  8656. END;
  8657. RETURN result
  8658. END IsSetValue;
  8659. PROCEDURE IsStringValue(x: SyntaxTree.Expression; VAR value: Scanner.StringType): BOOLEAN;
  8660. VAR result: BOOLEAN;
  8661. BEGIN
  8662. IF (x.resolved # NIL) & (x.resolved IS SyntaxTree.StringValue) THEN
  8663. value := x.resolved(SyntaxTree.StringValue).value;
  8664. result := TRUE
  8665. ELSE
  8666. result := FALSE
  8667. END;
  8668. RETURN result
  8669. END IsStringValue;
  8670. PROCEDURE Indexable(x: SyntaxTree.Type): BOOLEAN;
  8671. BEGIN
  8672. x := x.resolved;
  8673. RETURN (x IS SyntaxTree.ArrayType) OR (x IS SyntaxTree.MathArrayType);
  8674. END Indexable;
  8675. PROCEDURE SameType(t1,t2: SyntaxTree.Type): BOOLEAN;
  8676. BEGIN
  8677. RETURN t1.SameType(t2.resolved);
  8678. END SameType;
  8679. PROCEDURE ArrayBase*(t: SyntaxTree.Type; max: LONGINT): SyntaxTree.Type;
  8680. BEGIN
  8681. IF t IS SyntaxTree.MathArrayType THEN
  8682. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & ((t(SyntaxTree.MathArrayType).form # SyntaxTree.Tensor) OR (max = Infinity)) & (max > 0) DO
  8683. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase);
  8684. IF (t # NIL) & (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.MathArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8685. DEC(max);
  8686. END;
  8687. ELSIF t IS SyntaxTree.ArrayType THEN
  8688. WHILE (t IS SyntaxTree.ArrayType) & (max > 0) DO
  8689. t := t(SyntaxTree.ArrayType).arrayBase.resolved; DEC(max);
  8690. IF (t IS SyntaxTree.PointerType) & (t(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.ArrayType) THEN t := t(SyntaxTree.PointerType).pointerBase.resolved END;
  8691. END;
  8692. END;
  8693. RETURN t;
  8694. END ArrayBase;
  8695. PROCEDURE IsOpenArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8696. BEGIN
  8697. type := type.resolved;
  8698. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Open) THEN
  8699. base := type(SyntaxTree.ArrayType).arrayBase;
  8700. RETURN TRUE;
  8701. END;
  8702. RETURN FALSE;
  8703. END IsOpenArray;
  8704. PROCEDURE IsStaticArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type; VAR dim :LONGINT): BOOLEAN;
  8705. BEGIN
  8706. type := type.resolved;
  8707. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.Static) THEN
  8708. base := type(SyntaxTree.ArrayType).arrayBase;
  8709. dim := type(SyntaxTree.ArrayType).staticLength;
  8710. RETURN TRUE
  8711. ELSE
  8712. RETURN FALSE
  8713. END;
  8714. END IsStaticArray;
  8715. PROCEDURE IsDynamicArray*(type: SyntaxTree.Type; VAR base: SyntaxTree.Type): BOOLEAN;
  8716. BEGIN
  8717. type := type.resolved;
  8718. IF (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).form = SyntaxTree.SemiDynamic) THEN
  8719. base := type(SyntaxTree.ArrayType).arrayBase;
  8720. RETURN TRUE
  8721. ELSE
  8722. RETURN FALSE
  8723. END;
  8724. END IsDynamicArray;
  8725. PROCEDURE Dimension*(t: SyntaxTree.Type; form: SET): LONGINT;
  8726. VAR i: LONGINT;
  8727. BEGIN
  8728. i := 0;
  8729. t := t.resolved;
  8730. IF t IS SyntaxTree.MathArrayType THEN
  8731. WHILE (t # NIL) & (t IS SyntaxTree.MathArrayType) & (t(SyntaxTree.MathArrayType).form IN form) DO
  8732. t := Resolved(t(SyntaxTree.MathArrayType).arrayBase); INC(i);
  8733. END;
  8734. ELSIF t IS SyntaxTree.ArrayType THEN
  8735. WHILE(t IS SyntaxTree.ArrayType) & (t(SyntaxTree.ArrayType).form IN form) DO
  8736. t := t(SyntaxTree.ArrayType).arrayBase.resolved; INC(i);
  8737. END;
  8738. END;
  8739. RETURN i
  8740. END Dimension;
  8741. PROCEDURE IsVariable(expression: SyntaxTree.Expression): BOOLEAN;
  8742. BEGIN
  8743. RETURN expression.assignable;
  8744. END IsVariable;
  8745. PROCEDURE IsVariableParameter*(symbol: SyntaxTree.Symbol): BOOLEAN;
  8746. BEGIN
  8747. IF (symbol IS SyntaxTree.Parameter) THEN
  8748. WITH symbol: SyntaxTree.Parameter DO
  8749. RETURN (symbol.kind = SyntaxTree.VarParameter) OR (symbol.kind = SyntaxTree.ConstParameter) & ((symbol.type.resolved IS SyntaxTree.RecordType) OR (symbol.type.resolved IS SyntaxTree.ArrayType));
  8750. END;
  8751. ELSE
  8752. RETURN FALSE
  8753. END;
  8754. END IsVariableParameter;
  8755. PROCEDURE IsPointerType*(type: SyntaxTree.Type): BOOLEAN;
  8756. VAR result: BOOLEAN;
  8757. BEGIN
  8758. IF type = NIL THEN result := FALSE
  8759. ELSE
  8760. type := type.resolved;
  8761. result := (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.PointerType) OR (type IS SyntaxTree.NilType) OR (type IS SyntaxTree.ObjectType)
  8762. END;
  8763. RETURN result
  8764. END IsPointerType;
  8765. PROCEDURE IsUnsafePointer*(type: SyntaxTree.Type): BOOLEAN;
  8766. VAR result: BOOLEAN;
  8767. BEGIN
  8768. IF type = NIL THEN result := FALSE
  8769. ELSE
  8770. type := type.resolved;
  8771. result := (type IS SyntaxTree.PointerType) & type(SyntaxTree.PointerType).isUnsafe;
  8772. END;
  8773. RETURN result
  8774. END IsUnsafePointer;
  8775. PROCEDURE IsDisposable*(type: SyntaxTree.Type): BOOLEAN;
  8776. BEGIN
  8777. RETURN (type # NIL) & (type.resolved IS SyntaxTree.PointerType) & (type.resolved(SyntaxTree.PointerType).isDisposable)
  8778. END IsDisposable;
  8779. PROCEDURE IsPointerToRecord(type: SyntaxTree.Type): BOOLEAN;
  8780. VAR result: BOOLEAN;
  8781. BEGIN
  8782. IF type = NIL THEN result := FALSE
  8783. ELSE
  8784. type := type.resolved;
  8785. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType);
  8786. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8787. result := result OR (type IS SyntaxTree.ObjectType);
  8788. END;
  8789. RETURN result
  8790. END IsPointerToRecord;
  8791. PROCEDURE IsPointerToObject(type: SyntaxTree.Type): BOOLEAN;
  8792. VAR result: BOOLEAN;
  8793. BEGIN
  8794. IF type = NIL THEN result := FALSE
  8795. ELSE
  8796. type := type.resolved;
  8797. result := (type IS SyntaxTree.PointerType) & (type(SyntaxTree.PointerType).pointerBase.resolved IS SyntaxTree.RecordType)
  8798. & (type(SyntaxTree.PointerType).pointerBase.resolved(SyntaxTree.RecordType).isObject)
  8799. ;
  8800. result := result OR (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL);
  8801. result := result OR (type IS SyntaxTree.ObjectType);
  8802. END;
  8803. RETURN result
  8804. END IsPointerToObject;
  8805. PROCEDURE ContainsPointer*(type: SyntaxTree.Type): BOOLEAN;
  8806. BEGIN
  8807. IF type # NIL THEN
  8808. RETURN type.resolved.hasPointers
  8809. ELSE
  8810. RETURN FALSE
  8811. END;
  8812. END ContainsPointer;
  8813. PROCEDURE IsStringType*(type: SyntaxTree.Type): BOOLEAN;
  8814. BEGIN
  8815. IF type = NIL THEN RETURN FALSE END;
  8816. type := type.resolved;
  8817. RETURN (type IS SyntaxTree.StringType) OR (type IS SyntaxTree.ArrayType) & (type(SyntaxTree.ArrayType).arrayBase.resolved IS SyntaxTree.CharacterType);
  8818. END IsStringType;
  8819. PROCEDURE IsCharacterType*(type: SyntaxTree.Type):BOOLEAN;
  8820. BEGIN
  8821. IF type = NIL THEN RETURN FALSE END;
  8822. type := type.resolved;
  8823. RETURN (type IS SyntaxTree.CharacterType) OR (type IS SyntaxTree.ByteType) OR (type IS SyntaxTree.StringType) & (type(SyntaxTree.StringType).length = 2)
  8824. END IsCharacterType;
  8825. PROCEDURE IsEnumerationType*(type: SyntaxTree.Type):BOOLEAN;
  8826. BEGIN
  8827. IF type = NIL THEN RETURN FALSE END;
  8828. type := type.resolved;
  8829. RETURN (type IS SyntaxTree.EnumerationType)
  8830. END IsEnumerationType;
  8831. (** cf. section "Type extension (base type)" in the language report **)
  8832. PROCEDURE IsTypeExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8833. VAR result: BOOLEAN;
  8834. BEGIN
  8835. ASSERT(base # NIL); ASSERT(extension # NIL);
  8836. base := base.resolved; extension := extension.resolved;
  8837. IF ( (base IS SyntaxTree.ObjectType) OR (base IS SyntaxTree.AnyType)) & IsPointerToRecord(extension) THEN
  8838. result := TRUE;
  8839. ELSE
  8840. IF (base IS SyntaxTree.PointerType) & (extension IS SyntaxTree.PointerType) THEN
  8841. base := base(SyntaxTree.PointerType).pointerBase.resolved;
  8842. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8843. END;
  8844. WHILE (extension # NIL) & (extension # base) DO
  8845. IF extension IS SyntaxTree.RecordType THEN
  8846. extension := extension(SyntaxTree.RecordType).baseType;
  8847. IF (extension # NIL) THEN extension := extension.resolved END;
  8848. IF (extension # NIL) & (extension IS SyntaxTree.PointerType) THEN
  8849. extension := extension(SyntaxTree.PointerType).pointerBase.resolved;
  8850. END;
  8851. ELSE extension := NIL;
  8852. END;
  8853. END;
  8854. result := (extension = base) & (extension IS SyntaxTree.RecordType);
  8855. END;
  8856. RETURN result
  8857. END IsTypeExtension;
  8858. (** check if base is the base enumeration type of extension **)
  8859. PROCEDURE IsEnumerationExtension(base,extension: SyntaxTree.Type): BOOLEAN;
  8860. BEGIN
  8861. base := base.resolved; extension := extension.resolved;
  8862. WHILE (extension # NIL) & (extension # base) DO
  8863. IF extension IS SyntaxTree.EnumerationType THEN
  8864. extension := extension(SyntaxTree.EnumerationType).enumerationBase;
  8865. IF extension # NIL THEN extension := extension.resolved END;
  8866. ELSE
  8867. extension := NIL
  8868. END;
  8869. END;
  8870. RETURN (extension = base) & (base IS SyntaxTree.EnumerationType);
  8871. END IsEnumerationExtension;
  8872. PROCEDURE IsCallable(expression: SyntaxTree.Expression): BOOLEAN;
  8873. BEGIN
  8874. IF expression IS SyntaxTree.ProcedureCallDesignator THEN
  8875. RETURN TRUE
  8876. ELSIF expression IS SyntaxTree.BuiltinCallDesignator THEN
  8877. RETURN TRUE
  8878. ELSIF (expression.type # NIL) & (expression.type.resolved IS SyntaxTree.ProcedureType) THEN
  8879. RETURN TRUE
  8880. ELSE
  8881. RETURN FALSE
  8882. END
  8883. END IsCallable;
  8884. (** compute and return the distance of two record types
  8885. returns the number of extension levels of from to to, returns infinite if to is not an extension of from
  8886. **)
  8887. PROCEDURE RecordTypeDistance(from, to: SyntaxTree.RecordType): LONGINT;
  8888. VAR i: LONGINT; baseType: SyntaxTree.Type;
  8889. BEGIN
  8890. i := 0;
  8891. WHILE (from # NIL) & (from # to) DO
  8892. baseType := from.baseType;
  8893. IF (baseType # NIL) THEN
  8894. baseType := baseType.resolved;
  8895. IF baseType IS SyntaxTree.PointerType THEN
  8896. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8897. END;
  8898. IF baseType IS SyntaxTree.RecordType THEN
  8899. from := baseType(SyntaxTree.RecordType);
  8900. ELSE
  8901. from := NIL;
  8902. END;
  8903. ELSE
  8904. from := NIL
  8905. END;
  8906. INC(i)
  8907. END;
  8908. IF from = NIL THEN i := Infinity END;
  8909. RETURN i
  8910. END RecordTypeDistance;
  8911. (** compute and return the distance of two pointer types **)
  8912. PROCEDURE PointerTypeDistance(from, to: SyntaxTree.PointerType): LONGINT;
  8913. BEGIN
  8914. IF ~((to.pointerBase.resolved IS SyntaxTree.RecordType) & (from.pointerBase.resolved IS SyntaxTree.RecordType)) THEN
  8915. RETURN Infinity;
  8916. ELSE
  8917. RETURN RecordTypeDistance(from.pointerBase.resolved(SyntaxTree.RecordType), to.pointerBase.resolved(SyntaxTree.RecordType));
  8918. END;
  8919. END PointerTypeDistance;
  8920. (** check if expression contains a symbol designator pointing to a type declaration.
  8921. - if so then enter type declaration into typeDeclaration and return true else return false
  8922. **)
  8923. PROCEDURE IsTypeDesignator(expression: SyntaxTree.Expression; VAR typeDeclaration: SyntaxTree.TypeDeclaration): BOOLEAN;
  8924. VAR result: BOOLEAN;
  8925. BEGIN
  8926. result := FALSE;
  8927. IF (expression # NIL) & (expression.type.resolved = SyntaxTree.typeDeclarationType) THEN
  8928. result := TRUE;
  8929. typeDeclaration := expression(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.TypeDeclaration)
  8930. END;
  8931. RETURN result
  8932. END IsTypeDesignator;
  8933. (** returns true if type is an extensible type (pointer to record, record, object or any), returns false otherwise **)
  8934. PROCEDURE IsExtensibleType( type: SyntaxTree.Type): BOOLEAN;
  8935. VAR result: BOOLEAN;
  8936. BEGIN
  8937. type := type.resolved;
  8938. IF type IS SyntaxTree.PointerType THEN
  8939. result := IsExtensibleType(type(SyntaxTree.PointerType).pointerBase.resolved);
  8940. ELSIF (type IS SyntaxTree.AnyType) OR (type IS SyntaxTree.ObjectType) THEN
  8941. result := TRUE
  8942. ELSE
  8943. result := type IS SyntaxTree.RecordType
  8944. END;
  8945. RETURN result
  8946. END IsExtensibleType;
  8947. PROCEDURE IsUnextensibleRecord(d: SyntaxTree.Expression): BOOLEAN;
  8948. BEGIN
  8949. RETURN (d.type.resolved IS SyntaxTree.RecordType) &
  8950. (d IS SyntaxTree.SymbolDesignator) &
  8951. ( (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Variable)
  8952. OR
  8953. (d(SyntaxTree.SymbolDesignator).symbol IS SyntaxTree.Parameter) & (d(SyntaxTree.SymbolDesignator).symbol(SyntaxTree.Parameter).kind = SyntaxTree.ValueParameter));
  8954. END IsUnextensibleRecord;
  8955. PROCEDURE IsExtensibleDesignator(d: SyntaxTree.Expression): BOOLEAN;
  8956. BEGIN
  8957. IF IsUnextensibleRecord(d) THEN
  8958. RETURN FALSE
  8959. ELSE RETURN IsExtensibleType(d.type.resolved)
  8960. END;
  8961. END IsExtensibleDesignator;
  8962. PROCEDURE IsBasicType(type: SyntaxTree.Type): BOOLEAN;
  8963. BEGIN
  8964. type := type.resolved;
  8965. IF (type IS SyntaxTree.PointerType) THEN
  8966. RETURN TRUE
  8967. ELSIF (type IS SyntaxTree.RecordType) & (type(SyntaxTree.RecordType).pointerType # NIL) (* object *) THEN
  8968. RETURN TRUE
  8969. ELSIF (type IS SyntaxTree.ProcedureType) THEN
  8970. RETURN TRUE
  8971. ELSIF (type IS SyntaxTree.BasicType) THEN
  8972. RETURN TRUE
  8973. END;
  8974. RETURN FALSE
  8975. END IsBasicType;
  8976. PROCEDURE RecordBase*(record: SyntaxTree.RecordType): SyntaxTree.RecordType;
  8977. VAR baseType: SyntaxTree.Type; recordType: SyntaxTree.RecordType;
  8978. BEGIN
  8979. baseType := record.baseType;
  8980. IF (baseType # NIL) THEN
  8981. baseType := baseType.resolved;
  8982. IF (baseType IS SyntaxTree.PointerType) THEN
  8983. baseType := baseType(SyntaxTree.PointerType).pointerBase.resolved;
  8984. END;
  8985. END;
  8986. IF (baseType # NIL) & (baseType IS SyntaxTree.RecordType) THEN
  8987. recordType := baseType(SyntaxTree.RecordType);
  8988. ELSE
  8989. recordType := NIL;
  8990. END;
  8991. RETURN recordType
  8992. END RecordBase;
  8993. PROCEDURE FindSuperProcedure*(scope: SyntaxTree.RecordScope; procedure: SyntaxTree.Procedure): SyntaxTree.Procedure;
  8994. VAR super: SyntaxTree.Procedure; operator: SyntaxTree.Operator; procedureType: SyntaxTree.Type; baseRecord: SyntaxTree.RecordType;
  8995. BEGIN
  8996. baseRecord := RecordBase(scope.ownerRecord);
  8997. IF baseRecord = NIL THEN RETURN NIL END;
  8998. scope := baseRecord.recordScope;
  8999. procedureType := procedure.type.resolved;
  9000. IF procedure IS SyntaxTree.Operator THEN
  9001. operator := scope.firstOperator;
  9002. WHILE (operator # NIL) & ((operator.name # procedure.name) OR ~SameType(procedureType, operator.type)) DO
  9003. (*
  9004. Printout.Info("not same ",procedureType);
  9005. Printout.Info("with ",operator.type);
  9006. *)
  9007. operator := operator.nextOperator;
  9008. END;
  9009. super := operator;
  9010. ELSE
  9011. super := scope.firstProcedure;
  9012. WHILE (super # NIL) & (super.name # procedure.name) DO
  9013. super := super.nextProcedure;
  9014. END;
  9015. END;
  9016. IF (super # NIL) & ((super.scope.ownerModule = procedure.scope.ownerModule) OR (SyntaxTree.Public * super.access # {})) THEN
  9017. RETURN super
  9018. ELSIF (super # NIL) & (FindSuperProcedure(scope,procedure)#NIL) THEN (* check if there is an exported supermethod, in which case return (non-exported) supermethod *)
  9019. RETURN super
  9020. ELSE
  9021. RETURN FindSuperProcedure(scope,procedure);
  9022. END;
  9023. END FindSuperProcedure;
  9024. PROCEDURE GetConstructor(record: SyntaxTree.RecordType): SyntaxTree.Procedure;
  9025. VAR procedure: SyntaxTree.Procedure;
  9026. BEGIN
  9027. procedure := record.recordScope.constructor;
  9028. IF procedure = NIL THEN
  9029. record := RecordBase(record);
  9030. IF record # NIL THEN
  9031. procedure := GetConstructor(record)
  9032. END;
  9033. END;
  9034. RETURN procedure;
  9035. END GetConstructor;
  9036. (* enter a case into a list of cases in a sorted way and check for collision *)
  9037. PROCEDURE EnterCase(VAR root: SyntaxTree.CaseConstant; min,max: LONGINT): BOOLEAN;
  9038. VAR prev,this,new: SyntaxTree.CaseConstant;
  9039. BEGIN
  9040. this := root;
  9041. prev := NIL;
  9042. WHILE (this # NIL) & (min > this.max) DO prev := this; this := this.next END;
  9043. IF (this # NIL) & (max >= this.min) THEN (* collision since min <= this.max and max >= this.min *)
  9044. RETURN FALSE
  9045. ELSE
  9046. IF (this # NIL) & (this.min = max+1) THEN
  9047. this.min := min
  9048. ELSIF (prev # NIL) & (min+1 = prev.max) THEN
  9049. prev.max := min
  9050. ELSE
  9051. NEW(new); new.min := min; new.max := max;
  9052. new.next := this;
  9053. IF prev = NIL THEN
  9054. root := new;
  9055. ELSE
  9056. prev.next := new
  9057. END
  9058. END;
  9059. RETURN TRUE
  9060. END;
  9061. END EnterCase;
  9062. (** generate and return a new checker object, errors are entered into diagnostics **)
  9063. 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;
  9064. VAR checker: Checker;
  9065. BEGIN
  9066. NEW(checker, diagnostics,verboseErrorMessage,useDarwinCCalls,cooperative,system,symbolFileFormat,importCache,backend);
  9067. RETURN checker
  9068. END NewChecker;
  9069. PROCEDURE NewWarnings*(diagnostics: Diagnostics.Diagnostics): Warnings;
  9070. VAR warnings: Warnings;
  9071. BEGIN
  9072. NEW(warnings, diagnostics); RETURN warnings;
  9073. END NewWarnings;
  9074. PROCEDURE IsRangeType(type: SyntaxTree.Type): BOOLEAN;
  9075. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.RangeType);
  9076. END IsRangeType;
  9077. PROCEDURE IsMathArrayType(type: SyntaxTree.Type): BOOLEAN;
  9078. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.MathArrayType);
  9079. END IsMathArrayType;
  9080. PROCEDURE IsArrayType(type: SyntaxTree.Type): BOOLEAN;
  9081. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ArrayType);
  9082. END IsArrayType;
  9083. PROCEDURE IsComplexType(type: SyntaxTree.Type): BOOLEAN;
  9084. BEGIN RETURN (type # NIL) & (type.resolved IS SyntaxTree.ComplexType);
  9085. END IsComplexType;
  9086. (** if a type is an array-structured object type *)
  9087. PROCEDURE IsArrayStructuredObjectType*(type: SyntaxTree.Type): BOOLEAN;
  9088. VAR recordType: SyntaxTree.RecordType;
  9089. BEGIN
  9090. IF type = NIL THEN
  9091. RETURN FALSE
  9092. ELSE
  9093. type := type.resolved;
  9094. IF type IS SyntaxTree.PointerType THEN
  9095. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9096. IF type IS SyntaxTree.RecordType THEN
  9097. recordType := type(SyntaxTree.RecordType);
  9098. RETURN recordType.isObject & recordType.HasArrayStructure()
  9099. ELSE
  9100. RETURN FALSE
  9101. END
  9102. ELSE
  9103. RETURN FALSE
  9104. END
  9105. END
  9106. END IsArrayStructuredObjectType;
  9107. (** the math array structure of a type
  9108. - for math arrays: the array itself
  9109. - for pointers: the math array structure of the pointer base
  9110. - for array-structured object types: the underlying structure
  9111. - for non-math arrays and all other types: NIL
  9112. **)
  9113. PROCEDURE MathArrayStructureOfType(type: SyntaxTree.Type): SyntaxTree.MathArrayType;
  9114. VAR
  9115. result: SyntaxTree.MathArrayType;
  9116. BEGIN
  9117. IF type = NIL THEN
  9118. result := NIL
  9119. ELSE
  9120. type := type.resolved;
  9121. IF type IS SyntaxTree.PointerType THEN
  9122. type := type(SyntaxTree.PointerType).pointerBase.resolved;
  9123. END;
  9124. IF type IS SyntaxTree.MathArrayType THEN
  9125. result := type(SyntaxTree.MathArrayType)
  9126. ELSIF type IS SyntaxTree.RecordType THEN
  9127. result := type(SyntaxTree.RecordType).arrayStructure
  9128. ELSE
  9129. result := NIL
  9130. END
  9131. END;
  9132. RETURN result
  9133. END MathArrayStructureOfType;
  9134. PROCEDURE IsStaticRange(x: SyntaxTree.Expression; VAR firstValue, lastValue, stepValue: LONGINT): BOOLEAN;
  9135. VAR
  9136. result: BOOLEAN;
  9137. rangeExpression: SyntaxTree.RangeExpression;
  9138. BEGIN
  9139. IF x IS SyntaxTree.RangeExpression THEN
  9140. rangeExpression := x(SyntaxTree.RangeExpression);
  9141. result := TRUE;
  9142. IF ~IsIntegerValue(rangeExpression.first, firstValue) THEN result := FALSE END;
  9143. IF ~IsIntegerValue(rangeExpression.last, lastValue) THEN result := FALSE END;
  9144. IF ~IsIntegerValue(rangeExpression.step, stepValue) THEN result := FALSE END
  9145. ELSE
  9146. result := FALSE
  9147. END;
  9148. RETURN result
  9149. END IsStaticRange;
  9150. (** whether a type is a math array of tensor form **)
  9151. PROCEDURE IsTensor(type: SyntaxTree.Type): BOOLEAN;
  9152. BEGIN RETURN (type.resolved IS SyntaxTree.MathArrayType) & (type.resolved(SyntaxTree.MathArrayType).form = SyntaxTree.Tensor)
  9153. END IsTensor;
  9154. PROCEDURE IsStaticMathArray*(type: SyntaxTree.Type; VAR length: LONGINT; VAR baseType: SyntaxTree.Type): BOOLEAN;
  9155. BEGIN
  9156. IF (type IS SyntaxTree.MathArrayType) & (type(SyntaxTree.MathArrayType).form = SyntaxTree.Static) THEN
  9157. length := type(SyntaxTree.MathArrayType).staticLength;
  9158. baseType := type(SyntaxTree.MathArrayType).arrayBase.resolved;
  9159. RETURN TRUE
  9160. ELSE
  9161. RETURN FALSE
  9162. END;
  9163. END IsStaticMathArray;
  9164. PROCEDURE SymbolHasAddress(symbol: SyntaxTree.Symbol): BOOLEAN;
  9165. BEGIN
  9166. RETURN (symbol IS SyntaxTree.Variable) OR (symbol IS SyntaxTree.Parameter) OR (symbol IS SyntaxTree.Procedure)
  9167. END SymbolHasAddress;
  9168. PROCEDURE HasAddress(expression: SyntaxTree.Expression): BOOLEAN;
  9169. BEGIN
  9170. RETURN (expression # NIL) & (expression IS SyntaxTree.SymbolDesignator) & SymbolHasAddress(expression(SyntaxTree.SymbolDesignator).symbol) OR (expression IS SyntaxTree.ResultDesignator)
  9171. OR (expression IS SyntaxTree.IndexDesignator) OR (expression IS SyntaxTree.DereferenceDesignator)
  9172. OR (expression IS SyntaxTree.TypeGuardDesignator) OR (expression IS SyntaxTree.StringValue)
  9173. OR (expression IS SyntaxTree.StatementDesignator) & HasAddress(expression(SyntaxTree.StatementDesignator).result);
  9174. END HasAddress;
  9175. PROCEDURE IsLocalVariable*(e: SyntaxTree.Expression): BOOLEAN;
  9176. VAR d: SyntaxTree.Designator; symbol: SyntaxTree.Symbol;
  9177. BEGIN
  9178. IF (e IS SyntaxTree.Designator) THEN
  9179. d := e(SyntaxTree.Designator);
  9180. WHILE (d # NIL) & ~(d IS SyntaxTree.SymbolDesignator) DO
  9181. IF d IS SyntaxTree.DereferenceDesignator THEN (* on heap *) RETURN FALSE END;
  9182. e := d.left;
  9183. IF (e # NIL) & (e IS SyntaxTree.Designator) THEN d := e(SyntaxTree.Designator) ELSE d := NIL END;
  9184. END;
  9185. IF d # NIL THEN
  9186. symbol := d(SyntaxTree.SymbolDesignator).symbol;
  9187. RETURN (symbol.scope IS SyntaxTree.ProcedureScope) & (symbol.externalName = NIL);
  9188. END;
  9189. END;
  9190. RETURN FALSE;
  9191. END IsLocalVariable;
  9192. PROCEDURE IsStaticProcedure*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9193. BEGIN
  9194. IF procedure.scope IS SyntaxTree.RecordScope THEN
  9195. RETURN (procedure.super = NIL) & ((procedure.isFinal) OR (procedure.access * SyntaxTree.Public = {}) & ~procedure.isOverwritten)
  9196. ELSE
  9197. RETURN TRUE
  9198. END;
  9199. END IsStaticProcedure;
  9200. PROCEDURE InMethodTable*(procedure: SyntaxTree.Procedure): BOOLEAN;
  9201. CONST OptimizeMethodTable = FALSE;
  9202. BEGIN
  9203. RETURN ~OptimizeMethodTable OR IsStaticProcedure(procedure)
  9204. END InMethodTable;
  9205. END FoxSemanticChecker.
  9206. SystemTools.FreeDownTo FoxSemanticChecker ~