symdef.pas 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062
  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,
  23. { global }
  24. globtype,globals,tokens,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { ppu }
  28. ppu,
  29. { node }
  30. node,
  31. { aasm }
  32. aasmbase,aasmtai,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. { tstoreddef }
  42. tstoreddef = class(tdef)
  43. protected
  44. typesymderef : tderef;
  45. public
  46. {$ifdef EXTDEBUG}
  47. fileinfo : tfileposinfo;
  48. {$endif}
  49. { generic support }
  50. genericdef : tstoreddef;
  51. genericdefderef : tderef;
  52. generictokenbuf : tdynamicarray;
  53. constructor create(dt:tdeftyp);
  54. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  55. destructor destroy;override;
  56. procedure reset;virtual;
  57. function getcopy : tstoreddef;virtual;
  58. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  59. procedure buildderef;override;
  60. procedure buildderefimpl;override;
  61. procedure deref;override;
  62. procedure derefimpl;override;
  63. function size:aint;override;
  64. function getvardef:longint;override;
  65. function alignment:shortint;override;
  66. function is_publishable : boolean;override;
  67. function needs_inittable : boolean;override;
  68. function rtti_mangledname(rt:trttitype):string;override;
  69. { regvars }
  70. function is_intregable : boolean;
  71. function is_fpuregable : boolean;
  72. { generics }
  73. procedure initgeneric;
  74. private
  75. savesize : aint;
  76. end;
  77. tfiletyp = (ft_text,ft_typed,ft_untyped);
  78. tfiledef = class(tstoreddef)
  79. filetyp : tfiletyp;
  80. typedfiledef : tdef;
  81. typedfiledefderef : tderef;
  82. constructor createtext;
  83. constructor createuntyped;
  84. constructor createtyped(def : tdef);
  85. constructor ppuload(ppufile:tcompilerppufile);
  86. function getcopy : tstoreddef;override;
  87. procedure ppuwrite(ppufile:tcompilerppufile);override;
  88. procedure buildderef;override;
  89. procedure deref;override;
  90. function GetTypeName:string;override;
  91. function getmangledparaname:string;override;
  92. procedure setsize;
  93. end;
  94. tvariantdef = class(tstoreddef)
  95. varianttype : tvarianttype;
  96. constructor create(v : tvarianttype);
  97. constructor ppuload(ppufile:tcompilerppufile);
  98. function getcopy : tstoreddef;override;
  99. function GetTypeName:string;override;
  100. procedure ppuwrite(ppufile:tcompilerppufile);override;
  101. function getvardef:longint;override;
  102. procedure setsize;
  103. function is_publishable : boolean;override;
  104. function needs_inittable : boolean;override;
  105. end;
  106. tformaldef = class(tstoreddef)
  107. typed:boolean;
  108. constructor create(Atyped:boolean);
  109. constructor ppuload(ppufile:tcompilerppufile);
  110. procedure ppuwrite(ppufile:tcompilerppufile);override;
  111. function GetTypeName:string;override;
  112. end;
  113. tforwarddef = class(tstoreddef)
  114. tosymname : pshortstring;
  115. forwardpos : tfileposinfo;
  116. constructor create(const s:string;const pos : tfileposinfo);
  117. destructor destroy;override;
  118. function GetTypeName:string;override;
  119. end;
  120. tundefineddef = class(tstoreddef)
  121. constructor create;
  122. constructor ppuload(ppufile:tcompilerppufile);
  123. procedure ppuwrite(ppufile:tcompilerppufile);override;
  124. function GetTypeName:string;override;
  125. end;
  126. terrordef = class(tstoreddef)
  127. constructor create;
  128. procedure ppuwrite(ppufile:tcompilerppufile);override;
  129. function GetTypeName:string;override;
  130. function getmangledparaname : string;override;
  131. end;
  132. tabstractpointerdef = class(tstoreddef)
  133. pointeddef : tdef;
  134. pointeddefderef : tderef;
  135. constructor create(dt:tdeftyp;def:tdef);
  136. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. procedure buildderef;override;
  139. procedure deref;override;
  140. end;
  141. tpointerdef = class(tabstractpointerdef)
  142. is_far : boolean;
  143. constructor create(def:tdef);
  144. constructor createfar(def:tdef);
  145. function getcopy : tstoreddef;override;
  146. constructor ppuload(ppufile:tcompilerppufile);
  147. procedure ppuwrite(ppufile:tcompilerppufile);override;
  148. function GetTypeName:string;override;
  149. end;
  150. tabstractrecorddef= class(tstoreddef)
  151. symtable : TSymtable;
  152. cloneddef : tabstractrecorddef;
  153. cloneddefderef : tderef;
  154. procedure reset;override;
  155. function GetSymtable(t:tGetSymtable):TSymtable;override;
  156. function is_packed:boolean;
  157. end;
  158. trecorddef = class(tabstractrecorddef)
  159. public
  160. isunion : boolean;
  161. constructor create(p : TSymtable);
  162. constructor ppuload(ppufile:tcompilerppufile);
  163. destructor destroy;override;
  164. function getcopy : tstoreddef;override;
  165. procedure ppuwrite(ppufile:tcompilerppufile);override;
  166. procedure buildderef;override;
  167. procedure deref;override;
  168. function size:aint;override;
  169. function alignment : shortint;override;
  170. function padalignment: shortint;
  171. function GetTypeName:string;override;
  172. { debug }
  173. function needs_inittable : boolean;override;
  174. end;
  175. tprocdef = class;
  176. tobjectdef = class;
  177. { TImplementedInterface }
  178. TImplementedInterface = class
  179. IntfDef : tobjectdef;
  180. IntfDefDeref : tderef;
  181. IType : tinterfaceentrytype;
  182. IOffset : longint;
  183. VtblImplIntf : TImplementedInterface;
  184. NameMappings : TFPHashList;
  185. ProcDefs : TFPObjectList;
  186. ImplementsGetter : tsym;
  187. constructor create(aintf: tobjectdef);
  188. constructor create_deref(d:tderef);
  189. destructor destroy; override;
  190. function getcopy:TImplementedInterface;
  191. procedure buildderef;
  192. procedure deref;
  193. procedure AddMapping(const origname, newname: string);
  194. function GetMapping(const origname: string):string;
  195. procedure AddImplProc(pd:tprocdef);
  196. function IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  197. end;
  198. { tvmtentry }
  199. tvmtentry = record
  200. procdef : tprocdef;
  201. procdefderef : tderef;
  202. visibility : tvisibility;
  203. end;
  204. pvmtentry = ^tvmtentry;
  205. { tobjectdef }
  206. tvmcallstatic = (vmcs_default, vmcs_yes, vmcs_no, vmcs_unreachable);
  207. pmvcallstaticinfo = ^tmvcallstaticinfo;
  208. tmvcallstaticinfo = array[0..1024*1024-1] of tvmcallstatic;
  209. tobjectdef = class(tabstractrecorddef)
  210. public
  211. dwarf_struct_lab : tasmsymbol;
  212. childof : tobjectdef;
  213. childofderef : tderef;
  214. objname,
  215. objrealname,
  216. { for Objective-C: protocols and classes can have the same name there }
  217. objextname : pshortstring;
  218. objectoptions : tobjectoptions;
  219. { to be able to have a variable vmt position }
  220. { and no vmt field for objects without virtuals }
  221. vmtentries : TFPList;
  222. vmcallstaticinfo : pmvcallstaticinfo;
  223. vmt_offset : longint;
  224. objecttype : tobjecttyp;
  225. iidguid : pguid;
  226. iidstr : pshortstring;
  227. writing_class_record_dbginfo,
  228. { a class of this type has been created in this module }
  229. created_in_current_module,
  230. { a loadvmtnode for this class has been created in this
  231. module, so if a classrefdef variable of this or a parent
  232. class is used somewhere to instantiate a class, then this
  233. class may be instantiated
  234. }
  235. maybe_created_in_current_module,
  236. { a "class of" this particular class has been created in
  237. this module
  238. }
  239. classref_created_in_current_module : boolean;
  240. { store implemented interfaces defs and name mappings }
  241. ImplementedInterfaces : TFPObjectList;
  242. constructor create(ot : tobjecttyp;const n : string;c : tobjectdef);
  243. constructor ppuload(ppufile:tcompilerppufile);
  244. destructor destroy;override;
  245. function getcopy : tstoreddef;override;
  246. procedure ppuwrite(ppufile:tcompilerppufile);override;
  247. function GetTypeName:string;override;
  248. procedure buildderef;override;
  249. procedure deref;override;
  250. procedure buildderefimpl;override;
  251. procedure derefimpl;override;
  252. procedure resetvmtentries;
  253. procedure copyvmtentries(objdef:tobjectdef);
  254. function getparentdef:tdef;override;
  255. function size : aint;override;
  256. function alignment:shortint;override;
  257. function vmtmethodoffset(index:longint):longint;
  258. function members_need_inittable : boolean;
  259. function find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  260. { this should be called when this class implements an interface }
  261. procedure prepareguid;
  262. function is_publishable : boolean;override;
  263. function needs_inittable : boolean;override;
  264. function rtti_mangledname(rt:trttitype):string;override;
  265. function vmt_mangledname : string;
  266. procedure check_forwards;
  267. function is_related(d : tdef) : boolean;override;
  268. procedure insertvmt;
  269. procedure set_parent(c : tobjectdef);
  270. function FindDestructor : tprocdef;
  271. function implements_any_interfaces: boolean;
  272. procedure reset; override;
  273. { enumerator support }
  274. function search_enumerator_get: tprocdef;
  275. function search_enumerator_move: tprocdef;
  276. function search_enumerator_current: tsym;
  277. { WPO }
  278. procedure register_created_object_type;override;
  279. procedure register_maybe_created_object_type;
  280. procedure register_created_classref_type;
  281. procedure register_vmt_call(index:longint);
  282. { ObjC }
  283. procedure make_all_methods_external;
  284. procedure finish_objc_data;
  285. end;
  286. tclassrefdef = class(tabstractpointerdef)
  287. constructor create(def:tdef);
  288. constructor ppuload(ppufile:tcompilerppufile);
  289. procedure ppuwrite(ppufile:tcompilerppufile);override;
  290. function GetTypeName:string;override;
  291. function is_publishable : boolean;override;
  292. function rtti_mangledname(rt:trttitype):string;override;
  293. procedure register_created_object_type;override;
  294. procedure reset;override;
  295. end;
  296. tarraydef = class(tstoreddef)
  297. lowrange,
  298. highrange : aint;
  299. rangedef : tdef;
  300. rangedefderef : tderef;
  301. arrayoptions : tarraydefoptions;
  302. protected
  303. _elementdef : tdef;
  304. _elementdefderef : tderef;
  305. procedure setelementdef(def:tdef);
  306. public
  307. function elesize : aint;
  308. function elepackedbitsize : aint;
  309. function elecount : aword;
  310. constructor create_from_pointer(def:tdef);
  311. constructor create(l,h : aint;def:tdef);
  312. constructor ppuload(ppufile:tcompilerppufile);
  313. function getcopy : tstoreddef;override;
  314. procedure ppuwrite(ppufile:tcompilerppufile);override;
  315. function GetTypeName:string;override;
  316. function getmangledparaname : string;override;
  317. procedure buildderef;override;
  318. procedure deref;override;
  319. function size : aint;override;
  320. function alignment : shortint;override;
  321. { returns the label of the range check string }
  322. function needs_inittable : boolean;override;
  323. property elementdef : tdef read _elementdef write setelementdef;
  324. function is_publishable : boolean;override;
  325. end;
  326. torddef = class(tstoreddef)
  327. low,high : TConstExprInt;
  328. ordtype : tordtype;
  329. constructor create(t : tordtype;v,b : TConstExprInt);
  330. constructor ppuload(ppufile:tcompilerppufile);
  331. function getcopy : tstoreddef;override;
  332. procedure ppuwrite(ppufile:tcompilerppufile);override;
  333. function is_publishable : boolean;override;
  334. function GetTypeName:string;override;
  335. function alignment:shortint;override;
  336. procedure setsize;
  337. function packedbitsize: aint; override;
  338. function getvardef : longint;override;
  339. end;
  340. tfloatdef = class(tstoreddef)
  341. floattype : tfloattype;
  342. constructor create(t : tfloattype);
  343. constructor ppuload(ppufile:tcompilerppufile);
  344. function getcopy : tstoreddef;override;
  345. procedure ppuwrite(ppufile:tcompilerppufile);override;
  346. function GetTypeName:string;override;
  347. function is_publishable : boolean;override;
  348. function alignment:shortint;override;
  349. procedure setsize;
  350. function getvardef:longint;override;
  351. end;
  352. tabstractprocdef = class(tstoreddef)
  353. { saves a definition to the return type }
  354. returndef : tdef;
  355. returndefderef : tderef;
  356. parast : TSymtable;
  357. paras : tparalist;
  358. proctypeoption : tproctypeoption;
  359. proccalloption : tproccalloption;
  360. procoptions : tprocoptions;
  361. requiredargarea : aint;
  362. { number of user visibile parameters }
  363. maxparacount,
  364. minparacount : byte;
  365. {$ifdef m68k}
  366. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  367. {$endif}
  368. funcretloc : array[tcallercallee] of TLocation;
  369. has_paraloc_info : boolean; { paraloc info is available }
  370. constructor create(dt:tdeftyp;level:byte);
  371. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  372. destructor destroy;override;
  373. procedure ppuwrite(ppufile:tcompilerppufile);override;
  374. procedure buildderef;override;
  375. procedure deref;override;
  376. procedure calcparas;
  377. function typename_paras(showhidden:boolean): string;
  378. function is_methodpointer:boolean;virtual;
  379. function is_addressonly:boolean;virtual;
  380. private
  381. procedure count_para(p:TObject;arg:pointer);
  382. procedure insert_para(p:TObject;arg:pointer);
  383. end;
  384. tprocvardef = class(tabstractprocdef)
  385. constructor create(level:byte);
  386. constructor ppuload(ppufile:tcompilerppufile);
  387. function getcopy : tstoreddef;override;
  388. procedure ppuwrite(ppufile:tcompilerppufile);override;
  389. function GetSymtable(t:tGetSymtable):TSymtable;override;
  390. function size : aint;override;
  391. function GetTypeName:string;override;
  392. function is_publishable : boolean;override;
  393. function is_methodpointer:boolean;override;
  394. function is_addressonly:boolean;override;
  395. function getmangledparaname:string;override;
  396. end;
  397. tmessageinf = record
  398. case integer of
  399. 0 : (str : pshortstring);
  400. 1 : (i : longint);
  401. end;
  402. tinlininginfo = record
  403. { node tree }
  404. code : tnode;
  405. flags : tprocinfoflags;
  406. end;
  407. pinlininginfo = ^tinlininginfo;
  408. {$ifdef oldregvars}
  409. { register variables }
  410. pregvarinfo = ^tregvarinfo;
  411. tregvarinfo = record
  412. regvars : array[1..maxvarregs] of tsym;
  413. regvars_para : array[1..maxvarregs] of boolean;
  414. regvars_refs : array[1..maxvarregs] of longint;
  415. fpuregvars : array[1..maxfpuvarregs] of tsym;
  416. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  417. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  418. end;
  419. {$endif oldregvars}
  420. tprocdef = class(tabstractprocdef)
  421. private
  422. _mangledname : pshortstring;
  423. public
  424. messageinf : tmessageinf;
  425. dispid : longint;
  426. {$ifndef EXTDEBUG}
  427. { where is this function defined and what were the symbol
  428. flags, needed here because there
  429. is only one symbol for all overloaded functions
  430. EXTDEBUG has fileinfo in tdef (PFV) }
  431. fileinfo : tfileposinfo;
  432. {$endif}
  433. symoptions : tsymoptions;
  434. deprecatedmsg : pshortstring;
  435. { symbol owning this definition }
  436. procsym : tsym;
  437. procsymderef : tderef;
  438. { alias names }
  439. aliasnames : TCmdStrList;
  440. { symtables }
  441. localst : TSymtable;
  442. funcretsym : tsym;
  443. funcretsymderef : tderef;
  444. _class : tobjectdef;
  445. _classderef : tderef;
  446. {$if defined(powerpc) or defined(m68k)}
  447. { library symbol for AmigaOS/MorphOS }
  448. libsym : tsym;
  449. libsymderef : tderef;
  450. {$endif powerpc or m68k}
  451. { name of the result variable to insert in the localsymtable }
  452. resultname : pshortstring;
  453. { import info }
  454. import_dll,
  455. import_name : pshortstring;
  456. { info for inlining the subroutine, if this pointer is nil,
  457. the procedure can't be inlined }
  458. inlininginfo : pinlininginfo;
  459. {$ifdef oldregvars}
  460. regvarinfo: pregvarinfo;
  461. {$endif oldregvars}
  462. { position in aasmoutput list }
  463. procstarttai,
  464. procendtai : tai;
  465. import_nr : word;
  466. extnumber : word;
  467. {$ifdef i386}
  468. fpu_used : byte;
  469. {$endif i386}
  470. visibility : tvisibility;
  471. { true, if the procedure is only declared
  472. (forward procedure) }
  473. forwarddef,
  474. { true if the procedure is declared in the interface }
  475. interfacedef : boolean;
  476. { true if the procedure has a forward declaration }
  477. hasforward : boolean;
  478. constructor create(level:byte);
  479. constructor ppuload(ppufile:tcompilerppufile);
  480. destructor destroy;override;
  481. procedure ppuwrite(ppufile:tcompilerppufile);override;
  482. procedure buildderef;override;
  483. procedure buildderefimpl;override;
  484. procedure deref;override;
  485. procedure derefimpl;override;
  486. procedure reset;override;
  487. function GetSymtable(t:tGetSymtable):TSymtable;override;
  488. function GetTypeName : string;override;
  489. function mangledname : string;
  490. procedure setmangledname(const s : string);
  491. function fullprocname(showhidden:boolean):string;
  492. function cplusplusmangledname : string;
  493. function objcmangledname : string;
  494. function is_methodpointer:boolean;override;
  495. function is_addressonly:boolean;override;
  496. end;
  497. { single linked list of overloaded procs }
  498. pprocdeflist = ^tprocdeflist;
  499. tprocdeflist = record
  500. def : tprocdef;
  501. defderef : tderef;
  502. next : pprocdeflist;
  503. end;
  504. tstringdef = class(tstoreddef)
  505. stringtype : tstringtype;
  506. len : aint;
  507. constructor createshort(l : byte);
  508. constructor loadshort(ppufile:tcompilerppufile);
  509. constructor createlong(l : aint);
  510. constructor loadlong(ppufile:tcompilerppufile);
  511. constructor createansi;
  512. constructor loadansi(ppufile:tcompilerppufile);
  513. constructor createwide;
  514. constructor loadwide(ppufile:tcompilerppufile);
  515. constructor createunicode;
  516. constructor loadunicode(ppufile:tcompilerppufile);
  517. function getcopy : tstoreddef;override;
  518. function stringtypname:string;
  519. procedure ppuwrite(ppufile:tcompilerppufile);override;
  520. function GetTypeName:string;override;
  521. function getmangledparaname:string;override;
  522. function is_publishable : boolean;override;
  523. function alignment : shortint;override;
  524. function needs_inittable : boolean;override;
  525. function getvardef:longint;override;
  526. end;
  527. tenumdef = class(tstoreddef)
  528. minval,
  529. maxval : aint;
  530. has_jumps : boolean;
  531. firstenum : tsym; {tenumsym}
  532. basedef : tenumdef;
  533. basedefderef : tderef;
  534. constructor create;
  535. constructor create_subrange(_basedef:tenumdef;_min,_max:aint);
  536. constructor ppuload(ppufile:tcompilerppufile);
  537. function getcopy : tstoreddef;override;
  538. procedure ppuwrite(ppufile:tcompilerppufile);override;
  539. procedure buildderef;override;
  540. procedure deref;override;
  541. procedure derefimpl;override;
  542. function GetTypeName:string;override;
  543. function is_publishable : boolean;override;
  544. procedure calcsavesize;
  545. function packedbitsize: aint; override;
  546. procedure setmax(_max:aint);
  547. procedure setmin(_min:aint);
  548. function min:aint;
  549. function max:aint;
  550. end;
  551. tsetdef = class(tstoreddef)
  552. elementdef : tdef;
  553. elementdefderef : tderef;
  554. setbase,
  555. setmax : aword;
  556. constructor create(def:tdef;low, high : aint);
  557. constructor ppuload(ppufile:tcompilerppufile);
  558. function getcopy : tstoreddef;override;
  559. procedure ppuwrite(ppufile:tcompilerppufile);override;
  560. procedure buildderef;override;
  561. procedure deref;override;
  562. function GetTypeName:string;override;
  563. function is_publishable : boolean;override;
  564. end;
  565. var
  566. current_objectdef : tobjectdef; { used for private functions check !! }
  567. { default types }
  568. generrordef, { error in definition }
  569. voidpointertype, { pointer for Void-pointeddef }
  570. charpointertype, { pointer for Char-pointeddef }
  571. widecharpointertype, { pointer for WideChar-pointeddef }
  572. voidfarpointertype,
  573. cundefinedtype,
  574. cformaltype, { unique formal definition }
  575. ctypedformaltype, { unique typed formal definition }
  576. voidtype, { Void (procedure) }
  577. cchartype, { Char }
  578. cwidechartype, { WideChar }
  579. booltype, { boolean type }
  580. bool8type,
  581. bool16type,
  582. bool32type,
  583. bool64type, { implement me }
  584. u8inttype, { 8-Bit unsigned integer }
  585. s8inttype, { 8-Bit signed integer }
  586. u16inttype, { 16-Bit unsigned integer }
  587. s16inttype, { 16-Bit signed integer }
  588. u32inttype, { 32-Bit unsigned integer }
  589. s32inttype, { 32-Bit signed integer }
  590. u64inttype, { 64-bit unsigned integer }
  591. s64inttype, { 64-bit signed integer }
  592. s32floattype, { pointer for realconstn }
  593. s64floattype, { pointer for realconstn }
  594. s80floattype, { pointer to type of temp. floats }
  595. s64currencytype, { pointer to a currency type }
  596. cshortstringtype, { pointer to type of short string const }
  597. clongstringtype, { pointer to type of long string const }
  598. cansistringtype, { pointer to type of ansi string const }
  599. cwidestringtype, { pointer to type of wide string const }
  600. cunicodestringtype,
  601. openshortstringtype, { pointer to type of an open shortstring,
  602. needed for readln() }
  603. openchararraytype, { pointer to type of an open array of char,
  604. needed for readln() }
  605. cfiletype, { get the same definition for all file }
  606. { used for stabs }
  607. methodpointertype, { typecasting of methodpointers to extract self }
  608. hresultdef,
  609. { we use only one variant def for every variant class }
  610. cvarianttype,
  611. colevarianttype,
  612. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  613. sinttype,
  614. uinttype,
  615. { unsigned and signed ord type with the same size as a pointer }
  616. ptruinttype,
  617. ptrsinttype,
  618. { several types to simulate more or less C++ objects for GDB }
  619. vmttype,
  620. vmtarraytype,
  621. pvmttype : tdef; { type of classrefs, used for stabs }
  622. { pointer to the anchestor of all classes }
  623. class_tobject : tobjectdef;
  624. { pointer to the ancestor of all COM interfaces }
  625. interface_iunknown : tobjectdef;
  626. { pointer to the TGUID type
  627. of all interfaces }
  628. rec_tguid : trecorddef;
  629. { Objective-C base types }
  630. objc_metaclasstype,
  631. objc_superclasstype,
  632. objc_idtype,
  633. objc_seltype : tpointerdef;
  634. { base type of @protocol(protocolname) Objective-C statements }
  635. objc_protocoltype : tobjectdef;
  636. const
  637. {$ifdef i386}
  638. pbestrealtype : ^tdef = @s80floattype;
  639. {$endif}
  640. {$ifdef x86_64}
  641. pbestrealtype : ^tdef = @s80floattype;
  642. {$endif}
  643. {$ifdef m68k}
  644. pbestrealtype : ^tdef = @s64floattype;
  645. {$endif}
  646. {$ifdef alpha}
  647. pbestrealtype : ^tdef = @s64floattype;
  648. {$endif}
  649. {$ifdef powerpc}
  650. pbestrealtype : ^tdef = @s64floattype;
  651. {$endif}
  652. {$ifdef POWERPC64}
  653. pbestrealtype : ^tdef = @s64floattype;
  654. {$endif}
  655. {$ifdef ia64}
  656. pbestrealtype : ^tdef = @s64floattype;
  657. {$endif}
  658. {$ifdef SPARC}
  659. pbestrealtype : ^tdef = @s64floattype;
  660. {$endif SPARC}
  661. {$ifdef vis}
  662. pbestrealtype : ^tdef = @s64floattype;
  663. {$endif vis}
  664. {$ifdef ARM}
  665. pbestrealtype : ^tdef = @s64floattype;
  666. {$endif ARM}
  667. {$ifdef MIPS}
  668. pbestrealtype : ^tdef = @s64floattype;
  669. {$endif MIPS}
  670. {$ifdef AVR}
  671. pbestrealtype : ^tdef = @s64floattype;
  672. {$endif AVR}
  673. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  674. { should be in the types unit, but the types unit uses the node stuff :( }
  675. function is_interfacecom(def: tdef): boolean;
  676. function is_interfacecorba(def: tdef): boolean;
  677. function is_interface(def: tdef): boolean;
  678. function is_dispinterface(def: tdef): boolean;
  679. function is_object(def: tdef): boolean;
  680. function is_class(def: tdef): boolean;
  681. function is_cppclass(def: tdef): boolean;
  682. function is_objcclass(def: tdef): boolean;
  683. function is_objcclassref(def: tdef): boolean;
  684. function is_objcprotocol(def: tdef): boolean;
  685. function is_objc_class_or_protocol(def: tdef): boolean;
  686. function is_class_or_interface(def: tdef): boolean;
  687. function is_class_or_interface_or_objc(def: tdef): boolean;
  688. function is_class_or_interface_or_object(def: tdef): boolean;
  689. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  690. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  691. procedure loadobjctypes;
  692. {$ifdef x86}
  693. function use_sse(def : tdef) : boolean;
  694. {$endif x86}
  695. implementation
  696. uses
  697. SysUtils,
  698. cutils,
  699. { global }
  700. verbose,
  701. { target }
  702. systems,aasmcpu,paramgr,
  703. { symtable }
  704. symsym,symtable,symutil,defutil,
  705. { module }
  706. fmodule,
  707. { other }
  708. gendef,
  709. fpccrc
  710. ;
  711. {****************************************************************************
  712. Constants
  713. ****************************************************************************}
  714. const
  715. varempty = 0;
  716. varnull = 1;
  717. varsmallint = 2;
  718. varinteger = 3;
  719. varsingle = 4;
  720. vardouble = 5;
  721. varcurrency = 6;
  722. vardate = 7;
  723. varolestr = 8;
  724. vardispatch = 9;
  725. varerror = 10;
  726. varboolean = 11;
  727. varvariant = 12;
  728. varunknown = 13;
  729. vardecimal = 14;
  730. varshortint = 16;
  731. varbyte = 17;
  732. varword = 18;
  733. varlongword = 19;
  734. varint64 = 20;
  735. varqword = 21;
  736. varunicodestr = 22;
  737. varUndefined = -1;
  738. varstrarg = $48;
  739. varstring = $100;
  740. varany = $101;
  741. vardefmask = $fff;
  742. vararray = $2000;
  743. varbyref = $4000;
  744. {****************************************************************************
  745. Helpers
  746. ****************************************************************************}
  747. function make_mangledname(const typeprefix:string;st:TSymtable;const suffix:string):string;
  748. var
  749. s,hs,
  750. prefix : string;
  751. oldlen,
  752. newlen,
  753. i : longint;
  754. crc : dword;
  755. hp : tparavarsym;
  756. begin
  757. prefix:='';
  758. if not assigned(st) then
  759. internalerror(200204212);
  760. { sub procedures }
  761. while (st.symtabletype=localsymtable) do
  762. begin
  763. if st.defowner.typ<>procdef then
  764. internalerror(200204173);
  765. { Add the full mangledname of procedure to prevent
  766. conflicts with 2 overloads having both a nested procedure
  767. with the same name, see tb0314 (PFV) }
  768. s:=tprocdef(st.defowner).procsym.name;
  769. oldlen:=length(s);
  770. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  771. begin
  772. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  773. if not(vo_is_hidden_para in hp.varoptions) then
  774. s:=s+'$'+hp.vardef.mangledparaname;
  775. end;
  776. if not is_void(tprocdef(st.defowner).returndef) then
  777. s:=s+'$$'+tprocdef(st.defowner).returndef.mangledparaname;
  778. newlen:=length(s);
  779. { Replace with CRC if the parameter line is very long }
  780. if (newlen-oldlen>12) and
  781. ((newlen+length(prefix)>128) or (newlen-oldlen>32)) then
  782. begin
  783. crc:=0;
  784. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  785. begin
  786. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  787. if not(vo_is_hidden_para in hp.varoptions) then
  788. begin
  789. hs:=hp.vardef.mangledparaname;
  790. crc:=UpdateCrc32(crc,hs[1],length(hs));
  791. end;
  792. end;
  793. hs:=hp.vardef.mangledparaname;
  794. crc:=UpdateCrc32(crc,hs[1],length(hs));
  795. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  796. end;
  797. if prefix<>'' then
  798. prefix:=s+'_'+prefix
  799. else
  800. prefix:=s;
  801. st:=st.defowner.owner;
  802. end;
  803. { object/classes symtable }
  804. if (st.symtabletype=ObjectSymtable) then
  805. begin
  806. if st.defowner.typ<>objectdef then
  807. internalerror(200204174);
  808. prefix:=tobjectdef(st.defowner).objname^+'_$_'+prefix;
  809. st:=st.defowner.owner;
  810. end;
  811. { symtable must now be static or global }
  812. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  813. internalerror(200204175);
  814. result:='';
  815. if typeprefix<>'' then
  816. result:=result+typeprefix+'_';
  817. { Add P$ for program, which can have the same name as
  818. a unit }
  819. if (TSymtable(main_module.localsymtable)=st) and
  820. (not main_module.is_unit) then
  821. result:=result+'P$'+st.name^
  822. else
  823. result:=result+st.name^;
  824. if prefix<>'' then
  825. result:=result+'_'+prefix;
  826. if suffix<>'' then
  827. result:=result+'_'+suffix;
  828. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  829. { Further, the Mac OS X 10.5 linker does not consider symbols which do not }
  830. { start with '_' as regular symbols (it does not generate N_GSYM entries }
  831. { those in the debug map, leading to troubles with dsymutil). So always }
  832. { add an underscore on darwin. }
  833. if (target_info.system in systems_darwin) then
  834. result := '_' + result;
  835. end;
  836. {****************************************************************************
  837. TDEF (base class for definitions)
  838. ****************************************************************************}
  839. constructor tstoreddef.create(dt:tdeftyp);
  840. var
  841. insertstack : psymtablestackitem;
  842. begin
  843. inherited create(dt);
  844. savesize := 0;
  845. {$ifdef EXTDEBUG}
  846. fileinfo := current_filepos;
  847. {$endif}
  848. generictokenbuf:=nil;
  849. genericdef:=nil;
  850. { Don't register forwarddefs, they are disposed at the
  851. end of an type block }
  852. if (dt=forwarddef) then
  853. exit;
  854. { Register in current_module }
  855. if assigned(current_module) then
  856. begin
  857. current_module.deflist.Add(self);
  858. DefId:=current_module.deflist.Count-1;
  859. end;
  860. { Register in symtable stack }
  861. if assigned(symtablestack) then
  862. begin
  863. insertstack:=symtablestack.stack;
  864. while assigned(insertstack) and
  865. (insertstack^.symtable.symtabletype=withsymtable) do
  866. insertstack:=insertstack^.next;
  867. if not assigned(insertstack) then
  868. internalerror(200602044);
  869. insertstack^.symtable.insertdef(self);
  870. end;
  871. end;
  872. destructor tstoreddef.destroy;
  873. begin
  874. { Direct calls are not allowed, use symtable.deletedef() }
  875. if assigned(owner) then
  876. internalerror(200612311);
  877. if assigned(generictokenbuf) then
  878. begin
  879. generictokenbuf.free;
  880. generictokenbuf:=nil;
  881. end;
  882. inherited destroy;
  883. end;
  884. constructor tstoreddef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  885. var
  886. sizeleft,i : longint;
  887. buf : array[0..255] of byte;
  888. begin
  889. inherited create(dt);
  890. DefId:=ppufile.getlongint;
  891. current_module.deflist[DefId]:=self;
  892. {$ifdef EXTDEBUG}
  893. fillchar(fileinfo,sizeof(fileinfo),0);
  894. {$endif}
  895. { load }
  896. ppufile.getderef(typesymderef);
  897. ppufile.getsmallset(defoptions);
  898. ppufile.getsmallset(defstates);
  899. if df_generic in defoptions then
  900. begin
  901. sizeleft:=ppufile.getlongint;
  902. initgeneric;
  903. while sizeleft>0 do
  904. begin
  905. if sizeleft>sizeof(buf) then
  906. i:=sizeof(buf)
  907. else
  908. i:=sizeleft;
  909. ppufile.getdata(buf,i);
  910. generictokenbuf.write(buf,i);
  911. dec(sizeleft,i);
  912. end;
  913. end;
  914. if df_specialization in defoptions then
  915. ppufile.getderef(genericdefderef);
  916. end;
  917. function Tstoreddef.rtti_mangledname(rt:trttitype):string;
  918. var
  919. prefix : string[4];
  920. begin
  921. if rt=fullrtti then
  922. begin
  923. prefix:='RTTI';
  924. include(defstates,ds_rtti_table_used);
  925. end
  926. else
  927. begin
  928. prefix:='INIT';
  929. include(defstates,ds_init_table_used);
  930. end;
  931. if assigned(typesym) and
  932. (owner.symtabletype in [staticsymtable,globalsymtable]) then
  933. result:=make_mangledname(prefix,owner,typesym.name)
  934. else
  935. result:=make_mangledname(prefix,findunitsymtable(owner),'DEF'+tostr(DefId))
  936. end;
  937. procedure Tstoreddef.reset;
  938. begin
  939. end;
  940. function tstoreddef.getcopy : tstoreddef;
  941. begin
  942. Message(sym_e_cant_create_unique_type);
  943. getcopy:=terrordef.create;
  944. end;
  945. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  946. var
  947. sizeleft,i : longint;
  948. buf : array[0..255] of byte;
  949. oldintfcrc : boolean;
  950. begin
  951. ppufile.putlongint(DefId);
  952. ppufile.putderef(typesymderef);
  953. ppufile.putsmallset(defoptions);
  954. oldintfcrc:=ppufile.do_crc;
  955. ppufile.do_crc:=false;
  956. ppufile.putsmallset(defstates);
  957. if df_generic in defoptions then
  958. begin
  959. if assigned(generictokenbuf) then
  960. begin
  961. sizeleft:=generictokenbuf.size;
  962. generictokenbuf.seek(0);
  963. end
  964. else
  965. sizeleft:=0;
  966. ppufile.putlongint(sizeleft);
  967. while sizeleft>0 do
  968. begin
  969. if sizeleft>sizeof(buf) then
  970. i:=sizeof(buf)
  971. else
  972. i:=sizeleft;
  973. generictokenbuf.read(buf,i);
  974. ppufile.putdata(buf,i);
  975. dec(sizeleft,i);
  976. end;
  977. end;
  978. ppufile.do_crc:=oldintfcrc;
  979. if df_specialization in defoptions then
  980. ppufile.putderef(genericdefderef);
  981. end;
  982. procedure tstoreddef.buildderef;
  983. begin
  984. typesymderef.build(typesym);
  985. genericdefderef.build(genericdef);
  986. end;
  987. procedure tstoreddef.buildderefimpl;
  988. begin
  989. end;
  990. procedure tstoreddef.deref;
  991. begin
  992. typesym:=ttypesym(typesymderef.resolve);
  993. if df_specialization in defoptions then
  994. genericdef:=tstoreddef(genericdefderef.resolve);
  995. end;
  996. procedure tstoreddef.derefimpl;
  997. begin
  998. end;
  999. function tstoreddef.size : aint;
  1000. begin
  1001. size:=savesize;
  1002. end;
  1003. function tstoreddef.getvardef:longint;
  1004. begin
  1005. result:=varUndefined;
  1006. end;
  1007. function tstoreddef.alignment : shortint;
  1008. begin
  1009. { natural alignment by default }
  1010. alignment:=size_2_align(savesize);
  1011. { can happen if savesize = 0, e.g. for voiddef or
  1012. an empty record
  1013. }
  1014. if (alignment=0) then
  1015. alignment:=1;
  1016. end;
  1017. { returns true, if the definition can be published }
  1018. function tstoreddef.is_publishable : boolean;
  1019. begin
  1020. is_publishable:=false;
  1021. end;
  1022. { needs an init table }
  1023. function tstoreddef.needs_inittable : boolean;
  1024. begin
  1025. needs_inittable:=false;
  1026. end;
  1027. function tstoreddef.is_intregable : boolean;
  1028. var
  1029. recsize,temp: longint;
  1030. begin
  1031. is_intregable:=false;
  1032. case typ of
  1033. orddef,
  1034. pointerdef,
  1035. enumdef,
  1036. classrefdef:
  1037. is_intregable:=true;
  1038. procvardef :
  1039. is_intregable:=not(po_methodpointer in tprocvardef(self).procoptions);
  1040. objectdef:
  1041. is_intregable:=(is_class_or_interface_or_objc(self)) and not needs_inittable;
  1042. setdef:
  1043. is_intregable:=is_smallset(self);
  1044. recorddef:
  1045. begin
  1046. recsize:=size;
  1047. is_intregable:=
  1048. ispowerof2(recsize,temp) and
  1049. (recsize <= sizeof(aint));
  1050. end;
  1051. end;
  1052. end;
  1053. function tstoreddef.is_fpuregable : boolean;
  1054. begin
  1055. {$ifdef x86}
  1056. result:=use_sse(self);
  1057. {$else x86}
  1058. result:=(typ=floatdef) and not(cs_fp_emulation in current_settings.moduleswitches);
  1059. {$endif x86}
  1060. end;
  1061. procedure tstoreddef.initgeneric;
  1062. begin
  1063. if assigned(generictokenbuf) then
  1064. internalerror(200512131);
  1065. generictokenbuf:=tdynamicarray.create(256);
  1066. end;
  1067. {****************************************************************************
  1068. Tstringdef
  1069. ****************************************************************************}
  1070. constructor tstringdef.createshort(l : byte);
  1071. begin
  1072. inherited create(stringdef);
  1073. stringtype:=st_shortstring;
  1074. len:=l;
  1075. savesize:=len+1;
  1076. end;
  1077. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1078. begin
  1079. inherited ppuload(stringdef,ppufile);
  1080. stringtype:=st_shortstring;
  1081. len:=ppufile.getbyte;
  1082. savesize:=len+1;
  1083. end;
  1084. constructor tstringdef.createlong(l : aint);
  1085. begin
  1086. inherited create(stringdef);
  1087. stringtype:=st_longstring;
  1088. len:=l;
  1089. savesize:=sizeof(pint);
  1090. end;
  1091. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1092. begin
  1093. inherited ppuload(stringdef,ppufile);
  1094. stringtype:=st_longstring;
  1095. len:=ppufile.getaint;
  1096. savesize:=sizeof(pint);
  1097. end;
  1098. constructor tstringdef.createansi;
  1099. begin
  1100. inherited create(stringdef);
  1101. stringtype:=st_ansistring;
  1102. len:=-1;
  1103. savesize:=sizeof(pint);
  1104. end;
  1105. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1106. begin
  1107. inherited ppuload(stringdef,ppufile);
  1108. stringtype:=st_ansistring;
  1109. len:=ppufile.getaint;
  1110. savesize:=sizeof(pint);
  1111. end;
  1112. constructor tstringdef.createwide;
  1113. begin
  1114. inherited create(stringdef);
  1115. stringtype:=st_widestring;
  1116. len:=-1;
  1117. savesize:=sizeof(pint);
  1118. end;
  1119. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1120. begin
  1121. inherited ppuload(stringdef,ppufile);
  1122. stringtype:=st_widestring;
  1123. len:=ppufile.getaint;
  1124. savesize:=sizeof(pint);
  1125. end;
  1126. constructor tstringdef.createunicode;
  1127. begin
  1128. inherited create(stringdef);
  1129. stringtype:=st_unicodestring;
  1130. len:=-1;
  1131. savesize:=sizeof(pint);
  1132. end;
  1133. constructor tstringdef.loadunicode(ppufile:tcompilerppufile);
  1134. begin
  1135. inherited ppuload(stringdef,ppufile);
  1136. stringtype:=st_unicodestring;
  1137. len:=ppufile.getaint;
  1138. savesize:=sizeof(pint);
  1139. end;
  1140. function tstringdef.getcopy : tstoreddef;
  1141. begin
  1142. result:=tstringdef.create(typ);
  1143. result.typ:=stringdef;
  1144. tstringdef(result).stringtype:=stringtype;
  1145. tstringdef(result).len:=len;
  1146. tstringdef(result).savesize:=savesize;
  1147. end;
  1148. function tstringdef.stringtypname:string;
  1149. const
  1150. typname:array[tstringtype] of string[10]=(
  1151. 'shortstr','longstr','ansistr','widestr','unicodestr'
  1152. );
  1153. begin
  1154. stringtypname:=typname[stringtype];
  1155. end;
  1156. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1157. begin
  1158. inherited ppuwrite(ppufile);
  1159. if stringtype=st_shortstring then
  1160. begin
  1161. {$ifdef extdebug}
  1162. if len > 255 then internalerror(12122002);
  1163. {$endif}
  1164. ppufile.putbyte(byte(len))
  1165. end
  1166. else
  1167. ppufile.putaint(len);
  1168. case stringtype of
  1169. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1170. st_longstring : ppufile.writeentry(iblongstringdef);
  1171. st_ansistring : ppufile.writeentry(ibansistringdef);
  1172. st_widestring : ppufile.writeentry(ibwidestringdef);
  1173. st_unicodestring : ppufile.writeentry(ibunicodestringdef);
  1174. end;
  1175. end;
  1176. function tstringdef.needs_inittable : boolean;
  1177. begin
  1178. needs_inittable:=stringtype in [st_ansistring,st_widestring,st_unicodestring];
  1179. end;
  1180. function tstringdef.GetTypeName : string;
  1181. const
  1182. names : array[tstringtype] of string[15] = (
  1183. 'ShortString','LongString','AnsiString','WideString','UnicodeString');
  1184. begin
  1185. GetTypeName:=names[stringtype];
  1186. end;
  1187. function tstringdef.getvardef : longint;
  1188. const
  1189. vardef : array[tstringtype] of longint = (
  1190. varUndefined,varUndefined,varString,varOleStr,varUnicodeStr);
  1191. begin
  1192. result:=vardef[stringtype];
  1193. end;
  1194. function tstringdef.alignment : shortint;
  1195. begin
  1196. case stringtype of
  1197. st_unicodestring,
  1198. st_widestring,
  1199. st_ansistring:
  1200. alignment:=size_2_align(savesize);
  1201. st_longstring,
  1202. st_shortstring:
  1203. { char to string accesses byte 0 and 1 with one word access }
  1204. if (tf_requires_proper_alignment in target_info.flags) or
  1205. { macpas needs an alignment of 2 (MetroWerks compatible) }
  1206. (m_mac in current_settings.modeswitches) then
  1207. alignment:=size_2_align(2)
  1208. else
  1209. alignment:=size_2_align(1);
  1210. else
  1211. internalerror(200412301);
  1212. end;
  1213. end;
  1214. function tstringdef.getmangledparaname : string;
  1215. begin
  1216. getmangledparaname:='STRING';
  1217. end;
  1218. function tstringdef.is_publishable : boolean;
  1219. begin
  1220. is_publishable:=true;
  1221. end;
  1222. {****************************************************************************
  1223. TENUMDEF
  1224. ****************************************************************************}
  1225. constructor tenumdef.create;
  1226. begin
  1227. inherited create(enumdef);
  1228. minval:=0;
  1229. maxval:=0;
  1230. calcsavesize;
  1231. has_jumps:=false;
  1232. basedef:=nil;
  1233. firstenum:=nil;
  1234. end;
  1235. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:aint);
  1236. begin
  1237. inherited create(enumdef);
  1238. minval:=_min;
  1239. maxval:=_max;
  1240. basedef:=_basedef;
  1241. calcsavesize;
  1242. has_jumps:=false;
  1243. firstenum:=basedef.firstenum;
  1244. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1245. firstenum:=tenumsym(firstenum).nextenum;
  1246. end;
  1247. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1248. begin
  1249. inherited ppuload(enumdef,ppufile);
  1250. ppufile.getderef(basedefderef);
  1251. minval:=ppufile.getaint;
  1252. maxval:=ppufile.getaint;
  1253. savesize:=ppufile.getaint;
  1254. has_jumps:=false;
  1255. firstenum:=Nil;
  1256. end;
  1257. function tenumdef.getcopy : tstoreddef;
  1258. begin
  1259. if assigned(basedef) then
  1260. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1261. else
  1262. begin
  1263. result:=tenumdef.create;
  1264. tenumdef(result).minval:=minval;
  1265. tenumdef(result).maxval:=maxval;
  1266. end;
  1267. tenumdef(result).has_jumps:=has_jumps;
  1268. tenumdef(result).firstenum:=firstenum;
  1269. tenumdef(result).basedefderef:=basedefderef;
  1270. end;
  1271. procedure tenumdef.calcsavesize;
  1272. begin
  1273. if (current_settings.packenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1274. savesize:=8
  1275. else
  1276. if (current_settings.packenum=4) or (min<low(smallint)) or (max>high(word)) then
  1277. savesize:=4
  1278. else
  1279. if (current_settings.packenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1280. savesize:=2
  1281. else
  1282. savesize:=1;
  1283. end;
  1284. function tenumdef.packedbitsize: aint;
  1285. var
  1286. sizeval: tconstexprint;
  1287. power: longint;
  1288. begin
  1289. result := 0;
  1290. if (minval >= 0) and
  1291. (maxval <= 1) then
  1292. result := 1
  1293. else
  1294. begin
  1295. if (minval>=0) then
  1296. sizeval:=maxval
  1297. else
  1298. { don't count 0 twice }
  1299. sizeval:=(cutils.max(-minval,maxval)*2)-1;
  1300. { 256 must become 512 etc. }
  1301. nextpowerof2(sizeval+1,power);
  1302. result := power;
  1303. end;
  1304. end;
  1305. procedure tenumdef.setmax(_max:aint);
  1306. begin
  1307. maxval:=_max;
  1308. calcsavesize;
  1309. end;
  1310. procedure tenumdef.setmin(_min:aint);
  1311. begin
  1312. minval:=_min;
  1313. calcsavesize;
  1314. end;
  1315. function tenumdef.min:aint;
  1316. begin
  1317. min:=minval;
  1318. end;
  1319. function tenumdef.max:aint;
  1320. begin
  1321. max:=maxval;
  1322. end;
  1323. procedure tenumdef.buildderef;
  1324. begin
  1325. inherited buildderef;
  1326. basedefderef.build(basedef);
  1327. end;
  1328. procedure tenumdef.deref;
  1329. begin
  1330. inherited deref;
  1331. basedef:=tenumdef(basedefderef.resolve);
  1332. { restart ordering }
  1333. firstenum:=nil;
  1334. end;
  1335. procedure tenumdef.derefimpl;
  1336. begin
  1337. if assigned(basedef) and
  1338. (firstenum=nil) then
  1339. begin
  1340. firstenum:=basedef.firstenum;
  1341. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1342. firstenum:=tenumsym(firstenum).nextenum;
  1343. end;
  1344. end;
  1345. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1346. begin
  1347. inherited ppuwrite(ppufile);
  1348. ppufile.putderef(basedefderef);
  1349. ppufile.putaint(min);
  1350. ppufile.putaint(max);
  1351. ppufile.putaint(savesize);
  1352. ppufile.writeentry(ibenumdef);
  1353. end;
  1354. function tenumdef.is_publishable : boolean;
  1355. begin
  1356. is_publishable:=true;
  1357. end;
  1358. function tenumdef.GetTypeName : string;
  1359. begin
  1360. GetTypeName:='<enumeration type>';
  1361. end;
  1362. {****************************************************************************
  1363. TORDDEF
  1364. ****************************************************************************}
  1365. constructor torddef.create(t : tordtype;v,b : TConstExprInt);
  1366. begin
  1367. inherited create(orddef);
  1368. low:=v;
  1369. high:=b;
  1370. ordtype:=t;
  1371. setsize;
  1372. end;
  1373. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1374. begin
  1375. inherited ppuload(orddef,ppufile);
  1376. ordtype:=tordtype(ppufile.getbyte);
  1377. low:=ppufile.getexprint;
  1378. high:=ppufile.getexprint;
  1379. setsize;
  1380. end;
  1381. function torddef.getcopy : tstoreddef;
  1382. begin
  1383. result:=torddef.create(ordtype,low,high);
  1384. result.typ:=orddef;
  1385. torddef(result).low:=low;
  1386. torddef(result).high:=high;
  1387. torddef(result).ordtype:=ordtype;
  1388. torddef(result).savesize:=savesize;
  1389. end;
  1390. function torddef.alignment:shortint;
  1391. begin
  1392. if (target_info.system in [system_i386_darwin,system_arm_darwin]) and
  1393. (ordtype in [s64bit,u64bit]) then
  1394. result := 4
  1395. else
  1396. result := inherited alignment;
  1397. end;
  1398. procedure torddef.setsize;
  1399. const
  1400. sizetbl : array[tordtype] of longint = (
  1401. 0,
  1402. 1,2,4,8,
  1403. 1,2,4,8,
  1404. 1,1,2,4,8,
  1405. 1,2,8
  1406. );
  1407. begin
  1408. savesize:=sizetbl[ordtype];
  1409. end;
  1410. function torddef.packedbitsize: aint;
  1411. var
  1412. sizeval: tconstexprint;
  1413. power: longint;
  1414. begin
  1415. result := 0;
  1416. if ordtype = uvoid then
  1417. exit;
  1418. if (ordtype = u64bit) or
  1419. ((ordtype = s64bit) and
  1420. ((low <= (system.low(int64) div 2)) or
  1421. (high > (system.high(int64) div 2)))) then
  1422. result := 64
  1423. else if (low >= 0) and
  1424. (high <= 1) then
  1425. result := 1
  1426. else
  1427. begin
  1428. if (low>=0) then
  1429. sizeval:=high
  1430. else
  1431. { don't count 0 twice }
  1432. sizeval:=(cutils.max(-low,high)*2)-1;
  1433. { 256 must become 512 etc. }
  1434. nextpowerof2(sizeval+1,power);
  1435. result := power;
  1436. end;
  1437. end;
  1438. function torddef.getvardef : longint;
  1439. const
  1440. basetype2vardef : array[tordtype] of longint = (
  1441. varUndefined,
  1442. varbyte,varqword,varlongword,varqword,
  1443. varshortint,varsmallint,varinteger,varint64,
  1444. varboolean,varboolean,varboolean,varUndefined,varUndefined,
  1445. varUndefined,varUndefined,varCurrency);
  1446. begin
  1447. result:=basetype2vardef[ordtype];
  1448. end;
  1449. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1450. begin
  1451. inherited ppuwrite(ppufile);
  1452. ppufile.putbyte(byte(ordtype));
  1453. ppufile.putexprint(low);
  1454. ppufile.putexprint(high);
  1455. ppufile.writeentry(iborddef);
  1456. end;
  1457. function torddef.is_publishable : boolean;
  1458. begin
  1459. is_publishable:=(ordtype<>uvoid);
  1460. end;
  1461. function torddef.GetTypeName : string;
  1462. const
  1463. names : array[tordtype] of string[20] = (
  1464. 'untyped',
  1465. 'Byte','Word','DWord','QWord',
  1466. 'ShortInt','SmallInt','LongInt','Int64',
  1467. 'Boolean','ByteBool','WordBool','LongBool','QWordBool',
  1468. 'Char','WideChar','Currency');
  1469. begin
  1470. GetTypeName:=names[ordtype];
  1471. end;
  1472. {****************************************************************************
  1473. TFLOATDEF
  1474. ****************************************************************************}
  1475. constructor tfloatdef.create(t : tfloattype);
  1476. begin
  1477. inherited create(floatdef);
  1478. floattype:=t;
  1479. setsize;
  1480. end;
  1481. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1482. begin
  1483. inherited ppuload(floatdef,ppufile);
  1484. floattype:=tfloattype(ppufile.getbyte);
  1485. setsize;
  1486. end;
  1487. function tfloatdef.getcopy : tstoreddef;
  1488. begin
  1489. result:=tfloatdef.create(floattype);
  1490. result.typ:=floatdef;
  1491. tfloatdef(result).savesize:=savesize;
  1492. end;
  1493. function tfloatdef.alignment:shortint;
  1494. begin
  1495. if (target_info.system in [system_i386_darwin,system_arm_darwin]) then
  1496. case floattype of
  1497. s80real : result:=16;
  1498. s64real,
  1499. s64currency,
  1500. s64comp : result:=4;
  1501. else
  1502. result := inherited alignment;
  1503. end
  1504. else
  1505. result := inherited alignment;
  1506. end;
  1507. procedure tfloatdef.setsize;
  1508. begin
  1509. case floattype of
  1510. s32real : savesize:=4;
  1511. s80real : savesize:=10;
  1512. s64real,
  1513. s64currency,
  1514. s64comp : savesize:=8;
  1515. else
  1516. savesize:=0;
  1517. end;
  1518. end;
  1519. function tfloatdef.getvardef : longint;
  1520. const
  1521. floattype2vardef : array[tfloattype] of longint = (
  1522. varSingle,varDouble,varUndefined,
  1523. varUndefined,varCurrency,varUndefined);
  1524. begin
  1525. if (upper(typename)='TDATETIME') and
  1526. assigned(owner) and
  1527. assigned(owner.name) and
  1528. (owner.name^='SYSTEM') then
  1529. result:=varDate
  1530. else
  1531. result:=floattype2vardef[floattype];
  1532. end;
  1533. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1534. begin
  1535. inherited ppuwrite(ppufile);
  1536. ppufile.putbyte(byte(floattype));
  1537. ppufile.writeentry(ibfloatdef);
  1538. end;
  1539. function tfloatdef.is_publishable : boolean;
  1540. begin
  1541. is_publishable:=true;
  1542. end;
  1543. function tfloatdef.GetTypeName : string;
  1544. const
  1545. names : array[tfloattype] of string[20] = (
  1546. 'Single','Double','Extended','Comp','Currency','Float128');
  1547. begin
  1548. GetTypeName:=names[floattype];
  1549. end;
  1550. {****************************************************************************
  1551. TFILEDEF
  1552. ****************************************************************************}
  1553. constructor tfiledef.createtext;
  1554. begin
  1555. inherited create(filedef);
  1556. filetyp:=ft_text;
  1557. typedfiledef:=nil;
  1558. setsize;
  1559. end;
  1560. constructor tfiledef.createuntyped;
  1561. begin
  1562. inherited create(filedef);
  1563. filetyp:=ft_untyped;
  1564. typedfiledef:=nil;
  1565. setsize;
  1566. end;
  1567. constructor tfiledef.createtyped(def:tdef);
  1568. begin
  1569. inherited create(filedef);
  1570. filetyp:=ft_typed;
  1571. typedfiledef:=def;
  1572. setsize;
  1573. end;
  1574. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1575. begin
  1576. inherited ppuload(filedef,ppufile);
  1577. filetyp:=tfiletyp(ppufile.getbyte);
  1578. if filetyp=ft_typed then
  1579. ppufile.getderef(typedfiledefderef)
  1580. else
  1581. typedfiledef:=nil;
  1582. setsize;
  1583. end;
  1584. function tfiledef.getcopy : tstoreddef;
  1585. begin
  1586. case filetyp of
  1587. ft_typed:
  1588. result:=tfiledef.createtyped(typedfiledef);
  1589. ft_untyped:
  1590. result:=tfiledef.createuntyped;
  1591. ft_text:
  1592. result:=tfiledef.createtext;
  1593. else
  1594. internalerror(2004121201);
  1595. end;
  1596. end;
  1597. procedure tfiledef.buildderef;
  1598. begin
  1599. inherited buildderef;
  1600. if filetyp=ft_typed then
  1601. typedfiledefderef.build(typedfiledef);
  1602. end;
  1603. procedure tfiledef.deref;
  1604. begin
  1605. inherited deref;
  1606. if filetyp=ft_typed then
  1607. typedfiledef:=tdef(typedfiledefderef.resolve);
  1608. end;
  1609. procedure tfiledef.setsize;
  1610. begin
  1611. {$ifdef cpu64bitaddr}
  1612. case filetyp of
  1613. ft_text :
  1614. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1615. savesize:=632{+8}
  1616. else
  1617. savesize:=628{+8};
  1618. ft_typed,
  1619. ft_untyped :
  1620. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1621. savesize:=372
  1622. else
  1623. savesize:=368;
  1624. end;
  1625. {$else cpu64bitaddr}
  1626. case filetyp of
  1627. ft_text :
  1628. savesize:=592{+4};
  1629. ft_typed,
  1630. ft_untyped :
  1631. savesize:=332;
  1632. end;
  1633. {$endif cpu64bitaddr}
  1634. end;
  1635. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1636. begin
  1637. inherited ppuwrite(ppufile);
  1638. ppufile.putbyte(byte(filetyp));
  1639. if filetyp=ft_typed then
  1640. ppufile.putderef(typedfiledefderef);
  1641. ppufile.writeentry(ibfiledef);
  1642. end;
  1643. function tfiledef.GetTypeName : string;
  1644. begin
  1645. case filetyp of
  1646. ft_untyped:
  1647. GetTypeName:='File';
  1648. ft_typed:
  1649. GetTypeName:='File Of '+typedfiledef.typename;
  1650. ft_text:
  1651. GetTypeName:='Text'
  1652. end;
  1653. end;
  1654. function tfiledef.getmangledparaname : string;
  1655. begin
  1656. case filetyp of
  1657. ft_untyped:
  1658. getmangledparaname:='FILE';
  1659. ft_typed:
  1660. getmangledparaname:='FILE$OF$'+typedfiledef.mangledparaname;
  1661. ft_text:
  1662. getmangledparaname:='TEXT'
  1663. end;
  1664. end;
  1665. {****************************************************************************
  1666. TVARIANTDEF
  1667. ****************************************************************************}
  1668. constructor tvariantdef.create(v : tvarianttype);
  1669. begin
  1670. inherited create(variantdef);
  1671. varianttype:=v;
  1672. setsize;
  1673. end;
  1674. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1675. begin
  1676. inherited ppuload(variantdef,ppufile);
  1677. varianttype:=tvarianttype(ppufile.getbyte);
  1678. setsize;
  1679. end;
  1680. function tvariantdef.getcopy : tstoreddef;
  1681. begin
  1682. result:=tvariantdef.create(varianttype);
  1683. end;
  1684. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1685. begin
  1686. inherited ppuwrite(ppufile);
  1687. ppufile.putbyte(byte(varianttype));
  1688. ppufile.writeentry(ibvariantdef);
  1689. end;
  1690. function tvariantdef.getvardef : longint;
  1691. begin
  1692. Result:=varVariant;
  1693. end;
  1694. procedure tvariantdef.setsize;
  1695. begin
  1696. {$ifdef cpu64bitaddr}
  1697. savesize:=24;
  1698. {$else cpu64bitaddr}
  1699. savesize:=16;
  1700. {$endif cpu64bitaddr}
  1701. end;
  1702. function tvariantdef.GetTypeName : string;
  1703. begin
  1704. case varianttype of
  1705. vt_normalvariant:
  1706. GetTypeName:='Variant';
  1707. vt_olevariant:
  1708. GetTypeName:='OleVariant';
  1709. end;
  1710. end;
  1711. function tvariantdef.needs_inittable : boolean;
  1712. begin
  1713. needs_inittable:=true;
  1714. end;
  1715. function tvariantdef.is_publishable : boolean;
  1716. begin
  1717. is_publishable:=true;
  1718. end;
  1719. {****************************************************************************
  1720. TABSTRACtpointerdef
  1721. ****************************************************************************}
  1722. constructor tabstractpointerdef.create(dt:tdeftyp;def:tdef);
  1723. begin
  1724. inherited create(dt);
  1725. pointeddef:=def;
  1726. savesize:=sizeof(pint);
  1727. end;
  1728. constructor tabstractpointerdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  1729. begin
  1730. inherited ppuload(dt,ppufile);
  1731. ppufile.getderef(pointeddefderef);
  1732. savesize:=sizeof(pint);
  1733. end;
  1734. procedure tabstractpointerdef.buildderef;
  1735. begin
  1736. inherited buildderef;
  1737. pointeddefderef.build(pointeddef);
  1738. end;
  1739. procedure tabstractpointerdef.deref;
  1740. begin
  1741. inherited deref;
  1742. pointeddef:=tdef(pointeddefderef.resolve);
  1743. end;
  1744. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1745. begin
  1746. inherited ppuwrite(ppufile);
  1747. ppufile.putderef(pointeddefderef);
  1748. end;
  1749. {****************************************************************************
  1750. tpointerdef
  1751. ****************************************************************************}
  1752. constructor tpointerdef.create(def:tdef);
  1753. begin
  1754. inherited create(pointerdef,def);
  1755. is_far:=false;
  1756. end;
  1757. constructor tpointerdef.createfar(def:tdef);
  1758. begin
  1759. inherited create(pointerdef,def);
  1760. is_far:=true;
  1761. end;
  1762. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1763. begin
  1764. inherited ppuload(pointerdef,ppufile);
  1765. is_far:=(ppufile.getbyte<>0);
  1766. end;
  1767. function tpointerdef.getcopy : tstoreddef;
  1768. begin
  1769. result:=tpointerdef.create(pointeddef);
  1770. tpointerdef(result).is_far:=is_far;
  1771. tpointerdef(result).savesize:=savesize;
  1772. end;
  1773. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1774. begin
  1775. inherited ppuwrite(ppufile);
  1776. ppufile.putbyte(byte(is_far));
  1777. ppufile.writeentry(ibpointerdef);
  1778. end;
  1779. function tpointerdef.GetTypeName : string;
  1780. begin
  1781. if is_far then
  1782. GetTypeName:='^'+pointeddef.typename+';far'
  1783. else
  1784. GetTypeName:='^'+pointeddef.typename;
  1785. end;
  1786. {****************************************************************************
  1787. TCLASSREFDEF
  1788. ****************************************************************************}
  1789. constructor tclassrefdef.create(def:tdef);
  1790. begin
  1791. inherited create(classrefdef,def);
  1792. end;
  1793. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1794. begin
  1795. inherited ppuload(classrefdef,ppufile);
  1796. end;
  1797. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  1798. begin
  1799. inherited ppuwrite(ppufile);
  1800. ppufile.writeentry(ibclassrefdef);
  1801. end;
  1802. function tclassrefdef.GetTypeName : string;
  1803. begin
  1804. GetTypeName:='Class Of '+pointeddef.typename;
  1805. end;
  1806. function tclassrefdef.is_publishable : boolean;
  1807. begin
  1808. result:=true;
  1809. end;
  1810. function tclassrefdef.rtti_mangledname(rt: trttitype): string;
  1811. begin
  1812. if (tobjectdef(pointeddef).objecttype<>odt_objcclass) then
  1813. result:=inherited rtti_mangledname(rt)
  1814. else
  1815. result:=tobjectdef(pointeddef).rtti_mangledname(objcmetartti);
  1816. end;
  1817. procedure tclassrefdef.reset;
  1818. begin
  1819. tobjectdef(pointeddef).classref_created_in_current_module:=false;
  1820. inherited reset;
  1821. end;
  1822. procedure tclassrefdef.register_created_object_type;
  1823. begin
  1824. tobjectdef(pointeddef).register_created_classref_type;
  1825. end;
  1826. {***************************************************************************
  1827. TSETDEF
  1828. ***************************************************************************}
  1829. constructor tsetdef.create(def:tdef;low, high : aint);
  1830. var
  1831. setallocbits: aint;
  1832. packedsavesize: aint;
  1833. begin
  1834. inherited create(setdef);
  1835. elementdef:=def;
  1836. setmax:=high;
  1837. if (current_settings.setalloc=0) then
  1838. begin
  1839. setbase:=0;
  1840. if (high<32) then
  1841. savesize:=Sizeof(longint)
  1842. else if (high<256) then
  1843. savesize:=32
  1844. else
  1845. savesize:=(high+7) div 8
  1846. end
  1847. else
  1848. begin
  1849. setallocbits:=current_settings.setalloc*8;
  1850. setbase:=low and not(setallocbits-1);
  1851. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  1852. savesize:=packedsavesize;
  1853. if savesize=3 then
  1854. savesize:=4;
  1855. end;
  1856. end;
  1857. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  1858. begin
  1859. inherited ppuload(setdef,ppufile);
  1860. ppufile.getderef(elementdefderef);
  1861. savesize:=ppufile.getaint;
  1862. setbase:=ppufile.getaint;
  1863. setmax:=ppufile.getaint;
  1864. end;
  1865. function tsetdef.getcopy : tstoreddef;
  1866. begin
  1867. result:=tsetdef.create(elementdef,setbase,setmax);
  1868. { the copy might have been created with a different setalloc setting }
  1869. tsetdef(result).savesize:=savesize;
  1870. end;
  1871. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  1872. begin
  1873. inherited ppuwrite(ppufile);
  1874. ppufile.putderef(elementdefderef);
  1875. ppufile.putaint(savesize);
  1876. ppufile.putaint(setbase);
  1877. ppufile.putaint(setmax);
  1878. ppufile.writeentry(ibsetdef);
  1879. end;
  1880. procedure tsetdef.buildderef;
  1881. begin
  1882. inherited buildderef;
  1883. elementdefderef.build(elementdef);
  1884. end;
  1885. procedure tsetdef.deref;
  1886. begin
  1887. inherited deref;
  1888. elementdef:=tdef(elementdefderef.resolve);
  1889. end;
  1890. function tsetdef.is_publishable : boolean;
  1891. begin
  1892. is_publishable:=savesize in [1,2,4];
  1893. end;
  1894. function tsetdef.GetTypeName : string;
  1895. begin
  1896. if assigned(elementdef) then
  1897. GetTypeName:='Set Of '+elementdef.typename
  1898. else
  1899. GetTypeName:='Empty Set';
  1900. end;
  1901. {***************************************************************************
  1902. TFORMALDEF
  1903. ***************************************************************************}
  1904. constructor tformaldef.create(Atyped:boolean);
  1905. begin
  1906. inherited create(formaldef);
  1907. typed:=Atyped;
  1908. savesize:=0;
  1909. end;
  1910. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  1911. begin
  1912. inherited ppuload(formaldef,ppufile);
  1913. typed:=boolean(ppufile.getbyte);
  1914. savesize:=0;
  1915. end;
  1916. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  1917. begin
  1918. inherited ppuwrite(ppufile);
  1919. ppufile.putbyte(byte(typed));
  1920. ppufile.writeentry(ibformaldef);
  1921. end;
  1922. function tformaldef.GetTypeName : string;
  1923. begin
  1924. if typed then
  1925. GetTypeName:='<Typed formal type>'
  1926. else
  1927. GetTypeName:='<Formal type>';
  1928. end;
  1929. {***************************************************************************
  1930. TARRAYDEF
  1931. ***************************************************************************}
  1932. constructor tarraydef.create(l,h : aint;def:tdef);
  1933. begin
  1934. inherited create(arraydef);
  1935. lowrange:=l;
  1936. highrange:=h;
  1937. rangedef:=def;
  1938. _elementdef:=nil;
  1939. arrayoptions:=[];
  1940. end;
  1941. constructor tarraydef.create_from_pointer(def:tdef);
  1942. begin
  1943. { use -1 so that the elecount will not overflow }
  1944. self.create(0,high(aint)-1,s32inttype);
  1945. arrayoptions:=[ado_IsConvertedPointer];
  1946. setelementdef(def);
  1947. end;
  1948. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  1949. begin
  1950. inherited ppuload(arraydef,ppufile);
  1951. { the addresses are calculated later }
  1952. ppufile.getderef(_elementdefderef);
  1953. ppufile.getderef(rangedefderef);
  1954. lowrange:=ppufile.getaint;
  1955. highrange:=ppufile.getaint;
  1956. ppufile.getsmallset(arrayoptions);
  1957. end;
  1958. function tarraydef.getcopy : tstoreddef;
  1959. begin
  1960. result:=tarraydef.create(lowrange,highrange,rangedef);
  1961. tarraydef(result).arrayoptions:=arrayoptions;
  1962. tarraydef(result)._elementdef:=_elementdef;
  1963. end;
  1964. procedure tarraydef.buildderef;
  1965. begin
  1966. inherited buildderef;
  1967. _elementdefderef.build(_elementdef);
  1968. rangedefderef.build(rangedef);
  1969. end;
  1970. procedure tarraydef.deref;
  1971. begin
  1972. inherited deref;
  1973. _elementdef:=tdef(_elementdefderef.resolve);
  1974. rangedef:=tdef(rangedefderef.resolve);
  1975. end;
  1976. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  1977. begin
  1978. inherited ppuwrite(ppufile);
  1979. ppufile.putderef(_elementdefderef);
  1980. ppufile.putderef(rangedefderef);
  1981. ppufile.putaint(lowrange);
  1982. ppufile.putaint(highrange);
  1983. ppufile.putsmallset(arrayoptions);
  1984. ppufile.writeentry(ibarraydef);
  1985. end;
  1986. function tarraydef.elesize : aint;
  1987. begin
  1988. if (ado_IsBitPacked in arrayoptions) then
  1989. internalerror(2006080101);
  1990. if assigned(_elementdef) then
  1991. result:=_elementdef.size
  1992. else
  1993. result:=0;
  1994. end;
  1995. function tarraydef.elepackedbitsize : aint;
  1996. begin
  1997. if not(ado_IsBitPacked in arrayoptions) then
  1998. internalerror(2006080102);
  1999. if assigned(_elementdef) then
  2000. result:=_elementdef.packedbitsize
  2001. else
  2002. result:=0;
  2003. end;
  2004. function tarraydef.elecount : aword;
  2005. var
  2006. qhigh,qlow : qword;
  2007. begin
  2008. if ado_IsDynamicArray in arrayoptions then
  2009. begin
  2010. result:=0;
  2011. exit;
  2012. end;
  2013. if (highrange>0) and (lowrange<0) then
  2014. begin
  2015. qhigh:=highrange;
  2016. qlow:=qword(-lowrange);
  2017. { prevent overflow, return 0 to indicate overflow }
  2018. if qhigh+qlow>qword(high(aint)-1) then
  2019. result:=0
  2020. else
  2021. result:=qhigh+qlow+1;
  2022. end
  2023. else
  2024. result:=int64(highrange)-lowrange+1;
  2025. end;
  2026. function tarraydef.size : aint;
  2027. var
  2028. cachedelecount : aword;
  2029. cachedelesize : aint;
  2030. begin
  2031. if ado_IsDynamicArray in arrayoptions then
  2032. begin
  2033. size:=sizeof(pint);
  2034. exit;
  2035. end;
  2036. { Tarraydef.size may never be called for an open array! }
  2037. if highrange<lowrange then
  2038. internalerror(99080501);
  2039. if not (ado_IsBitPacked in arrayoptions) then
  2040. cachedelesize:=elesize
  2041. else
  2042. cachedelesize := elepackedbitsize;
  2043. cachedelecount:=elecount;
  2044. if (cachedelesize = 0) then
  2045. begin
  2046. size := 0;
  2047. exit;
  2048. end;
  2049. if (cachedelecount = 0) then
  2050. begin
  2051. size := -1;
  2052. exit;
  2053. end;
  2054. { prevent overflow, return -1 to indicate overflow }
  2055. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  2056. if (cachedelecount > aword(high(aint))) or
  2057. ((high(aint) div cachedelesize) < aint(cachedelecount)) or
  2058. { also lowrange*elesize must be < high(aint) to prevent overflow when
  2059. accessing the array, see ncgmem (PFV) }
  2060. ((high(aint) div cachedelesize) < abs(lowrange)) then
  2061. begin
  2062. result:=-1;
  2063. exit;
  2064. end;
  2065. result:=cachedelesize*aint(cachedelecount);
  2066. if (ado_IsBitPacked in arrayoptions) then
  2067. { can't just add 7 and divide by 8, because that may overflow }
  2068. result:=result div 8 + ord((result mod 8)<>0);
  2069. end;
  2070. procedure tarraydef.setelementdef(def:tdef);
  2071. begin
  2072. _elementdef:=def;
  2073. if not(
  2074. (ado_IsDynamicArray in arrayoptions) or
  2075. (ado_IsConvertedPointer in arrayoptions) or
  2076. (highrange<lowrange)
  2077. ) and
  2078. (size=-1) then
  2079. Message(sym_e_segment_too_large);
  2080. end;
  2081. function tarraydef.alignment : shortint;
  2082. begin
  2083. { alignment of dyn. arrays doesn't depend on the element size }
  2084. if (ado_IsDynamicArray in arrayoptions) then
  2085. alignment:=size_2_align(sizeof(pint))
  2086. { alignment is the alignment of the elements }
  2087. else if (elementdef.typ in [arraydef,recorddef,orddef,enumdef,floatdef]) or
  2088. ((elementdef.typ=objectdef) and
  2089. is_object(elementdef)) then
  2090. alignment:=elementdef.alignment
  2091. { alignment is the size of the elements }
  2092. else if not (ado_IsBitPacked in arrayoptions) then
  2093. alignment:=size_2_align(elesize)
  2094. else
  2095. alignment:=packedbitsloadsize(elepackedbitsize);
  2096. end;
  2097. function tarraydef.needs_inittable : boolean;
  2098. begin
  2099. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2100. end;
  2101. function tarraydef.GetTypeName : string;
  2102. begin
  2103. if (ado_IsConstString in arrayoptions) then
  2104. result:='Constant String'
  2105. else if (ado_isarrayofconst in arrayoptions) or
  2106. (ado_isConstructor in arrayoptions) then
  2107. begin
  2108. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2109. GetTypeName:='Array Of Const'
  2110. else
  2111. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2112. end
  2113. else if (ado_IsDynamicArray in arrayoptions) then
  2114. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2115. else if ((highrange=-1) and (lowrange=0)) then
  2116. GetTypeName:='Open Array Of '+elementdef.typename
  2117. else
  2118. begin
  2119. result := '';
  2120. if (ado_IsBitPacked in arrayoptions) then
  2121. result:='Packed ';
  2122. if rangedef.typ=enumdef then
  2123. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2124. else
  2125. result:=result+'Array['+tostr(lowrange)+'..'+
  2126. tostr(highrange)+'] Of '+elementdef.typename
  2127. end;
  2128. end;
  2129. function tarraydef.getmangledparaname : string;
  2130. begin
  2131. if ado_isarrayofconst in arrayoptions then
  2132. getmangledparaname:='array_of_const'
  2133. else
  2134. if ((highrange=-1) and (lowrange=0)) then
  2135. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2136. else
  2137. internalerror(200204176);
  2138. end;
  2139. function tarraydef.is_publishable : boolean;
  2140. begin
  2141. Result:=ado_IsDynamicArray in arrayoptions;
  2142. end;
  2143. {***************************************************************************
  2144. tabstractrecorddef
  2145. ***************************************************************************}
  2146. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2147. begin
  2148. if t=gs_record then
  2149. GetSymtable:=symtable
  2150. else
  2151. GetSymtable:=nil;
  2152. end;
  2153. procedure tabstractrecorddef.reset;
  2154. begin
  2155. inherited reset;
  2156. tstoredsymtable(symtable).reset_all_defs;
  2157. end;
  2158. function tabstractrecorddef.is_packed:boolean;
  2159. begin
  2160. result:=tabstractrecordsymtable(symtable).is_packed;
  2161. end;
  2162. {***************************************************************************
  2163. trecorddef
  2164. ***************************************************************************}
  2165. constructor trecorddef.create(p : TSymtable);
  2166. begin
  2167. inherited create(recorddef);
  2168. symtable:=p;
  2169. { we can own the symtable only if nobody else owns a copy so far }
  2170. if symtable.refcount=1 then
  2171. symtable.defowner:=self;
  2172. isunion:=false;
  2173. end;
  2174. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2175. begin
  2176. inherited ppuload(recorddef,ppufile);
  2177. if df_copied_def in defoptions then
  2178. ppufile.getderef(cloneddefderef)
  2179. else
  2180. begin
  2181. symtable:=trecordsymtable.create(0);
  2182. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2183. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2184. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2185. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2186. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2187. trecordsymtable(symtable).ppuload(ppufile);
  2188. { requires usefieldalignment to be set }
  2189. symtable.defowner:=self;
  2190. end;
  2191. isunion:=false;
  2192. end;
  2193. destructor trecorddef.destroy;
  2194. begin
  2195. if assigned(symtable) then
  2196. begin
  2197. symtable.free;
  2198. symtable:=nil;
  2199. end;
  2200. inherited destroy;
  2201. end;
  2202. function trecorddef.getcopy : tstoreddef;
  2203. begin
  2204. result:=trecorddef.create(symtable.getcopy);
  2205. trecorddef(result).isunion:=isunion;
  2206. include(trecorddef(result).defoptions,df_copied_def);
  2207. end;
  2208. function trecorddef.needs_inittable : boolean;
  2209. begin
  2210. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2211. end;
  2212. procedure trecorddef.buildderef;
  2213. begin
  2214. inherited buildderef;
  2215. if df_copied_def in defoptions then
  2216. cloneddefderef.build(symtable.defowner)
  2217. else
  2218. tstoredsymtable(symtable).buildderef;
  2219. end;
  2220. procedure trecorddef.deref;
  2221. begin
  2222. inherited deref;
  2223. { now dereference the definitions }
  2224. if df_copied_def in defoptions then
  2225. begin
  2226. cloneddef:=trecorddef(cloneddefderef.resolve);
  2227. symtable:=cloneddef.symtable.getcopy;
  2228. end
  2229. else
  2230. tstoredsymtable(symtable).deref;
  2231. { assign TGUID? load only from system unit }
  2232. if not(assigned(rec_tguid)) and
  2233. (upper(typename)='TGUID') and
  2234. assigned(owner) and
  2235. assigned(owner.name) and
  2236. (owner.name^='SYSTEM') then
  2237. rec_tguid:=self;
  2238. end;
  2239. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2240. begin
  2241. inherited ppuwrite(ppufile);
  2242. if df_copied_def in defoptions then
  2243. ppufile.putderef(cloneddefderef)
  2244. else
  2245. begin
  2246. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2247. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2248. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2249. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2250. ppufile.putaint(trecordsymtable(symtable).datasize);
  2251. end;
  2252. ppufile.writeentry(ibrecorddef);
  2253. if not(df_copied_def in defoptions) then
  2254. trecordsymtable(symtable).ppuwrite(ppufile);
  2255. end;
  2256. function trecorddef.size:aint;
  2257. begin
  2258. result:=trecordsymtable(symtable).datasize;
  2259. end;
  2260. function trecorddef.alignment:shortint;
  2261. begin
  2262. alignment:=trecordsymtable(symtable).recordalignment;
  2263. end;
  2264. function trecorddef.padalignment:shortint;
  2265. begin
  2266. padalignment := trecordsymtable(symtable).padalignment;
  2267. end;
  2268. function trecorddef.GetTypeName : string;
  2269. begin
  2270. GetTypeName:='<record type>'
  2271. end;
  2272. {***************************************************************************
  2273. TABSTRACTPROCDEF
  2274. ***************************************************************************}
  2275. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2276. begin
  2277. inherited create(dt);
  2278. parast:=tparasymtable.create(self,level);
  2279. paras:=nil;
  2280. minparacount:=0;
  2281. maxparacount:=0;
  2282. proctypeoption:=potype_none;
  2283. proccalloption:=pocall_none;
  2284. procoptions:=[];
  2285. returndef:=voidtype;
  2286. savesize:=sizeof(pint);
  2287. requiredargarea:=0;
  2288. has_paraloc_info:=false;
  2289. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2290. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2291. end;
  2292. destructor tabstractprocdef.destroy;
  2293. begin
  2294. if assigned(paras) then
  2295. begin
  2296. {$ifdef MEMDEBUG}
  2297. memprocpara.start;
  2298. {$endif MEMDEBUG}
  2299. paras.free;
  2300. paras:=nil;
  2301. {$ifdef MEMDEBUG}
  2302. memprocpara.stop;
  2303. {$endif MEMDEBUG}
  2304. end;
  2305. if assigned(parast) then
  2306. begin
  2307. {$ifdef MEMDEBUG}
  2308. memprocparast.start;
  2309. {$endif MEMDEBUG}
  2310. parast.free;
  2311. parast:=nil;
  2312. {$ifdef MEMDEBUG}
  2313. memprocparast.stop;
  2314. {$endif MEMDEBUG}
  2315. end;
  2316. inherited destroy;
  2317. end;
  2318. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2319. begin
  2320. if (tsym(p).typ<>paravarsym) then
  2321. exit;
  2322. inc(plongint(arg)^);
  2323. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2324. begin
  2325. if not assigned(tparavarsym(p).defaultconstsym) then
  2326. inc(minparacount);
  2327. inc(maxparacount);
  2328. end;
  2329. end;
  2330. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2331. begin
  2332. if (tsym(p).typ<>paravarsym) then
  2333. exit;
  2334. paras.add(p);
  2335. end;
  2336. procedure tabstractprocdef.calcparas;
  2337. var
  2338. paracount : longint;
  2339. begin
  2340. { This can already be assigned when
  2341. we need to reresolve this unit (PFV) }
  2342. if assigned(paras) then
  2343. paras.free;
  2344. paras:=tparalist.create(false);
  2345. paracount:=0;
  2346. minparacount:=0;
  2347. maxparacount:=0;
  2348. parast.SymList.ForEachCall(@count_para,@paracount);
  2349. paras.capacity:=paracount;
  2350. { Insert parameters in table }
  2351. parast.SymList.ForEachCall(@insert_para,nil);
  2352. { Order parameters }
  2353. paras.sortparas;
  2354. end;
  2355. procedure tabstractprocdef.buildderef;
  2356. begin
  2357. { released procdef? }
  2358. if not assigned(parast) then
  2359. exit;
  2360. inherited buildderef;
  2361. returndefderef.build(returndef);
  2362. { parast }
  2363. tparasymtable(parast).buildderef;
  2364. end;
  2365. procedure tabstractprocdef.deref;
  2366. begin
  2367. inherited deref;
  2368. returndef:=tdef(returndefderef.resolve);
  2369. { parast }
  2370. tparasymtable(parast).deref;
  2371. { recalculated parameters }
  2372. calcparas;
  2373. end;
  2374. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2375. var
  2376. b : byte;
  2377. begin
  2378. inherited ppuload(dt,ppufile);
  2379. parast:=nil;
  2380. Paras:=nil;
  2381. minparacount:=0;
  2382. maxparacount:=0;
  2383. ppufile.getderef(returndefderef);
  2384. { TODO: remove fpu_used loading}
  2385. ppufile.getbyte;
  2386. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2387. proccalloption:=tproccalloption(ppufile.getbyte);
  2388. ppufile.getnormalset(procoptions);
  2389. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2390. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2391. if po_explicitparaloc in procoptions then
  2392. begin
  2393. b:=ppufile.getbyte;
  2394. if b<>sizeof(funcretloc[callerside]) then
  2395. internalerror(200411155);
  2396. ppufile.getdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2397. end;
  2398. savesize:=sizeof(pint);
  2399. has_paraloc_info:=(po_explicitparaloc in procoptions);
  2400. end;
  2401. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2402. var
  2403. oldintfcrc : boolean;
  2404. begin
  2405. { released procdef? }
  2406. if not assigned(parast) then
  2407. exit;
  2408. inherited ppuwrite(ppufile);
  2409. ppufile.putderef(returndefderef);
  2410. oldintfcrc:=ppufile.do_interface_crc;
  2411. ppufile.do_interface_crc:=false;
  2412. ppufile.putbyte(0);
  2413. ppufile.putbyte(ord(proctypeoption));
  2414. ppufile.putbyte(ord(proccalloption));
  2415. ppufile.putnormalset(procoptions);
  2416. ppufile.do_interface_crc:=oldintfcrc;
  2417. if (po_explicitparaloc in procoptions) then
  2418. begin
  2419. { Make a 'valid' funcretloc for procedures }
  2420. ppufile.putbyte(sizeof(funcretloc[callerside]));
  2421. ppufile.putdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2422. end;
  2423. end;
  2424. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2425. var
  2426. hs,s : string;
  2427. hp : TParavarsym;
  2428. hpc : tconstsym;
  2429. first : boolean;
  2430. i : integer;
  2431. begin
  2432. s:='';
  2433. first:=true;
  2434. for i:=0 to paras.count-1 do
  2435. begin
  2436. hp:=tparavarsym(paras[i]);
  2437. if not(vo_is_hidden_para in hp.varoptions) or
  2438. (showhidden) then
  2439. begin
  2440. if first then
  2441. begin
  2442. s:=s+'(';
  2443. first:=false;
  2444. end
  2445. else
  2446. s:=s+',';
  2447. if vo_is_hidden_para in hp.varoptions then
  2448. s:=s+'<';
  2449. case hp.varspez of
  2450. vs_var :
  2451. s:=s+'var ';
  2452. vs_const :
  2453. s:=s+'const ';
  2454. vs_out :
  2455. s:=s+'out ';
  2456. end;
  2457. if assigned(hp.vardef.typesym) then
  2458. begin
  2459. hs:=hp.vardef.typesym.realname;
  2460. if hs[1]<>'$' then
  2461. s:=s+hs
  2462. else
  2463. s:=s+hp.vardef.GetTypeName;
  2464. end
  2465. else
  2466. s:=s+hp.vardef.GetTypeName;
  2467. { default value }
  2468. if assigned(hp.defaultconstsym) then
  2469. begin
  2470. hpc:=tconstsym(hp.defaultconstsym);
  2471. hs:='';
  2472. case hpc.consttyp of
  2473. conststring,
  2474. constresourcestring :
  2475. begin
  2476. If hpc.value.len>0 then
  2477. begin
  2478. setLength(hs,hpc.value.len);
  2479. move(hpc.value.valueptr^,hs[1],hpc.value.len);
  2480. end;
  2481. end;
  2482. constreal :
  2483. str(pbestreal(hpc.value.valueptr)^,hs);
  2484. constpointer :
  2485. hs:=tostr(hpc.value.valueordptr);
  2486. constord :
  2487. begin
  2488. if is_boolean(hpc.constdef) then
  2489. begin
  2490. if hpc.value.valueord<>0 then
  2491. hs:='TRUE'
  2492. else
  2493. hs:='FALSE';
  2494. end
  2495. else
  2496. hs:=tostr(hpc.value.valueord);
  2497. end;
  2498. constnil :
  2499. hs:='nil';
  2500. constset :
  2501. hs:='<set>';
  2502. end;
  2503. if hs<>'' then
  2504. s:=s+'="'+hs+'"';
  2505. end;
  2506. if vo_is_hidden_para in hp.varoptions then
  2507. s:=s+'>';
  2508. end;
  2509. end;
  2510. if not first then
  2511. s:=s+')';
  2512. if (po_varargs in procoptions) then
  2513. s:=s+';VarArgs';
  2514. typename_paras:=s;
  2515. end;
  2516. function tabstractprocdef.is_methodpointer:boolean;
  2517. begin
  2518. result:=false;
  2519. end;
  2520. function tabstractprocdef.is_addressonly:boolean;
  2521. begin
  2522. result:=true;
  2523. end;
  2524. {***************************************************************************
  2525. TPROCDEF
  2526. ***************************************************************************}
  2527. constructor tprocdef.create(level:byte);
  2528. begin
  2529. inherited create(procdef,level);
  2530. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2531. _mangledname:=nil;
  2532. fileinfo:=current_filepos;
  2533. extnumber:=$ffff;
  2534. aliasnames:=TCmdStrList.create;
  2535. funcretsym:=nil;
  2536. forwarddef:=true;
  2537. interfacedef:=false;
  2538. hasforward:=false;
  2539. _class := nil;
  2540. import_dll:=nil;
  2541. import_name:=nil;
  2542. import_nr:=0;
  2543. inlininginfo:=nil;
  2544. deprecatedmsg:=nil;
  2545. {$ifdef i386}
  2546. fpu_used:=maxfpuregs;
  2547. {$endif i386}
  2548. end;
  2549. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2550. var
  2551. i,aliasnamescount : longint;
  2552. level : byte;
  2553. begin
  2554. inherited ppuload(procdef,ppufile);
  2555. if po_has_mangledname in procoptions then
  2556. _mangledname:=stringdup(ppufile.getstring)
  2557. else
  2558. _mangledname:=nil;
  2559. extnumber:=ppufile.getword;
  2560. level:=ppufile.getbyte;
  2561. ppufile.getderef(_classderef);
  2562. ppufile.getderef(procsymderef);
  2563. ppufile.getposinfo(fileinfo);
  2564. visibility:=tvisibility(ppufile.getbyte);
  2565. ppufile.getsmallset(symoptions);
  2566. if sp_has_deprecated_msg in symoptions then
  2567. deprecatedmsg:=stringdup(ppufile.getstring)
  2568. else
  2569. deprecatedmsg:=nil;
  2570. {$ifdef powerpc}
  2571. { library symbol for AmigaOS/MorphOS }
  2572. ppufile.getderef(libsymderef);
  2573. {$endif powerpc}
  2574. { import stuff }
  2575. if po_has_importdll in procoptions then
  2576. import_dll:=stringdup(ppufile.getstring)
  2577. else
  2578. import_dll:=nil;
  2579. if po_has_importname in procoptions then
  2580. import_name:=stringdup(ppufile.getstring)
  2581. else
  2582. import_name:=nil;
  2583. import_nr:=ppufile.getword;
  2584. if (po_msgint in procoptions) then
  2585. messageinf.i:=ppufile.getlongint;
  2586. if (po_msgstr in procoptions) then
  2587. messageinf.str:=stringdup(ppufile.getstring);
  2588. if (po_dispid in procoptions) then
  2589. dispid:=ppufile.getlongint;
  2590. { inline stuff }
  2591. if (po_has_inlininginfo in procoptions) then
  2592. begin
  2593. ppufile.getderef(funcretsymderef);
  2594. new(inlininginfo);
  2595. ppufile.getsmallset(inlininginfo^.flags);
  2596. end
  2597. else
  2598. begin
  2599. inlininginfo:=nil;
  2600. funcretsym:=nil;
  2601. end;
  2602. aliasnames:=TCmdStrList.create;
  2603. { count alias names }
  2604. aliasnamescount:=ppufile.getbyte;
  2605. for i:=1 to aliasnamescount do
  2606. aliasnames.insert(ppufile.getstring);
  2607. { load para symtable }
  2608. parast:=tparasymtable.create(self,level);
  2609. tparasymtable(parast).ppuload(ppufile);
  2610. { load local symtable }
  2611. if (po_has_inlininginfo in procoptions) then
  2612. begin
  2613. localst:=tlocalsymtable.create(self,level);
  2614. tlocalsymtable(localst).ppuload(ppufile);
  2615. end
  2616. else
  2617. localst:=nil;
  2618. { inline stuff }
  2619. if (po_has_inlininginfo in procoptions) then
  2620. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2621. { default values for no persistent data }
  2622. if (cs_link_deffile in current_settings.globalswitches) and
  2623. (tf_need_export in target_info.flags) and
  2624. (po_exports in procoptions) then
  2625. deffile.AddExport(mangledname);
  2626. forwarddef:=false;
  2627. interfacedef:=false;
  2628. hasforward:=false;
  2629. { Disable po_has_inlining until the derefimpl is done }
  2630. exclude(procoptions,po_has_inlininginfo);
  2631. {$ifdef i386}
  2632. fpu_used:=maxfpuregs;
  2633. {$endif i386}
  2634. end;
  2635. destructor tprocdef.destroy;
  2636. begin
  2637. aliasnames.free;
  2638. aliasnames:=nil;
  2639. if assigned(localst) and
  2640. (localst.symtabletype<>staticsymtable) then
  2641. begin
  2642. {$ifdef MEMDEBUG}
  2643. memproclocalst.start;
  2644. {$endif MEMDEBUG}
  2645. localst.free;
  2646. localst:=nil;
  2647. {$ifdef MEMDEBUG}
  2648. memproclocalst.start;
  2649. {$endif MEMDEBUG}
  2650. end;
  2651. if assigned(inlininginfo) then
  2652. begin
  2653. {$ifdef MEMDEBUG}
  2654. memprocnodetree.start;
  2655. {$endif MEMDEBUG}
  2656. tnode(inlininginfo^.code).free;
  2657. {$ifdef MEMDEBUG}
  2658. memprocnodetree.start;
  2659. {$endif MEMDEBUG}
  2660. dispose(inlininginfo);
  2661. inlininginfo:=nil;
  2662. end;
  2663. stringdispose(resultname);
  2664. stringdispose(import_dll);
  2665. stringdispose(import_name);
  2666. stringdispose(deprecatedmsg);
  2667. if (po_msgstr in procoptions) then
  2668. stringdispose(messageinf.str);
  2669. if assigned(_mangledname) then
  2670. begin
  2671. {$ifdef MEMDEBUG}
  2672. memmanglednames.start;
  2673. {$endif MEMDEBUG}
  2674. stringdispose(_mangledname);
  2675. {$ifdef MEMDEBUG}
  2676. memmanglednames.stop;
  2677. {$endif MEMDEBUG}
  2678. end;
  2679. inherited destroy;
  2680. end;
  2681. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2682. var
  2683. oldintfcrc : boolean;
  2684. aliasnamescount : longint;
  2685. item : TCmdStrListItem;
  2686. begin
  2687. { released procdef? }
  2688. if not assigned(parast) then
  2689. exit;
  2690. inherited ppuwrite(ppufile);
  2691. if po_has_mangledname in procoptions then
  2692. ppufile.putstring(_mangledname^);
  2693. ppufile.putword(extnumber);
  2694. ppufile.putbyte(parast.symtablelevel);
  2695. ppufile.putderef(_classderef);
  2696. ppufile.putderef(procsymderef);
  2697. ppufile.putposinfo(fileinfo);
  2698. ppufile.putbyte(byte(visibility));
  2699. ppufile.putsmallset(symoptions);
  2700. if sp_has_deprecated_msg in symoptions then
  2701. ppufile.putstring(deprecatedmsg^);
  2702. {$ifdef powerpc}
  2703. { library symbol for AmigaOS/MorphOS }
  2704. ppufile.putderef(libsymderef);
  2705. {$endif powerpc}
  2706. { import }
  2707. if po_has_importdll in procoptions then
  2708. ppufile.putstring(import_dll^);
  2709. if po_has_importname in procoptions then
  2710. ppufile.putstring(import_name^);
  2711. ppufile.putword(import_nr);
  2712. if (po_msgint in procoptions) then
  2713. ppufile.putlongint(messageinf.i);
  2714. if (po_msgstr in procoptions) then
  2715. ppufile.putstring(messageinf.str^);
  2716. if (po_dispid in procoptions) then
  2717. ppufile.putlongint(dispid);
  2718. { inline stuff }
  2719. oldintfcrc:=ppufile.do_crc;
  2720. ppufile.do_crc:=false;
  2721. if (po_has_inlininginfo in procoptions) then
  2722. begin
  2723. ppufile.putderef(funcretsymderef);
  2724. ppufile.putsmallset(inlininginfo^.flags);
  2725. end;
  2726. { count alias names }
  2727. aliasnamescount:=0;
  2728. item:=TCmdStrListItem(aliasnames.first);
  2729. while assigned(item) do
  2730. begin
  2731. inc(aliasnamescount);
  2732. item:=TCmdStrListItem(item.next);
  2733. end;
  2734. if aliasnamescount>255 then
  2735. internalerror(200711021);
  2736. ppufile.putbyte(aliasnamescount);
  2737. item:=TCmdStrListItem(aliasnames.first);
  2738. while assigned(item) do
  2739. begin
  2740. ppufile.putstring(item.str);
  2741. item:=TCmdStrListItem(item.next);
  2742. end;
  2743. ppufile.do_crc:=oldintfcrc;
  2744. { write this entry }
  2745. ppufile.writeentry(ibprocdef);
  2746. { Save the para symtable, this is taken from the interface }
  2747. tparasymtable(parast).ppuwrite(ppufile);
  2748. { save localsymtable for inline procedures or when local
  2749. browser info is requested, this has no influence on the crc }
  2750. if (po_has_inlininginfo in procoptions) then
  2751. begin
  2752. oldintfcrc:=ppufile.do_crc;
  2753. ppufile.do_crc:=false;
  2754. tlocalsymtable(localst).ppuwrite(ppufile);
  2755. ppufile.do_crc:=oldintfcrc;
  2756. end;
  2757. { node tree for inlining }
  2758. oldintfcrc:=ppufile.do_crc;
  2759. ppufile.do_crc:=false;
  2760. if (po_has_inlininginfo in procoptions) then
  2761. ppuwritenodetree(ppufile,inlininginfo^.code);
  2762. ppufile.do_crc:=oldintfcrc;
  2763. end;
  2764. procedure tprocdef.reset;
  2765. begin
  2766. inherited reset;
  2767. procstarttai:=nil;
  2768. procendtai:=nil;
  2769. end;
  2770. function tprocdef.fullprocname(showhidden:boolean):string;
  2771. var
  2772. s : string;
  2773. t : ttoken;
  2774. begin
  2775. {$ifdef EXTDEBUG}
  2776. showhidden:=true;
  2777. {$endif EXTDEBUG}
  2778. s:='';
  2779. if owner.symtabletype=localsymtable then
  2780. s:=s+'local ';
  2781. if assigned(_class) then
  2782. begin
  2783. if po_classmethod in procoptions then
  2784. s:=s+'class ';
  2785. s:=s+_class.objrealname^+'.';
  2786. end;
  2787. if proctypeoption=potype_operator then
  2788. begin
  2789. for t:=NOTOKEN to last_overloaded do
  2790. if procsym.realname='$'+overloaded_names[t] then
  2791. begin
  2792. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2793. break;
  2794. end;
  2795. end
  2796. else
  2797. s:=s+procsym.realname+typename_paras(showhidden);
  2798. case proctypeoption of
  2799. potype_constructor:
  2800. s:='constructor '+s;
  2801. potype_destructor:
  2802. s:='destructor '+s;
  2803. else
  2804. if assigned(returndef) and
  2805. not(is_void(returndef)) then
  2806. s:=s+':'+returndef.GetTypeName;
  2807. end;
  2808. s:=s+';';
  2809. { forced calling convention? }
  2810. if (po_hascallingconvention in procoptions) then
  2811. s:=s+' '+ProcCallOptionStr[proccalloption]+';';
  2812. if po_staticmethod in procoptions then
  2813. s:=s+' Static;';
  2814. fullprocname:=s;
  2815. end;
  2816. function tprocdef.is_methodpointer:boolean;
  2817. begin
  2818. result:=assigned(_class);
  2819. end;
  2820. function tprocdef.is_addressonly:boolean;
  2821. begin
  2822. result:=assigned(owner) and
  2823. (owner.symtabletype<>ObjectSymtable);
  2824. end;
  2825. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2826. begin
  2827. case t of
  2828. gs_local :
  2829. GetSymtable:=localst;
  2830. gs_para :
  2831. GetSymtable:=parast;
  2832. else
  2833. GetSymtable:=nil;
  2834. end;
  2835. end;
  2836. procedure tprocdef.buildderef;
  2837. begin
  2838. inherited buildderef;
  2839. _classderef.build(_class);
  2840. { procsym that originaly defined this definition, should be in the
  2841. same symtable }
  2842. procsymderef.build(procsym);
  2843. {$ifdef powerpc}
  2844. { library symbol for AmigaOS/MorphOS }
  2845. libsymderef.build(libsym);
  2846. {$endif powerpc}
  2847. end;
  2848. procedure tprocdef.buildderefimpl;
  2849. begin
  2850. inherited buildderefimpl;
  2851. { Localst is not available for main/unit init }
  2852. if assigned(localst) then
  2853. begin
  2854. tlocalsymtable(localst).buildderef;
  2855. tlocalsymtable(localst).buildderefimpl;
  2856. end;
  2857. { inline tree }
  2858. if (po_has_inlininginfo in procoptions) then
  2859. begin
  2860. funcretsymderef.build(funcretsym);
  2861. inlininginfo^.code.buildderefimpl;
  2862. end;
  2863. end;
  2864. procedure tprocdef.deref;
  2865. begin
  2866. inherited deref;
  2867. _class:=tobjectdef(_classderef.resolve);
  2868. { procsym that originaly defined this definition, should be in the
  2869. same symtable }
  2870. procsym:=tprocsym(procsymderef.resolve);
  2871. {$ifdef powerpc}
  2872. { library symbol for AmigaOS/MorphOS }
  2873. libsym:=tsym(libsymderef.resolve);
  2874. {$endif powerpc}
  2875. end;
  2876. procedure tprocdef.derefimpl;
  2877. begin
  2878. { Enable has_inlininginfo when the inlininginfo
  2879. structure is available. The has_inlininginfo was disabled
  2880. after the load, since the data was invalid }
  2881. if assigned(inlininginfo) then
  2882. include(procoptions,po_has_inlininginfo);
  2883. { Locals }
  2884. if assigned(localst) then
  2885. begin
  2886. tlocalsymtable(localst).deref;
  2887. tlocalsymtable(localst).derefimpl;
  2888. end;
  2889. { Inline }
  2890. if (po_has_inlininginfo in procoptions) then
  2891. begin
  2892. inlininginfo^.code.derefimpl;
  2893. { funcretsym, this is always located in the localst }
  2894. funcretsym:=tsym(funcretsymderef.resolve);
  2895. end
  2896. else
  2897. begin
  2898. { safety }
  2899. funcretsym:=nil;
  2900. end;
  2901. end;
  2902. function tprocdef.GetTypeName : string;
  2903. begin
  2904. GetTypeName := FullProcName(false);
  2905. end;
  2906. function tprocdef.mangledname : string;
  2907. var
  2908. hp : TParavarsym;
  2909. hs : string;
  2910. crc : dword;
  2911. newlen,
  2912. oldlen,
  2913. i : integer;
  2914. begin
  2915. if assigned(_mangledname) then
  2916. begin
  2917. {$ifdef compress}
  2918. mangledname:=minilzw_decode(_mangledname^);
  2919. {$else}
  2920. mangledname:=_mangledname^;
  2921. {$endif}
  2922. exit;
  2923. end;
  2924. { we need to use the symtable where the procsym is inserted,
  2925. because that is visible to the world }
  2926. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  2927. oldlen:=length(mangledname);
  2928. { add parameter types }
  2929. for i:=0 to paras.count-1 do
  2930. begin
  2931. hp:=tparavarsym(paras[i]);
  2932. if not(vo_is_hidden_para in hp.varoptions) then
  2933. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  2934. end;
  2935. { add resultdef, add $$ as separator to make it unique from a
  2936. parameter separator }
  2937. if not is_void(returndef) then
  2938. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  2939. newlen:=length(mangledname);
  2940. { Replace with CRC if the parameter line is very long }
  2941. if (newlen-oldlen>12) and
  2942. ((newlen>128) or (newlen-oldlen>64)) then
  2943. begin
  2944. crc:=0;
  2945. for i:=0 to paras.count-1 do
  2946. begin
  2947. hp:=tparavarsym(paras[i]);
  2948. if not(vo_is_hidden_para in hp.varoptions) then
  2949. begin
  2950. hs:=hp.vardef.mangledparaname;
  2951. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2952. end;
  2953. end;
  2954. hs:=hp.vardef.mangledparaname;
  2955. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2956. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  2957. end;
  2958. {$ifdef compress}
  2959. _mangledname:=stringdup(minilzw_encode(mangledname));
  2960. {$else}
  2961. _mangledname:=stringdup(mangledname);
  2962. {$endif}
  2963. end;
  2964. function tprocdef.cplusplusmangledname : string;
  2965. function getcppparaname(p : tdef) : string;
  2966. const
  2967. ordtype2str : array[tordtype] of string[2] = (
  2968. '',
  2969. 'Uc','Us','Ui','Us',
  2970. 'Sc','s','i','x',
  2971. 'b','b','b','b','b',
  2972. 'c','w','x');
  2973. var
  2974. s : string;
  2975. begin
  2976. case p.typ of
  2977. orddef:
  2978. s:=ordtype2str[torddef(p).ordtype];
  2979. pointerdef:
  2980. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  2981. else
  2982. internalerror(2103001);
  2983. end;
  2984. getcppparaname:=s;
  2985. end;
  2986. var
  2987. s,s2 : string;
  2988. hp : TParavarsym;
  2989. i : integer;
  2990. begin
  2991. { outdated gcc 2.x name mangling scheme }
  2992. {$ifdef NAMEMANGLING_GCC2}
  2993. s := procsym.realname;
  2994. if procsym.owner.symtabletype=ObjectSymtable then
  2995. begin
  2996. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  2997. case proctypeoption of
  2998. potype_destructor:
  2999. s:='_$_'+tostr(length(s2))+s2;
  3000. potype_constructor:
  3001. s:='___'+tostr(length(s2))+s2;
  3002. else
  3003. s:='_'+s+'__'+tostr(length(s2))+s2;
  3004. end;
  3005. end
  3006. else s:=s+'__';
  3007. s:=s+'F';
  3008. { concat modifiers }
  3009. { !!!!! }
  3010. { now we handle the parameters }
  3011. if maxparacount>0 then
  3012. begin
  3013. for i:=0 to paras.count-1 do
  3014. begin
  3015. hp:=tparavarsym(paras[i]);
  3016. s2:=getcppparaname(hp.vardef);
  3017. if hp.varspez in [vs_var,vs_out] then
  3018. s2:='R'+s2;
  3019. s:=s+s2;
  3020. end;
  3021. end
  3022. else
  3023. s:=s+'v';
  3024. cplusplusmangledname:=s;
  3025. {$endif NAMEMANGLING_GCC2}
  3026. { gcc 3.x name mangling scheme }
  3027. if procsym.owner.symtabletype=ObjectSymtable then
  3028. begin
  3029. s:='_ZN';
  3030. s2:=tobjectdef(procsym.owner.defowner).objrealname^;
  3031. s:=s+tostr(length(s2))+s2;
  3032. case proctypeoption of
  3033. potype_constructor:
  3034. s:=s+'C1';
  3035. potype_destructor:
  3036. s:=s+'D1';
  3037. else
  3038. s:=s+tostr(length(procsym.realname))+procsym.realname;
  3039. end;
  3040. s:=s+'E';
  3041. end
  3042. else
  3043. s:=procsym.realname;
  3044. { now we handle the parameters }
  3045. if maxparacount>0 then
  3046. begin
  3047. for i:=0 to paras.count-1 do
  3048. begin
  3049. hp:=tparavarsym(paras[i]);
  3050. s2:=getcppparaname(hp.vardef);
  3051. if hp.varspez in [vs_var,vs_out] then
  3052. s2:='R'+s2;
  3053. s:=s+s2;
  3054. end;
  3055. end
  3056. else
  3057. s:=s+'v';
  3058. cplusplusmangledname:=s;
  3059. end;
  3060. function tprocdef.objcmangledname : string;
  3061. begin
  3062. if not (po_msgstr in procoptions) then
  3063. internalerror(2009030901);
  3064. { we may very well need longer strings to handle these... }
  3065. if ((255-length(tobjectdef(procsym.owner.defowner).objextname^)
  3066. -length('+"[ ]"')-length(messageinf.str^)) < 0) then
  3067. Message1(parser_e_objc_message_name_too_long,messageinf.str^);
  3068. if not(po_classmethod in procoptions) then
  3069. result:='"-['
  3070. else
  3071. result:='"+[';
  3072. { quotes are necessary because the +/- otherwise confuse the assembler
  3073. into expecting a number
  3074. }
  3075. result:=
  3076. result+tobjectdef(procsym.owner.defowner).objextname^+' '+
  3077. messageinf.str^+']"';
  3078. end;
  3079. procedure tprocdef.setmangledname(const s : string);
  3080. begin
  3081. { This is not allowed anymore, the forward declaration
  3082. already needs to create the correct mangledname, no changes
  3083. afterwards are allowed (PFV) }
  3084. { Exception: interface definitions in mode macpas, since in that }
  3085. { case no reference to the old name can exist yet (JM) }
  3086. if assigned(_mangledname) then
  3087. if ((m_mac in current_settings.modeswitches) and
  3088. (interfacedef)) then
  3089. stringdispose(_mangledname)
  3090. else
  3091. internalerror(200411171);
  3092. {$ifdef compress}
  3093. _mangledname:=stringdup(minilzw_encode(s));
  3094. {$else}
  3095. _mangledname:=stringdup(s);
  3096. {$endif}
  3097. include(procoptions,po_has_mangledname);
  3098. end;
  3099. {***************************************************************************
  3100. TPROCVARDEF
  3101. ***************************************************************************}
  3102. constructor tprocvardef.create(level:byte);
  3103. begin
  3104. inherited create(procvardef,level);
  3105. end;
  3106. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3107. begin
  3108. inherited ppuload(procvardef,ppufile);
  3109. { load para symtable }
  3110. parast:=tparasymtable.create(self,unknown_level);
  3111. tparasymtable(parast).ppuload(ppufile);
  3112. end;
  3113. function tprocvardef.getcopy : tstoreddef;
  3114. var
  3115. i : tcallercallee;
  3116. j : longint;
  3117. begin
  3118. result:=tprocvardef.create(parast.symtablelevel);
  3119. tprocvardef(result).returndef:=returndef;
  3120. tprocvardef(result).returndefderef:=returndefderef;
  3121. tprocvardef(result).parast:=parast.getcopy;
  3122. tprocvardef(result).savesize:=savesize;
  3123. { create paralist copy }
  3124. tprocvardef(result).paras:=tparalist.create(false);
  3125. tprocvardef(result).paras.count:=paras.count;
  3126. for j:=0 to paras.count-1 do
  3127. tprocvardef(result).paras[j]:=paras[j];
  3128. tprocvardef(result).proctypeoption:=proctypeoption;
  3129. tprocvardef(result).proccalloption:=proccalloption;
  3130. tprocvardef(result).procoptions:=procoptions;
  3131. tprocvardef(result).requiredargarea:=requiredargarea;
  3132. tprocvardef(result).maxparacount:=maxparacount;
  3133. tprocvardef(result).minparacount:=minparacount;
  3134. for i:=low(tcallercallee) to high(tcallercallee) do
  3135. location_copy(tprocvardef(result).funcretloc[i],funcretloc[i]);
  3136. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3137. {$ifdef m68k}
  3138. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3139. {$endif}
  3140. end;
  3141. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3142. begin
  3143. inherited ppuwrite(ppufile);
  3144. { Write this entry }
  3145. ppufile.writeentry(ibprocvardef);
  3146. { Save the para symtable, this is taken from the interface }
  3147. tparasymtable(parast).ppuwrite(ppufile);
  3148. end;
  3149. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3150. begin
  3151. case t of
  3152. gs_para :
  3153. GetSymtable:=parast;
  3154. else
  3155. GetSymtable:=nil;
  3156. end;
  3157. end;
  3158. function tprocvardef.size : aint;
  3159. begin
  3160. if (po_methodpointer in procoptions) and
  3161. not(po_addressonly in procoptions) then
  3162. size:=2*sizeof(pint)
  3163. else
  3164. size:=sizeof(pint);
  3165. end;
  3166. function tprocvardef.is_methodpointer:boolean;
  3167. begin
  3168. result:=(po_methodpointer in procoptions);
  3169. end;
  3170. function tprocvardef.is_addressonly:boolean;
  3171. begin
  3172. result:=not(po_methodpointer in procoptions) or
  3173. (po_addressonly in procoptions);
  3174. end;
  3175. function tprocvardef.getmangledparaname:string;
  3176. begin
  3177. result:='procvar';
  3178. end;
  3179. function tprocvardef.is_publishable : boolean;
  3180. begin
  3181. is_publishable:=(po_methodpointer in procoptions);
  3182. end;
  3183. function tprocvardef.GetTypeName : string;
  3184. var
  3185. s: string;
  3186. showhidden : boolean;
  3187. begin
  3188. {$ifdef EXTDEBUG}
  3189. showhidden:=true;
  3190. {$else EXTDEBUG}
  3191. showhidden:=false;
  3192. {$endif EXTDEBUG}
  3193. s:='<';
  3194. if po_classmethod in procoptions then
  3195. s := s+'class method type of'
  3196. else
  3197. if po_addressonly in procoptions then
  3198. s := s+'address of'
  3199. else
  3200. s := s+'procedure variable type of';
  3201. if po_local in procoptions then
  3202. s := s+' local';
  3203. if assigned(returndef) and
  3204. (returndef<>voidtype) then
  3205. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3206. else
  3207. s:=s+' procedure'+typename_paras(showhidden);
  3208. if po_methodpointer in procoptions then
  3209. s := s+' of object';
  3210. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3211. end;
  3212. {***************************************************************************
  3213. TOBJECTDEF
  3214. ***************************************************************************}
  3215. constructor tobjectdef.create(ot : tobjecttyp;const n : string;c : tobjectdef);
  3216. begin
  3217. inherited create(objectdef);
  3218. objecttype:=ot;
  3219. objectoptions:=[];
  3220. childof:=nil;
  3221. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3222. { create space for vmt !! }
  3223. vmtentries:=TFPList.Create;
  3224. vmt_offset:=0;
  3225. set_parent(c);
  3226. objname:=stringdup(upper(n));
  3227. objrealname:=stringdup(n);
  3228. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3229. prepareguid;
  3230. { setup implemented interfaces }
  3231. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3232. ImplementedInterfaces:=TFPObjectList.Create(true)
  3233. else
  3234. ImplementedInterfaces:=nil;
  3235. writing_class_record_dbginfo:=false;
  3236. end;
  3237. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3238. var
  3239. i,
  3240. implintfcount : longint;
  3241. d : tderef;
  3242. ImplIntf : TImplementedInterface;
  3243. vmtentry : pvmtentry;
  3244. begin
  3245. inherited ppuload(objectdef,ppufile);
  3246. objecttype:=tobjecttyp(ppufile.getbyte);
  3247. objrealname:=stringdup(ppufile.getstring);
  3248. objname:=stringdup(upper(objrealname^));
  3249. objextname:=stringdup(ppufile.getstring);
  3250. { only used for external Objective-C classes/protocols }
  3251. if (objextname^='') then
  3252. stringdispose(objextname);
  3253. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3254. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3255. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3256. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3257. vmt_offset:=ppufile.getlongint;
  3258. ppufile.getderef(childofderef);
  3259. ppufile.getsmallset(objectoptions);
  3260. { load guid }
  3261. iidstr:=nil;
  3262. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3263. begin
  3264. new(iidguid);
  3265. ppufile.getguid(iidguid^);
  3266. iidstr:=stringdup(ppufile.getstring);
  3267. end;
  3268. vmtentries:=TFPList.Create;
  3269. vmtentries.count:=ppufile.getlongint;
  3270. for i:=0 to vmtentries.count-1 do
  3271. begin
  3272. ppufile.getderef(d);
  3273. new(vmtentry);
  3274. vmtentry^.procdef:=nil;
  3275. vmtentry^.procdefderef:=d;
  3276. vmtentry^.visibility:=tvisibility(ppufile.getbyte);
  3277. vmtentries[i]:=vmtentry;
  3278. end;
  3279. { load implemented interfaces }
  3280. if objecttype in [odt_class,odt_objcclass,odt_objcprotocol] then
  3281. begin
  3282. ImplementedInterfaces:=TFPObjectList.Create(true);
  3283. implintfcount:=ppufile.getlongint;
  3284. for i:=0 to implintfcount-1 do
  3285. begin
  3286. ppufile.getderef(d);
  3287. ImplIntf:=TImplementedInterface.Create_deref(d);
  3288. ImplIntf.IOffset:=ppufile.getlongint;
  3289. ImplementedInterfaces.Add(ImplIntf);
  3290. end;
  3291. end
  3292. else
  3293. ImplementedInterfaces:=nil;
  3294. if df_copied_def in defoptions then
  3295. ppufile.getderef(cloneddefderef)
  3296. else
  3297. tObjectSymtable(symtable).ppuload(ppufile);
  3298. { handles the predefined class tobject }
  3299. { the last TOBJECT which is loaded gets }
  3300. { it ! }
  3301. if (childof=nil) and
  3302. (objecttype=odt_class) and
  3303. (objname^='TOBJECT') then
  3304. class_tobject:=self;
  3305. if (childof=nil) and
  3306. (objecttype=odt_interfacecom) and
  3307. (objname^='IUNKNOWN') then
  3308. interface_iunknown:=self;
  3309. if (childof=nil) and
  3310. (objecttype=odt_objcclass) and
  3311. (objname^='PROTOCOL') then
  3312. objc_protocoltype:=self;
  3313. writing_class_record_dbginfo:=false;
  3314. end;
  3315. destructor tobjectdef.destroy;
  3316. begin
  3317. if assigned(symtable) then
  3318. begin
  3319. symtable.free;
  3320. symtable:=nil;
  3321. end;
  3322. stringdispose(objname);
  3323. stringdispose(objrealname);
  3324. stringdispose(objextname);
  3325. stringdispose(iidstr);
  3326. if assigned(ImplementedInterfaces) then
  3327. begin
  3328. ImplementedInterfaces.free;
  3329. ImplementedInterfaces:=nil;
  3330. end;
  3331. if assigned(iidguid) then
  3332. begin
  3333. dispose(iidguid);
  3334. iidguid:=nil;
  3335. end;
  3336. if assigned(vmtentries) then
  3337. begin
  3338. resetvmtentries;
  3339. vmtentries.free;
  3340. vmtentries:=nil;
  3341. end;
  3342. if assigned(vmcallstaticinfo) then
  3343. begin
  3344. freemem(vmcallstaticinfo);
  3345. vmcallstaticinfo:=nil;
  3346. end;
  3347. inherited destroy;
  3348. end;
  3349. function tobjectdef.getcopy : tstoreddef;
  3350. var
  3351. i : longint;
  3352. begin
  3353. result:=tobjectdef.create(objecttype,objname^,childof);
  3354. { the constructor allocates a symtable which we release to avoid memory leaks }
  3355. tobjectdef(result).symtable.free;
  3356. tobjectdef(result).symtable:=symtable.getcopy;
  3357. if assigned(objname) then
  3358. tobjectdef(result).objname:=stringdup(objname^);
  3359. if assigned(objrealname) then
  3360. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3361. if assigned(objextname) then
  3362. tobjectdef(result).objextname:=stringdup(objextname^);
  3363. tobjectdef(result).objectoptions:=objectoptions;
  3364. include(tobjectdef(result).defoptions,df_copied_def);
  3365. tobjectdef(result).vmt_offset:=vmt_offset;
  3366. if assigned(iidguid) then
  3367. begin
  3368. new(tobjectdef(result).iidguid);
  3369. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3370. end;
  3371. if assigned(iidstr) then
  3372. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3373. if assigned(ImplementedInterfaces) then
  3374. begin
  3375. for i:=0 to ImplementedInterfaces.count-1 do
  3376. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3377. end;
  3378. if assigned(vmtentries) then
  3379. begin
  3380. tobjectdef(result).vmtentries:=TFPList.Create;
  3381. tobjectdef(result).copyvmtentries(self);
  3382. end;
  3383. end;
  3384. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3385. var
  3386. i : longint;
  3387. vmtentry : pvmtentry;
  3388. ImplIntf : TImplementedInterface;
  3389. begin
  3390. inherited ppuwrite(ppufile);
  3391. ppufile.putbyte(byte(objecttype));
  3392. ppufile.putstring(objrealname^);
  3393. if assigned(objextname) then
  3394. ppufile.putstring(objextname^)
  3395. else
  3396. ppufile.putstring('');
  3397. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3398. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3399. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3400. ppufile.putlongint(vmt_offset);
  3401. ppufile.putderef(childofderef);
  3402. ppufile.putsmallset(objectoptions);
  3403. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3404. begin
  3405. ppufile.putguid(iidguid^);
  3406. ppufile.putstring(iidstr^);
  3407. end;
  3408. ppufile.putlongint(vmtentries.count);
  3409. for i:=0 to vmtentries.count-1 do
  3410. begin
  3411. vmtentry:=pvmtentry(vmtentries[i]);
  3412. ppufile.putderef(vmtentry^.procdefderef);
  3413. ppufile.putbyte(byte(vmtentry^.visibility));
  3414. end;
  3415. if assigned(ImplementedInterfaces) then
  3416. begin
  3417. ppufile.putlongint(ImplementedInterfaces.Count);
  3418. for i:=0 to ImplementedInterfaces.Count-1 do
  3419. begin
  3420. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3421. ppufile.putderef(ImplIntf.intfdefderef);
  3422. ppufile.putlongint(ImplIntf.Ioffset);
  3423. end;
  3424. end;
  3425. if df_copied_def in defoptions then
  3426. ppufile.putderef(cloneddefderef);
  3427. ppufile.writeentry(ibobjectdef);
  3428. if not(df_copied_def in defoptions) then
  3429. tObjectSymtable(symtable).ppuwrite(ppufile);
  3430. end;
  3431. function tobjectdef.GetTypeName:string;
  3432. begin
  3433. { in this case we will go in endless recursion, because then }
  3434. { there is no tsym associated yet with the def. It can occur }
  3435. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3436. { instead of the actual type name }
  3437. if not assigned(typesym) then
  3438. result:='<Currently Parsed Class>'
  3439. else
  3440. result:=typename;
  3441. end;
  3442. procedure tobjectdef.buildderef;
  3443. var
  3444. i : longint;
  3445. vmtentry : pvmtentry;
  3446. begin
  3447. inherited buildderef;
  3448. childofderef.build(childof);
  3449. if df_copied_def in defoptions then
  3450. cloneddefderef.build(symtable.defowner)
  3451. else
  3452. tstoredsymtable(symtable).buildderef;
  3453. for i:=0 to vmtentries.count-1 do
  3454. begin
  3455. vmtentry:=pvmtentry(vmtentries[i]);
  3456. vmtentry^.procdefderef.build(vmtentry^.procdef);
  3457. end;
  3458. if assigned(ImplementedInterfaces) then
  3459. begin
  3460. for i:=0 to ImplementedInterfaces.count-1 do
  3461. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3462. end;
  3463. end;
  3464. procedure tobjectdef.deref;
  3465. var
  3466. i : longint;
  3467. vmtentry : pvmtentry;
  3468. begin
  3469. inherited deref;
  3470. childof:=tobjectdef(childofderef.resolve);
  3471. if df_copied_def in defoptions then
  3472. begin
  3473. cloneddef:=tobjectdef(cloneddefderef.resolve);
  3474. symtable:=cloneddef.symtable.getcopy;
  3475. end
  3476. else
  3477. tstoredsymtable(symtable).deref;
  3478. for i:=0 to vmtentries.count-1 do
  3479. begin
  3480. vmtentry:=pvmtentry(vmtentries[i]);
  3481. vmtentry^.procdef:=tprocdef(vmtentry^.procdefderef.resolve);
  3482. end;
  3483. if assigned(ImplementedInterfaces) then
  3484. begin
  3485. for i:=0 to ImplementedInterfaces.count-1 do
  3486. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3487. end;
  3488. end;
  3489. procedure tobjectdef.buildderefimpl;
  3490. begin
  3491. inherited buildderefimpl;
  3492. if not (df_copied_def in defoptions) then
  3493. tstoredsymtable(symtable).buildderefimpl;
  3494. end;
  3495. procedure tobjectdef.derefimpl;
  3496. begin
  3497. inherited derefimpl;
  3498. if not (df_copied_def in defoptions) then
  3499. tstoredsymtable(symtable).derefimpl;
  3500. end;
  3501. procedure tobjectdef.resetvmtentries;
  3502. var
  3503. i : longint;
  3504. begin
  3505. for i:=0 to vmtentries.Count-1 do
  3506. Dispose(pvmtentry(vmtentries[i]));
  3507. vmtentries.clear;
  3508. end;
  3509. procedure tobjectdef.copyvmtentries(objdef:tobjectdef);
  3510. var
  3511. i : longint;
  3512. vmtentry : pvmtentry;
  3513. begin
  3514. resetvmtentries;
  3515. vmtentries.count:=objdef.vmtentries.count;
  3516. for i:=0 to objdef.vmtentries.count-1 do
  3517. begin
  3518. new(vmtentry);
  3519. vmtentry^:=pvmtentry(objdef.vmtentries[i])^;
  3520. vmtentries[i]:=vmtentry;
  3521. end;
  3522. end;
  3523. function tobjectdef.getparentdef:tdef;
  3524. begin
  3525. { TODO: Remove getparentdef hack}
  3526. { With 2 forward declared classes with the child class before the
  3527. parent class the child class is written earlier to the ppu. Leaving it
  3528. possible to have a reference to the parent class for property overriding,
  3529. but the parent class still has the childof not resolved yet (PFV) }
  3530. if childof=nil then
  3531. childof:=tobjectdef(childofderef.resolve);
  3532. result:=childof;
  3533. end;
  3534. procedure tobjectdef.prepareguid;
  3535. begin
  3536. { set up guid }
  3537. if not assigned(iidguid) then
  3538. begin
  3539. new(iidguid);
  3540. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3541. end;
  3542. { setup iidstring }
  3543. if not assigned(iidstr) then
  3544. iidstr:=stringdup(''); { default is empty string }
  3545. end;
  3546. procedure tobjectdef.set_parent( c : tobjectdef);
  3547. begin
  3548. if assigned(childof) then
  3549. exit;
  3550. childof:=c;
  3551. if not assigned(c) then
  3552. exit;
  3553. { inherit options and status }
  3554. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3555. { add the data of the anchestor class/object }
  3556. if (objecttype in [odt_class,odt_object,odt_objcclass]) then
  3557. begin
  3558. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize+tObjectSymtable(c.symtable).datasize;
  3559. { inherit recordalignment }
  3560. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3561. if (oo_has_vmt in objectoptions) and
  3562. (oo_has_vmt in c.objectoptions) then
  3563. tObjectSymtable(symtable).datasize:=tObjectSymtable(symtable).datasize-sizeof(pint);
  3564. { if parent has a vmt field then the offset is the same for the child PM }
  3565. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3566. begin
  3567. vmt_offset:=c.vmt_offset;
  3568. include(objectoptions,oo_has_vmt);
  3569. end;
  3570. end;
  3571. end;
  3572. procedure tobjectdef.insertvmt;
  3573. var
  3574. vs: tfieldvarsym;
  3575. begin
  3576. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3577. exit;
  3578. if (oo_has_vmt in objectoptions) then
  3579. internalerror(12345)
  3580. else
  3581. begin
  3582. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3583. tObjectSymtable(symtable).fieldalignment);
  3584. if (tf_requires_proper_alignment in target_info.flags) then
  3585. begin
  3586. { Align VMT pointer and whole object instance if target CPU requires alignment. }
  3587. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3588. tObjectSymtable(symtable).alignrecord(tObjectSymtable(symtable).datasize,sizeof(pint));
  3589. end;
  3590. vmt_offset:=tObjectSymtable(symtable).datasize;
  3591. vs:=tfieldvarsym.create('_vptr$'+objname^,vs_value,voidpointertype,[]);
  3592. hidesym(vs);
  3593. tObjectSymtable(symtable).insert(vs);
  3594. tObjectSymtable(symtable).addfield(vs,vis_hidden);
  3595. include(objectoptions,oo_has_vmt);
  3596. end;
  3597. end;
  3598. procedure tobjectdef.check_forwards;
  3599. begin
  3600. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcprotocol]) then
  3601. tstoredsymtable(symtable).check_forwards;
  3602. if (oo_is_forward in objectoptions) then
  3603. begin
  3604. { ok, in future, the forward can be resolved }
  3605. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3606. exclude(objectoptions,oo_is_forward);
  3607. end;
  3608. end;
  3609. { true if prot implements d (or if they are equal) }
  3610. function is_related_protocol(prot: tobjectdef; d : tdef) : boolean;
  3611. var
  3612. i : longint;
  3613. begin
  3614. { objcprotocols have multiple inheritance, all protocols from which
  3615. the current protocol inherits are stored in implementedinterfaces }
  3616. result:=prot=d;
  3617. if result then
  3618. exit;
  3619. for i:=0 to prot.ImplementedInterfaces.count-1 do
  3620. begin
  3621. result:=is_related_protocol(tobjectdef(prot.ImplementedInterfaces[i]),d);
  3622. if result then
  3623. exit;
  3624. end;
  3625. end;
  3626. { true, if self inherits from d (or if they are equal) }
  3627. function tobjectdef.is_related(d : tdef) : boolean;
  3628. var
  3629. hp : tobjectdef;
  3630. begin
  3631. if (objecttype=odt_objcprotocol) then
  3632. begin
  3633. is_related:=is_related_protocol(self,d);
  3634. exit
  3635. end;
  3636. hp:=self;
  3637. while assigned(hp) do
  3638. begin
  3639. if hp=d then
  3640. begin
  3641. is_related:=true;
  3642. exit;
  3643. end;
  3644. hp:=hp.childof;
  3645. end;
  3646. is_related:=false;
  3647. end;
  3648. function tobjectdef.FindDestructor : tprocdef;
  3649. var
  3650. objdef : tobjectdef;
  3651. i : longint;
  3652. sym : tsym;
  3653. pd : tprocdef;
  3654. begin
  3655. result:=nil;
  3656. objdef:=self;
  3657. while assigned(objdef) do
  3658. begin
  3659. for i:=0 to objdef.symtable.SymList.Count-1 do
  3660. begin
  3661. sym:=TSym(objdef.symtable.SymList[i]);
  3662. if sym.typ=procsym then
  3663. begin
  3664. pd:=Tprocsym(sym).Find_procdef_bytype(potype_destructor);
  3665. if assigned(pd) then
  3666. begin
  3667. result:=pd;
  3668. exit;
  3669. end;
  3670. end;
  3671. end;
  3672. objdef:=objdef.childof;
  3673. end;
  3674. end;
  3675. function tobjectdef.implements_any_interfaces: boolean;
  3676. begin
  3677. result := (ImplementedInterfaces.Count > 0) or
  3678. (assigned(childof) and childof.implements_any_interfaces);
  3679. end;
  3680. function tobjectdef.size : aint;
  3681. begin
  3682. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3683. result:=sizeof(pint)
  3684. else
  3685. result:=tObjectSymtable(symtable).datasize;
  3686. end;
  3687. function tobjectdef.alignment:shortint;
  3688. begin
  3689. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol] then
  3690. alignment:=sizeof(pint)
  3691. else
  3692. alignment:=tObjectSymtable(symtable).recordalignment;
  3693. end;
  3694. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3695. begin
  3696. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3697. case objecttype of
  3698. odt_class:
  3699. { the +2*sizeof(pint) is size and -size }
  3700. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3701. odt_objcclass,
  3702. odt_objcprotocol:
  3703. vmtmethodoffset:=0;
  3704. odt_interfacecom,odt_interfacecorba:
  3705. vmtmethodoffset:=index*sizeof(pint);
  3706. else
  3707. {$ifdef WITHDMT}
  3708. vmtmethodoffset:=(index+4)*sizeof(pint);
  3709. {$else WITHDMT}
  3710. vmtmethodoffset:=(index+3)*sizeof(pint);
  3711. {$endif WITHDMT}
  3712. end;
  3713. end;
  3714. function tobjectdef.vmt_mangledname : string;
  3715. begin
  3716. if not(oo_has_vmt in objectoptions) then
  3717. Message1(parser_n_object_has_no_vmt,objrealname^);
  3718. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3719. end;
  3720. function tobjectdef.needs_inittable : boolean;
  3721. begin
  3722. case objecttype of
  3723. odt_dispinterface,
  3724. odt_class :
  3725. needs_inittable:=false;
  3726. odt_interfacecom:
  3727. needs_inittable:=true;
  3728. odt_interfacecorba:
  3729. needs_inittable:=is_related(interface_iunknown);
  3730. odt_object:
  3731. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  3732. odt_cppclass,
  3733. odt_objcclass,
  3734. odt_objcprotocol:
  3735. needs_inittable:=false;
  3736. else
  3737. internalerror(200108267);
  3738. end;
  3739. end;
  3740. function tobjectdef.rtti_mangledname(rt: trttitype): string;
  3741. begin
  3742. if not(objecttype in [odt_objcclass,odt_objcprotocol]) then
  3743. result:=inherited rtti_mangledname(rt)
  3744. else
  3745. begin
  3746. if not(target_info.system in system_objc_nfabi) then
  3747. begin
  3748. result:=target_asm.labelprefix;
  3749. case objecttype of
  3750. odt_objcclass:
  3751. begin
  3752. case rt of
  3753. objcclassrtti:
  3754. result:=result+'_OBJC_CLASS_';
  3755. objcmetartti:
  3756. result:=result+'_OBJC_METACLASS_';
  3757. else
  3758. internalerror(2009092302);
  3759. end;
  3760. end;
  3761. odt_objcprotocol:
  3762. result:=result+'_OBJC_PROTOCOL_';
  3763. end;
  3764. end
  3765. else
  3766. begin
  3767. case objecttype of
  3768. odt_objcclass:
  3769. begin
  3770. case rt of
  3771. objcclassrtti:
  3772. result:='_OBJC_CLASS_$_';
  3773. objcmetartti:
  3774. result:='_OBJC_METACLASS_$_';
  3775. objcclassrortti:
  3776. result:=lower(target_asm.labelprefix)+'_OBJC_CLASS_RO_$_';
  3777. objcmetarortti:
  3778. result:=lower(target_asm.labelprefix)+'_OBJC_METACLASS_RO_$_';
  3779. else
  3780. internalerror(2009092303);
  3781. end;
  3782. end;
  3783. odt_objcprotocol:
  3784. begin
  3785. result:=lower(target_asm.labelprefix);
  3786. case rt of
  3787. objcclassrtti:
  3788. result:=result+'_OBJC_PROTOCOL_$_';
  3789. objcmetartti:
  3790. result:=result+'_OBJC_LABEL_PROTOCOL_$_';
  3791. else
  3792. internalerror(2009092501);
  3793. end;
  3794. end;
  3795. end;
  3796. end;
  3797. result:=result+objextname^;
  3798. end;
  3799. end;
  3800. function tobjectdef.members_need_inittable : boolean;
  3801. begin
  3802. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  3803. end;
  3804. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  3805. var
  3806. ImplIntf : TImplementedInterface;
  3807. i : longint;
  3808. begin
  3809. result:=nil;
  3810. if not assigned(ImplementedInterfaces) then
  3811. exit;
  3812. for i:=0 to ImplementedInterfaces.Count-1 do
  3813. begin
  3814. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3815. if ImplIntf.intfdef=aintfdef then
  3816. begin
  3817. result:=ImplIntf;
  3818. exit;
  3819. end;
  3820. end;
  3821. end;
  3822. function tobjectdef.is_publishable : boolean;
  3823. begin
  3824. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  3825. end;
  3826. procedure tobjectdef.reset;
  3827. begin
  3828. inherited reset;
  3829. created_in_current_module:=false;
  3830. maybe_created_in_current_module:=false;
  3831. classref_created_in_current_module:=false;
  3832. end;
  3833. function tobjectdef.search_enumerator_get: tprocdef;
  3834. var
  3835. objdef : tobjectdef;
  3836. sym : tsym;
  3837. i : integer;
  3838. pd : tprocdef;
  3839. hashedid : THashedIDString;
  3840. begin
  3841. result:=nil;
  3842. objdef:=self;
  3843. hashedid.id:='GETENUMERATOR';
  3844. while assigned(objdef) do
  3845. begin
  3846. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  3847. if assigned(sym) and (sym.typ=procsym) then
  3848. begin
  3849. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3850. begin
  3851. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3852. if (pd.proctypeoption = potype_function) and
  3853. is_class_or_interface_or_object(pd.returndef) and
  3854. (pd.visibility >= vis_public) then
  3855. begin
  3856. result:=pd;
  3857. exit;
  3858. end;
  3859. end;
  3860. end;
  3861. objdef:=objdef.childof;
  3862. end;
  3863. end;
  3864. function tobjectdef.search_enumerator_move: tprocdef;
  3865. var
  3866. objdef : tobjectdef;
  3867. sym : tsym;
  3868. i : integer;
  3869. pd : tprocdef;
  3870. hashedid : THashedIDString;
  3871. begin
  3872. result:=nil;
  3873. objdef:=self;
  3874. // first search for po_enumerator_movenext method modifier
  3875. // then search for public function MoveNext: Boolean
  3876. hashedid.id:='MOVENEXT';
  3877. while assigned(objdef) do
  3878. begin
  3879. for i:=0 to objdef.symtable.SymList.Count-1 do
  3880. begin
  3881. sym:=TSym(objdef.symtable.SymList[i]);
  3882. if (sym.typ=procsym) then
  3883. begin
  3884. pd:=Tprocsym(sym).find_procdef_byoptions([po_enumerator_movenext]);
  3885. if assigned(pd) then
  3886. begin
  3887. result:=pd;
  3888. exit;
  3889. end;
  3890. end;
  3891. end;
  3892. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  3893. if assigned(sym) and (sym.typ=procsym) then
  3894. begin
  3895. for i := 0 to Tprocsym(sym).ProcdefList.Count - 1 do
  3896. begin
  3897. pd := tprocdef(Tprocsym(sym).ProcdefList[i]);
  3898. if (pd.proctypeoption = potype_function) and
  3899. is_boolean(pd.returndef) and
  3900. (pd.minparacount = 0) and
  3901. (pd.visibility >= vis_public) then
  3902. begin
  3903. result:=pd;
  3904. exit;
  3905. end;
  3906. end;
  3907. end;
  3908. objdef:=objdef.childof;
  3909. end;
  3910. end;
  3911. function tobjectdef.search_enumerator_current: tsym;
  3912. var
  3913. objdef : tobjectdef;
  3914. sym: tsym;
  3915. i: integer;
  3916. hashedid : THashedIDString;
  3917. begin
  3918. result:=nil;
  3919. objdef:=self;
  3920. hashedid.id:='CURRENT';
  3921. // first search for ppo_enumerator_current property modifier
  3922. // then search for public property Current
  3923. while assigned(objdef) do
  3924. begin
  3925. for i:=0 to objdef.symtable.SymList.Count-1 do
  3926. begin
  3927. sym:=TSym(objdef.symtable.SymList[i]);
  3928. if (sym.typ=propertysym) and (ppo_enumerator_current in tpropertysym(sym).propoptions) then
  3929. begin
  3930. result:=sym;
  3931. exit;
  3932. end;
  3933. end;
  3934. sym:=tsym(objdef.symtable.FindWithHash(hashedid));
  3935. if assigned(sym) and (sym.typ=propertysym) and
  3936. (sym.visibility >= vis_public) and not tpropertysym(sym).propaccesslist[palt_read].empty then
  3937. begin
  3938. result:=sym;
  3939. exit;
  3940. end;
  3941. objdef:=objdef.childof;
  3942. end;
  3943. end;
  3944. procedure tobjectdef.register_created_classref_type;
  3945. begin
  3946. if not classref_created_in_current_module then
  3947. begin
  3948. classref_created_in_current_module:=true;
  3949. current_module.wpoinfo.addcreatedobjtypeforclassref(self);
  3950. end;
  3951. end;
  3952. procedure tobjectdef.register_created_object_type;
  3953. begin
  3954. if not created_in_current_module then
  3955. begin
  3956. created_in_current_module:=true;
  3957. current_module.wpoinfo.addcreatedobjtype(self);
  3958. end;
  3959. end;
  3960. procedure tobjectdef.register_maybe_created_object_type;
  3961. begin
  3962. { if we know it has been created for sure, no need
  3963. to also record that it maybe can be created in
  3964. this module
  3965. }
  3966. if not (created_in_current_module) and
  3967. not (maybe_created_in_current_module) then
  3968. begin
  3969. maybe_created_in_current_module:=true;
  3970. current_module.wpoinfo.addmaybecreatedbyclassref(self);
  3971. end;
  3972. end;
  3973. procedure tobjectdef.register_vmt_call(index: longint);
  3974. begin
  3975. if (is_object(self) or is_class(self)) then
  3976. current_module.wpoinfo.addcalledvmtentry(self,index);
  3977. end;
  3978. procedure make_procdef_external(data: tobject; arg: pointer);
  3979. var
  3980. def: tdef absolute data;
  3981. begin
  3982. if (def.typ = procdef) then
  3983. begin
  3984. include(tprocdef(def).procoptions,po_external);
  3985. tprocdef(def).forwarddef:=false;
  3986. end;
  3987. end;
  3988. procedure tobjectdef.make_all_methods_external;
  3989. begin
  3990. self.symtable.deflist.foreachcall(@make_procdef_external,nil);
  3991. end;
  3992. procedure check_and_finish_msg(data: tobject; arg: pointer);
  3993. var
  3994. def: tdef absolute data;
  3995. pd: tprocdef absolute data;
  3996. i,
  3997. paracount: longint;
  3998. begin
  3999. if (def.typ=procdef) then
  4000. begin
  4001. { we have to wait until now to set the mangled name because it
  4002. depends on the (possibly external) class name, which is defined
  4003. at the very end. }
  4004. if (po_msgstr in pd.procoptions) then
  4005. begin
  4006. { Mangled name is already set in case this is a copy of
  4007. another type. }
  4008. if not(po_has_mangledname in pd.procoptions) then
  4009. begin
  4010. { check whether the number of formal parameters is correct }
  4011. paracount:=0;
  4012. for i:=1 to length(pd.messageinf.str^) do
  4013. if pd.messageinf.str^[i]=':' then
  4014. inc(paracount);
  4015. for i:=0 to pd.paras.count-1 do
  4016. if not(vo_is_hidden_para in tparavarsym(pd.paras[i]).varoptions) and
  4017. not is_array_of_const(tparavarsym(pd.paras[i]).vardef) then
  4018. dec(paracount);
  4019. if (paracount<>0) then
  4020. MessagePos(pd.fileinfo,sym_e_objc_para_mismatch);
  4021. pd.setmangledname(pd.objcmangledname);
  4022. end
  4023. else
  4024. { all checks already done }
  4025. exit;
  4026. end
  4027. else
  4028. MessagePos(pd.fileinfo,parser_e_objc_requires_msgstr);
  4029. if not(oo_is_external in pd._class.objectoptions) then
  4030. begin
  4031. if (po_varargs in pd.procoptions) then
  4032. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external)
  4033. else
  4034. begin
  4035. { check for "array of const" parameters }
  4036. for i:=0 to pd.parast.symlist.count-1 do
  4037. begin
  4038. if (tsym(pd.parast.symlist[i]).typ=paravarsym) and
  4039. is_array_of_const(tparavarsym(pd.parast.symlist[i]).vardef) then
  4040. MessagePos(pd.fileinfo,parser_e_varargs_need_cdecl_and_external);
  4041. end;
  4042. end;
  4043. end;
  4044. end;
  4045. end;
  4046. procedure mark_private_fields_used(data: tobject; arg: pointer);
  4047. var
  4048. sym: tsym absolute data;
  4049. begin
  4050. if (sym.typ=fieldvarsym) and
  4051. (tfieldvarsym(sym).visibility in [vis_private,vis_strictprivate]) then
  4052. sym.IncRefCount;
  4053. end;
  4054. procedure tobjectdef.finish_objc_data;
  4055. begin
  4056. self.symtable.DefList.foreachcall(@check_and_finish_msg,nil);
  4057. if (oo_is_external in objectoptions) then
  4058. self.symtable.SymList.ForEachCall(@mark_private_fields_used,nil);
  4059. end;
  4060. {****************************************************************************
  4061. TImplementedInterface
  4062. ****************************************************************************}
  4063. constructor TImplementedInterface.create(aintf: tobjectdef);
  4064. begin
  4065. inherited create;
  4066. intfdef:=aintf;
  4067. IOffset:=-1;
  4068. IType:=etStandard;
  4069. NameMappings:=nil;
  4070. procdefs:=nil;
  4071. end;
  4072. constructor TImplementedInterface.create_deref(d:tderef);
  4073. begin
  4074. inherited create;
  4075. intfdef:=nil;
  4076. intfdefderef:=d;
  4077. IOffset:=-1;
  4078. IType:=etStandard;
  4079. NameMappings:=nil;
  4080. procdefs:=nil;
  4081. end;
  4082. destructor TImplementedInterface.destroy;
  4083. var
  4084. i : longint;
  4085. mappedname : pshortstring;
  4086. begin
  4087. if assigned(NameMappings) then
  4088. begin
  4089. for i:=0 to NameMappings.Count-1 do
  4090. begin
  4091. mappedname:=pshortstring(NameMappings[i]);
  4092. stringdispose(mappedname);
  4093. end;
  4094. NameMappings.free;
  4095. NameMappings:=nil;
  4096. end;
  4097. if assigned(procdefs) then
  4098. begin
  4099. procdefs.free;
  4100. procdefs:=nil;
  4101. end;
  4102. inherited destroy;
  4103. end;
  4104. procedure TImplementedInterface.buildderef;
  4105. begin
  4106. intfdefderef.build(intfdef);
  4107. end;
  4108. procedure TImplementedInterface.deref;
  4109. begin
  4110. intfdef:=tobjectdef(intfdefderef.resolve);
  4111. end;
  4112. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  4113. begin
  4114. if not assigned(NameMappings) then
  4115. NameMappings:=TFPHashList.Create;
  4116. NameMappings.Add(origname,stringdup(newname));
  4117. end;
  4118. function TImplementedInterface.GetMapping(const origname: string):string;
  4119. var
  4120. mappedname : pshortstring;
  4121. begin
  4122. result:='';
  4123. if not assigned(NameMappings) then
  4124. exit;
  4125. mappedname:=PShortstring(NameMappings.Find(origname));
  4126. if assigned(mappedname) then
  4127. result:=mappedname^;
  4128. end;
  4129. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  4130. begin
  4131. if not assigned(procdefs) then
  4132. procdefs:=TFPObjectList.Create(false);
  4133. { duplicate entries must be stored, because multiple }
  4134. { interfaces can declare methods with the same name }
  4135. { and all of these get their own VMT entry }
  4136. procdefs.Add(pd);
  4137. end;
  4138. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  4139. var
  4140. i : longint;
  4141. begin
  4142. result:=false;
  4143. { interfaces being implemented through delegation are not mergable (FK) }
  4144. if (IType<>etStandard) or (MergingIntf.IType<>etStandard) or not(assigned(ProcDefs)) then
  4145. exit;
  4146. weight:=0;
  4147. { empty interface is mergeable }
  4148. if ProcDefs.Count=0 then
  4149. begin
  4150. result:=true;
  4151. exit;
  4152. end;
  4153. { The interface to merge must at least the number of
  4154. procedures of this interface }
  4155. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  4156. exit;
  4157. for i:=0 to ProcDefs.Count-1 do
  4158. begin
  4159. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  4160. exit;
  4161. end;
  4162. weight:=ProcDefs.Count;
  4163. result:=true;
  4164. end;
  4165. function TImplementedInterface.getcopy:TImplementedInterface;
  4166. begin
  4167. Result:=TImplementedInterface.Create(nil);
  4168. {$warning: this is completely wrong on so many levels...}
  4169. { 1) the procdefs list will be freed once for each copy
  4170. 2) since the procdefs list owns its elements, those will also be freed for each copy
  4171. 3) idem for the name mappings
  4172. }
  4173. Move(pointer(self)^,pointer(result)^,InstanceSize);
  4174. end;
  4175. {****************************************************************************
  4176. TFORWARDDEF
  4177. ****************************************************************************}
  4178. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4179. begin
  4180. inherited create(forwarddef);
  4181. tosymname:=stringdup(s);
  4182. forwardpos:=pos;
  4183. end;
  4184. function tforwarddef.GetTypeName:string;
  4185. begin
  4186. GetTypeName:='unresolved forward to '+tosymname^;
  4187. end;
  4188. destructor tforwarddef.destroy;
  4189. begin
  4190. stringdispose(tosymname);
  4191. inherited destroy;
  4192. end;
  4193. {****************************************************************************
  4194. TUNDEFINEDDEF
  4195. ****************************************************************************}
  4196. constructor tundefineddef.create;
  4197. begin
  4198. inherited create(undefineddef);
  4199. end;
  4200. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4201. begin
  4202. inherited ppuload(undefineddef,ppufile);
  4203. end;
  4204. function tundefineddef.GetTypeName:string;
  4205. begin
  4206. GetTypeName:='<undefined type>';
  4207. end;
  4208. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4209. begin
  4210. inherited ppuwrite(ppufile);
  4211. ppufile.writeentry(ibundefineddef);
  4212. end;
  4213. {****************************************************************************
  4214. TERRORDEF
  4215. ****************************************************************************}
  4216. constructor terrordef.create;
  4217. begin
  4218. inherited create(errordef);
  4219. { prevent consecutive faults }
  4220. savesize:=1;
  4221. end;
  4222. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4223. begin
  4224. { Can't write errordefs to ppu }
  4225. internalerror(200411063);
  4226. end;
  4227. function terrordef.GetTypeName:string;
  4228. begin
  4229. GetTypeName:='<erroneous type>';
  4230. end;
  4231. function terrordef.getmangledparaname:string;
  4232. begin
  4233. getmangledparaname:='error';
  4234. end;
  4235. {****************************************************************************
  4236. Definition Helpers
  4237. ****************************************************************************}
  4238. function is_interfacecom(def: tdef): boolean;
  4239. begin
  4240. is_interfacecom:=
  4241. assigned(def) and
  4242. (def.typ=objectdef) and
  4243. (tobjectdef(def).objecttype=odt_interfacecom);
  4244. end;
  4245. function is_interfacecorba(def: tdef): boolean;
  4246. begin
  4247. is_interfacecorba:=
  4248. assigned(def) and
  4249. (def.typ=objectdef) and
  4250. (tobjectdef(def).objecttype=odt_interfacecorba);
  4251. end;
  4252. function is_interface(def: tdef): boolean;
  4253. begin
  4254. is_interface:=
  4255. assigned(def) and
  4256. (def.typ=objectdef) and
  4257. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4258. end;
  4259. function is_dispinterface(def: tdef): boolean;
  4260. begin
  4261. result:=
  4262. assigned(def) and
  4263. (def.typ=objectdef) and
  4264. (tobjectdef(def).objecttype=odt_dispinterface);
  4265. end;
  4266. function is_class(def: tdef): boolean;
  4267. begin
  4268. is_class:=
  4269. assigned(def) and
  4270. (def.typ=objectdef) and
  4271. (tobjectdef(def).objecttype=odt_class);
  4272. end;
  4273. function is_object(def: tdef): boolean;
  4274. begin
  4275. is_object:=
  4276. assigned(def) and
  4277. (def.typ=objectdef) and
  4278. (tobjectdef(def).objecttype=odt_object);
  4279. end;
  4280. function is_cppclass(def: tdef): boolean;
  4281. begin
  4282. is_cppclass:=
  4283. assigned(def) and
  4284. (def.typ=objectdef) and
  4285. (tobjectdef(def).objecttype=odt_cppclass);
  4286. end;
  4287. function is_objcclass(def: tdef): boolean;
  4288. begin
  4289. is_objcclass:=
  4290. assigned(def) and
  4291. (def.typ=objectdef) and
  4292. (tobjectdef(def).objecttype=odt_objcclass);
  4293. end;
  4294. function is_objcclassref(def: tdef): boolean;
  4295. begin
  4296. is_objcclassref:=
  4297. assigned(def) and
  4298. (def.typ=classrefdef) and
  4299. is_objcclass(tclassrefdef(def).pointeddef);
  4300. end;
  4301. function is_objcprotocol(def: tdef): boolean;
  4302. begin
  4303. result:=
  4304. assigned(def) and
  4305. (def.typ=objectdef) and
  4306. (tobjectdef(def).objecttype=odt_objcprotocol);
  4307. end;
  4308. function is_objc_class_or_protocol(def: tdef): boolean;
  4309. begin
  4310. result:=
  4311. assigned(def) and
  4312. (def.typ=objectdef) and
  4313. (tobjectdef(def).objecttype in [odt_objcclass,odt_objcprotocol]);
  4314. end;
  4315. function is_class_or_interface(def: tdef): boolean;
  4316. begin
  4317. result:=
  4318. assigned(def) and
  4319. (def.typ=objectdef) and
  4320. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4321. end;
  4322. function is_class_or_interface_or_objc(def: tdef): boolean;
  4323. begin
  4324. result:=
  4325. assigned(def) and
  4326. (def.typ=objectdef) and
  4327. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_objcclass,odt_objcprotocol]);
  4328. end;
  4329. function is_class_or_interface_or_object(def: tdef): boolean;
  4330. begin
  4331. result:=
  4332. assigned(def) and
  4333. (def.typ=objectdef) and
  4334. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_object]);
  4335. end;
  4336. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  4337. begin
  4338. result:=
  4339. assigned(def) and
  4340. (def.typ=objectdef) and
  4341. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  4342. end;
  4343. function is_class_or_interface_or_dispinterface_or_objc(def: tdef): boolean;
  4344. begin
  4345. result:=
  4346. assigned(def) and
  4347. (def.typ=objectdef) and
  4348. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface,odt_objcclass,odt_objcprotocol]);
  4349. end;
  4350. procedure loadobjctypes;
  4351. begin
  4352. objc_metaclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_CLASS').typedef);
  4353. objc_superclasstype:=tpointerdef(search_named_unit_globaltype('OBJC','POBJC_SUPER').typedef);
  4354. objc_idtype:=tpointerdef(search_named_unit_globaltype('OBJC','ID').typedef);
  4355. objc_seltype:=tpointerdef(search_named_unit_globaltype('OBJC','SEL').typedef);
  4356. end;
  4357. {$ifdef x86}
  4358. function use_sse(def : tdef) : boolean;
  4359. begin
  4360. use_sse:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  4361. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  4362. end;
  4363. {$endif x86}
  4364. end.