symdef.pas 130 KB

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