symdef.pas 155 KB

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