symdef.pas 130 KB

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