symdef.pas 153 KB

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