symdef.pas 138 KB

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