symdef.pas 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420
  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. end;
  276. torddef = class(tstoreddef)
  277. low,high : TConstExprInt;
  278. ordtype : tordtype;
  279. constructor create(t : tordtype;v,b : TConstExprInt);
  280. constructor ppuload(ppufile:tcompilerppufile);
  281. function getcopy : tstoreddef;override;
  282. procedure ppuwrite(ppufile:tcompilerppufile);override;
  283. function is_publishable : boolean;override;
  284. function GetTypeName:string;override;
  285. function alignment:shortint;override;
  286. procedure setsize;
  287. function packedbitsize: aint; override;
  288. function getvardef : longint;override;
  289. end;
  290. tfloatdef = class(tstoreddef)
  291. floattype : tfloattype;
  292. constructor create(t : tfloattype);
  293. constructor ppuload(ppufile:tcompilerppufile);
  294. function getcopy : tstoreddef;override;
  295. procedure ppuwrite(ppufile:tcompilerppufile);override;
  296. function GetTypeName:string;override;
  297. function is_publishable : boolean;override;
  298. function alignment:shortint;override;
  299. procedure setsize;
  300. function getvardef:longint;override;
  301. end;
  302. tabstractprocdef = class(tstoreddef)
  303. { saves a definition to the return type }
  304. returndef : tdef;
  305. returndefderef : tderef;
  306. parast : TSymtable;
  307. paras : tparalist;
  308. proctypeoption : tproctypeoption;
  309. proccalloption : tproccalloption;
  310. procoptions : tprocoptions;
  311. requiredargarea : aint;
  312. { number of user visibile parameters }
  313. maxparacount,
  314. minparacount : byte;
  315. {$ifdef m68k}
  316. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  317. {$endif}
  318. funcretloc : array[tcallercallee] of TLocation;
  319. has_paraloc_info : boolean; { paraloc info is available }
  320. constructor create(dt:tdeftyp;level:byte);
  321. constructor ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  322. destructor destroy;override;
  323. procedure ppuwrite(ppufile:tcompilerppufile);override;
  324. procedure buildderef;override;
  325. procedure deref;override;
  326. procedure calcparas;
  327. function typename_paras(showhidden:boolean): string;
  328. function is_methodpointer:boolean;virtual;
  329. function is_addressonly:boolean;virtual;
  330. private
  331. procedure count_para(p:TObject;arg:pointer);
  332. procedure insert_para(p:TObject;arg:pointer);
  333. end;
  334. tprocvardef = class(tabstractprocdef)
  335. constructor create(level:byte);
  336. constructor ppuload(ppufile:tcompilerppufile);
  337. function getcopy : tstoreddef;override;
  338. procedure ppuwrite(ppufile:tcompilerppufile);override;
  339. function GetSymtable(t:tGetSymtable):TSymtable;override;
  340. function size : aint;override;
  341. function GetTypeName:string;override;
  342. function is_publishable : boolean;override;
  343. function is_methodpointer:boolean;override;
  344. function is_addressonly:boolean;override;
  345. function getmangledparaname:string;override;
  346. end;
  347. tmessageinf = record
  348. case integer of
  349. 0 : (str : pshortstring);
  350. 1 : (i : longint);
  351. end;
  352. tinlininginfo = record
  353. { node tree }
  354. code : tnode;
  355. flags : tprocinfoflags;
  356. end;
  357. pinlininginfo = ^tinlininginfo;
  358. {$ifdef oldregvars}
  359. { register variables }
  360. pregvarinfo = ^tregvarinfo;
  361. tregvarinfo = record
  362. regvars : array[1..maxvarregs] of tsym;
  363. regvars_para : array[1..maxvarregs] of boolean;
  364. regvars_refs : array[1..maxvarregs] of longint;
  365. fpuregvars : array[1..maxfpuvarregs] of tsym;
  366. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  367. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  368. end;
  369. {$endif oldregvars}
  370. tprocdef = class(tabstractprocdef)
  371. private
  372. _mangledname : pshortstring;
  373. public
  374. messageinf : tmessageinf;
  375. dispid : longint;
  376. extnumber : word;
  377. {$ifndef EXTDEBUG}
  378. { where is this function defined and what were the symbol
  379. flags, needed here because there
  380. is only one symbol for all overloaded functions
  381. EXTDEBUG has fileinfo in tdef (PFV) }
  382. fileinfo : tfileposinfo;
  383. {$endif}
  384. symoptions : tsymoptions;
  385. { symbol owning this definition }
  386. procsym : tsym;
  387. procsymderef : tderef;
  388. { alias names }
  389. aliasnames : TCmdStrList;
  390. { symtables }
  391. localst : TSymtable;
  392. funcretsym : tsym;
  393. funcretsymderef : tderef;
  394. _class : tobjectdef;
  395. _classderef : tderef;
  396. {$if defined(powerpc) or defined(m68k)}
  397. { library symbol for AmigaOS/MorphOS }
  398. libsym : tsym;
  399. libsymderef : tderef;
  400. {$endif powerpc or m68k}
  401. { name of the result variable to insert in the localsymtable }
  402. resultname : pshortstring;
  403. { true, if the procedure is only declared
  404. (forward procedure) }
  405. forwarddef,
  406. { true if the procedure is declared in the interface }
  407. interfacedef : boolean;
  408. { true if the procedure has a forward declaration }
  409. hasforward : boolean;
  410. { import info }
  411. import_dll,
  412. import_name : pshortstring;
  413. import_nr : word;
  414. { info for inlining the subroutine, if this pointer is nil,
  415. the procedure can't be inlined }
  416. inlininginfo : pinlininginfo;
  417. {$ifdef oldregvars}
  418. regvarinfo: pregvarinfo;
  419. {$endif oldregvars}
  420. {$ifdef i386}
  421. fpu_used : byte;
  422. {$endif i386}
  423. { position in aasmoutput list }
  424. procstarttai,
  425. procendtai : tai;
  426. constructor create(level:byte);
  427. constructor ppuload(ppufile:tcompilerppufile);
  428. destructor destroy;override;
  429. procedure ppuwrite(ppufile:tcompilerppufile);override;
  430. procedure buildderef;override;
  431. procedure buildderefimpl;override;
  432. procedure deref;override;
  433. procedure derefimpl;override;
  434. procedure reset;override;
  435. function GetSymtable(t:tGetSymtable):TSymtable;override;
  436. function GetTypeName : string;override;
  437. function mangledname : string;
  438. procedure setmangledname(const s : string);
  439. function fullprocname(showhidden:boolean):string;
  440. function cplusplusmangledname : string;
  441. function is_methodpointer:boolean;override;
  442. function is_addressonly:boolean;override;
  443. function is_visible_for_object(currobjdef,contextobjdef:tobjectdef):boolean;
  444. end;
  445. { single linked list of overloaded procs }
  446. pprocdeflist = ^tprocdeflist;
  447. tprocdeflist = record
  448. def : tprocdef;
  449. defderef : tderef;
  450. next : pprocdeflist;
  451. end;
  452. tstringdef = class(tstoreddef)
  453. stringtype : tstringtype;
  454. len : aint;
  455. constructor createshort(l : byte);
  456. constructor loadshort(ppufile:tcompilerppufile);
  457. constructor createlong(l : aint);
  458. constructor loadlong(ppufile:tcompilerppufile);
  459. constructor createansi;
  460. constructor loadansi(ppufile:tcompilerppufile);
  461. constructor createwide;
  462. constructor loadwide(ppufile:tcompilerppufile);
  463. constructor createunicode;
  464. constructor loadunicode(ppufile:tcompilerppufile);
  465. function getcopy : tstoreddef;override;
  466. function stringtypname:string;
  467. procedure ppuwrite(ppufile:tcompilerppufile);override;
  468. function GetTypeName:string;override;
  469. function getmangledparaname:string;override;
  470. function is_publishable : boolean;override;
  471. function alignment : shortint;override;
  472. function needs_inittable : boolean;override;
  473. function getvardef:longint;override;
  474. end;
  475. tenumdef = class(tstoreddef)
  476. minval,
  477. maxval : aint;
  478. has_jumps : boolean;
  479. firstenum : tsym; {tenumsym}
  480. basedef : tenumdef;
  481. basedefderef : tderef;
  482. constructor create;
  483. constructor create_subrange(_basedef:tenumdef;_min,_max:aint);
  484. constructor ppuload(ppufile:tcompilerppufile);
  485. function getcopy : tstoreddef;override;
  486. procedure ppuwrite(ppufile:tcompilerppufile);override;
  487. procedure buildderef;override;
  488. procedure deref;override;
  489. procedure derefimpl;override;
  490. function GetTypeName:string;override;
  491. function is_publishable : boolean;override;
  492. procedure calcsavesize;
  493. function packedbitsize: aint; override;
  494. procedure setmax(_max:aint);
  495. procedure setmin(_min:aint);
  496. function min:aint;
  497. function max:aint;
  498. end;
  499. tsetdef = class(tstoreddef)
  500. elementdef : tdef;
  501. elementdefderef : tderef;
  502. settype : tsettype;
  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:=(tsetdef(self).settype=smallset);
  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. begin
  1754. settype:=smallset;
  1755. savesize:=Sizeof(longint)
  1756. end
  1757. else if (high<256) then
  1758. begin
  1759. settype:=normset;
  1760. savesize:=32
  1761. end
  1762. else
  1763. savesize:=(high+7) div 8
  1764. end
  1765. else
  1766. begin
  1767. setallocbits:=current_settings.setalloc*8;
  1768. setbase:=low and not(setallocbits-1);
  1769. packedsavesize:=current_settings.setalloc*((((high+setallocbits)-setbase)) DIV setallocbits);
  1770. savesize:=packedsavesize;
  1771. if (packedsavesize<=4) then
  1772. begin
  1773. settype:=smallset;
  1774. if savesize=3 then
  1775. savesize:=4;
  1776. end
  1777. else if (packedsavesize<=32) then
  1778. settype:=normset;
  1779. end;
  1780. end;
  1781. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  1782. begin
  1783. inherited ppuload(setdef,ppufile);
  1784. ppufile.getderef(elementdefderef);
  1785. settype:=tsettype(ppufile.getbyte);
  1786. savesize:=ppufile.getaint;
  1787. setbase:=ppufile.getaint;
  1788. setmax:=ppufile.getaint;
  1789. end;
  1790. function tsetdef.getcopy : tstoreddef;
  1791. begin
  1792. result:=tsetdef.create(elementdef,setbase,setmax);
  1793. { the copy might have been created with a different setalloc setting }
  1794. tsetdef(result).settype:=settype;
  1795. tsetdef(result).savesize:=savesize;
  1796. end;
  1797. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  1798. begin
  1799. inherited ppuwrite(ppufile);
  1800. ppufile.putderef(elementdefderef);
  1801. ppufile.putbyte(byte(settype));
  1802. ppufile.putaint(savesize);
  1803. ppufile.putaint(setbase);
  1804. ppufile.putaint(setmax);
  1805. ppufile.writeentry(ibsetdef);
  1806. end;
  1807. procedure tsetdef.buildderef;
  1808. begin
  1809. inherited buildderef;
  1810. elementdefderef.build(elementdef);
  1811. end;
  1812. procedure tsetdef.deref;
  1813. begin
  1814. inherited deref;
  1815. elementdef:=tdef(elementdefderef.resolve);
  1816. end;
  1817. function tsetdef.is_publishable : boolean;
  1818. begin
  1819. is_publishable:=savesize in [1,2,4];
  1820. end;
  1821. function tsetdef.GetTypeName : string;
  1822. begin
  1823. if assigned(elementdef) then
  1824. GetTypeName:='Set Of '+elementdef.typename
  1825. else
  1826. GetTypeName:='Empty Set';
  1827. end;
  1828. {***************************************************************************
  1829. TFORMALDEF
  1830. ***************************************************************************}
  1831. constructor tformaldef.create(Atyped:boolean);
  1832. begin
  1833. inherited create(formaldef);
  1834. typed:=Atyped;
  1835. savesize:=0;
  1836. end;
  1837. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  1838. begin
  1839. inherited ppuload(formaldef,ppufile);
  1840. typed:=boolean(ppufile.getbyte);
  1841. savesize:=0;
  1842. end;
  1843. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  1844. begin
  1845. inherited ppuwrite(ppufile);
  1846. ppufile.putbyte(byte(typed));
  1847. ppufile.writeentry(ibformaldef);
  1848. end;
  1849. function tformaldef.GetTypeName : string;
  1850. begin
  1851. if typed then
  1852. GetTypeName:='<Typed formal type>'
  1853. else
  1854. GetTypeName:='<Formal type>';
  1855. end;
  1856. {***************************************************************************
  1857. TARRAYDEF
  1858. ***************************************************************************}
  1859. constructor tarraydef.create(l,h : aint;def:tdef);
  1860. begin
  1861. inherited create(arraydef);
  1862. lowrange:=l;
  1863. highrange:=h;
  1864. rangedef:=def;
  1865. _elementdef:=nil;
  1866. arrayoptions:=[];
  1867. end;
  1868. constructor tarraydef.create_from_pointer(def:tdef);
  1869. begin
  1870. { use -1 so that the elecount will not overflow }
  1871. self.create(0,high(aint)-1,s32inttype);
  1872. arrayoptions:=[ado_IsConvertedPointer];
  1873. setelementdef(def);
  1874. end;
  1875. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  1876. begin
  1877. inherited ppuload(arraydef,ppufile);
  1878. { the addresses are calculated later }
  1879. ppufile.getderef(_elementdefderef);
  1880. ppufile.getderef(rangedefderef);
  1881. lowrange:=ppufile.getaint;
  1882. highrange:=ppufile.getaint;
  1883. ppufile.getsmallset(arrayoptions);
  1884. end;
  1885. function tarraydef.getcopy : tstoreddef;
  1886. begin
  1887. result:=tarraydef.create(lowrange,highrange,rangedef);
  1888. tarraydef(result).arrayoptions:=arrayoptions;
  1889. tarraydef(result)._elementdef:=_elementdef;
  1890. end;
  1891. procedure tarraydef.buildderef;
  1892. begin
  1893. inherited buildderef;
  1894. _elementdefderef.build(_elementdef);
  1895. rangedefderef.build(rangedef);
  1896. end;
  1897. procedure tarraydef.deref;
  1898. begin
  1899. inherited deref;
  1900. _elementdef:=tdef(_elementdefderef.resolve);
  1901. rangedef:=tdef(rangedefderef.resolve);
  1902. end;
  1903. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  1904. begin
  1905. inherited ppuwrite(ppufile);
  1906. ppufile.putderef(_elementdefderef);
  1907. ppufile.putderef(rangedefderef);
  1908. ppufile.putaint(lowrange);
  1909. ppufile.putaint(highrange);
  1910. ppufile.putsmallset(arrayoptions);
  1911. ppufile.writeentry(ibarraydef);
  1912. end;
  1913. function tarraydef.elesize : aint;
  1914. begin
  1915. if (ado_IsBitPacked in arrayoptions) then
  1916. internalerror(2006080101);
  1917. if assigned(_elementdef) then
  1918. result:=_elementdef.size
  1919. else
  1920. result:=0;
  1921. end;
  1922. function tarraydef.elepackedbitsize : aint;
  1923. begin
  1924. if not(ado_IsBitPacked in arrayoptions) then
  1925. internalerror(2006080102);
  1926. if assigned(_elementdef) then
  1927. result:=_elementdef.packedbitsize
  1928. else
  1929. result:=0;
  1930. end;
  1931. function tarraydef.elecount : aword;
  1932. var
  1933. qhigh,qlow : qword;
  1934. begin
  1935. if ado_IsDynamicArray in arrayoptions then
  1936. begin
  1937. result:=0;
  1938. exit;
  1939. end;
  1940. if (highrange>0) and (lowrange<0) then
  1941. begin
  1942. qhigh:=highrange;
  1943. qlow:=qword(-lowrange);
  1944. { prevent overflow, return 0 to indicate overflow }
  1945. if qhigh+qlow>qword(high(aint)-1) then
  1946. result:=0
  1947. else
  1948. result:=qhigh+qlow+1;
  1949. end
  1950. else
  1951. result:=int64(highrange)-lowrange+1;
  1952. end;
  1953. function tarraydef.size : aint;
  1954. var
  1955. cachedelecount : aword;
  1956. cachedelesize : aint;
  1957. begin
  1958. if ado_IsDynamicArray in arrayoptions then
  1959. begin
  1960. size:=sizeof(pint);
  1961. exit;
  1962. end;
  1963. { Tarraydef.size may never be called for an open array! }
  1964. if highrange<lowrange then
  1965. internalerror(99080501);
  1966. if not (ado_IsBitPacked in arrayoptions) then
  1967. cachedelesize:=elesize
  1968. else
  1969. cachedelesize := elepackedbitsize;
  1970. cachedelecount:=elecount;
  1971. if (cachedelesize = 0) then
  1972. begin
  1973. size := 0;
  1974. exit;
  1975. end;
  1976. if (cachedelecount = 0) then
  1977. begin
  1978. size := -1;
  1979. exit;
  1980. end;
  1981. { prevent overflow, return -1 to indicate overflow }
  1982. { also make sure we don't need 64/128 bit arithmetic to calculate offsets }
  1983. if (cachedelecount > high(aint)) or
  1984. ((high(aint) div cachedelesize) < cachedelecount) or
  1985. { also lowrange*elesize must be < high(aint) to prevent overflow when
  1986. accessing the array, see ncgmem (PFV) }
  1987. ((high(aint) div cachedelesize) < abs(lowrange)) then
  1988. begin
  1989. result:=-1;
  1990. exit;
  1991. end;
  1992. if (ado_IsBitPacked in arrayoptions) then
  1993. size:=(cachedelesize * cachedelecount + 7) div 8
  1994. else
  1995. result:=cachedelesize*cachedelecount;
  1996. end;
  1997. procedure tarraydef.setelementdef(def:tdef);
  1998. begin
  1999. _elementdef:=def;
  2000. if not(
  2001. (ado_IsDynamicArray in arrayoptions) or
  2002. (ado_IsConvertedPointer in arrayoptions) or
  2003. (highrange<lowrange)
  2004. ) and
  2005. (size=-1) then
  2006. Message(sym_e_segment_too_large);
  2007. end;
  2008. function tarraydef.alignment : shortint;
  2009. begin
  2010. { alignment of dyn. arrays doesn't depend on the element size }
  2011. if (ado_IsDynamicArray in arrayoptions) then
  2012. alignment:=size_2_align(sizeof(pint))
  2013. { alignment is the size of the elements }
  2014. else if (elementdef.typ in [arraydef,recorddef]) or
  2015. ((elementdef.typ=objectdef) and
  2016. is_object(elementdef)) then
  2017. alignment:=elementdef.alignment
  2018. else if not (ado_IsBitPacked in arrayoptions) then
  2019. alignment:=size_2_align(elesize)
  2020. else
  2021. alignment:=packedbitsloadsize(elepackedbitsize);
  2022. end;
  2023. function tarraydef.needs_inittable : boolean;
  2024. begin
  2025. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementdef.needs_inittable;
  2026. end;
  2027. function tarraydef.GetTypeName : string;
  2028. begin
  2029. if (ado_IsConstString in arrayoptions) then
  2030. result:='Constant String'
  2031. else if (ado_isarrayofconst in arrayoptions) or
  2032. (ado_isConstructor in arrayoptions) then
  2033. begin
  2034. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2035. GetTypeName:='Array Of Const'
  2036. else
  2037. GetTypeName:='Array Of Const/Constant Open Array of '+elementdef.typename;
  2038. end
  2039. else if (ado_IsDynamicArray in arrayoptions) then
  2040. GetTypeName:='Dynamic Array Of '+elementdef.typename
  2041. else if ((highrange=-1) and (lowrange=0)) then
  2042. GetTypeName:='Open Array Of '+elementdef.typename
  2043. else
  2044. begin
  2045. result := '';
  2046. if (ado_IsBitPacked in arrayoptions) then
  2047. result:='Packed ';
  2048. if rangedef.typ=enumdef then
  2049. result:=result+'Array['+rangedef.typename+'] Of '+elementdef.typename
  2050. else
  2051. result:=result+'Array['+tostr(lowrange)+'..'+
  2052. tostr(highrange)+'] Of '+elementdef.typename
  2053. end;
  2054. end;
  2055. function tarraydef.getmangledparaname : string;
  2056. begin
  2057. if ado_isarrayofconst in arrayoptions then
  2058. getmangledparaname:='array_of_const'
  2059. else
  2060. if ((highrange=-1) and (lowrange=0)) then
  2061. getmangledparaname:='array_of_'+elementdef.mangledparaname
  2062. else
  2063. internalerror(200204176);
  2064. end;
  2065. {***************************************************************************
  2066. tabstractrecorddef
  2067. ***************************************************************************}
  2068. function tabstractrecorddef.GetSymtable(t:tGetSymtable):TSymtable;
  2069. begin
  2070. if t=gs_record then
  2071. GetSymtable:=symtable
  2072. else
  2073. GetSymtable:=nil;
  2074. end;
  2075. procedure tabstractrecorddef.reset;
  2076. begin
  2077. inherited reset;
  2078. tstoredsymtable(symtable).reset_all_defs;
  2079. end;
  2080. function tabstractrecorddef.is_packed:boolean;
  2081. begin
  2082. result:=tabstractrecordsymtable(symtable).is_packed;
  2083. end;
  2084. {***************************************************************************
  2085. trecorddef
  2086. ***************************************************************************}
  2087. constructor trecorddef.create(p : TSymtable);
  2088. begin
  2089. inherited create(recorddef);
  2090. symtable:=p;
  2091. symtable.defowner:=self;
  2092. isunion:=false;
  2093. end;
  2094. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2095. begin
  2096. inherited ppuload(recorddef,ppufile);
  2097. symtable:=trecordsymtable.create(0);
  2098. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2099. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2100. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2101. trecordsymtable(symtable).usefieldalignment:=shortint(ppufile.getbyte);
  2102. { requires usefieldalignment to be set }
  2103. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2104. trecordsymtable(symtable).ppuload(ppufile);
  2105. symtable.defowner:=self;
  2106. isunion:=false;
  2107. end;
  2108. destructor trecorddef.destroy;
  2109. begin
  2110. if assigned(symtable) then
  2111. begin
  2112. symtable.free;
  2113. symtable:=nil;
  2114. end;
  2115. inherited destroy;
  2116. end;
  2117. function trecorddef.getcopy : tstoreddef;
  2118. begin
  2119. result:=trecorddef.create(symtable.getcopy);
  2120. trecorddef(result).isunion:=isunion;
  2121. end;
  2122. function trecorddef.needs_inittable : boolean;
  2123. begin
  2124. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2125. end;
  2126. procedure trecorddef.buildderef;
  2127. begin
  2128. inherited buildderef;
  2129. tstoredsymtable(symtable).buildderef;
  2130. end;
  2131. procedure trecorddef.deref;
  2132. begin
  2133. inherited deref;
  2134. { now dereference the definitions }
  2135. tstoredsymtable(symtable).deref;
  2136. { assign TGUID? load only from system unit }
  2137. if not(assigned(rec_tguid)) and
  2138. (upper(typename)='TGUID') and
  2139. assigned(owner) and
  2140. assigned(owner.name) and
  2141. (owner.name^='SYSTEM') then
  2142. rec_tguid:=self;
  2143. end;
  2144. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2145. begin
  2146. inherited ppuwrite(ppufile);
  2147. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2148. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2149. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2150. ppufile.putbyte(byte(trecordsymtable(symtable).usefieldalignment));
  2151. ppufile.putaint(trecordsymtable(symtable).datasize);
  2152. ppufile.writeentry(ibrecorddef);
  2153. trecordsymtable(symtable).ppuwrite(ppufile);
  2154. end;
  2155. function trecorddef.size:aint;
  2156. begin
  2157. result:=trecordsymtable(symtable).datasize;
  2158. end;
  2159. function trecorddef.alignment:shortint;
  2160. begin
  2161. alignment:=trecordsymtable(symtable).recordalignment;
  2162. end;
  2163. function trecorddef.padalignment:shortint;
  2164. begin
  2165. padalignment := trecordsymtable(symtable).padalignment;
  2166. end;
  2167. function trecorddef.GetTypeName : string;
  2168. begin
  2169. GetTypeName:='<record type>'
  2170. end;
  2171. {***************************************************************************
  2172. TABSTRACTPROCDEF
  2173. ***************************************************************************}
  2174. constructor tabstractprocdef.create(dt:tdeftyp;level:byte);
  2175. begin
  2176. inherited create(dt);
  2177. parast:=tparasymtable.create(self,level);
  2178. paras:=nil;
  2179. minparacount:=0;
  2180. maxparacount:=0;
  2181. proctypeoption:=potype_none;
  2182. proccalloption:=pocall_none;
  2183. procoptions:=[];
  2184. returndef:=voidtype;
  2185. savesize:=sizeof(pint);
  2186. requiredargarea:=0;
  2187. has_paraloc_info:=false;
  2188. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2189. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2190. end;
  2191. destructor tabstractprocdef.destroy;
  2192. begin
  2193. if assigned(paras) then
  2194. begin
  2195. {$ifdef MEMDEBUG}
  2196. memprocpara.start;
  2197. {$endif MEMDEBUG}
  2198. paras.free;
  2199. paras:=nil;
  2200. {$ifdef MEMDEBUG}
  2201. memprocpara.stop;
  2202. {$endif MEMDEBUG}
  2203. end;
  2204. if assigned(parast) then
  2205. begin
  2206. {$ifdef MEMDEBUG}
  2207. memprocparast.start;
  2208. {$endif MEMDEBUG}
  2209. parast.free;
  2210. parast:=nil;
  2211. {$ifdef MEMDEBUG}
  2212. memprocparast.stop;
  2213. {$endif MEMDEBUG}
  2214. end;
  2215. inherited destroy;
  2216. end;
  2217. procedure tabstractprocdef.count_para(p:TObject;arg:pointer);
  2218. begin
  2219. if (tsym(p).typ<>paravarsym) then
  2220. exit;
  2221. inc(plongint(arg)^);
  2222. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2223. begin
  2224. if not assigned(tparavarsym(p).defaultconstsym) then
  2225. inc(minparacount);
  2226. inc(maxparacount);
  2227. end;
  2228. end;
  2229. procedure tabstractprocdef.insert_para(p:TObject;arg:pointer);
  2230. begin
  2231. if (tsym(p).typ<>paravarsym) then
  2232. exit;
  2233. paras.add(p);
  2234. end;
  2235. procedure tabstractprocdef.calcparas;
  2236. var
  2237. paracount : longint;
  2238. begin
  2239. { This can already be assigned when
  2240. we need to reresolve this unit (PFV) }
  2241. if assigned(paras) then
  2242. paras.free;
  2243. paras:=tparalist.create(false);
  2244. paracount:=0;
  2245. minparacount:=0;
  2246. maxparacount:=0;
  2247. parast.SymList.ForEachCall(@count_para,@paracount);
  2248. paras.capacity:=paracount;
  2249. { Insert parameters in table }
  2250. parast.SymList.ForEachCall(@insert_para,nil);
  2251. { Order parameters }
  2252. paras.sortparas;
  2253. end;
  2254. procedure tabstractprocdef.buildderef;
  2255. begin
  2256. { released procdef? }
  2257. if not assigned(parast) then
  2258. exit;
  2259. inherited buildderef;
  2260. returndefderef.build(returndef);
  2261. { parast }
  2262. tparasymtable(parast).buildderef;
  2263. end;
  2264. procedure tabstractprocdef.deref;
  2265. begin
  2266. inherited deref;
  2267. returndef:=tdef(returndefderef.resolve);
  2268. { parast }
  2269. tparasymtable(parast).deref;
  2270. { recalculated parameters }
  2271. calcparas;
  2272. end;
  2273. constructor tabstractprocdef.ppuload(dt:tdeftyp;ppufile:tcompilerppufile);
  2274. var
  2275. b : byte;
  2276. begin
  2277. inherited ppuload(dt,ppufile);
  2278. parast:=nil;
  2279. Paras:=nil;
  2280. minparacount:=0;
  2281. maxparacount:=0;
  2282. ppufile.getderef(returndefderef);
  2283. {$warning TODO remove fpu_used loading}
  2284. ppufile.getbyte;
  2285. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2286. proccalloption:=tproccalloption(ppufile.getbyte);
  2287. ppufile.getnormalset(procoptions);
  2288. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2289. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2290. if po_explicitparaloc in procoptions then
  2291. begin
  2292. b:=ppufile.getbyte;
  2293. if b<>sizeof(funcretloc[callerside]) then
  2294. internalerror(200411154);
  2295. ppufile.getdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2296. end;
  2297. savesize:=sizeof(pint);
  2298. has_paraloc_info:=(po_explicitparaloc in procoptions);
  2299. end;
  2300. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2301. var
  2302. oldintfcrc : boolean;
  2303. begin
  2304. { released procdef? }
  2305. if not assigned(parast) then
  2306. exit;
  2307. inherited ppuwrite(ppufile);
  2308. ppufile.putderef(returndefderef);
  2309. oldintfcrc:=ppufile.do_interface_crc;
  2310. ppufile.do_interface_crc:=false;
  2311. ppufile.putbyte(0);
  2312. ppufile.putbyte(ord(proctypeoption));
  2313. ppufile.putbyte(ord(proccalloption));
  2314. ppufile.putnormalset(procoptions);
  2315. ppufile.do_interface_crc:=oldintfcrc;
  2316. if (po_explicitparaloc in procoptions) then
  2317. begin
  2318. { Make a 'valid' funcretloc for procedures }
  2319. ppufile.putbyte(sizeof(funcretloc[callerside]));
  2320. ppufile.putdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2321. end;
  2322. end;
  2323. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2324. var
  2325. hs,s : string;
  2326. hp : TParavarsym;
  2327. hpc : tconstsym;
  2328. first : boolean;
  2329. i : integer;
  2330. begin
  2331. s:='';
  2332. first:=true;
  2333. for i:=0 to paras.count-1 do
  2334. begin
  2335. hp:=tparavarsym(paras[i]);
  2336. if not(vo_is_hidden_para in hp.varoptions) or
  2337. (showhidden) then
  2338. begin
  2339. if first then
  2340. begin
  2341. s:=s+'(';
  2342. first:=false;
  2343. end
  2344. else
  2345. s:=s+',';
  2346. if vo_is_hidden_para in hp.varoptions then
  2347. s:=s+'<';
  2348. case hp.varspez of
  2349. vs_var :
  2350. s:=s+'var';
  2351. vs_const :
  2352. s:=s+'const';
  2353. vs_out :
  2354. s:=s+'out';
  2355. end;
  2356. if assigned(hp.vardef.typesym) then
  2357. begin
  2358. if s<>'(' then
  2359. s:=s+' ';
  2360. hs:=hp.vardef.typesym.realname;
  2361. if hs[1]<>'$' then
  2362. s:=s+hp.vardef.typesym.realname
  2363. else
  2364. s:=s+hp.vardef.GetTypeName;
  2365. end
  2366. else
  2367. s:=s+hp.vardef.GetTypeName;
  2368. { default value }
  2369. if assigned(hp.defaultconstsym) then
  2370. begin
  2371. hpc:=tconstsym(hp.defaultconstsym);
  2372. hs:='';
  2373. case hpc.consttyp of
  2374. conststring,
  2375. constresourcestring :
  2376. hs:=strpas(pchar(hpc.value.valueptr));
  2377. constreal :
  2378. str(pbestreal(hpc.value.valueptr)^,hs);
  2379. constpointer :
  2380. hs:=tostr(hpc.value.valueordptr);
  2381. constord :
  2382. begin
  2383. if is_boolean(hpc.constdef) then
  2384. begin
  2385. if hpc.value.valueord<>0 then
  2386. hs:='TRUE'
  2387. else
  2388. hs:='FALSE';
  2389. end
  2390. else
  2391. hs:=tostr(hpc.value.valueord);
  2392. end;
  2393. constnil :
  2394. hs:='nil';
  2395. constset :
  2396. hs:='<set>';
  2397. end;
  2398. if hs<>'' then
  2399. s:=s+'="'+hs+'"';
  2400. end;
  2401. if vo_is_hidden_para in hp.varoptions then
  2402. s:=s+'>';
  2403. end;
  2404. end;
  2405. if not first then
  2406. s:=s+')';
  2407. if (po_varargs in procoptions) then
  2408. s:=s+';VarArgs';
  2409. typename_paras:=s;
  2410. end;
  2411. function tabstractprocdef.is_methodpointer:boolean;
  2412. begin
  2413. result:=false;
  2414. end;
  2415. function tabstractprocdef.is_addressonly:boolean;
  2416. begin
  2417. result:=true;
  2418. end;
  2419. {***************************************************************************
  2420. TPROCDEF
  2421. ***************************************************************************}
  2422. constructor tprocdef.create(level:byte);
  2423. begin
  2424. inherited create(procdef,level);
  2425. localst:=tlocalsymtable.create(self,parast.symtablelevel);
  2426. _mangledname:=nil;
  2427. fileinfo:=current_filepos;
  2428. extnumber:=$ffff;
  2429. aliasnames:=TCmdStrList.create;
  2430. funcretsym:=nil;
  2431. forwarddef:=true;
  2432. interfacedef:=false;
  2433. hasforward:=false;
  2434. _class := nil;
  2435. import_dll:=nil;
  2436. import_name:=nil;
  2437. import_nr:=0;
  2438. inlininginfo:=nil;
  2439. {$ifdef i386}
  2440. fpu_used:=maxfpuregs;
  2441. {$endif i386}
  2442. end;
  2443. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2444. var
  2445. i,aliasnamescount : longint;
  2446. item : TCmdStrListItem;
  2447. level : byte;
  2448. begin
  2449. inherited ppuload(procdef,ppufile);
  2450. if po_has_mangledname in procoptions then
  2451. _mangledname:=stringdup(ppufile.getstring)
  2452. else
  2453. _mangledname:=nil;
  2454. extnumber:=ppufile.getword;
  2455. level:=ppufile.getbyte;
  2456. ppufile.getderef(_classderef);
  2457. ppufile.getderef(procsymderef);
  2458. ppufile.getposinfo(fileinfo);
  2459. ppufile.getsmallset(symoptions);
  2460. {$ifdef powerpc}
  2461. { library symbol for AmigaOS/MorphOS }
  2462. ppufile.getderef(libsymderef);
  2463. {$endif powerpc}
  2464. { import stuff }
  2465. if po_has_importdll in procoptions then
  2466. import_dll:=stringdup(ppufile.getstring)
  2467. else
  2468. import_dll:=nil;
  2469. if po_has_importname in procoptions then
  2470. import_name:=stringdup(ppufile.getstring)
  2471. else
  2472. import_name:=nil;
  2473. import_nr:=ppufile.getword;
  2474. if (po_msgint in procoptions) then
  2475. messageinf.i:=ppufile.getlongint;
  2476. if (po_msgstr in procoptions) then
  2477. messageinf.str:=stringdup(ppufile.getstring);
  2478. if (po_dispid in procoptions) then
  2479. dispid:=ppufile.getlongint;
  2480. { inline stuff }
  2481. if (po_has_inlininginfo in procoptions) then
  2482. begin
  2483. ppufile.getderef(funcretsymderef);
  2484. new(inlininginfo);
  2485. ppufile.getsmallset(inlininginfo^.flags);
  2486. end
  2487. else
  2488. begin
  2489. inlininginfo:=nil;
  2490. funcretsym:=nil;
  2491. end;
  2492. aliasnames:=TCmdStrList.create;
  2493. { count alias names }
  2494. aliasnamescount:=ppufile.getbyte;
  2495. for i:=1 to aliasnamescount do
  2496. aliasnames.insert(ppufile.getstring);
  2497. { load para symtable }
  2498. parast:=tparasymtable.create(self,level);
  2499. tparasymtable(parast).ppuload(ppufile);
  2500. { load local symtable }
  2501. if (po_has_inlininginfo in procoptions) then
  2502. begin
  2503. localst:=tlocalsymtable.create(self,level);
  2504. tlocalsymtable(localst).ppuload(ppufile);
  2505. end
  2506. else
  2507. localst:=nil;
  2508. { inline stuff }
  2509. if (po_has_inlininginfo in procoptions) then
  2510. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2511. { default values for no persistent data }
  2512. if (cs_link_deffile in current_settings.globalswitches) and
  2513. (tf_need_export in target_info.flags) and
  2514. (po_exports in procoptions) then
  2515. deffile.AddExport(mangledname);
  2516. forwarddef:=false;
  2517. interfacedef:=false;
  2518. hasforward:=false;
  2519. { Disable po_has_inlining until the derefimpl is done }
  2520. exclude(procoptions,po_has_inlininginfo);
  2521. {$ifdef i386}
  2522. fpu_used:=maxfpuregs;
  2523. {$endif i386}
  2524. end;
  2525. destructor tprocdef.destroy;
  2526. begin
  2527. aliasnames.free;
  2528. aliasnames:=nil;
  2529. if assigned(localst) and
  2530. (localst.symtabletype<>staticsymtable) then
  2531. begin
  2532. {$ifdef MEMDEBUG}
  2533. memproclocalst.start;
  2534. {$endif MEMDEBUG}
  2535. localst.free;
  2536. localst:=nil;
  2537. {$ifdef MEMDEBUG}
  2538. memproclocalst.start;
  2539. {$endif MEMDEBUG}
  2540. end;
  2541. if assigned(inlininginfo) then
  2542. begin
  2543. {$ifdef MEMDEBUG}
  2544. memprocnodetree.start;
  2545. {$endif MEMDEBUG}
  2546. tnode(inlininginfo^.code).free;
  2547. {$ifdef MEMDEBUG}
  2548. memprocnodetree.start;
  2549. {$endif MEMDEBUG}
  2550. dispose(inlininginfo);
  2551. inlininginfo:=nil;
  2552. end;
  2553. stringdispose(resultname);
  2554. stringdispose(import_dll);
  2555. stringdispose(import_name);
  2556. if (po_msgstr in procoptions) then
  2557. stringdispose(messageinf.str);
  2558. if assigned(_mangledname) then
  2559. begin
  2560. {$ifdef MEMDEBUG}
  2561. memmanglednames.start;
  2562. {$endif MEMDEBUG}
  2563. stringdispose(_mangledname);
  2564. {$ifdef MEMDEBUG}
  2565. memmanglednames.stop;
  2566. {$endif MEMDEBUG}
  2567. end;
  2568. inherited destroy;
  2569. end;
  2570. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2571. var
  2572. oldintfcrc : boolean;
  2573. aliasnamescount : longint;
  2574. item : TCmdStrListItem;
  2575. begin
  2576. { released procdef? }
  2577. if not assigned(parast) then
  2578. exit;
  2579. inherited ppuwrite(ppufile);
  2580. if po_has_mangledname in procoptions then
  2581. ppufile.putstring(_mangledname^);
  2582. ppufile.putword(extnumber);
  2583. ppufile.putbyte(parast.symtablelevel);
  2584. ppufile.putderef(_classderef);
  2585. ppufile.putderef(procsymderef);
  2586. ppufile.putposinfo(fileinfo);
  2587. ppufile.putsmallset(symoptions);
  2588. {$ifdef powerpc}
  2589. { library symbol for AmigaOS/MorphOS }
  2590. ppufile.putderef(libsymderef);
  2591. {$endif powerpc}
  2592. { import }
  2593. if po_has_importdll in procoptions then
  2594. ppufile.putstring(import_dll^);
  2595. if po_has_importname in procoptions then
  2596. ppufile.putstring(import_name^);
  2597. ppufile.putword(import_nr);
  2598. if (po_msgint in procoptions) then
  2599. ppufile.putlongint(messageinf.i);
  2600. if (po_msgstr in procoptions) then
  2601. ppufile.putstring(messageinf.str^);
  2602. if (po_dispid in procoptions) then
  2603. ppufile.putlongint(dispid);
  2604. { inline stuff }
  2605. oldintfcrc:=ppufile.do_crc;
  2606. ppufile.do_crc:=false;
  2607. if (po_has_inlininginfo in procoptions) then
  2608. begin
  2609. ppufile.putderef(funcretsymderef);
  2610. ppufile.putsmallset(inlininginfo^.flags);
  2611. end;
  2612. { count alias names }
  2613. aliasnamescount:=0;
  2614. item:=TCmdStrListItem(aliasnames.first);
  2615. while assigned(item) do
  2616. begin
  2617. inc(aliasnamescount);
  2618. item:=TCmdStrListItem(item.next);
  2619. end;
  2620. if aliasnamescount>255 then
  2621. internalerror(200711021);
  2622. ppufile.putbyte(aliasnamescount);
  2623. item:=TCmdStrListItem(aliasnames.first);
  2624. while assigned(item) do
  2625. begin
  2626. ppufile.putstring(item.str);
  2627. item:=TCmdStrListItem(item.next);
  2628. end;
  2629. ppufile.do_crc:=oldintfcrc;
  2630. { write this entry }
  2631. ppufile.writeentry(ibprocdef);
  2632. { Save the para symtable, this is taken from the interface }
  2633. tparasymtable(parast).ppuwrite(ppufile);
  2634. { save localsymtable for inline procedures or when local
  2635. browser info is requested, this has no influence on the crc }
  2636. if (po_has_inlininginfo in procoptions) then
  2637. begin
  2638. oldintfcrc:=ppufile.do_crc;
  2639. ppufile.do_crc:=false;
  2640. tlocalsymtable(localst).ppuwrite(ppufile);
  2641. ppufile.do_crc:=oldintfcrc;
  2642. end;
  2643. { node tree for inlining }
  2644. oldintfcrc:=ppufile.do_crc;
  2645. ppufile.do_crc:=false;
  2646. if (po_has_inlininginfo in procoptions) then
  2647. ppuwritenodetree(ppufile,inlininginfo^.code);
  2648. ppufile.do_crc:=oldintfcrc;
  2649. end;
  2650. procedure tprocdef.reset;
  2651. begin
  2652. inherited reset;
  2653. procstarttai:=nil;
  2654. procendtai:=nil;
  2655. end;
  2656. function tprocdef.fullprocname(showhidden:boolean):string;
  2657. var
  2658. s : string;
  2659. t : ttoken;
  2660. begin
  2661. {$ifdef EXTDEBUG}
  2662. showhidden:=true;
  2663. {$endif EXTDEBUG}
  2664. s:='';
  2665. if owner.symtabletype=localsymtable then
  2666. s:=s+'local ';
  2667. if assigned(_class) then
  2668. begin
  2669. if po_classmethod in procoptions then
  2670. s:=s+'class ';
  2671. s:=s+_class.objrealname^+'.';
  2672. end;
  2673. if proctypeoption=potype_operator then
  2674. begin
  2675. for t:=NOTOKEN to last_overloaded do
  2676. if procsym.realname='$'+overloaded_names[t] then
  2677. begin
  2678. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2679. break;
  2680. end;
  2681. end
  2682. else
  2683. s:=s+procsym.realname+typename_paras(showhidden);
  2684. case proctypeoption of
  2685. potype_constructor:
  2686. s:='constructor '+s;
  2687. potype_destructor:
  2688. s:='destructor '+s;
  2689. else
  2690. if assigned(returndef) and
  2691. not(is_void(returndef)) then
  2692. s:=s+':'+returndef.GetTypeName;
  2693. end;
  2694. { forced calling convention? }
  2695. if (po_hascallingconvention in procoptions) then
  2696. s:=s+';'+ProcCallOptionStr[proccalloption];
  2697. fullprocname:=s;
  2698. end;
  2699. function tprocdef.is_methodpointer:boolean;
  2700. begin
  2701. result:=assigned(_class);
  2702. end;
  2703. function tprocdef.is_addressonly:boolean;
  2704. begin
  2705. result:=assigned(owner) and
  2706. (owner.symtabletype<>ObjectSymtable);
  2707. end;
  2708. function tprocdef.is_visible_for_object(currobjdef,contextobjdef:tobjectdef):boolean;
  2709. var
  2710. contextst : TSymtable;
  2711. begin
  2712. result:=false;
  2713. { Support passing a context in which module we are to find protected members }
  2714. if assigned(contextobjdef) then
  2715. contextst:=contextobjdef.owner
  2716. else
  2717. contextst:=nil;
  2718. { private symbols are allowed when we are in the same
  2719. module as they are defined }
  2720. if (sp_private in symoptions) and
  2721. (owner.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  2722. not(owner.defowner.owner.iscurrentunit or (owner.defowner.owner=contextst)) then
  2723. exit;
  2724. if (sp_strictprivate in symoptions) then
  2725. begin
  2726. result:=currobjdef=tobjectdef(owner.defowner);
  2727. exit;
  2728. end;
  2729. if (sp_strictprotected in symoptions) then
  2730. begin
  2731. result:=assigned(currobjdef) and
  2732. currobjdef.is_related(tobjectdef(owner.defowner));
  2733. exit;
  2734. end;
  2735. { protected symbols are visible in the module that defines them and
  2736. also visible to related objects. The related object must be defined
  2737. in the current module }
  2738. if (sp_protected in symoptions) and
  2739. (
  2740. (
  2741. (owner.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  2742. not((owner.defowner.owner.iscurrentunit) or (owner.defowner.owner=contextst))
  2743. ) and
  2744. not(
  2745. assigned(currobjdef) and
  2746. (currobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  2747. (currobjdef.owner.iscurrentunit) and
  2748. currobjdef.is_related(tobjectdef(owner.defowner))
  2749. )
  2750. ) then
  2751. exit;
  2752. result:=true;
  2753. end;
  2754. function tprocdef.GetSymtable(t:tGetSymtable):TSymtable;
  2755. begin
  2756. case t of
  2757. gs_local :
  2758. GetSymtable:=localst;
  2759. gs_para :
  2760. GetSymtable:=parast;
  2761. else
  2762. GetSymtable:=nil;
  2763. end;
  2764. end;
  2765. procedure tprocdef.buildderef;
  2766. begin
  2767. inherited buildderef;
  2768. _classderef.build(_class);
  2769. { procsym that originaly defined this definition, should be in the
  2770. same symtable }
  2771. procsymderef.build(procsym);
  2772. {$ifdef powerpc}
  2773. { library symbol for AmigaOS/MorphOS }
  2774. libsymderef.build(libsym);
  2775. {$endif powerpc}
  2776. end;
  2777. procedure tprocdef.buildderefimpl;
  2778. begin
  2779. inherited buildderefimpl;
  2780. { Localst is not available for main/unit init }
  2781. if assigned(localst) then
  2782. begin
  2783. tlocalsymtable(localst).buildderef;
  2784. tlocalsymtable(localst).buildderefimpl;
  2785. end;
  2786. { inline tree }
  2787. if (po_has_inlininginfo in procoptions) then
  2788. begin
  2789. funcretsymderef.build(funcretsym);
  2790. inlininginfo^.code.buildderefimpl;
  2791. end;
  2792. end;
  2793. procedure tprocdef.deref;
  2794. begin
  2795. inherited deref;
  2796. _class:=tobjectdef(_classderef.resolve);
  2797. { procsym that originaly defined this definition, should be in the
  2798. same symtable }
  2799. procsym:=tprocsym(procsymderef.resolve);
  2800. {$ifdef powerpc}
  2801. { library symbol for AmigaOS/MorphOS }
  2802. libsym:=tsym(libsymderef.resolve);
  2803. {$endif powerpc}
  2804. end;
  2805. procedure tprocdef.derefimpl;
  2806. begin
  2807. { Enable has_inlininginfo when the inlininginfo
  2808. structure is available. The has_inlininginfo was disabled
  2809. after the load, since the data was invalid }
  2810. if assigned(inlininginfo) then
  2811. include(procoptions,po_has_inlininginfo);
  2812. { Locals }
  2813. if assigned(localst) then
  2814. begin
  2815. tlocalsymtable(localst).deref;
  2816. tlocalsymtable(localst).derefimpl;
  2817. end;
  2818. { Inline }
  2819. if (po_has_inlininginfo in procoptions) then
  2820. begin
  2821. inlininginfo^.code.derefimpl;
  2822. { funcretsym, this is always located in the localst }
  2823. funcretsym:=tsym(funcretsymderef.resolve);
  2824. end
  2825. else
  2826. begin
  2827. { safety }
  2828. funcretsym:=nil;
  2829. end;
  2830. end;
  2831. function tprocdef.GetTypeName : string;
  2832. begin
  2833. GetTypeName := FullProcName(false);
  2834. end;
  2835. function tprocdef.mangledname : string;
  2836. var
  2837. hp : TParavarsym;
  2838. hs : string;
  2839. crc : dword;
  2840. newlen,
  2841. oldlen,
  2842. i : integer;
  2843. begin
  2844. if assigned(_mangledname) then
  2845. begin
  2846. {$ifdef compress}
  2847. mangledname:=minilzw_decode(_mangledname^);
  2848. {$else}
  2849. mangledname:=_mangledname^;
  2850. {$endif}
  2851. exit;
  2852. end;
  2853. { we need to use the symtable where the procsym is inserted,
  2854. because that is visible to the world }
  2855. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  2856. oldlen:=length(mangledname);
  2857. { add parameter types }
  2858. for i:=0 to paras.count-1 do
  2859. begin
  2860. hp:=tparavarsym(paras[i]);
  2861. if not(vo_is_hidden_para in hp.varoptions) then
  2862. mangledname:=mangledname+'$'+hp.vardef.mangledparaname;
  2863. end;
  2864. { add resultdef, add $$ as separator to make it unique from a
  2865. parameter separator }
  2866. if not is_void(returndef) then
  2867. mangledname:=mangledname+'$$'+returndef.mangledparaname;
  2868. newlen:=length(mangledname);
  2869. { Replace with CRC if the parameter line is very long }
  2870. if (newlen-oldlen>12) and
  2871. ((newlen>128) or (newlen-oldlen>64)) then
  2872. begin
  2873. crc:=0;
  2874. for i:=0 to paras.count-1 do
  2875. begin
  2876. hp:=tparavarsym(paras[i]);
  2877. if not(vo_is_hidden_para in hp.varoptions) then
  2878. begin
  2879. hs:=hp.vardef.mangledparaname;
  2880. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2881. end;
  2882. end;
  2883. hs:=hp.vardef.mangledparaname;
  2884. crc:=UpdateCrc32(crc,hs[1],length(hs));
  2885. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  2886. end;
  2887. {$ifdef compress}
  2888. _mangledname:=stringdup(minilzw_encode(mangledname));
  2889. {$else}
  2890. _mangledname:=stringdup(mangledname);
  2891. {$endif}
  2892. end;
  2893. function tprocdef.cplusplusmangledname : string;
  2894. function getcppparaname(p : tdef) : string;
  2895. const
  2896. ordtype2str : array[tordtype] of string[2] = (
  2897. '',
  2898. 'Uc','Us','Ui','Us',
  2899. 'Sc','s','i','x',
  2900. 'b','b','b','b','b',
  2901. 'c','w','x');
  2902. var
  2903. s : string;
  2904. begin
  2905. case p.typ of
  2906. orddef:
  2907. s:=ordtype2str[torddef(p).ordtype];
  2908. pointerdef:
  2909. s:='P'+getcppparaname(tpointerdef(p).pointeddef);
  2910. else
  2911. internalerror(2103001);
  2912. end;
  2913. getcppparaname:=s;
  2914. end;
  2915. var
  2916. s,s2 : string;
  2917. hp : TParavarsym;
  2918. i : integer;
  2919. begin
  2920. { outdated gcc 2.x name mangling scheme }
  2921. {$ifdef NAMEMANGLING_GCC2}
  2922. s := procsym.realname;
  2923. if procsym.owner.symtabletype=ObjectSymtable then
  2924. begin
  2925. s2:=upper(tobjectdef(procsym.owner.defowner).objrealname^);
  2926. case proctypeoption of
  2927. potype_destructor:
  2928. s:='_$_'+tostr(length(s2))+s2;
  2929. potype_constructor:
  2930. s:='___'+tostr(length(s2))+s2;
  2931. else
  2932. s:='_'+s+'__'+tostr(length(s2))+s2;
  2933. end;
  2934. end
  2935. else s:=s+'__';
  2936. s:=s+'F';
  2937. { concat modifiers }
  2938. { !!!!! }
  2939. { now we handle the parameters }
  2940. if maxparacount>0 then
  2941. begin
  2942. for i:=0 to paras.count-1 do
  2943. begin
  2944. hp:=tparavarsym(paras[i]);
  2945. s2:=getcppparaname(hp.vardef);
  2946. if hp.varspez in [vs_var,vs_out] then
  2947. s2:='R'+s2;
  2948. s:=s+s2;
  2949. end;
  2950. end
  2951. else
  2952. s:=s+'v';
  2953. cplusplusmangledname:=s;
  2954. {$endif NAMEMANGLING_GCC2}
  2955. { gcc 3.x name mangling scheme }
  2956. if procsym.owner.symtabletype=ObjectSymtable then
  2957. begin
  2958. s:='_ZN';
  2959. s2:=tobjectdef(procsym.owner.defowner).objrealname^;
  2960. s:=s+tostr(length(s2))+s2;
  2961. case proctypeoption of
  2962. potype_constructor:
  2963. s:=s+'C1';
  2964. potype_destructor:
  2965. s:=s+'D1';
  2966. else
  2967. s:=s+tostr(length(procsym.realname))+procsym.realname;
  2968. end;
  2969. s:=s+'E';
  2970. end
  2971. else
  2972. s:=procsym.realname;
  2973. { now we handle the parameters }
  2974. if maxparacount>0 then
  2975. begin
  2976. for i:=0 to paras.count-1 do
  2977. begin
  2978. hp:=tparavarsym(paras[i]);
  2979. s2:=getcppparaname(hp.vardef);
  2980. if hp.varspez in [vs_var,vs_out] then
  2981. s2:='R'+s2;
  2982. s:=s+s2;
  2983. end;
  2984. end
  2985. else
  2986. s:=s+'v';
  2987. cplusplusmangledname:=s;
  2988. end;
  2989. procedure tprocdef.setmangledname(const s : string);
  2990. begin
  2991. { This is not allowed anymore, the forward declaration
  2992. already needs to create the correct mangledname, no changes
  2993. afterwards are allowed (PFV) }
  2994. { Exception: interface definitions in mode macpas, since in that }
  2995. { case no reference to the old name can exist yet (JM) }
  2996. if assigned(_mangledname) then
  2997. if ((m_mac in current_settings.modeswitches) and
  2998. (interfacedef)) then
  2999. stringdispose(_mangledname)
  3000. else
  3001. internalerror(200411171);
  3002. {$ifdef compress}
  3003. _mangledname:=stringdup(minilzw_encode(s));
  3004. {$else}
  3005. _mangledname:=stringdup(s);
  3006. {$endif}
  3007. include(procoptions,po_has_mangledname);
  3008. end;
  3009. {***************************************************************************
  3010. TPROCVARDEF
  3011. ***************************************************************************}
  3012. constructor tprocvardef.create(level:byte);
  3013. begin
  3014. inherited create(procvardef,level);
  3015. end;
  3016. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3017. begin
  3018. inherited ppuload(procvardef,ppufile);
  3019. { load para symtable }
  3020. parast:=tparasymtable.create(self,unknown_level);
  3021. tparasymtable(parast).ppuload(ppufile);
  3022. end;
  3023. function tprocvardef.getcopy : tstoreddef;
  3024. var
  3025. i : tcallercallee;
  3026. j : longint;
  3027. begin
  3028. result:=tprocvardef.create(parast.symtablelevel);
  3029. tprocvardef(result).returndef:=returndef;
  3030. tprocvardef(result).returndefderef:=returndefderef;
  3031. tprocvardef(result).parast:=parast.getcopy;
  3032. tprocvardef(result).savesize:=savesize;
  3033. { create paralist copy }
  3034. tprocvardef(result).paras:=tparalist.create(false);
  3035. tprocvardef(result).paras.count:=paras.count;
  3036. for j:=0 to paras.count-1 do
  3037. tprocvardef(result).paras[j]:=paras[j];
  3038. tprocvardef(result).proctypeoption:=proctypeoption;
  3039. tprocvardef(result).proccalloption:=proccalloption;
  3040. tprocvardef(result).procoptions:=procoptions;
  3041. tprocvardef(result).requiredargarea:=requiredargarea;
  3042. tprocvardef(result).maxparacount:=maxparacount;
  3043. tprocvardef(result).minparacount:=minparacount;
  3044. for i:=low(tcallercallee) to high(tcallercallee) do
  3045. location_copy(tprocvardef(result).funcretloc[i],funcretloc[i]);
  3046. tprocvardef(result).has_paraloc_info:=has_paraloc_info;
  3047. {$ifdef m68k}
  3048. tprocvardef(result).exp_funcretloc:=exp_funcretloc;
  3049. {$endif}
  3050. end;
  3051. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3052. begin
  3053. inherited ppuwrite(ppufile);
  3054. { Write this entry }
  3055. ppufile.writeentry(ibprocvardef);
  3056. { Save the para symtable, this is taken from the interface }
  3057. tparasymtable(parast).ppuwrite(ppufile);
  3058. end;
  3059. function tprocvardef.GetSymtable(t:tGetSymtable):TSymtable;
  3060. begin
  3061. case t of
  3062. gs_para :
  3063. GetSymtable:=parast;
  3064. else
  3065. GetSymtable:=nil;
  3066. end;
  3067. end;
  3068. function tprocvardef.size : aint;
  3069. begin
  3070. if (po_methodpointer in procoptions) and
  3071. not(po_addressonly in procoptions) then
  3072. size:=2*sizeof(pint)
  3073. else
  3074. size:=sizeof(pint);
  3075. end;
  3076. function tprocvardef.is_methodpointer:boolean;
  3077. begin
  3078. result:=(po_methodpointer in procoptions);
  3079. end;
  3080. function tprocvardef.is_addressonly:boolean;
  3081. begin
  3082. result:=not(po_methodpointer in procoptions) or
  3083. (po_addressonly in procoptions);
  3084. end;
  3085. function tprocvardef.getmangledparaname:string;
  3086. begin
  3087. result:='procvar';
  3088. end;
  3089. function tprocvardef.is_publishable : boolean;
  3090. begin
  3091. is_publishable:=(po_methodpointer in procoptions);
  3092. end;
  3093. function tprocvardef.GetTypeName : string;
  3094. var
  3095. s: string;
  3096. showhidden : boolean;
  3097. begin
  3098. {$ifdef EXTDEBUG}
  3099. showhidden:=true;
  3100. {$else EXTDEBUG}
  3101. showhidden:=false;
  3102. {$endif EXTDEBUG}
  3103. s:='<';
  3104. if po_classmethod in procoptions then
  3105. s := s+'class method type of'
  3106. else
  3107. if po_addressonly in procoptions then
  3108. s := s+'address of'
  3109. else
  3110. s := s+'procedure variable type of';
  3111. if po_local in procoptions then
  3112. s := s+' local';
  3113. if assigned(returndef) and
  3114. (returndef<>voidtype) then
  3115. s:=s+' function'+typename_paras(showhidden)+':'+returndef.GetTypeName
  3116. else
  3117. s:=s+' procedure'+typename_paras(showhidden);
  3118. if po_methodpointer in procoptions then
  3119. s := s+' of object';
  3120. GetTypeName := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3121. end;
  3122. {***************************************************************************
  3123. TOBJECTDEF
  3124. ***************************************************************************}
  3125. constructor tobjectdef.create(ot : tobjecttyp;const n : string;c : tobjectdef);
  3126. begin
  3127. inherited create(objectdef);
  3128. objecttype:=ot;
  3129. objectoptions:=[];
  3130. childof:=nil;
  3131. symtable:=tObjectSymtable.create(self,n,current_settings.packrecords);
  3132. { create space for vmt !! }
  3133. vmtentries:=nil;
  3134. vmt_offset:=0;
  3135. set_parent(c);
  3136. objname:=stringdup(upper(n));
  3137. objrealname:=stringdup(n);
  3138. if objecttype in [odt_interfacecorba,odt_interfacecom,odt_dispinterface] then
  3139. prepareguid;
  3140. { setup implemented interfaces }
  3141. if objecttype in [odt_class,odt_interfacecorba] then
  3142. ImplementedInterfaces:=TFPObjectList.Create(true)
  3143. else
  3144. ImplementedInterfaces:=nil;
  3145. writing_class_record_dbginfo:=false;
  3146. end;
  3147. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3148. var
  3149. i,
  3150. implintfcount : longint;
  3151. d : tderef;
  3152. ImplIntf : TImplementedInterface;
  3153. begin
  3154. inherited ppuload(objectdef,ppufile);
  3155. objecttype:=tobjecttyp(ppufile.getbyte);
  3156. objrealname:=stringdup(ppufile.getstring);
  3157. objname:=stringdup(upper(objrealname^));
  3158. symtable:=tObjectSymtable.create(self,objrealname^,0);
  3159. tObjectSymtable(symtable).datasize:=ppufile.getaint;
  3160. tObjectSymtable(symtable).fieldalignment:=ppufile.getbyte;
  3161. tObjectSymtable(symtable).recordalignment:=ppufile.getbyte;
  3162. vmt_offset:=ppufile.getlongint;
  3163. vmtentries:=nil;
  3164. ppufile.getderef(childofderef);
  3165. ppufile.getsmallset(objectoptions);
  3166. { load guid }
  3167. iidstr:=nil;
  3168. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3169. begin
  3170. new(iidguid);
  3171. ppufile.getguid(iidguid^);
  3172. iidstr:=stringdup(ppufile.getstring);
  3173. end;
  3174. { load implemented interfaces }
  3175. if objecttype in [odt_class,odt_interfacecorba] then
  3176. begin
  3177. ImplementedInterfaces:=TFPObjectList.Create(true);
  3178. implintfcount:=ppufile.getlongint;
  3179. for i:=0 to implintfcount-1 do
  3180. begin
  3181. ppufile.getderef(d);
  3182. ImplIntf:=TImplementedInterface.Create_deref(d);
  3183. ImplIntf.IOffset:=ppufile.getlongint;
  3184. ImplementedInterfaces.Add(ImplIntf);
  3185. end;
  3186. end
  3187. else
  3188. ImplementedInterfaces:=nil;
  3189. tObjectSymtable(symtable).ppuload(ppufile);
  3190. { handles the predefined class tobject }
  3191. { the last TOBJECT which is loaded gets }
  3192. { it ! }
  3193. if (childof=nil) and
  3194. (objecttype=odt_class) and
  3195. (objname^='TOBJECT') then
  3196. class_tobject:=self;
  3197. if (childof=nil) and
  3198. (objecttype=odt_interfacecom) and
  3199. (objname^='IUNKNOWN') then
  3200. interface_iunknown:=self;
  3201. writing_class_record_dbginfo:=false;
  3202. end;
  3203. destructor tobjectdef.destroy;
  3204. begin
  3205. if assigned(symtable) then
  3206. begin
  3207. symtable.free;
  3208. symtable:=nil;
  3209. end;
  3210. stringdispose(objname);
  3211. stringdispose(objrealname);
  3212. stringdispose(iidstr);
  3213. if assigned(ImplementedInterfaces) then
  3214. begin
  3215. ImplementedInterfaces.free;
  3216. ImplementedInterfaces:=nil;
  3217. end;
  3218. if assigned(iidguid) then
  3219. begin
  3220. dispose(iidguid);
  3221. iidguid:=nil;
  3222. end;
  3223. if assigned(vmtentries) then
  3224. begin
  3225. vmtentries.free;
  3226. vmtentries:=nil;
  3227. end;
  3228. inherited destroy;
  3229. end;
  3230. function tobjectdef.getcopy : tstoreddef;
  3231. var
  3232. i : longint;
  3233. begin
  3234. result:=tobjectdef.create(objecttype,objname^,childof);
  3235. tobjectdef(result).symtable:=symtable.getcopy;
  3236. if assigned(objname) then
  3237. tobjectdef(result).objname:=stringdup(objname^);
  3238. if assigned(objrealname) then
  3239. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3240. tobjectdef(result).objectoptions:=objectoptions;
  3241. tobjectdef(result).vmt_offset:=vmt_offset;
  3242. if assigned(iidguid) then
  3243. begin
  3244. new(tobjectdef(result).iidguid);
  3245. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3246. end;
  3247. if assigned(iidstr) then
  3248. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3249. if assigned(ImplementedInterfaces) then
  3250. begin
  3251. for i:=0 to ImplementedInterfaces.count-1 do
  3252. tobjectdef(result).ImplementedInterfaces.Add(TImplementedInterface(ImplementedInterfaces[i]).Getcopy);
  3253. end;
  3254. if assigned(vmtentries) then
  3255. begin
  3256. tobjectdef(result).vmtentries:=TFPobjectList.Create(false);
  3257. tobjectdef(result).vmtentries.Assign(vmtentries);
  3258. end;
  3259. end;
  3260. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3261. var
  3262. i : longint;
  3263. ImplIntf : TImplementedInterface;
  3264. begin
  3265. inherited ppuwrite(ppufile);
  3266. ppufile.putbyte(byte(objecttype));
  3267. ppufile.putstring(objrealname^);
  3268. ppufile.putaint(tObjectSymtable(symtable).datasize);
  3269. ppufile.putbyte(tObjectSymtable(symtable).fieldalignment);
  3270. ppufile.putbyte(tObjectSymtable(symtable).recordalignment);
  3271. ppufile.putlongint(vmt_offset);
  3272. ppufile.putderef(childofderef);
  3273. ppufile.putsmallset(objectoptions);
  3274. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3275. begin
  3276. ppufile.putguid(iidguid^);
  3277. ppufile.putstring(iidstr^);
  3278. end;
  3279. if objecttype in [odt_class,odt_interfacecorba] then
  3280. begin
  3281. ppufile.putlongint(ImplementedInterfaces.Count);
  3282. for i:=0 to ImplementedInterfaces.Count-1 do
  3283. begin
  3284. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3285. ppufile.putderef(ImplIntf.intfdefderef);
  3286. ppufile.putlongint(ImplIntf.Ioffset);
  3287. end;
  3288. end;
  3289. ppufile.writeentry(ibobjectdef);
  3290. tObjectSymtable(symtable).ppuwrite(ppufile);
  3291. end;
  3292. function tobjectdef.GetTypeName:string;
  3293. begin
  3294. if (self <> aktobjectdef) then
  3295. GetTypeName:=typename
  3296. else
  3297. { in this case we will go in endless recursion, because then }
  3298. { there is no tsym associated yet with the def. It can occur }
  3299. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3300. { instead of the actual type name }
  3301. GetTypeName:='<Currently Parsed Class>';
  3302. end;
  3303. procedure tobjectdef.buildderef;
  3304. var
  3305. i : longint;
  3306. begin
  3307. inherited buildderef;
  3308. childofderef.build(childof);
  3309. tstoredsymtable(symtable).buildderef;
  3310. if objecttype in [odt_class,odt_interfacecorba] then
  3311. begin
  3312. for i:=0 to ImplementedInterfaces.count-1 do
  3313. TImplementedInterface(ImplementedInterfaces[i]).buildderef;
  3314. end;
  3315. end;
  3316. procedure tobjectdef.deref;
  3317. var
  3318. i : longint;
  3319. begin
  3320. inherited deref;
  3321. childof:=tobjectdef(childofderef.resolve);
  3322. tstoredsymtable(symtable).deref;
  3323. if objecttype in [odt_class,odt_interfacecorba] then
  3324. begin
  3325. for i:=0 to ImplementedInterfaces.count-1 do
  3326. TImplementedInterface(ImplementedInterfaces[i]).deref;
  3327. end;
  3328. end;
  3329. function tobjectdef.getparentdef:tdef;
  3330. begin
  3331. {$warning TODO Remove getparentdef hack}
  3332. { With 2 forward declared classes with the child class before the
  3333. parent class the child class is written earlier to the ppu. Leaving it
  3334. possible to have a reference to the parent class for property overriding,
  3335. but the parent class still has the childof not resolved yet (PFV) }
  3336. if childof=nil then
  3337. childof:=tobjectdef(childofderef.resolve);
  3338. result:=childof;
  3339. end;
  3340. procedure tobjectdef.prepareguid;
  3341. begin
  3342. { set up guid }
  3343. if not assigned(iidguid) then
  3344. begin
  3345. new(iidguid);
  3346. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3347. end;
  3348. { setup iidstring }
  3349. if not assigned(iidstr) then
  3350. iidstr:=stringdup(''); { default is empty string }
  3351. end;
  3352. procedure tobjectdef.set_parent( c : tobjectdef);
  3353. begin
  3354. { nothing to do if the parent was not forward !}
  3355. if assigned(childof) then
  3356. exit;
  3357. childof:=c;
  3358. { some options are inherited !! }
  3359. if assigned(c) then
  3360. begin
  3361. { only important for classes }
  3362. objectoptions:=objectoptions+(c.objectoptions*inherited_objectoptions);
  3363. if not (objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) then
  3364. begin
  3365. { add the data of the anchestor class }
  3366. tObjectSymtable(symtable).datasize:=
  3367. tObjectSymtable(symtable).datasize+
  3368. tObjectSymtable(c.symtable).datasize;
  3369. { inherit recordalignment }
  3370. tObjectSymtable(symtable).recordalignment:=tObjectSymtable(c.symtable).recordalignment;
  3371. if (oo_has_vmt in objectoptions) and
  3372. (oo_has_vmt in c.objectoptions) then
  3373. tObjectSymtable(symtable).datasize:=
  3374. tObjectSymtable(symtable).datasize-sizeof(pint);
  3375. { if parent has a vmt field then
  3376. the offset is the same for the child PM }
  3377. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3378. begin
  3379. vmt_offset:=c.vmt_offset;
  3380. include(objectoptions,oo_has_vmt);
  3381. end;
  3382. end;
  3383. end;
  3384. end;
  3385. procedure tobjectdef.insertvmt;
  3386. begin
  3387. if objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3388. exit;
  3389. if (oo_has_vmt in objectoptions) then
  3390. internalerror(12345)
  3391. else
  3392. begin
  3393. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,
  3394. tObjectSymtable(symtable).fieldalignment);
  3395. if (tf_requires_proper_alignment in target_info.flags) then
  3396. tObjectSymtable(symtable).datasize:=align(tObjectSymtable(symtable).datasize,sizeof(pint));
  3397. vmt_offset:=tObjectSymtable(symtable).datasize;
  3398. tObjectSymtable(symtable).datasize:=
  3399. tObjectSymtable(symtable).datasize+sizeof(pint);
  3400. include(objectoptions,oo_has_vmt);
  3401. end;
  3402. end;
  3403. procedure tobjectdef.check_forwards;
  3404. begin
  3405. if not(objecttype in [odt_interfacecom,odt_interfacecorba,odt_dispinterface]) then
  3406. tstoredsymtable(symtable).check_forwards;
  3407. if (oo_is_forward in objectoptions) then
  3408. begin
  3409. { ok, in future, the forward can be resolved }
  3410. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3411. exclude(objectoptions,oo_is_forward);
  3412. end;
  3413. end;
  3414. { true, if self inherits from d (or if they are equal) }
  3415. function tobjectdef.is_related(d : tdef) : boolean;
  3416. var
  3417. hp : tobjectdef;
  3418. begin
  3419. hp:=self;
  3420. while assigned(hp) do
  3421. begin
  3422. if hp=d then
  3423. begin
  3424. is_related:=true;
  3425. exit;
  3426. end;
  3427. hp:=hp.childof;
  3428. end;
  3429. is_related:=false;
  3430. end;
  3431. function tobjectdef.FindDestructor : tprocdef;
  3432. var
  3433. objdef : tobjectdef;
  3434. i : longint;
  3435. sym : tsym;
  3436. pd : tprocdef;
  3437. begin
  3438. result:=nil;
  3439. objdef:=self;
  3440. while assigned(objdef) do
  3441. begin
  3442. for i:=0 to objdef.symtable.SymList.Count-1 do
  3443. begin
  3444. sym:=TSym(objdef.symtable.SymList[i]);
  3445. if sym.typ=procsym then
  3446. begin
  3447. pd:=Tprocsym(sym).Find_procdef_bytype(potype_destructor);
  3448. if assigned(pd) then
  3449. begin
  3450. result:=pd;
  3451. exit;
  3452. end;
  3453. end;
  3454. end;
  3455. objdef:=objdef.childof;
  3456. end;
  3457. end;
  3458. function tobjectdef.size : aint;
  3459. begin
  3460. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3461. result:=sizeof(pint)
  3462. else
  3463. result:=tObjectSymtable(symtable).datasize;
  3464. end;
  3465. function tobjectdef.alignment:shortint;
  3466. begin
  3467. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface] then
  3468. alignment:=sizeof(pint)
  3469. else
  3470. alignment:=tObjectSymtable(symtable).recordalignment;
  3471. end;
  3472. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3473. begin
  3474. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3475. case objecttype of
  3476. odt_class:
  3477. { the +2*sizeof(pint) is size and -size }
  3478. vmtmethodoffset:=(index+10)*sizeof(pint)+2*sizeof(pint);
  3479. odt_interfacecom,odt_interfacecorba:
  3480. vmtmethodoffset:=index*sizeof(pint);
  3481. else
  3482. {$ifdef WITHDMT}
  3483. vmtmethodoffset:=(index+4)*sizeof(pint);
  3484. {$else WITHDMT}
  3485. vmtmethodoffset:=(index+3)*sizeof(pint);
  3486. {$endif WITHDMT}
  3487. end;
  3488. end;
  3489. function tobjectdef.vmt_mangledname : string;
  3490. begin
  3491. if not(oo_has_vmt in objectoptions) then
  3492. Message1(parser_n_object_has_no_vmt,objrealname^);
  3493. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3494. end;
  3495. function tobjectdef.needs_inittable : boolean;
  3496. begin
  3497. case objecttype of
  3498. odt_dispinterface,
  3499. odt_class :
  3500. needs_inittable:=false;
  3501. odt_interfacecom:
  3502. needs_inittable:=true;
  3503. odt_interfacecorba:
  3504. needs_inittable:=is_related(interface_iunknown);
  3505. odt_object:
  3506. needs_inittable:=tObjectSymtable(symtable).needs_init_final;
  3507. odt_cppclass:
  3508. needs_inittable:=false;
  3509. else
  3510. internalerror(200108267);
  3511. end;
  3512. end;
  3513. function tobjectdef.members_need_inittable : boolean;
  3514. begin
  3515. members_need_inittable:=tObjectSymtable(symtable).needs_init_final;
  3516. end;
  3517. function tobjectdef.find_implemented_interface(aintfdef:tobjectdef):TImplementedInterface;
  3518. var
  3519. ImplIntf : TImplementedInterface;
  3520. i : longint;
  3521. begin
  3522. result:=nil;
  3523. if not assigned(ImplementedInterfaces) then
  3524. exit;
  3525. for i:=0 to ImplementedInterfaces.Count-1 do
  3526. begin
  3527. ImplIntf:=TImplementedInterface(ImplementedInterfaces[i]);
  3528. if ImplIntf.intfdef=aintfdef then
  3529. begin
  3530. result:=ImplIntf;
  3531. exit;
  3532. end;
  3533. end;
  3534. end;
  3535. function tobjectdef.is_publishable : boolean;
  3536. begin
  3537. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface];
  3538. end;
  3539. {****************************************************************************
  3540. TImplementedInterface
  3541. ****************************************************************************}
  3542. constructor TImplementedInterface.create(aintf: tobjectdef);
  3543. begin
  3544. inherited create;
  3545. intfdef:=aintf;
  3546. IOffset:=-1;
  3547. IType:=etStandard;
  3548. NameMappings:=nil;
  3549. procdefs:=nil;
  3550. end;
  3551. constructor TImplementedInterface.create_deref(d:tderef);
  3552. begin
  3553. inherited create;
  3554. intfdef:=nil;
  3555. intfdefderef:=d;
  3556. IOffset:=-1;
  3557. IType:=etStandard;
  3558. NameMappings:=nil;
  3559. procdefs:=nil;
  3560. end;
  3561. destructor TImplementedInterface.destroy;
  3562. var
  3563. i : longint;
  3564. mappedname : pshortstring;
  3565. begin
  3566. if assigned(NameMappings) then
  3567. begin
  3568. for i:=0 to NameMappings.Count-1 do
  3569. begin
  3570. mappedname:=pshortstring(NameMappings[i]);
  3571. stringdispose(mappedname);
  3572. end;
  3573. NameMappings.free;
  3574. NameMappings:=nil;
  3575. end;
  3576. if assigned(procdefs) then
  3577. begin
  3578. procdefs.free;
  3579. procdefs:=nil;
  3580. end;
  3581. inherited destroy;
  3582. end;
  3583. procedure TImplementedInterface.buildderef;
  3584. begin
  3585. intfdefderef.build(intfdef);
  3586. end;
  3587. procedure TImplementedInterface.deref;
  3588. begin
  3589. intfdef:=tobjectdef(intfdefderef.resolve);
  3590. end;
  3591. procedure TImplementedInterface.AddMapping(const origname,newname: string);
  3592. begin
  3593. if not assigned(NameMappings) then
  3594. NameMappings:=TFPHashList.Create;
  3595. NameMappings.Add(origname,stringdup(newname));
  3596. end;
  3597. function TImplementedInterface.GetMapping(const origname: string):string;
  3598. var
  3599. mappedname : pshortstring;
  3600. begin
  3601. result:='';
  3602. if not assigned(NameMappings) then
  3603. exit;
  3604. mappedname:=PShortstring(NameMappings.Find(origname));
  3605. if assigned(mappedname) then
  3606. result:=mappedname^;
  3607. end;
  3608. procedure TImplementedInterface.AddImplProc(pd:tprocdef);
  3609. var
  3610. i : longint;
  3611. found : boolean;
  3612. begin
  3613. if not assigned(procdefs) then
  3614. procdefs:=TFPObjectList.Create(false);
  3615. { No duplicate entries of the same procdef }
  3616. found:=false;
  3617. for i:=0 to procdefs.count-1 do
  3618. if tprocdef(procdefs[i])=pd then
  3619. begin
  3620. found:=true;
  3621. break;
  3622. end;
  3623. if not found then
  3624. procdefs.Add(pd);
  3625. end;
  3626. function TImplementedInterface.IsImplMergePossible(MergingIntf:TImplementedInterface;out weight: longint): boolean;
  3627. var
  3628. i : longint;
  3629. begin
  3630. result:=false;
  3631. { interfaces being implemented through delegation are not mergable (FK) }
  3632. if MergingIntf.IType<>etStandard then
  3633. exit;
  3634. weight:=0;
  3635. { empty interface is mergeable }
  3636. if ProcDefs.Count=0 then
  3637. begin
  3638. result:=true;
  3639. exit;
  3640. end;
  3641. { The interface to merge must at least the number of
  3642. procedures of this interface }
  3643. if MergingIntf.ProcDefs.Count<ProcDefs.Count then
  3644. exit;
  3645. for i:=0 to ProcDefs.Count-1 do
  3646. begin
  3647. if MergingIntf.ProcDefs[i]<>ProcDefs[i] then
  3648. exit;
  3649. end;
  3650. weight:=ProcDefs.Count;
  3651. result:=true;
  3652. end;
  3653. function TImplementedInterface.getcopy:TImplementedInterface;
  3654. begin
  3655. Result:=TImplementedInterface.Create(nil);
  3656. Move(pointer(self)^,pointer(result)^,InstanceSize);
  3657. end;
  3658. {****************************************************************************
  3659. TFORWARDDEF
  3660. ****************************************************************************}
  3661. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  3662. begin
  3663. inherited create(forwarddef);
  3664. tosymname:=stringdup(s);
  3665. forwardpos:=pos;
  3666. end;
  3667. function tforwarddef.GetTypeName:string;
  3668. begin
  3669. GetTypeName:='unresolved forward to '+tosymname^;
  3670. end;
  3671. destructor tforwarddef.destroy;
  3672. begin
  3673. stringdispose(tosymname);
  3674. inherited destroy;
  3675. end;
  3676. {****************************************************************************
  3677. TUNDEFINEDDEF
  3678. ****************************************************************************}
  3679. constructor tundefineddef.create;
  3680. begin
  3681. inherited create(undefineddef);
  3682. end;
  3683. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  3684. begin
  3685. inherited ppuload(undefineddef,ppufile);
  3686. end;
  3687. function tundefineddef.GetTypeName:string;
  3688. begin
  3689. GetTypeName:='<undefined type>';
  3690. end;
  3691. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  3692. begin
  3693. inherited ppuwrite(ppufile);
  3694. ppufile.writeentry(ibundefineddef);
  3695. end;
  3696. {****************************************************************************
  3697. TERRORDEF
  3698. ****************************************************************************}
  3699. constructor terrordef.create;
  3700. begin
  3701. inherited create(errordef);
  3702. end;
  3703. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  3704. begin
  3705. { Can't write errordefs to ppu }
  3706. internalerror(200411063);
  3707. end;
  3708. function terrordef.GetTypeName:string;
  3709. begin
  3710. GetTypeName:='<erroneous type>';
  3711. end;
  3712. function terrordef.getmangledparaname:string;
  3713. begin
  3714. getmangledparaname:='error';
  3715. end;
  3716. {****************************************************************************
  3717. Definition Helpers
  3718. ****************************************************************************}
  3719. function is_interfacecom(def: tdef): boolean;
  3720. begin
  3721. is_interfacecom:=
  3722. assigned(def) and
  3723. (def.typ=objectdef) and
  3724. (tobjectdef(def).objecttype=odt_interfacecom);
  3725. end;
  3726. function is_interfacecorba(def: tdef): boolean;
  3727. begin
  3728. is_interfacecorba:=
  3729. assigned(def) and
  3730. (def.typ=objectdef) and
  3731. (tobjectdef(def).objecttype=odt_interfacecorba);
  3732. end;
  3733. function is_interface(def: tdef): boolean;
  3734. begin
  3735. is_interface:=
  3736. assigned(def) and
  3737. (def.typ=objectdef) and
  3738. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  3739. end;
  3740. function is_dispinterface(def: tdef): boolean;
  3741. begin
  3742. result:=
  3743. assigned(def) and
  3744. (def.typ=objectdef) and
  3745. (tobjectdef(def).objecttype=odt_dispinterface);
  3746. end;
  3747. function is_class(def: tdef): boolean;
  3748. begin
  3749. is_class:=
  3750. assigned(def) and
  3751. (def.typ=objectdef) and
  3752. (tobjectdef(def).objecttype=odt_class);
  3753. end;
  3754. function is_object(def: tdef): boolean;
  3755. begin
  3756. is_object:=
  3757. assigned(def) and
  3758. (def.typ=objectdef) and
  3759. (tobjectdef(def).objecttype=odt_object);
  3760. end;
  3761. function is_cppclass(def: tdef): boolean;
  3762. begin
  3763. is_cppclass:=
  3764. assigned(def) and
  3765. (def.typ=objectdef) and
  3766. (tobjectdef(def).objecttype=odt_cppclass);
  3767. end;
  3768. function is_class_or_interface(def: tdef): boolean;
  3769. begin
  3770. is_class_or_interface:=
  3771. assigned(def) and
  3772. (def.typ=objectdef) and
  3773. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  3774. end;
  3775. function is_class_or_interface_or_dispinterface(def: tdef): boolean;
  3776. begin
  3777. result:=
  3778. assigned(def) and
  3779. (def.typ=objectdef) and
  3780. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba,odt_dispinterface]);
  3781. end;
  3782. {$ifdef x86}
  3783. function use_sse(def : tdef) : boolean;
  3784. begin
  3785. use_sse:=(is_single(def) and (current_settings.fputype in sse_singlescalar)) or
  3786. (is_double(def) and (current_settings.fputype in sse_doublescalar));
  3787. end;
  3788. {$endif x86}
  3789. end.