symdef.pas 136 KB

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