symdef.pas 331 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742
  1. {
  2. Symbol table implementation for the definitions
  3. Copyright (c) 1998-2005 by Florian Klaempfl, Pierre Muller
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symdef;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cclasses,widestr,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { node }
  28. node,
  29. { aasm }
  30. aasmtai,
  31. cpuinfo,
  32. cgbase,
  33. parabase
  34. ;
  35. type
  36. {************************************************
  37. TDef
  38. ************************************************}
  39. tgenericconstraintdata=class
  40. interfaces : tfpobjectlist;
  41. interfacesderef : tfplist;
  42. flags : tgenericconstraintflags;
  43. { only required while parsing }
  44. fileinfo : tfileposinfo;
  45. constructor create;
  46. destructor destroy;override;
  47. procedure ppuload(ppufile:tcompilerppufile);
  48. procedure ppuwrite(ppufile:tcompilerppufile);
  49. procedure buildderef;
  50. procedure deref;
  51. end;
  52. { trtti_attribute_list }
  53. trtti_attribute = class
  54. typesym : tsym;
  55. typesymderef : tderef;
  56. typeconstr : tdef;
  57. typeconstrderef : tderef;
  58. { these two are not stored in PPU }
  59. constructorcall : tnode;
  60. constructorpd : tdef;
  61. paras : array of tnode;
  62. constructor ppuload(ppufile:tcompilerppufile);
  63. procedure ppuwrite(ppufile:tcompilerppufile);
  64. procedure ppuload_subentries(ppufile:tcompilerppufile);
  65. procedure ppuwrite_subentries(ppufile:tcompilerppufile);
  66. destructor destroy;override;
  67. procedure buildderef;
  68. procedure deref;
  69. end;
  70. trtti_attribute_list = class
  71. rtti_attributes : TFPObjectList;
  72. { if the attribute list is bound to a def or symbol }
  73. is_bound : Boolean;
  74. class procedure bind(var dangling,owned:trtti_attribute_list);
  75. class procedure copyandbind(alist :trtti_attribute_list; var owned : trtti_attribute_list);
  76. procedure addattribute(atypesym:tsym;typeconstr:tdef;constructorcall:tnode;constref paras:array of tnode);
  77. procedure addattribute(attr:trtti_attribute);
  78. destructor destroy; override;
  79. function get_attribute_count:longint;
  80. procedure buildderef;
  81. procedure deref;
  82. class function ppuload(ppufile:tcompilerppufile):trtti_attribute_list;
  83. class procedure ppuwrite(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  84. class procedure ppuload_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  85. class procedure ppuwrite_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  86. end;
  87. { tstoreddef }
  88. tstoreddef = class(tdef)
  89. private
  90. {$ifdef symansistr}
  91. _fullownerhierarchyname : ansistring;
  92. {$else symansistr}
  93. _fullownerhierarchyname : pshortstring;
  94. {$endif symansistr}
  95. procedure writeentry(ppufile: tcompilerppufile; ibnr: byte);
  96. protected
  97. typesymderef : tderef;
  98. procedure ppuwrite_platform(ppufile:tcompilerppufile);virtual;
  99. procedure ppuload_platform(ppufile:tcompilerppufile);virtual;
  100. { a (possibly) reusable def is always created on the basis of another
  101. def, and contains a reference to this other def. If this other
  102. def is in a non-persistent symboltable, the new def cannot actually
  103. be safely reused everywhere in the current module. This routine
  104. abtracts that checking, and also restores the symtable stack
  105. (which had to be reset before creating the new def, so that the new
  106. def did not automatically get added to its top) }
  107. class procedure setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  108. public
  109. {$ifdef EXTDEBUG}
  110. fileinfo : tfileposinfo;
  111. {$endif}
  112. { generic support }
  113. genericdef : tstoreddef;
  114. genericdefderef : tderef;
  115. generictokenbuf : tdynamicarray;
  116. { this list contains references to the symbols that make up the
  117. generic parameters; the symbols are not owned by this list
  118. Note: this list is allocated on demand! }
  119. genericparas : tfphashobjectlist;
  120. genericparaderefs : tfplist;
  121. { contains additional data if this def is a generic constraint
  122. Note: this class is allocated on demand! }
  123. genconstraintdata : tgenericconstraintdata;
  124. { this is Nil if the def has no RTTI attributes }
  125. rtti_attribute_list : trtti_attribute_list;
  126. { original def for "type <name>" declarations }
  127. orgdef : tstoreddef;
  128. orgdefderef : tderef;
  129. constructor create(dt:tdeftyp;doregister:boolean);
  130. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  131. destructor destroy;override;
  132. function getcopy : tstoreddef;virtual;
  133. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  134. { this is called directly after ppuload }
  135. procedure ppuload_subentries(ppufile:tcompilerppufile);virtual;
  136. { this is called directly after ppuwrite }
  137. procedure ppuwrite_subentries(ppufile:tcompilerppufile);virtual;
  138. procedure buildderef;override;
  139. procedure buildderefimpl;override;
  140. procedure deref;override;
  141. procedure derefimpl;override;
  142. function size:asizeint;override;
  143. function getvardef:longint;override;
  144. function alignment:shortint;override;
  145. function is_publishable : tpublishproperty;override;
  146. function needs_inittable : boolean;override;
  147. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  148. function rtti_mangledname(rt:trttitype):TSymStr;override;
  149. function OwnerHierarchyName: string; override;
  150. function OwnerHierarchyPrettyName: string; override;
  151. function fullownerhierarchyname(skipprocparams:boolean):TSymStr;override;
  152. function needs_separate_initrtti:boolean;override;
  153. function in_currentunit: boolean;
  154. { regvars }
  155. function is_intregable : boolean;
  156. function is_fpuregable : boolean;
  157. { def can be put into a register if it is const/immutable }
  158. function is_const_intregable : boolean;
  159. { generics }
  160. procedure initgeneric;
  161. { this function can be used to determine whether a def is really a
  162. generic declaration or just a normal type declared inside another
  163. generic }
  164. function is_generic:boolean;
  165. { same as above for specializations }
  166. function is_specialization:boolean;
  167. { generic utilities }
  168. function is_generic_param_const(index:integer):boolean;inline;
  169. function get_generic_param_def(index:integer):tdef;inline;
  170. { registers this def in the unit's deflist; no-op if already registered }
  171. procedure register_def; override;
  172. { add the def to the top of the symtable stack if it's not yet owned
  173. by another symtable }
  174. procedure maybe_put_in_symtable_stack;
  175. private
  176. savesize : asizeuint;
  177. end;
  178. tfiletyp = (ft_text,ft_typed,ft_untyped);
  179. tfiledef = class(tstoreddef)
  180. filetyp : tfiletyp;
  181. typedfiledef : tdef;
  182. typedfiledefderef : tderef;
  183. constructor createtext;virtual;
  184. constructor createuntyped;virtual;
  185. constructor createtyped(def : tdef);virtual;
  186. constructor ppuload(ppufile:tcompilerppufile);
  187. function getcopy : tstoreddef;override;
  188. { do not override this routine in platform-specific subclasses,
  189. override ppuwrite_platform instead }
  190. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  191. procedure buildderef;override;
  192. procedure deref;override;
  193. function GetTypeName:string;override;
  194. function getmangledparaname:TSymStr;override;
  195. function size:asizeint;override;
  196. procedure setsize;
  197. function alignment: shortint; override;
  198. end;
  199. tfiledefclass = class of tfiledef;
  200. tvariantdef = class(tstoreddef)
  201. varianttype : tvarianttype;
  202. constructor create(v : tvarianttype);virtual;
  203. constructor ppuload(ppufile:tcompilerppufile);
  204. function getcopy : tstoreddef;override;
  205. function GetTypeName:string;override;
  206. function alignment : shortint;override;
  207. { do not override this routine in platform-specific subclasses,
  208. override ppuwrite_platform instead }
  209. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  210. function getvardef:longint;override;
  211. procedure setsize;
  212. function is_publishable : tpublishproperty;override;
  213. function needs_inittable : boolean;override;
  214. end;
  215. tvariantdefclass = class of tvariantdef;
  216. tformaldef = class(tstoreddef)
  217. typed:boolean;
  218. constructor create(Atyped:boolean);virtual;
  219. constructor ppuload(ppufile:tcompilerppufile);
  220. { do not override this routine in platform-specific subclasses,
  221. override ppuwrite_platform instead }
  222. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  223. function GetTypeName:string;override;
  224. end;
  225. tformaldefclass = class of tformaldef;
  226. tforwarddef = class(tstoreddef)
  227. tosymname : pshortstring;
  228. forwardpos : tfileposinfo;
  229. constructor create(const s:string;const pos:tfileposinfo);virtual;
  230. destructor destroy;override;
  231. function getcopy:tstoreddef;override;
  232. function GetTypeName:string;override;
  233. end;
  234. tforwarddefclass = class of tforwarddef;
  235. tundefineddef = class(tstoreddef)
  236. constructor create(doregister:boolean);virtual;
  237. constructor ppuload(ppufile:tcompilerppufile);
  238. { do not override this routine in platform-specific subclasses,
  239. override ppuwrite_platform instead }
  240. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  241. function GetTypeName:string;override;
  242. end;
  243. tundefineddefclass = class of tundefineddef;
  244. terrordef = class(tstoreddef)
  245. constructor create;virtual;
  246. { do not override this routine in platform-specific subclasses,
  247. override ppuwrite_platform instead }
  248. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  249. function GetTypeName:string;override;
  250. function getmangledparaname : TSymStr;override;
  251. end;
  252. terrordefclass = class of terrordef;
  253. tabstractpointerdef = class(tstoreddef)
  254. pointeddef : tdef;
  255. pointeddefderef : tderef;
  256. constructor create(dt:tdeftyp;def:tdef);
  257. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  258. procedure ppuwrite(ppufile:tcompilerppufile);override;
  259. procedure buildderef;override;
  260. procedure deref;override;
  261. function size:asizeint;override;
  262. function alignment:shortint;override;
  263. end;
  264. tpointerdef = class(tabstractpointerdef)
  265. has_pointer_math : boolean;
  266. constructor create(def:tdef);virtual;
  267. { returns a pointerdef for def, reusing an existing one in case it
  268. exists in the current module }
  269. class function getreusable(def: tdef): tpointerdef; virtual;
  270. { same as above, but in case the def must never be freed after the
  271. current module has been compiled -- even if the def was not written
  272. to the ppu file (for defs in para locations, as we don't reset them
  273. so we don't have to recalculate them all the time) }
  274. class function getreusable_no_free(def: tdef): tpointerdef;
  275. function size:asizeint;override;
  276. function getcopy:tstoreddef;override;
  277. constructor ppuload(ppufile:tcompilerppufile);
  278. { do not override this routine in platform-specific subclasses,
  279. override ppuwrite_platform instead }
  280. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  281. function GetTypeName:string;override;
  282. {# returns the appropriate int type for pointer arithmetic with the given pointer type.
  283. When adding or subtracting a number to/from a pointer, this function returns the
  284. int type to which that number has to be converted, before the operation can be performed.
  285. Normally, this is sinttype, except on i8086, where it takes into account the
  286. special i8086 pointer types (near, far, huge). }
  287. function pointer_arithmetic_int_type:tdef;virtual;
  288. {# the unsigned version of pointer_arithmetic_int_type. Used with inc/dec. }
  289. function pointer_arithmetic_uint_type:tdef;virtual;
  290. {# returns the int type produced when subtracting two pointers of the given type.
  291. Normally, this is sinttype, except on i8086, where it takes into account the
  292. special i8086 pointer types (near, far, huge). }
  293. function pointer_subtraction_result_type:tdef;virtual;
  294. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  295. {# the integer index type used to convert the pointer to array (i.e. denotes int_type in: ptr[int_type]) }
  296. function converted_pointer_to_array_range_type:tdef;virtual;
  297. end;
  298. tpointerdefclass = class of tpointerdef;
  299. tprocdef = class;
  300. tabstractrecorddef= class(tstoreddef)
  301. private
  302. rttistring : string;
  303. {$ifdef DEBUG_NODE_XML}
  304. protected
  305. procedure XMLPrintDefData(var T: Text; Sym: TSym); override;
  306. {$endif DEBUG_NODE_XML}
  307. public
  308. objname,
  309. objrealname : PShortString;
  310. { for C++ classes: name of the library this class is imported from }
  311. { for Java classes/records: package name }
  312. import_lib : PShortString;
  313. symtable : TSymtable;
  314. cloneddef : tabstractrecorddef;
  315. cloneddefderef : tderef;
  316. objectoptions : tobjectoptions;
  317. rtti : trtti_directive;
  318. { for targets that initialise typed constants via explicit assignments
  319. instead of by generating an initialised data sectino }
  320. tcinitcode : tnode;
  321. constructor create(const n:string; dt:tdeftyp;doregister:boolean);
  322. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  323. procedure ppuwrite(ppufile:tcompilerppufile);override;
  324. destructor destroy; override;
  325. procedure buildderefimpl;override;
  326. procedure derefimpl;override;
  327. procedure check_forwards; virtual;
  328. function find_procdef_bytype(pt:tproctypeoption): tprocdef;
  329. function GetSymtable(t:tGetSymtable):TSymtable;override;
  330. function is_packed:boolean;
  331. function RttiName: string;
  332. { enumerator support }
  333. function search_enumerator_get: tprocdef; virtual;
  334. function search_enumerator_move: tprocdef; virtual;
  335. function search_enumerator_current: tsym; virtual;
  336. { JVM }
  337. function jvm_full_typename(with_package_name: boolean): string;
  338. { check if the symtable contains a float field }
  339. function contains_float_field : boolean;
  340. { check if the symtable contains a field that spans an aword boundary }
  341. function contains_cross_aword_field: boolean;
  342. { extended RTTI }
  343. procedure apply_rtti_directive(dir: trtti_directive); virtual;
  344. function is_visible_for_rtti(option: trtti_option; vis: tvisibility): boolean; inline;
  345. function rtti_visibilities_for_option(option: trtti_option): tvisibilities; inline;
  346. function has_extended_rtti: boolean; inline;
  347. end;
  348. pvariantrecdesc = ^tvariantrecdesc;
  349. tvariantrecbranch = record
  350. { we store only single values here and no ranges because tvariantrecdesc is only needed in iso mode
  351. which does not support range expressions in variant record definitions }
  352. values : array of Tconstexprint;
  353. nestedvariant : pvariantrecdesc;
  354. end;
  355. ppvariantrecdesc = ^pvariantrecdesc;
  356. tvariantrecdesc = record
  357. variantselector : tsym;
  358. variantselectorderef : tderef;
  359. branches : array of tvariantrecbranch;
  360. end;
  361. trecorddef = class(tabstractrecorddef)
  362. {$ifdef DEBUG_NODE_XML}
  363. protected
  364. function XMLPrintType: ansistring; override;
  365. {$endif DEBUG_NODE_XML}
  366. public
  367. variantrecdesc : pvariantrecdesc;
  368. isunion : boolean;
  369. constructor create(const n:string; p:TSymtable);virtual;
  370. constructor create_global_internal(const n: string; packrecords, recordalignmin: shortint); virtual;
  371. constructor create_internal(const n: string; packrecords, recordalignmin: shortint; where: tsymtable); virtual;
  372. function add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  373. procedure add_fields_from_deflist(fieldtypes: tfplist);
  374. constructor ppuload(ppufile:tcompilerppufile);
  375. destructor destroy;override;
  376. function getcopy : tstoreddef;override;
  377. { do not override this routine in platform-specific subclasses,
  378. override ppuwrite_platform instead }
  379. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  380. procedure buildderef;override;
  381. procedure deref;override;
  382. function size:asizeint;override;
  383. function alignment : shortint;override;
  384. function padalignment: shortint;
  385. function GetTypeName:string;override;
  386. { debug }
  387. function needs_inittable : boolean;override;
  388. function needs_separate_initrtti:boolean;override;
  389. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  390. end;
  391. trecorddefclass = class of trecorddef;
  392. tobjectdef = class;
  393. { TImplementedInterface }
  394. TImplementedInterface = class
  395. private
  396. fIOffset : longint;
  397. function GetIOffset: longint;
  398. public
  399. IntfDef : tobjectdef;
  400. IntfDefDeref : tderef;
  401. IType : tinterfaceentrytype;
  402. VtblImplIntf : TImplementedInterface;
  403. NameMappings : TFPHashList;
  404. ProcDefs : TFPObjectList;
  405. ImplementsGetter : tsym;
  406. ImplementsGetterDeref : tderef;
  407. ImplementsField : tsym;
  408. constructor create(aintf: tobjectdef);virtual;
  409. constructor create_deref(intfd,getterd:tderef);virtual;
  410. destructor destroy; override;
  411. function getcopy:TImplementedInterface;
  412. procedure buildderef;
  413. procedure deref;
  414. procedure AddMapping(const origname, newname: string);
  415. function GetMapping(const origname: string):string;
  416. procedure AddImplProc(pd:tprocdef);
  417. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  418. property IOffset: longint read GetIOffset write fIOffset;
  419. end;
  420. timplementedinterfaceclass = class of timplementedinterface;
  421. { tvmtentry }
  422. tvmtentry = record
  423. procdef : tprocdef;
  424. procdefderef : tderef;
  425. visibility : tvisibility;
  426. end;
  427. pvmtentry = ^tvmtentry;
  428. { tobjectdef }
  429. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  430. pmvcallstaticinfo = ^tmvcallstaticinfo;
  431. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  432. tobjectdef = class(tabstractrecorddef)
  433. private
  434. fcurrent_dispid: longint;
  435. {$ifdef DEBUG_NODE_XML}
  436. protected
  437. function XMLPrintType: ansistring; override;
  438. procedure XMLPrintDefInfo(var T: Text; Sym: TSym); override;
  439. procedure XMLPrintDefData(var T: Text; Sym: TSym); override;
  440. {$endif DEBUG_NODE_XML}
  441. public
  442. childof : tobjectdef;
  443. childofderef : tderef;
  444. { for Object Pascal helpers }
  445. extendeddef : tdef;
  446. extendeddefderef: tderef;
  447. helpertype : thelpertype;
  448. { for Objective-C: protocols and classes can have the same name there }
  449. objextname : pshortstring;
  450. { to be able to have a variable vmt position }
  451. { and no vmt field for objects without virtuals }
  452. vmtentries : TFPList;
  453. vmcallstaticinfo : pmvcallstaticinfo;
  454. vmt_field : tsym;
  455. vmt_fieldderef : tderef;
  456. iidguid : pguid;
  457. iidstr : pshortstring;
  458. { store implemented interfaces defs and name mappings }
  459. ImplementedInterfaces : TFPObjectList;
  460. { number of abstract methods (used by JVM target to determine whether
  461. or not the class should be marked as abstract: must be done if 1 or
  462. more abstract methods) }
  463. abstractcnt : longint;
  464. writing_class_record_dbginfo,
  465. { a class of this type has been created in this module }
  466. created_in_current_module,
  467. { a loadvmtnode for this class has been created in this
  468. module, so if a classrefdef variable of this or a parent
  469. class is used somewhere to instantiate a class, then this
  470. class may be instantiated
  471. }
  472. maybe_created_in_current_module,
  473. { a "class of" this particular class has been created in
  474. this module
  475. }
  476. classref_created_in_current_module : boolean;
  477. objecttype : tobjecttyp;
  478. { for interfaces that can be invoked using Invoke(),
  479. this is the definition of the hidden class that is generated by the compiler.
  480. we need this definition to reference it in the RTTI.
  481. Since interfaces can inherit, so can these hidden classes,
  482. so we need to write this to the ppu to be able to reference the parents.
  483. }
  484. hiddenclassdef : tobjectdef;
  485. hiddenclassdefref : tderef;
  486. constructor create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);virtual;
  487. constructor ppuload(ppufile:tcompilerppufile);
  488. destructor destroy;override;
  489. function getcopy : tstoreddef;override;
  490. { do not override this routine in platform-specific subclasses,
  491. override ppuwrite_platform instead }
  492. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  493. function GetTypeName:string;override;
  494. procedure buildderef;override;
  495. procedure deref;override;
  496. procedure derefimpl;override;
  497. procedure copyvmtentries(objdef:tobjectdef);
  498. function getparentdef:tdef;override;
  499. function size : asizeint;override;
  500. function alignment:shortint;override;
  501. function vmtmethodoffset(index:longint):longint;
  502. function members_need_inittable : boolean;
  503. { this should be called when this class implements an interface }
  504. procedure prepareguid;
  505. function is_publishable : tpublishproperty;override;
  506. function needs_inittable : boolean;override;
  507. function needs_separate_initrtti : boolean;override;
  508. function has_non_trivial_init_child(check_parent:boolean):boolean;override;
  509. function rtti_mangledname(rt:trttitype):TSymStr;override;
  510. function is_unique_objpasdef: boolean;
  511. function vmt_mangledname : TSymStr;
  512. function vmt_def: trecorddef;
  513. procedure check_forwards; override;
  514. procedure insertvmt;
  515. function vmt_offset: asizeint;
  516. procedure set_parent(c : tobjectdef);
  517. function find_destructor: tprocdef;
  518. function implements_any_interfaces: boolean;
  519. function register_implemented_interface(intfdef:tobjectdef;useguid:boolean):timplementedinterface;
  520. { dispinterface support }
  521. function get_next_dispid: longint;
  522. { enumerator support }
  523. function search_enumerator_get: tprocdef; override;
  524. function search_enumerator_move: tprocdef; override;
  525. function search_enumerator_current: tsym; override;
  526. { WPO }
  527. procedure register_created_object_type;override;
  528. procedure register_maybe_created_object_type;
  529. procedure register_created_classref_type;
  530. procedure register_vmt_call(index:longint);
  531. { ObjC }
  532. procedure finish_objc_data;
  533. function check_objc_types: boolean;
  534. { C++ }
  535. procedure finish_cpp_data;
  536. procedure apply_rtti_directive(dir: trtti_directive); override;
  537. end;
  538. tobjectdefclass = class of tobjectdef;
  539. tclassrefdef = class(tabstractpointerdef)
  540. constructor create(def:tdef);virtual;
  541. constructor ppuload(ppufile:tcompilerppufile);
  542. { do not override this routine in platform-specific subclasses,
  543. override ppuwrite_platform instead }
  544. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  545. function getcopy:tstoreddef;override;
  546. function GetTypeName:string;override;
  547. function is_publishable : tpublishproperty;override;
  548. function rtti_mangledname(rt:trttitype):TSymStr;override;
  549. procedure register_created_object_type;override;
  550. end;
  551. tclassrefdefclass = class of tclassrefdef;
  552. tarraydef = class(tstoreddef)
  553. lowrange,
  554. highrange : asizeint;
  555. rangedef : tdef;
  556. rangedefderef : tderef;
  557. arrayoptions : tarraydefoptions;
  558. symtable : TSymtable;
  559. protected
  560. _elementdef : tdef;
  561. _elementdefderef : tderef;
  562. procedure setelementdef(def:tdef);
  563. class function getreusable_intern(def: tdef; elems: asizeint; const options: tarraydefoptions): tarraydef;
  564. public
  565. function elesize : asizeint;
  566. function elepackedbitsize : asizeint;
  567. function elecount : asizeuint;
  568. constructor create_from_pointer(def: tpointerdef);virtual;
  569. constructor create(l, h: asizeint; def: tdef);virtual;
  570. constructor create_vector(l,h:asizeint; def:tdef);
  571. constructor create_openarray;virtual;
  572. class function getreusable(def: tdef; elems: asizeint): tarraydef;
  573. class function getreusable_vector(def: tdef; elems: asizeint): tarraydef;
  574. { same as above, but in case the def must never be freed after the
  575. current module has been compiled -- even if the def was not written
  576. to the ppu file (for defs in para locations, as we don't reset them
  577. so we don't have to recalculate them all the time) }
  578. class function getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  579. class function getreusable_no_free_vector(def: tdef; elems: asizeint): tarraydef;
  580. constructor ppuload(ppufile:tcompilerppufile);
  581. destructor destroy; override;
  582. function getcopy : tstoreddef;override;
  583. { do not override this routine in platform-specific subclasses,
  584. override ppuwrite_platform instead }
  585. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  586. function GetTypeName:string;override;
  587. function getmangledparaname : TSymStr;override;
  588. procedure buildderef;override;
  589. procedure deref;override;
  590. function size : asizeint;override;
  591. function alignment : shortint;override;
  592. { returns the label of the range check string }
  593. function needs_inittable : boolean;override;
  594. function needs_separate_initrtti : boolean;override;
  595. property elementdef : tdef read _elementdef write setelementdef;
  596. function is_publishable : tpublishproperty;override;
  597. function is_hwvector: boolean;
  598. end;
  599. tarraydefclass = class of tarraydef;
  600. torddef = class(tstoreddef)
  601. low,high : TConstExprInt;
  602. ordtype : tordtype;
  603. constructor create(t : tordtype;v,b : TConstExprInt; doregister: boolean);virtual;
  604. constructor ppuload(ppufile:tcompilerppufile);
  605. function getcopy : tstoreddef;override;
  606. { do not override this routine in platform-specific subclasses,
  607. override ppuwrite_platform instead }
  608. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  609. function is_publishable : tpublishproperty;override;
  610. function GetTypeName:string;override;
  611. function alignment:shortint;override;
  612. procedure setsize;
  613. function packedbitsize: asizeint; override;
  614. function getvardef : longint;override;
  615. end;
  616. torddefclass = class of torddef;
  617. tfloatdef = class(tstoreddef)
  618. floattype : tfloattype;
  619. constructor create(t: tfloattype; doregister: boolean);virtual;
  620. constructor ppuload(ppufile:tcompilerppufile);
  621. function getcopy : tstoreddef;override;
  622. { do not override this routine in platform-specific subclasses,
  623. override ppuwrite_platform instead }
  624. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  625. function GetTypeName:string;override;
  626. function is_publishable : tpublishproperty;override;
  627. function alignment:shortint;override;
  628. function structalignment: shortint;override;
  629. procedure setsize;
  630. function getvardef:longint;override;
  631. end;
  632. tfloatdefclass = class of tfloatdef;
  633. { tabstractprocdef }
  634. tprocnameoption = (pno_showhidden, pno_proctypeoption, pno_paranames,
  635. pno_ownername, pno_noclassmarker, pno_noleadingdollar,
  636. pno_mangledname, pno_noparams, pno_prettynames);
  637. tprocnameoptions = set of tprocnameoption;
  638. tproccopytyp = (pc_normal,
  639. {$ifdef i8086}
  640. { the address in a far format }
  641. pc_far_address,
  642. { the offset part of the far address }
  643. pc_offset,
  644. {$endif i8086}
  645. { creates a procvardef describing only the code pointer
  646. of a method/netsted function/... }
  647. pc_address_only,
  648. { everything except for hidden parameters }
  649. pc_normal_no_hidden,
  650. { everything except for the parameters (because they
  651. might be moved over to the copy) }
  652. pc_normal_no_paras,
  653. { always creates a top-level function, removes all
  654. special parameters (self, vmt, parentfp, ...) }
  655. pc_bareproc
  656. );
  657. tcacheableproccopytyp = pc_normal..pc_address_only;
  658. tabstractprocdef = class(tstoreddef)
  659. { saves a definition to the return type }
  660. returndef : tdef;
  661. returndefderef : tderef;
  662. parast : TSymtable;
  663. paras : tparalist;
  664. proctypeoption : tproctypeoption;
  665. proccalloption : tproccalloption;
  666. procoptions : tprocoptions;
  667. callerargareasize,
  668. calleeargareasize: pint;
  669. {$ifdef m68k}
  670. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  671. {$endif}
  672. funcretloc : array[callerside..calleeside] of TCGPara;
  673. has_paraloc_info : tcallercallee; { paraloc info is available }
  674. { number of user visible parameters }
  675. maxparacount,
  676. minparacount : byte;
  677. constructor create(dt:tdeftyp;level:byte;doregister:boolean);
  678. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  679. destructor destroy;override;
  680. procedure ppuwrite(ppufile:tcompilerppufile);override;
  681. procedure buildderef;override;
  682. procedure deref;override;
  683. procedure calcparas;
  684. function mangledprocparanames(oldlen : longint) : string;
  685. function typename_paras(pno: tprocnameoptions): ansistring;
  686. function is_methodpointer:boolean;virtual;
  687. function is_addressonly:boolean;virtual;
  688. function no_self_node:boolean;
  689. { get either a copy as a procdef or procvardef }
  690. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef; virtual;
  691. function compatible_with_pointerdef_size(ptr: tpointerdef): boolean; virtual;
  692. procedure check_mark_as_nested;
  693. procedure init_paraloc_info(side: tcallercallee);
  694. procedure done_paraloc_info(side: tcallercallee);
  695. function stack_tainting_parameter(side: tcallercallee): boolean;
  696. function is_pushleftright: boolean;virtual;
  697. function address_type:tdef;virtual;
  698. { address type, generated for ofs() }
  699. function ofs_address_type:tdef;virtual;
  700. procedure declared_far;virtual;
  701. procedure declared_near;virtual;
  702. function generate_safecall_wrapper: boolean; virtual;
  703. { returns true if the def is a generic param of the procdef }
  704. function is_generic_param(def:tdef): boolean;
  705. function wpo_may_create_instance(optionalmethodpointer: tnode): boolean;
  706. private
  707. procedure count_para(p:TObject;arg:pointer);
  708. procedure insert_para(p:TObject;arg:pointer);
  709. end;
  710. tprocvardef = class(tabstractprocdef)
  711. constructor create(level:byte;doregister:boolean);virtual;
  712. { returns a procvardef that represents the address of a proc(var)def }
  713. class function getreusableprocaddr(def: tabstractprocdef; copytyp: tcacheableproccopytyp): tprocvardef; virtual;
  714. { same as above, but in case the def must never be freed after the
  715. current module has been compiled -- even if the def was not written
  716. to the ppu file (for defs in para locations, as we don't reset them
  717. so we don't have to recalculate them all the time) }
  718. class function getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  719. constructor ppuload(ppufile:tcompilerppufile);
  720. function getcopy : tstoreddef;override;
  721. { do not override this routine in platform-specific subclasses,
  722. override ppuwrite_platform instead }
  723. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  724. function GetSymtable(t:tGetSymtable):TSymtable;override;
  725. function size : asizeint;override;
  726. function GetTypeName:string;override;
  727. function is_publishable : tpublishproperty;override;
  728. function is_methodpointer:boolean;override;
  729. function is_addressonly:boolean;override;
  730. function getmangledparaname:TSymStr;override;
  731. function getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef; override;
  732. end;
  733. tprocvardefclass = class of tprocvardef;
  734. tmessageinf = record
  735. case integer of
  736. 0 : (str : pshortstring);
  737. 1 : (i : longint);
  738. end;
  739. tinlininginfo = record
  740. { node tree }
  741. code : tnode;
  742. flags : tprocinfoflags;
  743. end;
  744. pinlininginfo = ^tinlininginfo;
  745. tcapturedsyminfo = record
  746. sym : tsym;
  747. def : tdef;
  748. { the location where the symbol was first encountered }
  749. fileinfo : tfileposinfo;
  750. end;
  751. pcapturedsyminfo = ^tcapturedsyminfo;
  752. timplprocdefinfo = record
  753. resultname : pshortstring;
  754. parentfpstruct: tsym;
  755. parentfpstructptrtype: tdef;
  756. parentfpinitblock: tnode;
  757. procstarttai,
  758. procendtai : tai;
  759. skpara: pointer;
  760. personality: tprocdef;
  761. was_anonymous,
  762. has_capturer,
  763. forwarddef,
  764. interfacedef : boolean;
  765. hasforward : boolean;
  766. is_implemented : boolean;
  767. capturedsyms : tfplist;
  768. end;
  769. pimplprocdefinfo = ^timplprocdefinfo;
  770. { tprocdef }
  771. tprocdef = class(tabstractprocdef)
  772. protected
  773. {$ifdef symansistr}
  774. _mangledname : ansistring;
  775. {$else symansistr}
  776. _mangledname : pshortstring;
  777. {$endif}
  778. _parentfpsym : tsym;
  779. { information that is only required until the implementation of the
  780. procdef has been handled }
  781. implprocdefinfo : pimplprocdefinfo;
  782. function store_localst:boolean;
  783. function GetResultName: PShortString;
  784. procedure SetResultName(AValue: PShortString);
  785. function GetParentFPStruct: tsym;
  786. procedure SetParentFPStruct(AValue: tsym);
  787. function GetParentFPStructPtrType: tdef;
  788. procedure SetParentFPStructPtrType(AValue: tdef);
  789. function GetParentFPInitBlock: tnode;
  790. procedure SetParentFPInitBlock(AValue: tnode);
  791. function Getprocstarttai: tai;
  792. procedure Setprocstarttai(AValue: tai);
  793. function Getprocendtai: tai;
  794. procedure Setprocendtai(AValue: tai);
  795. function Getskpara: pointer;
  796. procedure Setskpara(AValue: pointer);
  797. function Getpersonality: tprocdef;
  798. procedure Setpersonality(AValue: tprocdef);
  799. function Getforwarddef: boolean;
  800. procedure Setforwarddef(AValue: boolean);
  801. function Getinterfacedef: boolean;
  802. procedure Setinterfacedef(AValue: boolean);virtual;
  803. function Gethasforward: boolean;
  804. procedure Sethasforward(AValue: boolean);
  805. function GetIsEmpty: boolean;
  806. procedure SetIsEmpty(AValue: boolean);
  807. function GetHasInliningInfo: boolean;
  808. procedure SetHasInliningInfo(AValue: boolean);
  809. function Getis_implemented: boolean;
  810. procedure Setis_implemented(AValue: boolean);
  811. function getwas_anonymous:boolean;
  812. procedure setwas_anonymous(avalue:boolean);
  813. function gethas_capturer:boolean;
  814. procedure sethas_capturer(avalue:boolean);
  815. function Getcapturedsyms:tfplist;
  816. function getparentfpsym: tsym;
  817. public
  818. messageinf : tmessageinf;
  819. dispid : longint;
  820. {$ifndef EXTDEBUG}
  821. { where is this function defined and what were the symbol
  822. flags, needed here because there
  823. is only one symbol for all overloaded functions
  824. EXTDEBUG has fileinfo in tdef (PFV) }
  825. fileinfo : tfileposinfo;
  826. {$endif}
  827. symoptions : tsymoptions;
  828. deprecatedmsg : pshortstring;
  829. { generic support }
  830. genericdecltokenbuf : tdynamicarray;
  831. { symbol owning this definition }
  832. procsym : tsym;
  833. procsymderef : tderef;
  834. { alias names }
  835. aliasnames : TCmdStrList;
  836. { symtables }
  837. localst : TSymtable;
  838. funcretsym : tsym;
  839. funcretsymderef : tderef;
  840. struct : tabstractrecorddef;
  841. structderef : tderef;
  842. implprocoptions: timplprocoptions;
  843. { import info }
  844. import_dll,
  845. import_name : pshortstring;
  846. { info for inlining the subroutine, if this pointer is nil,
  847. the procedure can't be inlined }
  848. inlininginfo : pinlininginfo;
  849. import_nr : word;
  850. extnumber : word;
  851. { set to a value different from tsk_none in case this procdef is for
  852. a routine that has to be internally generated by the compiler }
  853. synthetickind: tsynthetickind;
  854. visibility : tvisibility;
  855. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  856. seenmarker : pointer; // used for filtering in tcandidate
  857. {$endif}
  858. {$ifdef symansistr}
  859. section: ansistring;
  860. {$else symansistr}
  861. section: pshortstring;
  862. {$endif}
  863. { only needed when actually compiling a unit, no need to save/load from ppu }
  864. invoke_helper : tprocdef;
  865. copied_from : tprocdef;
  866. constructor create(level:byte;doregister:boolean);virtual;
  867. constructor ppuload(ppufile:tcompilerppufile);
  868. destructor destroy;override;
  869. procedure freeimplprocdefinfo;
  870. { do not override this routine in platform-specific subclasses,
  871. override ppuwrite_platform instead }
  872. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  873. procedure buildderef;override;
  874. procedure buildderefimpl;override;
  875. procedure deref;override;
  876. procedure derefimpl;override;
  877. function GetSymtable(t:tGetSymtable):TSymtable;override;
  878. { warnings:
  879. * the symtablestack top has to be the symtable to which the copy
  880. should be added
  881. * getcopy does not create a finished/ready-to-use procdef; it
  882. needs to be finalised afterwards by calling
  883. symcreat.finish_copied_procdef() afterwards
  884. }
  885. function getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef; override;
  886. function getcopy: tstoreddef; override;
  887. function GetTypeName : string;override;
  888. function mangledname : TSymStr; virtual;
  889. procedure setmangledname(const s : TSymStr);
  890. function needsglobalasmsym: boolean;
  891. procedure setcompilerprocname;
  892. function fullprocname(showhidden:boolean):string;
  893. function customprocname(pno: tprocnameoptions):ansistring;
  894. function defaultmangledname: TSymStr;
  895. function cplusplusmangledname : TSymStr;
  896. function objcmangledname : TSymStr;
  897. function is_methodpointer:boolean;override;
  898. function is_addressonly:boolean;override;
  899. procedure make_external;
  900. procedure init_genericdecl;
  901. function get_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean; virtual;
  902. function get_safecall_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean; virtual;
  903. procedure add_captured_sym(sym:tsym;def:tdef;const filepos:tfileposinfo);
  904. { returns whether the mangled name or any of its aliases is equal to
  905. s }
  906. function has_alias_name(const s: TSymStr):boolean;
  907. { aliases to fields only required when a function is implemented in
  908. the current unit }
  909. property resultname: PShortString read GetResultName write SetResultName;
  910. { temporary reference to structure containing copies of all local
  911. variables and parameters accessed by nested routines; reference to
  912. this structure is passed as "parent frame pointer" on targets that
  913. lack this concept (at least JVM and LLVM); no need to save to/
  914. restore from ppu, since nested routines are always in the same
  915. unit }
  916. property parentfpstruct: tsym read GetParentFPStruct write SetParentFPStruct;
  917. { pointer to parentfpstruct's type (not yet valid during parsing, so
  918. cannot be used for $parentfp parameter) (no need to save to ppu) }
  919. property parentfpstructptrtype: tdef read GetParentFPStructPtrType write SetParentFPStructPtrType;
  920. { code to copy the parameters accessed from nested routines into the
  921. parentfpstruct (no need to save to ppu) }
  922. property parentfpinitblock: tnode read GetParentFPInitBlock write SetParentFPInitBlock;
  923. { First/last assembler symbol/instruction in aasmoutput list.
  924. Note: initialised after compiling the code for the procdef, but
  925. not saved to/restored from ppu. Used when inserting debug info }
  926. property procstarttai: tai read Getprocstarttai write Setprocstarttai;
  927. property procendtai: tai read Getprocendtai write Setprocendtai;
  928. { optional parameter for the synthetic routine generation logic }
  929. property skpara: pointer read Getskpara write Setskpara;
  930. { ABI-conformant exception handling personality function }
  931. property personality: tprocdef read Getpersonality write Setpersonality;
  932. { true, if the procedure is only declared
  933. (forward procedure) }
  934. property forwarddef: boolean read Getforwarddef write Setforwarddef;
  935. { true if the procedure is declared in the interface }
  936. property interfacedef: boolean read Getinterfacedef write Setinterfacedef;
  937. { true if the procedure has a forward declaration }
  938. property hasforward: boolean read Gethasforward write Sethasforward;
  939. { true if the routine's body is empty }
  940. property isempty: boolean read GetIsEmpty write SetIsEmpty;
  941. { true if all information required to inline this routine is available }
  942. property has_inlininginfo: boolean read GetHasInliningInfo write SetHasInliningInfo;
  943. { returns the $parentfp parameter for nested routines }
  944. property parentfpsym: tsym read getparentfpsym;
  945. { true if the implementation part for this procdef has been handled }
  946. property is_implemented: boolean read Getis_implemented write Setis_implemented;
  947. { valid if the procdef captures any symbols from outer scopes }
  948. property capturedsyms:tfplist read Getcapturedsyms;
  949. { true if this procdef was originally an anonymous function }
  950. property was_anonymous:boolean read getwas_anonymous write setwas_anonymous;
  951. { true if the procdef has a capturer for anonymous functions }
  952. property has_capturer:boolean read gethas_capturer write sethas_capturer;
  953. end;
  954. tprocdefclass = class of tprocdef;
  955. { single linked list of overloaded procs }
  956. pprocdeflist = ^tprocdeflist;
  957. tprocdeflist = record
  958. def : tprocdef;
  959. defderef : tderef;
  960. next : pprocdeflist;
  961. end;
  962. tstringdef = class(tstoreddef)
  963. encoding : tstringencoding;
  964. stringtype : tstringtype;
  965. len : asizeint;
  966. constructor createshort(l: byte; doregister: boolean);virtual;
  967. constructor loadshort(ppufile:tcompilerppufile);
  968. constructor createlong(l: asizeint; doregister: boolean);virtual;
  969. constructor loadlong(ppufile:tcompilerppufile);
  970. constructor createansi(aencoding: tstringencoding; doregister: boolean);virtual;
  971. constructor loadansi(ppufile:tcompilerppufile);
  972. constructor createwide(doregister: boolean);virtual;
  973. constructor loadwide(ppufile:tcompilerppufile);
  974. constructor createunicode(doregister: boolean);virtual;
  975. constructor loadunicode(ppufile:tcompilerppufile);virtual;
  976. function getcopy : tstoreddef;override;
  977. function stringtypname:string;
  978. { do not override this routine in platform-specific subclasses,
  979. override ppuwrite_platform instead }
  980. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  981. function GetTypeName:string;override;
  982. function getmangledparaname:TSymStr;override;
  983. function is_publishable : tpublishproperty;override;
  984. function size:asizeint;override;
  985. function alignment : shortint;override;
  986. function needs_inittable : boolean;override;
  987. function getvardef:longint;override;
  988. { returns the default char type def for the string }
  989. function get_default_char_type: tdef;
  990. { returns the default string type }
  991. function get_default_string_type: tdef;
  992. end;
  993. tstringdefclass = class of tstringdef;
  994. tenumdef = class(tstoreddef)
  995. minval,
  996. maxval : asizeint;
  997. basedef : tenumdef;
  998. basedefderef : tderef;
  999. symtable : TSymtable;
  1000. has_jumps : boolean;
  1001. constructor create;virtual;
  1002. constructor create_subrange(_basedef:tenumdef;_min,_max:asizeint);virtual;
  1003. constructor ppuload(ppufile:tcompilerppufile);
  1004. destructor destroy;override;
  1005. function getcopy : tstoreddef;override;
  1006. { do not override this routine in platform-specific subclasses,
  1007. override ppuwrite_platform instead }
  1008. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  1009. procedure buildderef;override;
  1010. procedure deref;override;
  1011. function GetTypeName:string;override;
  1012. function is_publishable : tpublishproperty;override;
  1013. procedure calcsavesize(packenum: shortint);
  1014. function packedbitsize: asizeint; override;
  1015. procedure setmax(_max:asizeint);
  1016. procedure setmin(_min:asizeint);
  1017. function min:asizeint;
  1018. function max:asizeint;
  1019. function getfirstsym:tsym;
  1020. function int2enumsym(l: asizeint): tsym;
  1021. { returns basedef if assigned, otherwise self }
  1022. function getbasedef: tenumdef;
  1023. end;
  1024. tenumdefclass = class of tenumdef;
  1025. tsetdef = class(tstoreddef)
  1026. elementdef : tdef;
  1027. elementdefderef : tderef;
  1028. setbase,
  1029. setmax : asizeint;
  1030. constructor create(def: tdef; low, high: asizeint; doregister: boolean);virtual;
  1031. constructor ppuload(ppufile:tcompilerppufile);
  1032. function getcopy : tstoreddef;override;
  1033. { do not override this routine in platform-specific subclasses,
  1034. override ppuwrite_platform instead }
  1035. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  1036. procedure buildderef;override;
  1037. procedure deref;override;
  1038. function GetTypeName:string;override;
  1039. function is_publishable : tpublishproperty;override;
  1040. function alignment: shortint; override;
  1041. end;
  1042. tsetdefclass = class of tsetdef;
  1043. tgenericdummyentry = class
  1044. dummysym : tsym;
  1045. resolvedsym : tsym;
  1046. end;
  1047. tdefawaresymtablestack = class(TSymtablestack)
  1048. private
  1049. procedure add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1050. procedure remove_helpers_and_generics(st:tsymtable);inline;
  1051. procedure remove_helpers(st:tsymtable);
  1052. procedure remove_generics(st:tsymtable);
  1053. procedure pushcommon(st:tsymtable);inline;
  1054. public
  1055. procedure push(st: TSymtable); override;
  1056. procedure pushafter(st,afterst:TSymtable); override;
  1057. procedure pop(st: TSymtable); override;
  1058. end;
  1059. var
  1060. current_structdef: tabstractrecorddef; { used for private functions check !! }
  1061. current_genericdef: tstoreddef; { used to reject declaration of generic class inside generic class }
  1062. current_specializedef: tstoreddef; { used to implement usage of generic class in itself }
  1063. cfiledef: tfiledefclass;
  1064. cvariantdef: tvariantdefclass;
  1065. cformaldef: tformaldefclass;
  1066. cforwarddef: tforwarddefclass;
  1067. cundefineddef: tundefineddefclass;
  1068. cerrordef: terrordefclass;
  1069. cpointerdef: tpointerdefclass;
  1070. crecorddef: trecorddefclass;
  1071. cimplementedinterface: timplementedinterfaceclass;
  1072. cobjectdef: tobjectdefclass;
  1073. cclassrefdef: tclassrefdefclass;
  1074. carraydef: tarraydefclass;
  1075. corddef: torddefclass;
  1076. cfloatdef: tfloatdefclass;
  1077. cprocvardef: tprocvardefclass;
  1078. cprocdef: tprocdefclass;
  1079. cstringdef: tstringdefclass;
  1080. cenumdef: tenumdefclass;
  1081. csetdef: tsetdefclass;
  1082. { default types }
  1083. generrordef, { error in definition }
  1084. voidpointertype, { pointer for Void-pointeddef }
  1085. charpointertype, { pointer for Char-pointeddef }
  1086. widecharpointertype, { pointer for WideChar-pointeddef }
  1087. voidcodepointertype, { pointer to code; corresponds to System.CodePointer }
  1088. voidstackpointertype, { the pointer type used for accessing parameters and local vars on the stack }
  1089. parentfpvoidpointertype, { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  1090. {$ifdef x86}
  1091. voidnearpointertype,
  1092. voidnearcspointertype,
  1093. voidneardspointertype,
  1094. voidnearsspointertype,
  1095. voidnearespointertype,
  1096. voidnearfspointertype,
  1097. voidneargspointertype,
  1098. {$ifdef i8086}
  1099. voidfarpointertype,
  1100. voidhugepointertype,
  1101. charnearpointertype,
  1102. charfarpointertype,
  1103. charhugepointertype,
  1104. bytefarpointertype, { used for Mem[] }
  1105. wordfarpointertype, { used for MemW[] }
  1106. longintfarpointertype, { used for MemL[] }
  1107. {$endif i8086}
  1108. {$endif x86}
  1109. {$ifdef wasm}
  1110. wasmvoidexternreftype,
  1111. {$endif wasm}
  1112. cundefinedtype,
  1113. cformaltype, { unique formal definition }
  1114. ctypedformaltype, { unique typed formal definition }
  1115. voidtype, { Void (procedure) }
  1116. cansichartype, { Char }
  1117. cwidechartype, { WideChar }
  1118. cchartype, { either cansichartype or cwidechartype. Do not free }
  1119. pasbool1type, { boolean type }
  1120. pasbool8type,
  1121. pasbool16type,
  1122. pasbool32type,
  1123. pasbool64type,
  1124. bool8type,
  1125. bool16type,
  1126. bool32type,
  1127. bool64type, { implement me }
  1128. {$ifdef llvm}
  1129. llvmbool1type, { LLVM i1 type }
  1130. {$endif llvm}
  1131. u8inttype, { 8-Bit unsigned integer }
  1132. s8inttype, { 8-Bit signed integer }
  1133. u16inttype, { 16-Bit unsigned integer }
  1134. s16inttype, { 16-Bit signed integer }
  1135. u24inttype, { 24-Bit unsigned integer }
  1136. s24inttype, { 24-Bit signed integer }
  1137. u32inttype, { 32-Bit unsigned integer }
  1138. s32inttype, { 32-Bit signed integer }
  1139. u40inttype, { 40-Bit unsigned integer }
  1140. s40inttype, { 40-Bit signed integer }
  1141. u48inttype, { 48-Bit unsigned integer }
  1142. s48inttype, { 48-Bit signed integer }
  1143. u56inttype, { 56-Bit unsigned integer }
  1144. s56inttype, { 56-Bit signed integer }
  1145. u64inttype, { 64-bit unsigned integer }
  1146. s64inttype, { 64-bit signed integer }
  1147. u128inttype, { 128-bit unsigned integer }
  1148. s128inttype, { 128-bit signed integer }
  1149. s32floattype, { 32 bit floating point number }
  1150. s64floattype, { 64 bit floating point number }
  1151. s80floattype, { 80 bit floating point number }
  1152. sc80floattype, { 80 bit floating point number but stored like in C }
  1153. s64currencytype, { pointer to a currency type }
  1154. cshortstringtype, { pointer to type of short string const }
  1155. clongstringtype, { pointer to type of long string const }
  1156. cansistringtype, { pointer to type of ansi string const }
  1157. cwidestringtype, { pointer to type of wide string const }
  1158. cunicodestringtype,
  1159. openshortstringtype, { pointer to type of an open shortstring,
  1160. needed for readln() }
  1161. openchararraytype, { pointer to type of an open array of char,
  1162. needed for readln() }
  1163. cfiletype, { get the same definition for all file }
  1164. { used for stabs }
  1165. methodpointertype, { typecasting of methodpointers to extract self }
  1166. nestedprocpointertype, { typecasting of nestedprocpointers to extract parentfp }
  1167. hresultdef,
  1168. typekindtype, { def of TTypeKind for correct handling of GetTypeKind parameters }
  1169. { we use only one variant def for every variant class }
  1170. cvarianttype,
  1171. colevarianttype,
  1172. { default integer type, normally s32inttype on 32 bit systems and s64bittype on 64 bit systems }
  1173. sinttype,
  1174. uinttype,
  1175. { integer types corresponding to OS_SINT/OS_INT }
  1176. ossinttype,
  1177. osuinttype,
  1178. { integer types corresponding to the ALU size, sizeof(aint) and the ALUSInt/ALUUInt types in the system unit }
  1179. alusinttype,
  1180. aluuinttype,
  1181. { integer types corresponding to SizeInt and SizeUInt for the target platform }
  1182. sizeuinttype,
  1183. sizesinttype,
  1184. { unsigned and signed ord type with the same size as a pointer }
  1185. ptruinttype,
  1186. ptrsinttype,
  1187. { unsigned and signed ord type with the same size as a codepointer }
  1188. codeptruinttype,
  1189. codeptrsinttype,
  1190. { several types to simulate more or less C++ objects for GDB }
  1191. vmttype,
  1192. vmtarraytype,
  1193. { type of classrefs, used for stabs }
  1194. pvmttype,
  1195. { return type of the setjmp function }
  1196. exceptionreasontype : tdef;
  1197. { pointer to the anchestor of all classes }
  1198. class_tobject : tobjectdef;
  1199. { pointer to the base type for custom attributes }
  1200. class_tcustomattribute : tobjectdef;
  1201. { pointer to the ancestor of all COM interfaces }
  1202. interface_iunknown : tobjectdef;
  1203. { pointer to the ancestor of all dispinterfaces }
  1204. interface_idispatch : tobjectdef;
  1205. { pointer to the TGUID type
  1206. of all interfaces }
  1207. rec_tguid : trecorddef;
  1208. { jump buffer type, used by setjmp }
  1209. rec_jmp_buf : trecorddef;
  1210. { system.texceptaddr type, used by fpc_pushexceptaddr }
  1211. rec_exceptaddr: trecorddef;
  1212. { Objective-C base types }
  1213. objc_metaclasstype,
  1214. objc_superclasstype,
  1215. objc_idtype,
  1216. objc_seltype : tpointerdef;
  1217. objc_objecttype : trecorddef;
  1218. { base type of @protocol(protocolname) Objective-C statements }
  1219. objc_protocoltype : tobjectdef;
  1220. { helper types for for-in "fast enumeration" support in Objective-C 2.0 }
  1221. objc_fastenumeration : tobjectdef;
  1222. objc_fastenumerationstate : trecorddef;
  1223. {$ifdef llvm}
  1224. { llvm types }
  1225. { a unique def to identify any kind of metadata }
  1226. llvm_metadatatype : tdef;
  1227. {$endif llvm}
  1228. { Java base types }
  1229. { java.lang.Object }
  1230. java_jlobject : tobjectdef;
  1231. { java.lang.Throwable }
  1232. java_jlthrowable : tobjectdef;
  1233. { FPC base type for records }
  1234. java_fpcbaserecordtype : tobjectdef;
  1235. { java.lang.String }
  1236. java_jlstring : tobjectdef;
  1237. { java.lang.Enum }
  1238. java_jlenum : tobjectdef;
  1239. { java.util.EnumSet }
  1240. java_juenumset : tobjectdef;
  1241. { java.util.BitSet }
  1242. java_jubitset : tobjectdef;
  1243. { FPC java implementation of ansistrings }
  1244. java_ansistring : tobjectdef;
  1245. { FPC java implementation of shortstrings }
  1246. java_shortstring : tobjectdef;
  1247. { FPC java procvar base class }
  1248. java_procvarbase : tobjectdef;
  1249. { x86 vector types }
  1250. x86_m64type,
  1251. x86_m128type,
  1252. x86_m128dtype,
  1253. x86_m128itype,
  1254. x86_m256type,
  1255. x86_m256dtype,
  1256. x86_m256itype : tdef;
  1257. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1258. function make_dllmangledname(const dllname,importname:TSymStr;
  1259. import_nr : word; pco : tproccalloption):TSymStr;
  1260. { should be in the types unit, but the types unit uses the node stuff :( }
  1261. function is_interfacecom(def: tdef): boolean;
  1262. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  1263. function is_any_interface_kind(def: tdef): boolean;
  1264. function is_interfacecorba(def: tdef): boolean;
  1265. function is_interface(def: tdef): boolean;
  1266. function is_dispinterface(def: tdef): boolean;
  1267. function is_object(def: tdef): boolean;
  1268. function is_class(def: tdef): boolean;
  1269. function is_cppclass(def: tdef): boolean;
  1270. function is_objectpascal_helper(def: tdef): boolean;
  1271. function is_objcclass(def: tdef): boolean;
  1272. function is_objcclassref(def: tdef): boolean;
  1273. function is_objcprotocol(def: tdef): boolean;
  1274. function is_objccategory(def: tdef): boolean;
  1275. function is_objc_class_or_protocol(def: tdef): boolean;
  1276. function is_objc_protocol_or_category(def: tdef): boolean;
  1277. function is_classhelper(def: tdef): boolean;
  1278. function is_class_or_interface(def: tdef): boolean;
  1279. function is_class_or_interface_or_objc(def: tdef): boolean;
  1280. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  1281. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  1282. function is_class_or_interface_or_object(def: tdef): boolean;
  1283. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  1284. function is_implicit_pointer_object_type(def: tdef): boolean;
  1285. { returns true, if def is a type which is an implicit pointer to an array (dyn. array or dyn. string) }
  1286. function is_implicit_array_pointer(def: tdef): boolean;
  1287. function is_class_or_object(def: tdef): boolean;
  1288. function is_record(def: tdef): boolean;
  1289. function is_javaclass(def: tdef): boolean;
  1290. function is_javaclassref(def: tdef): boolean;
  1291. function is_javainterface(def: tdef): boolean;
  1292. function is_java_class_or_interface(def: tdef): boolean;
  1293. procedure loadobjctypes;
  1294. procedure maybeloadcocoatypes;
  1295. function use_vectorfpu(def : tdef) : boolean;
  1296. function getansistringcodepage:tstringencoding; inline;
  1297. function getansistringdef:tstringdef;
  1298. function getparaencoding(def:tdef):tstringencoding; inline;
  1299. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1300. function get_recorddef(prefix:tinternaltypeprefix;const fields:array of tdef; packrecords:shortint): trecorddef;
  1301. { get a table def of the form
  1302. record
  1303. count: countdef;
  1304. elements: array[0..count-1] of elementdef
  1305. end;
  1306. Returns both the outer record and the inner arraydef
  1307. }
  1308. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1309. function fileinfo_of_typesym_in_def(def:tdef;sym:tsym;out filepos:tfileposinfo):boolean;
  1310. procedure reset_all_default_types; {housekeeping for Textmode IDE sanity}
  1311. implementation
  1312. uses
  1313. SysUtils,
  1314. cutils,
  1315. { global }
  1316. verbose,
  1317. { target }
  1318. systems,paramgr,
  1319. { symtable }
  1320. symsym,symtable,defutil,objcdef,
  1321. { parser }
  1322. pgenutil,
  1323. { module }
  1324. fmodule,ppu,
  1325. { other }
  1326. aasmbase,
  1327. gendef,
  1328. fpchash,
  1329. entfile
  1330. ;
  1331. {****************************************************************************
  1332. Helpers
  1333. ****************************************************************************}
  1334. function fileinfo_of_typesym_in_def(def:tdef;sym:tsym;out filepos:tfileposinfo):boolean;
  1335. var
  1336. gst : tgetsymtable;
  1337. st : tsymtable;
  1338. i : longint;
  1339. srsym : tsym;
  1340. begin
  1341. result:=false;
  1342. if sym.typ<>typesym then
  1343. exit;
  1344. for gst:=low(tgetsymtable) to high(tgetsymtable) do
  1345. begin
  1346. st:=def.getsymtable(gst);
  1347. if not assigned(st) then
  1348. continue;
  1349. for i:=0 to st.symlist.count-1 do
  1350. begin
  1351. srsym:=tsym(st.symlist[i]);
  1352. case srsym.typ of
  1353. fieldvarsym,
  1354. paravarsym:
  1355. if tabstractvarsym(srsym).vardef.typesym=sym then
  1356. begin
  1357. filepos:=srsym.fileinfo;
  1358. result:=true;
  1359. break;
  1360. end;
  1361. typesym:
  1362. begin
  1363. result:=fileinfo_of_typesym_in_def(ttypesym(srsym).typedef,sym,filepos);
  1364. if result then
  1365. break;
  1366. end
  1367. else
  1368. ;
  1369. end;
  1370. end;
  1371. if result then
  1372. break;
  1373. end;
  1374. { for this we would need the position of the result declaration :/ }
  1375. {if not result and
  1376. (def.typ in [procdef,procvardef]) and
  1377. assigned(tabstractprocdef(def).returndef) and
  1378. (tabstractprocdef(def).returndef.typesym=sym) then
  1379. begin
  1380. result:=true;
  1381. end;}
  1382. end;
  1383. function getansistringcodepage:tstringencoding; inline;
  1384. begin
  1385. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1386. result:=current_settings.sourcecodepage
  1387. else
  1388. result:=0;
  1389. end;
  1390. function getansistringdef:tstringdef;
  1391. var
  1392. symtable:tsymtable;
  1393. oldstack : tsymtablestack;
  1394. begin
  1395. { if a codepage is explicitly defined in this mudule we need to return
  1396. a replacement for ansistring def }
  1397. if ([cs_explicit_codepage,cs_system_codepage]*current_settings.moduleswitches)<>[] then
  1398. begin
  1399. if not assigned(current_module) then
  1400. internalerror(2011101301);
  1401. { codepage can be redeclared only once per unit so we don't need a list of
  1402. redefined ansistring but only one pointer }
  1403. if not assigned(current_module.ansistrdef) then
  1404. begin
  1405. { if we did not create it yet we need to do this now }
  1406. if current_module.in_interface then
  1407. symtable:=current_module.globalsymtable
  1408. else
  1409. symtable:=current_module.localsymtable;
  1410. { create a temporary stack as it's not good (TM) to mess around
  1411. with the order if the unit contains generics or helpers; don't
  1412. use a def aware symtablestack though }
  1413. oldstack:=symtablestack;
  1414. symtablestack:=tsymtablestack.create;
  1415. symtablestack.push(symtable);
  1416. current_module.ansistrdef:=cstringdef.createansi(current_settings.sourcecodepage,true);
  1417. symtablestack.pop(symtable);
  1418. symtablestack.free;
  1419. symtablestack:=oldstack;
  1420. end;
  1421. result:=tstringdef(current_module.ansistrdef);
  1422. end
  1423. else
  1424. result:=tstringdef(cansistringtype);
  1425. end;
  1426. function getparaencoding(def:tdef):tstringencoding; inline;
  1427. begin
  1428. { don't pass CP_NONE encoding to internal functions
  1429. they expect 0 encoding instead
  1430. exception: result of string concatenation, because if you pass the
  1431. result of a string concatenation to a rawbytestring, the result of
  1432. that concatenation shouldn't be converted to defaultsystemcodepage
  1433. if all strings have the same type }
  1434. result:=tstringdef(def).encoding;
  1435. if result=globals.CP_NONE then
  1436. result:=0
  1437. end;
  1438. function get_threadvar_record(def: tdef; out index_field, non_mt_data_field: tsym): trecorddef;
  1439. var
  1440. typ: ttypesym;
  1441. name: string;
  1442. begin
  1443. name:=internaltypeprefixName[itp_threadvar_record]+def.unique_id_str;
  1444. typ:=try_search_current_module_type(name);
  1445. if assigned(typ) then
  1446. begin
  1447. result:=trecorddef(ttypesym(typ).typedef);
  1448. index_field:=tsym(result.symtable.symlist[0]);
  1449. non_mt_data_field:=tsym(result.symtable.symlist[1]);
  1450. exit;
  1451. end;
  1452. { set recordalinmin to sizeof(pint), so the second field gets put at
  1453. offset = sizeof(pint) as expected }
  1454. result:=crecorddef.create_global_internal(
  1455. name,sizeof(pint),sizeof(pint));
  1456. {$ifdef cpu16bitaddr}
  1457. index_field:=result.add_field_by_def('',u16inttype);
  1458. {$else cpu16bitaddr}
  1459. index_field:=result.add_field_by_def('',u32inttype);
  1460. {$endif cpu16bitaddr}
  1461. non_mt_data_field:=result.add_field_by_def('',def);
  1462. { no need to add alignment padding, we won't create arrays of these }
  1463. end;
  1464. function get_recorddef(prefix:tinternaltypeprefix; const fields:array of tdef; packrecords:shortint): trecorddef;
  1465. var
  1466. fieldlist: tfplist;
  1467. srsym: tsym;
  1468. srsymtable: tsymtable;
  1469. i: longint;
  1470. name : TIDString;
  1471. begin
  1472. name:=copy(internaltypeprefixName[prefix],2,length(internaltypeprefixName[prefix]));
  1473. if searchsym_type(name,srsym,srsymtable) then
  1474. begin
  1475. result:=trecorddef(ttypesym(srsym).typedef);
  1476. exit
  1477. end;
  1478. { also always search in the current module (symtables are popped for
  1479. RTTI related code already) }
  1480. if searchsym_in_module(pointer(current_module),name,srsym,srsymtable) then
  1481. begin
  1482. result:=trecorddef(ttypesym(srsym).typedef);
  1483. exit;
  1484. end;
  1485. fieldlist:=tfplist.create;
  1486. for i:=low(fields) to high(fields) do
  1487. fieldlist.add(fields[i]);
  1488. result:=crecorddef.create_global_internal(internaltypeprefixName[prefix],packrecords,
  1489. targetinfos[target_info.system]^.alignment.recordalignmin);
  1490. result.add_fields_from_deflist(fieldlist);
  1491. fieldlist.free;
  1492. end;
  1493. procedure get_tabledef(prefix:tinternaltypeprefix;countdef,elementdef:tdef;count:longint;packrecords:shortint;out recdef:trecorddef;out arrdef:tarraydef);
  1494. var
  1495. fields: tfplist;
  1496. name: TIDString;
  1497. srsym: tsym;
  1498. fieldvarsym: tfieldvarsym;
  1499. srsymtable: tsymtable;
  1500. begin
  1501. { already created a message string table with this number of elements
  1502. in this unit -> reuse the def }
  1503. name:=internaltypeprefixName[prefix]+tostr(count);
  1504. if searchsym_type(copy(name,2,length(name)),srsym,srsymtable) then
  1505. begin
  1506. recdef:=trecorddef(ttypesym(srsym).typedef);
  1507. fieldvarsym:=trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size);
  1508. if fieldvarsym<>nil then
  1509. arrdef:=tarraydef(fieldvarsym.vardef)
  1510. else
  1511. arrdef:=nil;
  1512. exit
  1513. end;
  1514. { also always search in the current module (symtables are popped for
  1515. RTTI related code already) }
  1516. if searchsym_in_module(pointer(current_module),copy(name,2,length(name)),srsym,srsymtable) then
  1517. begin
  1518. recdef:=trecorddef(ttypesym(srsym).typedef);
  1519. fieldvarsym:=trecordsymtable(recdef.symtable).findfieldbyoffset(countdef.size);
  1520. if fieldvarsym<>nil then
  1521. arrdef:=tarraydef(fieldvarsym.vardef)
  1522. else
  1523. arrdef:=nil;
  1524. exit;
  1525. end;
  1526. recdef:=crecorddef.create_global_internal(name,packrecords,
  1527. targetinfos[target_info.system]^.alignment.recordalignmin);
  1528. fields:=tfplist.create;
  1529. fields.add(countdef);
  1530. if count>0 then
  1531. begin
  1532. arrdef:=carraydef.create(0,count-1,sizeuinttype);
  1533. arrdef.elementdef:=elementdef;
  1534. fields.add(arrdef);
  1535. end
  1536. else
  1537. arrdef:=nil;
  1538. recdef.add_fields_from_deflist(fields);
  1539. fields.free;
  1540. end;
  1541. function make_mangledname(const typeprefix:TSymStr;st:TSymtable;const suffix:TSymStr):TSymStr;
  1542. var
  1543. s,t,
  1544. prefix : TSymStr;
  1545. hash : qword;
  1546. begin
  1547. prefix:='';
  1548. if not assigned(st) then
  1549. internalerror(200204212);
  1550. repeat
  1551. { sub procedures }
  1552. while (st.symtabletype in [localsymtable,parasymtable]) do
  1553. begin
  1554. if st.defowner.typ<>procdef then
  1555. internalerror(200204173);
  1556. { Add the full mangledname of the routine to prevent
  1557. conflicts with two overloads both having a local entity
  1558. -- routine (tb0314), class, interface -- with the same name }
  1559. s:=tprocdef(st.defowner).procsym.name;
  1560. s:=s+tprocdef(st.defowner).mangledprocparanames(Length(s));
  1561. if prefix<>'' then
  1562. begin
  1563. if length(prefix)>(maxidlen-length(s)-1) then
  1564. begin
  1565. hash:=InitFnv64;
  1566. hash:=UpdateFnv64(hash,prefix[1],length(prefix));
  1567. prefix:='$H'+Base64Mangle(hash);
  1568. end;
  1569. prefix:=s+'_'+prefix
  1570. end
  1571. else
  1572. prefix:=s;
  1573. st:=st.defowner.owner;
  1574. end;
  1575. { object/classes symtable, nested type definitions in classes require the while loop }
  1576. while st.symtabletype in [ObjectSymtable,recordsymtable] do
  1577. begin
  1578. if not (st.defowner.typ in [objectdef,recorddef]) then
  1579. internalerror(200204174);
  1580. if length(prefix)>(maxidlen-length(tabstractrecorddef(st.defowner).objname^)-3) then
  1581. begin
  1582. hash:=InitFnv64;
  1583. hash:=UpdateFnv64(hash,prefix[1],length(prefix));
  1584. prefix:='$H'+Base64Mangle(hash);
  1585. end;
  1586. prefix:=tabstractrecorddef(st.defowner).objname^+'_$_'+prefix;
  1587. st:=st.defowner.owner;
  1588. end;
  1589. { local classes & interfaces are possible (because of closures)
  1590. or parasymtable for this case:
  1591. class function Trace<TResult>(const Func: TFunc<TLogToken, TResult>): TResult;
  1592. }
  1593. if not (st.symtabletype in [localsymtable,parasymtable]) then
  1594. break;
  1595. prefix:='$'+prefix;
  1596. until false;
  1597. { symtable must now be static or global }
  1598. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  1599. internalerror(200204175);
  1600. { The mangled name is made out of at most 4 parts:
  1601. 1) Optional typeprefix given as first parameter
  1602. with '_$' appended if not empty
  1603. 2) Unit name or 'P$'+program name (never empty)
  1604. 3) optional prefix variable that contains a unique
  1605. name for the local symbol table (prepended with '$_$'
  1606. if not empty)
  1607. 4) suffix as given as third parameter,
  1608. also optional (i.e. can be empty)
  1609. prepended by '_$$_' if not empty }
  1610. result:='';
  1611. if typeprefix<>'' then
  1612. result:=result+typeprefix+'_$';
  1613. { Add P$ for program, which can have the same name as
  1614. a unit }
  1615. if (TSymtable(main_module.localsymtable)=st) and
  1616. (not main_module.is_unit) then
  1617. result:=result+'P$'+st.name^
  1618. else
  1619. result:=result+st.name^;
  1620. if prefix<>'' then
  1621. result:=result+'$_$'+prefix;
  1622. if suffix<>'' then
  1623. result:=result+'_$$_'+suffix;
  1624. if length(result)>(maxidlen-1) then
  1625. begin
  1626. hash:=InitFnv64;
  1627. hash:=UpdateFnv64(hash,result[1],length(result));
  1628. result:=copy(result,1,maxidlen-(high(Base64OfUint64String)-low(Base64OfUint64String)+1)-2)+'$H'+Base64Mangle(hash);
  1629. end;
  1630. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  1631. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  1632. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  1633. { those in the debug map, leading to troubles with dsymutil). So always }
  1634. { add an underscore on darwin. }
  1635. if (target_info.system in systems_darwin) then
  1636. result := '_' + result;
  1637. end;
  1638. function make_dllmangledname(const dllname,importname:TSymStr;import_nr : word; pco : tproccalloption):TSymStr;
  1639. var
  1640. crc : cardinal;
  1641. i : longint;
  1642. use_crc : boolean;
  1643. dllprefix : TSymStr;
  1644. begin
  1645. if (target_info.system in (systems_all_windows + systems_nativent +
  1646. [system_i386_emx, system_i386_os2]))
  1647. and (dllname <> '') then
  1648. begin
  1649. dllprefix:=lower(ExtractFileName(dllname));
  1650. { Remove .dll suffix if present }
  1651. if copy(dllprefix,length(dllprefix)-3,length(dllprefix))='.dll' then
  1652. dllprefix:=copy(dllprefix,1,length(dllprefix)-4);
  1653. use_crc:=false;
  1654. for i:=1 to length(dllprefix) do
  1655. if not (dllprefix[i] in ['a'..'z','A'..'Z','_','0'..'9']) then
  1656. begin
  1657. use_crc:=true;
  1658. break;
  1659. end;
  1660. if use_crc then
  1661. begin
  1662. crc:=0;
  1663. crc:=UpdateCrc32(crc,dllprefix[1],length(dllprefix));
  1664. dllprefix:='_$dll$crc$'+hexstr(crc,8)+'$';
  1665. end
  1666. else
  1667. dllprefix:='_$dll$'+dllprefix+'$';
  1668. if importname<>'' then
  1669. result:=dllprefix+importname
  1670. else
  1671. result:=dllprefix+'_index_'+tostr(import_nr);
  1672. { Replace ? and @ in import name, since GNU AS does not allow these characters in symbol names. }
  1673. { This allows to import VC++ mangled names from DLLs. }
  1674. { Do not perform replacement, if external symbol is not imported from DLL. }
  1675. if (dllname<>'') then
  1676. begin
  1677. Replace(result,'?','__q$$');
  1678. {$ifdef arm}
  1679. { @ symbol is not allowed in ARM assembler only }
  1680. Replace(result,'@','__a$$');
  1681. {$endif arm}
  1682. end;
  1683. end
  1684. else
  1685. begin
  1686. if importname<>'' then
  1687. begin
  1688. if not(pco in [pocall_cdecl,pocall_cppdecl]) then
  1689. result:=importname
  1690. else
  1691. result:=target_info.Cprefix+importname;
  1692. end
  1693. else
  1694. result:='_index_'+tostr(import_nr);
  1695. end;
  1696. end;
  1697. {****************************************************************************
  1698. TDEFAWARESYMTABLESTACK
  1699. (symtablestack descendant that does some special actions on
  1700. the pushed/popped symtables)
  1701. ****************************************************************************}
  1702. procedure tdefawaresymtablestack.add_helpers_and_generics(st:tsymtable;addgenerics:boolean);
  1703. var
  1704. s: TSymStr;
  1705. list: TFPObjectList;
  1706. def: tdef;
  1707. sym : tsym;
  1708. i: integer;
  1709. begin
  1710. { search the symtable from first to last; the helper to use will be the
  1711. last one in the list }
  1712. for i:=0 to st.symlist.count-1 do
  1713. begin
  1714. sym:=tsym(st.symlist[i]);
  1715. if not (sym.typ in [typesym,procsym]) then
  1716. continue;
  1717. if sym.typ=typesym then
  1718. def:=ttypesym(st.SymList[i]).typedef
  1719. else
  1720. def:=nil;
  1721. if is_objectpascal_helper(def) then
  1722. begin
  1723. s:=generate_objectpascal_helper_key(tobjectdef(def).extendeddef);
  1724. Message1(sym_d_adding_helper_for,s);
  1725. list:=TFPObjectList(current_module.extendeddefs.Find(s));
  1726. if not assigned(list) then
  1727. begin
  1728. list:=TFPObjectList.Create(false);
  1729. current_module.extendeddefs.Add(s,list);
  1730. end;
  1731. list.Add(def);
  1732. end
  1733. else
  1734. begin
  1735. if addgenerics then
  1736. add_generic_dummysym(sym);
  1737. { add nested helpers as well }
  1738. if assigned(def) and
  1739. (def.typ in [recorddef,objectdef]) and
  1740. (sto_has_helper in tabstractrecorddef(def).symtable.tableoptions) and
  1741. not is_owned_by(def,tdef(st.defowner)) then
  1742. add_helpers_and_generics(tabstractrecorddef(def).symtable,false);
  1743. end;
  1744. end;
  1745. end;
  1746. procedure tdefawaresymtablestack.remove_helpers_and_generics(st:tsymtable);
  1747. begin
  1748. if sto_has_helper in st.tableoptions then
  1749. remove_helpers(st);
  1750. if sto_has_generic in st.tableoptions then
  1751. remove_generics(st);
  1752. end;
  1753. procedure tdefawaresymtablestack.remove_helpers(st:TSymtable);
  1754. var
  1755. i, j: integer;
  1756. tmpst: TSymtable;
  1757. list: TFPObjectList;
  1758. begin
  1759. for i:=current_module.extendeddefs.count-1 downto 0 do
  1760. begin
  1761. list:=TFPObjectList(current_module.extendeddefs[i]);
  1762. for j:=list.count-1 downto 0 do
  1763. begin
  1764. if not (list[j] is tobjectdef) then
  1765. Internalerror(2011031501);
  1766. tmpst:=tobjectdef(list[j]).owner;
  1767. repeat
  1768. if tmpst=st then
  1769. begin
  1770. list.delete(j);
  1771. break;
  1772. end
  1773. else
  1774. begin
  1775. if assigned(tmpst.defowner) then
  1776. tmpst:=tmpst.defowner.owner
  1777. else
  1778. tmpst:=nil;
  1779. end;
  1780. until not assigned(tmpst) or (tmpst.symtabletype in [globalsymtable,staticsymtable]);
  1781. end;
  1782. if list.count=0 then
  1783. current_module.extendeddefs.delete(i);
  1784. end;
  1785. end;
  1786. procedure tdefawaresymtablestack.remove_generics(st:tsymtable);
  1787. var
  1788. i,j : longint;
  1789. entry : tgenericdummyentry;
  1790. list : tfpobjectlist;
  1791. begin
  1792. for i:=current_module.genericdummysyms.count-1 downto 0 do
  1793. begin
  1794. list:=tfpobjectlist(current_module.genericdummysyms[i]);
  1795. if not assigned(list) then
  1796. continue;
  1797. for j:=list.count-1 downto 0 do
  1798. begin
  1799. entry:=tgenericdummyentry(list[j]);
  1800. if entry.dummysym.owner=st then
  1801. list.delete(j);
  1802. end;
  1803. if list.count=0 then
  1804. current_module.genericdummysyms.delete(i);
  1805. end;
  1806. end;
  1807. procedure tdefawaresymtablestack.pushcommon(st:tsymtable);
  1808. begin
  1809. if (sto_has_generic in st.tableoptions) or
  1810. (
  1811. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1812. (sto_has_helper in st.tableoptions)
  1813. ) then
  1814. { nested helpers will be added as well }
  1815. add_helpers_and_generics(st,true);
  1816. end;
  1817. procedure tdefawaresymtablestack.push(st: TSymtable);
  1818. begin
  1819. pushcommon(st);
  1820. inherited push(st);
  1821. end;
  1822. procedure tdefawaresymtablestack.pushafter(st,afterst:TSymtable);
  1823. begin
  1824. pushcommon(st);
  1825. inherited pushafter(st,afterst);
  1826. end;
  1827. procedure tdefawaresymtablestack.pop(st: TSymtable);
  1828. begin
  1829. inherited pop(st);
  1830. if (sto_has_generic in st.tableoptions) or
  1831. (
  1832. (st.symtabletype in [globalsymtable,staticsymtable]) and
  1833. (sto_has_helper in st.tableoptions)
  1834. ) then
  1835. { nested helpers will be removed as well }
  1836. remove_helpers_and_generics(st);
  1837. end;
  1838. {****************************************************************************
  1839. TDEF (base class for definitions)
  1840. ****************************************************************************}
  1841. constructor tgenericconstraintdata.create;
  1842. begin
  1843. interfaces:=tfpobjectlist.create(false);
  1844. interfacesderef:=tfplist.create;
  1845. end;
  1846. destructor tgenericconstraintdata.destroy;
  1847. begin
  1848. TFPList.FreeAndNilDisposing(interfacesderef,typeinfo(tderef));
  1849. interfaces.free;
  1850. inherited destroy;
  1851. end;
  1852. procedure tgenericconstraintdata.ppuload(ppufile: tcompilerppufile);
  1853. var
  1854. cnt,i : longint;
  1855. intfderef : pderef;
  1856. begin
  1857. ppufile.getset(tppuset1(flags));
  1858. cnt:=ppufile.getlongint;
  1859. for i:=0 to cnt-1 do
  1860. begin
  1861. new(intfderef);
  1862. ppufile.getderef(intfderef^);
  1863. interfacesderef.add(intfderef);
  1864. end;
  1865. end;
  1866. procedure tgenericconstraintdata.ppuwrite(ppufile: tcompilerppufile);
  1867. var
  1868. i : longint;
  1869. begin
  1870. ppufile.putset(tppuset1(flags));
  1871. ppufile.putlongint(interfacesderef.count);
  1872. for i:=0 to interfacesderef.count-1 do
  1873. ppufile.putderef(pderef(interfacesderef[i])^);
  1874. end;
  1875. procedure tgenericconstraintdata.buildderef;
  1876. var
  1877. intfderef : pderef;
  1878. i : longint;
  1879. begin
  1880. interfacesderef.capacity:=interfacesderef.count+interfaces.count;
  1881. for i:=0 to interfaces.count-1 do
  1882. begin
  1883. new(intfderef);
  1884. intfderef^.build(tobjectdef(interfaces[i]));
  1885. interfacesderef.add(intfderef);
  1886. end;
  1887. end;
  1888. procedure tgenericconstraintdata.deref;
  1889. var
  1890. i : longint;
  1891. begin
  1892. interfaces.capacity:=interfaces.count+interfacesderef.count;
  1893. for i:=0 to interfacesderef.count-1 do
  1894. interfaces.add(pderef(interfacesderef[i])^.resolve);
  1895. end;
  1896. procedure tstoreddef.writeentry(ppufile: tcompilerppufile; ibnr: byte);
  1897. begin
  1898. ppuwrite_platform(ppufile);
  1899. ppufile.writeentry(ibnr);
  1900. end;
  1901. procedure tstoreddef.ppuwrite_platform(ppufile: tcompilerppufile);
  1902. begin
  1903. { by default: do nothing }
  1904. end;
  1905. procedure tstoreddef.ppuload_platform(ppufile: tcompilerppufile);
  1906. begin
  1907. { by default: do nothing }
  1908. end;
  1909. class procedure tstoreddef.setup_reusable_def(origdef, newdef: tdef; res: PHashSetItem; oldsymtablestack: tsymtablestack);
  1910. var
  1911. reusablesymtab: tsymtable;
  1912. begin
  1913. { must not yet belong to a symtable }
  1914. if assigned(newdef.owner) then
  1915. internalerror(2015111503);
  1916. reusablesymtab:=origdef.getreusablesymtab;
  1917. res^.Data:=newdef;
  1918. reusablesymtab.insertdef(newdef);
  1919. symtablestack:=oldsymtablestack;
  1920. end;
  1921. constructor tstoreddef.create(dt:tdeftyp;doregister:boolean);
  1922. begin
  1923. inherited create(dt);
  1924. savesize := 0;
  1925. {$ifdef EXTDEBUG}
  1926. fileinfo := current_filepos;
  1927. {$endif}
  1928. generictokenbuf:=nil;
  1929. genericdef:=nil;
  1930. typesymderef.reset;
  1931. genericdefderef.reset;
  1932. { Don't register forwarddefs, they are disposed at the
  1933. end of an type block }
  1934. if (dt=forwarddef) then
  1935. exit;
  1936. { Register in symtable stack }
  1937. if doregister then
  1938. begin
  1939. { immediately register interface defs, as they will always be
  1940. written to the ppu, their defid influences the interface crc and
  1941. if we wait, depending on e.g. compiler defines they may get a
  1942. different defid (e.g. when a function is called, its procdef is
  1943. registered, so depending on whether or not, or when, an interface
  1944. procedure is called in the implementation, that may change its
  1945. defid otherwise) }
  1946. if assigned(current_module) and
  1947. current_module.in_interface then
  1948. register_def
  1949. else
  1950. maybe_put_in_symtable_stack;
  1951. end;
  1952. end;
  1953. destructor tstoreddef.destroy;
  1954. begin
  1955. { Direct calls are not allowed, use symtable.deletedef() }
  1956. if assigned(owner) then
  1957. internalerror(200612311);
  1958. if assigned(generictokenbuf) then
  1959. begin
  1960. generictokenbuf.free;
  1961. generictokenbuf:=nil;
  1962. end;
  1963. rtti_attribute_list.free;
  1964. genericparas.free;
  1965. TFPList.FreeAndNilDisposing(genericparaderefs,typeinfo(tderef));
  1966. genconstraintdata.free;
  1967. {$ifndef symansistr}
  1968. stringdispose(_fullownerhierarchyname);
  1969. {$endif not symansistr}
  1970. inherited destroy;
  1971. end;
  1972. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1973. var
  1974. sizeleft,i,cnt : longint;
  1975. buf : array[0..255] of byte;
  1976. symderef : pderef;
  1977. begin
  1978. inherited create(dt);
  1979. DefId:=ppufile.getlongint;
  1980. current_module.deflist[DefId]:=self;
  1981. {$ifdef EXTDEBUG}
  1982. fillchar(fileinfo,sizeof(fileinfo),0);
  1983. {$endif}
  1984. { load }
  1985. ppufile.getderef(typesymderef);
  1986. ppufile.getset(tppuset2(defoptions));
  1987. ppufile.getset(tppuset1(defstates));
  1988. if df_unique in defoptions then
  1989. ppufile.getderef(orgdefderef);
  1990. if df_genconstraint in defoptions then
  1991. begin
  1992. genconstraintdata:=tgenericconstraintdata.create;
  1993. genconstraintdata.ppuload(ppufile);
  1994. end;
  1995. if [df_generic,df_specialization]*defoptions<>[] then
  1996. begin
  1997. cnt:=ppufile.getlongint;
  1998. if cnt>0 then
  1999. begin
  2000. genericparas:=tfphashobjectlist.create(false);
  2001. genericparaderefs:=tfplist.create;
  2002. for i:=0 to cnt-1 do
  2003. begin
  2004. genericparas.add(ppufile.getstring,nil);
  2005. New(symderef);
  2006. ppufile.getderef(symderef^);
  2007. genericparaderefs.add(symderef);
  2008. end;
  2009. end;
  2010. end;
  2011. if df_generic in defoptions then
  2012. begin
  2013. sizeleft:=ppufile.getlongint;
  2014. initgeneric;
  2015. while sizeleft>0 do
  2016. begin
  2017. if sizeleft>sizeof(buf) then
  2018. i:=sizeof(buf)
  2019. else
  2020. i:=sizeleft;
  2021. ppufile.getdata(buf,i);
  2022. generictokenbuf.write(buf,i);
  2023. dec(sizeleft,i);
  2024. end;
  2025. end;
  2026. if df_specialization in defoptions then
  2027. ppufile.getderef(genericdefderef);
  2028. rtti_attribute_list:=trtti_attribute_list.ppuload(ppufile);
  2029. end;
  2030. function tstoreddef.needs_separate_initrtti:boolean;
  2031. begin
  2032. result:=false;
  2033. end;
  2034. function tstoreddef.rtti_mangledname(rt : trttitype) : TSymStr;
  2035. var
  2036. prefix : string[4];
  2037. begin
  2038. if (rt=fullrtti) or (not needs_separate_initrtti) then
  2039. begin
  2040. prefix:='RTTI';
  2041. include(defstates,ds_rtti_table_used);
  2042. end
  2043. else
  2044. begin
  2045. prefix:='INIT';
  2046. include(defstates,ds_init_table_used);
  2047. end;
  2048. if assigned(typesym) and
  2049. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  2050. result:=make_mangledname(prefix,typesym.owner,typesym.name)
  2051. else
  2052. result:=make_mangledname(prefix,findunitsymtable(owner),'def'+unique_id_str)
  2053. end;
  2054. function tstoreddef.OwnerHierarchyName: string;
  2055. var
  2056. tmp: tdef;
  2057. begin
  2058. tmp:=self;
  2059. result:='';
  2060. repeat
  2061. { can be not assigned in case of a forwarddef }
  2062. if assigned(tmp.owner) and
  2063. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2064. tmp:=tdef(tmp.owner.defowner)
  2065. else
  2066. break;
  2067. result:=tabstractrecorddef(tmp).objrealname^+'.'+result;
  2068. until tmp=nil;
  2069. end;
  2070. function tstoreddef.OwnerHierarchyPrettyName: string;
  2071. var
  2072. tmp: tdef;
  2073. begin
  2074. tmp:=self;
  2075. result:='';
  2076. repeat
  2077. { can be not assigned in case of a forwarddef }
  2078. if assigned(tmp.owner) and
  2079. (tmp.owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  2080. tmp:=tdef(tmp.owner.defowner)
  2081. else
  2082. break;
  2083. result:=tabstractrecorddef(tmp).typesymbolprettyname+'.'+result;
  2084. until tmp=nil;
  2085. end;
  2086. function tstoreddef.fullownerhierarchyname(skipprocparams:boolean): TSymStr;
  2087. var
  2088. lastowner: tsymtable;
  2089. tmp: tdef;
  2090. pno: tprocnameoptions;
  2091. begin
  2092. {$ifdef symansistr}
  2093. if not skipprocparams and (_fullownerhierarchyname<>'') then
  2094. exit(_fullownerhierarchyname);
  2095. {$else symansistr}
  2096. if not skipprocparams and assigned(_fullownerhierarchyname) then
  2097. exit(_fullownerhierarchyname^);
  2098. {$endif symansistr}
  2099. { the def can only reside inside structured types or
  2100. procedures/functions/methods }
  2101. tmp:=self;
  2102. result:='';
  2103. repeat
  2104. lastowner:=tmp.owner;
  2105. { can be not assigned in case of a forwarddef }
  2106. if not assigned(lastowner) then
  2107. break
  2108. else
  2109. tmp:=tdef(lastowner.defowner);
  2110. if not assigned(tmp) then
  2111. break;
  2112. if tmp.typ in [recorddef,objectdef] then
  2113. result:=tabstractrecorddef(tmp).objrealname^+'.'+result
  2114. else
  2115. if tmp.typ=procdef then
  2116. begin
  2117. pno:=[pno_paranames,pno_proctypeoption];
  2118. if skipprocparams then
  2119. include(pno,pno_noparams);
  2120. result:=tprocdef(tmp).customprocname(pno)+'.'+result;
  2121. end;
  2122. until tmp=nil;
  2123. { add the unit name }
  2124. if assigned(lastowner) and
  2125. assigned(lastowner.realname) then
  2126. result:=lastowner.realname^+'.'+result;
  2127. if not skipprocparams then
  2128. { don't store the name in this case }
  2129. {$ifdef symansistr}
  2130. _fullownerhierarchyname:=result;
  2131. {$else symansistr}
  2132. _fullownerhierarchyname:=stringdup(result);
  2133. {$endif symansistr}
  2134. end;
  2135. function tstoreddef.in_currentunit: boolean;
  2136. var
  2137. st: tsymtable;
  2138. begin
  2139. st:=owner;
  2140. while not(st.symtabletype in [globalsymtable,staticsymtable]) do
  2141. st:=st.defowner.owner;
  2142. result:=st.iscurrentunit;
  2143. end;
  2144. function tstoreddef.getcopy : tstoreddef;
  2145. begin
  2146. Message(sym_e_cant_create_unique_type);
  2147. getcopy:=cerrordef.create;
  2148. end;
  2149. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  2150. var
  2151. sizeleft,i : longint;
  2152. buf : array[0..255] of byte;
  2153. oldintfcrc : boolean;
  2154. begin
  2155. if defid<0 then
  2156. internalerror(2015101401);
  2157. ppufile.putlongint(DefId);
  2158. ppufile.putderef(typesymderef);
  2159. ppufile.putset(tppuset2(defoptions));
  2160. oldintfcrc:=ppufile.do_crc;
  2161. ppufile.do_crc:=false;
  2162. ppufile.putset(tppuset1(defstates));
  2163. if df_unique in defoptions then
  2164. ppufile.putderef(orgdefderef);
  2165. if df_genconstraint in defoptions then
  2166. genconstraintdata.ppuwrite(ppufile);
  2167. if [df_generic,df_specialization]*defoptions<>[] then
  2168. begin
  2169. if not assigned(genericparas) then
  2170. ppufile.putlongint(0)
  2171. else
  2172. begin
  2173. if not assigned(genericparaderefs) then
  2174. internalerror(2014052305);
  2175. ppufile.putlongint(genericparas.count);
  2176. for i:=0 to genericparas.count-1 do
  2177. begin
  2178. ppufile.putstring(genericparas.nameofindex(i));
  2179. ppufile.putderef(pderef(genericparaderefs[i])^);
  2180. end;
  2181. end;
  2182. end;
  2183. if df_generic in defoptions then
  2184. begin
  2185. if assigned(generictokenbuf) then
  2186. begin
  2187. sizeleft:=generictokenbuf.size;
  2188. generictokenbuf.seek(0);
  2189. end
  2190. else
  2191. sizeleft:=0;
  2192. ppufile.putlongint(sizeleft);
  2193. while sizeleft>0 do
  2194. begin
  2195. if sizeleft>sizeof(buf) then
  2196. i:=sizeof(buf)
  2197. else
  2198. i:=sizeleft;
  2199. generictokenbuf.read(buf,i);
  2200. ppufile.putdata(buf,i);
  2201. dec(sizeleft,i);
  2202. end;
  2203. end;
  2204. ppufile.do_crc:=oldintfcrc;
  2205. if df_specialization in defoptions then
  2206. ppufile.putderef(genericdefderef);
  2207. trtti_attribute_list.ppuwrite(rtti_attribute_list,ppufile);
  2208. end;
  2209. procedure tstoreddef.ppuload_subentries(ppufile: tcompilerppufile);
  2210. begin
  2211. trtti_attribute_list.ppuload_subentries(rtti_attribute_list,ppufile);
  2212. end;
  2213. procedure tstoreddef.ppuwrite_subentries(ppufile: tcompilerppufile);
  2214. begin
  2215. trtti_attribute_list.ppuwrite_subentries(rtti_attribute_list,ppufile);
  2216. end;
  2217. procedure tstoreddef.buildderef;
  2218. var
  2219. i : longint;
  2220. sym : tsym;
  2221. symderef : pderef;
  2222. begin
  2223. if not registered then
  2224. register_def;
  2225. typesymderef.build(typesym);
  2226. orgdefderef.build(orgdef);
  2227. genericdefderef.build(genericdef);
  2228. if assigned(rtti_attribute_list) then
  2229. rtti_attribute_list.buildderef;
  2230. if assigned(genconstraintdata) then
  2231. genconstraintdata.buildderef;
  2232. if assigned(genericparas) then
  2233. begin
  2234. if not assigned(genericparaderefs) then
  2235. genericparaderefs:=tfplist.create;
  2236. genericparaderefs.capacity:=genericparaderefs.count+genericparas.count;
  2237. for i:=0 to genericparas.count-1 do
  2238. begin
  2239. sym:=tsym(genericparas.items[i]);
  2240. new(symderef);
  2241. symderef^.build(sym);
  2242. genericparaderefs.add(symderef);
  2243. end;
  2244. end;
  2245. end;
  2246. procedure tstoreddef.buildderefimpl;
  2247. begin
  2248. end;
  2249. procedure tstoreddef.deref;
  2250. var
  2251. symderef : pderef;
  2252. i : longint;
  2253. begin
  2254. typesym:=ttypesym(typesymderef.resolve);
  2255. if df_unique in defoptions then
  2256. orgdef:=tstoreddef(orgdefderef.resolve);
  2257. if df_specialization in defoptions then
  2258. genericdef:=tstoreddef(genericdefderef.resolve);
  2259. if assigned(rtti_attribute_list) then
  2260. rtti_attribute_list.deref;
  2261. if assigned(genconstraintdata) then
  2262. genconstraintdata.deref;
  2263. if assigned(genericparas) then
  2264. begin
  2265. if not assigned(genericparaderefs) then
  2266. internalerror(2014052302);
  2267. if genericparas.count<>genericparaderefs.count then
  2268. internalerror(2014052303);
  2269. for i:=0 to genericparaderefs.count-1 do
  2270. begin
  2271. symderef:=pderef(genericparaderefs[i]);
  2272. genericparas.items[i]:=symderef^.resolve;
  2273. end;
  2274. end;
  2275. end;
  2276. procedure tstoreddef.derefimpl;
  2277. begin
  2278. end;
  2279. function tstoreddef.size : asizeint;
  2280. begin
  2281. size:=savesize;
  2282. end;
  2283. function tstoreddef.getvardef:longint;
  2284. begin
  2285. result:=varUndefined;
  2286. end;
  2287. function tstoreddef.alignment : shortint;
  2288. begin
  2289. { natural alignment by default }
  2290. alignment:=size_2_align(savesize);
  2291. { can happen if savesize = 0, e.g. for voiddef or
  2292. an empty record
  2293. }
  2294. if (alignment=0) then
  2295. alignment:=1;
  2296. end;
  2297. { returns true, if the definition can be published }
  2298. function tstoreddef.is_publishable : tpublishproperty;
  2299. begin
  2300. is_publishable:=pp_error;
  2301. end;
  2302. { needs an init table }
  2303. function tstoreddef.needs_inittable : boolean;
  2304. begin
  2305. needs_inittable:=false;
  2306. end;
  2307. function tstoreddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  2308. begin
  2309. result:=false;
  2310. end;
  2311. function tstoreddef.is_intregable : boolean;
  2312. {$ifndef cpuhighleveltarget}
  2313. var
  2314. recsize,temp: longint;
  2315. {$endif cpuhighleveltarget}
  2316. begin
  2317. case typ of
  2318. orddef,
  2319. pointerdef,
  2320. enumdef,
  2321. classrefdef:
  2322. is_intregable:=true;
  2323. procvardef :
  2324. is_intregable:=tprocvardef(self).is_addressonly or (po_methodpointer in tprocvardef(self).procoptions);
  2325. objectdef:
  2326. is_intregable:=(is_implicit_pointer_object_type(self)) and not needs_inittable;
  2327. setdef:
  2328. is_intregable:=is_smallset(self);
  2329. arraydef:
  2330. {$ifdef cpuhighleveltarget}
  2331. is_intregable:=false
  2332. {$else cpuhighleveltarget}
  2333. is_intregable:=not(is_special_array(self)) and
  2334. (tarraydef(self).size<=8) and (tarraydef(self).size in [1,2,4,8]) and
  2335. tstoreddef(tarraydef(self).elementdef).is_intregable
  2336. {$ifdef SUPPORT_MMX}
  2337. and not((cs_mmx in current_settings.localswitches) and
  2338. is_mmx_able_array(self))
  2339. {$endif SUPPORT_MMX}
  2340. {$endif cpuhighleveltarget}
  2341. ;
  2342. recorddef:
  2343. begin
  2344. {$ifdef cpuhighleveltarget}
  2345. is_intregable:=false;
  2346. {$else cpuhighleveltarget}
  2347. recsize:=size;
  2348. is_intregable:=
  2349. ispowerof2(recsize,temp) and
  2350. ((recsize<=sizeof(aint)*2) and
  2351. not trecorddef(self).contains_cross_aword_field and
  2352. { records cannot go into registers on 16 bit targets for now }
  2353. (sizeof(aint)>2) and
  2354. not trecorddef(self).contains_float_field
  2355. ) and
  2356. not needs_inittable;
  2357. {$endif cpuhighleveltarget}
  2358. end;
  2359. else
  2360. is_intregable:=false;
  2361. end;
  2362. end;
  2363. function tstoreddef.is_fpuregable : boolean;
  2364. begin
  2365. {$ifdef x86}
  2366. result:=use_vectorfpu(self);
  2367. {$else x86}
  2368. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches)
  2369. {$ifdef xtensa}
  2370. and (FPUXTENSA_SINGLE in fpu_capabilities[init_settings.fputype]) and (tfloatdef(self).floattype=s32real)
  2371. {$endif xtensa}
  2372. {$ifdef arm}
  2373. and (((FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[init_settings.fputype]) and (tfloatdef(self).floattype=s32real)) or
  2374. (FPUARM_HAS_VFP_DOUBLE in fpu_capabilities[init_settings.fputype]))
  2375. {$endif arm}
  2376. ;
  2377. {$endif x86}
  2378. end;
  2379. function tstoreddef.is_const_intregable : boolean;
  2380. begin
  2381. case typ of
  2382. stringdef:
  2383. result:=tstringdef(self).stringtype in [st_ansistring,st_unicodestring,st_widestring];
  2384. arraydef:
  2385. result:=is_dynamic_array(self);
  2386. objectdef:
  2387. result:=is_interface(self);
  2388. else
  2389. result:=false;
  2390. end;
  2391. end;
  2392. procedure tstoreddef.initgeneric;
  2393. begin
  2394. if assigned(generictokenbuf) then
  2395. internalerror(200512131);
  2396. generictokenbuf:=tdynamicarray.create(256);
  2397. end;
  2398. function tstoreddef.is_generic: boolean;
  2399. var
  2400. sym: tsym;
  2401. i: longint;
  2402. begin
  2403. result:=assigned(genericparas) and
  2404. (genericparas.count>0) and
  2405. (df_generic in defoptions);
  2406. if result then
  2407. { if any of the type parameters does *not* belong to as (meaning it was passed
  2408. in from outside) then we aren't a generic, but a specialization }
  2409. for i:=0 to genericparas.count-1 do
  2410. begin
  2411. sym:=tsym(genericparas[i]);
  2412. { sym must be either a type or const }
  2413. if not (sym.typ in [symconst.typesym,symconst.constsym]) then
  2414. internalerror(2014050903);
  2415. if (sym.owner.defowner<>self) or (sp_generic_unnamed_type in sym.symoptions) then
  2416. exit(false);
  2417. if (sym.typ=symconst.constsym) and (sp_generic_const in sym.symoptions) then
  2418. exit(false);
  2419. end;
  2420. end;
  2421. function tstoreddef.is_generic_param_const(index:integer):boolean;
  2422. begin
  2423. result:=tsym(genericparas[index]).typ=constsym;
  2424. end;
  2425. function tstoreddef.get_generic_param_def(index:integer):tdef;
  2426. begin
  2427. if tsym(genericparas[index]).typ=constsym then
  2428. result:=tconstsym(genericparas[index]).constdef
  2429. else
  2430. result:=ttypesym(genericparas[index]).typedef;
  2431. end;
  2432. function tstoreddef.is_specialization: boolean;
  2433. var
  2434. i : longint;
  2435. sym : tsym;
  2436. begin
  2437. result:=assigned(genericparas) and
  2438. (genericparas.count>0) and
  2439. (df_specialization in defoptions);
  2440. if result then
  2441. begin
  2442. { if at least one of the generic parameters is not owned by us (meaning it was
  2443. passed in from outside) then we have a specialization, otherwise we have a generic }
  2444. for i:=0 to genericparas.count-1 do
  2445. begin
  2446. sym:=tsym(genericparas[i]);
  2447. { sym must be either a type or const }
  2448. if not (sym.typ in [symconst.typesym,symconst.constsym]) then
  2449. internalerror(2014050904);
  2450. if (sym.owner.defowner<>self) or (sp_generic_unnamed_type in sym.symoptions) then
  2451. exit(true);
  2452. if (sym.typ=symconst.constsym) and (sp_generic_const in sym.symoptions) then
  2453. exit(true);
  2454. end;
  2455. result:=false;
  2456. end;
  2457. end;
  2458. procedure tstoreddef.register_def;
  2459. var
  2460. gst : tgetsymtable;
  2461. st : tsymtable;
  2462. begin
  2463. if registered then
  2464. exit;
  2465. { Register in current_module }
  2466. if assigned(current_module) then
  2467. begin
  2468. exclude(defoptions,df_not_registered_no_free);
  2469. for gst:=low(tgetsymtable) to high(tgetsymtable) do
  2470. begin
  2471. st:=getsymtable(gst);
  2472. if assigned(st) then
  2473. tstoredsymtable(st).register_children;
  2474. end;
  2475. if defid<defid_not_registered then
  2476. defid:=deflist_index
  2477. else
  2478. begin
  2479. current_module.deflist.Add(self);
  2480. defid:=current_module.deflist.Count-1;
  2481. registered_in_module:=current_module;
  2482. end;
  2483. maybe_put_in_symtable_stack;
  2484. end
  2485. else
  2486. DefId:=defid_registered_nost;
  2487. end;
  2488. procedure tstoreddef.maybe_put_in_symtable_stack;
  2489. var
  2490. insertstack: psymtablestackitem;
  2491. begin
  2492. if assigned(symtablestack) and
  2493. not assigned(self.owner) then
  2494. begin
  2495. insertstack:=symtablestack.stack;
  2496. { don't insert defs in exception symtables, as they are freed before
  2497. the module is compiled, so we can get crashes on high level targets
  2498. if they still need it while e.g. writing assembler code }
  2499. while assigned(insertstack) and
  2500. (insertstack^.symtable.symtabletype in [exceptsymtable,withsymtable]) do
  2501. insertstack:=insertstack^.next;
  2502. if not assigned(insertstack) then
  2503. internalerror(200602044);
  2504. if insertstack^.symtable.sealed then
  2505. internalerror(2015022301);
  2506. insertstack^.symtable.insertdef(self);
  2507. end;
  2508. end;
  2509. {****************************************************************************
  2510. Tstringdef
  2511. ****************************************************************************}
  2512. constructor tstringdef.createshort(l: byte; doregister: boolean);
  2513. begin
  2514. inherited create(stringdef,doregister);
  2515. stringtype:=st_shortstring;
  2516. encoding:=0;
  2517. len:=l;
  2518. end;
  2519. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  2520. begin
  2521. inherited ppuload(stringdef,ppufile);
  2522. stringtype:=st_shortstring;
  2523. encoding:=0;
  2524. len:=ppufile.getbyte;
  2525. ppuload_platform(ppufile);
  2526. end;
  2527. constructor tstringdef.createlong(l: asizeint; doregister: boolean);
  2528. begin
  2529. inherited create(stringdef,doregister);
  2530. stringtype:=st_longstring;
  2531. encoding:=0;
  2532. len:=l;
  2533. end;
  2534. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  2535. begin
  2536. inherited ppuload(stringdef,ppufile);
  2537. stringtype:=st_longstring;
  2538. encoding:=0;
  2539. len:=ppufile.getasizeint;
  2540. ppuload_platform(ppufile);
  2541. end;
  2542. constructor tstringdef.createansi(aencoding: tstringencoding; doregister: boolean);
  2543. begin
  2544. inherited create(stringdef,doregister);
  2545. stringtype:=st_ansistring;
  2546. encoding:=aencoding;
  2547. len:=-1;
  2548. end;
  2549. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  2550. begin
  2551. inherited ppuload(stringdef,ppufile);
  2552. stringtype:=st_ansistring;
  2553. len:=ppufile.getasizeint;
  2554. encoding:=ppufile.getword;
  2555. ppuload_platform(ppufile);
  2556. end;
  2557. constructor tstringdef.createwide(doregister: boolean);
  2558. begin
  2559. inherited create(stringdef,doregister);
  2560. stringtype:=st_widestring;
  2561. if target_info.endian=endian_little then
  2562. encoding:=CP_UTF16LE
  2563. else
  2564. encoding:=CP_UTF16BE;
  2565. len:=-1;
  2566. end;
  2567. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  2568. begin
  2569. inherited ppuload(stringdef,ppufile);
  2570. stringtype:=st_widestring;
  2571. if target_info.endian=endian_little then
  2572. encoding:=CP_UTF16LE
  2573. else
  2574. encoding:=CP_UTF16BE;
  2575. len:=ppufile.getasizeint;
  2576. ppuload_platform(ppufile);
  2577. end;
  2578. constructor tstringdef.createunicode(doregister: boolean);
  2579. begin
  2580. inherited create(stringdef,doregister);
  2581. stringtype:=st_unicodestring;
  2582. if target_info.endian=endian_little then
  2583. encoding:=CP_UTF16LE
  2584. else
  2585. encoding:=CP_UTF16BE;
  2586. len:=-1;
  2587. end;
  2588. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  2589. begin
  2590. inherited ppuload(stringdef,ppufile);
  2591. stringtype:=st_unicodestring;
  2592. len:=ppufile.getasizeint;
  2593. encoding:=ppufile.getword;
  2594. ppuload_platform(ppufile);
  2595. end;
  2596. function tstringdef.getcopy : tstoreddef;
  2597. begin
  2598. result:=cstringdef.create(typ,true);
  2599. result.typ:=stringdef;
  2600. tstringdef(result).stringtype:=stringtype;
  2601. tstringdef(result).encoding:=encoding;
  2602. tstringdef(result).len:=len;
  2603. end;
  2604. function tstringdef.stringtypname:string;
  2605. const
  2606. typname:array[tstringtype] of string[10]=(
  2607. 'shortstr','longstr','ansistr','widestr','unicodestr'
  2608. );
  2609. begin
  2610. stringtypname:=typname[stringtype];
  2611. end;
  2612. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  2613. begin
  2614. inherited ppuwrite(ppufile);
  2615. if stringtype=st_shortstring then
  2616. begin
  2617. {$ifdef extdebug}
  2618. if len > 255 then internalerror(12122002);
  2619. {$endif}
  2620. ppufile.putbyte(byte(len))
  2621. end
  2622. else
  2623. ppufile.putasizeint(len);
  2624. if stringtype in [st_ansistring,st_unicodestring] then
  2625. ppufile.putword(encoding);
  2626. case stringtype of
  2627. st_shortstring : writeentry(ppufile,ibshortstringdef);
  2628. st_longstring : writeentry(ppufile,iblongstringdef);
  2629. st_ansistring : writeentry(ppufile,ibansistringdef);
  2630. st_widestring : writeentry(ppufile,ibwidestringdef);
  2631. st_unicodestring : writeentry(ppufile,ibunicodestringdef);
  2632. end;
  2633. end;
  2634. function tstringdef.needs_inittable : boolean;
  2635. begin
  2636. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  2637. end;
  2638. function tstringdef.GetTypeName : string;
  2639. const
  2640. names : array[tstringtype] of string[15] = (
  2641. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  2642. begin
  2643. GetTypeName:=names[stringtype];
  2644. case stringtype of
  2645. st_ansistring,st_unicodestring:
  2646. GetTypeName:=GetTypeName+'('+tostr(encoding)+')';
  2647. st_shortstring:
  2648. GetTypeName:=GetTypeName+'['+tostr(len)+']';
  2649. else
  2650. ;
  2651. end;
  2652. end;
  2653. function tstringdef.getvardef : longint;
  2654. const
  2655. vardef : array[tstringtype] of longint = (
  2656. varUndefined,varUndefined,varString,varOleStr,varUString);
  2657. begin
  2658. result:=vardef[stringtype];
  2659. end;
  2660. function tstringdef.get_default_char_type: tdef;
  2661. begin
  2662. case stringtype of
  2663. st_shortstring,
  2664. st_longstring,
  2665. st_ansistring:
  2666. result:=cansichartype;
  2667. st_unicodestring,
  2668. st_widestring:
  2669. result:=cwidechartype;
  2670. end;
  2671. end;
  2672. function tstringdef.get_default_string_type: tdef;
  2673. begin
  2674. case stringtype of
  2675. st_shortstring:
  2676. result:=cshortstringtype;
  2677. { st_longstring is currently not supported but
  2678. when it is this case will need to be supplied }
  2679. st_longstring:
  2680. internalerror(2021040801);
  2681. st_ansistring:
  2682. result:=cansistringtype;
  2683. st_widestring:
  2684. result:=cwidestringtype;
  2685. st_unicodestring:
  2686. result:=cunicodestringtype;
  2687. end
  2688. end;
  2689. function tstringdef.alignment : shortint;
  2690. begin
  2691. case stringtype of
  2692. st_unicodestring,
  2693. st_widestring,
  2694. st_ansistring:
  2695. alignment:=voidpointertype.alignment;
  2696. st_longstring,
  2697. st_shortstring:
  2698. { char to string accesses byte 0 and 1 with one word access }
  2699. if (tf_requires_proper_alignment in target_info.flags) or
  2700. { macpas needs an alignment of 2 (MetroWerks compatible) }
  2701. (m_mac in current_settings.modeswitches) then
  2702. alignment:=size_2_align(2)
  2703. else
  2704. alignment:=size_2_align(1);
  2705. end;
  2706. end;
  2707. function tstringdef.getmangledparaname : TSymStr;
  2708. begin
  2709. getmangledparaname:='STRING';
  2710. end;
  2711. function tstringdef.is_publishable : tpublishproperty;
  2712. begin
  2713. is_publishable:=pp_publish;
  2714. end;
  2715. function tstringdef.size: asizeint;
  2716. begin
  2717. case stringtype of
  2718. st_shortstring:
  2719. Result:=len+1;
  2720. st_longstring,
  2721. st_ansistring,
  2722. st_widestring,
  2723. st_unicodestring:
  2724. Result:=voidpointertype.size;
  2725. end;
  2726. end;
  2727. {****************************************************************************
  2728. TENUMDEF
  2729. ****************************************************************************}
  2730. constructor tenumdef.create;
  2731. begin
  2732. inherited create(enumdef,true);
  2733. minval:=0;
  2734. maxval:=0;
  2735. calcsavesize(current_settings.packenum);
  2736. has_jumps:=false;
  2737. basedef:=nil;
  2738. basedefderef.reset;
  2739. symtable:=tenumsymtable.create(self);
  2740. end;
  2741. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:asizeint);
  2742. begin
  2743. inherited create(enumdef,true);
  2744. minval:=_min;
  2745. maxval:=_max;
  2746. basedef:=_basedef;
  2747. calcsavesize(current_settings.packenum);
  2748. has_jumps:=false;
  2749. symtable:=basedef.symtable.getcopy;
  2750. include(defoptions, df_copied_def);
  2751. end;
  2752. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  2753. begin
  2754. inherited ppuload(enumdef,ppufile);
  2755. minval:=ppufile.getaint;
  2756. maxval:=ppufile.getaint;
  2757. savesize:=ppufile.getaint;
  2758. has_jumps:=false;
  2759. if df_copied_def in defoptions then
  2760. begin
  2761. symtable:=nil;
  2762. ppufile.getderef(basedefderef);
  2763. ppuload_platform(ppufile);
  2764. end
  2765. else
  2766. begin
  2767. ppuload_platform(ppufile);
  2768. // create with nil defowner first to prevent values changes on insert
  2769. symtable:=tenumsymtable.create(nil);
  2770. tenumsymtable(symtable).ppuload(ppufile);
  2771. symtable.defowner:=self;
  2772. end;
  2773. end;
  2774. destructor tenumdef.destroy;
  2775. begin
  2776. symtable.free;
  2777. symtable:=nil;
  2778. inherited destroy;
  2779. end;
  2780. function tenumdef.getcopy : tstoreddef;
  2781. begin
  2782. if assigned(basedef) then
  2783. result:=cenumdef.create_subrange(basedef,minval,maxval)
  2784. else
  2785. begin
  2786. result:=cenumdef.create;
  2787. tenumdef(result).minval:=minval;
  2788. tenumdef(result).maxval:=maxval;
  2789. tenumdef(result).symtable.free;
  2790. tenumdef(result).symtable:=symtable.getcopy;
  2791. tenumdef(result).basedef:=self;
  2792. end;
  2793. tenumdef(result).has_jumps:=has_jumps;
  2794. tenumdef(result).basedefderef:=basedefderef;
  2795. include(tenumdef(result).defoptions,df_copied_def);
  2796. end;
  2797. procedure tenumdef.calcsavesize(packenum: shortint);
  2798. begin
  2799. {$IFNDEF cpu64bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2800. if (packenum=8) or (int64(min)<low(longint)) or (int64(max)>high(cardinal)) or ((min<0) and (max>high(longint))) then
  2801. savesize:=8
  2802. {$IFNDEF cpu64bitaddr} {$pop} {$ENDIF}
  2803. else
  2804. {$IFDEF cpu16bitaddr} {$push}{$warnings off} {$ENDIF} //comparison always false warning
  2805. if (packenum=4) or (min<low(smallint)) or (max>high(word)) or ((min<0) and (max>high(smallint))) then
  2806. savesize:=4
  2807. {$IFDEF cpu16bitaddr} {$pop} {$ENDIF}
  2808. else
  2809. if (packenum=2) or (min<low(shortint)) or (max>high(byte)) or ((min<0) and (max>high(shortint))) then
  2810. savesize:=2
  2811. else
  2812. savesize:=1;
  2813. end;
  2814. function tenumdef.packedbitsize: asizeint;
  2815. var
  2816. sizeval: tconstexprint;
  2817. power: longint;
  2818. begin
  2819. result := 0;
  2820. if (minval >= 0) and
  2821. (maxval <= 1) then
  2822. result := 1
  2823. else
  2824. begin
  2825. if (minval>=0) then
  2826. sizeval:=maxval
  2827. else
  2828. { don't count 0 twice, but take into account that range goes from -n-1..n }
  2829. sizeval:=(cutils.max(-minval,maxval+1)*2)-1;
  2830. { 256 must become 512 etc. }
  2831. nextpowerof2(sizeval+1,power);
  2832. result := power;
  2833. end;
  2834. end;
  2835. procedure tenumdef.setmax(_max:asizeint);
  2836. begin
  2837. maxval:=_max;
  2838. calcsavesize(current_settings.packenum);
  2839. end;
  2840. procedure tenumdef.setmin(_min:asizeint);
  2841. begin
  2842. minval:=_min;
  2843. calcsavesize(current_settings.packenum);
  2844. end;
  2845. function tenumdef.min:asizeint;
  2846. begin
  2847. min:=minval;
  2848. end;
  2849. function tenumdef.max:asizeint;
  2850. begin
  2851. max:=maxval;
  2852. end;
  2853. function tenumdef.getfirstsym: tsym;
  2854. var
  2855. i:integer;
  2856. begin
  2857. for i := 0 to symtable.SymList.Count - 1 do
  2858. begin
  2859. result:=tsym(symtable.SymList[i]);
  2860. if tenumsym(result).value=minval then
  2861. exit;
  2862. end;
  2863. result:=nil;
  2864. end;
  2865. function tenumdef.int2enumsym(l: asizeint): tsym;
  2866. var
  2867. i: longint;
  2868. sym: tsym;
  2869. bdef: tenumdef;
  2870. begin
  2871. result:=nil;
  2872. if (l<minval) or
  2873. (l>maxval) then
  2874. exit;
  2875. bdef:=getbasedef;
  2876. for i:=0 to bdef.symtable.symlist.count-1 do
  2877. begin
  2878. sym:=tsym(bdef.symtable.symlist[i]);
  2879. if (sym.typ=enumsym) and
  2880. (tenumsym(sym).value=l) then
  2881. begin
  2882. result:=sym;
  2883. exit;
  2884. end;
  2885. end;
  2886. end;
  2887. function tenumdef.getbasedef: tenumdef;
  2888. begin
  2889. if not assigned(basedef) then
  2890. result:=self
  2891. else
  2892. result:=basedef;
  2893. end;
  2894. procedure tenumdef.buildderef;
  2895. begin
  2896. inherited buildderef;
  2897. if df_copied_def in defoptions then
  2898. basedefderef.build(basedef)
  2899. else
  2900. tenumsymtable(symtable).buildderef;
  2901. end;
  2902. procedure tenumdef.deref;
  2903. begin
  2904. inherited deref;
  2905. if df_copied_def in defoptions then
  2906. begin
  2907. basedef:=tenumdef(basedefderef.resolve);
  2908. symtable:=basedef.symtable.getcopy;
  2909. end
  2910. else
  2911. tenumsymtable(symtable).deref(false);
  2912. end;
  2913. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  2914. begin
  2915. inherited ppuwrite(ppufile);
  2916. ppufile.putaint(min);
  2917. ppufile.putaint(max);
  2918. ppufile.putaint(savesize);
  2919. if df_copied_def in defoptions then
  2920. ppufile.putderef(basedefderef);
  2921. writeentry(ppufile,ibenumdef);
  2922. if not (df_copied_def in defoptions) then
  2923. tenumsymtable(symtable).ppuwrite(ppufile);
  2924. end;
  2925. function tenumdef.is_publishable : tpublishproperty;
  2926. begin
  2927. if not has_jumps then
  2928. is_publishable:=pp_publish
  2929. else
  2930. if m_delphi in current_settings.modeswitches then
  2931. is_publishable:=pp_ignore
  2932. else
  2933. is_publishable:=pp_error;
  2934. end;
  2935. function tenumdef.GetTypeName : string;
  2936. begin
  2937. GetTypeName:='<enumeration type>';
  2938. end;
  2939. {****************************************************************************
  2940. TRTTI_ATTRIBUTE_LIST
  2941. ****************************************************************************}
  2942. constructor trtti_attribute.ppuload(ppufile: tcompilerppufile);
  2943. begin
  2944. ppufile.getderef(typesymderef);
  2945. ppufile.getderef(typeconstrderef);
  2946. setlength(paras,ppufile.getlongint);
  2947. end;
  2948. procedure trtti_attribute.ppuwrite(ppufile: tcompilerppufile);
  2949. begin
  2950. ppufile.putderef(typesymderef);
  2951. ppufile.putderef(typeconstrderef);
  2952. ppufile.putlongint(length(paras));
  2953. end;
  2954. procedure trtti_attribute.ppuload_subentries(ppufile: tcompilerppufile);
  2955. var
  2956. i : sizeint;
  2957. begin
  2958. for i:=0 to high(paras) do
  2959. paras[i]:=ppuloadnodetree(ppufile);
  2960. end;
  2961. procedure trtti_attribute.ppuwrite_subentries(ppufile: tcompilerppufile);
  2962. var
  2963. i : sizeint;
  2964. begin
  2965. for i:=0 to high(paras) do
  2966. ppuwritenodetree(ppufile,paras[i]);
  2967. end;
  2968. destructor trtti_attribute.destroy;
  2969. var
  2970. n : tnode;
  2971. begin
  2972. constructorcall.free;
  2973. for n in paras do
  2974. n.free;
  2975. inherited destroy;
  2976. end;
  2977. procedure trtti_attribute.buildderef;
  2978. var
  2979. i : sizeint;
  2980. begin
  2981. typesymderef.build(typesym);
  2982. typeconstrderef.build(typeconstr);
  2983. for i:=0 to high(paras) do
  2984. paras[i].buildderefimpl;
  2985. end;
  2986. procedure trtti_attribute.deref;
  2987. var
  2988. i : sizeint;
  2989. begin
  2990. typesym:=tsym(typesymderef.resolve);
  2991. typeconstr:=tdef(typeconstrderef.resolve);
  2992. for i:=0 to high(paras) do
  2993. paras[i].derefimpl;
  2994. end;
  2995. class procedure trtti_attribute_list.bind(var dangling,owned:trtti_attribute_list);
  2996. begin
  2997. if assigned(owned) then
  2998. internalerror(2019071001);
  2999. if not assigned(dangling) then
  3000. exit;
  3001. if dangling.is_bound then
  3002. internalerror(2019071002);
  3003. current_module.used_rtti_attrs.concatlistcopy(dangling.rtti_attributes);
  3004. dangling.is_bound:=true;
  3005. owned:=dangling;
  3006. dangling:=nil;
  3007. end;
  3008. class procedure trtti_attribute_list.copyandbind(alist : trtti_attribute_list; var owned: trtti_attribute_list);
  3009. var
  3010. i,j : Integer;
  3011. attr,newattribute : trtti_attribute;
  3012. begin
  3013. if owned=Nil then
  3014. owned:=trtti_attribute_list.Create;
  3015. owned.is_bound:=True;
  3016. for i:=0 to aList.rtti_attributes.Count-1 do
  3017. begin
  3018. attr:=trtti_attribute(alist.rtti_attributes[i]);
  3019. newattribute:=trtti_attribute.Create;
  3020. newattribute.typesym:=attr.typesym;
  3021. newattribute.typeconstr:=attr.typeconstr;
  3022. newattribute.constructorcall:=attr.constructorcall.getcopy;
  3023. setlength(newattribute.paras,length(attr.paras));
  3024. for j:=0 to length(attr.paras)-1 do
  3025. newattribute.paras[j]:=attr.paras[j].getcopy;
  3026. owned.AddAttribute(newattribute);
  3027. end;
  3028. current_module.used_rtti_attrs.concatlistcopy(owned.rtti_attributes);
  3029. end;
  3030. procedure trtti_attribute_list.addattribute(atypesym:tsym;typeconstr:tdef;constructorcall:tnode;constref paras:array of tnode);
  3031. var
  3032. newattribute : trtti_attribute;
  3033. i : sizeint;
  3034. begin
  3035. if not assigned(rtti_attributes) then
  3036. rtti_attributes:=TFPObjectList.Create(true);
  3037. newattribute:=trtti_attribute.Create;
  3038. newattribute.typesym:=atypesym;
  3039. newattribute.typeconstr:=typeconstr;
  3040. newattribute.constructorcall:=constructorcall;
  3041. setlength(newattribute.paras,length(paras));
  3042. for i:=0 to high(paras) do
  3043. newattribute.paras[i]:=paras[i];
  3044. rtti_attributes.Add(newattribute);
  3045. end;
  3046. procedure trtti_attribute_list.addattribute(attr:trtti_attribute);
  3047. begin
  3048. if not assigned(rtti_attributes) then
  3049. rtti_attributes:=TFPObjectList.Create(true);
  3050. rtti_attributes.add(attr);
  3051. end;
  3052. destructor trtti_attribute_list.destroy;
  3053. begin
  3054. rtti_attributes.Free;
  3055. inherited destroy;
  3056. end;
  3057. function trtti_attribute_list.get_attribute_count:longint;
  3058. begin
  3059. if assigned(rtti_attributes) then
  3060. result:=rtti_attributes.Count
  3061. else
  3062. result:=0;
  3063. end;
  3064. procedure trtti_attribute_list.buildderef;
  3065. var
  3066. i : sizeint;
  3067. begin
  3068. if not assigned(rtti_attributes) then
  3069. exit;
  3070. for i:=0 to rtti_attributes.count-1 do
  3071. trtti_attribute(rtti_attributes[i]).buildderef;
  3072. end;
  3073. procedure trtti_attribute_list.deref;
  3074. var
  3075. i : sizeint;
  3076. begin
  3077. if not assigned(rtti_attributes) then
  3078. exit;
  3079. for i:=0 to rtti_attributes.count-1 do
  3080. trtti_attribute(rtti_attributes[i]).deref;
  3081. end;
  3082. class procedure trtti_attribute_list.ppuload_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  3083. var
  3084. i : sizeint;
  3085. begin
  3086. if assigned(attrlist) then
  3087. begin
  3088. if not assigned(attrlist.rtti_attributes) then
  3089. internalerror(2019071101);
  3090. for i:=0 to attrlist.rtti_attributes.count-1 do
  3091. trtti_attribute(attrlist.rtti_attributes[i]).ppuload_subentries(ppufile);
  3092. end;
  3093. end;
  3094. class procedure trtti_attribute_list.ppuwrite_subentries(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  3095. var
  3096. i : sizeint;
  3097. begin
  3098. if assigned(attrlist) and assigned(attrlist.rtti_attributes) then
  3099. begin
  3100. for i:=0 to attrlist.rtti_attributes.count-1 do
  3101. trtti_attribute(attrlist.rtti_attributes[i]).ppuwrite_subentries(ppufile);
  3102. end;
  3103. end;
  3104. class function trtti_attribute_list.ppuload(ppufile:tcompilerppufile):trtti_attribute_list;
  3105. var
  3106. cnt,i : longint;
  3107. begin
  3108. cnt:=ppufile.getlongint;
  3109. if cnt>0 then
  3110. begin
  3111. result:=trtti_attribute_list.create;
  3112. for i:=0 to cnt-1 do
  3113. result.addattribute(trtti_attribute.ppuload(ppufile));
  3114. end
  3115. else
  3116. result:=nil;
  3117. end;
  3118. class procedure trtti_attribute_list.ppuwrite(attrlist:trtti_attribute_list;ppufile:tcompilerppufile);
  3119. var
  3120. i : longint;
  3121. begin
  3122. if assigned(attrlist) and assigned(attrlist.rtti_attributes) then
  3123. begin
  3124. ppufile.putlongint(attrlist.rtti_attributes.count);
  3125. for i:=0 to attrlist.rtti_attributes.count-1 do
  3126. trtti_attribute(attrlist.rtti_attributes[i]).ppuwrite(ppufile);
  3127. end
  3128. else
  3129. ppufile.putlongint(0);
  3130. end;
  3131. {****************************************************************************
  3132. TORDDEF
  3133. ****************************************************************************}
  3134. constructor torddef.create(t : tordtype;v,b : TConstExprInt; doregister: boolean);
  3135. begin
  3136. inherited create(orddef,doregister);
  3137. low:=v;
  3138. high:=b;
  3139. ordtype:=t;
  3140. setsize;
  3141. end;
  3142. constructor torddef.ppuload(ppufile:tcompilerppufile);
  3143. begin
  3144. inherited ppuload(orddef,ppufile);
  3145. ordtype:=tordtype(ppufile.getbyte);
  3146. low:=ppufile.getexprint;
  3147. high:=ppufile.getexprint;
  3148. setsize;
  3149. ppuload_platform(ppufile);
  3150. end;
  3151. function torddef.getcopy : tstoreddef;
  3152. begin
  3153. result:=corddef.create(ordtype,low,high,true);
  3154. result.typ:=orddef;
  3155. torddef(result).low:=low;
  3156. torddef(result).high:=high;
  3157. torddef(result).ordtype:=ordtype;
  3158. torddef(result).savesize:=savesize;
  3159. end;
  3160. function torddef.alignment:shortint;
  3161. begin
  3162. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_ios]) and
  3163. (ordtype in [s64bit,u64bit]) then
  3164. result := 4
  3165. else
  3166. result := inherited alignment;
  3167. end;
  3168. procedure torddef.setsize;
  3169. const
  3170. sizetbl : array[tordtype] of longint = (
  3171. 0,
  3172. 1,2,4,8,16,
  3173. 1,2,4,8,16,
  3174. 1,1,2,4,8,
  3175. 1,2,4,8,
  3176. 1,2,8,system.high(longint)
  3177. );
  3178. begin
  3179. savesize:=sizetbl[ordtype];
  3180. if savesize=system.high(longint) then
  3181. savesize:=packedbitsize div 8;
  3182. end;
  3183. function torddef.packedbitsize: asizeint;
  3184. var
  3185. sizeval: tconstexprint;
  3186. power: longint;
  3187. begin
  3188. result := 0;
  3189. if ordtype = uvoid then
  3190. exit;
  3191. {$ifndef cpu64bitalu}
  3192. if (ordtype in [s64bit,u64bit]) then
  3193. {$else not cpu64bitalu}
  3194. if ((ordtype = u64bit) and
  3195. (high > system.high(int64))) or
  3196. ((ordtype = s64bit) and
  3197. ((low <= (system.low(int64) div 2)) or
  3198. ((low < 0) and
  3199. (high > (system.high(int64) div 2))))) then
  3200. {$endif cpu64bitalu}
  3201. result := 64
  3202. else if (
  3203. (low >= 0) and
  3204. (high <= 1)
  3205. ) or (
  3206. ordtype in [pasbool1,pasbool8,pasbool16,pasbool32,pasbool64,bool8bit,bool16bit,bool32bit,bool64bit]
  3207. ) then
  3208. result := 1
  3209. else
  3210. begin
  3211. if (low>=0) then
  3212. sizeval:=high
  3213. else
  3214. { don't count 0 twice, but take into account that range goes from -n-1..n }
  3215. sizeval:=(cutils.max(-low,high+1)*2)-1;
  3216. { 256 must become 512 etc. }
  3217. nextpowerof2(sizeval+1,power);
  3218. result := power;
  3219. end;
  3220. end;
  3221. function torddef.getvardef : longint;
  3222. const
  3223. basetype2vardef : array[tordtype] of longint = (
  3224. varUndefined,
  3225. varbyte,varword,varlongword,varqword,varUndefined,
  3226. varshortint,varsmallint,varinteger,varint64,varUndefined,
  3227. varboolean,varboolean,varboolean,varboolean,varboolean,
  3228. varboolean,varboolean,varUndefined,varUndefined,
  3229. varUndefined,varUndefined,varCurrency,varEmpty);
  3230. begin
  3231. result:=basetype2vardef[ordtype];
  3232. if result=varEmpty then
  3233. result:=basetype2vardef[range_to_basetype(low,high)];
  3234. end;
  3235. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  3236. begin
  3237. inherited ppuwrite(ppufile);
  3238. ppufile.putbyte(byte(ordtype));
  3239. ppufile.putexprint(low);
  3240. ppufile.putexprint(high);
  3241. writeentry(ppufile,iborddef);
  3242. end;
  3243. function torddef.is_publishable : tpublishproperty;
  3244. begin
  3245. if ordtype<>uvoid then
  3246. is_publishable:=pp_publish
  3247. else
  3248. is_publishable:=pp_error;
  3249. end;
  3250. function torddef.GetTypeName : string;
  3251. const
  3252. names : array[tordtype] of string[20] = (
  3253. 'untyped',
  3254. 'Byte','Word','DWord','QWord','UInt128',
  3255. 'ShortInt','SmallInt','LongInt','Int64','Int128',
  3256. 'Boolean','Boolean8','Boolean16','Boolean32','Boolean64',
  3257. 'ByteBool','WordBool','LongBool','QWordBool',
  3258. 'AnsiChar','WideChar','Currency','CustomRange');
  3259. begin
  3260. GetTypeName:=names[ordtype];
  3261. end;
  3262. {****************************************************************************
  3263. TFLOATDEF
  3264. ****************************************************************************}
  3265. constructor tfloatdef.create(t: tfloattype; doregister: boolean);
  3266. begin
  3267. inherited create(floatdef,doregister);
  3268. floattype:=t;
  3269. setsize;
  3270. end;
  3271. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  3272. begin
  3273. inherited ppuload(floatdef,ppufile);
  3274. floattype:=tfloattype(ppufile.getbyte);
  3275. setsize;
  3276. ppuload_platform(ppufile);
  3277. end;
  3278. function tfloatdef.getcopy : tstoreddef;
  3279. begin
  3280. result:=cfloatdef.create(floattype,true);
  3281. result.typ:=floatdef;
  3282. tfloatdef(result).savesize:=savesize;
  3283. end;
  3284. function tfloatdef.alignment:shortint;
  3285. begin
  3286. if (target_info.system in [system_i386_darwin,system_i386_iphonesim,system_arm_ios]) then
  3287. case floattype of
  3288. sc80real,
  3289. s80real: result:=16;
  3290. s64real,
  3291. s64currency,
  3292. s64comp : result:=4;
  3293. else
  3294. result := inherited alignment;
  3295. end
  3296. else
  3297. result := inherited alignment;
  3298. end;
  3299. function tfloatdef.structalignment: shortint;
  3300. begin
  3301. { aix is really annoying: the recommended scalar alignment for both
  3302. int64 and double is 64 bits, but in structs int64 has to be aligned
  3303. to 8 bytes and double to 4 bytes }
  3304. if (target_info.system in systems_aix) and
  3305. (floattype=s64real) then
  3306. result:=4
  3307. else
  3308. result:=alignment;
  3309. end;
  3310. procedure tfloatdef.setsize;
  3311. begin
  3312. case floattype of
  3313. s32real : savesize:=4;
  3314. s80real : savesize:=10;
  3315. sc80real:
  3316. if target_info.system in [system_i386_darwin,
  3317. system_i386_iphonesim,system_x86_64_darwin,
  3318. system_x86_64_iphonesim,
  3319. system_x86_64_linux,system_x86_64_freebsd,
  3320. system_x86_64_openbsd,system_x86_64_netbsd,
  3321. system_x86_64_solaris,system_x86_64_embedded,
  3322. system_x86_64_dragonfly,system_x86_64_haiku] then
  3323. savesize:=16
  3324. else
  3325. savesize:=12;
  3326. s64real,
  3327. s64currency,
  3328. s64comp : savesize:=8;
  3329. else
  3330. savesize:=0;
  3331. end;
  3332. end;
  3333. function tfloatdef.getvardef : longint;
  3334. const
  3335. floattype2vardef : array[tfloattype] of longint = (
  3336. varSingle,varDouble,varUndefined,varUndefined,
  3337. varUndefined,varCurrency,varUndefined);
  3338. begin
  3339. if (upper(typename)='TDATETIME') and
  3340. assigned(owner) and
  3341. assigned(owner.name) and
  3342. (owner.name^='SYSTEM') then
  3343. result:=varDate
  3344. else
  3345. result:=floattype2vardef[floattype];
  3346. end;
  3347. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  3348. begin
  3349. inherited ppuwrite(ppufile);
  3350. ppufile.putbyte(byte(floattype));
  3351. writeentry(ppufile,ibfloatdef);
  3352. end;
  3353. function tfloatdef.is_publishable : tpublishproperty;
  3354. begin
  3355. is_publishable:=pp_publish;
  3356. end;
  3357. function tfloatdef.GetTypeName : string;
  3358. const
  3359. names : array[tfloattype] of string[20] = (
  3360. 'Single','Double','Extended','CExtended','Comp','Currency','Float128');
  3361. begin
  3362. GetTypeName:=names[floattype];
  3363. end;
  3364. {****************************************************************************
  3365. TFILEDEF
  3366. ****************************************************************************}
  3367. constructor tfiledef.createtext;
  3368. begin
  3369. inherited create(filedef,true);
  3370. filetyp:=ft_text;
  3371. typedfiledef:=nil;
  3372. typedfiledefderef.reset;
  3373. end;
  3374. constructor tfiledef.createuntyped;
  3375. begin
  3376. inherited create(filedef,true);
  3377. filetyp:=ft_untyped;
  3378. typedfiledef:=nil;
  3379. end;
  3380. constructor tfiledef.createtyped(def:tdef);
  3381. begin
  3382. inherited create(filedef,true);
  3383. filetyp:=ft_typed;
  3384. typedfiledef:=def;
  3385. end;
  3386. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  3387. begin
  3388. inherited ppuload(filedef,ppufile);
  3389. filetyp:=tfiletyp(ppufile.getbyte);
  3390. if filetyp=ft_typed then
  3391. ppufile.getderef(typedfiledefderef)
  3392. else
  3393. typedfiledef:=nil;
  3394. ppuload_platform(ppufile);
  3395. end;
  3396. function tfiledef.getcopy : tstoreddef;
  3397. begin
  3398. case filetyp of
  3399. ft_typed:
  3400. result:=cfiledef.createtyped(typedfiledef);
  3401. ft_untyped:
  3402. result:=cfiledef.createuntyped;
  3403. ft_text:
  3404. result:=cfiledef.createtext;
  3405. end;
  3406. end;
  3407. procedure tfiledef.buildderef;
  3408. begin
  3409. inherited buildderef;
  3410. if filetyp=ft_typed then
  3411. typedfiledefderef.build(typedfiledef);
  3412. end;
  3413. procedure tfiledef.deref;
  3414. begin
  3415. inherited deref;
  3416. if filetyp=ft_typed then
  3417. typedfiledef:=tdef(typedfiledefderef.resolve);
  3418. end;
  3419. function tfiledef.size:asizeint;
  3420. begin
  3421. if savesize=0 then
  3422. setsize;
  3423. size:=savesize;
  3424. end;
  3425. procedure tfiledef.setsize;
  3426. begin
  3427. case filetyp of
  3428. ft_text:
  3429. savesize:=search_system_type('TEXTREC').typedef.size;
  3430. ft_typed:
  3431. begin
  3432. savesize:=search_system_type('FILEREC').typedef.size;
  3433. { allocate put/get buffer in iso mode }
  3434. if m_isolike_io in current_settings.modeswitches then
  3435. inc(savesize,typedfiledef.size);
  3436. end;
  3437. ft_untyped:
  3438. savesize:=search_system_type('FILEREC').typedef.size;
  3439. end;
  3440. end;
  3441. function tfiledef.alignment: shortint;
  3442. begin
  3443. case filetyp of
  3444. ft_text:
  3445. result:=search_system_type('TEXTREC').typedef.alignment;
  3446. ft_typed,
  3447. ft_untyped:
  3448. result:=search_system_type('FILEREC').typedef.alignment;
  3449. end;
  3450. end;
  3451. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  3452. begin
  3453. inherited ppuwrite(ppufile);
  3454. ppufile.putbyte(byte(filetyp));
  3455. if filetyp=ft_typed then
  3456. ppufile.putderef(typedfiledefderef);
  3457. writeentry(ppufile,ibfiledef);
  3458. end;
  3459. function tfiledef.GetTypeName : string;
  3460. begin
  3461. case filetyp of
  3462. ft_untyped:
  3463. GetTypeName:='File';
  3464. ft_typed:
  3465. GetTypeName:='File Of '+typedfiledef.typename;
  3466. ft_text:
  3467. GetTypeName:='Text'
  3468. end;
  3469. end;
  3470. function tfiledef.getmangledparaname : TSymStr;
  3471. begin
  3472. case filetyp of
  3473. ft_untyped:
  3474. getmangledparaname:='FILE';
  3475. ft_typed:
  3476. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  3477. ft_text:
  3478. getmangledparaname:='TEXT'
  3479. end;
  3480. end;
  3481. {****************************************************************************
  3482. TVARIANTDEF
  3483. ****************************************************************************}
  3484. constructor tvariantdef.create(v : tvarianttype);
  3485. begin
  3486. inherited create(variantdef,true);
  3487. varianttype:=v;
  3488. setsize;
  3489. end;
  3490. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  3491. begin
  3492. inherited ppuload(variantdef,ppufile);
  3493. varianttype:=tvarianttype(ppufile.getbyte);
  3494. setsize;
  3495. ppuload_platform(ppufile);
  3496. end;
  3497. function tvariantdef.getcopy : tstoreddef;
  3498. begin
  3499. result:=cvariantdef.create(varianttype);
  3500. end;
  3501. function tvariantdef.alignment: shortint;
  3502. begin
  3503. result:=search_system_type('TVARDATA').typedef.alignment;
  3504. end;
  3505. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  3506. begin
  3507. inherited ppuwrite(ppufile);
  3508. ppufile.putbyte(byte(varianttype));
  3509. writeentry(ppufile,ibvariantdef);
  3510. end;
  3511. function tvariantdef.getvardef : longint;
  3512. begin
  3513. Result:=varVariant;
  3514. end;
  3515. procedure tvariantdef.setsize;
  3516. begin
  3517. {$ifdef cpu64bitaddr}
  3518. savesize:=24;
  3519. {$else cpu64bitaddr}
  3520. savesize:=16;
  3521. {$endif cpu64bitaddr}
  3522. end;
  3523. function tvariantdef.GetTypeName : string;
  3524. begin
  3525. case varianttype of
  3526. vt_normalvariant:
  3527. GetTypeName:='Variant';
  3528. vt_olevariant:
  3529. GetTypeName:='OleVariant';
  3530. end;
  3531. end;
  3532. function tvariantdef.needs_inittable : boolean;
  3533. begin
  3534. needs_inittable:=true;
  3535. end;
  3536. function tvariantdef.is_publishable : tpublishproperty;
  3537. begin
  3538. is_publishable:=pp_publish;
  3539. end;
  3540. {****************************************************************************
  3541. TABSTRACtpointerdef
  3542. ****************************************************************************}
  3543. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  3544. begin
  3545. inherited create(dt,true);
  3546. pointeddef:=def;
  3547. pointeddefderef.reset;
  3548. if df_generic in pointeddef.defoptions then
  3549. include(defoptions,df_generic);
  3550. if df_specialization in pointeddef.defoptions then
  3551. include(defoptions,df_specialization);
  3552. end;
  3553. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  3554. begin
  3555. inherited ppuload(dt,ppufile);
  3556. ppufile.getderef(pointeddefderef);
  3557. end;
  3558. procedure tabstractpointerdef.buildderef;
  3559. begin
  3560. inherited buildderef;
  3561. pointeddefderef.build(pointeddef);
  3562. end;
  3563. procedure tabstractpointerdef.deref;
  3564. begin
  3565. inherited deref;
  3566. pointeddef:=tdef(pointeddefderef.resolve);
  3567. end;
  3568. function tabstractpointerdef.size: asizeint;
  3569. begin
  3570. Result:=voidpointertype.size;
  3571. end;
  3572. function tabstractpointerdef.alignment: shortint;
  3573. begin
  3574. alignment:=size_2_align(voidpointertype.size);
  3575. end;
  3576. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3577. begin
  3578. inherited ppuwrite(ppufile);
  3579. ppufile.putderef(pointeddefderef);
  3580. end;
  3581. {****************************************************************************
  3582. tpointerdef
  3583. ****************************************************************************}
  3584. constructor tpointerdef.create(def:tdef);
  3585. begin
  3586. inherited create(pointerdef,def);
  3587. has_pointer_math:=cs_pointermath in current_settings.localswitches;
  3588. if (df_specialization in tstoreddef(def).defoptions)
  3589. {$ifndef genericdef_for_nested}
  3590. { currently, nested procdefs of generic routines get df_specialization,
  3591. but no genericdef }
  3592. and assigned(tstoreddef(def).genericdef)
  3593. {$endif}
  3594. then
  3595. genericdef:=cpointerdef.getreusable(tstoreddef(def).genericdef);
  3596. end;
  3597. class function tpointerdef.getreusable(def: tdef): tpointerdef;
  3598. var
  3599. res: PHashSetItem;
  3600. oldsymtablestack: tsymtablestack;
  3601. begin
  3602. if not assigned(current_module) then
  3603. internalerror(2011071101);
  3604. res:=current_module.ptrdefs.FindOrAdd(@def,sizeof(def));
  3605. if not assigned(res^.Data) then
  3606. begin
  3607. { since these pointerdefs can be reused anywhere in the current
  3608. unit, add them to the global/staticsymtable (or local symtable
  3609. if they're a local def, because otherwise they'll be saved
  3610. to the ppu referencing a local symtable entry that doesn't
  3611. exist in the ppu) }
  3612. oldsymtablestack:=symtablestack;
  3613. { do not simply push/pop current_module.localsymtable, because
  3614. that can have side-effects (e.g., it removes helpers) }
  3615. symtablestack:=nil;
  3616. result:=cpointerdef.create(def);
  3617. setup_reusable_def(def,result,res,oldsymtablestack);
  3618. { res^.Data may still be nil -> don't overwrite result }
  3619. exit;
  3620. end;
  3621. result:=tpointerdef(res^.Data);
  3622. end;
  3623. class function tpointerdef.getreusable_no_free(def: tdef): tpointerdef;
  3624. begin
  3625. result:=getreusable(def);
  3626. if not result.is_registered then
  3627. include(result.defoptions,df_not_registered_no_free);
  3628. end;
  3629. function tpointerdef.size: asizeint;
  3630. begin
  3631. result:=sizeof(pint);
  3632. end;
  3633. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  3634. begin
  3635. inherited ppuload(pointerdef,ppufile);
  3636. has_pointer_math:=(ppufile.getbyte<>0);
  3637. ppuload_platform(ppufile);
  3638. end;
  3639. function tpointerdef.getcopy : tstoreddef;
  3640. begin
  3641. { don't use direct pointeddef if it is a forwarddef because in other case
  3642. one of them will be destroyed on forward type resolve and the second will
  3643. point to garbage }
  3644. if pointeddef.typ=forwarddef then
  3645. result:=cpointerdef.create(tforwarddef(pointeddef).getcopy)
  3646. else
  3647. result:=cpointerdef.create(pointeddef);
  3648. tpointerdef(result).has_pointer_math:=has_pointer_math;
  3649. tpointerdef(result).savesize:=savesize;
  3650. end;
  3651. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  3652. begin
  3653. inherited ppuwrite(ppufile);
  3654. ppufile.putbyte(byte(has_pointer_math));
  3655. writeentry(ppufile,ibpointerdef);
  3656. end;
  3657. function tpointerdef.GetTypeName : string;
  3658. begin
  3659. { parameter types and the resultdef of a procvardef can contain a
  3660. pointer to this procvardef itself, resulting in endless recursion ->
  3661. use the typesym's name instead if it exists (if it doesn't, such as
  3662. for anynonymous procedure types in macpas/iso mode, then there cannot
  3663. be any recursive references to it either) }
  3664. if (pointeddef.typ<>procvardef) or
  3665. not assigned(pointeddef.typesym) then
  3666. GetTypeName:='^'+pointeddef.typename
  3667. else
  3668. GetTypeName:='^'+pointeddef.typesym.realname;
  3669. end;
  3670. function tpointerdef.pointer_arithmetic_int_type:tdef;
  3671. begin
  3672. result:=ptrsinttype;
  3673. end;
  3674. function tpointerdef.pointer_arithmetic_uint_type:tdef;
  3675. begin
  3676. result:=ptruinttype;
  3677. end;
  3678. function tpointerdef.pointer_subtraction_result_type:tdef;
  3679. begin
  3680. result:=ptrsinttype;
  3681. end;
  3682. function tpointerdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  3683. begin
  3684. result:=true;
  3685. end;
  3686. function tpointerdef.converted_pointer_to_array_range_type:tdef;
  3687. begin
  3688. result:=ptrsinttype;
  3689. end;
  3690. {****************************************************************************
  3691. TCLASSREFDEF
  3692. ****************************************************************************}
  3693. constructor tclassrefdef.create(def:tdef);
  3694. begin
  3695. while (def.typ=objectdef) and tobjectdef(def).is_unique_objpasdef do
  3696. def:=tobjectdef(def).childof;
  3697. inherited create(classrefdef,def);
  3698. if df_specialization in tstoreddef(def).defoptions then
  3699. genericdef:=cclassrefdef.create(tstoreddef(def).genericdef);
  3700. end;
  3701. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  3702. begin
  3703. inherited ppuload(classrefdef,ppufile);
  3704. ppuload_platform(ppufile);
  3705. end;
  3706. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  3707. begin
  3708. inherited ppuwrite(ppufile);
  3709. writeentry(ppufile,ibclassrefdef);
  3710. end;
  3711. function tclassrefdef.getcopy:tstoreddef;
  3712. begin
  3713. if pointeddef.typ=forwarddef then
  3714. result:=cclassrefdef.create(tforwarddef(pointeddef).getcopy)
  3715. else
  3716. result:=cclassrefdef.create(pointeddef);
  3717. end;
  3718. function tclassrefdef.GetTypeName : string;
  3719. begin
  3720. GetTypeName:='Class Of '+pointeddef.typename;
  3721. end;
  3722. function tclassrefdef.is_publishable : tpublishproperty;
  3723. begin
  3724. is_publishable:=pp_publish;
  3725. end;
  3726. function tclassrefdef.rtti_mangledname(rt: trttitype): TSymStr;
  3727. begin
  3728. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  3729. result:=inherited rtti_mangledname(rt)
  3730. else
  3731. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  3732. end;
  3733. procedure tclassrefdef.register_created_object_type;
  3734. begin
  3735. tobjectdef(pointeddef).register_created_classref_type;
  3736. end;
  3737. {***************************************************************************
  3738. TSETDEF
  3739. ***************************************************************************}
  3740. constructor tsetdef.create(def: tdef; low, high: asizeint; doregister: boolean);
  3741. var
  3742. setallocbits: aint;
  3743. packedsavesize: aint;
  3744. actual_setalloc: ShortInt;
  3745. begin
  3746. inherited create(setdef,doregister);
  3747. elementdef:=def;
  3748. elementdefderef.reset;
  3749. setmax:=high;
  3750. actual_setalloc:=current_settings.setalloc;
  3751. {$if defined(cpu8bitalu) or defined(cpu16bitalu)}
  3752. if actual_setalloc=0 then
  3753. actual_setalloc:=1;
  3754. {$endif}
  3755. if (actual_setalloc=0) then
  3756. begin
  3757. setbase:=0;
  3758. if (high<32) then
  3759. savesize:=Sizeof(longint)
  3760. else if (high<256) then
  3761. savesize:=32
  3762. else
  3763. savesize:=(high+7) div 8
  3764. end
  3765. else
  3766. begin
  3767. setallocbits:=actual_setalloc*8;
  3768. setbase:=low and not(setallocbits-1);
  3769. packedsavesize:=actual_setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  3770. savesize:=packedsavesize;
  3771. {$if not defined(cpu8bitalu) and not defined(cpu16bitalu)}
  3772. if savesize=3 then
  3773. savesize:=4;
  3774. {$endif}
  3775. end;
  3776. end;
  3777. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  3778. begin
  3779. inherited ppuload(setdef,ppufile);
  3780. ppufile.getderef(elementdefderef);
  3781. savesize:=ppufile.getasizeint;
  3782. setbase:=ppufile.getasizeint;
  3783. setmax:=ppufile.getasizeint;
  3784. ppuload_platform(ppufile);
  3785. end;
  3786. function tsetdef.getcopy : tstoreddef;
  3787. begin
  3788. result:=csetdef.create(elementdef,setbase,setmax,true);
  3789. { the copy might have been created with a different setalloc setting }
  3790. tsetdef(result).savesize:=savesize;
  3791. end;
  3792. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  3793. begin
  3794. inherited ppuwrite(ppufile);
  3795. ppufile.putderef(elementdefderef);
  3796. ppufile.putasizeint(savesize);
  3797. ppufile.putasizeint(setbase);
  3798. ppufile.putasizeint(setmax);
  3799. writeentry(ppufile,ibsetdef);
  3800. end;
  3801. procedure tsetdef.buildderef;
  3802. begin
  3803. inherited buildderef;
  3804. elementdefderef.build(elementdef);
  3805. end;
  3806. procedure tsetdef.deref;
  3807. begin
  3808. inherited deref;
  3809. elementdef:=tdef(elementdefderef.resolve);
  3810. end;
  3811. function tsetdef.is_publishable : tpublishproperty;
  3812. begin
  3813. if savesize in [1,2,4] then
  3814. is_publishable:=pp_publish
  3815. else
  3816. is_publishable:=pp_error;
  3817. end;
  3818. function tsetdef.alignment: shortint;
  3819. begin
  3820. Result:=inherited;
  3821. if result>sizeof(aint) then
  3822. result:=sizeof(aint);
  3823. end;
  3824. function tsetdef.GetTypeName : string;
  3825. begin
  3826. if assigned(elementdef) then
  3827. GetTypeName:='Set Of '+elementdef.typename
  3828. else
  3829. GetTypeName:='Empty Set';
  3830. end;
  3831. {***************************************************************************
  3832. TFORMALDEF
  3833. ***************************************************************************}
  3834. constructor tformaldef.create(Atyped:boolean);
  3835. begin
  3836. inherited create(formaldef,true);
  3837. typed:=Atyped;
  3838. savesize:=0;
  3839. end;
  3840. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  3841. begin
  3842. inherited ppuload(formaldef,ppufile);
  3843. typed:=boolean(ppufile.getbyte);
  3844. savesize:=0;
  3845. ppuload_platform(ppufile);
  3846. end;
  3847. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  3848. begin
  3849. inherited ppuwrite(ppufile);
  3850. ppufile.putbyte(byte(typed));
  3851. writeentry(ppufile,ibformaldef);
  3852. end;
  3853. function tformaldef.GetTypeName : string;
  3854. begin
  3855. if typed then
  3856. GetTypeName:='<Typed formal type>'
  3857. else
  3858. GetTypeName:='<Formal type>';
  3859. end;
  3860. {***************************************************************************
  3861. TARRAYDEF
  3862. ***************************************************************************}
  3863. constructor tarraydef.create(l, h: asizeint; def: tdef);
  3864. begin
  3865. inherited create(arraydef,true);
  3866. lowrange:=l;
  3867. highrange:=h;
  3868. rangedef:=def;
  3869. rangedefderef.reset;
  3870. _elementdef:=nil;
  3871. _elementdefderef.reset;
  3872. arrayoptions:=[];
  3873. symtable:=tarraysymtable.create(self);
  3874. end;
  3875. constructor tarraydef.create_vector(l ,h: asizeint; def: tdef);
  3876. begin
  3877. self.create(l,h,def);
  3878. include(arrayoptions,ado_IsVector);
  3879. end;
  3880. constructor tarraydef.create_openarray;
  3881. begin
  3882. self.create(0,-1,sizesinttype);
  3883. include(arrayoptions,ado_OpenArray);
  3884. end;
  3885. class function tarraydef.getreusable_intern(def: tdef; elems: asizeint; const options: tarraydefoptions): tarraydef;
  3886. var
  3887. res: PHashSetItem;
  3888. oldsymtablestack: tsymtablestack;
  3889. arrdesc: packed record
  3890. def: tdef;
  3891. elecount: asizeint;
  3892. options: tarraydefoptions
  3893. end;
  3894. begin
  3895. if not assigned(current_module) then
  3896. internalerror(2011081301);
  3897. arrdesc.def:=def;
  3898. arrdesc.elecount:=elems;
  3899. arrdesc.options:=options;
  3900. res:=current_module.arraydefs.FindOrAdd(@arrdesc,sizeof(arrdesc));
  3901. if not assigned(res^.Data) then
  3902. begin
  3903. { since these pointerdefs can be reused anywhere in the current
  3904. unit, add them to the global/staticsymtable (or local symtable
  3905. if they're a local def, because otherwise they'll be saved
  3906. to the ppu referencing a local symtable entry that doesn't
  3907. exist in the ppu) }
  3908. oldsymtablestack:=symtablestack;
  3909. { do not simply push/pop current_module.localsymtable, because
  3910. that can have side-effects (e.g., it removes helpers) }
  3911. symtablestack:=nil;
  3912. result:=carraydef.create(0,elems-1,sizesinttype);
  3913. result.elementdef:=def;
  3914. result.arrayoptions:=options;
  3915. setup_reusable_def(def,result,res,oldsymtablestack);
  3916. { res^.Data may still be nil -> don't overwrite result }
  3917. exit;
  3918. end;
  3919. result:=tarraydef(res^.Data);
  3920. end;
  3921. class function tarraydef.getreusable(def: tdef; elems: asizeint): tarraydef;
  3922. begin
  3923. result:=getreusable_intern(def,elems,[]);
  3924. end;
  3925. class function tarraydef.getreusable_vector(def: tdef; elems: asizeint): tarraydef;
  3926. begin
  3927. result:=getreusable_intern(def,elems,[ado_IsVector]);
  3928. end;
  3929. class function tarraydef.getreusable_no_free(def: tdef; elems: asizeint): tarraydef;
  3930. begin
  3931. result:=getreusable(def,elems);
  3932. if not result.is_registered then
  3933. include(result.defoptions,df_not_registered_no_free);
  3934. end;
  3935. class function tarraydef.getreusable_no_free_vector(def: tdef; elems: asizeint): tarraydef;
  3936. begin
  3937. result:=getreusable_vector(def,elems);
  3938. if not result.is_registered then
  3939. include(result.defoptions,df_not_registered_no_free);
  3940. end;
  3941. destructor tarraydef.destroy;
  3942. begin
  3943. symtable.free;
  3944. symtable:=nil;
  3945. inherited;
  3946. end;
  3947. constructor tarraydef.create_from_pointer(def:tpointerdef);
  3948. begin
  3949. { divide by the element size and do -1 so the array will have a valid size,
  3950. further, the element size might be 0 e.g. for empty records, so use max(...,1)
  3951. to avoid a division by zero }
  3952. self.create(0,(high(asizeint) div max(def.pointeddef.size,1))-1,
  3953. def.converted_pointer_to_array_range_type);
  3954. arrayoptions:=[ado_IsConvertedPointer];
  3955. setelementdef(def.pointeddef);
  3956. end;
  3957. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  3958. begin
  3959. inherited ppuload(arraydef,ppufile);
  3960. { the addresses are calculated later }
  3961. ppufile.getderef(_elementdefderef);
  3962. ppufile.getderef(rangedefderef);
  3963. lowrange:=ppufile.getasizeint;
  3964. highrange:=ppufile.getasizeint;
  3965. ppufile.getset(tppuset2(arrayoptions));
  3966. ppuload_platform(ppufile);
  3967. symtable:=tarraysymtable.create(self);
  3968. tarraysymtable(symtable).ppuload(ppufile)
  3969. end;
  3970. function tarraydef.getcopy : tstoreddef;
  3971. begin
  3972. result:=carraydef.create(lowrange,highrange,rangedef);
  3973. tarraydef(result).arrayoptions:=arrayoptions;
  3974. tarraydef(result)._elementdef:=_elementdef;
  3975. end;
  3976. procedure tarraydef.buildderef;
  3977. begin
  3978. inherited buildderef;
  3979. tarraysymtable(symtable).buildderef;
  3980. _elementdefderef.build(_elementdef);
  3981. rangedefderef.build(rangedef);
  3982. end;
  3983. procedure tarraydef.deref;
  3984. begin
  3985. inherited deref;
  3986. tarraysymtable(symtable).deref(false);
  3987. _elementdef:=tdef(_elementdefderef.resolve);
  3988. rangedef:=tdef(rangedefderef.resolve);
  3989. end;
  3990. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  3991. begin
  3992. inherited ppuwrite(ppufile);
  3993. ppufile.putderef(_elementdefderef);
  3994. ppufile.putderef(rangedefderef);
  3995. ppufile.putasizeint(lowrange);
  3996. ppufile.putasizeint(highrange);
  3997. ppufile.putset(tppuset2(arrayoptions));
  3998. writeentry(ppufile,ibarraydef);
  3999. tarraysymtable(symtable).ppuwrite(ppufile);
  4000. end;
  4001. function tarraydef.elesize : asizeint;
  4002. begin
  4003. if (ado_IsBitPacked in arrayoptions) then
  4004. internalerror(2006080101);
  4005. if assigned(_elementdef) then
  4006. result:=_elementdef.size
  4007. else
  4008. result:=0;
  4009. end;
  4010. function tarraydef.elepackedbitsize : asizeint;
  4011. begin
  4012. if not(ado_IsBitPacked in arrayoptions) then
  4013. internalerror(2006080102);
  4014. if assigned(_elementdef) then
  4015. result:=_elementdef.packedbitsize
  4016. else
  4017. result:=0;
  4018. end;
  4019. function tarraydef.elecount : asizeuint;
  4020. var
  4021. qhigh,qlow : qword;
  4022. begin
  4023. if ado_IsDynamicArray in arrayoptions then
  4024. begin
  4025. result:=0;
  4026. exit;
  4027. end;
  4028. { check whether the range might be larger than high(asizeint). Has
  4029. to include 0..high(sizeint), since that's high(sizeint)+1 elements }
  4030. if (highrange>=0) and (lowrange<=0) then
  4031. begin
  4032. qhigh:=highrange;
  4033. if lowrange=low(asizeint) then
  4034. qlow:=high(asizeint) + 1
  4035. else
  4036. qlow:=qword(-lowrange);
  4037. { prevent overflow, return 0 to indicate overflow }
  4038. if qhigh+qlow>qword(high(asizeint)-1) then
  4039. result:=0
  4040. else
  4041. result:=qhigh+qlow+1;
  4042. end
  4043. else
  4044. result:=int64(highrange)-lowrange+1;
  4045. end;
  4046. function tarraydef.size : asizeint;
  4047. var
  4048. cachedelecount : asizeuint;
  4049. cachedelesize : asizeint;
  4050. begin
  4051. if ado_IsDynamicArray in arrayoptions then
  4052. begin
  4053. size:=voidpointertype.size;
  4054. exit;
  4055. end;
  4056. { Tarraydef.size may never be called for an open array! }
  4057. if ado_OpenArray in arrayoptions then
  4058. internalerror(99080501);
  4059. if not (ado_IsBitPacked in arrayoptions) then
  4060. cachedelesize:=elesize
  4061. else
  4062. cachedelesize := elepackedbitsize;
  4063. cachedelecount:=elecount;
  4064. if (cachedelesize = 0) then
  4065. begin
  4066. size := 0;
  4067. exit;
  4068. end;
  4069. if (cachedelecount = 0) then
  4070. begin
  4071. if ado_isconststring in arrayoptions then
  4072. size := 0
  4073. else
  4074. size := -1;
  4075. exit;
  4076. end;
  4077. { prevent overflow, return -1 to indicate overflow }
  4078. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  4079. if (cachedelecount > asizeuint(high(asizeint))) or
  4080. ((high(asizeint) div cachedelesize) < asizeint(cachedelecount)) or
  4081. { also lowrange*elesize must be < high(asizeint) to prevent overflow when
  4082. accessing the array, see ncgmem (PFV) }
  4083. ((high(asizeint) div cachedelesize) < abs(lowrange)) then
  4084. begin
  4085. result:=-1;
  4086. exit;
  4087. end;
  4088. result:=cachedelesize*asizeint(cachedelecount);
  4089. if (ado_IsBitPacked in arrayoptions) then
  4090. { can't just add 7 and divide by 8, because that may overflow }
  4091. result:=result div 8 + ord((result mod 8)<>0);
  4092. {$ifdef cpu16bitaddr}
  4093. if result>65535 then
  4094. begin
  4095. result:=-1;
  4096. exit;
  4097. end;
  4098. {$endif cpu16bitaddr}
  4099. end;
  4100. procedure tarraydef.setelementdef(def:tdef);
  4101. begin
  4102. _elementdef:=def;
  4103. if not(
  4104. (ado_IsDynamicArray in arrayoptions) or
  4105. (ado_IsConvertedPointer in arrayoptions) or
  4106. (ado_IsConstructor in arrayoptions) or
  4107. (ado_IsGeneric in arrayoptions) or
  4108. (highrange<lowrange)
  4109. ) and
  4110. (size=-1) then
  4111. Message(sym_e_segment_too_large);
  4112. end;
  4113. function tarraydef.alignment : shortint;
  4114. begin
  4115. if ado_IsVector in arrayoptions then
  4116. alignment:=size
  4117. { alignment of dyn. arrays doesn't depend on the element size }
  4118. else if ado_IsDynamicArray in arrayoptions then
  4119. alignment:=voidpointertype.alignment
  4120. { alignment is the target alignment for the used load size }
  4121. else if (ado_IsBitPacked in arrayoptions) and
  4122. (elementdef.typ in [enumdef,orddef]) then
  4123. alignment:=cgsize_orddef(int_cgsize(packedbitsloadsize(elepackedbitsize))).alignment
  4124. { alignment is the alignment of the elements }
  4125. else
  4126. alignment:=elementdef.alignment
  4127. end;
  4128. function tarraydef.needs_inittable : boolean;
  4129. begin
  4130. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  4131. end;
  4132. function tarraydef.needs_separate_initrtti : boolean;
  4133. begin
  4134. if ado_IsBitPacked in arrayoptions then
  4135. result:=false
  4136. else
  4137. result:=elementdef.needs_separate_initrtti;
  4138. end;
  4139. function tarraydef.GetTypeName : string;
  4140. begin
  4141. if (ado_IsConstString in arrayoptions) then
  4142. result:='Constant String'
  4143. else if (ado_isarrayofconst in arrayoptions) or
  4144. (ado_isConstructor in arrayoptions) then
  4145. begin
  4146. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  4147. GetTypeName:='Array Of Const'
  4148. else
  4149. GetTypeName:='{Array Of Const/Constant Open} Array of '+elementdef.typename;
  4150. end
  4151. else if (ado_IsDynamicArray in arrayoptions) then
  4152. GetTypeName:='{Dynamic} Array Of '+elementdef.typename
  4153. else if (ado_OpenArray in arrayoptions) then
  4154. GetTypeName:='{Open} Array Of '+elementdef.typename
  4155. else
  4156. begin
  4157. result := '';
  4158. if (ado_IsBitPacked in arrayoptions) then
  4159. result:='BitPacked ';
  4160. if rangedef.typ=enumdef then
  4161. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  4162. else
  4163. result:=result+'Array['+tostr(lowrange)+'..'+
  4164. tostr(highrange)+'] Of '+elementdef.typename
  4165. end;
  4166. end;
  4167. function tarraydef.getmangledparaname : TSymStr;
  4168. begin
  4169. if ado_isarrayofconst in arrayoptions then
  4170. getmangledparaname:='array_of_const'
  4171. else
  4172. if ((highrange=-1) and (lowrange=0)) then
  4173. getmangledparaname:='array_of_'+elementdef.mangledparaname
  4174. else
  4175. internalerror(200204176);
  4176. end;
  4177. function tarraydef.is_publishable : tpublishproperty;
  4178. begin
  4179. if ado_IsDynamicArray in arrayoptions then
  4180. is_publishable:=pp_publish
  4181. else
  4182. is_publishable:=pp_error;
  4183. end;
  4184. function tarraydef.is_hwvector: boolean;
  4185. begin
  4186. result:=ado_IsVector in arrayoptions;
  4187. end;
  4188. {***************************************************************************
  4189. tabstractrecorddef
  4190. ***************************************************************************}
  4191. constructor tabstractrecorddef.create(const n:string; dt:tdeftyp;doregister:boolean);
  4192. begin
  4193. inherited create(dt,doregister);
  4194. objname:=stringdup(upper(n));
  4195. objrealname:=stringdup(n);
  4196. objectoptions:=[];
  4197. if assigned(current_module.namespace) then
  4198. begin
  4199. import_lib:=stringdup(current_module.namespace^);
  4200. replace(import_lib^,'.','/');
  4201. end;
  4202. end;
  4203. constructor tabstractrecorddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  4204. var
  4205. ro: trtti_option;
  4206. begin
  4207. inherited ppuload(dt,ppufile);
  4208. objrealname:=ppufile.getpshortstring;
  4209. objname:=stringdup(upper(objrealname^));
  4210. import_lib:=ppufile.getpshortstring;
  4211. { only used for external C++ classes and Java classes/records }
  4212. if (import_lib^='') then
  4213. stringdispose(import_lib);
  4214. ppufile.getset(tppuset4(objectoptions));
  4215. rtti.clause:=trtti_clause(ppufile.getbyte);
  4216. for ro in trtti_option do
  4217. ppufile.getset(tppuset1(rtti.options[ro]));
  4218. end;
  4219. procedure tabstractrecorddef.ppuwrite(ppufile: tcompilerppufile);
  4220. var
  4221. ro: trtti_option;
  4222. begin
  4223. inherited ppuwrite(ppufile);
  4224. ppufile.putstring(objrealname^);
  4225. if assigned(import_lib) then
  4226. ppufile.putstring(import_lib^)
  4227. else
  4228. ppufile.putstring('');
  4229. ppufile.putset(tppuset4(objectoptions));
  4230. ppufile.putbyte(byte(rtti.clause));
  4231. for ro in trtti_option do
  4232. ppufile.putset(tppuset1(rtti.options[ro]));
  4233. end;
  4234. destructor tabstractrecorddef.destroy;
  4235. begin
  4236. stringdispose(objname);
  4237. stringdispose(objrealname);
  4238. stringdispose(import_lib);
  4239. tcinitcode.free;
  4240. inherited destroy;
  4241. end;
  4242. procedure tabstractrecorddef.buildderefimpl;
  4243. begin
  4244. inherited buildderefimpl;
  4245. if not (df_copied_def in defoptions) then
  4246. tstoredsymtable(symtable).buildderefimpl;
  4247. end;
  4248. procedure tabstractrecorddef.derefimpl;
  4249. begin
  4250. inherited derefimpl;
  4251. if not (df_copied_def in defoptions) then
  4252. tstoredsymtable(symtable).derefimpl(false);
  4253. end;
  4254. procedure tabstractrecorddef.check_forwards;
  4255. begin
  4256. { the defs of a copied def are defined for the original type only }
  4257. if not(df_copied_def in defoptions) then
  4258. tstoredsymtable(symtable).check_forwards;
  4259. end;
  4260. function tabstractrecorddef.find_procdef_bytype(pt:tproctypeoption): tprocdef;
  4261. var
  4262. i: longint;
  4263. sym: tsym;
  4264. begin
  4265. for i:=0 to symtable.SymList.Count-1 do
  4266. begin
  4267. sym:=tsym(symtable.SymList[i]);
  4268. if sym.typ=procsym then
  4269. begin
  4270. result:=tprocsym(sym).find_procdef_bytype(pt);
  4271. if assigned(result) then
  4272. exit;
  4273. end;
  4274. end;
  4275. result:=nil;
  4276. end;
  4277. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  4278. begin
  4279. if t=gs_record then
  4280. GetSymtable:=symtable
  4281. else
  4282. GetSymtable:=nil;
  4283. end;
  4284. function tabstractrecorddef.is_packed:boolean;
  4285. begin
  4286. result:=tabstractrecordsymtable(symtable).is_packed;
  4287. end;
  4288. function tabstractrecorddef.RttiName: string;
  4289. function generate_full_paramname(maxlength:longint):string;
  4290. const
  4291. commacount : array[boolean] of longint = (0,1);
  4292. var
  4293. fullparas,
  4294. paramname : ansistring;
  4295. module : tmodule;
  4296. sym : tsym;
  4297. def : tdef;
  4298. i : longint;
  4299. begin
  4300. { we want at least enough space for an ellipsis }
  4301. if maxlength<3 then
  4302. internalerror(2014121203);
  4303. fullparas:='';
  4304. for i:=0 to genericparas.count-1 do
  4305. begin
  4306. sym:=tsym(genericparas[i]);
  4307. module:=find_module_from_symtable(sym.owner);
  4308. if not assigned(module) then
  4309. internalerror(2014121202);
  4310. if not (sym.typ in [constsym,symconst.typesym]) then
  4311. internalerror(2020042501);
  4312. if sym.typ=constsym then
  4313. def:=tconstsym(sym).constdef
  4314. else
  4315. def:=ttypesym(sym).typedef;
  4316. paramname:=module.realmodulename^;
  4317. if def.typ in [objectdef,recorddef] then
  4318. paramname:=paramname+'.'+tabstractrecorddef(def).rttiname
  4319. else
  4320. paramname:=paramname+'.'+def.typename;
  4321. if length(fullparas)+commacount[i>0]+length(paramname)>maxlength then
  4322. begin
  4323. if i>0 then
  4324. fullparas:=fullparas+',...'
  4325. else
  4326. fullparas:=fullparas+'...';
  4327. break;
  4328. end;
  4329. { could we fit an ellipsis after this parameter if it should be too long? }
  4330. if (maxlength-(length(fullparas)+commacount[i>0]+length(paramname))<4) and (i<genericparas.count-1) then
  4331. begin
  4332. { then omit already this parameter }
  4333. if i>0 then
  4334. fullparas:=fullparas+',...'
  4335. else
  4336. fullparas:=fullparas+'...';
  4337. break;
  4338. end;
  4339. if i>0 then
  4340. fullparas:=fullparas+',';
  4341. fullparas:=fullparas+paramname;
  4342. end;
  4343. result:=fullparas;
  4344. end;
  4345. var
  4346. nongeneric,
  4347. basename : string;
  4348. i,
  4349. remlength,
  4350. paramcount,
  4351. crcidx : longint;
  4352. begin
  4353. if rttistring='' then
  4354. begin
  4355. if is_specialization then
  4356. begin
  4357. rttistring:=OwnerHierarchyName;
  4358. { there should be two $ characters, one before the CRC and one before the count }
  4359. crcidx:=-1;
  4360. for i:=length(objrealname^) downto 1 do
  4361. if objrealname^[i]='$' then
  4362. begin
  4363. crcidx:=i;
  4364. break;
  4365. end;
  4366. if crcidx<0 then
  4367. internalerror(2014121201);
  4368. basename:=copy(objrealname^,1,crcidx-1);
  4369. split_generic_name(basename,nongeneric,paramcount);
  4370. rttistring:=rttistring+nongeneric+'<';
  4371. remlength:=255-length(rttistring)-1;
  4372. if remlength<4 then
  4373. rttistring:=rttistring+'>'
  4374. else
  4375. rttistring:=rttistring+generate_full_paramname(remlength)+'>';
  4376. end
  4377. else
  4378. if is_generic then
  4379. begin
  4380. rttistring:=OwnerHierarchyName;
  4381. split_generic_name(objrealname^,nongeneric,paramcount);
  4382. rttistring:=rttistring+nongeneric+'<';
  4383. { we don't want any ',' if there is only one parameter }
  4384. for i:=0 to paramcount-0 do
  4385. rttistring:=rttistring+',';
  4386. rttistring:=rttistring+'>';
  4387. end
  4388. else
  4389. rttistring:=OwnerHierarchyName+objrealname^;
  4390. end;
  4391. result:=rttistring;
  4392. end;
  4393. function tabstractrecorddef.search_enumerator_get: tprocdef;
  4394. var
  4395. sym : tsym;
  4396. i : integer;
  4397. pd : tprocdef;
  4398. hashedid : THashedIDString;
  4399. begin
  4400. result:=nil;
  4401. hashedid.id:='GETENUMERATOR';
  4402. sym:=tsym(symtable.FindWithHash(hashedid));
  4403. if assigned(sym) and (sym.typ=procsym) then
  4404. begin
  4405. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  4406. begin
  4407. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4408. if (pd.proctypeoption = potype_function) and
  4409. (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) and
  4410. (pd.visibility >= vis_public) then
  4411. begin
  4412. result:=pd;
  4413. exit;
  4414. end;
  4415. end;
  4416. end;
  4417. end;
  4418. function tabstractrecorddef.search_enumerator_move: tprocdef;
  4419. var
  4420. sym : tsym;
  4421. i : integer;
  4422. pd : tprocdef;
  4423. hashedid : THashedIDString;
  4424. begin
  4425. result:=nil;
  4426. // first search for po_enumerator_movenext method modifier
  4427. // then search for public function MoveNext: Boolean
  4428. for i:=0 to symtable.SymList.Count-1 do
  4429. begin
  4430. sym:=TSym(symtable.SymList[i]);
  4431. if (sym.typ=procsym) then
  4432. begin
  4433. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  4434. if assigned(pd) then
  4435. begin
  4436. result:=pd;
  4437. exit;
  4438. end;
  4439. end;
  4440. end;
  4441. hashedid.id:='MOVENEXT';
  4442. sym:=tsym(symtable.FindWithHash(hashedid));
  4443. if assigned(sym) and (sym.typ=procsym) then
  4444. begin
  4445. for i:=0 to Tprocsym(sym).ProcdefList.Count-1 do
  4446. begin
  4447. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  4448. if (pd.proctypeoption = potype_function) and
  4449. is_boolean(pd.returndef) and
  4450. (pd.minparacount = 0) and
  4451. (pd.visibility >= vis_public) then
  4452. begin
  4453. result:=pd;
  4454. exit;
  4455. end;
  4456. end;
  4457. end;
  4458. end;
  4459. function tabstractrecorddef.search_enumerator_current: tsym;
  4460. var
  4461. sym: tsym;
  4462. i: integer;
  4463. hashedid : THashedIDString;
  4464. begin
  4465. result:=nil;
  4466. // first search for ppo_enumerator_current property modifier
  4467. // then search for public property Current
  4468. for i:=0 to symtable.SymList.Count-1 do
  4469. begin
  4470. sym:=TSym(symtable.SymList[i]);
  4471. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  4472. begin
  4473. result:=sym;
  4474. exit;
  4475. end;
  4476. end;
  4477. hashedid.id:='CURRENT';
  4478. sym:=tsym(symtable.FindWithHash(hashedid));
  4479. if assigned(sym) and (sym.typ=propertysym) and
  4480. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  4481. begin
  4482. result:=sym;
  4483. exit;
  4484. end;
  4485. end;
  4486. function tabstractrecorddef.jvm_full_typename(with_package_name: boolean): string;
  4487. var
  4488. st: tsymtable;
  4489. enclosingdef: tdef;
  4490. begin
  4491. if typ=objectdef then
  4492. result:=tobjectdef(self).objextname^
  4493. else if assigned(typesym) then
  4494. result:=typesym.realname
  4495. { have to generate anonymous nested type in current unit/class/record }
  4496. else
  4497. internalerror(2011032601);
  4498. { in case of specializations, add some extras to prevent name conflicts
  4499. with nested classes }
  4500. if df_specialization in defoptions then
  4501. result:='$'+result+'$specialization$';
  4502. st:=owner;
  4503. while assigned(st) and
  4504. (st.symtabletype in [objectsymtable,recordsymtable,localsymtable]) do
  4505. begin
  4506. { nested classes are named as "OuterClass$InnerClass" }
  4507. enclosingdef:=tdef(st.defowner);
  4508. if enclosingdef.typ=procdef then
  4509. result:=result+tprocdef(enclosingdef).procsym.realname+'$$'+tostr(tprocdef(enclosingdef).procsym.symid)+'$'
  4510. else if enclosingdef.typ=objectdef then
  4511. result:=tobjectdef(enclosingdef).objextname^+'$'+result
  4512. else if assigned(enclosingdef.typesym) then
  4513. result:=enclosingdef.typesym.realname+'$'+result
  4514. else
  4515. internalerror(2011060305);
  4516. st:=enclosingdef.owner;
  4517. end;
  4518. if with_package_name and
  4519. assigned(import_lib) then
  4520. result:=import_lib^+'/'+result;
  4521. end;
  4522. function tabstractrecorddef.contains_float_field: boolean;
  4523. var
  4524. i : longint;
  4525. begin
  4526. result:=true;
  4527. for i:=0 to symtable.symlist.count-1 do
  4528. begin
  4529. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4530. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4531. continue;
  4532. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4533. begin
  4534. if tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).is_fpuregable then
  4535. exit;
  4536. { search recursively }
  4537. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4538. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_float_field) then
  4539. exit;
  4540. end;
  4541. end;
  4542. result:=false;
  4543. end;
  4544. function tabstractrecorddef.contains_cross_aword_field: boolean;
  4545. var
  4546. i : longint;
  4547. foffset, fsize: aword;
  4548. begin
  4549. result:=true;
  4550. for i:=0 to symtable.symlist.count-1 do
  4551. begin
  4552. if (tsym(symtable.symlist[i]).typ<>fieldvarsym) or
  4553. (sp_static in tsym(symtable.symlist[i]).symoptions) then
  4554. continue;
  4555. if assigned(tfieldvarsym(symtable.symlist[i]).vardef) then
  4556. begin
  4557. if is_packed then
  4558. begin
  4559. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset;
  4560. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.packedbitsize;
  4561. end
  4562. else
  4563. begin
  4564. foffset:=tfieldvarsym(symtable.symlist[i]).fieldoffset*8;
  4565. fsize:=tfieldvarsym(symtable.symlist[i]).vardef.size*8;
  4566. end;
  4567. if (fsize>0) and ((foffset div (sizeof(aword)*8)) <> ((foffset+fsize-1) div (sizeof(aword)*8))) then
  4568. exit;
  4569. { search recursively }
  4570. if (tstoreddef(tfieldvarsym(symtable.symlist[i]).vardef).typ=recorddef) and
  4571. (tabstractrecorddef(tfieldvarsym(symtable.symlist[i]).vardef).contains_cross_aword_field) then
  4572. exit;
  4573. end;
  4574. end;
  4575. result:=false;
  4576. end;
  4577. procedure tabstractrecorddef.apply_rtti_directive(dir: trtti_directive);
  4578. begin
  4579. { records don't support the inherit clause but shouldn't
  4580. give an error either if used (for Delphi compatibility),
  4581. so we silently enforce the clause as explicit. }
  4582. rtti.clause:=rtc_explicit;
  4583. rtti.options:=dir.options;
  4584. end;
  4585. function tabstractrecorddef.is_visible_for_rtti(option: trtti_option; vis: tvisibility): boolean;
  4586. begin
  4587. case vis of
  4588. vis_private,
  4589. vis_strictprivate: result:=rv_private in rtti.options[option];
  4590. vis_protected,
  4591. vis_strictprotected: result:=rv_protected in rtti.options[option];
  4592. vis_public: result:=rv_public in rtti.options[option];
  4593. vis_published: result:=rv_published in rtti.options[option];
  4594. otherwise
  4595. result:=false;
  4596. end;
  4597. end;
  4598. function tabstractrecorddef.rtti_visibilities_for_option(option: trtti_option): tvisibilities;
  4599. begin
  4600. result:=[];
  4601. if rv_private in rtti.options[option] then
  4602. begin
  4603. include(result,vis_private);
  4604. include(result,vis_strictprivate);
  4605. end;
  4606. if rv_protected in rtti.options[option] then
  4607. begin
  4608. include(result,vis_protected);
  4609. include(result,vis_strictprotected);
  4610. end;
  4611. if rv_public in rtti.options[option] then
  4612. include(result,vis_public);
  4613. if rv_published in rtti.options[option] then
  4614. include(result,vis_published);
  4615. end;
  4616. function tabstractrecorddef.has_extended_rtti: boolean;
  4617. begin
  4618. result := (rtti.options[ro_fields]<>[]) or
  4619. (rtti.options[ro_methods]<>[]) or
  4620. (rtti.options[ro_properties]<>[]);
  4621. end;
  4622. {$ifdef DEBUG_NODE_XML}
  4623. procedure tabstractrecorddef.XMLPrintDefData(var T: Text; Sym: TSym);
  4624. procedure WriteSymOptions(SourceSym: TSym);
  4625. var
  4626. i: TSymOption;
  4627. first: Boolean;
  4628. begin
  4629. First := True;
  4630. for i := Low(TSymOption) to High(TSymOption) do
  4631. if i in SourceSym.symoptions then
  4632. begin
  4633. if First then
  4634. begin
  4635. Write(T, '" symoptions="', i);
  4636. First := False;
  4637. end
  4638. else
  4639. Write(T, ',', i)
  4640. end;
  4641. end;
  4642. var
  4643. List: TFPHashObjectList;
  4644. i: Integer;
  4645. begin
  4646. WriteLn(T, PrintNodeIndention, '<size>', size, '</size>');
  4647. if (alignment = structalignment) and (alignment = aggregatealignment) then
  4648. begin
  4649. { Straightforward and simple }
  4650. WriteLn(T, PrintNodeIndention, '<alignment>', alignment, '</alignment>');
  4651. end
  4652. else
  4653. begin
  4654. WriteLn(T, PrintNodeIndention, '<alignment>');
  4655. printnodeindent;
  4656. WriteLn(T, PrintNodeIndention, '<basic>', alignment, '</basic>');
  4657. if (structalignment <> alignment) then
  4658. WriteLn(T, PrintNodeIndention, '<struct>', structalignment, '</struct>');
  4659. if (aggregatealignment <> alignment) and (aggregatealignment <> structalignment) then
  4660. WriteLn(T, PrintNodeIndention, '<aggregate>', aggregatealignment, '</aggregate>');
  4661. printnodeunindent;
  4662. WriteLn(T, PrintNodeIndention, '</alignment>');
  4663. end;
  4664. { List the fields }
  4665. List := Symtable.SymList;
  4666. for i := 0 to List.Count - 1 do
  4667. case TSym(List[i]).typ of
  4668. { staticvarsym,localvarsym,paravarsym,fieldvarsym,
  4669. typesym,procsym,unitsym,}
  4670. constsym:
  4671. with TConstSym(List[i]) do
  4672. begin
  4673. Write(T, PrintNodeIndention, '<const name="', RealName, '" pos="', fileinfo.line, ',', fileinfo.column);
  4674. WriteSymOptions(TSym(List[i]));
  4675. WriteLn(T, '">');
  4676. PrintNodeIndent;
  4677. XMLPrintConstData(T);
  4678. PrintNodeUnindent;
  4679. WriteLn(T, PrintNodeIndention, '</const>');
  4680. end;
  4681. {
  4682. errorsym,syssym,labelsym,absolutevarsym,propertysym,
  4683. macrosym,namespacesym,undefinedsym,programparasym
  4684. }
  4685. fieldvarsym:
  4686. with TFieldVarSym(List[i]) do
  4687. begin
  4688. Write(T, PrintNodeIndention, '<field name="', RealName, '" pos="', fileinfo.line, ',', fileinfo.column);
  4689. WriteSymOptions(TSym(List[i]));
  4690. WriteLn(T, '">');
  4691. PrintNodeIndent;
  4692. XMLPrintFieldData(T);
  4693. PrintNodeUnindent;
  4694. WriteLn(T, PrintNodeIndention, '</field>');
  4695. end;
  4696. else
  4697. ;
  4698. end;
  4699. end;
  4700. {$endif DEBUG_NODE_XML}
  4701. {***************************************************************************
  4702. trecorddef
  4703. ***************************************************************************}
  4704. constructor trecorddef.create(const n:string; p:TSymtable);
  4705. begin
  4706. inherited create(n,recorddef,true);
  4707. symtable:=p;
  4708. { we can own the symtable only if nobody else owns a copy so far }
  4709. if symtable.refcount=1 then
  4710. symtable.defowner:=self;
  4711. isunion:=false;
  4712. cloneddefderef.reset;
  4713. end;
  4714. constructor trecorddef.create_internal(const n: string; packrecords, recordalignmin: shortint; where: tsymtable);
  4715. var
  4716. name : string;
  4717. pname : pshortstring;
  4718. oldsymtablestack: tsymtablestack;
  4719. ts: ttypesym;
  4720. begin
  4721. { construct name }
  4722. if n<>'' then
  4723. pname:=@n
  4724. else
  4725. begin
  4726. init_defid;
  4727. name:='$InternalRec'+unique_id_str;
  4728. pname:=@name;
  4729. end;
  4730. oldsymtablestack:=symtablestack;
  4731. { do not simply push/pop current_module.localsymtable, because
  4732. that can have side-effects (e.g., it removes helpers) }
  4733. symtablestack:=nil;
  4734. symtable:=trecordsymtable.create(pname^,packrecords,recordalignmin);
  4735. symtable.defowner:=self;
  4736. isunion:=false;
  4737. inherited create(pname^,recorddef,true);
  4738. where.insertdef(self);
  4739. { if we specified a name, then we'll probably want to look up the
  4740. type again by name too -> create typesym }
  4741. if n<>'' then
  4742. begin
  4743. ts:=ctypesym.create(n,self);
  4744. include(ts.symoptions,sp_internal);
  4745. { avoid hints about unused types (these may only be used for
  4746. typed constant data) }
  4747. ts.increfcount;
  4748. where.insertsym(ts);
  4749. end;
  4750. symtablestack:=oldsymtablestack;
  4751. { don't create RTTI for internal types, these are not exported }
  4752. defstates:=defstates+[ds_rtti_table_written,ds_init_table_written];
  4753. include(defoptions,df_internal);
  4754. end;
  4755. constructor trecorddef.create_global_internal(const n: string; packrecords, recordalignmin: shortint);
  4756. var
  4757. where : tsymtable;
  4758. begin
  4759. where:=current_module.localsymtable;
  4760. if not assigned(where) then
  4761. where:=current_module.globalsymtable;
  4762. create_internal(n,packrecords,recordalignmin,where);
  4763. end;
  4764. function trecorddef.add_field_by_def(const optionalname: TIDString; def: tdef): tsym;
  4765. var
  4766. sym: tfieldvarsym;
  4767. name: TIDString;
  4768. pname: ^TIDString;
  4769. begin
  4770. if optionalname='' then
  4771. begin
  4772. name:='$f'+tostr(trecordsymtable(symtable).symlist.count);
  4773. pname:=@name
  4774. end
  4775. else
  4776. pname:=@optionalname;
  4777. sym:=cfieldvarsym.create(pname^,vs_value,def,[]);
  4778. symtable.insertsym(sym);
  4779. trecordsymtable(symtable).addfield(sym,vis_hidden);
  4780. result:=sym;
  4781. end;
  4782. procedure trecorddef.add_fields_from_deflist(fieldtypes: tfplist);
  4783. var
  4784. i: longint;
  4785. begin
  4786. for i:=0 to fieldtypes.count-1 do
  4787. add_field_by_def('',tdef(fieldtypes[i]));
  4788. end;
  4789. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  4790. procedure readvariantrecdesc(var variantrecdesc : pvariantrecdesc);
  4791. var
  4792. i,j : longint;
  4793. begin
  4794. if ppufile.getbyte=1 then
  4795. begin
  4796. new(variantrecdesc);
  4797. ppufile.getderef(variantrecdesc^.variantselectorderef);
  4798. SetLength(variantrecdesc^.branches,ppufile.getasizeint);
  4799. for i:=0 to high(variantrecdesc^.branches) do
  4800. begin
  4801. SetLength(variantrecdesc^.branches[i].values,ppufile.getasizeint);
  4802. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4803. variantrecdesc^.branches[i].values[j]:=ppufile.getexprint;
  4804. readvariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4805. end;
  4806. end
  4807. else
  4808. variantrecdesc:=nil;
  4809. end;
  4810. begin
  4811. inherited ppuload(recorddef,ppufile);
  4812. if df_copied_def in defoptions then
  4813. begin
  4814. ppufile.getderef(cloneddefderef);
  4815. ppuload_platform(ppufile);
  4816. end
  4817. else
  4818. begin
  4819. symtable:=trecordsymtable.create(objrealname^,0,0);
  4820. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  4821. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  4822. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  4823. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  4824. trecordsymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  4825. trecordsymtable(symtable).datasize:=ppufile.getasizeint;
  4826. trecordsymtable(symtable).paddingsize:=ppufile.getword;
  4827. ppufile.getset(tppuset1(trecordsymtable(symtable).managementoperators));
  4828. { position of ppuload_platform call must correspond
  4829. to position of writeentry in ppuwrite method }
  4830. ppuload_platform(ppufile);
  4831. trecordsymtable(symtable).ppuload(ppufile);
  4832. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4833. but because iso mode supports no units, there is no need to store the variantrecdesc
  4834. in the ppu
  4835. }
  4836. // readvariantrecdesc(variantrecdesc);
  4837. { requires usefieldalignment to be set }
  4838. symtable.defowner:=self;
  4839. end;
  4840. isunion:=false;
  4841. end;
  4842. destructor trecorddef.destroy;
  4843. procedure free_variantrecdesc(var variantrecdesc : pvariantrecdesc);
  4844. var
  4845. i : longint;
  4846. begin
  4847. while assigned(variantrecdesc) do
  4848. begin
  4849. for i:=0 to high(variantrecdesc^.branches) do
  4850. begin
  4851. free_variantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4852. SetLength(variantrecdesc^.branches[i].values,0);
  4853. end;
  4854. SetLength(variantrecdesc^.branches,0);
  4855. dispose(variantrecdesc);
  4856. variantrecdesc:=nil;
  4857. end;
  4858. end;
  4859. begin
  4860. if assigned(variantrecdesc) then
  4861. free_variantrecdesc(variantrecdesc);
  4862. if assigned(symtable) then
  4863. begin
  4864. symtable.free;
  4865. symtable:=nil;
  4866. end;
  4867. inherited destroy;
  4868. end;
  4869. function trecorddef.getcopy : tstoreddef;
  4870. begin
  4871. result:=crecorddef.create(objrealname^,symtable.getcopy);
  4872. trecorddef(result).isunion:=isunion;
  4873. include(trecorddef(result).defoptions,df_copied_def);
  4874. if assigned(tcinitcode) then
  4875. trecorddef(result).tcinitcode:=tcinitcode.getcopy;
  4876. if assigned(import_lib) then
  4877. trecorddef(result).import_lib:=stringdup(import_lib^);
  4878. end;
  4879. function trecorddef.needs_inittable : boolean;
  4880. begin
  4881. { each record with managed field or with any management operator needs
  4882. init table }
  4883. needs_inittable:=(trecordsymtable(symtable).managementoperators<>[]) or
  4884. trecordsymtable(symtable).needs_init_final
  4885. end;
  4886. function trecorddef.needs_separate_initrtti : boolean;
  4887. begin
  4888. result:=true;
  4889. end;
  4890. function trecorddef.has_non_trivial_init_child(check_parent:boolean):boolean;
  4891. begin
  4892. result:=trecordsymtable(symtable).has_non_trivial_init;
  4893. end;
  4894. procedure trecorddef.buildderef;
  4895. begin
  4896. inherited buildderef;
  4897. if df_copied_def in defoptions then
  4898. cloneddefderef.build(symtable.defowner)
  4899. else
  4900. tstoredsymtable(symtable).buildderef;
  4901. end;
  4902. procedure trecorddef.deref;
  4903. begin
  4904. inherited deref;
  4905. { now dereference the definitions }
  4906. if df_copied_def in defoptions then
  4907. begin
  4908. cloneddef:=trecorddef(cloneddefderef.resolve);
  4909. symtable:=cloneddef.symtable.getcopy;
  4910. end
  4911. else
  4912. tstoredsymtable(symtable).deref(false);
  4913. { internal types, only load from the system unit }
  4914. if assigned(owner) and
  4915. assigned(owner.name) and
  4916. (owner.name^='SYSTEM') then
  4917. begin
  4918. { TGUID }
  4919. if not assigned(rec_tguid) and
  4920. (upper(typename)='TGUID') then
  4921. rec_tguid:=self
  4922. { JMP_BUF }
  4923. else if not assigned(rec_jmp_buf) and
  4924. (upper(typename)='JMP_BUF') then
  4925. rec_jmp_buf:=self
  4926. else if not assigned(rec_exceptaddr) and
  4927. (upper(typename)='TEXCEPTADDR') then
  4928. rec_exceptaddr:=self;
  4929. end;
  4930. end;
  4931. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  4932. procedure writevariantrecdesc(variantrecdesc : pvariantrecdesc);
  4933. var
  4934. i,j : longint;
  4935. begin
  4936. if assigned(variantrecdesc) then
  4937. begin
  4938. ppufile.putbyte(1);
  4939. ppufile.putderef(variantrecdesc^.variantselectorderef);
  4940. ppufile.putasizeint(length(variantrecdesc^.branches));
  4941. for i:=0 to high(variantrecdesc^.branches) do
  4942. begin
  4943. ppufile.putasizeint(length(variantrecdesc^.branches[i].values));
  4944. for j:=0 to high(variantrecdesc^.branches[i].values) do
  4945. ppufile.putexprint(variantrecdesc^.branches[i].values[j]);
  4946. writevariantrecdesc(variantrecdesc^.branches[i].nestedvariant);
  4947. end;
  4948. end
  4949. else
  4950. ppufile.putbyte(0);
  4951. end;
  4952. begin
  4953. inherited ppuwrite(ppufile);
  4954. if df_copied_def in defoptions then
  4955. ppufile.putderef(cloneddefderef)
  4956. else
  4957. begin
  4958. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  4959. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  4960. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  4961. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  4962. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignmin));
  4963. ppufile.putasizeint(trecordsymtable(symtable).datasize);
  4964. ppufile.putword(trecordsymtable(symtable).paddingsize);
  4965. ppufile.putset(tppuset1(trecordsymtable(symtable).managementoperators));
  4966. { the variantrecdesc is needed only for iso-like new statements new(prec,1,2,3 ...);
  4967. but because iso mode supports no units, there is no need to store the variantrecdesc
  4968. in the ppu
  4969. }
  4970. // writevariantrecdesc(variantrecdesc);
  4971. end;
  4972. writeentry(ppufile,ibrecorddef);
  4973. if not(df_copied_def in defoptions) then
  4974. trecordsymtable(symtable).ppuwrite(ppufile);
  4975. end;
  4976. function trecorddef.size:asizeint;
  4977. begin
  4978. result:=trecordsymtable(symtable).datasize;
  4979. end;
  4980. function trecorddef.alignment:shortint;
  4981. begin
  4982. alignment:=trecordsymtable(symtable).recordalignment;
  4983. end;
  4984. function trecorddef.padalignment:shortint;
  4985. begin
  4986. padalignment := trecordsymtable(symtable).padalignment;
  4987. end;
  4988. function trecorddef.GetTypeName : string;
  4989. begin
  4990. GetTypeName:='<record type>'
  4991. end;
  4992. {$ifdef DEBUG_NODE_XML}
  4993. function TRecordDef.XMLPrintType: ansistring;
  4994. begin
  4995. Result := '&lt;record&gt;';
  4996. end;
  4997. {$endif DEBUG_NODE_XML}
  4998. {***************************************************************************
  4999. TABSTRACTPROCDEF
  5000. ***************************************************************************}
  5001. constructor tabstractprocdef.create(dt:tdeftyp;level:byte;doregister:boolean);
  5002. begin
  5003. inherited create(dt,doregister);
  5004. parast:=tparasymtable.create(self,level);
  5005. paras:=nil;
  5006. minparacount:=0;
  5007. maxparacount:=0;
  5008. proctypeoption:=potype_none;
  5009. proccalloption:=pocall_none;
  5010. procoptions:=[];
  5011. returndef:=voidtype;
  5012. returndefderef.reset;
  5013. savesize:=sizeof(pint);
  5014. callerargareasize:=0;
  5015. calleeargareasize:=0;
  5016. has_paraloc_info:=callnoside;
  5017. funcretloc[callerside].init;
  5018. funcretloc[calleeside].init;
  5019. check_mark_as_nested;
  5020. end;
  5021. destructor tabstractprocdef.destroy;
  5022. begin
  5023. if assigned(paras) then
  5024. begin
  5025. {$ifdef MEMDEBUG}
  5026. memprocpara.start;
  5027. {$endif MEMDEBUG}
  5028. paras.free;
  5029. paras:=nil;
  5030. {$ifdef MEMDEBUG}
  5031. memprocpara.stop;
  5032. {$endif MEMDEBUG}
  5033. end;
  5034. if assigned(parast) then
  5035. begin
  5036. {$ifdef MEMDEBUG}
  5037. memprocparast.start;
  5038. {$endif MEMDEBUG}
  5039. parast.free;
  5040. parast:=nil;
  5041. {$ifdef MEMDEBUG}
  5042. memprocparast.stop;
  5043. {$endif MEMDEBUG}
  5044. end;
  5045. funcretloc[callerside].done;
  5046. funcretloc[calleeside].done;
  5047. inherited destroy;
  5048. end;
  5049. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  5050. begin
  5051. if (tsym(p).typ<>paravarsym) then
  5052. exit;
  5053. inc(plongint(arg)^);
  5054. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  5055. begin
  5056. if not assigned(tparavarsym(p).defaultconstsym) then
  5057. inc(minparacount);
  5058. inc(maxparacount);
  5059. end;
  5060. end;
  5061. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  5062. begin
  5063. if (tsym(p).typ<>paravarsym) then
  5064. exit;
  5065. paras.add(p);
  5066. end;
  5067. procedure tabstractprocdef.calcparas;
  5068. var
  5069. paracount : longint;
  5070. begin
  5071. { This can already be assigned when
  5072. we need to reresolve this unit (PFV) }
  5073. if assigned(paras) then
  5074. paras.free;
  5075. paras:=tparalist.create(false);
  5076. paracount:=0;
  5077. minparacount:=0;
  5078. maxparacount:=0;
  5079. parast.SymList.ForEachCall(@count_para,@paracount);
  5080. paras.capacity:=paracount;
  5081. { Insert parameters in table }
  5082. parast.SymList.ForEachCall(@insert_para,nil);
  5083. { Order parameters }
  5084. paras.sortparas;
  5085. end;
  5086. function tabstractprocdef.mangledprocparanames(oldlen : longint) : string;
  5087. var
  5088. hash : qword;
  5089. hp : TParavarsym;
  5090. hs : TSymStr;
  5091. newlen,
  5092. i : integer;
  5093. begin
  5094. result:='';
  5095. hp:=nil;
  5096. { add parameter types }
  5097. for i:=0 to paras.count-1 do
  5098. begin
  5099. hp:=tparavarsym(paras[i]);
  5100. if not(vo_is_hidden_para in hp.varoptions) then
  5101. result:=result+'$'+hp.vardef.mangledparaname;
  5102. end;
  5103. { add resultdef, add $$ as separator to make it unique from a
  5104. parameter separator }
  5105. if not is_void(returndef) then
  5106. result:=result+'$$'+returndef.mangledparaname;
  5107. newlen:=length(result)+oldlen;
  5108. { Replace with hash if the parameter line is very long }
  5109. if (newlen-oldlen>12) and
  5110. ((newlen>100) or (newlen-oldlen>64)) then
  5111. begin
  5112. hash:=InitFnv64;
  5113. for i:=0 to paras.count-1 do
  5114. begin
  5115. hp:=tparavarsym(paras[i]);
  5116. if not(vo_is_hidden_para in hp.varoptions) then
  5117. begin
  5118. hs:=hp.vardef.mangledparaname;
  5119. hash:=UpdateFnv64(hash,hs[1],length(hs));
  5120. end;
  5121. end;
  5122. if not is_void(returndef) then
  5123. begin
  5124. { add a little prefix so that x(integer; integer) is different from x(integer):integer }
  5125. hs:='$$'+returndef.mangledparaname;
  5126. hash:=UpdateFnv64(hash,hs[1],length(hs));
  5127. end;
  5128. result:='$h'+Base64Mangle(hash);
  5129. end;
  5130. end;
  5131. procedure tabstractprocdef.buildderef;
  5132. begin
  5133. { released procdef? }
  5134. if not assigned(parast) then
  5135. exit;
  5136. inherited buildderef;
  5137. returndefderef.build(returndef);
  5138. if po_explicitparaloc in procoptions then
  5139. funcretloc[callerside].buildderef;
  5140. { parast }
  5141. tparasymtable(parast).buildderef;
  5142. end;
  5143. procedure tabstractprocdef.deref;
  5144. begin
  5145. inherited deref;
  5146. returndef:=tdef(returndefderef.resolve);
  5147. if po_explicitparaloc in procoptions then
  5148. begin
  5149. funcretloc[callerside].deref;
  5150. has_paraloc_info:=callerside;
  5151. end
  5152. else
  5153. begin
  5154. { deref is called after loading from a ppu, but also after another
  5155. unit has been reloaded/recompiled and all references must be
  5156. re-resolved. Since the funcretloc contains a reference to a tdef,
  5157. reset it so that we won't try to access the stale def }
  5158. funcretloc[callerside].init;
  5159. has_paraloc_info:=callnoside;
  5160. end;
  5161. { parast }
  5162. tparasymtable(parast).deref(false);
  5163. { recalculated parameters }
  5164. calcparas;
  5165. end;
  5166. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  5167. begin
  5168. inherited ppuload(dt,ppufile);
  5169. parast:=nil;
  5170. Paras:=nil;
  5171. minparacount:=0;
  5172. maxparacount:=0;
  5173. ppufile.getderef(returndefderef);
  5174. proctypeoption:=tproctypeoption(ppufile.getbyte);
  5175. proccalloption:=tproccalloption(ppufile.getbyte);
  5176. ppufile.getset(tppuset8(procoptions));
  5177. funcretloc[callerside].init;
  5178. if po_explicitparaloc in procoptions then
  5179. funcretloc[callerside].ppuload(ppufile);
  5180. savesize:=sizeof(pint);
  5181. if (po_explicitparaloc in procoptions) then
  5182. has_paraloc_info:=callerside;
  5183. end;
  5184. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  5185. var
  5186. oldintfcrc : boolean;
  5187. begin
  5188. { released procdef? }
  5189. if not assigned(parast) then
  5190. exit;
  5191. inherited ppuwrite(ppufile);
  5192. ppufile.putderef(returndefderef);
  5193. oldintfcrc:=ppufile.do_interface_crc;
  5194. ppufile.do_interface_crc:=false;
  5195. ppufile.putbyte(ord(proctypeoption));
  5196. ppufile.putbyte(ord(proccalloption));
  5197. ppufile.putset(tppuset8(procoptions));
  5198. ppufile.do_interface_crc:=oldintfcrc;
  5199. if (po_explicitparaloc in procoptions) then
  5200. funcretloc[callerside].ppuwrite(ppufile);
  5201. end;
  5202. function tabstractprocdef.typename_paras(pno: tprocnameoptions) : ansistring;
  5203. var
  5204. hs,s : ansistring;
  5205. hp : TParavarsym;
  5206. hpc : tconstsym;
  5207. first : boolean;
  5208. i,j : integer;
  5209. begin
  5210. s:='';
  5211. first:=true;
  5212. for i:=0 to paras.count-1 do
  5213. begin
  5214. hp:=tparavarsym(paras[i]);
  5215. if not(vo_is_hidden_para in hp.varoptions) or
  5216. (pno_showhidden in pno) then
  5217. begin
  5218. if first then
  5219. begin
  5220. s:=s+'(';
  5221. first:=false;
  5222. end
  5223. else
  5224. s:=s+';';
  5225. if vo_is_hidden_para in hp.varoptions then
  5226. s:=s+'<';
  5227. case hp.varspez of
  5228. vs_var :
  5229. s:=s+'var ';
  5230. vs_const :
  5231. s:=s+'const ';
  5232. vs_out :
  5233. s:=s+'out ';
  5234. vs_constref :
  5235. s:=s+'constref ';
  5236. else
  5237. ;
  5238. end;
  5239. if (pno_paranames in pno) then
  5240. s:=s+hp.realname+':';
  5241. if hp.univpara then
  5242. s:=s+'univ ';
  5243. if assigned(hp.vardef.typesym) then
  5244. begin
  5245. hs:=hp.vardef.typesym.realname;
  5246. if hs[1]<>'$' then
  5247. s:=s+hp.vardef.OwnerHierarchyName+hs
  5248. else
  5249. s:=s+hp.vardef.GetTypeName;
  5250. end
  5251. else
  5252. s:=s+hp.vardef.GetTypeName;
  5253. { default value }
  5254. if assigned(hp.defaultconstsym) then
  5255. begin
  5256. hpc:=tconstsym(hp.defaultconstsym);
  5257. hs:='';
  5258. case hpc.consttyp of
  5259. constwresourcestring,
  5260. constwstring:
  5261. begin
  5262. if pcompilerwidestring(hpc.value.valueptr)^.len>0 then
  5263. begin
  5264. setlength(hs,pcompilerwidestring(hpc.value.valueptr)^.len);
  5265. for j:=0 to pcompilerwidestring(hpc.value.valueptr)^.len-1 do
  5266. begin
  5267. if (ord(pcompilerwidestring(hpc.value.valueptr)^.data[j])<127) and
  5268. not(byte(pcompilerwidestring(hpc.value.valueptr)^.data[j]) in [0,10,13]) then
  5269. hs[j+1]:=char(pcompilerwidestring(hpc.value.valueptr)^.data[j])
  5270. else
  5271. hs[j+1]:='.';
  5272. end;
  5273. end;
  5274. end;
  5275. conststring,
  5276. constresourcestring :
  5277. begin
  5278. if hpc.value.len>0 then
  5279. begin
  5280. setLength(hs,hpc.value.len);
  5281. { don't write past the end of hs if the constant
  5282. is > 255 chars }
  5283. move(hpc.value.valueptr^,hs[1],length(hs));
  5284. { make sure that constant strings with newline chars
  5285. don't create a linebreak in the assembler code,
  5286. since comments are line-based. Also remove nulls
  5287. because the comments are written as a pchar. }
  5288. ReplaceCase(hs,#0,'.');
  5289. ReplaceCase(hs,#10,'.');
  5290. ReplaceCase(hs,#13,'.');
  5291. end;
  5292. end;
  5293. constreal :
  5294. str(pbestreal(hpc.value.valueptr)^,hs);
  5295. constpointer :
  5296. hs:=tostr(hpc.value.valueordptr);
  5297. constord :
  5298. begin
  5299. if is_boolean(hpc.constdef) then
  5300. begin
  5301. if hpc.value.valueord<>0 then
  5302. hs:='TRUE'
  5303. else
  5304. hs:='FALSE';
  5305. end
  5306. else
  5307. hs:=tostr(hpc.value.valueord);
  5308. end;
  5309. constnil :
  5310. hs:='nil';
  5311. constset :
  5312. hs:='<set>';
  5313. constguid:
  5314. hs:=guid2string(pguid(hpc.value.valueptr)^);
  5315. constnone:
  5316. internalerror(2019050704);
  5317. end;
  5318. if hs<>'' then
  5319. s:=s+'=`'+hs+'`';
  5320. end;
  5321. if vo_is_hidden_para in hp.varoptions then
  5322. s:=s+'>';
  5323. end;
  5324. end;
  5325. if not first then
  5326. s:=s+')';
  5327. if (po_varargs in procoptions) then
  5328. s:=s+';VarArgs';
  5329. typename_paras:=s;
  5330. end;
  5331. function tabstractprocdef.is_methodpointer:boolean;
  5332. begin
  5333. result:=false;
  5334. end;
  5335. function tabstractprocdef.is_addressonly:boolean;
  5336. begin
  5337. result:=true;
  5338. end;
  5339. function tabstractprocdef.no_self_node: boolean;
  5340. begin
  5341. Result:=([po_staticmethod,po_classmethod]<=procoptions)or
  5342. (proctypeoption in [potype_class_constructor,potype_class_destructor,potype_operator]);
  5343. end;
  5344. function tabstractprocdef.getcopyas(newtyp:tdeftyp;copytyp:tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef;
  5345. var
  5346. j, nestinglevel: longint;
  5347. pvs, npvs: tparavarsym;
  5348. begin
  5349. nestinglevel:=parast.symtablelevel;
  5350. if newtyp=procdef then
  5351. begin
  5352. if (copytyp<>pc_bareproc) then
  5353. result:=cprocdef.create(nestinglevel,doregister)
  5354. else
  5355. result:=cprocdef.create(normal_function_level,doregister);
  5356. tprocdef(result).visibility:=vis_public;
  5357. end
  5358. else
  5359. begin
  5360. result:=cprocvardef.create(nestinglevel,true);
  5361. end;
  5362. tabstractprocdef(result).returndef:=returndef;
  5363. tabstractprocdef(result).returndefderef:=returndefderef;
  5364. pvs:=nil;
  5365. npvs:=nil;
  5366. if copytyp<>pc_normal_no_paras then
  5367. begin
  5368. for j:=0 to parast.symlist.count-1 do
  5369. begin
  5370. case tsym(parast.symlist[j]).typ of
  5371. paravarsym:
  5372. begin
  5373. pvs:=tparavarsym(parast.symlist[j]);
  5374. { in case of bare proc, don't copy self, vmt or framepointer
  5375. parameters }
  5376. if (copytyp in [pc_bareproc,pc_normal_no_hidden]) and
  5377. (([vo_is_self,vo_is_vmt,vo_is_parentfp,vo_is_result,vo_is_funcret,vo_is_high_para]*pvs.varoptions)<>[]) then
  5378. continue;
  5379. if paraprefix='' then
  5380. npvs:=cparavarsym.create(pvs.realname,pvs.paranr,pvs.varspez,
  5381. pvs.vardef,pvs.varoptions)
  5382. else if not(vo_is_high_para in pvs.varoptions) then
  5383. npvs:=cparavarsym.create(paraprefix+pvs.realname,pvs.paranr,pvs.varspez,
  5384. pvs.vardef,pvs.varoptions)
  5385. else
  5386. npvs:=cparavarsym.create('$high'+paraprefix+copy(pvs.name,5,length(pvs.name)),pvs.paranr,pvs.varspez,
  5387. pvs.vardef,pvs.varoptions);
  5388. npvs.defaultconstsym:=pvs.defaultconstsym;
  5389. tabstractprocdef(result).parast.insertsym(npvs);
  5390. end;
  5391. constsym:
  5392. begin
  5393. // ignore, reuse original constym. Should also be duplicated
  5394. // be safe though
  5395. end;
  5396. symconst.typesym:
  5397. begin
  5398. // reuse original, part of generic declaration
  5399. end
  5400. else
  5401. internalerror(201160604);
  5402. end;
  5403. end;
  5404. end;
  5405. tabstractprocdef(result).savesize:=savesize;
  5406. if (typ<>procvardef) and
  5407. (newtyp=procvardef) then
  5408. begin
  5409. { procvars can't be (class)constructures/destructors etc }
  5410. if proctypeoption=potype_constructor then
  5411. begin
  5412. tabstractprocdef(result).returndef:=tdef(owner.defowner);
  5413. if not(is_implicit_pointer_object_type(returndef) or
  5414. (returndef.typ<>objectdef)) then
  5415. tabstractprocdef(result).returndef:=cpointerdef.getreusable(tabstractprocdef(result).returndef);
  5416. tabstractprocdef(result).proctypeoption:=potype_function;
  5417. end
  5418. else if is_void(returndef) then
  5419. tabstractprocdef(result).proctypeoption:=potype_procedure
  5420. else
  5421. tabstractprocdef(result).proctypeoption:=potype_function;
  5422. end
  5423. else
  5424. tabstractprocdef(result).proctypeoption:=proctypeoption;
  5425. tabstractprocdef(result).proccalloption:=proccalloption;
  5426. tabstractprocdef(result).procoptions:=procoptions;
  5427. if (copytyp=pc_bareproc) then
  5428. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions*[po_explicitparaloc,po_hascallingconvention,po_varargs,po_iocheck,po_has_importname,po_has_importdll];
  5429. if newtyp=procvardef then
  5430. tabstractprocdef(result).procoptions:=tabstractprocdef(result).procoptions-[po_has_importname,po_has_importdll];
  5431. if copytyp=pc_address_only then
  5432. include(tabstractprocdef(result).procoptions,po_addressonly);
  5433. tabstractprocdef(result).callerargareasize:=callerargareasize;
  5434. tabstractprocdef(result).calleeargareasize:=calleeargareasize;
  5435. tabstractprocdef(result).maxparacount:=maxparacount;
  5436. tabstractprocdef(result).minparacount:=minparacount;
  5437. if po_explicitparaloc in procoptions then
  5438. tabstractprocdef(result).funcretloc[callerside]:=funcretloc[callerside].getcopy;
  5439. { recalculate parameter info }
  5440. tabstractprocdef(result).has_paraloc_info:=callnoside;
  5441. {$ifdef m68k}
  5442. tabstractprocdef(result).exp_funcretloc:=exp_funcretloc;
  5443. {$endif}
  5444. if (typ=procdef) and
  5445. (newtyp=procvardef) and
  5446. (owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  5447. include(tprocvardef(result).procoptions,po_methodpointer);
  5448. end;
  5449. function tabstractprocdef.compatible_with_pointerdef_size(ptr: tpointerdef): boolean;
  5450. begin
  5451. result:=is_addressonly;
  5452. end;
  5453. procedure tabstractprocdef.check_mark_as_nested;
  5454. begin
  5455. { nested procvars require that nested functions use the Delphi-style
  5456. nested procedure calling convention }
  5457. if (parast.symtablelevel>normal_function_level) and
  5458. (m_nested_procvars in current_settings.modeswitches) then
  5459. include(procoptions,po_delphi_nested_cc);
  5460. end;
  5461. procedure tabstractprocdef.init_paraloc_info(side: tcallercallee);
  5462. begin
  5463. if (side in [callerside,callbothsides]) and
  5464. not(has_paraloc_info in [callerside,callbothsides]) then
  5465. begin
  5466. if not is_c_variadic(self) then
  5467. callerargareasize:=paramanager.create_paraloc_info(self,callerside)
  5468. else
  5469. callerargareasize:=paramanager.create_varargs_paraloc_info(self,callerside,nil);
  5470. if has_paraloc_info in [calleeside,callbothsides] then
  5471. has_paraloc_info:=callbothsides
  5472. else
  5473. has_paraloc_info:=callerside;
  5474. end;
  5475. if (side in [calleeside,callbothsides]) and
  5476. not(has_paraloc_info in [calleeside,callbothsides]) then
  5477. begin
  5478. if not is_c_variadic(self) then
  5479. calleeargareasize:=paramanager.create_paraloc_info(self,calleeside)
  5480. else
  5481. callerargareasize:=paramanager.create_varargs_paraloc_info(self,calleeside,nil);
  5482. if has_paraloc_info in [callerside,callbothsides] then
  5483. has_paraloc_info:=callbothsides
  5484. else
  5485. has_paraloc_info:=calleeside;
  5486. end;
  5487. end;
  5488. procedure tabstractprocdef.done_paraloc_info(side: tcallercallee);
  5489. var
  5490. i: longint;
  5491. begin
  5492. if (side in [callerside,callbothsides]) and
  5493. (has_paraloc_info in [callerside,callbothsides]) then
  5494. begin
  5495. funcretloc[callerside].done;
  5496. for i:=0 to paras.count-1 do
  5497. tparavarsym(paras[i]).paraloc[callerside].done;
  5498. if has_paraloc_info=callerside then
  5499. has_paraloc_info:=callnoside
  5500. else
  5501. has_paraloc_info:=calleeside;
  5502. end;
  5503. if (side in [calleeside,callbothsides]) and
  5504. (has_paraloc_info in [calleeside,callbothsides]) then
  5505. begin
  5506. funcretloc[calleeside].done;
  5507. for i:=0 to paras.count-1 do
  5508. tparavarsym(paras[i]).paraloc[calleeside].done;
  5509. if has_paraloc_info=calleeside then
  5510. has_paraloc_info:=callnoside
  5511. else
  5512. has_paraloc_info:=callerside;
  5513. end;
  5514. end;
  5515. function tabstractprocdef.stack_tainting_parameter(side: tcallercallee): boolean;
  5516. var
  5517. p: tparavarsym;
  5518. ploc: PCGParalocation;
  5519. i: longint;
  5520. begin
  5521. result:=false;
  5522. init_paraloc_info(side);
  5523. for i:=0 to parast.SymList.Count-1 do
  5524. if tsym(parast.SymList[i]).typ=paravarsym then
  5525. begin
  5526. p:=tparavarsym(parast.SymList[i]);
  5527. if not p.is_used then
  5528. continue;
  5529. { check if no parameter is located on the stack }
  5530. if (is_open_array(p.vardef) or
  5531. is_array_of_const(p.vardef)) and (p.varspez=vs_value) then
  5532. begin
  5533. result:=true;
  5534. exit;
  5535. end;
  5536. ploc:=p.paraloc[side].location;
  5537. while assigned(ploc) do
  5538. begin
  5539. if (ploc^.loc=LOC_REFERENCE) then
  5540. begin
  5541. result:=true;
  5542. exit
  5543. end;
  5544. ploc:=ploc^.next;
  5545. end;
  5546. end;
  5547. end;
  5548. function tabstractprocdef.is_pushleftright: boolean;
  5549. begin
  5550. result:=false;
  5551. end;
  5552. function tabstractprocdef.address_type: tdef;
  5553. begin
  5554. result:=voidcodepointertype;
  5555. end;
  5556. function tabstractprocdef.ofs_address_type:tdef;
  5557. begin
  5558. result:=address_type;
  5559. end;
  5560. procedure tabstractprocdef.declared_far;
  5561. begin
  5562. Message1(parser_w_proc_directive_ignored,'FAR');
  5563. end;
  5564. procedure tabstractprocdef.declared_near;
  5565. begin
  5566. Message1(parser_w_proc_directive_ignored,'NEAR');
  5567. end;
  5568. function tabstractprocdef.generate_safecall_wrapper: boolean;
  5569. begin
  5570. {$ifdef SUPPORT_SAFECALL}
  5571. result:=
  5572. (proccalloption=pocall_safecall) and
  5573. not(po_assembler in procoptions) and
  5574. (tf_safecall_exceptions in target_info.flags);
  5575. {$else SUPPORT_SAFECALL}
  5576. result:=false;
  5577. {$endif}
  5578. end;
  5579. function tabstractprocdef.is_generic_param(def:tdef): boolean;
  5580. begin
  5581. if def.typ=undefineddef then
  5582. result:=def.owner=self.parast
  5583. else if def.typ=objectdef then
  5584. result:=(def.owner=self.parast) and (tstoreddef(def).genconstraintdata<>nil)
  5585. else
  5586. result:=false;
  5587. end;
  5588. function tabstractprocdef.wpo_may_create_instance(optionalmethodpointer: tnode): boolean;
  5589. begin
  5590. result:=
  5591. (proctypeoption=potype_constructor) or
  5592. ((typ=procdef) and
  5593. ((not assigned(optionalmethodpointer) and
  5594. is_class(tdef(owner.defowner))) or
  5595. (assigned(optionalmethodpointer) and
  5596. ((optionalmethodpointer.resultdef.typ=classrefdef) or
  5597. (optionalmethodpointer.nodetype=typen)))) and
  5598. (tprocdef(self).procsym.Name='NEWINSTANCE'))
  5599. end;
  5600. {***************************************************************************
  5601. TPROCDEF
  5602. ***************************************************************************}
  5603. function tprocdef.getparentfpsym: tsym;
  5604. begin
  5605. if not assigned(_parentfpsym) then
  5606. begin
  5607. _parentfpsym:=tsym(parast.Find('parentfp'));
  5608. if not assigned(_parentfpsym) then
  5609. internalerror(200309281);
  5610. end;
  5611. result:=_parentfpsym;
  5612. end;
  5613. function tprocdef.Getis_implemented: boolean;
  5614. begin
  5615. result:=not assigned(implprocdefinfo) or implprocdefinfo^.is_implemented;
  5616. end;
  5617. procedure tprocdef.Setis_implemented(AValue: boolean);
  5618. begin
  5619. if not assigned(implprocdefinfo) then
  5620. internalerror(2020062101);
  5621. implprocdefinfo^.is_implemented:=AValue;
  5622. end;
  5623. function tprocdef.getwas_anonymous:boolean;
  5624. begin
  5625. result:=assigned(implprocdefinfo) and implprocdefinfo^.was_anonymous;
  5626. end;
  5627. procedure tprocdef.setwas_anonymous(avalue:boolean);
  5628. begin
  5629. if not assigned(implprocdefinfo) then
  5630. internalerror(2022020502);
  5631. implprocdefinfo^.was_anonymous:=avalue;
  5632. end;
  5633. function tprocdef.gethas_capturer:boolean;
  5634. begin
  5635. result:=assigned(implprocdefinfo) and implprocdefinfo^.has_capturer;
  5636. end;
  5637. procedure tprocdef.sethas_capturer(avalue:boolean);
  5638. begin
  5639. if not assigned(implprocdefinfo) then
  5640. internalerror(2022020503);
  5641. implprocdefinfo^.has_capturer:=avalue;
  5642. end;
  5643. function tprocdef.Getcapturedsyms:tfplist;
  5644. begin
  5645. if not assigned(implprocdefinfo) then
  5646. result:=nil
  5647. else
  5648. result:=implprocdefinfo^.capturedsyms;
  5649. end;
  5650. function tprocdef.store_localst: boolean;
  5651. begin
  5652. result:=has_inlininginfo or (df_generic in defoptions);
  5653. end;
  5654. function tprocdef.GetResultName: PShortString;
  5655. begin
  5656. if not assigned(implprocdefinfo) then
  5657. internalerror(2014010301);
  5658. result:=implprocdefinfo^.resultname;
  5659. end;
  5660. procedure tprocdef.SetResultName(AValue: PShortString);
  5661. begin
  5662. if not assigned(implprocdefinfo) then
  5663. internalerror(2014010302);
  5664. implprocdefinfo^.resultname:=AValue;
  5665. end;
  5666. function tprocdef.GetParentFPInitBlock: tnode;
  5667. begin
  5668. if not assigned(implprocdefinfo) then
  5669. internalerror(2014010303);
  5670. result:=implprocdefinfo^.parentfpinitblock;
  5671. end;
  5672. function tprocdef.GetParentFPStruct: tsym;
  5673. begin
  5674. if not assigned(implprocdefinfo) then
  5675. internalerror(2014010304);
  5676. result:=implprocdefinfo^.parentfpstruct;
  5677. end;
  5678. function tprocdef.GetParentFPStructPtrType: tdef;
  5679. begin
  5680. if not assigned(implprocdefinfo) then
  5681. internalerror(2014010305);
  5682. result:=implprocdefinfo^.parentfpstructptrtype;
  5683. end;
  5684. procedure tprocdef.SetParentFPInitBlock(AValue: tnode);
  5685. begin
  5686. if not assigned(implprocdefinfo) then
  5687. internalerror(2014010306);
  5688. implprocdefinfo^.parentfpinitblock:=AValue;
  5689. end;
  5690. function tprocdef.Getprocendtai: tai;
  5691. begin
  5692. if not assigned(implprocdefinfo) then
  5693. internalerror(2014010307);
  5694. result:=implprocdefinfo^.procendtai;
  5695. end;
  5696. function tprocdef.Getprocstarttai: tai;
  5697. begin
  5698. if not assigned(implprocdefinfo) then
  5699. internalerror(2014010308);
  5700. result:=implprocdefinfo^.procstarttai;
  5701. end;
  5702. procedure tprocdef.Setprocendtai(AValue: tai);
  5703. begin
  5704. if not assigned(implprocdefinfo) then
  5705. internalerror(2014010309);
  5706. implprocdefinfo^.procendtai:=AValue;
  5707. end;
  5708. function tprocdef.Getskpara: pointer;
  5709. begin
  5710. if not assigned(implprocdefinfo) then
  5711. internalerror(2014010310);
  5712. result:=implprocdefinfo^.skpara;
  5713. end;
  5714. procedure tprocdef.Setskpara(AValue: pointer);
  5715. begin
  5716. if not assigned(implprocdefinfo) then
  5717. internalerror(2014010311);
  5718. implprocdefinfo^.skpara:=AValue;
  5719. end;
  5720. function tprocdef.Getpersonality: tprocdef;
  5721. begin
  5722. if not assigned(implprocdefinfo) then
  5723. internalerror(2016121704);
  5724. result:=implprocdefinfo^.personality;
  5725. end;
  5726. procedure tprocdef.Setpersonality(AValue: tprocdef);
  5727. begin
  5728. if not assigned(implprocdefinfo) then
  5729. internalerror(2016121705);
  5730. implprocdefinfo^.personality:=AValue;
  5731. end;
  5732. function tprocdef.Getforwarddef: boolean;
  5733. begin
  5734. if not assigned(implprocdefinfo) then
  5735. internalerror(2014010312);
  5736. result:=implprocdefinfo^.forwarddef;
  5737. end;
  5738. function tprocdef.Gethasforward: boolean;
  5739. begin
  5740. if not assigned(implprocdefinfo) then
  5741. internalerror(2014010313);
  5742. result:=implprocdefinfo^.hasforward;
  5743. end;
  5744. function tprocdef.Getinterfacedef: boolean;
  5745. begin
  5746. if not assigned(implprocdefinfo) then
  5747. internalerror(2014010314);
  5748. result:=implprocdefinfo^.interfacedef;
  5749. end;
  5750. procedure tprocdef.Setforwarddef(AValue: boolean);
  5751. begin
  5752. if not assigned(implprocdefinfo) then
  5753. internalerror(2014010315);
  5754. implprocdefinfo^.forwarddef:=AValue;
  5755. end;
  5756. procedure tprocdef.Sethasforward(AValue: boolean);
  5757. begin
  5758. if not assigned(implprocdefinfo) then
  5759. internalerror(2014010316);
  5760. implprocdefinfo^.hasforward:=AValue;
  5761. end;
  5762. function tprocdef.GetIsEmpty: boolean;
  5763. begin
  5764. result:=pio_empty in implprocoptions;
  5765. end;
  5766. procedure tprocdef.SetIsEmpty(AValue: boolean);
  5767. begin
  5768. if AValue then
  5769. include(implprocoptions,pio_empty)
  5770. else
  5771. exclude(implprocoptions,pio_empty);
  5772. end;
  5773. function tprocdef.GetHasInliningInfo: boolean;
  5774. begin
  5775. result:=pio_has_inlininginfo in implprocoptions;
  5776. end;
  5777. procedure tprocdef.SetHasInliningInfo(AValue: boolean);
  5778. begin
  5779. if AValue then
  5780. include(implprocoptions,pio_has_inlininginfo)
  5781. else
  5782. exclude(implprocoptions,pio_has_inlininginfo);
  5783. end;
  5784. procedure tprocdef.Setinterfacedef(AValue: boolean);
  5785. begin
  5786. if not assigned(implprocdefinfo) then
  5787. internalerror(2014010317);
  5788. implprocdefinfo^.interfacedef:=AValue;
  5789. end;
  5790. procedure tprocdef.Setprocstarttai(AValue: tai);
  5791. begin
  5792. if not assigned(implprocdefinfo) then
  5793. internalerror(2014010318);
  5794. implprocdefinfo^.procstarttai:=AValue;
  5795. end;
  5796. procedure tprocdef.SetParentFPStruct(AValue: tsym);
  5797. begin
  5798. if not assigned(implprocdefinfo) then
  5799. internalerror(2014010319);
  5800. implprocdefinfo^.parentfpstruct:=AValue;
  5801. end;
  5802. procedure tprocdef.SetParentFPStructPtrType(AValue: tdef);
  5803. begin
  5804. if not assigned(implprocdefinfo) then
  5805. internalerror(2014010320);
  5806. implprocdefinfo^.parentfpstructptrtype:=AValue;
  5807. end;
  5808. constructor tprocdef.create(level:byte;doregister:boolean);
  5809. begin
  5810. inherited create(procdef,level,doregister);
  5811. implprocdefinfo:=allocmem(sizeof(implprocdefinfo^));
  5812. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  5813. {$ifdef symansistr}
  5814. _mangledname:='';
  5815. {$else symansistr}
  5816. _mangledname:=nil;
  5817. {$endif symansistr}
  5818. {$ifndef DISABLE_FAST_OVERLOAD_PATCH}
  5819. seenmarker := nil;
  5820. {$endif}
  5821. fileinfo:=current_filepos;
  5822. extnumber:=$ffff;
  5823. aliasnames:=TCmdStrList.create;
  5824. funcretsym:=nil;
  5825. funcretsymderef.reset;
  5826. procsymderef.reset;
  5827. forwarddef:=true;
  5828. interfacedef:=false;
  5829. hasforward:=false;
  5830. struct := nil;
  5831. structderef.reset;
  5832. import_dll:=nil;
  5833. import_name:=nil;
  5834. import_nr:=0;
  5835. inlininginfo:=nil;
  5836. deprecatedmsg:=nil;
  5837. genericdecltokenbuf:=nil;
  5838. if cs_opt_fastmath in current_settings.optimizerswitches then
  5839. include(implprocoptions, pio_fastmath);
  5840. end;
  5841. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  5842. var
  5843. i,aliasnamescount,sizeleft : longint;
  5844. level : byte;
  5845. buf : array[0..255] of byte;
  5846. begin
  5847. inherited ppuload(procdef,ppufile);
  5848. {$ifdef symansistr}
  5849. if po_has_mangledname in procoptions then
  5850. _mangledname:=ppufile.getansistring
  5851. else
  5852. _mangledname:='';
  5853. {$else symansistr}
  5854. if po_has_mangledname in procoptions then
  5855. _mangledname:=ppufile.getpshortstring
  5856. else
  5857. _mangledname:=nil;
  5858. {$endif symansistr}
  5859. extnumber:=ppufile.getword;
  5860. level:=ppufile.getbyte;
  5861. ppufile.getderef(structderef);
  5862. ppufile.getderef(procsymderef);
  5863. ppufile.getposinfo(fileinfo);
  5864. visibility:=tvisibility(ppufile.getbyte);
  5865. ppufile.getset(tppuset2(symoptions));
  5866. if sp_has_deprecated_msg in symoptions then
  5867. deprecatedmsg:=ppufile.getpshortstring
  5868. else
  5869. deprecatedmsg:=nil;
  5870. { import stuff }
  5871. if po_has_importdll in procoptions then
  5872. import_dll:=ppufile.getpshortstring
  5873. else
  5874. import_dll:=nil;
  5875. if po_has_importname in procoptions then
  5876. import_name:=ppufile.getpshortstring
  5877. else
  5878. import_name:=nil;
  5879. import_nr:=ppufile.getword;
  5880. if (po_msgint in procoptions) then
  5881. messageinf.i:=ppufile.getlongint;
  5882. if (po_msgstr in procoptions) then
  5883. messageinf.str:=ppufile.getpshortstring;
  5884. if (po_dispid in procoptions) then
  5885. dispid:=ppufile.getlongint;
  5886. { inline stuff }
  5887. ppufile.getset(tppuset1(implprocoptions));
  5888. if has_inlininginfo then
  5889. begin
  5890. ppufile.getderef(funcretsymderef);
  5891. new(inlininginfo);
  5892. ppufile.getset(tppuset4(inlininginfo^.flags));
  5893. end
  5894. else
  5895. begin
  5896. inlininginfo:=nil;
  5897. funcretsym:=nil;
  5898. end;
  5899. aliasnames:=TCmdStrList.create;
  5900. { count alias names }
  5901. aliasnamescount:=ppufile.getbyte;
  5902. for i:=1 to aliasnamescount do
  5903. aliasnames.insert(ppufile.getstring);
  5904. { load the token stream containing the declaration }
  5905. sizeleft:=ppufile.getlongint;
  5906. if sizeleft>0 then
  5907. begin
  5908. init_genericdecl;
  5909. while sizeleft>0 do
  5910. begin
  5911. if sizeleft>sizeof(buf) then
  5912. i:=sizeof(buf)
  5913. else
  5914. i:=sizeleft;
  5915. ppufile.getdata(buf,i);
  5916. genericdecltokenbuf.write(buf,i);
  5917. dec(sizeleft,i);
  5918. end;
  5919. end;
  5920. ppuload_platform(ppufile);
  5921. { load para symtable }
  5922. parast:=tparasymtable.create(self,level);
  5923. tparasymtable(parast).ppuload(ppufile);
  5924. { load local symtable }
  5925. if store_localst then
  5926. begin
  5927. localst:=tlocalsymtable.create(self,level);
  5928. tlocalsymtable(localst).ppuload(ppufile);
  5929. end
  5930. else
  5931. localst:=nil;
  5932. { inline stuff }
  5933. if has_inlininginfo then
  5934. inlininginfo^.code:=ppuloadnodetree(ppufile);
  5935. { default values for no persistent data }
  5936. if (cs_link_deffile in current_settings.globalswitches) and
  5937. (tf_need_export in target_info.flags) and
  5938. (po_exports in procoptions) then
  5939. deffile.AddExport(mangledname);
  5940. { Disable po_has_inlining until the derefimpl is done }
  5941. has_inlininginfo:=false;
  5942. end;
  5943. destructor tprocdef.destroy;
  5944. begin
  5945. aliasnames.free;
  5946. aliasnames:=nil;
  5947. if assigned(localst) and
  5948. (localst.symtabletype<>staticsymtable) then
  5949. begin
  5950. {$ifdef MEMDEBUG}
  5951. memproclocalst.start;
  5952. {$endif MEMDEBUG}
  5953. localst.free;
  5954. localst:=nil;
  5955. {$ifdef MEMDEBUG}
  5956. memproclocalst.start;
  5957. {$endif MEMDEBUG}
  5958. end;
  5959. if assigned(inlininginfo) then
  5960. begin
  5961. {$ifdef MEMDEBUG}
  5962. memprocnodetree.start;
  5963. {$endif MEMDEBUG}
  5964. tnode(inlininginfo^.code).free;
  5965. {$ifdef MEMDEBUG}
  5966. memprocnodetree.start;
  5967. {$endif MEMDEBUG}
  5968. dispose(inlininginfo);
  5969. inlininginfo:=nil;
  5970. end;
  5971. freeimplprocdefinfo;
  5972. genericdecltokenbuf.free;
  5973. genericdecltokenbuf:=nil;
  5974. stringdispose(import_dll);
  5975. stringdispose(import_name);
  5976. stringdispose(deprecatedmsg);
  5977. if (po_msgstr in procoptions) then
  5978. stringdispose(messageinf.str);
  5979. {$ifndef symansistr}
  5980. if assigned(_mangledname) then
  5981. begin
  5982. {$ifdef MEMDEBUG}
  5983. memmanglednames.start;
  5984. {$endif MEMDEBUG}
  5985. stringdispose(_mangledname);
  5986. {$ifdef MEMDEBUG}
  5987. memmanglednames.stop;
  5988. {$endif MEMDEBUG}
  5989. end;
  5990. {$endif symansistr}
  5991. inherited destroy;
  5992. end;
  5993. procedure tprocdef.freeimplprocdefinfo;
  5994. begin
  5995. if assigned(implprocdefinfo) then
  5996. begin
  5997. stringdispose(implprocdefinfo^.resultname);
  5998. TFPList.FreeAndNilDisposing(implprocdefinfo^.capturedsyms,typeinfo(tcapturedsyminfo));
  5999. freemem(implprocdefinfo);
  6000. implprocdefinfo:=nil;
  6001. end;
  6002. end;
  6003. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  6004. var
  6005. oldintfcrc : boolean;
  6006. aliasnamescount,i,sizeleft : longint;
  6007. item : TCmdStrListItem;
  6008. buf : array[0..255] of byte;
  6009. begin
  6010. { released procdef? }
  6011. if not assigned(parast) then
  6012. exit;
  6013. inherited ppuwrite(ppufile);
  6014. {$ifdef symansistr}
  6015. if po_has_mangledname in procoptions then
  6016. ppufile.putansistring(_mangledname);
  6017. {$else symansistr}
  6018. if po_has_mangledname in procoptions then
  6019. ppufile.putstring(_mangledname^);
  6020. {$endif symansistr}
  6021. ppufile.putword(extnumber);
  6022. ppufile.putbyte(parast.symtablelevel);
  6023. ppufile.putderef(structderef);
  6024. ppufile.putderef(procsymderef);
  6025. ppufile.putposinfo(fileinfo);
  6026. ppufile.putbyte(byte(visibility));
  6027. ppufile.putset(tppuset2(symoptions));
  6028. if sp_has_deprecated_msg in symoptions then
  6029. ppufile.putstring(deprecatedmsg^);
  6030. { import }
  6031. if po_has_importdll in procoptions then
  6032. ppufile.putstring(import_dll^);
  6033. if po_has_importname in procoptions then
  6034. ppufile.putstring(import_name^);
  6035. ppufile.putword(import_nr);
  6036. if (po_msgint in procoptions) then
  6037. ppufile.putlongint(messageinf.i);
  6038. if (po_msgstr in procoptions) then
  6039. ppufile.putstring(messageinf.str^);
  6040. if (po_dispid in procoptions) then
  6041. ppufile.putlongint(dispid);
  6042. { inline stuff }
  6043. oldintfcrc:=ppufile.do_crc;
  6044. ppufile.do_crc:=false;
  6045. ppufile.putset(tppuset1(implprocoptions));
  6046. if has_inlininginfo then
  6047. begin
  6048. ppufile.putderef(funcretsymderef);
  6049. ppufile.putset(tppuset4(inlininginfo^.flags));
  6050. end;
  6051. { count alias names }
  6052. aliasnamescount:=0;
  6053. item:=TCmdStrListItem(aliasnames.first);
  6054. while assigned(item) do
  6055. begin
  6056. inc(aliasnamescount);
  6057. item:=TCmdStrListItem(item.next);
  6058. end;
  6059. if aliasnamescount>255 then
  6060. internalerror(200711021);
  6061. ppufile.putbyte(aliasnamescount);
  6062. item:=TCmdStrListItem(aliasnames.first);
  6063. while assigned(item) do
  6064. begin
  6065. ppufile.putstring(item.str);
  6066. item:=TCmdStrListItem(item.next);
  6067. end;
  6068. ppufile.do_crc:=oldintfcrc;
  6069. { generic tokens for the declaration }
  6070. if assigned(genericdecltokenbuf) and (genericdecltokenbuf.size>0) then
  6071. begin
  6072. sizeleft:=genericdecltokenbuf.size;
  6073. genericdecltokenbuf.seek(0);
  6074. ppufile.putlongint(sizeleft);
  6075. while sizeleft>0 do
  6076. begin
  6077. if sizeleft>sizeof(buf) then
  6078. i:=sizeof(buf)
  6079. else
  6080. i:=sizeleft;
  6081. genericdecltokenbuf.read(buf,i);
  6082. ppufile.putdata(buf,i);
  6083. dec(sizeleft,i);
  6084. end;
  6085. end
  6086. else
  6087. ppufile.putlongint(0);
  6088. { write this entry }
  6089. writeentry(ppufile,ibprocdef);
  6090. { Save the para symtable, this is taken from the interface }
  6091. tparasymtable(parast).ppuwrite(ppufile);
  6092. { save localsymtable for inline procedures or when local
  6093. browser info is requested, this has no influence on the crc }
  6094. if store_localst and not ppufile.crc_only then
  6095. begin
  6096. oldintfcrc:=ppufile.do_crc;
  6097. ppufile.do_crc:=false;
  6098. tlocalsymtable(localst).ppuwrite(ppufile);
  6099. ppufile.do_crc:=oldintfcrc;
  6100. end;
  6101. { node tree for inlining }
  6102. oldintfcrc:=ppufile.do_crc;
  6103. ppufile.do_crc:=false;
  6104. if has_inlininginfo then
  6105. ppuwritenodetree(ppufile,inlininginfo^.code);
  6106. ppufile.do_crc:=oldintfcrc;
  6107. end;
  6108. function tprocdef.fullprocname(showhidden:boolean):string;
  6109. var
  6110. pno: tprocnameoptions;
  6111. begin
  6112. pno:=[];
  6113. if showhidden then
  6114. include(pno,pno_showhidden);
  6115. result:=customprocname(pno);
  6116. end;
  6117. function tprocdef.customprocname(pno: tprocnameoptions):ansistring;
  6118. var
  6119. s, hs, rn : ansistring;
  6120. t : ttoken;
  6121. module : tmodule;
  6122. syssym : tsyssym;
  6123. begin
  6124. {$ifdef EXTDEBUG}
  6125. include(pno,pno_showhidden);
  6126. {$endif EXTDEBUG}
  6127. s:='';
  6128. rn:='';
  6129. if proctypeoption=potype_operator then
  6130. begin
  6131. for t:=NOTOKEN to last_overloaded do
  6132. if procsym.realname='$'+overloaded_names[t] then
  6133. begin
  6134. s:='operator ';
  6135. if (pno_ownername in pno) and
  6136. assigned(struct) then
  6137. s:=s+struct.RttiName+'.';
  6138. s:=s+arraytokeninfo[t].str;
  6139. if not (pno_noparams in pno) then
  6140. s:=s+typename_paras(pno);
  6141. break;
  6142. end;
  6143. end
  6144. else
  6145. begin
  6146. if (po_classmethod in procoptions) and
  6147. not(pno_noclassmarker in pno) then
  6148. s:='class ';
  6149. case proctypeoption of
  6150. potype_constructor,
  6151. potype_class_constructor:
  6152. s:=s+'constructor ';
  6153. potype_class_destructor,
  6154. potype_destructor:
  6155. s:=s+'destructor ';
  6156. else
  6157. if (pno_proctypeoption in pno) and not (po_anonymous in procoptions) then
  6158. begin
  6159. if assigned(returndef) and
  6160. not(is_void(returndef)) then
  6161. s:=s+'function '
  6162. else
  6163. s:=s+'procedure ';
  6164. end;
  6165. end;
  6166. if (pno_ownername in pno) and
  6167. (owner.symtabletype in [recordsymtable,objectsymtable]) then
  6168. s:=s+tabstractrecorddef(owner.defowner).RttiName+'.';
  6169. if (po_compilerproc in procoptions) and (extnumber<>$ffff) then
  6170. begin
  6171. syssym:=tsyssym.find_by_number(extnumber);
  6172. if not assigned(syssym) then
  6173. internalerror(2016060305);
  6174. rn:=syssym.realname;
  6175. end
  6176. else if po_anonymous in procoptions then
  6177. begin
  6178. s:=s+'anonymous ';
  6179. if assigned(returndef) and
  6180. not(is_void(returndef)) then
  6181. s:=s+'function'
  6182. else
  6183. s:=s+'procedure';
  6184. end
  6185. else if pno_prettynames in pno then
  6186. rn:=procsym.prettyname
  6187. else
  6188. rn:=procsym.RealName;
  6189. if (pno_noleadingdollar in pno) and
  6190. (rn[1]='$') then
  6191. delete(rn,1,1);
  6192. s:=s+rn;
  6193. if not (pno_noparams in pno) then
  6194. s:=s+typename_paras(pno);
  6195. end;
  6196. if not(proctypeoption in [potype_constructor,potype_destructor,
  6197. potype_class_constructor,potype_class_destructor]) and
  6198. assigned(returndef) and
  6199. not(is_void(returndef)) then
  6200. begin
  6201. if assigned(returndef.typesym) then
  6202. begin
  6203. module:=find_module_from_symtable(returndef.typesym.owner);
  6204. if module <> current_module then
  6205. s:=s+':'+module.realmodulename^+'.'
  6206. else
  6207. s:=s+':';
  6208. if pno_prettynames in pno then
  6209. hs:=returndef.typesym.prettyname
  6210. else
  6211. hs:=returndef.typesym.realname;
  6212. if hs[1]<>'$' then
  6213. s:=s+returndef.OwnerHierarchyName+hs
  6214. else
  6215. s:=s+returndef.GetTypeName;
  6216. end
  6217. else
  6218. s:=s+':'+returndef.GetTypeName;
  6219. end;
  6220. if not (po_anonymous in procoptions) then
  6221. if assigned(owner) and (owner.symtabletype=localsymtable) then
  6222. s:=s+' is nested'
  6223. else if po_is_block in procoptions then
  6224. s:=s+' is block';
  6225. s:=s+';';
  6226. if po_far in procoptions then
  6227. s:=s+' far;';
  6228. { forced calling convention? }
  6229. if (po_hascallingconvention in procoptions) then
  6230. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  6231. if (po_staticmethod in procoptions) and
  6232. not (proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  6233. s:=s+' Static;';
  6234. if pno_mangledname in pno then
  6235. s:=s+' -- mangled name: '+mangledname;
  6236. customprocname:=s;
  6237. end;
  6238. function tprocdef.is_methodpointer:boolean;
  6239. begin
  6240. { don't check assigned(_class), that's also the case for nested
  6241. procedures inside methods }
  6242. result:=(owner.symtabletype in [recordsymtable,ObjectSymtable]) and not no_self_node;
  6243. end;
  6244. function tprocdef.is_addressonly:boolean;
  6245. begin
  6246. result:=assigned(owner) and
  6247. not is_methodpointer and
  6248. (not(m_nested_procvars in current_settings.modeswitches) or
  6249. not is_nested_pd(self)) and
  6250. (
  6251. not (po_anonymous in procoptions) or
  6252. not assigned(capturedsyms) or
  6253. (capturedsyms.count=0)
  6254. );
  6255. end;
  6256. procedure tprocdef.make_external;
  6257. begin
  6258. include(procoptions,po_external);
  6259. forwarddef:=false;
  6260. end;
  6261. procedure tprocdef.init_genericdecl;
  6262. begin
  6263. if assigned(genericdecltokenbuf) then
  6264. internalerror(2015061901);
  6265. genericdecltokenbuf:=tdynamicarray.create(256);
  6266. end;
  6267. function tprocdef.get_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean;
  6268. begin
  6269. result:=false;
  6270. if proctypeoption=potype_constructor then
  6271. begin
  6272. result:=true;
  6273. ressym:=tsym(parast.Find('self'));
  6274. resdef:=tabstractnormalvarsym(ressym).vardef;
  6275. { and TP-style constructors return a pointer to self }
  6276. if is_object(resdef) then
  6277. resdef:=cpointerdef.getreusable(resdef);
  6278. end
  6279. else if (proccalloption=pocall_safecall) and
  6280. (tf_safecall_exceptions in target_info.flags) then
  6281. begin
  6282. result:=true;
  6283. ressym:=tsym(localst.Find('safecallresult'));
  6284. resdef:=tabstractnormalvarsym(ressym).vardef;
  6285. end
  6286. else if not is_void(returndef) then
  6287. begin
  6288. result:=true;
  6289. ressym:=funcretsym;
  6290. resdef:=tabstractnormalvarsym(ressym).vardef;
  6291. end;
  6292. end;
  6293. function tprocdef.get_safecall_funcretsym_info(out ressym: tsym; out resdef: tdef): boolean;
  6294. begin
  6295. result:=false;
  6296. if (proctypeoption<>potype_constructor) and
  6297. (proccalloption=pocall_safecall) and
  6298. (tf_safecall_exceptions in target_info.flags) then
  6299. begin
  6300. result:=true;
  6301. ressym:=tsym(localst.Find('safecallresult'));
  6302. resdef:=tabstractnormalvarsym(ressym).vardef;
  6303. end
  6304. end;
  6305. procedure tprocdef.add_captured_sym(sym:tsym;def:tdef;const filepos:tfileposinfo);
  6306. var
  6307. i : longint;
  6308. capturedsym : pcapturedsyminfo;
  6309. begin
  6310. if not assigned(implprocdefinfo) then
  6311. internalerror(2021052601);
  6312. if not assigned(implprocdefinfo^.capturedsyms) then
  6313. implprocdefinfo^.capturedsyms:=tfplist.create;
  6314. for i:=0 to implprocdefinfo^.capturedsyms.count-1 do
  6315. begin
  6316. capturedsym:=pcapturedsyminfo(implprocdefinfo^.capturedsyms[i]);
  6317. if (capturedsym^.sym=sym) and (capturedsym^.def=def) then
  6318. exit;
  6319. end;
  6320. new(capturedsym);
  6321. capturedsym^.sym:=sym;
  6322. capturedsym^.def:=def;
  6323. capturedsym^.fileinfo:=filepos;
  6324. implprocdefinfo^.capturedsyms.add(capturedsym);
  6325. end;
  6326. function tprocdef.has_alias_name(const s: TSymStr): boolean;
  6327. var
  6328. item : TCmdStrListItem;
  6329. begin
  6330. result:=true;
  6331. if mangledname=s then
  6332. exit;
  6333. item:=TCmdStrListItem(aliasnames.first);
  6334. while assigned(item) do
  6335. begin
  6336. if item.str=s then
  6337. exit;
  6338. item:=TCmdStrListItem(item.next);
  6339. end;
  6340. result:=false;
  6341. end;
  6342. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  6343. begin
  6344. case t of
  6345. gs_local :
  6346. GetSymtable:=localst;
  6347. gs_para :
  6348. GetSymtable:=parast;
  6349. else
  6350. GetSymtable:=nil;
  6351. end;
  6352. end;
  6353. function tprocdef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef;
  6354. var
  6355. j : longint;
  6356. begin
  6357. result:=inherited;
  6358. if newtyp=procvardef then
  6359. begin
  6360. { create new paralist }
  6361. tprocvardef(result).calcparas;
  6362. exit;
  6363. end;
  6364. { don't copy mangled name, can be different }
  6365. tprocdef(result).messageinf:=messageinf;
  6366. tprocdef(result).dispid:=dispid;
  6367. if po_msgstr in procoptions then
  6368. tprocdef(result).messageinf.str:=stringdup(messageinf.str^);
  6369. tprocdef(result).symoptions:=symoptions;
  6370. if assigned(deprecatedmsg) then
  6371. tprocdef(result).deprecatedmsg:=stringdup(deprecatedmsg^);
  6372. { will have to be associated with appropriate procsym }
  6373. tprocdef(result).procsym:=nil;
  6374. { don't create aliases for bare copies, nor copy the funcretsym as
  6375. the function result parameter will be inserted again if necessary
  6376. (e.g. if the calling convention is changed) }
  6377. if not(copytyp in [pc_bareproc,pc_normal_no_hidden]) then
  6378. begin
  6379. tprocdef(result).aliasnames.concatListcopy(aliasnames);
  6380. if assigned(funcretsym) then
  6381. begin
  6382. if funcretsym.owner=parast then
  6383. begin
  6384. j:=parast.symlist.indexof(funcretsym);
  6385. if j<0 then
  6386. internalerror(2011040606);
  6387. tprocdef(result).funcretsym:=tsym(tprocdef(result).parast.symlist[j]);
  6388. end
  6389. else if funcretsym.owner=localst then
  6390. begin
  6391. { nothing to do, will be inserted for the new procdef while
  6392. parsing its body (by pdecsub.insert_funcret_local) }
  6393. end
  6394. else
  6395. internalerror(2011040605);
  6396. end;
  6397. end;
  6398. { will have to be associated with a new struct }
  6399. tprocdef(result).struct:=nil;
  6400. if assigned(implprocdefinfo) then
  6401. begin
  6402. if assigned(resultname) then
  6403. tprocdef(result).resultname:=stringdup(resultname^);
  6404. tprocdef(result).synthetickind:=synthetickind;
  6405. end;
  6406. if assigned(import_dll) then
  6407. tprocdef(result).import_dll:=stringdup(import_dll^);
  6408. if assigned(import_name) then
  6409. tprocdef(result).import_name:=stringdup(import_name^);
  6410. tprocdef(result).import_nr:=import_nr;
  6411. tprocdef(result).extnumber:=$ffff;
  6412. tprocdef(result).visibility:=visibility;
  6413. { we need a separate implementation for the copied def }
  6414. tprocdef(result).forwarddef:=true;
  6415. tprocdef(result).interfacedef:=true;
  6416. { create new paralist }
  6417. tprocdef(result).calcparas;
  6418. end;
  6419. function tprocdef.getcopy: tstoreddef;
  6420. begin
  6421. result:=getcopyas(procdef,pc_normal,'',true);
  6422. end;
  6423. procedure tprocdef.buildderef;
  6424. begin
  6425. inherited buildderef;
  6426. structderef.build(struct);
  6427. { procsym that originaly defined this definition, should be in the
  6428. same symtable }
  6429. procsymderef.build(procsym);
  6430. end;
  6431. procedure tprocdef.buildderefimpl;
  6432. begin
  6433. inherited buildderefimpl;
  6434. { Localst is not available for main/unit init }
  6435. if store_localst and assigned(localst) then
  6436. begin
  6437. tlocalsymtable(localst).buildderef;
  6438. tlocalsymtable(localst).buildderefimpl;
  6439. end;
  6440. { inline tree }
  6441. if has_inlininginfo then
  6442. begin
  6443. funcretsymderef.build(funcretsym);
  6444. inlininginfo^.code.buildderefimpl;
  6445. end;
  6446. end;
  6447. procedure tprocdef.deref;
  6448. begin
  6449. inherited deref;
  6450. struct:=tabstractrecorddef(structderef.resolve);
  6451. { procsym that originaly defined this definition, should be in the
  6452. same symtable }
  6453. procsym:=tprocsym(procsymderef.resolve);
  6454. end;
  6455. procedure tprocdef.derefimpl;
  6456. begin
  6457. { Enable has_inlininginfo when the inlininginfo
  6458. structure is available. The has_inlininginfo was disabled
  6459. after the load, since the data was invalid }
  6460. if assigned(inlininginfo) then
  6461. has_inlininginfo:=true;
  6462. { Locals }
  6463. if store_localst and assigned(localst) then
  6464. begin
  6465. tlocalsymtable(localst).deref(false);
  6466. tlocalsymtable(localst).derefimpl(false);
  6467. end;
  6468. { Inline }
  6469. if has_inlininginfo then
  6470. begin
  6471. inlininginfo^.code.derefimpl;
  6472. { funcretsym, this is always located in the localst }
  6473. funcretsym:=tsym(funcretsymderef.resolve);
  6474. end
  6475. else
  6476. begin
  6477. { safety }
  6478. { Not safe! A unit may be reresolved after its interface has been
  6479. parsed but before its implementation has been parsed, and in that
  6480. case the funcretsym is still required!
  6481. funcretsym:=nil; }
  6482. end;
  6483. end;
  6484. function tprocdef.GetTypeName : string;
  6485. begin
  6486. GetTypeName := FullProcName(false);
  6487. end;
  6488. function tprocdef.mangledname : TSymStr;
  6489. begin
  6490. {$ifdef symansistr}
  6491. if _mangledname='' then
  6492. begin
  6493. result:=defaultmangledname;
  6494. _mangledname:=result;
  6495. end
  6496. else
  6497. result:=_mangledname;
  6498. {$else symansistr}
  6499. if not assigned(_mangledname) then
  6500. begin
  6501. result:=defaultmangledname;
  6502. _mangledname:=stringdup(mangledname);
  6503. end
  6504. else
  6505. result:=_mangledname^;
  6506. {$endif symansistr}
  6507. end;
  6508. function tprocdef.defaultmangledname: TSymStr;
  6509. var
  6510. n : TSymStr;
  6511. begin
  6512. n:=procsym.name;
  6513. { make sure that the mangled names of these overloadable methods types is
  6514. unique even if it's made lowercase (e.g. for section names) }
  6515. if proctypeoption in [potype_operator,potype_class_constructor,potype_class_destructor] then
  6516. n:='$'+n;
  6517. { we need to use the symtable where the procsym is inserted,
  6518. because that is visible to the world }
  6519. defaultmangledname:=make_mangledname('',procsym.owner,n);
  6520. defaultmangledname:=defaultmangledname+mangledprocparanames(Length(defaultmangledname))
  6521. end;
  6522. function tprocdef.cplusplusmangledname : TSymStr;
  6523. function getcppparaname(p : tdef) : TSymStr;
  6524. const
  6525. {$ifdef NAMEMANGLING_GCC2}
  6526. ordtype2str : array[tordtype] of string[2] = (
  6527. '',
  6528. 'Uc','Us','Ui','Us','',
  6529. 'Sc','s','i','x','',
  6530. 'b','b','b','b','b','b',
  6531. 'c','w','x');
  6532. {$else NAMEMANGLING_GCC2}
  6533. ordtype2str : array[tordtype] of string[1] = (
  6534. 'v',
  6535. 'h','t','j','y','',
  6536. 'a','s','i','x','',
  6537. 'b','b','b','b','b',
  6538. 'b','b','b','b',
  6539. 'c','w','x','C');
  6540. floattype2str : array[tfloattype] of string[1] = (
  6541. 'f','d','e','e',
  6542. 'd','d','g');
  6543. {$endif NAMEMANGLING_GCC2}
  6544. var
  6545. s : TSymStr;
  6546. begin
  6547. case p.typ of
  6548. orddef:
  6549. begin
  6550. s:=ordtype2str[torddef(p).ordtype];
  6551. if s='C' then
  6552. s:=ordtype2str[range_to_basetype(torddef(p).low,torddef(p).high)];
  6553. end;
  6554. pointerdef:
  6555. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  6556. {$ifndef NAMEMANGLING_GCC2}
  6557. floatdef:
  6558. s:=floattype2str[tfloatdef(p).floattype];
  6559. {$endif NAMEMANGLING_GCC2}
  6560. else
  6561. internalerror(2103001);
  6562. end;
  6563. getcppparaname:=s;
  6564. end;
  6565. var
  6566. s,s2 : TSymStr;
  6567. hp : TParavarsym;
  6568. i : integer;
  6569. begin
  6570. {$ifdef NAMEMANGLING_GCC2}
  6571. { outdated gcc 2.x name mangling scheme }
  6572. s := procsym.realname;
  6573. if procsym.owner.symtabletype=ObjectSymtable then
  6574. begin
  6575. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  6576. case proctypeoption of
  6577. potype_destructor:
  6578. s:='_$_'+tostr(length(s2))+s2;
  6579. potype_constructor:
  6580. s:='___'+tostr(length(s2))+s2;
  6581. else
  6582. s:='_'+s+'__'+tostr(length(s2))+s2;
  6583. end;
  6584. end
  6585. else s:=s+'__';
  6586. s:=s+'F';
  6587. { concat modifiers }
  6588. { !!!!! }
  6589. { now we handle the parameters }
  6590. if maxparacount>0 then
  6591. begin
  6592. for i:=0 to paras.count-1 do
  6593. begin
  6594. hp:=tparavarsym(paras[i]);
  6595. { no hidden parameters form part of a C++ mangled name:
  6596. a) self is not included
  6597. b) there are no "high" or other hidden parameters
  6598. }
  6599. if vo_is_hidden_para in hp.varoptions then
  6600. continue;
  6601. s2:=getcppparaname(hp.vardef);
  6602. if hp.varspez in [vs_var,vs_out] then
  6603. s2:='R'+s2;
  6604. s:=s+s2;
  6605. end;
  6606. end
  6607. else
  6608. s:=s+'v';
  6609. cplusplusmangledname:=s;
  6610. {$else NAMEMANGLING_GCC2}
  6611. { gcc 3.x and 4.x name mangling scheme }
  6612. { see http://www.codesourcery.com/public/cxx-abi/abi.html#mangling }
  6613. if procsym.owner.symtabletype=ObjectSymtable then
  6614. begin
  6615. s:='_ZN';
  6616. s2:=tobjectdef(procsym.owner.defowner).objextname^;
  6617. s:=s+tostr(length(s2))+s2;
  6618. case proctypeoption of
  6619. potype_constructor:
  6620. s:=s+'C1';
  6621. potype_destructor:
  6622. s:=s+'D1';
  6623. else
  6624. s:=s+tostr(length(procsym.realname))+procsym.realname;
  6625. end;
  6626. s:=s+'E';
  6627. end
  6628. else
  6629. s:=procsym.realname;
  6630. { now we handle the parameters }
  6631. if maxparacount>0 then
  6632. begin
  6633. for i:=0 to paras.count-1 do
  6634. begin
  6635. hp:=tparavarsym(paras[i]);
  6636. { no hidden parameters form part of a C++ mangled name:
  6637. a) self is not included
  6638. b) there are no "high" or other hidden parameters
  6639. }
  6640. if vo_is_hidden_para in hp.varoptions then
  6641. continue;
  6642. s2:=getcppparaname(hp.vardef);
  6643. if hp.varspez in [vs_var,vs_out] then
  6644. s2:='R'+s2;
  6645. s:=s+s2;
  6646. end;
  6647. end
  6648. else
  6649. s:=s+'v';
  6650. cplusplusmangledname:=s;
  6651. {$endif NAMEMANGLING_GCC2}
  6652. end;
  6653. function tprocdef.objcmangledname : TSymStr;
  6654. var
  6655. manglednamelen: longint;
  6656. iscatmethod : boolean;
  6657. begin
  6658. if not (po_msgstr in procoptions) then
  6659. internalerror(2009030901);
  6660. { we may very well need longer strings to handle these... }
  6661. manglednamelen:=length(tobjectdef(procsym.owner.defowner).objextname^)+
  6662. length('+"[ ]"')+length(messageinf.str^);
  6663. iscatmethod:=oo_is_classhelper in tobjectdef(procsym.owner.defowner).objectoptions;
  6664. if (iscatmethod) then
  6665. inc(manglednamelen,length(tobjectdef(procsym.owner.defowner).childof.objextname^)+length('()'));
  6666. if manglednamelen>255 then
  6667. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  6668. if not(po_classmethod in procoptions) then
  6669. result:='"-['
  6670. else
  6671. result:='"+[';
  6672. { quotes are necessary because the +/- otherwise confuse the assembler
  6673. into expecting a number
  6674. }
  6675. if iscatmethod then
  6676. result:=result+tobjectdef(procsym.owner.defowner).childof.objextname^+'(';
  6677. result:=result+tobjectdef(procsym.owner.defowner).objextname^;
  6678. if iscatmethod then
  6679. result:=result+')';
  6680. result:=result+' '+messageinf.str^+']"';
  6681. end;
  6682. procedure tprocdef.setmangledname(const s : TSymStr);
  6683. begin
  6684. { This is not allowed anymore, the forward declaration
  6685. already needs to create the correct mangledname, no changes
  6686. afterwards are allowed (PFV) }
  6687. { Exception: interface definitions in mode macpas, since in that }
  6688. { case no reference to the old name can exist yet (JM) }
  6689. {$ifdef symansistr}
  6690. if _mangledname<>'' then
  6691. if ((m_mac in current_settings.modeswitches) and
  6692. (interfacedef)) then
  6693. _mangledname:=''
  6694. else
  6695. internalerror(200411171);
  6696. {$else symansistr}
  6697. if assigned(_mangledname) then
  6698. if ((m_mac in current_settings.modeswitches) and
  6699. (interfacedef)) then
  6700. stringdispose(_mangledname)
  6701. else
  6702. internalerror(200411171);
  6703. {$endif symansistr}
  6704. {$ifdef jvm}
  6705. { this routine can be called for compilerproces. can't set mangled
  6706. name since it must be calculated, but it uses import_name when set
  6707. -> set that one }
  6708. import_name:=stringdup(s);
  6709. include(procoptions,po_has_importname);
  6710. include(procoptions,po_has_mangledname);
  6711. {$else}
  6712. {$ifdef symansistr}
  6713. _mangledname:=s;
  6714. {$else symansistr}
  6715. _mangledname:=stringdup(s);
  6716. {$endif symansistr}
  6717. {$endif jvm}
  6718. include(procoptions,po_has_mangledname);
  6719. end;
  6720. function tprocdef.needsglobalasmsym: boolean;
  6721. begin
  6722. result:=
  6723. (cs_profile in current_settings.moduleswitches) or
  6724. { smart linking using a library requires to promote
  6725. all non-nested procedures to AB_GLOBAL
  6726. otherwise you get undefined symbol error at linking
  6727. for msdos target with -CX option for instance }
  6728. (create_smartlink_library and not is_nested_pd(self)) or
  6729. (po_global in procoptions);
  6730. end;
  6731. procedure tprocdef.setcompilerprocname;
  6732. begin
  6733. procsym.realname:='$'+lower(procsym.name);
  6734. end;
  6735. {***************************************************************************
  6736. TPROCVARDEF
  6737. ***************************************************************************}
  6738. constructor tprocvardef.create(level:byte;doregister:boolean);
  6739. begin
  6740. inherited create(procvardef,level,doregister);
  6741. end;
  6742. class function tprocvardef.getreusableprocaddr(def: tabstractprocdef; copytyp: tcacheableproccopytyp): tprocvardef;
  6743. var
  6744. res: PHashSetItem;
  6745. oldsymtablestack: tsymtablestack;
  6746. key: packed record
  6747. def: tabstractprocdef;
  6748. copytyp: tcacheableproccopytyp;
  6749. end;
  6750. begin
  6751. if not assigned(current_module) then
  6752. internalerror(2011081302);
  6753. key.def:=def;
  6754. key.copytyp:=copytyp;
  6755. res:=current_module.procaddrdefs.FindOrAdd(@key,sizeof(key));
  6756. if not assigned(res^.Data) then
  6757. begin
  6758. { since these pointerdefs can be reused anywhere in the current
  6759. unit, add them to the global/staticsymtable (or local symtable
  6760. if they're a local def, because otherwise they'll be saved
  6761. to the ppu referencing a local symtable entry that doesn't
  6762. exist in the ppu) }
  6763. oldsymtablestack:=symtablestack;
  6764. { do not simply push/pop current_module.localsymtable, because
  6765. that can have side-effects (e.g., it removes helpers) }
  6766. symtablestack:=nil;
  6767. result:=tprocvardef(def.getcopyas(procvardef,copytyp,'',true));
  6768. setup_reusable_def(def,result,res,oldsymtablestack);
  6769. { res^.Data may still be nil -> don't overwrite result }
  6770. exit;
  6771. end;
  6772. result:=tprocvardef(res^.Data);
  6773. end;
  6774. class function tprocvardef.getreusableprocaddr_no_free(def: tabstractprocdef): tprocvardef;
  6775. begin
  6776. result:=getreusableprocaddr(def,pc_address_only);
  6777. if not result.is_registered then
  6778. include(result.defoptions,df_not_registered_no_free);
  6779. end;
  6780. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  6781. begin
  6782. inherited ppuload(procvardef,ppufile);
  6783. { load para symtable }
  6784. parast:=tparasymtable.create(self,ppufile.getbyte);
  6785. ppuload_platform(ppufile);
  6786. tparasymtable(parast).ppuload(ppufile);
  6787. end;
  6788. function tprocvardef.getcopy : tstoreddef;
  6789. var
  6790. i : tcallercallee;
  6791. j : longint;
  6792. begin
  6793. result:=cprocvardef.create(parast.symtablelevel,true);
  6794. tprocvardef(result).returndef:=returndef;
  6795. tprocvardef(result).returndefderef:=returndefderef;
  6796. tprocvardef(result).parast:=parast.getcopy;
  6797. tprocvardef(result).savesize:=savesize;
  6798. { create paralist copy }
  6799. calcparas;
  6800. tprocvardef(result).paras:=tparalist.create(false);
  6801. tprocvardef(result).paras.count:=paras.count;
  6802. for j:=0 to paras.count-1 do
  6803. tprocvardef(result).paras[j]:=paras[j];
  6804. tprocvardef(result).proctypeoption:=proctypeoption;
  6805. tprocvardef(result).proccalloption:=proccalloption;
  6806. tprocvardef(result).procoptions:=procoptions;
  6807. tprocvardef(result).callerargareasize:=callerargareasize;
  6808. tprocvardef(result).calleeargareasize:=calleeargareasize;
  6809. tprocvardef(result).maxparacount:=maxparacount;
  6810. tprocvardef(result).minparacount:=minparacount;
  6811. for i:=low(funcretloc) to high(funcretloc) do
  6812. tprocvardef(result).funcretloc[i]:=funcretloc[i].getcopy;
  6813. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  6814. {$ifdef m68k}
  6815. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  6816. {$endif}
  6817. end;
  6818. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  6819. begin
  6820. inherited ppuwrite(ppufile);
  6821. { Save the para symtable level (necessary to distinguish nested
  6822. procvars) }
  6823. ppufile.putbyte(parast.symtablelevel);
  6824. { Write this entry }
  6825. writeentry(ppufile,ibprocvardef);
  6826. { Save the para symtable, this is taken from the interface }
  6827. tparasymtable(parast).ppuwrite(ppufile);
  6828. end;
  6829. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  6830. begin
  6831. case t of
  6832. gs_para :
  6833. GetSymtable:=parast;
  6834. else
  6835. GetSymtable:=nil;
  6836. end;
  6837. end;
  6838. function tprocvardef.size : asizeint;
  6839. begin
  6840. { we return false for is_addressonly for a block (because it's not a
  6841. simple pointer to a function), but they are handled as implicit
  6842. pointers to a datastructure that contains everything ->
  6843. voidpointertype.size instead of voidcodepointertype.size }
  6844. if po_is_block in procoptions then
  6845. size:=voidpointertype.size
  6846. else if not is_addressonly then
  6847. begin
  6848. if is_nested_pd(self) then
  6849. size:=voidcodepointertype.size+parentfpvoidpointertype.size
  6850. else
  6851. size:=voidcodepointertype.size+voidpointertype.size;
  6852. end
  6853. else
  6854. size:=voidcodepointertype.size;
  6855. end;
  6856. function tprocvardef.is_methodpointer:boolean;
  6857. begin
  6858. result:=(po_methodpointer in procoptions);
  6859. end;
  6860. function tprocvardef.is_addressonly:boolean;
  6861. begin
  6862. result:=((not(po_methodpointer in procoptions) or (po_staticmethod in procoptions)) and
  6863. not(po_is_block in procoptions) and
  6864. not is_nested_pd(self)) or
  6865. (po_addressonly in procoptions);
  6866. end;
  6867. function tprocvardef.getmangledparaname:TSymStr;
  6868. begin
  6869. if not(po_methodpointer in procoptions) then
  6870. if not is_nested_pd(self) then
  6871. result:='procvar'
  6872. else
  6873. { we need the manglednames here, because nestedprocvars can be anonymous, e.g.
  6874. having not a type name or not an unique one, see webtbs/tw27515.pp
  6875. Further, use $_ ... _$ delimiters to avoid ambiguous names, see webtbs/tw27515.pp }
  6876. result:='$_nestedprovar'+mangledprocparanames(0)+'_$'
  6877. else
  6878. result:='procvarofobj'
  6879. end;
  6880. function tprocvardef.getcopyas(newtyp: tdeftyp; copytyp: tproccopytyp; const paraprefix: string;doregister:boolean): tstoreddef;
  6881. begin
  6882. result:=inherited;
  6883. tabstractprocdef(result).calcparas;
  6884. end;
  6885. function tprocvardef.is_publishable : tpublishproperty;
  6886. begin
  6887. if po_methodpointer in procoptions then
  6888. is_publishable:=pp_publish
  6889. else
  6890. is_publishable:=pp_error;
  6891. end;
  6892. function tprocvardef.GetTypeName : string;
  6893. var
  6894. s: string;
  6895. pno : tprocnameoptions;
  6896. begin
  6897. {$ifdef EXTDEBUG}
  6898. pno:=[pno_showhidden];
  6899. {$else EXTDEBUG}
  6900. pno:=[];
  6901. {$endif EXTDEBUG}
  6902. s:='<';
  6903. if po_is_block in procoptions then
  6904. s := s+'reference to'
  6905. else if po_classmethod in procoptions then
  6906. s := s+'class method type of'
  6907. else
  6908. if po_addressonly in procoptions then
  6909. s := s+'address of'
  6910. else
  6911. s := s+'procedure variable type of';
  6912. if assigned(returndef) and
  6913. (returndef<>voidtype) then
  6914. s:=s+' function'+typename_paras(pno)+':'+returndef.GetTypeName
  6915. else
  6916. s:=s+' procedure'+typename_paras(pno);
  6917. if po_methodpointer in procoptions then
  6918. s := s+' of object';
  6919. if is_nested_pd(self) then
  6920. s := s+' is nested';
  6921. if po_far in procoptions then
  6922. s := s+';far';
  6923. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  6924. end;
  6925. {***************************************************************************
  6926. TOBJECTDEF
  6927. ***************************************************************************}
  6928. constructor tobjectdef.create(ot:tobjecttyp;const n:string;c:tobjectdef;doregister:boolean);
  6929. begin
  6930. inherited create(n,objectdef,doregister);
  6931. fcurrent_dispid:=0;
  6932. objecttype:=ot;
  6933. childof:=nil;
  6934. childofderef.reset;
  6935. vmt_fieldderef.reset;
  6936. hiddenclassdefref.reset;
  6937. extendeddefderef.reset;
  6938. cloneddefderef.reset;
  6939. if objecttype=odt_helper then
  6940. owner.includeoption(sto_has_helper);
  6941. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords,
  6942. current_settings.alignment.recordalignmin);
  6943. { create space for vmt !! }
  6944. vmtentries:=TFPList.Create;
  6945. set_parent(c);
  6946. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  6947. prepareguid;
  6948. { setup implemented interfaces }
  6949. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  6950. ImplementedInterfaces:=TFPObjectList.Create(true)
  6951. else
  6952. ImplementedInterfaces:=nil;
  6953. writing_class_record_dbginfo:=false;
  6954. end;
  6955. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  6956. var
  6957. i,
  6958. implintfcount : longint;
  6959. d, getterd : tderef;
  6960. ImplIntf : TImplementedInterface;
  6961. vmtentry : pvmtentry;
  6962. begin
  6963. inherited ppuload(objectdef,ppufile);
  6964. objecttype:=tobjecttyp(ppufile.getbyte);
  6965. helpertype:=thelpertype(ppufile.getbyte);
  6966. objextname:=ppufile.getpshortstring;
  6967. { only used for external Objective-C classes/protocols }
  6968. if (objextname^='') then
  6969. stringdispose(objextname);
  6970. symtable:=tObjectSymtable.create(self,objrealname^,0,0);
  6971. tObjectSymtable(symtable).datasize:=ppufile.getasizeint;
  6972. tObjectSymtable(symtable).paddingsize:=ppufile.getword;
  6973. tObjectSymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  6974. tObjectSymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  6975. tObjectSymtable(symtable).recordalignmin:=shortint(ppufile.getbyte);
  6976. ppufile.getderef(vmt_fieldderef);
  6977. ppufile.getderef(childofderef);
  6978. { load guid }
  6979. iidstr:=nil;
  6980. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  6981. begin
  6982. new(iidguid);
  6983. ppufile.getguid(iidguid^);
  6984. iidstr:=ppufile.getpshortstring;
  6985. end;
  6986. abstractcnt:=ppufile.getlongint;
  6987. if objecttype=odt_helper then
  6988. ppufile.getderef(extendeddefderef);
  6989. vmtentries:=TFPList.Create;
  6990. vmtentries.count:=ppufile.getlongint;
  6991. for i:=0 to vmtentries.count-1 do
  6992. begin
  6993. ppufile.getderef(d);
  6994. new(vmtentry);
  6995. vmtentry^.procdef:=nil;
  6996. vmtentry^.procdefderef:=d;
  6997. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  6998. vmtentries[i]:=vmtentry;
  6999. end;
  7000. { load implemented interfaces }
  7001. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol,odt_objccategory,odt_javaclass,odt_interfacejava] then
  7002. begin
  7003. ImplementedInterfaces:=TFPObjectList.Create(true);
  7004. implintfcount:=ppufile.getlongint;
  7005. for i:=0 to implintfcount-1 do
  7006. begin
  7007. ppufile.getderef(d);
  7008. ppufile.getderef(getterd);
  7009. ImplIntf:=TImplementedInterface.Create_deref(d,getterd);
  7010. ImplIntf.IOffset:=ppufile.getlongint;
  7011. byte(ImplIntf.IType):=ppufile.getbyte;
  7012. ImplementedInterfaces.Add(ImplIntf);
  7013. end;
  7014. end
  7015. else
  7016. ImplementedInterfaces:=nil;
  7017. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7018. ppufile.getderef(hiddenclassdefref);
  7019. if df_copied_def in defoptions then
  7020. begin
  7021. ppufile.getderef(cloneddefderef);
  7022. ppuload_platform(ppufile);
  7023. end
  7024. else
  7025. begin
  7026. ppuload_platform(ppufile);
  7027. tObjectSymtable(symtable).ppuload(ppufile);
  7028. end;
  7029. { handles the predefined class tobject }
  7030. { the last TOBJECT which is loaded gets }
  7031. { it ! }
  7032. { but do this only from a unit that's }
  7033. { marked as system unit to avoid some }
  7034. { equally named user's type to override }
  7035. { the internal types! }
  7036. if mf_system_unit in current_module.moduleflags then
  7037. begin
  7038. if (childof=nil) and
  7039. (objecttype in [odt_class,odt_javaclass]) and
  7040. (objname^='TOBJECT') then
  7041. class_tobject:=self;
  7042. if (childof=nil) and
  7043. (objecttype=odt_interfacecom) then
  7044. if (objname^='IUNKNOWN') then
  7045. interface_iunknown:=self
  7046. else
  7047. if (objname^='IDISPATCH') then
  7048. interface_idispatch:=self;
  7049. if (objecttype=odt_javaclass) and
  7050. not(oo_is_formal in objectoptions) then
  7051. begin
  7052. if (objname^='JLOBJECT') then
  7053. java_jlobject:=self
  7054. else if (objname^='JLTHROWABLE') then
  7055. java_jlthrowable:=self
  7056. else if (objname^='FPCBASERECORDTYPE') then
  7057. java_fpcbaserecordtype:=self
  7058. else if (objname^='JLSTRING') then
  7059. java_jlstring:=self
  7060. else if (objname^='ANSISTRINGCLASS') then
  7061. java_ansistring:=self
  7062. else if (objname^='SHORTSTRINGCLASS') then
  7063. java_shortstring:=self
  7064. else if (objname^='JLENUM') then
  7065. java_jlenum:=self
  7066. else if (objname^='JUENUMSET') then
  7067. java_juenumset:=self
  7068. else if (objname^='FPCBITSET') then
  7069. java_jubitset:=self
  7070. else if (objname^='FPCBASEPROCVARTYPE') then
  7071. java_procvarbase:=self;
  7072. end;
  7073. end;
  7074. if (childof=nil) and
  7075. (objecttype=odt_objcclass) and
  7076. (objname^='PROTOCOL') then
  7077. objc_protocoltype:=self;
  7078. writing_class_record_dbginfo:=false;
  7079. end;
  7080. destructor tobjectdef.destroy;
  7081. begin
  7082. if assigned(symtable) then
  7083. begin
  7084. symtable.free;
  7085. symtable:=nil;
  7086. end;
  7087. stringdispose(objextname);
  7088. stringdispose(iidstr);
  7089. if assigned(ImplementedInterfaces) then
  7090. begin
  7091. ImplementedInterfaces.free;
  7092. ImplementedInterfaces:=nil;
  7093. end;
  7094. if assigned(iidguid) then
  7095. begin
  7096. dispose(iidguid);
  7097. iidguid:=nil;
  7098. end;
  7099. TFPList.FreeAndNilDisposing(vmtentries,TypeInfo(tvmtentry));
  7100. if assigned(vmcallstaticinfo) then
  7101. begin
  7102. freemem(vmcallstaticinfo);
  7103. vmcallstaticinfo:=nil;
  7104. end;
  7105. inherited destroy;
  7106. end;
  7107. function tobjectdef.getcopy : tstoreddef;
  7108. var
  7109. i : longint;
  7110. begin
  7111. result:=cobjectdef.create(objecttype,objrealname^,childof,true);
  7112. { the constructor allocates a symtable which we release to avoid memory leaks }
  7113. tobjectdef(result).symtable.free;
  7114. tobjectdef(result).symtable:=symtable.getcopy;
  7115. if assigned(objextname) then
  7116. tobjectdef(result).objextname:=stringdup(objextname^);
  7117. if assigned(import_lib) then
  7118. tobjectdef(result).import_lib:=stringdup(import_lib^);
  7119. tobjectdef(result).objectoptions:=objectoptions;
  7120. include(tobjectdef(result).defoptions,df_copied_def);
  7121. tobjectdef(result).extendeddef:=extendeddef;
  7122. if assigned(tcinitcode) then
  7123. tobjectdef(result).tcinitcode:=tcinitcode.getcopy;
  7124. tobjectdef(result).vmt_field:=vmt_field;
  7125. if assigned(iidguid) then
  7126. begin
  7127. new(tobjectdef(result).iidguid);
  7128. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  7129. end;
  7130. if assigned(iidstr) then
  7131. tobjectdef(result).iidstr:=stringdup(iidstr^);
  7132. tobjectdef(result).abstractcnt:=abstractcnt;
  7133. if assigned(ImplementedInterfaces) then
  7134. begin
  7135. tobjectdef(result).ImplementedInterfaces.capacity:=tobjectdef(result).ImplementedInterfaces.count+ImplementedInterfaces.count;
  7136. for i:=0 to ImplementedInterfaces.count-1 do
  7137. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  7138. end;
  7139. tobjectdef(result).copyvmtentries(self);
  7140. end;
  7141. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  7142. var
  7143. i : longint;
  7144. vmtentry : pvmtentry;
  7145. ImplIntf : TImplementedInterface;
  7146. old_do_indirect_crc: boolean;
  7147. begin
  7148. { if class1 in unit A changes, and class2 in unit B inherits from it
  7149. (so unit B uses unit A), then unit B with class2 will be recompiled.
  7150. However, if there is also a class3 in unit C that only depends on
  7151. unit B, then unit C will not be recompiled because nothing changed
  7152. to the interface of unit B. Nevertheless, unit C can indirectly
  7153. depend on unit A via derefs, and these must be updated -> the
  7154. indirect crc keeps track of such changes. }
  7155. old_do_indirect_crc:=ppufile.do_indirect_crc;
  7156. ppufile.do_indirect_crc:=true;
  7157. inherited ppuwrite(ppufile);
  7158. ppufile.putbyte(byte(objecttype));
  7159. ppufile.putbyte(byte(helpertype));
  7160. if assigned(objextname) then
  7161. ppufile.putstring(objextname^)
  7162. else
  7163. ppufile.putstring('');
  7164. ppufile.putasizeint(tObjectSymtable(symtable).datasize);
  7165. ppufile.putword(tObjectSymtable(symtable).paddingsize);
  7166. ppufile.putbyte(byte(tObjectSymtable(symtable).fieldalignment));
  7167. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignment));
  7168. ppufile.putbyte(byte(tObjectSymtable(symtable).recordalignmin));
  7169. ppufile.putderef(vmt_fieldderef);
  7170. ppufile.putderef(childofderef);
  7171. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  7172. begin
  7173. ppufile.putguid(iidguid^);
  7174. ppufile.putstring(iidstr^);
  7175. end;
  7176. ppufile.putlongint(abstractcnt);
  7177. if objecttype=odt_helper then
  7178. ppufile.putderef(extendeddefderef);
  7179. ppufile.putlongint(vmtentries.count);
  7180. for i:=0 to vmtentries.count-1 do
  7181. begin
  7182. vmtentry:=pvmtentry(vmtentries[i]);
  7183. ppufile.putderef(vmtentry^.procdefderef);
  7184. ppufile.putbyte(byte(vmtentry^.visibility));
  7185. end;
  7186. if assigned(ImplementedInterfaces) then
  7187. begin
  7188. ppufile.putlongint(ImplementedInterfaces.Count);
  7189. for i:=0 to ImplementedInterfaces.Count-1 do
  7190. begin
  7191. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  7192. ppufile.putderef(ImplIntf.intfdefderef);
  7193. ppufile.putderef(ImplIntf.ImplementsGetterDeref);
  7194. ppufile.putlongint(ImplIntf.Ioffset);
  7195. ppufile.putbyte(byte(ImplIntf.IType));
  7196. end;
  7197. end;
  7198. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7199. ppufile.putderef(hiddenclassdefref);
  7200. if df_copied_def in defoptions then
  7201. ppufile.putderef(cloneddefderef);
  7202. writeentry(ppufile,ibobjectdef);
  7203. if not(df_copied_def in defoptions) then
  7204. tObjectSymtable(symtable).ppuwrite(ppufile);
  7205. ppufile.do_indirect_crc:=old_do_indirect_crc;
  7206. end;
  7207. function tobjectdef.GetTypeName:string;
  7208. begin
  7209. { in this case we will go in endless recursion, because then }
  7210. { there is no tsym associated yet with the def. It can occur }
  7211. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  7212. { instead of the actual type name }
  7213. if not assigned(typesym) then
  7214. result:='<Currently Parsed Class>'
  7215. else
  7216. result:=typesymbolprettyname;
  7217. end;
  7218. procedure tobjectdef.buildderef;
  7219. var
  7220. i : longint;
  7221. vmtentry : pvmtentry;
  7222. begin
  7223. inherited buildderef;
  7224. vmt_fieldderef.build(vmt_field);
  7225. childofderef.build(childof);
  7226. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7227. hiddenclassdefref.build(hiddenclassdef);
  7228. if df_copied_def in defoptions then
  7229. cloneddefderef.build(symtable.defowner)
  7230. else
  7231. tstoredsymtable(symtable).buildderef;
  7232. if objecttype=odt_helper then
  7233. extendeddefderef.build(extendeddef);
  7234. for i:=0 to vmtentries.count-1 do
  7235. begin
  7236. vmtentry:=pvmtentry(vmtentries[i]);
  7237. vmtentry^.procdefderef.build(vmtentry^.procdef);
  7238. end;
  7239. if assigned(ImplementedInterfaces) then
  7240. begin
  7241. for i:=0 to ImplementedInterfaces.count-1 do
  7242. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  7243. end;
  7244. end;
  7245. procedure tobjectdef.deref;
  7246. var
  7247. i : longint;
  7248. vmtentry : pvmtentry;
  7249. begin
  7250. inherited deref;
  7251. vmt_field:=tsym(vmt_fieldderef.resolve);
  7252. childof:=tobjectdef(childofderef.resolve);
  7253. if (target_cpu=tsystemcpu.cpu_wasm32) and (objecttype in objecttypes_with_thunk) then
  7254. hiddenclassdef:=tobjectdef(hiddenclassdefref.resolve);
  7255. if df_copied_def in defoptions then
  7256. begin
  7257. cloneddef:=tobjectdef(cloneddefderef.resolve);
  7258. symtable:=cloneddef.symtable.getcopy;
  7259. end
  7260. else
  7261. tstoredsymtable(symtable).deref(false);
  7262. if objecttype=odt_helper then
  7263. extendeddef:=tdef(extendeddefderef.resolve);
  7264. for i:=0 to vmtentries.count-1 do
  7265. begin
  7266. vmtentry:=pvmtentry(vmtentries[i]);
  7267. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  7268. end;
  7269. if assigned(ImplementedInterfaces) then
  7270. begin
  7271. for i:=0 to ImplementedInterfaces.count-1 do
  7272. TImplementedInterface(ImplementedInterfaces[i]).deref;
  7273. end;
  7274. end;
  7275. procedure create_class_helper_for_procdef(def: tobject; arg: pointer);
  7276. var
  7277. pd: tprocdef absolute def;
  7278. st: tsymtable;
  7279. psym: tsym;
  7280. nname: TIDString;
  7281. begin
  7282. if (tdef(def).typ<>procdef) then
  7283. exit;
  7284. { pd.owner = objcclass symtable -> defowner = objcclassdef ->
  7285. owner = symtable in which objcclassdef is defined
  7286. }
  7287. st:=pd.owner.defowner.owner;
  7288. nname:=class_helper_prefix+tprocsym(pd.procsym).name;
  7289. { check for an existing procsym with our special name }
  7290. psym:=tsym(st.find(nname));
  7291. if not assigned(psym) then
  7292. begin
  7293. psym:=cprocsym.create(nname);
  7294. { avoid warning about this symbol being unused }
  7295. psym.IncRefCount;
  7296. { don't check for duplicates:
  7297. a) we checked above
  7298. b) in case we are in the implementation section of a unit, this
  7299. will also check for this symbol in the interface section
  7300. (since you normally cannot have symbols with the same name
  7301. both interface and implementation), and it's possible to
  7302. have class helpers for the same class in the interface and
  7303. in the implementation, and they cannot be merged since only
  7304. the once in the interface must be saved to the ppu/visible
  7305. from other units }
  7306. st.insertsym(psym,false);
  7307. end
  7308. else if (psym.typ<>procsym) then
  7309. internalerror(2009111501);
  7310. { add ourselves to this special procsym }
  7311. tprocsym(psym).procdeflist.add(def);
  7312. end;
  7313. procedure tobjectdef.derefimpl;
  7314. begin
  7315. inherited derefimpl;
  7316. { the procdefs are not owned by the class helper procsyms, so they
  7317. are not stored/restored either -> re-add them here }
  7318. if (objecttype in [odt_objcclass,odt_objcprotocol]) or
  7319. (oo_is_classhelper in objectoptions) then
  7320. symtable.DefList.ForEachCall(@create_class_helper_for_procdef,nil);
  7321. end;
  7322. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  7323. var
  7324. i : longint;
  7325. vmtentry : pvmtentry;
  7326. begin
  7327. if vmtentries.count<>0 then
  7328. internalerror(2019081401);
  7329. vmtentries.count:=objdef.vmtentries.count;
  7330. for i:=0 to objdef.vmtentries.count-1 do
  7331. begin
  7332. new(vmtentry);
  7333. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  7334. vmtentries[i]:=vmtentry;
  7335. end;
  7336. end;
  7337. function tobjectdef.getparentdef:tdef;
  7338. begin
  7339. { TODO: Remove getparentdef hack}
  7340. { With 2 forward declared classes with the child class before the
  7341. parent class the child class is written earlier to the ppu. Leaving it
  7342. possible to have a reference to the parent class for property overriding,
  7343. but the parent class still has the childof not resolved yet (PFV) }
  7344. if childof=nil then
  7345. childof:=tobjectdef(childofderef.resolve);
  7346. result:=childof;
  7347. end;
  7348. procedure tobjectdef.prepareguid;
  7349. begin
  7350. { set up guid }
  7351. if not assigned(iidguid) then
  7352. begin
  7353. new(iidguid);
  7354. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  7355. end;
  7356. { setup iidstring }
  7357. if not assigned(iidstr) then
  7358. iidstr:=stringdup(''); { default is empty string }
  7359. end;
  7360. procedure tobjectdef.set_parent( c : tobjectdef);
  7361. begin
  7362. if assigned(childof) then
  7363. exit;
  7364. childof:=c;
  7365. if not assigned(c) then
  7366. exit;
  7367. { inherit options and status }
  7368. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  7369. { check if parent is a generic parameter }
  7370. if sp_generic_para in c.typesym.symoptions then
  7371. objectoptions:=objectoptions+[oo_inherits_not_specialized];
  7372. { initially has the same number of abstract methods as the parent }
  7373. abstractcnt:=c.abstractcnt;
  7374. { add the data of the anchestor class/object }
  7375. if (objecttype in [odt_class,odt_object,odt_objcclass,odt_javaclass]) then
  7376. begin
  7377. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  7378. { inherit recordalignment }
  7379. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  7380. { if both the parent and this record use C-alignment, also inherit
  7381. the current field alignment }
  7382. if (tObjectSymtable(c.symtable).usefieldalignment=C_alignment) and
  7383. (tObjectSymtable(symtable).usefieldalignment=C_alignment) then
  7384. tObjectSymtable(symtable).fieldalignment:=tObjectSymtable(c.symtable).fieldalignment;
  7385. { the padding is not inherited for Objective-C classes (maybe not
  7386. for cppclass either?) }
  7387. if objecttype=odt_objcclass then
  7388. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-tObjectSymtable(c.symtable).paddingsize;
  7389. if (oo_has_vmt in objectoptions) and
  7390. (oo_has_vmt in c.objectoptions) then
  7391. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  7392. { if parent has a vmt field then the offset is the same for the child PM }
  7393. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  7394. begin
  7395. vmt_field:=c.vmt_field;
  7396. include(objectoptions,oo_has_vmt);
  7397. end;
  7398. end;
  7399. end;
  7400. procedure tobjectdef.insertvmt;
  7401. begin
  7402. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  7403. exit;
  7404. if (oo_has_vmt in objectoptions) then
  7405. internalerror(2020100816)
  7406. else
  7407. begin
  7408. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  7409. tObjectSymtable(symtable).fieldalignment);
  7410. if (tf_requires_proper_alignment in target_info.flags) then
  7411. begin
  7412. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  7413. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  7414. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  7415. end;
  7416. vmt_field:=cfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  7417. hidesym(vmt_field);
  7418. tObjectSymtable(symtable).insertsym(vmt_field);
  7419. tObjectSymtable(symtable).addfield(tfieldvarsym(vmt_field),vis_hidden);
  7420. include(objectoptions,oo_has_vmt);
  7421. end;
  7422. end;
  7423. function tobjectdef.vmt_offset: asizeint;
  7424. begin
  7425. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava] then
  7426. result:=0
  7427. else if (tObjectSymtable(symtable).usefieldalignment<>bit_alignment) then
  7428. result:=tfieldvarsym(vmt_field).fieldoffset
  7429. else
  7430. result:=tfieldvarsym(vmt_field).fieldoffset div 8;
  7431. end;
  7432. procedure tobjectdef.check_forwards;
  7433. begin
  7434. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol,odt_interfacejava]) then
  7435. inherited;
  7436. if (oo_is_forward in objectoptions) then
  7437. begin
  7438. { ok, in future, the forward can be resolved }
  7439. Message1(sym_e_class_forward_not_resolved,objrealname^);
  7440. exclude(objectoptions,oo_is_forward);
  7441. end;
  7442. end;
  7443. function tobjectdef.find_destructor: tprocdef;
  7444. var
  7445. objdef: tobjectdef;
  7446. begin
  7447. objdef:=self;
  7448. while assigned(objdef) do
  7449. begin
  7450. result:=objdef.find_procdef_bytype(potype_destructor);
  7451. if assigned(result) then
  7452. exit;
  7453. objdef:=objdef.childof;
  7454. end;
  7455. result:=nil;
  7456. end;
  7457. function tobjectdef.implements_any_interfaces: boolean;
  7458. begin
  7459. result := (ImplementedInterfaces.Count > 0) or
  7460. (assigned(childof) and childof.implements_any_interfaces);
  7461. end;
  7462. function tobjectdef.register_implemented_interface(intfdef:tobjectdef;useguid:boolean):timplementedinterface;
  7463. begin
  7464. { allocate the GUID only if the class implements at least one interface }
  7465. if useguid then
  7466. prepareguid;
  7467. result:=timplementedinterface.create(intfdef);
  7468. ImplementedInterfaces.Add(result);
  7469. end;
  7470. function tobjectdef.size : asizeint;
  7471. begin
  7472. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  7473. result:=voidpointertype.size
  7474. else
  7475. result:=tObjectSymtable(symtable).datasize;
  7476. end;
  7477. function tobjectdef.alignment:shortint;
  7478. begin
  7479. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava] then
  7480. alignment:=voidpointertype.alignment
  7481. else
  7482. alignment:=tObjectSymtable(symtable).recordalignment;
  7483. end;
  7484. function tobjectdef.vmtmethodoffset(index:longint):longint;
  7485. begin
  7486. { for offset of methods for classes, see rtl/inc/objpash.inc }
  7487. case objecttype of
  7488. odt_class:
  7489. { the +2*sizeof(pint) is size and -size }
  7490. vmtmethodoffset:=index*voidcodepointertype.size+10*voidpointertype.size+2*sizeof(pint);
  7491. odt_helper,
  7492. odt_objcclass,
  7493. odt_objcprotocol:
  7494. vmtmethodoffset:=0;
  7495. odt_interfacecom,odt_interfacecorba,odt_dispinterface:
  7496. vmtmethodoffset:=index*voidcodepointertype.size;
  7497. odt_javaclass,
  7498. odt_interfacejava:
  7499. { invalid }
  7500. vmtmethodoffset:=-1;
  7501. else
  7502. { the +2*sizeof(pint) is size and -size }
  7503. {$ifdef WITHDMT}
  7504. vmtmethodoffset:=index*voidcodepointertype.size+2*voidpointertype.size+2*sizeof(pint);
  7505. {$else WITHDMT}
  7506. vmtmethodoffset:=index*voidcodepointertype.size+1*voidpointertype.size+2*sizeof(pint);
  7507. {$endif WITHDMT}
  7508. end;
  7509. end;
  7510. function tobjectdef.vmt_mangledname : TSymStr;
  7511. begin
  7512. if not(oo_has_vmt in objectoptions) then
  7513. Message1(parser_n_object_has_no_vmt,objrealname^);
  7514. if not is_unique_objpasdef then
  7515. vmt_mangledname:=make_mangledname('VMT',owner,objname^)
  7516. else
  7517. vmt_mangledname:=childof.vmt_mangledname;
  7518. end;
  7519. function tobjectdef.vmt_def: trecorddef;
  7520. var
  7521. where: tsymtable;
  7522. vmttypesym: tsymentry;
  7523. begin
  7524. if not is_unique_objpasdef then
  7525. begin
  7526. vmttypesym:=symtable.Find('vmtdef');
  7527. if not assigned(vmttypesym) or
  7528. (vmttypesym.typ<>symconst.typesym) or
  7529. (ttypesym(vmttypesym).typedef.typ<>recorddef) then
  7530. internalerror(2015052501);
  7531. result:=trecorddef(ttypesym(vmttypesym).typedef);
  7532. end
  7533. else
  7534. result:=childof.vmt_def;
  7535. end;
  7536. function tobjectdef.needs_inittable : boolean;
  7537. var
  7538. hp : tobjectdef;
  7539. begin
  7540. case objecttype of
  7541. odt_helper,
  7542. odt_class :
  7543. needs_inittable:=false;
  7544. odt_dispinterface,
  7545. odt_interfacecom:
  7546. needs_inittable:=true;
  7547. odt_interfacecorba:
  7548. begin
  7549. hp:=childof;
  7550. while assigned(hp) do
  7551. begin
  7552. if hp=interface_iunknown then
  7553. begin
  7554. needs_inittable:=true;
  7555. exit;
  7556. end;
  7557. hp:=hp.childof;
  7558. end;
  7559. needs_inittable:=false;
  7560. end;
  7561. odt_object:
  7562. needs_inittable:=
  7563. tObjectSymtable(symtable).needs_init_final or
  7564. (assigned(childof) and
  7565. childof.needs_inittable);
  7566. odt_cppclass,
  7567. odt_objcclass,
  7568. odt_objcprotocol,
  7569. odt_javaclass,
  7570. odt_interfacejava:
  7571. needs_inittable:=false;
  7572. else
  7573. internalerror(200108267);
  7574. end;
  7575. end;
  7576. function tobjectdef.needs_separate_initrtti : boolean;
  7577. begin
  7578. result:=not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  7579. end;
  7580. function tobjectdef.has_non_trivial_init_child(check_parent:boolean):boolean;
  7581. begin
  7582. if objecttype in [odt_class,odt_object] then
  7583. begin
  7584. result:=tobjectsymtable(symtable).has_non_trivial_init or
  7585. (check_parent and assigned(childof) and childof.has_non_trivial_init_child(true));
  7586. end
  7587. else
  7588. result:=false;
  7589. end;
  7590. function tobjectdef.rtti_mangledname(rt: trttitype): TSymStr;
  7591. begin
  7592. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  7593. begin
  7594. if not is_unique_objpasdef then
  7595. result:=inherited rtti_mangledname(rt)
  7596. else
  7597. result:=childof.rtti_mangledname(rt)
  7598. end
  7599. else
  7600. begin
  7601. { necessary in case of a dynamic array of nsobject, or
  7602. if an nsobject field appears in a record that needs
  7603. init/finalisation }
  7604. if rt=initrtti then
  7605. begin
  7606. result:=voidpointertype.rtti_mangledname(rt);
  7607. exit;
  7608. end;
  7609. if not(target_info.system in systems_objc_nfabi) then
  7610. begin
  7611. result:=target_asm.labelprefix;
  7612. case objecttype of
  7613. odt_objcclass:
  7614. begin
  7615. case rt of
  7616. objcclassrtti:
  7617. if not(oo_is_classhelper in objectoptions) then
  7618. result:=result+'_OBJC_CLASS_'
  7619. else
  7620. result:=result+'_OBJC_CATEGORY_';
  7621. objcmetartti:
  7622. if not(oo_is_classhelper in objectoptions) then
  7623. result:=result+'_OBJC_METACLASS_'
  7624. else
  7625. internalerror(2009111511);
  7626. else
  7627. internalerror(2009092302);
  7628. end;
  7629. end;
  7630. odt_objcprotocol:
  7631. result:=result+'_OBJC_PROTOCOL_';
  7632. else
  7633. ;
  7634. end;
  7635. end
  7636. else
  7637. begin
  7638. case objecttype of
  7639. odt_objcclass:
  7640. begin
  7641. if (oo_is_classhelper in objectoptions) and
  7642. (rt<>objcclassrtti) then
  7643. internalerror(2009111512);
  7644. case rt of
  7645. objcclassrtti:
  7646. if not(oo_is_classhelper in objectoptions) then
  7647. result:='_OBJC_CLASS_$_'
  7648. else
  7649. result:='_OBJC_$_CATEGORY_';
  7650. objcmetartti:
  7651. result:='_OBJC_METACLASS_$_';
  7652. objcclassrortti:
  7653. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  7654. objcmetarortti:
  7655. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  7656. else
  7657. internalerror(2009092303);
  7658. end;
  7659. end;
  7660. odt_objcprotocol:
  7661. begin
  7662. result:=lower(target_asm.labelprefix);
  7663. case rt of
  7664. objcclassrtti:
  7665. result:=result+'_OBJC_PROTOCOL_$_';
  7666. objcmetartti:
  7667. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  7668. else
  7669. internalerror(2009092501);
  7670. end;
  7671. end;
  7672. else
  7673. internalerror(2013113005);
  7674. end;
  7675. end;
  7676. result:=result+objextname^;
  7677. end;
  7678. end;
  7679. function tobjectdef.is_unique_objpasdef: boolean;
  7680. begin
  7681. result:=
  7682. (df_unique in defoptions) and
  7683. is_class_or_interface_or_dispinterface(self)
  7684. end;
  7685. function tobjectdef.members_need_inittable : boolean;
  7686. begin
  7687. members_need_inittable:=tObjectSymtable(symtable).needs_init_final or
  7688. has_non_trivial_init_child(true);
  7689. end;
  7690. function tobjectdef.is_publishable : tpublishproperty;
  7691. begin
  7692. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  7693. is_publishable:=pp_publish
  7694. else
  7695. is_publishable:=pp_error;
  7696. end;
  7697. function tobjectdef.get_next_dispid: longint;
  7698. begin
  7699. inc(fcurrent_dispid);
  7700. result:=fcurrent_dispid;
  7701. end;
  7702. function tobjectdef.search_enumerator_get: tprocdef;
  7703. begin
  7704. result:=inherited;
  7705. if not assigned(result) and assigned(childof) then
  7706. result:=childof.search_enumerator_get;
  7707. end;
  7708. function tobjectdef.search_enumerator_move: tprocdef;
  7709. begin
  7710. result:=inherited;
  7711. if not assigned(result) and assigned(childof) then
  7712. result:=childof.search_enumerator_move;
  7713. end;
  7714. function tobjectdef.search_enumerator_current: tsym;
  7715. begin
  7716. result:=inherited;
  7717. if not assigned(result) and assigned(childof) then
  7718. result:=childof.search_enumerator_current;
  7719. end;
  7720. procedure tobjectdef.register_created_classref_type;
  7721. begin
  7722. if not classref_created_in_current_module then
  7723. begin
  7724. classref_created_in_current_module:=true;
  7725. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  7726. end;
  7727. end;
  7728. procedure tobjectdef.register_created_object_type;
  7729. begin
  7730. if not created_in_current_module then
  7731. begin
  7732. created_in_current_module:=true;
  7733. current_module.wpoinfo.addcreatedobjtype(self);
  7734. end;
  7735. end;
  7736. procedure tobjectdef.register_maybe_created_object_type;
  7737. begin
  7738. { if we know it has been created for sure, no need
  7739. to also record that it maybe can be created in
  7740. this module
  7741. }
  7742. if not (created_in_current_module) and
  7743. not (maybe_created_in_current_module) then
  7744. begin
  7745. maybe_created_in_current_module:=true;
  7746. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  7747. end;
  7748. end;
  7749. procedure tobjectdef.register_vmt_call(index: longint);
  7750. begin
  7751. if (is_object(self) or is_class(self)) then
  7752. current_module.wpoinfo.addcalledvmtentry(self,index);
  7753. end;
  7754. procedure check_and_finish_msg(data: tobject; arg: pointer);
  7755. var
  7756. def: tdef absolute data;
  7757. pd: tprocdef absolute data;
  7758. i,
  7759. paracount: longint;
  7760. begin
  7761. if (def.typ=procdef) then
  7762. begin
  7763. { add all messages also under a dummy name to the symtable in
  7764. which the objcclass/protocol/category is declared, so they can
  7765. be called via id.<name>
  7766. }
  7767. create_class_helper_for_procdef(pd,nil);
  7768. { we have to wait until now to set the mangled name because it
  7769. depends on the (possibly external) class name, which is defined
  7770. at the very end. }
  7771. if not(po_msgstr in pd.procoptions) then
  7772. begin
  7773. CGMessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  7774. { recover to avoid internalerror later on }
  7775. include(pd.procoptions,po_msgstr);
  7776. pd.messageinf.str:=stringdup('MissingDeclaration');
  7777. end;
  7778. { Mangled name is already set in case this is a copy of
  7779. another type. }
  7780. if not(po_has_mangledname in pd.procoptions) then
  7781. begin
  7782. { check whether the number of formal parameters is correct,
  7783. and whether they have valid Objective-C types }
  7784. paracount:=0;
  7785. for i:=1 to length(pd.messageinf.str^) do
  7786. if pd.messageinf.str^[i]=':' then
  7787. inc(paracount);
  7788. for i:=0 to pd.paras.count-1 do
  7789. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  7790. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  7791. dec(paracount);
  7792. if (paracount<>0) then
  7793. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  7794. pd.setmangledname(pd.objcmangledname);
  7795. end
  7796. else
  7797. { all checks already done }
  7798. exit;
  7799. if not(oo_is_external in pd.struct.objectoptions) then
  7800. begin
  7801. if (po_varargs in pd.procoptions) then
  7802. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  7803. else
  7804. begin
  7805. { check for "array of const" parameters }
  7806. for i:=0 to pd.parast.symlist.count-1 do
  7807. begin
  7808. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  7809. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  7810. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  7811. end;
  7812. end;
  7813. end;
  7814. end;
  7815. end;
  7816. procedure mark_private_fields_used(data: tobject; arg: pointer);
  7817. var
  7818. sym: tsym absolute data;
  7819. begin
  7820. if (sym.typ=fieldvarsym) and
  7821. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  7822. sym.IncRefCount;
  7823. end;
  7824. procedure tobjectdef.finish_objc_data;
  7825. begin
  7826. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  7827. if (oo_is_external in objectoptions) then
  7828. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  7829. end;
  7830. procedure verify_objc_vardef(data: tobject; arg: pointer);
  7831. var
  7832. sym: tabstractvarsym absolute data;
  7833. res: pboolean absolute arg;
  7834. founderrordef: tdef;
  7835. begin
  7836. if not(tsym(data).typ in [paravarsym,fieldvarsym]) then
  7837. exit;
  7838. if (sym.typ=paravarsym) and
  7839. ((vo_is_hidden_para in tparavarsym(sym).varoptions) or
  7840. is_array_of_const(tparavarsym(sym).vardef)) then
  7841. exit;
  7842. if not objcchecktype(sym.vardef,founderrordef) then
  7843. begin
  7844. MessagePos1(sym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7845. res^:=false;
  7846. end;
  7847. end;
  7848. procedure verify_objc_procdef_paras(data: tobject; arg: pointer);
  7849. var
  7850. def: tdef absolute data;
  7851. res: pboolean absolute arg;
  7852. founderrordef: tdef;
  7853. begin
  7854. if (def.typ<>procdef) then
  7855. exit;
  7856. { check parameter types for validity }
  7857. tprocdef(def).paras.foreachcall(@verify_objc_vardef,arg);
  7858. { check the result type for validity }
  7859. if not objcchecktype(tprocdef(def).returndef,founderrordef) then
  7860. begin
  7861. MessagePos1(tprocdef(def).funcretsym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename);
  7862. res^:=false;
  7863. end;
  7864. end;
  7865. function tobjectdef.check_objc_types: boolean;
  7866. begin
  7867. { done in separate step from finish_objc_data, because when
  7868. finish_objc_data is called, not all forwarddefs have been resolved
  7869. yet and we need to know all types here }
  7870. result:=true;
  7871. self.symtable.symlist.foreachcall(@verify_objc_vardef,@result);
  7872. self.symtable.deflist.foreachcall(@verify_objc_procdef_paras,@result);
  7873. end;
  7874. procedure do_cpp_import_info(data: tobject; arg: pointer);
  7875. var
  7876. def: tdef absolute data;
  7877. pd: tprocdef absolute data;
  7878. begin
  7879. if (def.typ=procdef) then
  7880. begin
  7881. pd.setmangledname(target_info.Cprefix+pd.cplusplusmangledname);
  7882. if (oo_is_external in pd.struct.objectoptions) then
  7883. begin
  7884. { copied from psub.read_proc }
  7885. if assigned(tobjectdef(pd.struct).import_lib) then
  7886. current_module.AddExternalImport(tobjectdef(pd.struct).import_lib^,pd.mangledname,pd.mangledname,0,false,false)
  7887. else
  7888. begin
  7889. { add import name to external list for DLL scanning }
  7890. if tf_has_dllscanner in target_info.flags then
  7891. current_module.dllscannerinputlist.Add(pd.mangledname,pd);
  7892. end;
  7893. end;
  7894. end;
  7895. end;
  7896. procedure tobjectdef.finish_cpp_data;
  7897. begin
  7898. self.symtable.DefList.ForEachCall(@do_cpp_import_info,nil);
  7899. end;
  7900. procedure tobjectdef.apply_rtti_directive(dir: trtti_directive);
  7901. begin
  7902. rtti.clause:=dir.clause;
  7903. rtti.options:=dir.options;
  7904. if (dir.clause=rtc_inherit) and assigned(childof) and (childof.rtti.clause<>rtc_none) then
  7905. begin
  7906. rtti.options[ro_methods]:=rtti.options[ro_methods]+childof.rtti.options[ro_methods];
  7907. rtti.options[ro_fields]:=rtti.options[ro_fields]+childof.rtti.options[ro_fields];
  7908. rtti.options[ro_properties]:=rtti.options[ro_properties]+childof.rtti.options[ro_properties];
  7909. end;
  7910. end;
  7911. {$ifdef DEBUG_NODE_XML}
  7912. function TObjectDef.XMLPrintType: ansistring;
  7913. begin
  7914. if (oo_is_forward in objectoptions) then
  7915. Result := '&lt;class prototype&gt;'
  7916. else
  7917. Result := '&lt;class&gt;';
  7918. end;
  7919. procedure TObjectDef.XMLPrintDefInfo(var T: Text; Sym: TSym);
  7920. var
  7921. i: TObjectOption;
  7922. first: Boolean;
  7923. begin
  7924. inherited XMLPrintDefInfo(T, Sym);
  7925. First := True;
  7926. for i := Low(TObjectOption) to High(TObjectOption) do
  7927. if i in objectoptions then
  7928. begin
  7929. if First then
  7930. begin
  7931. Write(T, ' objectoptions="', i);
  7932. First := False;
  7933. end
  7934. else
  7935. Write(T, ',', i)
  7936. end;
  7937. if not first then
  7938. Write(T, '"');
  7939. end;
  7940. procedure TObjectDef.XMLPrintDefData(var T: Text; Sym: TSym);
  7941. begin
  7942. { There's nothing useful yet if the type is only forward-declared }
  7943. if not (oo_is_forward in objectoptions) then
  7944. begin
  7945. if Assigned(childof) then
  7946. WriteLn(T, printnodeindention, '<ancestor>', SanitiseXMLString(childof.typesym.RealName), '</ancestor>');
  7947. inherited XMLPrintDefData(T, Sym);
  7948. end;
  7949. end;
  7950. {$endif DEBUG_NODE_XML}
  7951. {****************************************************************************
  7952. TImplementedInterface
  7953. ****************************************************************************}
  7954. function TImplementedInterface.GetIOffset: longint;
  7955. begin
  7956. if (fIOffset=-1) and
  7957. (IType in [etFieldValue,etFieldValueClass]) then
  7958. result:=tfieldvarsym(ImplementsField).fieldoffset
  7959. else
  7960. result:=fIOffset;
  7961. end;
  7962. constructor TImplementedInterface.create(aintf: tobjectdef);
  7963. begin
  7964. inherited create;
  7965. intfdef:=aintf;
  7966. intfdefderef.reset;
  7967. IOffset:=-1;
  7968. IType:=etStandard;
  7969. NameMappings:=nil;
  7970. procdefs:=nil;
  7971. end;
  7972. constructor TImplementedInterface.create_deref(intfd,getterd:tderef);
  7973. begin
  7974. inherited create;
  7975. intfdef:=nil;
  7976. intfdefderef:=intfd;
  7977. ImplementsGetterDeref:=getterd;
  7978. IOffset:=-1;
  7979. IType:=etStandard;
  7980. NameMappings:=nil;
  7981. procdefs:=nil;
  7982. end;
  7983. destructor TImplementedInterface.destroy;
  7984. var
  7985. i : longint;
  7986. mappedname : pshortstring;
  7987. begin
  7988. if assigned(NameMappings) then
  7989. begin
  7990. for i:=0 to NameMappings.Count-1 do
  7991. begin
  7992. mappedname:=pshortstring(NameMappings[i]);
  7993. stringdispose(mappedname);
  7994. end;
  7995. NameMappings.free;
  7996. NameMappings:=nil;
  7997. end;
  7998. if assigned(procdefs) then
  7999. begin
  8000. procdefs.free;
  8001. procdefs:=nil;
  8002. end;
  8003. inherited destroy;
  8004. end;
  8005. procedure TImplementedInterface.buildderef;
  8006. begin
  8007. intfdefderef.build(intfdef);
  8008. ImplementsGetterDeref.build(ImplementsGetter);
  8009. end;
  8010. procedure TImplementedInterface.deref;
  8011. begin
  8012. intfdef:=tobjectdef(intfdefderef.resolve);
  8013. ImplementsGetter:=tsym(ImplementsGetterDeref.resolve);
  8014. end;
  8015. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  8016. begin
  8017. if not assigned(NameMappings) then
  8018. NameMappings:=TFPHashList.Create;
  8019. NameMappings.Add(origname,stringdup(newname));
  8020. end;
  8021. function TImplementedInterface.GetMapping(const origname: string):string;
  8022. var
  8023. mappedname : pshortstring;
  8024. begin
  8025. result:='';
  8026. if not assigned(NameMappings) then
  8027. exit;
  8028. mappedname:=PShortstring(NameMappings.Find(origname));
  8029. if assigned(mappedname) then
  8030. result:=mappedname^;
  8031. end;
  8032. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  8033. begin
  8034. if not assigned(procdefs) then
  8035. procdefs:=TFPObjectList.Create(false);
  8036. { duplicate entries must be stored, because multiple }
  8037. { interfaces can declare methods with the same name }
  8038. { and all of these get their own VMT entry }
  8039. procdefs.Add(pd);
  8040. end;
  8041. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  8042. var
  8043. i : longint;
  8044. begin
  8045. result:=false;
  8046. { interfaces being implemented through delegation are not mergable (FK) }
  8047. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) or not(assigned(MergingIntf.ProcDefs)) then
  8048. exit;
  8049. weight:=0;
  8050. { empty interface is mergeable }
  8051. if ProcDefs.Count=0 then
  8052. begin
  8053. result:=true;
  8054. exit;
  8055. end;
  8056. { The interface to merge must at least the number of
  8057. procedures of this interface }
  8058. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  8059. exit;
  8060. for i:=0 to ProcDefs.Count-1 do
  8061. begin
  8062. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  8063. exit;
  8064. end;
  8065. weight:=ProcDefs.Count;
  8066. result:=true;
  8067. end;
  8068. function TImplementedInterface.getcopy:TImplementedInterface;
  8069. var
  8070. i : longint;
  8071. begin
  8072. Result:=TImplementedInterface.Create(nil);
  8073. { 1) the procdefs list will be freed once for each copy
  8074. 2) since the procdefs list owns its elements, those will also be freed for each copy
  8075. Nope: procdefs are owned by their symtable, so no copy necessary
  8076. 3) idem for the name mappings
  8077. }
  8078. result.fIOffset:=fIOffset;
  8079. result.IntfDef:=IntfDef;
  8080. result.IntfDefDeref.reset;
  8081. result.IType:=IType;
  8082. result.VtblImplIntf:=VtblImplIntf;
  8083. if assigned(NameMappings) then
  8084. begin
  8085. result.NameMappings:=TFPHashList.create;
  8086. for i:=0 to NameMappings.Count-1 do
  8087. Result.NameMappings.Add(NameMappings.NameOfIndex(i),
  8088. stringdup(pshortstring(NameMappings.Items[i])^));
  8089. end;
  8090. if assigned(ProcDefs) then
  8091. begin
  8092. result.ProcDefs:=TFPObjectList.create(false);
  8093. { Note: this is probably wrong, because those procdefs are owned by
  8094. the old objectdef from which we copy, what would be the correct way
  8095. of doing this is to lookup the equivalent copy in the new owner
  8096. and reference this instead... But this is complicated so let's try
  8097. it this way until it blows up ok? }
  8098. for i:=0 to ProcDefs.Count-1 do
  8099. Result.ProcDefs.add(tprocdef(procdefs[i]).getcopy);
  8100. end;
  8101. result.ImplementsGetter:=ImplementsGetter;
  8102. result.ImplementsGetterDeref.reset;
  8103. result.ImplementsField:=ImplementsField;
  8104. end;
  8105. {****************************************************************************
  8106. TFORWARDDEF
  8107. ****************************************************************************}
  8108. constructor tforwarddef.create(const s:string;const pos:tfileposinfo);
  8109. begin
  8110. inherited create(forwarddef,true);
  8111. tosymname:=stringdup(s);
  8112. forwardpos:=pos;
  8113. end;
  8114. function tforwarddef.GetTypeName:string;
  8115. begin
  8116. GetTypeName:='unresolved forward to '+tosymname^;
  8117. end;
  8118. destructor tforwarddef.destroy;
  8119. begin
  8120. stringdispose(tosymname);
  8121. inherited destroy;
  8122. end;
  8123. function tforwarddef.getcopy:tstoreddef;
  8124. begin
  8125. result:=cforwarddef.create(tosymname^, forwardpos);
  8126. end;
  8127. {****************************************************************************
  8128. TUNDEFINEDDEF
  8129. ****************************************************************************}
  8130. constructor tundefineddef.create(doregister:boolean);
  8131. begin
  8132. inherited create(undefineddef,doregister);
  8133. end;
  8134. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  8135. begin
  8136. inherited ppuload(undefineddef,ppufile);
  8137. ppuload_platform(ppufile);
  8138. end;
  8139. function tundefineddef.GetTypeName:string;
  8140. begin
  8141. GetTypeName:='<undefined type>';
  8142. end;
  8143. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  8144. begin
  8145. inherited ppuwrite(ppufile);
  8146. writeentry(ppufile,ibundefineddef);
  8147. end;
  8148. {****************************************************************************
  8149. TERRORDEF
  8150. ****************************************************************************}
  8151. constructor terrordef.create;
  8152. begin
  8153. inherited create(errordef,true);
  8154. { prevent consecutive faults }
  8155. savesize:=1;
  8156. end;
  8157. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  8158. begin
  8159. { Can't write errordefs to ppu }
  8160. internalerror(200411063);
  8161. end;
  8162. function terrordef.GetTypeName:string;
  8163. begin
  8164. GetTypeName:='<erroneous type>';
  8165. end;
  8166. function terrordef.getmangledparaname:TSymStr;
  8167. begin
  8168. getmangledparaname:='error';
  8169. end;
  8170. {****************************************************************************
  8171. Definition Helpers
  8172. ****************************************************************************}
  8173. function is_interfacecom(def: tdef): boolean;
  8174. begin
  8175. is_interfacecom:=
  8176. assigned(def) and
  8177. (def.typ=objectdef) and
  8178. (tobjectdef(def).objecttype=odt_interfacecom);
  8179. end;
  8180. function is_interfacecom_or_dispinterface(def: tdef): boolean;
  8181. begin
  8182. is_interfacecom_or_dispinterface:=
  8183. assigned(def) and
  8184. (def.typ=objectdef) and
  8185. (tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface]);
  8186. end;
  8187. function is_any_interface_kind(def: tdef): boolean;
  8188. begin
  8189. result:=
  8190. assigned(def) and
  8191. (def.typ=objectdef) and
  8192. ((tobjectdef(def).objecttype in [odt_interfacecom,odt_dispinterface,odt_interfacecorba,odt_interfacejava,odt_objcprotocol]) or
  8193. is_objccategory(def));
  8194. end;
  8195. function is_interfacecorba(def: tdef): boolean;
  8196. begin
  8197. is_interfacecorba:=
  8198. assigned(def) and
  8199. (def.typ=objectdef) and
  8200. (tobjectdef(def).objecttype=odt_interfacecorba);
  8201. end;
  8202. function is_interface(def: tdef): boolean;
  8203. begin
  8204. is_interface:=
  8205. assigned(def) and
  8206. (def.typ=objectdef) and
  8207. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  8208. end;
  8209. function is_dispinterface(def: tdef): boolean;
  8210. begin
  8211. result:=
  8212. assigned(def) and
  8213. (def.typ=objectdef) and
  8214. (tobjectdef(def).objecttype=odt_dispinterface);
  8215. end;
  8216. function is_class(def: tdef): boolean;
  8217. begin
  8218. is_class:=
  8219. assigned(def) and
  8220. (def.typ=objectdef) and
  8221. (tobjectdef(def).objecttype=odt_class);
  8222. end;
  8223. function is_object(def: tdef): boolean;
  8224. begin
  8225. is_object:=
  8226. assigned(def) and
  8227. (def.typ=objectdef) and
  8228. (tobjectdef(def).objecttype=odt_object);
  8229. end;
  8230. function is_cppclass(def: tdef): boolean;
  8231. begin
  8232. is_cppclass:=
  8233. assigned(def) and
  8234. (def.typ=objectdef) and
  8235. (tobjectdef(def).objecttype=odt_cppclass);
  8236. end;
  8237. function is_objcclass(def: tdef): boolean;
  8238. begin
  8239. is_objcclass:=
  8240. assigned(def) and
  8241. (def.typ=objectdef) and
  8242. (tobjectdef(def).objecttype=odt_objcclass);
  8243. end;
  8244. function is_objectpascal_helper(def: tdef): boolean;
  8245. begin
  8246. result:=
  8247. assigned(def) and
  8248. (def.typ=objectdef) and
  8249. (tobjectdef(def).objecttype=odt_helper);
  8250. end;
  8251. function is_objcclassref(def: tdef): boolean;
  8252. begin
  8253. is_objcclassref:=
  8254. assigned(def) and
  8255. (def.typ=classrefdef) and
  8256. is_objcclass(tclassrefdef(def).pointeddef);
  8257. end;
  8258. function is_objcprotocol(def: tdef): boolean;
  8259. begin
  8260. result:=
  8261. assigned(def) and
  8262. (def.typ=objectdef) and
  8263. (tobjectdef(def).objecttype=odt_objcprotocol);
  8264. end;
  8265. function is_objccategory(def: tdef): boolean;
  8266. begin
  8267. result:=
  8268. assigned(def) and
  8269. (def.typ=objectdef) and
  8270. { if used as a forward type }
  8271. ((tobjectdef(def).objecttype=odt_objccategory) or
  8272. { if used as after it has been resolved }
  8273. ((tobjectdef(def).objecttype=odt_objcclass) and
  8274. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  8275. end;
  8276. function is_objc_class_or_protocol(def: tdef): boolean;
  8277. begin
  8278. result:=
  8279. assigned(def) and
  8280. (def.typ=objectdef) and
  8281. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  8282. end;
  8283. function is_objc_protocol_or_category(def: tdef): boolean;
  8284. begin
  8285. result:=
  8286. assigned(def) and
  8287. (def.typ=objectdef) and
  8288. ((tobjectdef(def).objecttype = odt_objcprotocol) or
  8289. ((tobjectdef(def).objecttype = odt_objcclass) and
  8290. (oo_is_classhelper in tobjectdef(def).objectoptions)));
  8291. end;
  8292. function is_classhelper(def: tdef): boolean;
  8293. begin
  8294. result:=
  8295. is_objectpascal_helper(def) or
  8296. is_objccategory(def);
  8297. end;
  8298. function is_class_or_interface(def: tdef): boolean;
  8299. begin
  8300. result:=
  8301. assigned(def) and
  8302. (def.typ=objectdef) and
  8303. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  8304. end;
  8305. function is_class_or_interface_or_objc(def: tdef): boolean;
  8306. begin
  8307. result:=
  8308. assigned(def) and
  8309. (def.typ=objectdef) and
  8310. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  8311. end;
  8312. function is_class_or_interface_or_objc_or_java(def: tdef): boolean;
  8313. begin
  8314. result:=
  8315. assigned(def) and
  8316. (def.typ=objectdef) and
  8317. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  8318. end;
  8319. function is_class_or_interface_or_dispinterface_or_objc_or_java(def: tdef): boolean;
  8320. begin
  8321. result:=
  8322. assigned(def) and
  8323. (def.typ=objectdef) and
  8324. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_javaclass,odt_interfacejava]);
  8325. end;
  8326. function is_class_or_interface_or_object(def: tdef): boolean;
  8327. begin
  8328. result:=
  8329. assigned(def) and
  8330. (def.typ=objectdef) and
  8331. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  8332. end;
  8333. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  8334. begin
  8335. result:=
  8336. assigned(def) and
  8337. (def.typ=objectdef) and
  8338. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  8339. end;
  8340. function is_implicit_pointer_object_type(def: tdef): boolean;
  8341. begin
  8342. result:=
  8343. assigned(def) and
  8344. (((def.typ=objectdef) and
  8345. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol,odt_helper,odt_javaclass,odt_interfacejava])) or
  8346. ((target_info.system in systems_jvm) and
  8347. (def.typ=recorddef)));
  8348. end;
  8349. function is_implicit_array_pointer(def: tdef): boolean;
  8350. begin
  8351. result:=is_dynamic_array(def) or is_dynamicstring(def);
  8352. end;
  8353. function is_class_or_object(def: tdef): boolean;
  8354. begin
  8355. result:=
  8356. assigned(def) and
  8357. (def.typ=objectdef) and
  8358. (tobjectdef(def).objecttype in [odt_class,odt_object]);
  8359. end;
  8360. function is_record(def: tdef): boolean;
  8361. begin
  8362. result:=
  8363. assigned(def) and
  8364. (def.typ=recorddef);
  8365. end;
  8366. function is_javaclass(def: tdef): boolean;
  8367. begin
  8368. result:=
  8369. assigned(def) and
  8370. (def.typ=objectdef) and
  8371. (tobjectdef(def).objecttype=odt_javaclass);
  8372. end;
  8373. function is_javaclassref(def: tdef): boolean;
  8374. begin
  8375. is_javaclassref:=
  8376. assigned(def) and
  8377. (def.typ=classrefdef) and
  8378. is_javaclass(tclassrefdef(def).pointeddef);
  8379. end;
  8380. function is_javainterface(def: tdef): boolean;
  8381. begin
  8382. result:=
  8383. assigned(def) and
  8384. (def.typ=objectdef) and
  8385. (tobjectdef(def).objecttype=odt_interfacejava);
  8386. end;
  8387. function is_java_class_or_interface(def: tdef): boolean;
  8388. begin
  8389. result:=
  8390. assigned(def) and
  8391. (def.typ=objectdef) and
  8392. (tobjectdef(def).objecttype in [odt_javaclass,odt_interfacejava]);
  8393. end;
  8394. procedure loadobjctypes;
  8395. begin
  8396. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS',true).typedef);
  8397. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER',true).typedef);
  8398. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID',true).typedef);
  8399. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL',true).typedef);
  8400. objc_objecttype:=trecorddef(search_named_unit_globaltype('OBJC','OBJC_OBJECT',true).typedef);
  8401. end;
  8402. procedure maybeloadcocoatypes;
  8403. var
  8404. tsym: ttypesym;
  8405. cocoaunit: string[15];
  8406. begin
  8407. if assigned(objc_fastenumeration) then
  8408. exit;
  8409. if not(target_info.system in [system_arm_ios,system_i386_iphonesim,system_aarch64_ios,system_x86_64_iphonesim,system_aarch64_iphonesim]) then
  8410. cocoaunit:='COCOAALL'
  8411. else
  8412. cocoaunit:='IPHONEALL';
  8413. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONPROTOCOL',false);
  8414. if assigned(tsym) then
  8415. objc_fastenumeration:=tobjectdef(tsym.typedef)
  8416. else
  8417. objc_fastenumeration:=nil;
  8418. tsym:=search_named_unit_globaltype(cocoaunit,'NSFASTENUMERATIONSTATE',false);
  8419. if assigned(tsym) then
  8420. objc_fastenumerationstate:=trecorddef(tsym.typedef)
  8421. else
  8422. objc_fastenumerationstate:=nil;
  8423. end;
  8424. function use_vectorfpu(def : tdef) : boolean;
  8425. begin
  8426. {$ifdef x86}
  8427. {$define use_vectorfpuimplemented}
  8428. use_vectorfpu:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  8429. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  8430. {$endif x86}
  8431. {$ifdef arm}
  8432. {$define use_vectorfpuimplemented}
  8433. use_vectorfpu:=FPUARM_HAS_VFP_EXTENSION in fpu_capabilities[current_settings.fputype];
  8434. {$endif arm}
  8435. {$ifdef aarch64}
  8436. {$define use_vectorfpuimplemented}
  8437. use_vectorfpu:=true;
  8438. {$endif aarch64}
  8439. {$ifndef use_vectorfpuimplemented}
  8440. use_vectorfpu:=false;
  8441. {$endif}
  8442. end;
  8443. procedure reset_all_default_types;
  8444. { Those class pointers have to be set to nil manually }
  8445. { after memory they point to have been released. }
  8446. { Necessary strictly for Textmode IDE. }
  8447. begin
  8448. voidpointertype:=nil; { pointer for Void-pointeddef }
  8449. charpointertype:=nil; { pointer for Char-pointeddef }
  8450. widecharpointertype:=nil; { pointer for WideChar-pointeddef }
  8451. voidcodepointertype:=nil; { pointer to code; corresponds to System.CodePointer }
  8452. voidstackpointertype:=nil; { the pointer type used for accessing parameters and local vars on the stack }
  8453. parentfpvoidpointertype:=nil; { void pointer with the size of the hidden parentfp parameter, passed to nested functions }
  8454. {$ifdef x86}
  8455. voidnearpointertype:=nil;
  8456. voidnearcspointertype:=nil;
  8457. voidneardspointertype:=nil;
  8458. voidnearsspointertype:=nil;
  8459. voidnearespointertype:=nil;
  8460. voidnearfspointertype:=nil;
  8461. voidneargspointertype:=nil;
  8462. {$ifdef i8086}
  8463. voidfarpointertype:=nil;
  8464. voidhugepointertype:=nil;
  8465. charnearpointertype:=nil;
  8466. charfarpointertype:=nil;
  8467. charhugepointertype:=nil;
  8468. bytefarpointertype:=nil; { used for Mem[] }
  8469. wordfarpointertype:=nil; { used for MemW[] }
  8470. longintfarpointertype:=nil; { used for MemL[] }
  8471. {$endif i8086}
  8472. {$endif x86}
  8473. {$ifdef wasm}
  8474. wasmvoidexternreftype:=nil;
  8475. {$endif wasm}
  8476. cundefinedtype:=nil;
  8477. cformaltype:=nil; { unique formal definition }
  8478. ctypedformaltype:=nil; { unique typed formal definition }
  8479. voidtype:=nil; { Void (procedure) }
  8480. cansichartype:=nil; { Char }
  8481. cwidechartype:=nil; { WideChar }
  8482. cchartype:=nil; { either cansichartype or cwidechartype. Do not free }
  8483. pasbool1type:=nil; { boolean type }
  8484. pasbool8type:=nil;
  8485. pasbool16type:=nil;
  8486. pasbool32type:=nil;
  8487. pasbool64type:=nil;
  8488. bool8type:=nil;
  8489. bool16type:=nil;
  8490. bool32type:=nil;
  8491. bool64type:=nil;
  8492. {$ifdef llvm}
  8493. llvmbool1type:=nil; { LLVM i1 type }
  8494. {$endif llvm}
  8495. u8inttype:=nil; { 8-Bit unsigned integer }
  8496. s8inttype:=nil; { 8-Bit signed integer }
  8497. u16inttype:=nil; { 16-Bit unsigned integer }
  8498. s16inttype:=nil; { 16-Bit signed integer }
  8499. u24inttype:=nil; { 24-Bit unsigned integer }
  8500. s24inttype:=nil; { 24-Bit signed integer }
  8501. u32inttype:=nil; { 32-Bit unsigned integer }
  8502. s32inttype:=nil; { 32-Bit signed integer }
  8503. u40inttype:=nil; { 40-Bit unsigned integer }
  8504. s40inttype:=nil; { 40-Bit signed integer }
  8505. u48inttype:=nil; { 48-Bit unsigned integer }
  8506. s48inttype:=nil; { 48-Bit signed integer }
  8507. u56inttype:=nil; { 56-Bit unsigned integer }
  8508. s56inttype:=nil; { 56-Bit signed integer }
  8509. u64inttype:=nil; { 64-bit unsigned integer }
  8510. s64inttype:=nil; { 64-bit signed integer }
  8511. u128inttype:=nil; { 128-bit unsigned integer }
  8512. s128inttype:=nil; { 128-bit signed integer }
  8513. s32floattype:=nil; { 32 bit floating point number }
  8514. s64floattype:=nil; { 64 bit floating point number }
  8515. s80floattype:=nil; { 80 bit floating point number }
  8516. sc80floattype:=nil; { 80 bit floating point number but stored like in C }
  8517. s64currencytype:=nil; { pointer to a currency type }
  8518. cshortstringtype:=nil; { pointer to type of short string const }
  8519. clongstringtype:=nil; { pointer to type of long string const }
  8520. cansistringtype:=nil; { pointer to type of ansi string const }
  8521. cwidestringtype:=nil; { pointer to type of wide string const }
  8522. cunicodestringtype:=nil;
  8523. openshortstringtype:=nil;
  8524. openchararraytype:=nil;
  8525. cfiletype:=nil;
  8526. methodpointertype:=nil; { typecasting of methodpointers to extract self }
  8527. nestedprocpointertype:=nil; { typecasting of nestedprocpointers to extract parentfp }
  8528. hresultdef:=nil;
  8529. typekindtype:=nil; { def of TTypeKind for correct handling of GetTypeKind parameters }
  8530. { we use only one variant def for every variant class }
  8531. cvarianttype:=nil;
  8532. colevarianttype:=nil;
  8533. { default integer type, normally s32inttype on 32 bit systems and s64bittype on 64 bit systems }
  8534. sinttype:=nil;
  8535. uinttype:=nil;
  8536. { integer types corresponding to OS_SINT/OS_INT }
  8537. ossinttype:=nil;
  8538. osuinttype:=nil;
  8539. { integer types corresponding to the ALU size, sizeof(aint) and the ALUSInt/ALUUInt types in the system unit }
  8540. alusinttype:=nil;
  8541. aluuinttype:=nil;
  8542. { integer types corresponding to SizeInt and SizeUInt for the target platform }
  8543. sizeuinttype:=nil;
  8544. sizesinttype:=nil;
  8545. { unsigned and signed ord type with the same size as a pointer }
  8546. ptruinttype:=nil;
  8547. ptrsinttype:=nil;
  8548. { unsigned and signed ord type with the same size as a codepointer }
  8549. codeptruinttype:=nil;
  8550. codeptrsinttype:=nil;
  8551. { several types to simulate more or less C++ objects for GDB }
  8552. vmttype:=nil;
  8553. vmtarraytype:=nil;
  8554. { type of classrefs, used for stabs }
  8555. pvmttype:=nil;
  8556. { return type of the setjmp function }
  8557. exceptionreasontype:=nil;
  8558. class_tobject:=nil;
  8559. class_tcustomattribute:=nil;
  8560. interface_iunknown:=nil;
  8561. interface_idispatch:=nil;
  8562. rec_tguid:=nil;
  8563. rec_jmp_buf:=nil;
  8564. rec_exceptaddr:=nil;
  8565. objc_metaclasstype:=nil;
  8566. objc_superclasstype:=nil;
  8567. objc_idtype:=nil;
  8568. objc_seltype:=nil;
  8569. objc_objecttype:=nil;
  8570. objc_protocoltype:=nil;
  8571. objc_fastenumeration:=nil;
  8572. objc_fastenumerationstate:=nil;
  8573. {$ifdef llvm}
  8574. { llvm types }
  8575. { a unique def to identify any kind of metadata }
  8576. llvm_metadatatype:=nil;
  8577. {$endif llvm}
  8578. { Java base types }
  8579. java_jlobject:=nil;
  8580. java_jlthrowable:=nil;
  8581. java_fpcbaserecordtype:=nil;
  8582. java_jlstring:=nil;
  8583. java_jlenum:=nil;
  8584. java_juenumset:=nil;
  8585. java_jubitset:=nil;
  8586. java_ansistring:=nil;
  8587. java_shortstring:=nil;
  8588. java_procvarbase:=nil;
  8589. { x86 vector types }
  8590. x86_m64type:=nil;
  8591. x86_m128type:=nil;
  8592. x86_m128dtype:=nil;
  8593. x86_m128itype:=nil;
  8594. x86_m256type:=nil;
  8595. x86_m256dtype:=nil;
  8596. x86_m256itype:=nil;
  8597. end;
  8598. end.