symdef.pas 141 KB

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