symdef.pas 133 KB

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