symdef.pas 131 KB

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