symdef.pas 133 KB

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