symdef.pas 171 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509
  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. cutils,cclasses,
  23. { global }
  24. globtype,globals,tokens,
  25. { symtable }
  26. symconst,symbase,symtype,
  27. { ppu }
  28. ppu,
  29. { node }
  30. node,
  31. { aasm }
  32. aasmbase,aasmtai,aasmdata,
  33. cpubase,cpuinfo,
  34. cgbase,cgutils,
  35. parabase
  36. ;
  37. type
  38. {************************************************
  39. TDef
  40. ************************************************}
  41. tstoreddef = class(tdef)
  42. protected
  43. typesymderef : tderef;
  44. public
  45. { persistent (available across units) rtti and init tables }
  46. rttitablesym,
  47. inittablesym : tsym; {trttisym}
  48. rttitablesymderef,
  49. inittablesymderef : tderef;
  50. { local (per module) rtti and init tables }
  51. localrttilab : array[trttitype] of tasmlabel;
  52. {$ifdef EXTDEBUG}
  53. fileinfo : tfileposinfo;
  54. {$endif}
  55. { generic support }
  56. genericdef : tstoreddef;
  57. genericdefderef : tderef;
  58. generictokenbuf : tdynamicarray;
  59. constructor create(dt:tdeftype);
  60. constructor ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  61. destructor destroy;override;
  62. procedure reset;virtual;
  63. function getcopy : tstoreddef;virtual;
  64. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  65. procedure buildderef;override;
  66. procedure buildderefimpl;override;
  67. procedure deref;override;
  68. procedure derefimpl;override;
  69. function size:aint;override;
  70. function getvartype:longint;override;
  71. function alignment:shortint;override;
  72. function is_publishable : boolean;override;
  73. function needs_inittable : boolean;override;
  74. { rtti generation }
  75. procedure write_rtti_name;
  76. procedure write_rtti_data(rt:trttitype);virtual;
  77. procedure write_child_rtti_data(rt:trttitype);virtual;
  78. function get_rtti_label(rt:trttitype):tasmsymbol;
  79. { regvars }
  80. function is_intregable : boolean;
  81. function is_fpuregable : boolean;
  82. { generics }
  83. procedure initgeneric;
  84. private
  85. savesize : aint;
  86. end;
  87. tfiletyp = (ft_text,ft_typed,ft_untyped);
  88. tfiledef = class(tstoreddef)
  89. filetyp : tfiletyp;
  90. typedfiletype : ttype;
  91. constructor createtext;
  92. constructor createuntyped;
  93. constructor createtyped(const tt : ttype);
  94. constructor ppuload(ppufile:tcompilerppufile);
  95. function getcopy : tstoreddef;override;
  96. procedure ppuwrite(ppufile:tcompilerppufile);override;
  97. procedure buildderef;override;
  98. procedure deref;override;
  99. function gettypename:string;override;
  100. function getmangledparaname:string;override;
  101. procedure setsize;
  102. end;
  103. tvariantdef = class(tstoreddef)
  104. varianttype : tvarianttype;
  105. constructor create(v : tvarianttype);
  106. constructor ppuload(ppufile:tcompilerppufile);
  107. function getcopy : tstoreddef;override;
  108. function gettypename:string;override;
  109. procedure ppuwrite(ppufile:tcompilerppufile);override;
  110. procedure setsize;
  111. function is_publishable : boolean;override;
  112. function needs_inittable : boolean;override;
  113. procedure write_rtti_data(rt:trttitype);override;
  114. end;
  115. tformaldef = class(tstoreddef)
  116. constructor create;
  117. constructor ppuload(ppufile:tcompilerppufile);
  118. procedure ppuwrite(ppufile:tcompilerppufile);override;
  119. function gettypename:string;override;
  120. end;
  121. tforwarddef = class(tstoreddef)
  122. tosymname : pstring;
  123. forwardpos : tfileposinfo;
  124. constructor create(const s:string;const pos : tfileposinfo);
  125. destructor destroy;override;
  126. function gettypename:string;override;
  127. end;
  128. tundefineddef = class(tstoreddef)
  129. constructor create;
  130. constructor ppuload(ppufile:tcompilerppufile);
  131. procedure ppuwrite(ppufile:tcompilerppufile);override;
  132. function gettypename:string;override;
  133. end;
  134. terrordef = class(tstoreddef)
  135. constructor create;
  136. procedure ppuwrite(ppufile:tcompilerppufile);override;
  137. function gettypename:string;override;
  138. function getmangledparaname : string;override;
  139. end;
  140. tabstractpointerdef = class(tstoreddef)
  141. pointertype : ttype;
  142. constructor create(dt:tdeftype;const tt : ttype);
  143. constructor ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  144. procedure ppuwrite(ppufile:tcompilerppufile);override;
  145. procedure buildderef;override;
  146. procedure deref;override;
  147. end;
  148. tpointerdef = class(tabstractpointerdef)
  149. is_far : boolean;
  150. constructor create(const tt : ttype);
  151. constructor createfar(const tt : ttype);
  152. function getcopy : tstoreddef;override;
  153. constructor ppuload(ppufile:tcompilerppufile);
  154. procedure ppuwrite(ppufile:tcompilerppufile);override;
  155. function gettypename:string;override;
  156. end;
  157. tabstractrecorddef= class(tstoreddef)
  158. private
  159. Count : integer;
  160. FRTTIType : trttitype;
  161. procedure count_field_rtti(sym : tnamedindexitem;arg:pointer);
  162. procedure write_field_rtti(sym : tnamedindexitem;arg:pointer);
  163. procedure generate_field_rtti(sym : tnamedindexitem;arg:pointer);
  164. public
  165. symtable : tsymtable;
  166. procedure reset;override;
  167. function getsymtable(t:tgetsymtable):tsymtable;override;
  168. procedure buildderefimpl;override;
  169. procedure derefimpl;override;
  170. end;
  171. trecorddef = class(tabstractrecorddef)
  172. public
  173. isunion : boolean;
  174. constructor create(p : tsymtable);
  175. constructor ppuload(ppufile:tcompilerppufile);
  176. destructor destroy;override;
  177. function getcopy : tstoreddef;override;
  178. procedure ppuwrite(ppufile:tcompilerppufile);override;
  179. procedure buildderef;override;
  180. procedure deref;override;
  181. function size:aint;override;
  182. function alignment : shortint;override;
  183. function padalignment: shortint;
  184. function gettypename:string;override;
  185. { debug }
  186. function needs_inittable : boolean;override;
  187. { rtti }
  188. procedure write_child_rtti_data(rt:trttitype);override;
  189. procedure write_rtti_data(rt:trttitype);override;
  190. end;
  191. tprocdef = class;
  192. tobjectdef = class;
  193. timplementedinterfaces = class;
  194. timplintfentry = class(TNamedIndexItem)
  195. intf : tobjectdef;
  196. intfderef : tderef;
  197. ioffset : longint;
  198. implindex : longint;
  199. namemappings : tdictionary;
  200. procdefs : TIndexArray;
  201. constructor create(aintf: tobjectdef);
  202. constructor create_deref(const d:tderef);
  203. destructor destroy; override;
  204. end;
  205. tobjectdef = class(tabstractrecorddef)
  206. private
  207. procedure count_published_properties(sym:tnamedindexitem;arg:pointer);
  208. procedure collect_published_properties(sym:tnamedindexitem;arg:pointer);
  209. procedure write_property_info(sym : tnamedindexitem;arg:pointer);
  210. procedure generate_published_child_rtti(sym : tnamedindexitem;arg:pointer);
  211. procedure count_published_fields(sym:tnamedindexitem;arg:pointer);
  212. procedure writefields(sym:tnamedindexitem;arg:pointer);
  213. public
  214. childof : tobjectdef;
  215. childofderef : tderef;
  216. objname,
  217. objrealname : pstring;
  218. objectoptions : tobjectoptions;
  219. { to be able to have a variable vmt position }
  220. { and no vmt field for objects without virtuals }
  221. vmt_offset : longint;
  222. writing_class_record_dbginfo : boolean;
  223. objecttype : tobjectdeftype;
  224. iidguid: pguid;
  225. iidstr: pstring;
  226. lastvtableindex: longint;
  227. { store implemented interfaces defs and name mappings }
  228. implementedinterfaces: timplementedinterfaces;
  229. constructor create(ot : tobjectdeftype;const n : string;c : tobjectdef);
  230. constructor ppuload(ppufile:tcompilerppufile);
  231. destructor destroy;override;
  232. function getcopy : tstoreddef;override;
  233. procedure ppuwrite(ppufile:tcompilerppufile);override;
  234. function gettypename:string;override;
  235. procedure buildderef;override;
  236. procedure deref;override;
  237. function getparentdef:tdef;override;
  238. function size : aint;override;
  239. function alignment:shortint;override;
  240. function vmtmethodoffset(index:longint):longint;
  241. function members_need_inittable : boolean;
  242. { this should be called when this class implements an interface }
  243. procedure prepareguid;
  244. function is_publishable : boolean;override;
  245. function needs_inittable : boolean;override;
  246. function vmt_mangledname : string;
  247. function rtti_name : string;
  248. procedure check_forwards;
  249. function is_related(d : tdef) : boolean;override;
  250. procedure insertvmt;
  251. procedure set_parent(c : tobjectdef);
  252. function searchdestructor : tprocdef;
  253. { rtti }
  254. procedure write_child_rtti_data(rt:trttitype);override;
  255. procedure write_rtti_data(rt:trttitype);override;
  256. function generate_field_table : tasmlabel;
  257. end;
  258. timplementedinterfaces = class
  259. constructor create;
  260. destructor destroy; override;
  261. function count: longint;
  262. function interfaces(intfindex: longint): tobjectdef;
  263. function interfacesderef(intfindex: longint): tderef;
  264. function ioffsets(intfindex: longint): longint;
  265. procedure setioffsets(intfindex,iofs:longint);
  266. function implindex(intfindex:longint):longint;
  267. procedure setimplindex(intfindex,implidx:longint);
  268. function searchintf(def: tdef): longint;
  269. procedure addintf(def: tdef);
  270. procedure buildderef;
  271. procedure deref;
  272. { add interface reference loaded from ppu }
  273. procedure addintf_deref(const d:tderef;iofs:longint);
  274. procedure addintf_ioffset(d:tdef;iofs:longint);
  275. procedure clearmappings;
  276. procedure addmappings(intfindex: longint; const origname, newname: string);
  277. function getmappings(intfindex: longint; const origname: string; var nextexist: pointer): string;
  278. procedure addimplproc(intfindex: longint; procdef: tprocdef);
  279. function implproccount(intfindex: longint): longint;
  280. function implprocs(intfindex: longint; procindex: longint): tprocdef;
  281. function isimplmergepossible(intfindex, remainindex: longint; var weight: longint): boolean;
  282. private
  283. finterfaces: tindexarray;
  284. procedure checkindex(intfindex: longint);
  285. end;
  286. tclassrefdef = class(tabstractpointerdef)
  287. constructor create(const t:ttype);
  288. constructor ppuload(ppufile:tcompilerppufile);
  289. procedure ppuwrite(ppufile:tcompilerppufile);override;
  290. function gettypename:string;override;
  291. function is_publishable : boolean;override;
  292. end;
  293. tarraydef = class(tstoreddef)
  294. lowrange,
  295. highrange : aint;
  296. rangetype : ttype;
  297. arrayoptions : tarraydefoptions;
  298. protected
  299. _elementtype : ttype;
  300. public
  301. function elesize : aint;
  302. function elecount : aint;
  303. constructor create_from_pointer(const elemt : ttype);
  304. constructor create(l,h : aint;const t : ttype);
  305. constructor ppuload(ppufile:tcompilerppufile);
  306. function getcopy : tstoreddef;override;
  307. procedure ppuwrite(ppufile:tcompilerppufile);override;
  308. function gettypename:string;override;
  309. function getmangledparaname : string;override;
  310. procedure setelementtype(t: ttype);
  311. procedure buildderef;override;
  312. procedure deref;override;
  313. function size : aint;override;
  314. function alignment : shortint;override;
  315. { returns the label of the range check string }
  316. function needs_inittable : boolean;override;
  317. procedure write_child_rtti_data(rt:trttitype);override;
  318. procedure write_rtti_data(rt:trttitype);override;
  319. property elementtype : ttype Read _ElementType;
  320. end;
  321. torddef = class(tstoreddef)
  322. low,high : TConstExprInt;
  323. typ : tbasetype;
  324. constructor create(t : tbasetype;v,b : TConstExprInt);
  325. constructor ppuload(ppufile:tcompilerppufile);
  326. function getcopy : tstoreddef;override;
  327. procedure ppuwrite(ppufile:tcompilerppufile);override;
  328. function is_publishable : boolean;override;
  329. function gettypename:string;override;
  330. function alignment:shortint;override;
  331. procedure setsize;
  332. function getvartype : longint;override;
  333. { rtti }
  334. procedure write_rtti_data(rt:trttitype);override;
  335. end;
  336. tfloatdef = class(tstoreddef)
  337. typ : tfloattype;
  338. constructor create(t : tfloattype);
  339. constructor ppuload(ppufile:tcompilerppufile);
  340. function getcopy : tstoreddef;override;
  341. procedure ppuwrite(ppufile:tcompilerppufile);override;
  342. function gettypename:string;override;
  343. function is_publishable : boolean;override;
  344. function alignment:shortint;override;
  345. procedure setsize;
  346. function getvartype:longint;override;
  347. { rtti }
  348. procedure write_rtti_data(rt:trttitype);override;
  349. end;
  350. tabstractprocdef = class(tstoreddef)
  351. { saves a definition to the return type }
  352. rettype : ttype;
  353. parast : tsymtable;
  354. paras : tparalist;
  355. proctypeoption : tproctypeoption;
  356. proccalloption : tproccalloption;
  357. procoptions : tprocoptions;
  358. requiredargarea : aint;
  359. { number of user visibile parameters }
  360. maxparacount,
  361. minparacount : byte;
  362. {$ifdef i386}
  363. fpu_used : longint; { how many stack fpu must be empty }
  364. {$endif i386}
  365. {$ifdef m68k}
  366. exp_funcretloc : tregister; { explicit funcretloc for AmigaOS }
  367. {$endif}
  368. funcretloc : array[tcallercallee] of TLocation;
  369. has_paraloc_info : boolean; { paraloc info is available }
  370. constructor create(dt:tdeftype;level:byte);
  371. constructor ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  372. destructor destroy;override;
  373. procedure ppuwrite(ppufile:tcompilerppufile);override;
  374. procedure buildderef;override;
  375. procedure deref;override;
  376. procedure releasemem;
  377. procedure calcparas;
  378. function typename_paras(showhidden:boolean): string;
  379. procedure test_if_fpu_result;
  380. function is_methodpointer:boolean;virtual;
  381. function is_addressonly:boolean;virtual;
  382. private
  383. procedure count_para(p:tnamedindexitem;arg:pointer);
  384. procedure insert_para(p:tnamedindexitem;arg:pointer);
  385. end;
  386. tprocvardef = class(tabstractprocdef)
  387. constructor create(level:byte);
  388. constructor ppuload(ppufile:tcompilerppufile);
  389. function getcopy : tstoreddef;override;
  390. procedure ppuwrite(ppufile:tcompilerppufile);override;
  391. procedure buildderef;override;
  392. procedure deref;override;
  393. function getsymtable(t:tgetsymtable):tsymtable;override;
  394. function size : aint;override;
  395. function gettypename:string;override;
  396. function is_publishable : boolean;override;
  397. function is_methodpointer:boolean;override;
  398. function is_addressonly:boolean;override;
  399. function getmangledparaname:string;override;
  400. { rtti }
  401. procedure write_rtti_data(rt:trttitype);override;
  402. end;
  403. tmessageinf = record
  404. case integer of
  405. 0 : (str : pchar);
  406. 1 : (i : longint);
  407. end;
  408. tinlininginfo = record
  409. { node tree }
  410. code : tnode;
  411. flags : tprocinfoflags;
  412. end;
  413. pinlininginfo = ^tinlininginfo;
  414. {$ifdef oldregvars}
  415. { register variables }
  416. pregvarinfo = ^tregvarinfo;
  417. tregvarinfo = record
  418. regvars : array[1..maxvarregs] of tsym;
  419. regvars_para : array[1..maxvarregs] of boolean;
  420. regvars_refs : array[1..maxvarregs] of longint;
  421. fpuregvars : array[1..maxfpuvarregs] of tsym;
  422. fpuregvars_para : array[1..maxfpuvarregs] of boolean;
  423. fpuregvars_refs : array[1..maxfpuvarregs] of longint;
  424. end;
  425. {$endif oldregvars}
  426. tprocdef = class(tabstractprocdef)
  427. private
  428. _mangledname : pstring;
  429. public
  430. extnumber : word;
  431. messageinf : tmessageinf;
  432. {$ifndef EXTDEBUG}
  433. { where is this function defined and what were the symbol
  434. flags, needed here because there
  435. is only one symbol for all overloaded functions
  436. EXTDEBUG has fileinfo in tdef (PFV) }
  437. fileinfo : tfileposinfo;
  438. {$endif}
  439. symoptions : tsymoptions;
  440. { symbol owning this definition }
  441. procsym : tsym;
  442. procsymderef : tderef;
  443. { alias names }
  444. aliasnames : tstringlist;
  445. { symtables }
  446. localst : tsymtable;
  447. funcretsym : tsym;
  448. funcretsymderef : tderef;
  449. { browser info }
  450. lastref,
  451. defref,
  452. lastwritten : tref;
  453. refcount : longint;
  454. _class : tobjectdef;
  455. _classderef : tderef;
  456. {$if defined(powerpc) or defined(m68k)}
  457. { library symbol for AmigaOS/MorphOS }
  458. libsym : tsym;
  459. libsymderef : tderef;
  460. {$endif powerpc or m68k}
  461. { name of the result variable to insert in the localsymtable }
  462. resultname : stringid;
  463. { true, if the procedure is only declared
  464. (forward procedure) }
  465. forwarddef,
  466. { true if the procedure is declared in the interface }
  467. interfacedef : boolean;
  468. { true if the procedure has a forward declaration }
  469. hasforward : boolean;
  470. { import info }
  471. import_dll,
  472. import_name : pstring;
  473. import_nr : word;
  474. { info for inlining the subroutine, if this pointer is nil,
  475. the procedure can't be inlined }
  476. inlininginfo : pinlininginfo;
  477. {$ifdef oldregvars}
  478. regvarinfo: pregvarinfo;
  479. {$endif oldregvars}
  480. { position in aasmoutput list }
  481. procstarttai,
  482. procendtai : tai;
  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. procedure load_references(ppufile:tcompilerppufile;locals:boolean);
  497. function write_references(ppufile:tcompilerppufile;locals:boolean):boolean;
  498. { inserts the local symbol table, if this is not
  499. no local symbol table is built. Should be called only
  500. when we are sure that a local symbol table will be required.
  501. }
  502. procedure insert_localst;
  503. function fullprocname(showhidden:boolean):string;
  504. function cplusplusmangledname : string;
  505. function is_methodpointer:boolean;override;
  506. function is_addressonly:boolean;override;
  507. function is_visible_for_object(currobjdef:tobjectdef):boolean;
  508. end;
  509. { single linked list of overloaded procs }
  510. pprocdeflist = ^tprocdeflist;
  511. tprocdeflist = record
  512. def : tprocdef;
  513. defderef : tderef;
  514. next : pprocdeflist;
  515. end;
  516. tstringdef = class(tstoreddef)
  517. string_typ : tstringtype;
  518. len : aint;
  519. constructor createshort(l : byte);
  520. constructor loadshort(ppufile:tcompilerppufile);
  521. constructor createlong(l : aint);
  522. constructor loadlong(ppufile:tcompilerppufile);
  523. constructor createansi(l : aint);
  524. constructor loadansi(ppufile:tcompilerppufile);
  525. constructor createwide(l : aint);
  526. constructor loadwide(ppufile:tcompilerppufile);
  527. function getcopy : tstoreddef;override;
  528. function stringtypname:string;
  529. procedure ppuwrite(ppufile:tcompilerppufile);override;
  530. function gettypename:string;override;
  531. function getmangledparaname:string;override;
  532. function is_publishable : boolean;override;
  533. function alignment : shortint;override;
  534. { init/final }
  535. function needs_inittable : boolean;override;
  536. { rtti }
  537. procedure write_rtti_data(rt:trttitype);override;
  538. end;
  539. tenumdef = class(tstoreddef)
  540. minval,
  541. maxval : aint;
  542. has_jumps : boolean;
  543. firstenum : tsym; {tenumsym}
  544. basedef : tenumdef;
  545. basedefderef : tderef;
  546. constructor create;
  547. constructor create_subrange(_basedef:tenumdef;_min,_max:aint);
  548. constructor ppuload(ppufile:tcompilerppufile);
  549. destructor destroy;override;
  550. function getcopy : tstoreddef;override;
  551. procedure ppuwrite(ppufile:tcompilerppufile);override;
  552. procedure buildderef;override;
  553. procedure deref;override;
  554. procedure derefimpl;override;
  555. function gettypename:string;override;
  556. function is_publishable : boolean;override;
  557. procedure calcsavesize;
  558. procedure setmax(_max:aint);
  559. procedure setmin(_min:aint);
  560. function min:aint;
  561. function max:aint;
  562. { rtti }
  563. procedure write_rtti_data(rt:trttitype);override;
  564. procedure write_child_rtti_data(rt:trttitype);override;
  565. end;
  566. tsetdef = class(tstoreddef)
  567. elementtype : ttype;
  568. settype : tsettype;
  569. setbase,
  570. setmax : aint;
  571. constructor create(const t:ttype;high : aint);
  572. constructor ppuload(ppufile:tcompilerppufile);
  573. destructor destroy;override;
  574. function getcopy : tstoreddef;override;
  575. procedure ppuwrite(ppufile:tcompilerppufile);override;
  576. procedure buildderef;override;
  577. procedure deref;override;
  578. function gettypename:string;override;
  579. function is_publishable : boolean;override;
  580. { rtti }
  581. procedure write_rtti_data(rt:trttitype);override;
  582. procedure write_child_rtti_data(rt:trttitype);override;
  583. end;
  584. Tdefmatch=(dm_exact,dm_equal,dm_convertl1);
  585. var
  586. aktobjectdef : tobjectdef; { used for private functions check !! }
  587. { default types }
  588. generrortype, { error in definition }
  589. voidpointertype, { pointer for Void-Pointerdef }
  590. charpointertype, { pointer for Char-Pointerdef }
  591. widecharpointertype, { pointer for WideChar-Pointerdef }
  592. voidfarpointertype,
  593. cundefinedtype,
  594. cformaltype, { unique formal definition }
  595. voidtype, { Void (procedure) }
  596. cchartype, { Char }
  597. cwidechartype, { WideChar }
  598. booltype, { boolean type }
  599. u8inttype, { 8-Bit unsigned integer }
  600. s8inttype, { 8-Bit signed integer }
  601. u16inttype, { 16-Bit unsigned integer }
  602. s16inttype, { 16-Bit signed integer }
  603. u32inttype, { 32-Bit unsigned integer }
  604. s32inttype, { 32-Bit signed integer }
  605. u64inttype, { 64-bit unsigned integer }
  606. s64inttype, { 64-bit signed integer }
  607. s32floattype, { pointer for realconstn }
  608. s64floattype, { pointer for realconstn }
  609. s80floattype, { pointer to type of temp. floats }
  610. s64currencytype, { pointer to a currency type }
  611. cshortstringtype, { pointer to type of short string const }
  612. clongstringtype, { pointer to type of long string const }
  613. cansistringtype, { pointer to type of ansi string const }
  614. cwidestringtype, { pointer to type of wide string const }
  615. openshortstringtype, { pointer to type of an open shortstring,
  616. needed for readln() }
  617. openchararraytype, { pointer to type of an open array of char,
  618. needed for readln() }
  619. cfiletype, { get the same definition for all file }
  620. { used for stabs }
  621. methodpointertype, { typecasting of methodpointers to extract self }
  622. { we use only one variant def for every variant class }
  623. cvarianttype,
  624. colevarianttype,
  625. { default integer type s32inttype on 32 bit systems, s64bittype on 64 bit systems }
  626. sinttype,
  627. uinttype,
  628. { unsigned ord type with the same size as a pointer }
  629. ptrinttype,
  630. { several types to simulate more or less C++ objects for GDB }
  631. vmttype,
  632. vmtarraytype,
  633. pvmttype : ttype; { type of classrefs, used for stabs }
  634. { pointer to the anchestor of all classes }
  635. class_tobject : tobjectdef;
  636. { pointer to the ancestor of all COM interfaces }
  637. interface_iunknown : tobjectdef;
  638. { pointer to the TGUID type
  639. of all interfaces }
  640. rec_tguid : trecorddef;
  641. const
  642. {$ifdef i386}
  643. pbestrealtype : ^ttype = @s80floattype;
  644. {$endif}
  645. {$ifdef x86_64}
  646. pbestrealtype : ^ttype = @s80floattype;
  647. {$endif}
  648. {$ifdef m68k}
  649. pbestrealtype : ^ttype = @s64floattype;
  650. {$endif}
  651. {$ifdef alpha}
  652. pbestrealtype : ^ttype = @s64floattype;
  653. {$endif}
  654. {$ifdef powerpc}
  655. pbestrealtype : ^ttype = @s64floattype;
  656. {$endif}
  657. {$ifdef POWERPC64}
  658. pbestrealtype : ^ttype = @s64floattype;
  659. {$endif}
  660. {$ifdef ia64}
  661. pbestrealtype : ^ttype = @s64floattype;
  662. {$endif}
  663. {$ifdef SPARC}
  664. pbestrealtype : ^ttype = @s64floattype;
  665. {$endif SPARC}
  666. {$ifdef vis}
  667. pbestrealtype : ^ttype = @s64floattype;
  668. {$endif vis}
  669. {$ifdef ARM}
  670. pbestrealtype : ^ttype = @s64floattype;
  671. {$endif ARM}
  672. {$ifdef MIPS}
  673. pbestrealtype : ^ttype = @s64floattype;
  674. {$endif MIPS}
  675. function make_mangledname(const typeprefix:string;st:tsymtable;const suffix:string):string;
  676. { should be in the types unit, but the types unit uses the node stuff :( }
  677. function is_interfacecom(def: tdef): boolean;
  678. function is_interfacecorba(def: tdef): boolean;
  679. function is_interface(def: tdef): boolean;
  680. function is_object(def: tdef): boolean;
  681. function is_class(def: tdef): boolean;
  682. function is_cppclass(def: tdef): boolean;
  683. function is_class_or_interface(def: tdef): boolean;
  684. {$ifdef x86}
  685. function use_sse(def : tdef) : boolean;
  686. {$endif x86}
  687. implementation
  688. uses
  689. strings,
  690. { global }
  691. verbose,
  692. { target }
  693. systems,aasmcpu,paramgr,
  694. { symtable }
  695. symsym,symtable,symutil,defutil,
  696. { module }
  697. fmodule,
  698. { other }
  699. gendef,
  700. fpccrc
  701. ;
  702. {****************************************************************************
  703. Constants
  704. ****************************************************************************}
  705. const
  706. varempty = 0;
  707. varnull = 1;
  708. varsmallint = 2;
  709. varinteger = 3;
  710. varsingle = 4;
  711. vardouble = 5;
  712. varcurrency = 6;
  713. vardate = 7;
  714. varolestr = 8;
  715. vardispatch = 9;
  716. varerror = 10;
  717. varboolean = 11;
  718. varvariant = 12;
  719. varunknown = 13;
  720. vardecimal = 14;
  721. varshortint = 16;
  722. varbyte = 17;
  723. varword = 18;
  724. varlongword = 19;
  725. varint64 = 20;
  726. varqword = 21;
  727. varUndefined = -1;
  728. varstrarg = $48;
  729. varstring = $100;
  730. varany = $101;
  731. vartypemask = $fff;
  732. vararray = $2000;
  733. varbyref = $4000;
  734. {****************************************************************************
  735. Helpers
  736. ****************************************************************************}
  737. function make_mangledname(const typeprefix:string;st:tsymtable;const suffix:string):string;
  738. var
  739. s,hs,
  740. prefix : string;
  741. oldlen,
  742. newlen,
  743. i : longint;
  744. crc : dword;
  745. hp : tparavarsym;
  746. begin
  747. prefix:='';
  748. if not assigned(st) then
  749. internalerror(200204212);
  750. { sub procedures }
  751. while (st.symtabletype=localsymtable) do
  752. begin
  753. if st.defowner.deftype<>procdef then
  754. internalerror(200204173);
  755. { Add the full mangledname of procedure to prevent
  756. conflicts with 2 overloads having both a nested procedure
  757. with the same name, see tb0314 (PFV) }
  758. s:=tprocdef(st.defowner).procsym.name;
  759. oldlen:=length(s);
  760. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  761. begin
  762. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  763. if not(vo_is_hidden_para in hp.varoptions) then
  764. s:=s+'$'+hp.vartype.def.mangledparaname;
  765. end;
  766. if not is_void(tprocdef(st.defowner).rettype.def) then
  767. s:=s+'$$'+tprocdef(st.defowner).rettype.def.mangledparaname;
  768. newlen:=length(s);
  769. { Replace with CRC if the parameter line is very long }
  770. if (newlen-oldlen>12) and
  771. ((newlen>128) or (newlen-oldlen>64)) then
  772. begin
  773. crc:=$ffffffff;
  774. for i:=0 to tprocdef(st.defowner).paras.count-1 do
  775. begin
  776. hp:=tparavarsym(tprocdef(st.defowner).paras[i]);
  777. if not(vo_is_hidden_para in hp.varoptions) then
  778. begin
  779. hs:=hp.vartype.def.mangledparaname;
  780. crc:=UpdateCrc32(crc,hs[1],length(hs));
  781. end;
  782. end;
  783. hs:=hp.vartype.def.mangledparaname;
  784. crc:=UpdateCrc32(crc,hs[1],length(hs));
  785. s:=Copy(s,1,oldlen)+'$crc'+hexstr(crc,8);
  786. end;
  787. if prefix<>'' then
  788. prefix:=s+'_'+prefix
  789. else
  790. prefix:=s;
  791. st:=st.defowner.owner;
  792. end;
  793. { object/classes symtable }
  794. if (st.symtabletype=objectsymtable) then
  795. begin
  796. if st.defowner.deftype<>objectdef then
  797. internalerror(200204174);
  798. prefix:=tobjectdef(st.defowner).objname^+'_$_'+prefix;
  799. st:=st.defowner.owner;
  800. end;
  801. { symtable must now be static or global }
  802. if not(st.symtabletype in [staticsymtable,globalsymtable]) then
  803. internalerror(200204175);
  804. result:='';
  805. if typeprefix<>'' then
  806. result:=result+typeprefix+'_';
  807. { Add P$ for program, which can have the same name as
  808. a unit }
  809. if (tsymtable(main_module.localsymtable)=st) and
  810. (not main_module.is_unit) then
  811. result:=result+'P$'+st.name^
  812. else
  813. result:=result+st.name^;
  814. if prefix<>'' then
  815. result:=result+'_'+prefix;
  816. if suffix<>'' then
  817. result:=result+'_'+suffix;
  818. { the Darwin assembler assumes that all symbols starting with 'L' are local }
  819. if (target_info.system in [system_powerpc_darwin,system_i386_darwin]) and
  820. (result[1] = 'L') then
  821. result := '_' + result;
  822. end;
  823. {****************************************************************************
  824. TDEF (base class for definitions)
  825. ****************************************************************************}
  826. constructor tstoreddef.create(dt:tdeftype);
  827. var
  828. insertstack : psymtablestackitem;
  829. begin
  830. inherited create(dt);
  831. savesize := 0;
  832. {$ifdef EXTDEBUG}
  833. fileinfo := aktfilepos;
  834. {$endif}
  835. fillchar(localrttilab,sizeof(localrttilab),0);
  836. generictokenbuf:=nil;
  837. genericdef:=nil;
  838. { Register in symtable stack.
  839. Don't register forwarddefs, they are disposed at the
  840. end of an type block }
  841. if assigned(symtablestack) and
  842. (dt<>forwarddef) then
  843. begin
  844. insertstack:=symtablestack.stack;
  845. while assigned(insertstack) and
  846. (insertstack^.symtable.symtabletype=withsymtable) do
  847. insertstack:=insertstack^.next;
  848. if not assigned(insertstack) then
  849. internalerror(200602044);
  850. insertstack^.symtable.insertdef(self);
  851. end;
  852. end;
  853. destructor tstoreddef.destroy;
  854. begin
  855. { remove also index from symtable }
  856. if assigned(owner) then
  857. owner.deletedef(self);
  858. if assigned(generictokenbuf) then
  859. generictokenbuf.free;
  860. inherited destroy;
  861. end;
  862. constructor tstoreddef.ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  863. var
  864. sizeleft,i : longint;
  865. buf : array[0..255] of byte;
  866. begin
  867. inherited create(dt);
  868. {$ifdef EXTDEBUG}
  869. fillchar(fileinfo,sizeof(fileinfo),0);
  870. {$endif}
  871. fillchar(localrttilab,sizeof(localrttilab),0);
  872. { load }
  873. indexnr:=ppufile.getword;
  874. ppufile.getderef(typesymderef);
  875. ppufile.getsmallset(defoptions);
  876. if df_has_rttitable in defoptions then
  877. ppufile.getderef(rttitablesymderef);
  878. if df_has_inittable in defoptions then
  879. ppufile.getderef(inittablesymderef);
  880. if df_generic in defoptions then
  881. begin
  882. sizeleft:=ppufile.getlongint;
  883. initgeneric;
  884. while sizeleft>0 do
  885. begin
  886. if sizeleft>sizeof(buf) then
  887. i:=sizeof(buf)
  888. else
  889. i:=sizeleft;
  890. ppufile.getdata(buf,i);
  891. generictokenbuf.write(buf,i);
  892. dec(sizeleft,i);
  893. end;
  894. end;
  895. if df_specialization in defoptions then
  896. ppufile.getderef(genericdefderef);
  897. end;
  898. procedure Tstoreddef.reset;
  899. begin
  900. if assigned(rttitablesym) then
  901. trttisym(rttitablesym).lab := nil;
  902. if assigned(inittablesym) then
  903. trttisym(inittablesym).lab := nil;
  904. localrttilab[initrtti]:=nil;
  905. localrttilab[fullrtti]:=nil;
  906. end;
  907. function tstoreddef.getcopy : tstoreddef;
  908. begin
  909. Message(sym_e_cant_create_unique_type);
  910. getcopy:=terrordef.create;
  911. end;
  912. procedure tstoreddef.ppuwrite(ppufile:tcompilerppufile);
  913. var
  914. sizeleft,i : longint;
  915. buf : array[0..255] of byte;
  916. oldintfcrc : boolean;
  917. begin
  918. ppufile.putword(indexnr);
  919. ppufile.putderef(typesymderef);
  920. ppufile.putsmallset(defoptions);
  921. if df_has_rttitable in defoptions then
  922. ppufile.putderef(rttitablesymderef);
  923. if df_has_inittable in defoptions then
  924. ppufile.putderef(inittablesymderef);
  925. if df_generic in defoptions then
  926. begin
  927. oldintfcrc:=ppufile.do_interface_crc;
  928. ppufile.do_interface_crc:=false;
  929. if assigned(generictokenbuf) then
  930. begin
  931. sizeleft:=generictokenbuf.size;
  932. generictokenbuf.seek(0);
  933. end
  934. else
  935. sizeleft:=0;
  936. ppufile.putlongint(sizeleft);
  937. while sizeleft>0 do
  938. begin
  939. if sizeleft>sizeof(buf) then
  940. i:=sizeof(buf)
  941. else
  942. i:=sizeleft;
  943. generictokenbuf.read(buf,i);
  944. ppufile.putdata(buf,i);
  945. dec(sizeleft,i);
  946. end;
  947. ppufile.do_interface_crc:=oldintfcrc;
  948. end;
  949. if df_specialization in defoptions then
  950. ppufile.putderef(genericdefderef);
  951. end;
  952. procedure tstoreddef.buildderef;
  953. begin
  954. typesymderef.build(typesym);
  955. rttitablesymderef.build(rttitablesym);
  956. inittablesymderef.build(inittablesym);
  957. genericdefderef.build(genericdef);
  958. end;
  959. procedure tstoreddef.buildderefimpl;
  960. begin
  961. end;
  962. procedure tstoreddef.deref;
  963. begin
  964. typesym:=ttypesym(typesymderef.resolve);
  965. if df_has_rttitable in defoptions then
  966. rttitablesym:=trttisym(rttitablesymderef.resolve);
  967. if df_has_inittable in defoptions then
  968. inittablesym:=trttisym(inittablesymderef.resolve);
  969. if df_specialization in defoptions then
  970. genericdef:=tstoreddef(genericdefderef.resolve);
  971. end;
  972. procedure tstoreddef.derefimpl;
  973. begin
  974. end;
  975. function tstoreddef.size : aint;
  976. begin
  977. size:=savesize;
  978. end;
  979. function tstoreddef.getvartype:longint;
  980. begin
  981. result:=varUndefined;
  982. end;
  983. function tstoreddef.alignment : shortint;
  984. begin
  985. { natural alignment by default }
  986. alignment:=size_2_align(savesize);
  987. end;
  988. procedure tstoreddef.write_rtti_name;
  989. var
  990. str : string;
  991. begin
  992. { name }
  993. if assigned(typesym) then
  994. begin
  995. str:=ttypesym(typesym).realname;
  996. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(chr(length(str))+str));
  997. end
  998. else
  999. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(#0))
  1000. end;
  1001. procedure tstoreddef.write_rtti_data(rt:trttitype);
  1002. begin
  1003. current_asmdata.asmlists[al_rtti].concat(tai_const.create_8bit(tkUnknown));
  1004. write_rtti_name;
  1005. end;
  1006. procedure tstoreddef.write_child_rtti_data(rt:trttitype);
  1007. begin
  1008. end;
  1009. function tstoreddef.get_rtti_label(rt:trttitype) : tasmsymbol;
  1010. begin
  1011. { try to reuse persistent rtti data }
  1012. if (rt=fullrtti) and (df_has_rttitable in defoptions) then
  1013. get_rtti_label:=trttisym(rttitablesym).get_label
  1014. else
  1015. if (rt=initrtti) and (df_has_inittable in defoptions) then
  1016. get_rtti_label:=trttisym(inittablesym).get_label
  1017. else
  1018. begin
  1019. if not assigned(localrttilab[rt]) then
  1020. begin
  1021. current_asmdata.getdatalabel(localrttilab[rt]);
  1022. write_child_rtti_data(rt);
  1023. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  1024. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,localrttilab[rt].name,const_align(sizeof(aint)));
  1025. current_asmdata.asmlists[al_rtti].concat(Tai_symbol.Create_global(localrttilab[rt],0));
  1026. write_rtti_data(rt);
  1027. current_asmdata.asmlists[al_rtti].concat(Tai_symbol_end.Create(localrttilab[rt]));
  1028. end;
  1029. get_rtti_label:=localrttilab[rt];
  1030. end;
  1031. end;
  1032. { returns true, if the definition can be published }
  1033. function tstoreddef.is_publishable : boolean;
  1034. begin
  1035. is_publishable:=false;
  1036. end;
  1037. { needs an init table }
  1038. function tstoreddef.needs_inittable : boolean;
  1039. begin
  1040. needs_inittable:=false;
  1041. end;
  1042. function tstoreddef.is_intregable : boolean;
  1043. begin
  1044. is_intregable:=false;
  1045. case deftype of
  1046. orddef,
  1047. pointerdef,
  1048. enumdef,
  1049. classrefdef:
  1050. is_intregable:=true;
  1051. procvardef :
  1052. is_intregable:=not(po_methodpointer in tprocvardef(self).procoptions);
  1053. objectdef:
  1054. is_intregable:=is_class(self) or is_interface(self);
  1055. setdef:
  1056. is_intregable:=(tsetdef(self).settype=smallset);
  1057. end;
  1058. end;
  1059. function tstoreddef.is_fpuregable : boolean;
  1060. begin
  1061. {$ifdef x86}
  1062. result:=use_sse(self);
  1063. {$else x86}
  1064. result:=(deftype=floatdef) and not(cs_fp_emulation in aktmoduleswitches);
  1065. {$endif x86}
  1066. end;
  1067. procedure tstoreddef.initgeneric;
  1068. begin
  1069. if assigned(generictokenbuf) then
  1070. internalerror(200512131);
  1071. generictokenbuf:=tdynamicarray.create(256);
  1072. end;
  1073. {****************************************************************************
  1074. Tstringdef
  1075. ****************************************************************************}
  1076. constructor tstringdef.createshort(l : byte);
  1077. begin
  1078. inherited create(stringdef);
  1079. string_typ:=st_shortstring;
  1080. len:=l;
  1081. savesize:=len+1;
  1082. end;
  1083. constructor tstringdef.loadshort(ppufile:tcompilerppufile);
  1084. begin
  1085. inherited ppuload(stringdef,ppufile);
  1086. string_typ:=st_shortstring;
  1087. len:=ppufile.getbyte;
  1088. savesize:=len+1;
  1089. end;
  1090. constructor tstringdef.createlong(l : aint);
  1091. begin
  1092. inherited create(stringdef);
  1093. string_typ:=st_longstring;
  1094. len:=l;
  1095. savesize:=sizeof(aint);
  1096. end;
  1097. constructor tstringdef.loadlong(ppufile:tcompilerppufile);
  1098. begin
  1099. inherited ppuload(stringdef,ppufile);
  1100. string_typ:=st_longstring;
  1101. len:=ppufile.getaint;
  1102. savesize:=sizeof(aint);
  1103. end;
  1104. constructor tstringdef.createansi(l:aint);
  1105. begin
  1106. inherited create(stringdef);
  1107. string_typ:=st_ansistring;
  1108. len:=l;
  1109. savesize:=sizeof(aint);
  1110. end;
  1111. constructor tstringdef.loadansi(ppufile:tcompilerppufile);
  1112. begin
  1113. inherited ppuload(stringdef,ppufile);
  1114. string_typ:=st_ansistring;
  1115. len:=ppufile.getaint;
  1116. savesize:=sizeof(aint);
  1117. end;
  1118. constructor tstringdef.createwide(l : aint);
  1119. begin
  1120. inherited create(stringdef);
  1121. string_typ:=st_widestring;
  1122. len:=l;
  1123. savesize:=sizeof(aint);
  1124. end;
  1125. constructor tstringdef.loadwide(ppufile:tcompilerppufile);
  1126. begin
  1127. inherited ppuload(stringdef,ppufile);
  1128. string_typ:=st_widestring;
  1129. len:=ppufile.getaint;
  1130. savesize:=sizeof(aint);
  1131. end;
  1132. function tstringdef.getcopy : tstoreddef;
  1133. begin
  1134. result:=tstringdef.create(deftype);
  1135. result.deftype:=stringdef;
  1136. tstringdef(result).string_typ:=string_typ;
  1137. tstringdef(result).len:=len;
  1138. tstringdef(result).savesize:=savesize;
  1139. end;
  1140. function tstringdef.stringtypname:string;
  1141. const
  1142. typname:array[tstringtype] of string[8]=(
  1143. 'shortstr','longstr','ansistr','widestr'
  1144. );
  1145. begin
  1146. stringtypname:=typname[string_typ];
  1147. end;
  1148. procedure tstringdef.ppuwrite(ppufile:tcompilerppufile);
  1149. begin
  1150. inherited ppuwrite(ppufile);
  1151. if string_typ=st_shortstring then
  1152. begin
  1153. {$ifdef extdebug}
  1154. if len > 255 then internalerror(12122002);
  1155. {$endif}
  1156. ppufile.putbyte(byte(len))
  1157. end
  1158. else
  1159. ppufile.putaint(len);
  1160. case string_typ of
  1161. st_shortstring : ppufile.writeentry(ibshortstringdef);
  1162. st_longstring : ppufile.writeentry(iblongstringdef);
  1163. st_ansistring : ppufile.writeentry(ibansistringdef);
  1164. st_widestring : ppufile.writeentry(ibwidestringdef);
  1165. end;
  1166. end;
  1167. function tstringdef.needs_inittable : boolean;
  1168. begin
  1169. needs_inittable:=string_typ in [st_ansistring,st_widestring];
  1170. end;
  1171. function tstringdef.gettypename : string;
  1172. const
  1173. names : array[tstringtype] of string[11] = (
  1174. 'ShortString','LongString','AnsiString','WideString');
  1175. begin
  1176. gettypename:=names[string_typ];
  1177. end;
  1178. function tstringdef.alignment : shortint;
  1179. begin
  1180. case string_typ of
  1181. st_widestring,
  1182. st_ansistring:
  1183. alignment:=size_2_align(savesize);
  1184. st_longstring,
  1185. st_shortstring:
  1186. {$ifdef cpurequiresproperalignment}
  1187. { char to string accesses byte 0 and 1 with one word access }
  1188. alignment:=size_2_align(2);
  1189. {$else cpurequiresproperalignment}
  1190. alignment:=size_2_align(1);
  1191. {$endif cpurequiresproperalignment}
  1192. else
  1193. internalerror(200412301);
  1194. end;
  1195. end;
  1196. procedure tstringdef.write_rtti_data(rt:trttitype);
  1197. begin
  1198. case string_typ of
  1199. st_ansistring:
  1200. begin
  1201. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkAString));
  1202. write_rtti_name;
  1203. end;
  1204. st_widestring:
  1205. begin
  1206. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWString));
  1207. write_rtti_name;
  1208. end;
  1209. st_longstring:
  1210. begin
  1211. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkLString));
  1212. write_rtti_name;
  1213. end;
  1214. st_shortstring:
  1215. begin
  1216. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSString));
  1217. write_rtti_name;
  1218. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(len));
  1219. {$ifdef cpurequiresproperalignment}
  1220. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1221. {$endif cpurequiresproperalignment}
  1222. end;
  1223. end;
  1224. end;
  1225. function tstringdef.getmangledparaname : string;
  1226. begin
  1227. getmangledparaname:='STRING';
  1228. end;
  1229. function tstringdef.is_publishable : boolean;
  1230. begin
  1231. is_publishable:=true;
  1232. end;
  1233. {****************************************************************************
  1234. TENUMDEF
  1235. ****************************************************************************}
  1236. constructor tenumdef.create;
  1237. begin
  1238. inherited create(enumdef);
  1239. minval:=0;
  1240. maxval:=0;
  1241. calcsavesize;
  1242. has_jumps:=false;
  1243. basedef:=nil;
  1244. firstenum:=nil;
  1245. end;
  1246. constructor tenumdef.create_subrange(_basedef:tenumdef;_min,_max:aint);
  1247. begin
  1248. inherited create(enumdef);
  1249. minval:=_min;
  1250. maxval:=_max;
  1251. basedef:=_basedef;
  1252. calcsavesize;
  1253. has_jumps:=false;
  1254. firstenum:=basedef.firstenum;
  1255. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1256. firstenum:=tenumsym(firstenum).nextenum;
  1257. end;
  1258. constructor tenumdef.ppuload(ppufile:tcompilerppufile);
  1259. begin
  1260. inherited ppuload(enumdef,ppufile);
  1261. ppufile.getderef(basedefderef);
  1262. minval:=ppufile.getaint;
  1263. maxval:=ppufile.getaint;
  1264. savesize:=ppufile.getaint;
  1265. has_jumps:=false;
  1266. firstenum:=Nil;
  1267. end;
  1268. function tenumdef.getcopy : tstoreddef;
  1269. begin
  1270. if assigned(basedef) then
  1271. result:=tenumdef.create_subrange(basedef,minval,maxval)
  1272. else
  1273. begin
  1274. result:=tenumdef.create;
  1275. tenumdef(result).minval:=minval;
  1276. tenumdef(result).maxval:=maxval;
  1277. end;
  1278. tenumdef(result).has_jumps:=has_jumps;
  1279. tenumdef(result).firstenum:=firstenum;
  1280. tenumdef(result).basedefderef:=basedefderef;
  1281. end;
  1282. procedure tenumdef.calcsavesize;
  1283. begin
  1284. if (aktpackenum=8) or (min<low(longint)) or (int64(max)>high(cardinal)) then
  1285. savesize:=8
  1286. else
  1287. if (aktpackenum=4) or (min<low(smallint)) or (max>high(word)) then
  1288. savesize:=4
  1289. else
  1290. if (aktpackenum=2) or (min<low(shortint)) or (max>high(byte)) then
  1291. savesize:=2
  1292. else
  1293. savesize:=1;
  1294. end;
  1295. procedure tenumdef.setmax(_max:aint);
  1296. begin
  1297. maxval:=_max;
  1298. calcsavesize;
  1299. end;
  1300. procedure tenumdef.setmin(_min:aint);
  1301. begin
  1302. minval:=_min;
  1303. calcsavesize;
  1304. end;
  1305. function tenumdef.min:aint;
  1306. begin
  1307. min:=minval;
  1308. end;
  1309. function tenumdef.max:aint;
  1310. begin
  1311. max:=maxval;
  1312. end;
  1313. procedure tenumdef.buildderef;
  1314. begin
  1315. inherited buildderef;
  1316. basedefderef.build(basedef);
  1317. end;
  1318. procedure tenumdef.deref;
  1319. begin
  1320. inherited deref;
  1321. basedef:=tenumdef(basedefderef.resolve);
  1322. { restart ordering }
  1323. firstenum:=nil;
  1324. end;
  1325. procedure tenumdef.derefimpl;
  1326. begin
  1327. if assigned(basedef) and
  1328. (firstenum=nil) then
  1329. begin
  1330. firstenum:=basedef.firstenum;
  1331. while assigned(firstenum) and (tenumsym(firstenum).value<>minval) do
  1332. firstenum:=tenumsym(firstenum).nextenum;
  1333. end;
  1334. end;
  1335. destructor tenumdef.destroy;
  1336. begin
  1337. inherited destroy;
  1338. end;
  1339. procedure tenumdef.ppuwrite(ppufile:tcompilerppufile);
  1340. begin
  1341. inherited ppuwrite(ppufile);
  1342. ppufile.putderef(basedefderef);
  1343. ppufile.putaint(min);
  1344. ppufile.putaint(max);
  1345. ppufile.putaint(savesize);
  1346. ppufile.writeentry(ibenumdef);
  1347. end;
  1348. procedure tenumdef.write_child_rtti_data(rt:trttitype);
  1349. begin
  1350. if assigned(basedef) then
  1351. basedef.get_rtti_label(rt);
  1352. end;
  1353. procedure tenumdef.write_rtti_data(rt:trttitype);
  1354. var
  1355. hp : tenumsym;
  1356. begin
  1357. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkEnumeration));
  1358. write_rtti_name;
  1359. {$ifdef cpurequiresproperalignment}
  1360. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1361. {$endif cpurequiresproperalignment}
  1362. case longint(savesize) of
  1363. 1:
  1364. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUByte));
  1365. 2:
  1366. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otUWord));
  1367. 4:
  1368. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  1369. end;
  1370. {$ifdef cpurequiresproperalignment}
  1371. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(4));
  1372. {$endif cpurequiresproperalignment}
  1373. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(min));
  1374. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(max));
  1375. if assigned(basedef) then
  1376. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(basedef.get_rtti_label(rt)))
  1377. else
  1378. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  1379. hp:=tenumsym(firstenum);
  1380. while assigned(hp) do
  1381. begin
  1382. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(hp.realname)));
  1383. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(hp.realname));
  1384. hp:=hp.nextenum;
  1385. end;
  1386. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  1387. end;
  1388. function tenumdef.is_publishable : boolean;
  1389. begin
  1390. is_publishable:=true;
  1391. end;
  1392. function tenumdef.gettypename : string;
  1393. begin
  1394. gettypename:='<enumeration type>';
  1395. end;
  1396. {****************************************************************************
  1397. TORDDEF
  1398. ****************************************************************************}
  1399. constructor torddef.create(t : tbasetype;v,b : TConstExprInt);
  1400. begin
  1401. inherited create(orddef);
  1402. low:=v;
  1403. high:=b;
  1404. typ:=t;
  1405. setsize;
  1406. end;
  1407. constructor torddef.ppuload(ppufile:tcompilerppufile);
  1408. begin
  1409. inherited ppuload(orddef,ppufile);
  1410. typ:=tbasetype(ppufile.getbyte);
  1411. if sizeof(TConstExprInt)=8 then
  1412. begin
  1413. low:=ppufile.getint64;
  1414. high:=ppufile.getint64;
  1415. end
  1416. else
  1417. begin
  1418. low:=ppufile.getlongint;
  1419. high:=ppufile.getlongint;
  1420. end;
  1421. setsize;
  1422. end;
  1423. function torddef.getcopy : tstoreddef;
  1424. begin
  1425. result:=torddef.create(typ,low,high);
  1426. result.deftype:=orddef;
  1427. torddef(result).low:=low;
  1428. torddef(result).high:=high;
  1429. torddef(result).typ:=typ;
  1430. torddef(result).savesize:=savesize;
  1431. end;
  1432. function torddef.alignment:shortint;
  1433. begin
  1434. if (target_info.system = system_i386_darwin) and
  1435. (typ in [s64bit,u64bit]) then
  1436. result := 4
  1437. else
  1438. result := inherited alignment;
  1439. end;
  1440. procedure torddef.setsize;
  1441. const
  1442. sizetbl : array[tbasetype] of longint = (
  1443. 0,
  1444. 1,2,4,8,
  1445. 1,2,4,8,
  1446. 1,2,4,
  1447. 1,2,8
  1448. );
  1449. begin
  1450. savesize:=sizetbl[typ];
  1451. end;
  1452. function torddef.getvartype : longint;
  1453. const
  1454. basetype2vartype : array[tbasetype] of longint = (
  1455. varUndefined,
  1456. varbyte,varqword,varlongword,varqword,
  1457. varshortint,varsmallint,varinteger,varint64,
  1458. varboolean,varUndefined,varUndefined,
  1459. varUndefined,varUndefined,varCurrency);
  1460. begin
  1461. result:=basetype2vartype[typ];
  1462. end;
  1463. procedure torddef.ppuwrite(ppufile:tcompilerppufile);
  1464. begin
  1465. inherited ppuwrite(ppufile);
  1466. ppufile.putbyte(byte(typ));
  1467. if sizeof(TConstExprInt)=8 then
  1468. begin
  1469. ppufile.putint64(low);
  1470. ppufile.putint64(high);
  1471. end
  1472. else
  1473. begin
  1474. ppufile.putlongint(low);
  1475. ppufile.putlongint(high);
  1476. end;
  1477. ppufile.writeentry(iborddef);
  1478. end;
  1479. procedure torddef.write_rtti_data(rt:trttitype);
  1480. procedure dointeger;
  1481. const
  1482. trans : array[tbasetype] of byte =
  1483. (otUByte{otNone},
  1484. otUByte,otUWord,otULong,otUByte{otNone},
  1485. otSByte,otSWord,otSLong,otUByte{otNone},
  1486. otUByte,otUWord,otULong,
  1487. otUByte,otUWord,otUByte);
  1488. begin
  1489. write_rtti_name;
  1490. {$ifdef cpurequiresproperalignment}
  1491. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1492. {$endif cpurequiresproperalignment}
  1493. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(byte(trans[typ])));
  1494. {$ifdef cpurequiresproperalignment}
  1495. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(4));
  1496. {$endif cpurequiresproperalignment}
  1497. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(low)));
  1498. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(high)));
  1499. end;
  1500. begin
  1501. case typ of
  1502. s64bit :
  1503. begin
  1504. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInt64));
  1505. write_rtti_name;
  1506. {$ifdef cpurequiresproperalignment}
  1507. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1508. {$endif cpurequiresproperalignment}
  1509. { low }
  1510. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(int64($80000000) shl 32));
  1511. { high }
  1512. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit((int64($7fffffff) shl 32) or int64($ffffffff)));
  1513. end;
  1514. u64bit :
  1515. begin
  1516. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkQWord));
  1517. write_rtti_name;
  1518. {$ifdef cpurequiresproperalignment}
  1519. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1520. {$endif cpurequiresproperalignment}
  1521. { low }
  1522. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(0));
  1523. { high }
  1524. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_64bit(int64((int64($ffffffff) shl 32) or int64($ffffffff))));
  1525. end;
  1526. bool8bit:
  1527. begin
  1528. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkBool));
  1529. dointeger;
  1530. end;
  1531. uchar:
  1532. begin
  1533. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkChar));
  1534. dointeger;
  1535. end;
  1536. uwidechar:
  1537. begin
  1538. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkWChar));
  1539. dointeger;
  1540. end;
  1541. else
  1542. begin
  1543. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkInteger));
  1544. dointeger;
  1545. end;
  1546. end;
  1547. end;
  1548. function torddef.is_publishable : boolean;
  1549. begin
  1550. is_publishable:=(typ<>uvoid);
  1551. end;
  1552. function torddef.gettypename : string;
  1553. const
  1554. names : array[tbasetype] of string[20] = (
  1555. 'untyped',
  1556. 'Byte','Word','DWord','QWord',
  1557. 'ShortInt','SmallInt','LongInt','Int64',
  1558. 'Boolean','WordBool','LongBool',
  1559. 'Char','WideChar','Currency');
  1560. begin
  1561. gettypename:=names[typ];
  1562. end;
  1563. {****************************************************************************
  1564. TFLOATDEF
  1565. ****************************************************************************}
  1566. constructor tfloatdef.create(t : tfloattype);
  1567. begin
  1568. inherited create(floatdef);
  1569. typ:=t;
  1570. setsize;
  1571. end;
  1572. constructor tfloatdef.ppuload(ppufile:tcompilerppufile);
  1573. begin
  1574. inherited ppuload(floatdef,ppufile);
  1575. typ:=tfloattype(ppufile.getbyte);
  1576. setsize;
  1577. end;
  1578. function tfloatdef.getcopy : tstoreddef;
  1579. begin
  1580. result:=tfloatdef.create(typ);
  1581. result.deftype:=floatdef;
  1582. tfloatdef(result).savesize:=savesize;
  1583. end;
  1584. function tfloatdef.alignment:shortint;
  1585. begin
  1586. if (target_info.system = system_i386_darwin) then
  1587. case typ of
  1588. s80real : result:=16;
  1589. s64real,
  1590. s64currency,
  1591. s64comp : result:=4;
  1592. else
  1593. result := inherited alignment;
  1594. end
  1595. else
  1596. result := inherited alignment;
  1597. end;
  1598. procedure tfloatdef.setsize;
  1599. begin
  1600. case typ of
  1601. s32real : savesize:=4;
  1602. s80real : savesize:=10;
  1603. s64real,
  1604. s64currency,
  1605. s64comp : savesize:=8;
  1606. else
  1607. savesize:=0;
  1608. end;
  1609. end;
  1610. function tfloatdef.getvartype : longint;
  1611. const
  1612. floattype2vartype : array[tfloattype] of longint = (
  1613. varSingle,varDouble,varUndefined,
  1614. varUndefined,varCurrency,varUndefined);
  1615. begin
  1616. if (upper(typename)='TDATETIME') and
  1617. assigned(owner) and
  1618. assigned(owner.name) and
  1619. (owner.name^='SYSTEM') then
  1620. result:=varDate
  1621. else
  1622. result:=floattype2vartype[typ];
  1623. end;
  1624. procedure tfloatdef.ppuwrite(ppufile:tcompilerppufile);
  1625. begin
  1626. inherited ppuwrite(ppufile);
  1627. ppufile.putbyte(byte(typ));
  1628. ppufile.writeentry(ibfloatdef);
  1629. end;
  1630. procedure tfloatdef.write_rtti_data(rt:trttitype);
  1631. const
  1632. {tfloattype = (s32real,s64real,s80real,s64bit,s128bit);}
  1633. translate : array[tfloattype] of byte =
  1634. (ftSingle,ftDouble,ftExtended,ftComp,ftCurr,ftFloat128);
  1635. begin
  1636. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkFloat));
  1637. write_rtti_name;
  1638. {$ifdef cpurequiresproperalignment}
  1639. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  1640. {$endif cpurequiresproperalignment}
  1641. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(translate[typ]));
  1642. end;
  1643. function tfloatdef.is_publishable : boolean;
  1644. begin
  1645. is_publishable:=true;
  1646. end;
  1647. function tfloatdef.gettypename : string;
  1648. const
  1649. names : array[tfloattype] of string[20] = (
  1650. 'Single','Double','Extended','Comp','Currency','Float128');
  1651. begin
  1652. gettypename:=names[typ];
  1653. end;
  1654. {****************************************************************************
  1655. TFILEDEF
  1656. ****************************************************************************}
  1657. constructor tfiledef.createtext;
  1658. begin
  1659. inherited create(filedef);
  1660. filetyp:=ft_text;
  1661. typedfiletype.reset;
  1662. setsize;
  1663. end;
  1664. constructor tfiledef.createuntyped;
  1665. begin
  1666. inherited create(filedef);
  1667. filetyp:=ft_untyped;
  1668. typedfiletype.reset;
  1669. setsize;
  1670. end;
  1671. constructor tfiledef.createtyped(const tt : ttype);
  1672. begin
  1673. inherited create(filedef);
  1674. filetyp:=ft_typed;
  1675. typedfiletype:=tt;
  1676. setsize;
  1677. end;
  1678. constructor tfiledef.ppuload(ppufile:tcompilerppufile);
  1679. begin
  1680. inherited ppuload(filedef,ppufile);
  1681. filetyp:=tfiletyp(ppufile.getbyte);
  1682. if filetyp=ft_typed then
  1683. ppufile.gettype(typedfiletype)
  1684. else
  1685. typedfiletype.reset;
  1686. setsize;
  1687. end;
  1688. function tfiledef.getcopy : tstoreddef;
  1689. begin
  1690. case filetyp of
  1691. ft_typed:
  1692. result:=tfiledef.createtyped(typedfiletype);
  1693. ft_untyped:
  1694. result:=tfiledef.createuntyped;
  1695. ft_text:
  1696. result:=tfiledef.createtext;
  1697. else
  1698. internalerror(2004121201);
  1699. end;
  1700. end;
  1701. procedure tfiledef.buildderef;
  1702. begin
  1703. inherited buildderef;
  1704. if filetyp=ft_typed then
  1705. typedfiletype.buildderef;
  1706. end;
  1707. procedure tfiledef.deref;
  1708. begin
  1709. inherited deref;
  1710. if filetyp=ft_typed then
  1711. typedfiletype.resolve;
  1712. end;
  1713. procedure tfiledef.setsize;
  1714. begin
  1715. {$ifdef cpu64bit}
  1716. case filetyp of
  1717. ft_text :
  1718. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1719. savesize:=632
  1720. else
  1721. savesize:=628;
  1722. ft_typed,
  1723. ft_untyped :
  1724. if target_info.system in [system_x86_64_win64,system_ia64_win64] then
  1725. savesize:=372
  1726. else
  1727. savesize:=368;
  1728. end;
  1729. {$else cpu64bit}
  1730. case filetyp of
  1731. ft_text :
  1732. savesize:=592;
  1733. ft_typed,
  1734. ft_untyped :
  1735. savesize:=332;
  1736. end;
  1737. {$endif cpu64bit}
  1738. end;
  1739. procedure tfiledef.ppuwrite(ppufile:tcompilerppufile);
  1740. begin
  1741. inherited ppuwrite(ppufile);
  1742. ppufile.putbyte(byte(filetyp));
  1743. if filetyp=ft_typed then
  1744. ppufile.puttype(typedfiletype);
  1745. ppufile.writeentry(ibfiledef);
  1746. end;
  1747. function tfiledef.gettypename : string;
  1748. begin
  1749. case filetyp of
  1750. ft_untyped:
  1751. gettypename:='File';
  1752. ft_typed:
  1753. gettypename:='File Of '+typedfiletype.def.typename;
  1754. ft_text:
  1755. gettypename:='Text'
  1756. end;
  1757. end;
  1758. function tfiledef.getmangledparaname : string;
  1759. begin
  1760. case filetyp of
  1761. ft_untyped:
  1762. getmangledparaname:='FILE';
  1763. ft_typed:
  1764. getmangledparaname:='FILE$OF$'+typedfiletype.def.mangledparaname;
  1765. ft_text:
  1766. getmangledparaname:='TEXT'
  1767. end;
  1768. end;
  1769. {****************************************************************************
  1770. TVARIANTDEF
  1771. ****************************************************************************}
  1772. constructor tvariantdef.create(v : tvarianttype);
  1773. begin
  1774. inherited create(variantdef);
  1775. varianttype:=v;
  1776. setsize;
  1777. end;
  1778. constructor tvariantdef.ppuload(ppufile:tcompilerppufile);
  1779. begin
  1780. inherited ppuload(variantdef,ppufile);
  1781. varianttype:=tvarianttype(ppufile.getbyte);
  1782. setsize;
  1783. end;
  1784. function tvariantdef.getcopy : tstoreddef;
  1785. begin
  1786. result:=tvariantdef.create(varianttype);
  1787. end;
  1788. procedure tvariantdef.ppuwrite(ppufile:tcompilerppufile);
  1789. begin
  1790. inherited ppuwrite(ppufile);
  1791. ppufile.putbyte(byte(varianttype));
  1792. ppufile.writeentry(ibvariantdef);
  1793. end;
  1794. procedure tvariantdef.setsize;
  1795. begin
  1796. savesize:=16;
  1797. end;
  1798. function tvariantdef.gettypename : string;
  1799. begin
  1800. case varianttype of
  1801. vt_normalvariant:
  1802. gettypename:='Variant';
  1803. vt_olevariant:
  1804. gettypename:='OleVariant';
  1805. end;
  1806. end;
  1807. procedure tvariantdef.write_rtti_data(rt:trttitype);
  1808. begin
  1809. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkVariant));
  1810. end;
  1811. function tvariantdef.needs_inittable : boolean;
  1812. begin
  1813. needs_inittable:=true;
  1814. end;
  1815. function tvariantdef.is_publishable : boolean;
  1816. begin
  1817. is_publishable:=true;
  1818. end;
  1819. {****************************************************************************
  1820. TABSTRACTPOINTERDEF
  1821. ****************************************************************************}
  1822. constructor tabstractpointerdef.create(dt:tdeftype;const tt : ttype);
  1823. begin
  1824. inherited create(dt);
  1825. pointertype:=tt;
  1826. savesize:=sizeof(aint);
  1827. end;
  1828. constructor tabstractpointerdef.ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  1829. begin
  1830. inherited ppuload(dt,ppufile);
  1831. ppufile.gettype(pointertype);
  1832. savesize:=sizeof(aint);
  1833. end;
  1834. procedure tabstractpointerdef.buildderef;
  1835. begin
  1836. inherited buildderef;
  1837. pointertype.buildderef;
  1838. end;
  1839. procedure tabstractpointerdef.deref;
  1840. begin
  1841. inherited deref;
  1842. pointertype.resolve;
  1843. end;
  1844. procedure tabstractpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1845. begin
  1846. inherited ppuwrite(ppufile);
  1847. ppufile.puttype(pointertype);
  1848. end;
  1849. {****************************************************************************
  1850. TPOINTERDEF
  1851. ****************************************************************************}
  1852. constructor tpointerdef.create(const tt : ttype);
  1853. begin
  1854. inherited create(pointerdef,tt);
  1855. is_far:=false;
  1856. end;
  1857. constructor tpointerdef.createfar(const tt : ttype);
  1858. begin
  1859. inherited create(pointerdef,tt);
  1860. is_far:=true;
  1861. end;
  1862. constructor tpointerdef.ppuload(ppufile:tcompilerppufile);
  1863. begin
  1864. inherited ppuload(pointerdef,ppufile);
  1865. is_far:=(ppufile.getbyte<>0);
  1866. end;
  1867. function tpointerdef.getcopy : tstoreddef;
  1868. begin
  1869. result:=tpointerdef.create(pointertype);
  1870. tpointerdef(result).is_far:=is_far;
  1871. tpointerdef(result).savesize:=savesize;
  1872. end;
  1873. procedure tpointerdef.ppuwrite(ppufile:tcompilerppufile);
  1874. begin
  1875. inherited ppuwrite(ppufile);
  1876. ppufile.putbyte(byte(is_far));
  1877. ppufile.writeentry(ibpointerdef);
  1878. end;
  1879. function tpointerdef.gettypename : string;
  1880. begin
  1881. if is_far then
  1882. gettypename:='^'+pointertype.def.typename+';far'
  1883. else
  1884. gettypename:='^'+pointertype.def.typename;
  1885. end;
  1886. {****************************************************************************
  1887. TCLASSREFDEF
  1888. ****************************************************************************}
  1889. constructor tclassrefdef.create(const t:ttype);
  1890. begin
  1891. inherited create(classrefdef,t);
  1892. end;
  1893. constructor tclassrefdef.ppuload(ppufile:tcompilerppufile);
  1894. begin
  1895. inherited ppuload(classrefdef,ppufile);
  1896. end;
  1897. procedure tclassrefdef.ppuwrite(ppufile:tcompilerppufile);
  1898. begin
  1899. inherited ppuwrite(ppufile);
  1900. ppufile.writeentry(ibclassrefdef);
  1901. end;
  1902. function tclassrefdef.gettypename : string;
  1903. begin
  1904. gettypename:='Class Of '+pointertype.def.typename;
  1905. end;
  1906. function tclassrefdef.is_publishable : boolean;
  1907. begin
  1908. result:=true;
  1909. end;
  1910. {***************************************************************************
  1911. TSETDEF
  1912. ***************************************************************************}
  1913. constructor tsetdef.create(const t:ttype;high : aint);
  1914. begin
  1915. inherited create(setdef);
  1916. elementtype:=t;
  1917. // setbase:=low;
  1918. setmax:=high;
  1919. if high<32 then
  1920. begin
  1921. settype:=smallset;
  1922. {$ifdef testvarsets}
  1923. if aktsetalloc=0 THEN { $PACKSET Fixed?}
  1924. {$endif}
  1925. savesize:=Sizeof(longint)
  1926. {$ifdef testvarsets}
  1927. else {No, use $PACKSET VALUE for rounding}
  1928. savesize:=aktsetalloc*((high+aktsetalloc*8-1) DIV (aktsetalloc*8))
  1929. {$endif}
  1930. ;
  1931. end
  1932. else
  1933. if high<256 then
  1934. begin
  1935. settype:=normset;
  1936. savesize:=32;
  1937. end
  1938. else
  1939. {$ifdef testvarsets}
  1940. if high<$10000 then
  1941. begin
  1942. settype:=varset;
  1943. savesize:=4*((high+31) div 32);
  1944. end
  1945. else
  1946. {$endif testvarsets}
  1947. Message(sym_e_ill_type_decl_set);
  1948. end;
  1949. constructor tsetdef.ppuload(ppufile:tcompilerppufile);
  1950. begin
  1951. inherited ppuload(setdef,ppufile);
  1952. ppufile.gettype(elementtype);
  1953. settype:=tsettype(ppufile.getbyte);
  1954. case settype of
  1955. normset : savesize:=32;
  1956. varset : savesize:=ppufile.getlongint;
  1957. smallset : savesize:=Sizeof(longint);
  1958. end;
  1959. end;
  1960. destructor tsetdef.destroy;
  1961. begin
  1962. inherited destroy;
  1963. end;
  1964. function tsetdef.getcopy : tstoreddef;
  1965. begin
  1966. case settype of
  1967. smallset:
  1968. result:=tsetdef.create(elementtype,31);
  1969. normset:
  1970. result:=tsetdef.create(elementtype,255);
  1971. else
  1972. internalerror(2004121202);
  1973. end;
  1974. end;
  1975. procedure tsetdef.ppuwrite(ppufile:tcompilerppufile);
  1976. begin
  1977. inherited ppuwrite(ppufile);
  1978. ppufile.puttype(elementtype);
  1979. ppufile.putbyte(byte(settype));
  1980. if settype=varset then
  1981. ppufile.putlongint(savesize);
  1982. if settype=normset then
  1983. ppufile.putaint(savesize);
  1984. ppufile.writeentry(ibsetdef);
  1985. end;
  1986. procedure tsetdef.buildderef;
  1987. begin
  1988. inherited buildderef;
  1989. elementtype.buildderef;
  1990. end;
  1991. procedure tsetdef.deref;
  1992. begin
  1993. inherited deref;
  1994. elementtype.resolve;
  1995. end;
  1996. procedure tsetdef.write_child_rtti_data(rt:trttitype);
  1997. begin
  1998. tstoreddef(elementtype.def).get_rtti_label(rt);
  1999. end;
  2000. procedure tsetdef.write_rtti_data(rt:trttitype);
  2001. begin
  2002. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkSet));
  2003. write_rtti_name;
  2004. {$ifdef cpurequiresproperalignment}
  2005. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  2006. {$endif cpurequiresproperalignment}
  2007. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(otULong));
  2008. {$ifdef cpurequiresproperalignment}
  2009. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  2010. {$endif cpurequiresproperalignment}
  2011. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(tstoreddef(elementtype.def).get_rtti_label(rt)));
  2012. end;
  2013. function tsetdef.is_publishable : boolean;
  2014. begin
  2015. is_publishable:=(settype=smallset);
  2016. end;
  2017. function tsetdef.gettypename : string;
  2018. begin
  2019. if assigned(elementtype.def) then
  2020. gettypename:='Set Of '+elementtype.def.typename
  2021. else
  2022. gettypename:='Empty Set';
  2023. end;
  2024. {***************************************************************************
  2025. TFORMALDEF
  2026. ***************************************************************************}
  2027. constructor tformaldef.create;
  2028. begin
  2029. inherited create(formaldef);
  2030. savesize:=0;
  2031. end;
  2032. constructor tformaldef.ppuload(ppufile:tcompilerppufile);
  2033. begin
  2034. inherited ppuload(formaldef,ppufile);
  2035. savesize:=0;
  2036. end;
  2037. procedure tformaldef.ppuwrite(ppufile:tcompilerppufile);
  2038. begin
  2039. inherited ppuwrite(ppufile);
  2040. ppufile.writeentry(ibformaldef);
  2041. end;
  2042. function tformaldef.gettypename : string;
  2043. begin
  2044. gettypename:='<Formal type>';
  2045. end;
  2046. {***************************************************************************
  2047. TARRAYDEF
  2048. ***************************************************************************}
  2049. constructor tarraydef.create(l,h : aint;const t : ttype);
  2050. begin
  2051. inherited create(arraydef);
  2052. lowrange:=l;
  2053. highrange:=h;
  2054. rangetype:=t;
  2055. elementtype.reset;
  2056. arrayoptions:=[];
  2057. end;
  2058. constructor tarraydef.create_from_pointer(const elemt : ttype);
  2059. begin
  2060. self.create(0,$7fffffff,s32inttype);
  2061. arrayoptions:=[ado_IsConvertedPointer];
  2062. setelementtype(elemt);
  2063. end;
  2064. constructor tarraydef.ppuload(ppufile:tcompilerppufile);
  2065. begin
  2066. inherited ppuload(arraydef,ppufile);
  2067. { the addresses are calculated later }
  2068. ppufile.gettype(_elementtype);
  2069. ppufile.gettype(rangetype);
  2070. lowrange:=ppufile.getaint;
  2071. highrange:=ppufile.getaint;
  2072. ppufile.getsmallset(arrayoptions);
  2073. end;
  2074. function tarraydef.getcopy : tstoreddef;
  2075. begin
  2076. result:=tarraydef.create(lowrange,highrange,rangetype);
  2077. tarraydef(result).arrayoptions:=arrayoptions;
  2078. tarraydef(result)._elementtype:=_elementtype;
  2079. end;
  2080. procedure tarraydef.buildderef;
  2081. begin
  2082. inherited buildderef;
  2083. _elementtype.buildderef;
  2084. rangetype.buildderef;
  2085. end;
  2086. procedure tarraydef.deref;
  2087. begin
  2088. inherited deref;
  2089. _elementtype.resolve;
  2090. rangetype.resolve;
  2091. end;
  2092. procedure tarraydef.ppuwrite(ppufile:tcompilerppufile);
  2093. begin
  2094. inherited ppuwrite(ppufile);
  2095. ppufile.puttype(_elementtype);
  2096. ppufile.puttype(rangetype);
  2097. ppufile.putaint(lowrange);
  2098. ppufile.putaint(highrange);
  2099. ppufile.putsmallset(arrayoptions);
  2100. ppufile.writeentry(ibarraydef);
  2101. end;
  2102. function tarraydef.elesize : aint;
  2103. begin
  2104. elesize:=_elementtype.def.size;
  2105. end;
  2106. function tarraydef.elecount : aint;
  2107. var
  2108. qhigh,qlow : qword;
  2109. begin
  2110. if ado_IsDynamicArray in arrayoptions then
  2111. begin
  2112. result:=0;
  2113. exit;
  2114. end;
  2115. if (highrange>0) and (lowrange<0) then
  2116. begin
  2117. qhigh:=highrange;
  2118. qlow:=qword(-lowrange);
  2119. { prevent overflow, return -1 to indicate overflow }
  2120. if qhigh+qlow>qword(high(aint)-1) then
  2121. result:=-1
  2122. else
  2123. result:=qhigh+qlow+1;
  2124. end
  2125. else
  2126. result:=int64(highrange)-lowrange+1;
  2127. end;
  2128. function tarraydef.size : aint;
  2129. var
  2130. cachedelecount,
  2131. cachedelesize : aint;
  2132. begin
  2133. if ado_IsDynamicArray in arrayoptions then
  2134. begin
  2135. size:=sizeof(aint);
  2136. exit;
  2137. end;
  2138. { Tarraydef.size may never be called for an open array! }
  2139. if highrange<lowrange then
  2140. internalerror(99080501);
  2141. cachedelesize:=elesize;
  2142. cachedelecount:=elecount;
  2143. { prevent overflow, return -1 to indicate overflow }
  2144. if (cachedelesize <> 0) and
  2145. (
  2146. (cachedelecount < 0) or
  2147. ((high(aint) div cachedelesize) < cachedelecount) or
  2148. { also lowrange*elesize must be < high(aint) to prevent overflow when
  2149. accessing the array, see ncgmem (PFV) }
  2150. ((high(aint) div cachedelesize) < abs(lowrange))
  2151. ) then
  2152. result:=-1
  2153. else
  2154. result:=cachedelesize*cachedelecount;
  2155. end;
  2156. procedure tarraydef.setelementtype(t: ttype);
  2157. begin
  2158. _elementtype:=t;
  2159. if not((ado_IsDynamicArray in arrayoptions) or
  2160. (ado_IsConvertedPointer in arrayoptions) or
  2161. (highrange<lowrange)) then
  2162. begin
  2163. if (size=-1) then
  2164. Message(sym_e_segment_too_large);
  2165. end;
  2166. end;
  2167. function tarraydef.alignment : shortint;
  2168. begin
  2169. { alignment is the size of the elements }
  2170. if (elementtype.def.deftype in [arraydef,recorddef]) or
  2171. ((elementtype.def.deftype=objectdef) and
  2172. is_object(elementtype.def)) then
  2173. alignment:=elementtype.def.alignment
  2174. else
  2175. alignment:=size_2_align(elesize);
  2176. end;
  2177. function tarraydef.needs_inittable : boolean;
  2178. begin
  2179. needs_inittable:=(ado_IsDynamicArray in arrayoptions) or elementtype.def.needs_inittable;
  2180. end;
  2181. procedure tarraydef.write_child_rtti_data(rt:trttitype);
  2182. begin
  2183. tstoreddef(elementtype.def).get_rtti_label(rt);
  2184. end;
  2185. procedure tarraydef.write_rtti_data(rt:trttitype);
  2186. begin
  2187. if ado_IsDynamicArray in arrayoptions then
  2188. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkdynarray))
  2189. else
  2190. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkarray));
  2191. write_rtti_name;
  2192. {$ifdef cpurequiresproperalignment}
  2193. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  2194. {$endif cpurequiresproperalignment}
  2195. { size of elements }
  2196. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_aint(elesize));
  2197. if not(ado_IsDynamicArray in arrayoptions) then
  2198. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_aint(elecount));
  2199. { element type }
  2200. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(tstoreddef(elementtype.def).get_rtti_label(rt)));
  2201. { variant type }
  2202. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tstoreddef(elementtype.def).getvartype));
  2203. end;
  2204. function tarraydef.gettypename : string;
  2205. begin
  2206. if (ado_IsConstString in arrayoptions) then
  2207. result:='Constant String'
  2208. else if (ado_isarrayofconst in arrayoptions) or
  2209. (ado_isConstructor in arrayoptions) then
  2210. begin
  2211. if (ado_isvariant in arrayoptions) or ((highrange=-1) and (lowrange=0)) then
  2212. gettypename:='Array Of Const'
  2213. else
  2214. gettypename:='Array Of '+elementtype.def.typename;
  2215. end
  2216. else if ((highrange=-1) and (lowrange=0)) or (ado_IsDynamicArray in arrayoptions) then
  2217. gettypename:='Array Of '+elementtype.def.typename
  2218. else
  2219. begin
  2220. if rangetype.def.deftype=enumdef then
  2221. gettypename:='Array['+rangetype.def.typename+'] Of '+elementtype.def.typename
  2222. else
  2223. gettypename:='Array['+tostr(lowrange)+'..'+
  2224. tostr(highrange)+'] Of '+elementtype.def.typename
  2225. end;
  2226. end;
  2227. function tarraydef.getmangledparaname : string;
  2228. begin
  2229. if ado_isarrayofconst in arrayoptions then
  2230. getmangledparaname:='array_of_const'
  2231. else
  2232. if ((highrange=-1) and (lowrange=0)) then
  2233. getmangledparaname:='array_of_'+elementtype.def.mangledparaname
  2234. else
  2235. internalerror(200204176);
  2236. end;
  2237. {***************************************************************************
  2238. tabstractrecorddef
  2239. ***************************************************************************}
  2240. function tabstractrecorddef.getsymtable(t:tgetsymtable):tsymtable;
  2241. begin
  2242. if t=gs_record then
  2243. getsymtable:=symtable
  2244. else
  2245. getsymtable:=nil;
  2246. end;
  2247. procedure tabstractrecorddef.reset;
  2248. begin
  2249. inherited reset;
  2250. tstoredsymtable(symtable).reset_all_defs;
  2251. end;
  2252. procedure tabstractrecorddef.count_field_rtti(sym : tnamedindexitem;arg:pointer);
  2253. begin
  2254. if (FRTTIType=fullrtti) or
  2255. ((tsym(sym).typ=fieldvarsym) and
  2256. tfieldvarsym(sym).vartype.def.needs_inittable) then
  2257. inc(Count);
  2258. end;
  2259. procedure tabstractrecorddef.generate_field_rtti(sym:tnamedindexitem;arg:pointer);
  2260. begin
  2261. if (FRTTIType=fullrtti) or
  2262. ((tsym(sym).typ=fieldvarsym) and
  2263. tfieldvarsym(sym).vartype.def.needs_inittable) then
  2264. tstoreddef(tfieldvarsym(sym).vartype.def).get_rtti_label(FRTTIType);
  2265. end;
  2266. procedure tabstractrecorddef.write_field_rtti(sym : tnamedindexitem;arg:pointer);
  2267. begin
  2268. if (FRTTIType=fullrtti) or
  2269. ((tsym(sym).typ=fieldvarsym) and
  2270. tfieldvarsym(sym).vartype.def.needs_inittable) then
  2271. begin
  2272. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(tstoreddef(tfieldvarsym(sym).vartype.def).get_rtti_label(FRTTIType)));
  2273. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tfieldvarsym(sym).fieldoffset));
  2274. end;
  2275. end;
  2276. procedure tabstractrecorddef.buildderefimpl;
  2277. begin
  2278. inherited buildderefimpl;
  2279. tstoredsymtable(symtable).buildderefimpl;
  2280. end;
  2281. procedure tabstractrecorddef.derefimpl;
  2282. var
  2283. storesymtable : tsymtable;
  2284. begin
  2285. inherited derefimpl;
  2286. tstoredsymtable(symtable).derefimpl;
  2287. end;
  2288. {***************************************************************************
  2289. trecorddef
  2290. ***************************************************************************}
  2291. constructor trecorddef.create(p : tsymtable);
  2292. begin
  2293. inherited create(recorddef);
  2294. symtable:=p;
  2295. symtable.defowner:=self;
  2296. isunion:=false;
  2297. end;
  2298. constructor trecorddef.ppuload(ppufile:tcompilerppufile);
  2299. begin
  2300. inherited ppuload(recorddef,ppufile);
  2301. symtable:=trecordsymtable.create(0);
  2302. trecordsymtable(symtable).datasize:=ppufile.getaint;
  2303. trecordsymtable(symtable).fieldalignment:=shortint(ppufile.getbyte);
  2304. trecordsymtable(symtable).recordalignment:=shortint(ppufile.getbyte);
  2305. trecordsymtable(symtable).padalignment:=shortint(ppufile.getbyte);
  2306. trecordsymtable(symtable).ppuload(ppufile);
  2307. symtable.defowner:=self;
  2308. isunion:=false;
  2309. end;
  2310. destructor trecorddef.destroy;
  2311. begin
  2312. if assigned(symtable) then
  2313. symtable.free;
  2314. inherited destroy;
  2315. end;
  2316. function trecorddef.getcopy : tstoreddef;
  2317. begin
  2318. result:=trecorddef.create(symtable.getcopy);
  2319. trecorddef(result).isunion:=isunion;
  2320. end;
  2321. function trecorddef.needs_inittable : boolean;
  2322. begin
  2323. needs_inittable:=trecordsymtable(symtable).needs_init_final
  2324. end;
  2325. procedure trecorddef.buildderef;
  2326. var
  2327. oldrecsyms : tsymtable;
  2328. begin
  2329. inherited buildderef;
  2330. oldrecsyms:=aktrecordsymtable;
  2331. aktrecordsymtable:=symtable;
  2332. { now build the definitions }
  2333. tstoredsymtable(symtable).buildderef;
  2334. aktrecordsymtable:=oldrecsyms;
  2335. end;
  2336. procedure trecorddef.deref;
  2337. var
  2338. oldrecsyms : tsymtable;
  2339. begin
  2340. inherited deref;
  2341. oldrecsyms:=aktrecordsymtable;
  2342. aktrecordsymtable:=symtable;
  2343. { now dereference the definitions }
  2344. tstoredsymtable(symtable).deref;
  2345. aktrecordsymtable:=oldrecsyms;
  2346. { assign TGUID? load only from system unit }
  2347. if not(assigned(rec_tguid)) and
  2348. (upper(typename)='TGUID') and
  2349. assigned(owner) and
  2350. assigned(owner.name) and
  2351. (owner.name^='SYSTEM') then
  2352. rec_tguid:=self;
  2353. end;
  2354. procedure trecorddef.ppuwrite(ppufile:tcompilerppufile);
  2355. begin
  2356. inherited ppuwrite(ppufile);
  2357. ppufile.putaint(trecordsymtable(symtable).datasize);
  2358. ppufile.putbyte(byte(trecordsymtable(symtable).fieldalignment));
  2359. ppufile.putbyte(byte(trecordsymtable(symtable).recordalignment));
  2360. ppufile.putbyte(byte(trecordsymtable(symtable).padalignment));
  2361. ppufile.writeentry(ibrecorddef);
  2362. trecordsymtable(symtable).ppuwrite(ppufile);
  2363. end;
  2364. function trecorddef.size:aint;
  2365. begin
  2366. result:=trecordsymtable(symtable).datasize;
  2367. end;
  2368. function trecorddef.alignment:shortint;
  2369. begin
  2370. alignment:=trecordsymtable(symtable).recordalignment;
  2371. end;
  2372. function trecorddef.padalignment:shortint;
  2373. begin
  2374. padalignment := trecordsymtable(symtable).padalignment;
  2375. end;
  2376. procedure trecorddef.write_child_rtti_data(rt:trttitype);
  2377. begin
  2378. FRTTIType:=rt;
  2379. symtable.foreach(@generate_field_rtti,nil);
  2380. end;
  2381. procedure trecorddef.write_rtti_data(rt:trttitype);
  2382. begin
  2383. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkrecord));
  2384. write_rtti_name;
  2385. {$ifdef cpurequiresproperalignment}
  2386. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  2387. {$endif cpurequiresproperalignment}
  2388. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(size));
  2389. Count:=0;
  2390. FRTTIType:=rt;
  2391. symtable.foreach(@count_field_rtti,nil);
  2392. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(Count));
  2393. symtable.foreach(@write_field_rtti,nil);
  2394. end;
  2395. function trecorddef.gettypename : string;
  2396. begin
  2397. gettypename:='<record type>'
  2398. end;
  2399. {***************************************************************************
  2400. TABSTRACTPROCDEF
  2401. ***************************************************************************}
  2402. constructor tabstractprocdef.create(dt:tdeftype;level:byte);
  2403. begin
  2404. inherited create(dt);
  2405. parast:=tparasymtable.create(level);
  2406. parast.defowner:=self;
  2407. paras:=nil;
  2408. minparacount:=0;
  2409. maxparacount:=0;
  2410. proctypeoption:=potype_none;
  2411. proccalloption:=pocall_none;
  2412. procoptions:=[];
  2413. rettype:=voidtype;
  2414. {$ifdef i386}
  2415. fpu_used:=0;
  2416. {$endif i386}
  2417. savesize:=sizeof(aint);
  2418. requiredargarea:=0;
  2419. has_paraloc_info:=false;
  2420. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2421. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2422. end;
  2423. destructor tabstractprocdef.destroy;
  2424. begin
  2425. if assigned(paras) then
  2426. begin
  2427. {$ifdef MEMDEBUG}
  2428. memprocpara.start;
  2429. {$endif MEMDEBUG}
  2430. paras.free;
  2431. {$ifdef MEMDEBUG}
  2432. memprocpara.stop;
  2433. {$endif MEMDEBUG}
  2434. end;
  2435. if assigned(parast) then
  2436. begin
  2437. {$ifdef MEMDEBUG}
  2438. memprocparast.start;
  2439. {$endif MEMDEBUG}
  2440. parast.free;
  2441. {$ifdef MEMDEBUG}
  2442. memprocparast.stop;
  2443. {$endif MEMDEBUG}
  2444. end;
  2445. inherited destroy;
  2446. end;
  2447. procedure tabstractprocdef.releasemem;
  2448. begin
  2449. if assigned(paras) then
  2450. begin
  2451. paras.free;
  2452. paras:=nil;
  2453. end;
  2454. parast.free;
  2455. parast:=nil;
  2456. end;
  2457. procedure tabstractprocdef.count_para(p:tnamedindexitem;arg:pointer);
  2458. begin
  2459. if (tsym(p).typ<>paravarsym) then
  2460. exit;
  2461. inc(plongint(arg)^);
  2462. if not(vo_is_hidden_para in tparavarsym(p).varoptions) then
  2463. begin
  2464. if not assigned(tparavarsym(p).defaultconstsym) then
  2465. inc(minparacount);
  2466. inc(maxparacount);
  2467. end;
  2468. end;
  2469. procedure tabstractprocdef.insert_para(p:tnamedindexitem;arg:pointer);
  2470. begin
  2471. if (tsym(p).typ<>paravarsym) then
  2472. exit;
  2473. paras.add(p);
  2474. end;
  2475. procedure tabstractprocdef.calcparas;
  2476. var
  2477. paracount : longint;
  2478. begin
  2479. { This can already be assigned when
  2480. we need to reresolve this unit (PFV) }
  2481. if assigned(paras) then
  2482. paras.free;
  2483. paras:=tparalist.create(false);
  2484. paracount:=0;
  2485. minparacount:=0;
  2486. maxparacount:=0;
  2487. parast.foreach(@count_para,@paracount);
  2488. paras.capacity:=paracount;
  2489. { Insert parameters in table }
  2490. parast.foreach(@insert_para,nil);
  2491. { Order parameters }
  2492. paras.sortparas;
  2493. end;
  2494. { all functions returning in FPU are
  2495. assume to use 2 FPU registers
  2496. until the function implementation
  2497. is processed PM }
  2498. procedure tabstractprocdef.test_if_fpu_result;
  2499. begin
  2500. {$ifdef i386}
  2501. if assigned(rettype.def) and
  2502. (rettype.def.deftype=floatdef) then
  2503. fpu_used:=maxfpuregs;
  2504. {$endif i386}
  2505. end;
  2506. procedure tabstractprocdef.buildderef;
  2507. begin
  2508. { released procdef? }
  2509. if not assigned(parast) then
  2510. exit;
  2511. inherited buildderef;
  2512. rettype.buildderef;
  2513. { parast }
  2514. tparasymtable(parast).buildderef;
  2515. end;
  2516. procedure tabstractprocdef.deref;
  2517. begin
  2518. inherited deref;
  2519. rettype.resolve;
  2520. { parast }
  2521. tparasymtable(parast).deref;
  2522. { recalculated parameters }
  2523. calcparas;
  2524. end;
  2525. constructor tabstractprocdef.ppuload(dt:tdeftype;ppufile:tcompilerppufile);
  2526. var
  2527. b : byte;
  2528. begin
  2529. inherited ppuload(dt,ppufile);
  2530. parast:=nil;
  2531. Paras:=nil;
  2532. minparacount:=0;
  2533. maxparacount:=0;
  2534. ppufile.gettype(rettype);
  2535. {$ifdef i386}
  2536. fpu_used:=ppufile.getbyte;
  2537. {$else}
  2538. ppufile.getbyte;
  2539. {$endif i386}
  2540. proctypeoption:=tproctypeoption(ppufile.getbyte);
  2541. proccalloption:=tproccalloption(ppufile.getbyte);
  2542. ppufile.getnormalset(procoptions);
  2543. location_reset(funcretloc[callerside],LOC_INVALID,OS_NO);
  2544. location_reset(funcretloc[calleeside],LOC_INVALID,OS_NO);
  2545. if po_explicitparaloc in procoptions then
  2546. begin
  2547. b:=ppufile.getbyte;
  2548. if b<>sizeof(funcretloc[callerside]) then
  2549. internalerror(200411154);
  2550. ppufile.getdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2551. end;
  2552. savesize:=sizeof(aint);
  2553. has_paraloc_info:=(po_explicitparaloc in procoptions);
  2554. end;
  2555. procedure tabstractprocdef.ppuwrite(ppufile:tcompilerppufile);
  2556. var
  2557. oldintfcrc : boolean;
  2558. begin
  2559. { released procdef? }
  2560. if not assigned(parast) then
  2561. exit;
  2562. inherited ppuwrite(ppufile);
  2563. ppufile.puttype(rettype);
  2564. oldintfcrc:=ppufile.do_interface_crc;
  2565. ppufile.do_interface_crc:=false;
  2566. {$ifdef i386}
  2567. if simplify_ppu then
  2568. fpu_used:=0;
  2569. ppufile.putbyte(fpu_used);
  2570. {$else}
  2571. ppufile.putbyte(0);
  2572. {$endif}
  2573. ppufile.putbyte(ord(proctypeoption));
  2574. ppufile.putbyte(ord(proccalloption));
  2575. ppufile.putnormalset(procoptions);
  2576. ppufile.do_interface_crc:=oldintfcrc;
  2577. if (po_explicitparaloc in procoptions) then
  2578. begin
  2579. { Make a 'valid' funcretloc for procedures }
  2580. ppufile.putbyte(sizeof(funcretloc[callerside]));
  2581. ppufile.putdata(funcretloc[callerside],sizeof(funcretloc[callerside]));
  2582. end;
  2583. end;
  2584. function tabstractprocdef.typename_paras(showhidden:boolean) : string;
  2585. var
  2586. hs,s : string;
  2587. hp : TParavarsym;
  2588. hpc : tconstsym;
  2589. first : boolean;
  2590. i : integer;
  2591. begin
  2592. s:='';
  2593. first:=true;
  2594. for i:=0 to paras.count-1 do
  2595. begin
  2596. hp:=tparavarsym(paras[i]);
  2597. if not(vo_is_hidden_para in hp.varoptions) or
  2598. (showhidden) then
  2599. begin
  2600. if first then
  2601. begin
  2602. s:=s+'(';
  2603. first:=false;
  2604. end
  2605. else
  2606. s:=s+',';
  2607. case hp.varspez of
  2608. vs_var :
  2609. s:=s+'var';
  2610. vs_const :
  2611. s:=s+'const';
  2612. vs_out :
  2613. s:=s+'out';
  2614. end;
  2615. if assigned(hp.vartype.def.typesym) then
  2616. begin
  2617. if s<>'(' then
  2618. s:=s+' ';
  2619. hs:=hp.vartype.def.typesym.realname;
  2620. if hs[1]<>'$' then
  2621. s:=s+hp.vartype.def.typesym.realname
  2622. else
  2623. s:=s+hp.vartype.def.gettypename;
  2624. end
  2625. else
  2626. s:=s+hp.vartype.def.gettypename;
  2627. { default value }
  2628. if assigned(hp.defaultconstsym) then
  2629. begin
  2630. hpc:=tconstsym(hp.defaultconstsym);
  2631. hs:='';
  2632. case hpc.consttyp of
  2633. conststring,
  2634. constresourcestring :
  2635. hs:=strpas(pchar(hpc.value.valueptr));
  2636. constreal :
  2637. str(pbestreal(hpc.value.valueptr)^,hs);
  2638. constpointer :
  2639. hs:=tostr(hpc.value.valueordptr);
  2640. constord :
  2641. begin
  2642. if is_boolean(hpc.consttype.def) then
  2643. begin
  2644. if hpc.value.valueord<>0 then
  2645. hs:='TRUE'
  2646. else
  2647. hs:='FALSE';
  2648. end
  2649. else
  2650. hs:=tostr(hpc.value.valueord);
  2651. end;
  2652. constnil :
  2653. hs:='nil';
  2654. constset :
  2655. hs:='<set>';
  2656. end;
  2657. if hs<>'' then
  2658. s:=s+'="'+hs+'"';
  2659. end;
  2660. end;
  2661. end;
  2662. if not first then
  2663. s:=s+')';
  2664. if (po_varargs in procoptions) then
  2665. s:=s+';VarArgs';
  2666. typename_paras:=s;
  2667. end;
  2668. function tabstractprocdef.is_methodpointer:boolean;
  2669. begin
  2670. result:=false;
  2671. end;
  2672. function tabstractprocdef.is_addressonly:boolean;
  2673. begin
  2674. result:=true;
  2675. end;
  2676. {***************************************************************************
  2677. TPROCDEF
  2678. ***************************************************************************}
  2679. constructor tprocdef.create(level:byte);
  2680. begin
  2681. inherited create(procdef,level);
  2682. _mangledname:=nil;
  2683. fileinfo:=aktfilepos;
  2684. extnumber:=$ffff;
  2685. aliasnames:=tstringlist.create;
  2686. funcretsym:=nil;
  2687. localst := nil;
  2688. defref:=nil;
  2689. lastwritten:=nil;
  2690. refcount:=0;
  2691. if (cs_browser in aktmoduleswitches) and make_ref then
  2692. begin
  2693. defref:=tref.create(defref,@akttokenpos);
  2694. inc(refcount);
  2695. end;
  2696. lastref:=defref;
  2697. forwarddef:=true;
  2698. interfacedef:=false;
  2699. hasforward:=false;
  2700. _class := nil;
  2701. import_dll:=nil;
  2702. import_name:=nil;
  2703. import_nr:=0;
  2704. inlininginfo:=nil;
  2705. end;
  2706. constructor tprocdef.ppuload(ppufile:tcompilerppufile);
  2707. var
  2708. level : byte;
  2709. begin
  2710. inherited ppuload(procdef,ppufile);
  2711. if po_has_mangledname in procoptions then
  2712. _mangledname:=stringdup(ppufile.getstring)
  2713. else
  2714. _mangledname:=nil;
  2715. extnumber:=ppufile.getword;
  2716. level:=ppufile.getbyte;
  2717. ppufile.getderef(_classderef);
  2718. ppufile.getderef(procsymderef);
  2719. ppufile.getposinfo(fileinfo);
  2720. ppufile.getsmallset(symoptions);
  2721. {$ifdef powerpc}
  2722. { library symbol for AmigaOS/MorphOS }
  2723. ppufile.getderef(libsymderef);
  2724. {$endif powerpc}
  2725. { import stuff }
  2726. if po_has_importdll in procoptions then
  2727. import_dll:=stringdup(ppufile.getstring)
  2728. else
  2729. import_dll:=nil;
  2730. if po_has_importname in procoptions then
  2731. import_name:=stringdup(ppufile.getstring)
  2732. else
  2733. import_name:=nil;
  2734. import_nr:=ppufile.getword;
  2735. { inline stuff }
  2736. if (po_has_inlininginfo in procoptions) then
  2737. begin
  2738. ppufile.getderef(funcretsymderef);
  2739. new(inlininginfo);
  2740. ppufile.getsmallset(inlininginfo^.flags);
  2741. end
  2742. else
  2743. begin
  2744. inlininginfo:=nil;
  2745. funcretsym:=nil;
  2746. end;
  2747. { load para symtable }
  2748. parast:=tparasymtable.create(level);
  2749. tparasymtable(parast).ppuload(ppufile);
  2750. parast.defowner:=self;
  2751. { load local symtable }
  2752. if (po_has_inlininginfo in procoptions) or
  2753. ((current_module.flags and uf_local_browser)<>0) then
  2754. begin
  2755. localst:=tlocalsymtable.create(level);
  2756. tlocalsymtable(localst).ppuload(ppufile);
  2757. localst.defowner:=self;
  2758. end
  2759. else
  2760. localst:=nil;
  2761. { inline stuff }
  2762. if (po_has_inlininginfo in procoptions) then
  2763. inlininginfo^.code:=ppuloadnodetree(ppufile);
  2764. { default values for no persistent data }
  2765. if (cs_link_deffile in aktglobalswitches) and
  2766. (tf_need_export in target_info.flags) and
  2767. (po_exports in procoptions) then
  2768. deffile.AddExport(mangledname);
  2769. aliasnames:=tstringlist.create;
  2770. forwarddef:=false;
  2771. interfacedef:=false;
  2772. hasforward:=false;
  2773. lastref:=nil;
  2774. lastwritten:=nil;
  2775. defref:=nil;
  2776. refcount:=0;
  2777. { Disable po_has_inlining until the derefimpl is done }
  2778. exclude(procoptions,po_has_inlininginfo);
  2779. end;
  2780. destructor tprocdef.destroy;
  2781. begin
  2782. if assigned(defref) then
  2783. begin
  2784. defref.freechain;
  2785. defref.free;
  2786. end;
  2787. aliasnames.free;
  2788. if assigned(localst) and (localst.symtabletype<>staticsymtable) then
  2789. begin
  2790. {$ifdef MEMDEBUG}
  2791. memproclocalst.start;
  2792. {$endif MEMDEBUG}
  2793. localst.free;
  2794. {$ifdef MEMDEBUG}
  2795. memproclocalst.start;
  2796. {$endif MEMDEBUG}
  2797. end;
  2798. if assigned(inlininginfo) then
  2799. begin
  2800. {$ifdef MEMDEBUG}
  2801. memprocnodetree.start;
  2802. {$endif MEMDEBUG}
  2803. tnode(inlininginfo^.code).free;
  2804. {$ifdef MEMDEBUG}
  2805. memprocnodetree.start;
  2806. {$endif MEMDEBUG}
  2807. dispose(inlininginfo);
  2808. end;
  2809. stringdispose(import_dll);
  2810. stringdispose(import_name);
  2811. if (po_msgstr in procoptions) then
  2812. strdispose(messageinf.str);
  2813. if assigned(_mangledname) then
  2814. begin
  2815. {$ifdef MEMDEBUG}
  2816. memmanglednames.start;
  2817. {$endif MEMDEBUG}
  2818. stringdispose(_mangledname);
  2819. {$ifdef MEMDEBUG}
  2820. memmanglednames.stop;
  2821. {$endif MEMDEBUG}
  2822. end;
  2823. inherited destroy;
  2824. end;
  2825. procedure tprocdef.ppuwrite(ppufile:tcompilerppufile);
  2826. var
  2827. oldintfcrc : boolean;
  2828. oldparasymtable,
  2829. oldlocalsymtable : tsymtable;
  2830. begin
  2831. { released procdef? }
  2832. if not assigned(parast) then
  2833. exit;
  2834. oldparasymtable:=aktparasymtable;
  2835. oldlocalsymtable:=aktlocalsymtable;
  2836. aktparasymtable:=parast;
  2837. aktlocalsymtable:=localst;
  2838. inherited ppuwrite(ppufile);
  2839. oldintfcrc:=ppufile.do_interface_crc;
  2840. ppufile.do_interface_crc:=false;
  2841. ppufile.do_interface_crc:=oldintfcrc;
  2842. if po_has_mangledname in procoptions then
  2843. ppufile.putstring(_mangledname^);
  2844. ppufile.putword(extnumber);
  2845. ppufile.putbyte(parast.symtablelevel);
  2846. ppufile.putderef(_classderef);
  2847. ppufile.putderef(procsymderef);
  2848. ppufile.putposinfo(fileinfo);
  2849. ppufile.putsmallset(symoptions);
  2850. {$ifdef powerpc}
  2851. { library symbol for AmigaOS/MorphOS }
  2852. ppufile.putderef(libsymderef);
  2853. {$endif powerpc}
  2854. { import }
  2855. if po_has_importdll in procoptions then
  2856. ppufile.putstring(import_dll^);
  2857. if po_has_importname in procoptions then
  2858. ppufile.putstring(import_name^);
  2859. ppufile.putword(import_nr);
  2860. { inline stuff }
  2861. oldintfcrc:=ppufile.do_crc;
  2862. ppufile.do_crc:=false;
  2863. if (po_has_inlininginfo in procoptions) then
  2864. begin
  2865. ppufile.putderef(funcretsymderef);
  2866. ppufile.putsmallset(inlininginfo^.flags);
  2867. end;
  2868. ppufile.do_crc:=oldintfcrc;
  2869. { write this entry }
  2870. ppufile.writeentry(ibprocdef);
  2871. { Save the para symtable, this is taken from the interface }
  2872. tparasymtable(parast).ppuwrite(ppufile);
  2873. { save localsymtable for inline procedures or when local
  2874. browser info is requested, this has no influence on the crc }
  2875. if (po_has_inlininginfo in procoptions) or
  2876. ((current_module.flags and uf_local_browser)<>0) then
  2877. begin
  2878. { we must write a localsymtable }
  2879. if not assigned(localst) then
  2880. insert_localst;
  2881. oldintfcrc:=ppufile.do_crc;
  2882. ppufile.do_crc:=false;
  2883. tlocalsymtable(localst).ppuwrite(ppufile);
  2884. ppufile.do_crc:=oldintfcrc;
  2885. end;
  2886. { node tree for inlining }
  2887. oldintfcrc:=ppufile.do_crc;
  2888. ppufile.do_crc:=false;
  2889. if (po_has_inlininginfo in procoptions) then
  2890. ppuwritenodetree(ppufile,inlininginfo^.code);
  2891. ppufile.do_crc:=oldintfcrc;
  2892. aktparasymtable:=oldparasymtable;
  2893. aktlocalsymtable:=oldlocalsymtable;
  2894. end;
  2895. procedure tprocdef.reset;
  2896. begin
  2897. inherited reset;
  2898. procstarttai:=nil;
  2899. procendtai:=nil;
  2900. end;
  2901. procedure tprocdef.insert_localst;
  2902. begin
  2903. localst:=tlocalsymtable.create(parast.symtablelevel);
  2904. localst.defowner:=self;
  2905. end;
  2906. function tprocdef.fullprocname(showhidden:boolean):string;
  2907. var
  2908. s : string;
  2909. t : ttoken;
  2910. begin
  2911. {$ifdef EXTDEBUG}
  2912. showhidden:=true;
  2913. {$endif EXTDEBUG}
  2914. s:='';
  2915. if owner.symtabletype=localsymtable then
  2916. s:=s+'local ';
  2917. if assigned(_class) then
  2918. begin
  2919. if po_classmethod in procoptions then
  2920. s:=s+'class ';
  2921. s:=s+_class.objrealname^+'.';
  2922. end;
  2923. if proctypeoption=potype_operator then
  2924. begin
  2925. for t:=NOTOKEN to last_overloaded do
  2926. if procsym.realname='$'+overloaded_names[t] then
  2927. begin
  2928. s:='operator '+arraytokeninfo[t].str+typename_paras(showhidden);
  2929. break;
  2930. end;
  2931. end
  2932. else
  2933. s:=s+procsym.realname+typename_paras(showhidden);
  2934. case proctypeoption of
  2935. potype_constructor:
  2936. s:='constructor '+s;
  2937. potype_destructor:
  2938. s:='destructor '+s;
  2939. else
  2940. if assigned(rettype.def) and
  2941. not(is_void(rettype.def)) then
  2942. s:=s+':'+rettype.def.gettypename;
  2943. end;
  2944. { forced calling convention? }
  2945. if (po_hascallingconvention in procoptions) then
  2946. s:=s+';'+ProcCallOptionStr[proccalloption];
  2947. fullprocname:=s;
  2948. end;
  2949. function tprocdef.is_methodpointer:boolean;
  2950. begin
  2951. result:=assigned(_class);
  2952. end;
  2953. function tprocdef.is_addressonly:boolean;
  2954. begin
  2955. result:=assigned(owner) and
  2956. (owner.symtabletype<>objectsymtable);
  2957. end;
  2958. function tprocdef.is_visible_for_object(currobjdef:tobjectdef):boolean;
  2959. begin
  2960. is_visible_for_object:=false;
  2961. { private symbols are allowed when we are in the same
  2962. module as they are defined }
  2963. if (sp_private in symoptions) and
  2964. (owner.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  2965. not(owner.defowner.owner.iscurrentunit) then
  2966. exit;
  2967. if (sp_strictprivate in symoptions) then
  2968. begin
  2969. result:=currobjdef=tobjectdef(owner.defowner);
  2970. exit;
  2971. end;
  2972. if (sp_strictprotected in symoptions) then
  2973. begin
  2974. result:=assigned(currobjdef) and
  2975. currobjdef.is_related(tobjectdef(owner.defowner));
  2976. exit;
  2977. end;
  2978. { protected symbols are visible in the module that defines them and
  2979. also visible to related objects. The related object must be defined
  2980. in the current module }
  2981. if (sp_protected in symoptions) and
  2982. (
  2983. (
  2984. (owner.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  2985. not(owner.defowner.owner.iscurrentunit)
  2986. ) and
  2987. not(
  2988. assigned(currobjdef) and
  2989. (currobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  2990. (currobjdef.owner.iscurrentunit) and
  2991. currobjdef.is_related(tobjectdef(owner.defowner))
  2992. )
  2993. ) then
  2994. exit;
  2995. is_visible_for_object:=true;
  2996. end;
  2997. function tprocdef.getsymtable(t:tgetsymtable):tsymtable;
  2998. begin
  2999. case t of
  3000. gs_local :
  3001. getsymtable:=localst;
  3002. gs_para :
  3003. getsymtable:=parast;
  3004. else
  3005. getsymtable:=nil;
  3006. end;
  3007. end;
  3008. procedure tprocdef.load_references(ppufile:tcompilerppufile;locals:boolean);
  3009. var
  3010. pos : tfileposinfo;
  3011. move_last : boolean;
  3012. oldparasymtable,
  3013. oldlocalsymtable : tsymtable;
  3014. begin
  3015. oldparasymtable:=aktparasymtable;
  3016. oldlocalsymtable:=aktlocalsymtable;
  3017. aktparasymtable:=parast;
  3018. aktlocalsymtable:=localst;
  3019. move_last:=lastwritten=lastref;
  3020. while (not ppufile.endofentry) do
  3021. begin
  3022. ppufile.getposinfo(pos);
  3023. inc(refcount);
  3024. lastref:=tref.create(lastref,@pos);
  3025. lastref.is_written:=true;
  3026. if refcount=1 then
  3027. defref:=lastref;
  3028. end;
  3029. if move_last then
  3030. lastwritten:=lastref;
  3031. if ((current_module.flags and uf_local_browser)<>0) and
  3032. assigned(localst) and
  3033. locals then
  3034. begin
  3035. tparasymtable(parast).load_references(ppufile,locals);
  3036. tlocalsymtable(localst).load_references(ppufile,locals);
  3037. end;
  3038. aktparasymtable:=oldparasymtable;
  3039. aktlocalsymtable:=oldlocalsymtable;
  3040. end;
  3041. Const
  3042. local_symtable_index : word = $8001;
  3043. function tprocdef.write_references(ppufile:tcompilerppufile;locals:boolean):boolean;
  3044. var
  3045. ref : tref;
  3046. {$ifdef supportbrowser}
  3047. pdo : tobjectdef;
  3048. {$endif supportbrowser}
  3049. move_last : boolean;
  3050. d : tderef;
  3051. oldparasymtable,
  3052. oldlocalsymtable : tsymtable;
  3053. begin
  3054. d.reset;
  3055. move_last:=lastwritten=lastref;
  3056. if move_last and
  3057. (((current_module.flags and uf_local_browser)=0) or
  3058. not locals) then
  3059. exit;
  3060. oldparasymtable:=aktparasymtable;
  3061. oldlocalsymtable:=aktlocalsymtable;
  3062. aktparasymtable:=parast;
  3063. aktlocalsymtable:=localst;
  3064. { write address of this symbol }
  3065. d.build(self);
  3066. ppufile.putderef(d);
  3067. { write refs }
  3068. if assigned(lastwritten) then
  3069. ref:=lastwritten
  3070. else
  3071. ref:=defref;
  3072. while assigned(ref) do
  3073. begin
  3074. if ref.moduleindex=current_module.unit_index then
  3075. begin
  3076. ppufile.putposinfo(ref.posinfo);
  3077. ref.is_written:=true;
  3078. if move_last then
  3079. lastwritten:=ref;
  3080. end
  3081. else if not ref.is_written then
  3082. move_last:=false
  3083. else if move_last then
  3084. lastwritten:=ref;
  3085. ref:=ref.nextref;
  3086. end;
  3087. ppufile.writeentry(ibdefref);
  3088. write_references:=true;
  3089. {$ifdef supportbrowser}
  3090. if ((current_module.flags and uf_local_browser)<>0) and
  3091. assigned(localst) and
  3092. locals then
  3093. begin
  3094. pdo:=_class;
  3095. if (owner.symtabletype<>localsymtable) then
  3096. while assigned(pdo) do
  3097. begin
  3098. if pdo.symtable<>aktrecordsymtable then
  3099. begin
  3100. pdo.symtable.moduleid:=local_symtable_index;
  3101. inc(local_symtable_index);
  3102. end;
  3103. pdo:=pdo.childof;
  3104. end;
  3105. parast.moduleid:=local_symtable_index;
  3106. inc(local_symtable_index);
  3107. localst.moduleid:=local_symtable_index;
  3108. inc(local_symtable_index);
  3109. tstoredsymtable(parast).write_references(ppufile,locals);
  3110. tstoredsymtable(localst).write_references(ppufile,locals);
  3111. { decrement for }
  3112. local_symtable_index:=local_symtable_index-2;
  3113. pdo:=_class;
  3114. if (owner.symtabletype<>localsymtable) then
  3115. while assigned(pdo) do
  3116. begin
  3117. if pdo.symtable<>aktrecordsymtable then
  3118. dec(local_symtable_index);
  3119. pdo:=pdo.childof;
  3120. end;
  3121. end;
  3122. {$endif supportbrowser}
  3123. aktparasymtable:=oldparasymtable;
  3124. aktlocalsymtable:=oldlocalsymtable;
  3125. end;
  3126. procedure tprocdef.buildderef;
  3127. var
  3128. oldparasymtable,
  3129. oldlocalsymtable : tsymtable;
  3130. begin
  3131. oldparasymtable:=aktparasymtable;
  3132. oldlocalsymtable:=aktlocalsymtable;
  3133. aktparasymtable:=parast;
  3134. aktlocalsymtable:=localst;
  3135. inherited buildderef;
  3136. _classderef.build(_class);
  3137. { procsym that originaly defined this definition, should be in the
  3138. same symtable }
  3139. procsymderef.build(procsym);
  3140. {$ifdef powerpc}
  3141. { library symbol for AmigaOS/MorphOS }
  3142. libsymderef.build(libsym);
  3143. {$endif powerpc}
  3144. aktparasymtable:=oldparasymtable;
  3145. aktlocalsymtable:=oldlocalsymtable;
  3146. end;
  3147. procedure tprocdef.buildderefimpl;
  3148. var
  3149. oldparasymtable,
  3150. oldlocalsymtable : tsymtable;
  3151. begin
  3152. { released procdef? }
  3153. if not assigned(parast) then
  3154. exit;
  3155. oldparasymtable:=aktparasymtable;
  3156. oldlocalsymtable:=aktlocalsymtable;
  3157. aktparasymtable:=parast;
  3158. aktlocalsymtable:=localst;
  3159. inherited buildderefimpl;
  3160. { Locals }
  3161. if assigned(localst) and
  3162. ((po_has_inlininginfo in procoptions) or
  3163. ((current_module.flags and uf_local_browser)<>0)) then
  3164. begin
  3165. tlocalsymtable(localst).buildderef;
  3166. tlocalsymtable(localst).buildderefimpl;
  3167. end;
  3168. { inline tree }
  3169. if (po_has_inlininginfo in procoptions) then
  3170. begin
  3171. funcretsymderef.build(funcretsym);
  3172. inlininginfo^.code.buildderefimpl;
  3173. end;
  3174. aktparasymtable:=oldparasymtable;
  3175. aktlocalsymtable:=oldlocalsymtable;
  3176. end;
  3177. procedure tprocdef.deref;
  3178. var
  3179. oldparasymtable,
  3180. oldlocalsymtable : tsymtable;
  3181. begin
  3182. { released procdef? }
  3183. if not assigned(parast) then
  3184. exit;
  3185. oldparasymtable:=aktparasymtable;
  3186. oldlocalsymtable:=aktlocalsymtable;
  3187. aktparasymtable:=parast;
  3188. aktlocalsymtable:=localst;
  3189. inherited deref;
  3190. _class:=tobjectdef(_classderef.resolve);
  3191. { procsym that originaly defined this definition, should be in the
  3192. same symtable }
  3193. procsym:=tprocsym(procsymderef.resolve);
  3194. {$ifdef powerpc}
  3195. { library symbol for AmigaOS/MorphOS }
  3196. libsym:=tsym(libsymderef.resolve);
  3197. {$endif powerpc}
  3198. aktparasymtable:=oldparasymtable;
  3199. aktlocalsymtable:=oldlocalsymtable;
  3200. end;
  3201. procedure tprocdef.derefimpl;
  3202. var
  3203. oldparasymtable,
  3204. oldlocalsymtable : tsymtable;
  3205. begin
  3206. oldparasymtable:=aktparasymtable;
  3207. oldlocalsymtable:=aktlocalsymtable;
  3208. aktparasymtable:=parast;
  3209. aktlocalsymtable:=localst;
  3210. { Enable has_inlininginfo when the inlininginfo
  3211. structure is available. The has_inlininginfo was disabled
  3212. after the load, since the data was invalid }
  3213. if assigned(inlininginfo) then
  3214. include(procoptions,po_has_inlininginfo);
  3215. { Locals }
  3216. if assigned(localst) then
  3217. begin
  3218. tlocalsymtable(localst).deref;
  3219. tlocalsymtable(localst).derefimpl;
  3220. end;
  3221. { Inline }
  3222. if (po_has_inlininginfo in procoptions) then
  3223. begin
  3224. inlininginfo^.code.derefimpl;
  3225. { funcretsym, this is always located in the localst }
  3226. funcretsym:=tsym(funcretsymderef.resolve);
  3227. end
  3228. else
  3229. begin
  3230. { safety }
  3231. funcretsym:=nil;
  3232. end;
  3233. aktparasymtable:=oldparasymtable;
  3234. aktlocalsymtable:=oldlocalsymtable;
  3235. end;
  3236. function tprocdef.gettypename : string;
  3237. begin
  3238. gettypename := FullProcName(false);
  3239. end;
  3240. function tprocdef.mangledname : string;
  3241. var
  3242. hp : TParavarsym;
  3243. hs : string;
  3244. crc : dword;
  3245. newlen,
  3246. oldlen,
  3247. i : integer;
  3248. begin
  3249. if assigned(_mangledname) then
  3250. begin
  3251. {$ifdef compress}
  3252. mangledname:=minilzw_decode(_mangledname^);
  3253. {$else}
  3254. mangledname:=_mangledname^;
  3255. {$endif}
  3256. exit;
  3257. end;
  3258. { we need to use the symtable where the procsym is inserted,
  3259. because that is visible to the world }
  3260. mangledname:=make_mangledname('',procsym.owner,procsym.name);
  3261. oldlen:=length(mangledname);
  3262. { add parameter types }
  3263. for i:=0 to paras.count-1 do
  3264. begin
  3265. hp:=tparavarsym(paras[i]);
  3266. if not(vo_is_hidden_para in hp.varoptions) then
  3267. mangledname:=mangledname+'$'+hp.vartype.def.mangledparaname;
  3268. end;
  3269. { add resulttype, add $$ as separator to make it unique from a
  3270. parameter separator }
  3271. if not is_void(rettype.def) then
  3272. mangledname:=mangledname+'$$'+rettype.def.mangledparaname;
  3273. newlen:=length(mangledname);
  3274. { Replace with CRC if the parameter line is very long }
  3275. if (newlen-oldlen>12) and
  3276. ((newlen>128) or (newlen-oldlen>64)) then
  3277. begin
  3278. crc:=$ffffffff;
  3279. for i:=0 to paras.count-1 do
  3280. begin
  3281. hp:=tparavarsym(paras[i]);
  3282. if not(vo_is_hidden_para in hp.varoptions) then
  3283. begin
  3284. hs:=hp.vartype.def.mangledparaname;
  3285. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3286. end;
  3287. end;
  3288. hs:=hp.vartype.def.mangledparaname;
  3289. crc:=UpdateCrc32(crc,hs[1],length(hs));
  3290. mangledname:=Copy(mangledname,1,oldlen)+'$crc'+hexstr(crc,8);
  3291. end;
  3292. {$ifdef compress}
  3293. _mangledname:=stringdup(minilzw_encode(mangledname));
  3294. {$else}
  3295. _mangledname:=stringdup(mangledname);
  3296. {$endif}
  3297. end;
  3298. function tprocdef.cplusplusmangledname : string;
  3299. function getcppparaname(p : tdef) : string;
  3300. const
  3301. ordtype2str : array[tbasetype] of string[2] = (
  3302. '',
  3303. 'Uc','Us','Ui','Us',
  3304. 'Sc','s','i','x',
  3305. 'b','b','b',
  3306. 'c','w','x');
  3307. var
  3308. s : string;
  3309. begin
  3310. case p.deftype of
  3311. orddef:
  3312. s:=ordtype2str[torddef(p).typ];
  3313. pointerdef:
  3314. s:='P'+getcppparaname(tpointerdef(p).pointertype.def);
  3315. else
  3316. internalerror(2103001);
  3317. end;
  3318. getcppparaname:=s;
  3319. end;
  3320. var
  3321. s,s2 : string;
  3322. hp : TParavarsym;
  3323. i : integer;
  3324. begin
  3325. s := procsym.realname;
  3326. if procsym.owner.symtabletype=objectsymtable then
  3327. begin
  3328. s2:=upper(tobjectdef(procsym.owner.defowner).typesym.realname);
  3329. case proctypeoption of
  3330. potype_destructor:
  3331. s:='_$_'+tostr(length(s2))+s2;
  3332. potype_constructor:
  3333. s:='___'+tostr(length(s2))+s2;
  3334. else
  3335. s:='_'+s+'__'+tostr(length(s2))+s2;
  3336. end;
  3337. end
  3338. else s:=s+'__';
  3339. s:=s+'F';
  3340. { concat modifiers }
  3341. { !!!!! }
  3342. { now we handle the parameters }
  3343. if maxparacount>0 then
  3344. begin
  3345. for i:=0 to paras.count-1 do
  3346. begin
  3347. hp:=tparavarsym(paras[i]);
  3348. s2:=getcppparaname(hp.vartype.def);
  3349. if hp.varspez in [vs_var,vs_out] then
  3350. s2:='R'+s2;
  3351. s:=s+s2;
  3352. end;
  3353. end
  3354. else
  3355. s:=s+'v';
  3356. cplusplusmangledname:=s;
  3357. end;
  3358. procedure tprocdef.setmangledname(const s : string);
  3359. begin
  3360. { This is not allowed anymore, the forward declaration
  3361. already needs to create the correct mangledname, no changes
  3362. afterwards are allowed (PFV) }
  3363. { Exception: interface definitions in mode macpas, since in that }
  3364. { case no reference to the old name can exist yet (JM) }
  3365. if assigned(_mangledname) then
  3366. if ((m_mac in aktmodeswitches) and
  3367. (interfacedef)) then
  3368. stringdispose(_mangledname)
  3369. else
  3370. internalerror(200411171);
  3371. {$ifdef compress}
  3372. _mangledname:=stringdup(minilzw_encode(s));
  3373. {$else}
  3374. _mangledname:=stringdup(s);
  3375. {$endif}
  3376. include(procoptions,po_has_mangledname);
  3377. end;
  3378. {***************************************************************************
  3379. TPROCVARDEF
  3380. ***************************************************************************}
  3381. constructor tprocvardef.create(level:byte);
  3382. begin
  3383. inherited create(procvardef,level);
  3384. end;
  3385. constructor tprocvardef.ppuload(ppufile:tcompilerppufile);
  3386. begin
  3387. inherited ppuload(procvardef,ppufile);
  3388. { load para symtable }
  3389. parast:=tparasymtable.create(unknown_level);
  3390. tparasymtable(parast).ppuload(ppufile);
  3391. parast.defowner:=self;
  3392. end;
  3393. function tprocvardef.getcopy : tstoreddef;
  3394. begin
  3395. result:=self;
  3396. (*
  3397. { saves a definition to the return type }
  3398. rettype : ttype;
  3399. parast : tsymtable;
  3400. paras : tparalist;
  3401. proctypeoption : tproctypeoption;
  3402. proccalloption : tproccalloption;
  3403. procoptions : tprocoptions;
  3404. requiredargarea : aint;
  3405. { number of user visibile parameters }
  3406. maxparacount,
  3407. minparacount : byte;
  3408. {$ifdef i386}
  3409. fpu_used : longint; { how many stack fpu must be empty }
  3410. {$endif i386}
  3411. funcretloc : array[tcallercallee] of TLocation;
  3412. has_paraloc_info : boolean; { paraloc info is available }
  3413. tprocvardef = class(tabstractprocdef)
  3414. constructor create(level:byte);
  3415. constructor ppuload(ppufile:tcompilerppufile);
  3416. function getcopy : tstoreddef;override;
  3417. *)
  3418. end;
  3419. procedure tprocvardef.ppuwrite(ppufile:tcompilerppufile);
  3420. var
  3421. oldparasymtable,
  3422. oldlocalsymtable : tsymtable;
  3423. begin
  3424. oldparasymtable:=aktparasymtable;
  3425. oldlocalsymtable:=aktlocalsymtable;
  3426. aktparasymtable:=parast;
  3427. aktlocalsymtable:=nil;
  3428. { here we cannot get a real good value so just give something }
  3429. { plausible (PM) }
  3430. { a more secure way would be
  3431. to allways store in a temp }
  3432. {$ifdef i386}
  3433. if is_fpu(rettype.def) then
  3434. fpu_used:={2}maxfpuregs
  3435. else
  3436. fpu_used:=0;
  3437. {$endif i386}
  3438. inherited ppuwrite(ppufile);
  3439. { Write this entry }
  3440. ppufile.writeentry(ibprocvardef);
  3441. { Save the para symtable, this is taken from the interface }
  3442. tparasymtable(parast).ppuwrite(ppufile);
  3443. aktparasymtable:=oldparasymtable;
  3444. aktlocalsymtable:=oldlocalsymtable;
  3445. end;
  3446. procedure tprocvardef.buildderef;
  3447. var
  3448. oldparasymtable,
  3449. oldlocalsymtable : tsymtable;
  3450. begin
  3451. oldparasymtable:=aktparasymtable;
  3452. oldlocalsymtable:=aktlocalsymtable;
  3453. aktparasymtable:=parast;
  3454. aktlocalsymtable:=nil;
  3455. inherited buildderef;
  3456. aktparasymtable:=oldparasymtable;
  3457. aktlocalsymtable:=oldlocalsymtable;
  3458. end;
  3459. procedure tprocvardef.deref;
  3460. var
  3461. oldparasymtable,
  3462. oldlocalsymtable : tsymtable;
  3463. begin
  3464. oldparasymtable:=aktparasymtable;
  3465. oldlocalsymtable:=aktlocalsymtable;
  3466. aktparasymtable:=parast;
  3467. aktlocalsymtable:=nil;
  3468. inherited deref;
  3469. aktparasymtable:=oldparasymtable;
  3470. aktlocalsymtable:=oldlocalsymtable;
  3471. end;
  3472. function tprocvardef.getsymtable(t:tgetsymtable):tsymtable;
  3473. begin
  3474. case t of
  3475. gs_para :
  3476. getsymtable:=parast;
  3477. else
  3478. getsymtable:=nil;
  3479. end;
  3480. end;
  3481. function tprocvardef.size : aint;
  3482. begin
  3483. if (po_methodpointer in procoptions) and
  3484. not(po_addressonly in procoptions) then
  3485. size:=2*sizeof(aint)
  3486. else
  3487. size:=sizeof(aint);
  3488. end;
  3489. function tprocvardef.is_methodpointer:boolean;
  3490. begin
  3491. result:=(po_methodpointer in procoptions);
  3492. end;
  3493. function tprocvardef.is_addressonly:boolean;
  3494. begin
  3495. result:=not(po_methodpointer in procoptions) or
  3496. (po_addressonly in procoptions);
  3497. end;
  3498. function tprocvardef.getmangledparaname:string;
  3499. begin
  3500. result:='procvar';
  3501. end;
  3502. procedure tprocvardef.write_rtti_data(rt:trttitype);
  3503. procedure write_para(parasym:tparavarsym);
  3504. var
  3505. paraspec : byte;
  3506. begin
  3507. { only store user visible parameters }
  3508. if not(vo_is_hidden_para in parasym.varoptions) then
  3509. begin
  3510. case parasym.varspez of
  3511. vs_value: paraspec := 0;
  3512. vs_const: paraspec := pfConst;
  3513. vs_var : paraspec := pfVar;
  3514. vs_out : paraspec := pfOut;
  3515. end;
  3516. { write flags for current parameter }
  3517. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(paraspec));
  3518. { write name of current parameter }
  3519. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(parasym.realname)));
  3520. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(parasym.realname));
  3521. { write name of type of current parameter }
  3522. tstoreddef(parasym.vartype.def).write_rtti_name;
  3523. end;
  3524. end;
  3525. var
  3526. methodkind : byte;
  3527. i : integer;
  3528. begin
  3529. if po_methodpointer in procoptions then
  3530. begin
  3531. { write method id and name }
  3532. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkmethod));
  3533. write_rtti_name;
  3534. {$ifdef cpurequiresproperalignment}
  3535. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  3536. {$endif cpurequiresproperalignment}
  3537. { write kind of method (can only be function or procedure)}
  3538. if rettype.def = voidtype.def then
  3539. methodkind := mkProcedure
  3540. else
  3541. methodkind := mkFunction;
  3542. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(methodkind));
  3543. { get # of parameters }
  3544. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(maxparacount));
  3545. { write parameter info. The parameters must be written in reverse order
  3546. if this method uses right to left parameter pushing! }
  3547. if proccalloption in pushleftright_pocalls then
  3548. begin
  3549. for i:=0 to paras.count-1 do
  3550. write_para(tparavarsym(paras[i]));
  3551. end
  3552. else
  3553. begin
  3554. for i:=paras.count-1 downto 0 do
  3555. write_para(tparavarsym(paras[i]));
  3556. end;
  3557. { write name of result type }
  3558. tstoreddef(rettype.def).write_rtti_name;
  3559. end
  3560. else
  3561. begin
  3562. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkprocvar));
  3563. write_rtti_name;
  3564. end;
  3565. end;
  3566. function tprocvardef.is_publishable : boolean;
  3567. begin
  3568. is_publishable:=(po_methodpointer in procoptions);
  3569. end;
  3570. function tprocvardef.gettypename : string;
  3571. var
  3572. s: string;
  3573. showhidden : boolean;
  3574. begin
  3575. {$ifdef EXTDEBUG}
  3576. showhidden:=true;
  3577. {$else EXTDEBUG}
  3578. showhidden:=false;
  3579. {$endif EXTDEBUG}
  3580. s:='<';
  3581. if po_classmethod in procoptions then
  3582. s := s+'class method type of'
  3583. else
  3584. if po_addressonly in procoptions then
  3585. s := s+'address of'
  3586. else
  3587. s := s+'procedure variable type of';
  3588. if po_local in procoptions then
  3589. s := s+' local';
  3590. if assigned(rettype.def) and
  3591. (rettype.def<>voidtype.def) then
  3592. s:=s+' function'+typename_paras(showhidden)+':'+rettype.def.gettypename
  3593. else
  3594. s:=s+' procedure'+typename_paras(showhidden);
  3595. if po_methodpointer in procoptions then
  3596. s := s+' of object';
  3597. gettypename := s+';'+ProcCallOptionStr[proccalloption]+'>';
  3598. end;
  3599. {***************************************************************************
  3600. TOBJECTDEF
  3601. ***************************************************************************}
  3602. type
  3603. tproptablelistitem = class(TLinkedListItem)
  3604. index : longint;
  3605. def : tobjectdef;
  3606. end;
  3607. tpropnamelistitem = class(TLinkedListItem)
  3608. index : longint;
  3609. name : stringid;
  3610. owner : tsymtable;
  3611. end;
  3612. var
  3613. proptablelist : tlinkedlist;
  3614. propnamelist : tlinkedlist;
  3615. function searchproptablelist(p : tobjectdef) : tproptablelistitem;
  3616. var
  3617. hp : tproptablelistitem;
  3618. begin
  3619. hp:=tproptablelistitem(proptablelist.first);
  3620. while assigned(hp) do
  3621. if hp.def=p then
  3622. begin
  3623. result:=hp;
  3624. exit;
  3625. end
  3626. else
  3627. hp:=tproptablelistitem(hp.next);
  3628. result:=nil;
  3629. end;
  3630. function searchpropnamelist(const n:string) : tpropnamelistitem;
  3631. var
  3632. hp : tpropnamelistitem;
  3633. begin
  3634. hp:=tpropnamelistitem(propnamelist.first);
  3635. while assigned(hp) do
  3636. if hp.name=n then
  3637. begin
  3638. result:=hp;
  3639. exit;
  3640. end
  3641. else
  3642. hp:=tpropnamelistitem(hp.next);
  3643. result:=nil;
  3644. end;
  3645. constructor tobjectdef.create(ot : tobjectdeftype;const n : string;c : tobjectdef);
  3646. begin
  3647. inherited create(objectdef);
  3648. objecttype:=ot;
  3649. objectoptions:=[];
  3650. childof:=nil;
  3651. symtable:=tobjectsymtable.create(n,aktpackrecords);
  3652. { create space for vmt !! }
  3653. vmt_offset:=0;
  3654. symtable.defowner:=self;
  3655. lastvtableindex:=0;
  3656. set_parent(c);
  3657. objname:=stringdup(upper(n));
  3658. objrealname:=stringdup(n);
  3659. if objecttype in [odt_interfacecorba,odt_interfacecom] then
  3660. prepareguid;
  3661. { setup implemented interfaces }
  3662. if objecttype in [odt_class,odt_interfacecorba] then
  3663. implementedinterfaces:=timplementedinterfaces.create
  3664. else
  3665. implementedinterfaces:=nil;
  3666. writing_class_record_dbginfo:=false;
  3667. end;
  3668. constructor tobjectdef.ppuload(ppufile:tcompilerppufile);
  3669. var
  3670. i,implintfcount: longint;
  3671. d : tderef;
  3672. begin
  3673. inherited ppuload(objectdef,ppufile);
  3674. objecttype:=tobjectdeftype(ppufile.getbyte);
  3675. objrealname:=stringdup(ppufile.getstring);
  3676. objname:=stringdup(upper(objrealname^));
  3677. symtable:=tobjectsymtable.create(objrealname^,0);
  3678. tobjectsymtable(symtable).datasize:=ppufile.getaint;
  3679. tobjectsymtable(symtable).fieldalignment:=ppufile.getbyte;
  3680. tobjectsymtable(symtable).recordalignment:=ppufile.getbyte;
  3681. vmt_offset:=ppufile.getlongint;
  3682. ppufile.getderef(childofderef);
  3683. ppufile.getsmallset(objectoptions);
  3684. { load guid }
  3685. iidstr:=nil;
  3686. if objecttype in [odt_interfacecom,odt_interfacecorba] then
  3687. begin
  3688. new(iidguid);
  3689. ppufile.getguid(iidguid^);
  3690. iidstr:=stringdup(ppufile.getstring);
  3691. lastvtableindex:=ppufile.getlongint;
  3692. end;
  3693. { load implemented interfaces }
  3694. if objecttype in [odt_class,odt_interfacecorba] then
  3695. begin
  3696. implementedinterfaces:=timplementedinterfaces.create;
  3697. implintfcount:=ppufile.getlongint;
  3698. for i:=1 to implintfcount do
  3699. begin
  3700. ppufile.getderef(d);
  3701. implementedinterfaces.addintf_deref(d,ppufile.getlongint);
  3702. end;
  3703. end
  3704. else
  3705. implementedinterfaces:=nil;
  3706. tobjectsymtable(symtable).ppuload(ppufile);
  3707. symtable.defowner:=self;
  3708. { handles the predefined class tobject }
  3709. { the last TOBJECT which is loaded gets }
  3710. { it ! }
  3711. if (childof=nil) and
  3712. (objecttype=odt_class) and
  3713. (objname^='TOBJECT') then
  3714. class_tobject:=self;
  3715. if (childof=nil) and
  3716. (objecttype=odt_interfacecom) and
  3717. (objname^='IUNKNOWN') then
  3718. interface_iunknown:=self;
  3719. writing_class_record_dbginfo:=false;
  3720. end;
  3721. destructor tobjectdef.destroy;
  3722. begin
  3723. if assigned(symtable) then
  3724. symtable.free;
  3725. stringdispose(objname);
  3726. stringdispose(objrealname);
  3727. if assigned(iidstr) then
  3728. stringdispose(iidstr);
  3729. if assigned(implementedinterfaces) then
  3730. implementedinterfaces.free;
  3731. if assigned(iidguid) then
  3732. dispose(iidguid);
  3733. inherited destroy;
  3734. end;
  3735. function tobjectdef.getcopy : tstoreddef;
  3736. var
  3737. i,
  3738. implintfcount : longint;
  3739. begin
  3740. result:=tobjectdef.create(objecttype,objname^,childof);
  3741. tobjectdef(result).symtable:=symtable.getcopy;
  3742. if assigned(objname) then
  3743. tobjectdef(result).objname:=stringdup(objname^);
  3744. if assigned(objrealname) then
  3745. tobjectdef(result).objrealname:=stringdup(objrealname^);
  3746. tobjectdef(result).objectoptions:=objectoptions;
  3747. tobjectdef(result).vmt_offset:=vmt_offset;
  3748. if assigned(iidguid) then
  3749. begin
  3750. new(tobjectdef(result).iidguid);
  3751. move(iidguid^,tobjectdef(result).iidguid^,sizeof(iidguid^));
  3752. end;
  3753. if assigned(iidstr) then
  3754. tobjectdef(result).iidstr:=stringdup(iidstr^);
  3755. tobjectdef(result).lastvtableindex:=lastvtableindex;
  3756. if assigned(implementedinterfaces) then
  3757. begin
  3758. implintfcount:=implementedinterfaces.count;
  3759. for i:=1 to implintfcount do
  3760. begin
  3761. tobjectdef(result).implementedinterfaces.addintf_ioffset(implementedinterfaces.interfaces(i),
  3762. implementedinterfaces.ioffsets(i));
  3763. end;
  3764. end;
  3765. end;
  3766. procedure tobjectdef.ppuwrite(ppufile:tcompilerppufile);
  3767. var
  3768. implintfcount : longint;
  3769. i : longint;
  3770. begin
  3771. inherited ppuwrite(ppufile);
  3772. ppufile.putbyte(byte(objecttype));
  3773. ppufile.putstring(objrealname^);
  3774. ppufile.putaint(tobjectsymtable(symtable).datasize);
  3775. ppufile.putbyte(tobjectsymtable(symtable).fieldalignment);
  3776. ppufile.putbyte(tobjectsymtable(symtable).recordalignment);
  3777. ppufile.putlongint(vmt_offset);
  3778. ppufile.putderef(childofderef);
  3779. ppufile.putsmallset(objectoptions);
  3780. if objecttype in [odt_interfacecom,odt_interfacecorba] then
  3781. begin
  3782. ppufile.putguid(iidguid^);
  3783. ppufile.putstring(iidstr^);
  3784. ppufile.putlongint(lastvtableindex);
  3785. end;
  3786. if objecttype in [odt_class,odt_interfacecorba] then
  3787. begin
  3788. implintfcount:=implementedinterfaces.count;
  3789. ppufile.putlongint(implintfcount);
  3790. for i:=1 to implintfcount do
  3791. begin
  3792. ppufile.putderef(implementedinterfaces.interfacesderef(i));
  3793. ppufile.putlongint(implementedinterfaces.ioffsets(i));
  3794. end;
  3795. end;
  3796. ppufile.writeentry(ibobjectdef);
  3797. tobjectsymtable(symtable).ppuwrite(ppufile);
  3798. end;
  3799. function tobjectdef.gettypename:string;
  3800. begin
  3801. if (self <> aktobjectdef) then
  3802. gettypename:=typename
  3803. else
  3804. { in this case we will go in endless recursion, because then }
  3805. { there is no tsym associated yet with the def. It can occur }
  3806. { (tests/webtbf/tw4757.pp), so for now give a generic name }
  3807. { instead of the actual type name }
  3808. gettypename:='<Currently Parsed Class>';
  3809. end;
  3810. procedure tobjectdef.buildderef;
  3811. var
  3812. oldrecsyms : tsymtable;
  3813. begin
  3814. inherited buildderef;
  3815. childofderef.build(childof);
  3816. oldrecsyms:=aktrecordsymtable;
  3817. aktrecordsymtable:=symtable;
  3818. tstoredsymtable(symtable).buildderef;
  3819. aktrecordsymtable:=oldrecsyms;
  3820. if objecttype in [odt_class,odt_interfacecorba] then
  3821. implementedinterfaces.buildderef;
  3822. end;
  3823. procedure tobjectdef.deref;
  3824. var
  3825. oldrecsyms : tsymtable;
  3826. begin
  3827. inherited deref;
  3828. childof:=tobjectdef(childofderef.resolve);
  3829. oldrecsyms:=aktrecordsymtable;
  3830. aktrecordsymtable:=symtable;
  3831. tstoredsymtable(symtable).deref;
  3832. aktrecordsymtable:=oldrecsyms;
  3833. if objecttype in [odt_class,odt_interfacecorba] then
  3834. implementedinterfaces.deref;
  3835. end;
  3836. function tobjectdef.getparentdef:tdef;
  3837. begin
  3838. {$warning TODO Remove getparentdef hack}
  3839. { With 2 forward declared classes with the child class before the
  3840. parent class the child class is written earlier to the ppu. Leaving it
  3841. possible to have a reference to the parent class for property overriding,
  3842. but the parent class still has the childof not resolved yet (PFV) }
  3843. if childof=nil then
  3844. childof:=tobjectdef(childofderef.resolve);
  3845. result:=childof;
  3846. end;
  3847. procedure tobjectdef.prepareguid;
  3848. begin
  3849. { set up guid }
  3850. if not assigned(iidguid) then
  3851. begin
  3852. new(iidguid);
  3853. fillchar(iidguid^,sizeof(iidguid^),0); { default null guid }
  3854. end;
  3855. { setup iidstring }
  3856. if not assigned(iidstr) then
  3857. iidstr:=stringdup(''); { default is empty string }
  3858. end;
  3859. procedure tobjectdef.set_parent( c : tobjectdef);
  3860. begin
  3861. { nothing to do if the parent was not forward !}
  3862. if assigned(childof) then
  3863. exit;
  3864. childof:=c;
  3865. { some options are inherited !! }
  3866. if assigned(c) then
  3867. begin
  3868. { only important for classes }
  3869. lastvtableindex:=c.lastvtableindex;
  3870. objectoptions:=objectoptions+(c.objectoptions*
  3871. inherited_objectoptions);
  3872. if not (objecttype in [odt_interfacecom,odt_interfacecorba]) then
  3873. begin
  3874. { add the data of the anchestor class }
  3875. inc(tobjectsymtable(symtable).datasize,tobjectsymtable(c.symtable).datasize);
  3876. if (oo_has_vmt in objectoptions) and
  3877. (oo_has_vmt in c.objectoptions) then
  3878. dec(tobjectsymtable(symtable).datasize,sizeof(aint));
  3879. { if parent has a vmt field then
  3880. the offset is the same for the child PM }
  3881. if (oo_has_vmt in c.objectoptions) or is_class(self) then
  3882. begin
  3883. vmt_offset:=c.vmt_offset;
  3884. include(objectoptions,oo_has_vmt);
  3885. end;
  3886. end;
  3887. end;
  3888. end;
  3889. procedure tobjectdef.insertvmt;
  3890. begin
  3891. if objecttype in [odt_interfacecom,odt_interfacecorba] then
  3892. exit;
  3893. if (oo_has_vmt in objectoptions) then
  3894. internalerror(12345)
  3895. else
  3896. begin
  3897. tobjectsymtable(symtable).datasize:=align(tobjectsymtable(symtable).datasize,
  3898. tobjectsymtable(symtable).fieldalignment);
  3899. {$ifdef cpurequiresproperalignment}
  3900. tobjectsymtable(symtable).datasize:=align(tobjectsymtable(symtable).datasize,sizeof(aint));
  3901. {$endif cpurequiresproperalignment}
  3902. vmt_offset:=tobjectsymtable(symtable).datasize;
  3903. inc(tobjectsymtable(symtable).datasize,sizeof(aint));
  3904. include(objectoptions,oo_has_vmt);
  3905. end;
  3906. end;
  3907. procedure tobjectdef.check_forwards;
  3908. begin
  3909. if not(objecttype in [odt_interfacecom,odt_interfacecorba]) then
  3910. tstoredsymtable(symtable).check_forwards;
  3911. if (oo_is_forward in objectoptions) then
  3912. begin
  3913. { ok, in future, the forward can be resolved }
  3914. Message1(sym_e_class_forward_not_resolved,objrealname^);
  3915. exclude(objectoptions,oo_is_forward);
  3916. end;
  3917. end;
  3918. { true, if self inherits from d (or if they are equal) }
  3919. function tobjectdef.is_related(d : tdef) : boolean;
  3920. var
  3921. hp : tobjectdef;
  3922. begin
  3923. hp:=self;
  3924. while assigned(hp) do
  3925. begin
  3926. if hp=d then
  3927. begin
  3928. is_related:=true;
  3929. exit;
  3930. end;
  3931. hp:=hp.childof;
  3932. end;
  3933. is_related:=false;
  3934. end;
  3935. procedure _searchdestructor(sym:Tnamedindexitem;sd:pointer);
  3936. begin
  3937. { if we found already a destructor, then we exit }
  3938. if (ppointer(sd)^=nil) and
  3939. (Tsym(sym).typ=procsym) then
  3940. ppointer(sd)^:=Tprocsym(sym).search_procdef_bytype(potype_destructor);
  3941. end;
  3942. function tobjectdef.searchdestructor : tprocdef;
  3943. var
  3944. o : tobjectdef;
  3945. sd : tprocdef;
  3946. begin
  3947. searchdestructor:=nil;
  3948. o:=self;
  3949. sd:=nil;
  3950. while assigned(o) do
  3951. begin
  3952. o.symtable.foreach_static(@_searchdestructor,@sd);
  3953. if assigned(sd) then
  3954. begin
  3955. searchdestructor:=sd;
  3956. exit;
  3957. end;
  3958. o:=o.childof;
  3959. end;
  3960. end;
  3961. function tobjectdef.size : aint;
  3962. begin
  3963. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba] then
  3964. result:=sizeof(aint)
  3965. else
  3966. result:=tobjectsymtable(symtable).datasize;
  3967. end;
  3968. function tobjectdef.alignment:shortint;
  3969. begin
  3970. if objecttype in [odt_class,odt_interfacecom,odt_interfacecorba] then
  3971. alignment:=sizeof(aint)
  3972. else
  3973. alignment:=tobjectsymtable(symtable).recordalignment;
  3974. end;
  3975. function tobjectdef.vmtmethodoffset(index:longint):longint;
  3976. begin
  3977. { for offset of methods for classes, see rtl/inc/objpash.inc }
  3978. case objecttype of
  3979. odt_class:
  3980. { the +2*sizeof(Aint) is size and -size }
  3981. vmtmethodoffset:=(index+10)*sizeof(aint)+2*sizeof(AInt);
  3982. odt_interfacecom,odt_interfacecorba:
  3983. vmtmethodoffset:=index*sizeof(aint);
  3984. else
  3985. {$ifdef WITHDMT}
  3986. vmtmethodoffset:=(index+4)*sizeof(aint);
  3987. {$else WITHDMT}
  3988. vmtmethodoffset:=(index+3)*sizeof(aint);
  3989. {$endif WITHDMT}
  3990. end;
  3991. end;
  3992. function tobjectdef.vmt_mangledname : string;
  3993. begin
  3994. if not(oo_has_vmt in objectoptions) then
  3995. Message1(parser_n_object_has_no_vmt,objrealname^);
  3996. vmt_mangledname:=make_mangledname('VMT',owner,objname^);
  3997. end;
  3998. function tobjectdef.rtti_name : string;
  3999. begin
  4000. rtti_name:=make_mangledname('RTTI',owner,objname^);
  4001. end;
  4002. function tobjectdef.needs_inittable : boolean;
  4003. begin
  4004. case objecttype of
  4005. odt_class :
  4006. needs_inittable:=false;
  4007. odt_interfacecom:
  4008. needs_inittable:=true;
  4009. odt_interfacecorba:
  4010. needs_inittable:=is_related(interface_iunknown);
  4011. odt_object:
  4012. needs_inittable:=tobjectsymtable(symtable).needs_init_final;
  4013. else
  4014. internalerror(200108267);
  4015. end;
  4016. end;
  4017. function tobjectdef.members_need_inittable : boolean;
  4018. begin
  4019. members_need_inittable:=tobjectsymtable(symtable).needs_init_final;
  4020. end;
  4021. procedure tobjectdef.collect_published_properties(sym:tnamedindexitem;arg:pointer);
  4022. var
  4023. hp : tpropnamelistitem;
  4024. begin
  4025. if (tsym(sym).typ=propertysym) and
  4026. (sp_published in tsym(sym).symoptions) then
  4027. begin
  4028. hp:=searchpropnamelist(tsym(sym).name);
  4029. if not(assigned(hp)) then
  4030. begin
  4031. hp:=tpropnamelistitem.create;
  4032. hp.name:=tsym(sym).name;
  4033. hp.index:=propnamelist.count;
  4034. hp.owner:=tsym(sym).owner;
  4035. propnamelist.concat(hp);
  4036. end;
  4037. end;
  4038. end;
  4039. procedure tobjectdef.count_published_properties(sym:tnamedindexitem;arg:pointer);
  4040. begin
  4041. if (tsym(sym).typ=propertysym) and
  4042. (sp_published in tsym(sym).symoptions) then
  4043. inc(plongint(arg)^);
  4044. end;
  4045. procedure tobjectdef.write_property_info(sym : tnamedindexitem;arg:pointer);
  4046. var
  4047. proctypesinfo : byte;
  4048. propnameitem : tpropnamelistitem;
  4049. procedure writeproc(proc : tsymlist; shiftvalue : byte);
  4050. var
  4051. typvalue : byte;
  4052. hp : psymlistitem;
  4053. address : longint;
  4054. def : tdef;
  4055. begin
  4056. if not(assigned(proc) and assigned(proc.firstsym)) then
  4057. begin
  4058. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,1));
  4059. typvalue:=3;
  4060. end
  4061. else if proc.firstsym^.sym.typ=fieldvarsym then
  4062. begin
  4063. address:=0;
  4064. hp:=proc.firstsym;
  4065. def:=nil;
  4066. while assigned(hp) do
  4067. begin
  4068. case hp^.sltype of
  4069. sl_load :
  4070. begin
  4071. def:=tfieldvarsym(hp^.sym).vartype.def;
  4072. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  4073. end;
  4074. sl_subscript :
  4075. begin
  4076. if not(assigned(def) and (def.deftype=recorddef)) then
  4077. internalerror(200402171);
  4078. inc(address,tfieldvarsym(hp^.sym).fieldoffset);
  4079. def:=tfieldvarsym(hp^.sym).vartype.def;
  4080. end;
  4081. sl_vec :
  4082. begin
  4083. if not(assigned(def) and (def.deftype=arraydef)) then
  4084. internalerror(200402172);
  4085. def:=tarraydef(def).elementtype.def;
  4086. inc(address,def.size*hp^.value);
  4087. end;
  4088. end;
  4089. hp:=hp^.next;
  4090. end;
  4091. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,address));
  4092. typvalue:=0;
  4093. end
  4094. else
  4095. begin
  4096. { When there was an error then procdef is not assigned }
  4097. if not assigned(proc.procdef) then
  4098. exit;
  4099. if not(po_virtualmethod in tprocdef(proc.procdef).procoptions) then
  4100. begin
  4101. current_asmdata.asmlists[al_rtti].concat(Tai_const.createname(tprocdef(proc.procdef).mangledname,0));
  4102. typvalue:=1;
  4103. end
  4104. else
  4105. begin
  4106. { virtual method, write vmt offset }
  4107. current_asmdata.asmlists[al_rtti].concat(Tai_const.create(aitconst_ptr,
  4108. tprocdef(proc.procdef)._class.vmtmethodoffset(tprocdef(proc.procdef).extnumber)));
  4109. typvalue:=2;
  4110. end;
  4111. end;
  4112. proctypesinfo:=proctypesinfo or (typvalue shl shiftvalue);
  4113. end;
  4114. begin
  4115. if (tsym(sym).typ=propertysym) and
  4116. (sp_published in tsym(sym).symoptions) then
  4117. begin
  4118. if ppo_indexed in tpropertysym(sym).propoptions then
  4119. proctypesinfo:=$40
  4120. else
  4121. proctypesinfo:=0;
  4122. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(tstoreddef(tpropertysym(sym).proptype.def).get_rtti_label(fullrtti)));
  4123. writeproc(tpropertysym(sym).readaccess,0);
  4124. writeproc(tpropertysym(sym).writeaccess,2);
  4125. { isn't it stored ? }
  4126. if not(ppo_stored in tpropertysym(sym).propoptions) then
  4127. begin
  4128. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  4129. proctypesinfo:=proctypesinfo or (3 shl 4);
  4130. end
  4131. else
  4132. writeproc(tpropertysym(sym).storedaccess,4);
  4133. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).index));
  4134. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(tpropertysym(sym).default));
  4135. propnameitem:=searchpropnamelist(tpropertysym(sym).name);
  4136. if not assigned(propnameitem) then
  4137. internalerror(200512201);
  4138. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnameitem.index));
  4139. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(proctypesinfo));
  4140. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tpropertysym(sym).realname)));
  4141. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tpropertysym(sym).realname));
  4142. {$ifdef cpurequiresproperalignment}
  4143. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4144. {$endif cpurequiresproperalignment}
  4145. end;
  4146. end;
  4147. procedure tobjectdef.generate_published_child_rtti(sym : tnamedindexitem;arg:pointer);
  4148. begin
  4149. if needs_prop_entry(tsym(sym)) then
  4150. begin
  4151. case tsym(sym).typ of
  4152. propertysym:
  4153. tstoreddef(tpropertysym(sym).proptype.def).get_rtti_label(fullrtti);
  4154. fieldvarsym:
  4155. tstoreddef(tfieldvarsym(sym).vartype.def).get_rtti_label(fullrtti);
  4156. else
  4157. internalerror(1509991);
  4158. end;
  4159. end;
  4160. end;
  4161. procedure tobjectdef.write_child_rtti_data(rt:trttitype);
  4162. begin
  4163. FRTTIType:=rt;
  4164. case rt of
  4165. initrtti :
  4166. symtable.foreach(@generate_field_rtti,nil);
  4167. fullrtti :
  4168. symtable.foreach(@generate_published_child_rtti,nil);
  4169. else
  4170. internalerror(200108301);
  4171. end;
  4172. end;
  4173. procedure tobjectdef.count_published_fields(sym:tnamedindexitem;arg:pointer);
  4174. var
  4175. hp : tproptablelistitem;
  4176. begin
  4177. if (tsym(sym).typ=fieldvarsym) and
  4178. (sp_published in tsym(sym).symoptions) then
  4179. begin
  4180. if tfieldvarsym(sym).vartype.def.deftype<>objectdef then
  4181. internalerror(0206001);
  4182. hp:=searchproptablelist(tobjectdef(tfieldvarsym(sym).vartype.def));
  4183. if not(assigned(hp)) then
  4184. begin
  4185. hp:=tproptablelistitem.create;
  4186. hp.def:=tobjectdef(tfieldvarsym(sym).vartype.def);
  4187. hp.index:=proptablelist.count+1;
  4188. proptablelist.concat(hp);
  4189. end;
  4190. inc(plongint(arg)^);
  4191. end;
  4192. end;
  4193. procedure tobjectdef.writefields(sym:tnamedindexitem;arg:pointer);
  4194. var
  4195. hp : tproptablelistitem;
  4196. begin
  4197. if needs_prop_entry(tsym(sym)) and
  4198. (tsym(sym).typ=fieldvarsym) then
  4199. begin
  4200. {$ifdef cpurequiresproperalignment}
  4201. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(AInt)));
  4202. {$endif cpurequiresproperalignment}
  4203. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_aint(tfieldvarsym(sym).fieldoffset));
  4204. hp:=searchproptablelist(tobjectdef(tfieldvarsym(sym).vartype.def));
  4205. if not(assigned(hp)) then
  4206. internalerror(0206002);
  4207. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(hp.index));
  4208. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(tfieldvarsym(sym).realname)));
  4209. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(tfieldvarsym(sym).realname));
  4210. end;
  4211. end;
  4212. function tobjectdef.generate_field_table : tasmlabel;
  4213. var
  4214. fieldtable,
  4215. classtable : tasmlabel;
  4216. hp : tproptablelistitem;
  4217. fieldcount : longint;
  4218. begin
  4219. proptablelist:=TLinkedList.Create;
  4220. current_asmdata.getdatalabel(fieldtable);
  4221. current_asmdata.getdatalabel(classtable);
  4222. maybe_new_object_file(current_asmdata.asmlists[al_rtti]);
  4223. new_section(current_asmdata.asmlists[al_rtti],sec_rodata,classtable.name,const_align(sizeof(aint)));
  4224. { fields }
  4225. fieldcount:=0;
  4226. symtable.foreach(@count_published_fields,@fieldcount);
  4227. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(fieldtable));
  4228. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(fieldcount));
  4229. {$ifdef cpurequiresproperalignment}
  4230. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4231. {$endif cpurequiresproperalignment}
  4232. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(classtable));
  4233. symtable.foreach(@writefields,nil);
  4234. { generate the class table }
  4235. current_asmdata.asmlists[al_rtti].concat(tai_align.create(const_align(sizeof(aint))));
  4236. current_asmdata.asmlists[al_rtti].concat(Tai_label.Create(classtable));
  4237. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(proptablelist.count));
  4238. {$ifdef cpurequiresproperalignment}
  4239. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4240. {$endif cpurequiresproperalignment}
  4241. hp:=tproptablelistitem(proptablelist.first);
  4242. while assigned(hp) do
  4243. begin
  4244. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(tobjectdef(hp.def).vmt_mangledname,0));
  4245. hp:=tproptablelistitem(hp.next);
  4246. end;
  4247. generate_field_table:=fieldtable;
  4248. proptablelist.free;
  4249. proptablelist:=nil;
  4250. end;
  4251. procedure tobjectdef.write_rtti_data(rt:trttitype);
  4252. procedure collect_unique_published_props(pd:tobjectdef);
  4253. begin
  4254. if assigned(pd.childof) then
  4255. collect_unique_published_props(pd.childof);
  4256. pd.symtable.foreach(@collect_published_properties,nil);
  4257. end;
  4258. var
  4259. i : longint;
  4260. propcount : longint;
  4261. begin
  4262. case objecttype of
  4263. odt_class:
  4264. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkclass));
  4265. odt_object:
  4266. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkobject));
  4267. odt_interfacecom:
  4268. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterface));
  4269. odt_interfacecorba:
  4270. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(tkinterfaceCorba));
  4271. else
  4272. exit;
  4273. end;
  4274. { generate the name }
  4275. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(objrealname^)));
  4276. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(objrealname^));
  4277. {$ifdef cpurequiresproperalignment}
  4278. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4279. {$endif cpurequiresproperalignment}
  4280. case rt of
  4281. initrtti :
  4282. begin
  4283. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(size));
  4284. if objecttype in [odt_class,odt_object] then
  4285. begin
  4286. count:=0;
  4287. FRTTIType:=rt;
  4288. symtable.foreach(@count_field_rtti,nil);
  4289. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(count));
  4290. symtable.foreach(@write_field_rtti,nil);
  4291. end;
  4292. end;
  4293. fullrtti :
  4294. begin
  4295. { Collect unique property names with nameindex }
  4296. propnamelist:=TLinkedList.Create;
  4297. collect_unique_published_props(self);
  4298. if not(objecttype in [odt_interfacecom,odt_interfacecorba]) then
  4299. begin
  4300. if (oo_has_vmt in objectoptions) then
  4301. current_asmdata.asmlists[al_rtti].concat(Tai_const.Createname(vmt_mangledname,0))
  4302. else
  4303. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  4304. end;
  4305. { write parent typeinfo }
  4306. if assigned(childof) and ((oo_can_have_published in childof.objectoptions) or
  4307. (objecttype in [odt_interfacecom,odt_interfacecorba])) then
  4308. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_sym(childof.get_rtti_label(fullrtti)))
  4309. else
  4310. current_asmdata.asmlists[al_rtti].concat(Tai_const.create_sym(nil));
  4311. if objecttype in [odt_object,odt_class] then
  4312. begin
  4313. { total number of unique properties }
  4314. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propnamelist.count));
  4315. end
  4316. else
  4317. { interface: write flags, iid and iidstr }
  4318. begin
  4319. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(
  4320. { ugly, but working }
  4321. longint([
  4322. TCompilerIntfFlag(ord(ifHasGuid)*ord(assigned(iidguid))),
  4323. TCompilerIntfFlag(ord(ifHasStrGUID)*ord(assigned(iidstr)))
  4324. ])
  4325. {
  4326. ifDispInterface,
  4327. ifDispatch, }
  4328. ));
  4329. {$ifdef cpurequiresproperalignment}
  4330. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4331. {$endif cpurequiresproperalignment}
  4332. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_32bit(longint(iidguid^.D1)));
  4333. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(iidguid^.D2));
  4334. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(iidguid^.D3));
  4335. for i:=Low(iidguid^.D4) to High(iidguid^.D4) do
  4336. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(iidguid^.D4[i]));
  4337. end;
  4338. { write unit name }
  4339. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(current_module.realmodulename^)));
  4340. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(current_module.realmodulename^));
  4341. {$ifdef cpurequiresproperalignment}
  4342. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4343. {$endif cpurequiresproperalignment}
  4344. { write iidstr }
  4345. if objecttype in [odt_interfacecom,odt_interfacecorba] then
  4346. begin
  4347. if assigned(iidstr) then
  4348. begin
  4349. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(length(iidstr^)));
  4350. current_asmdata.asmlists[al_rtti].concat(Tai_string.Create(iidstr^));
  4351. end
  4352. else
  4353. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_8bit(0));
  4354. {$ifdef cpurequiresproperalignment}
  4355. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4356. {$endif cpurequiresproperalignment}
  4357. end;
  4358. { write published properties for this object }
  4359. if objecttype in [odt_object,odt_class] then
  4360. begin
  4361. propcount:=0;
  4362. symtable.foreach(@count_published_properties,@propcount);
  4363. current_asmdata.asmlists[al_rtti].concat(Tai_const.Create_16bit(propcount));
  4364. {$ifdef cpurequiresproperalignment}
  4365. current_asmdata.asmlists[al_rtti].concat(Tai_align.Create(sizeof(TConstPtrUInt)));
  4366. {$endif cpurequiresproperalignment}
  4367. end;
  4368. symtable.foreach(@write_property_info,nil);
  4369. propnamelist.free;
  4370. propnamelist:=nil;
  4371. end;
  4372. end;
  4373. end;
  4374. function tobjectdef.is_publishable : boolean;
  4375. begin
  4376. is_publishable:=objecttype in [odt_class,odt_interfacecom,odt_interfacecorba];
  4377. end;
  4378. {****************************************************************************
  4379. TIMPLEMENTEDINTERFACES
  4380. ****************************************************************************}
  4381. type
  4382. tnamemap = class(TNamedIndexItem)
  4383. listnext : TNamedIndexItem;
  4384. newname: pstring;
  4385. constructor create(const aname, anewname: string);
  4386. destructor destroy; override;
  4387. end;
  4388. constructor tnamemap.create(const aname, anewname: string);
  4389. begin
  4390. inherited createname(aname);
  4391. newname:=stringdup(anewname);
  4392. end;
  4393. destructor tnamemap.destroy;
  4394. begin
  4395. stringdispose(newname);
  4396. inherited destroy;
  4397. end;
  4398. type
  4399. tprocdefstore = class(TNamedIndexItem)
  4400. procdef: tprocdef;
  4401. constructor create(aprocdef: tprocdef);
  4402. end;
  4403. constructor tprocdefstore.create(aprocdef: tprocdef);
  4404. begin
  4405. inherited create;
  4406. procdef:=aprocdef;
  4407. end;
  4408. constructor timplintfentry.create(aintf: tobjectdef);
  4409. begin
  4410. inherited create;
  4411. intf:=aintf;
  4412. ioffset:=-1;
  4413. namemappings:=nil;
  4414. procdefs:=nil;
  4415. end;
  4416. constructor timplintfentry.create_deref(const d:tderef);
  4417. begin
  4418. inherited create;
  4419. intf:=nil;
  4420. intfderef:=d;
  4421. ioffset:=-1;
  4422. namemappings:=nil;
  4423. procdefs:=nil;
  4424. end;
  4425. destructor timplintfentry.destroy;
  4426. begin
  4427. if assigned(namemappings) then
  4428. namemappings.free;
  4429. if assigned(procdefs) then
  4430. procdefs.free;
  4431. inherited destroy;
  4432. end;
  4433. constructor timplementedinterfaces.create;
  4434. begin
  4435. finterfaces:=tindexarray.create(1);
  4436. end;
  4437. destructor timplementedinterfaces.destroy;
  4438. begin
  4439. finterfaces.destroy;
  4440. end;
  4441. function timplementedinterfaces.count: longint;
  4442. begin
  4443. count:=finterfaces.count;
  4444. end;
  4445. procedure timplementedinterfaces.checkindex(intfindex: longint);
  4446. begin
  4447. if (intfindex<1) or (intfindex>count) then
  4448. InternalError(200006123);
  4449. end;
  4450. function timplementedinterfaces.interfaces(intfindex: longint): tobjectdef;
  4451. begin
  4452. checkindex(intfindex);
  4453. interfaces:=timplintfentry(finterfaces.search(intfindex)).intf;
  4454. end;
  4455. function timplementedinterfaces.interfacesderef(intfindex: longint): tderef;
  4456. begin
  4457. checkindex(intfindex);
  4458. interfacesderef:=timplintfentry(finterfaces.search(intfindex)).intfderef;
  4459. end;
  4460. function timplementedinterfaces.ioffsets(intfindex: longint): longint;
  4461. begin
  4462. checkindex(intfindex);
  4463. ioffsets:=timplintfentry(finterfaces.search(intfindex)).ioffset;
  4464. end;
  4465. procedure timplementedinterfaces.setioffsets(intfindex,iofs:longint);
  4466. begin
  4467. checkindex(intfindex);
  4468. timplintfentry(finterfaces.search(intfindex)).ioffset:=iofs;
  4469. end;
  4470. function timplementedinterfaces.implindex(intfindex:longint):longint;
  4471. begin
  4472. checkindex(intfindex);
  4473. result:=timplintfentry(finterfaces.search(intfindex)).implindex;
  4474. end;
  4475. procedure timplementedinterfaces.setimplindex(intfindex,implidx:longint);
  4476. begin
  4477. checkindex(intfindex);
  4478. timplintfentry(finterfaces.search(intfindex)).implindex:=implidx;
  4479. end;
  4480. function timplementedinterfaces.searchintf(def: tdef): longint;
  4481. var
  4482. i: longint;
  4483. begin
  4484. i:=1;
  4485. while (i<=count) and (tdef(interfaces(i))<>def) do inc(i);
  4486. if i<=count then
  4487. searchintf:=i
  4488. else
  4489. searchintf:=-1;
  4490. end;
  4491. procedure timplementedinterfaces.buildderef;
  4492. var
  4493. i: longint;
  4494. begin
  4495. for i:=1 to count do
  4496. with timplintfentry(finterfaces.search(i)) do
  4497. intfderef.build(intf);
  4498. end;
  4499. procedure timplementedinterfaces.deref;
  4500. var
  4501. i: longint;
  4502. begin
  4503. for i:=1 to count do
  4504. with timplintfentry(finterfaces.search(i)) do
  4505. intf:=tobjectdef(intfderef.resolve);
  4506. end;
  4507. procedure timplementedinterfaces.addintf_deref(const d:tderef;iofs:longint);
  4508. var
  4509. hintf : timplintfentry;
  4510. begin
  4511. hintf:=timplintfentry.create_deref(d);
  4512. hintf.ioffset:=iofs;
  4513. finterfaces.insert(hintf);
  4514. end;
  4515. procedure timplementedinterfaces.addintf_ioffset(d:tdef;iofs:longint);
  4516. var
  4517. hintf : timplintfentry;
  4518. begin
  4519. hintf:=timplintfentry.create(tobjectdef(d));
  4520. hintf.ioffset:=iofs;
  4521. finterfaces.insert(hintf);
  4522. end;
  4523. procedure timplementedinterfaces.addintf(def: tdef);
  4524. begin
  4525. if not assigned(def) or (searchintf(def)<>-1) or (def.deftype<>objectdef) or
  4526. not (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]) then
  4527. internalerror(200006124);
  4528. finterfaces.insert(timplintfentry.create(tobjectdef(def)));
  4529. end;
  4530. procedure timplementedinterfaces.clearmappings;
  4531. var
  4532. i: longint;
  4533. begin
  4534. for i:=1 to count do
  4535. with timplintfentry(finterfaces.search(i)) do
  4536. begin
  4537. if assigned(namemappings) then
  4538. namemappings.free;
  4539. namemappings:=nil;
  4540. end;
  4541. end;
  4542. procedure timplementedinterfaces.addmappings(intfindex: longint; const origname, newname: string);
  4543. begin
  4544. checkindex(intfindex);
  4545. with timplintfentry(finterfaces.search(intfindex)) do
  4546. begin
  4547. if not assigned(namemappings) then
  4548. namemappings:=tdictionary.create;
  4549. namemappings.insert(tnamemap.create(origname,newname));
  4550. end;
  4551. end;
  4552. function timplementedinterfaces.getmappings(intfindex: longint; const origname: string; var nextexist: pointer): string;
  4553. begin
  4554. checkindex(intfindex);
  4555. if not assigned(nextexist) then
  4556. with timplintfentry(finterfaces.search(intfindex)) do
  4557. begin
  4558. if assigned(namemappings) then
  4559. nextexist:=namemappings.search(origname)
  4560. else
  4561. nextexist:=nil;
  4562. end;
  4563. if assigned(nextexist) then
  4564. begin
  4565. getmappings:=tnamemap(nextexist).newname^;
  4566. nextexist:=tnamemap(nextexist).listnext;
  4567. end
  4568. else
  4569. getmappings:='';
  4570. end;
  4571. procedure timplementedinterfaces.addimplproc(intfindex: longint; procdef: tprocdef);
  4572. var
  4573. found : boolean;
  4574. i : longint;
  4575. begin
  4576. checkindex(intfindex);
  4577. with timplintfentry(finterfaces.search(intfindex)) do
  4578. begin
  4579. if not assigned(procdefs) then
  4580. procdefs:=tindexarray.create(4);
  4581. { No duplicate entries of the same procdef }
  4582. found:=false;
  4583. for i:=1 to procdefs.count do
  4584. if tprocdefstore(procdefs.search(i)).procdef=procdef then
  4585. begin
  4586. found:=true;
  4587. break;
  4588. end;
  4589. if not found then
  4590. procdefs.insert(tprocdefstore.create(procdef));
  4591. end;
  4592. end;
  4593. function timplementedinterfaces.implproccount(intfindex: longint): longint;
  4594. begin
  4595. checkindex(intfindex);
  4596. with timplintfentry(finterfaces.search(intfindex)) do
  4597. if assigned(procdefs) then
  4598. implproccount:=procdefs.count
  4599. else
  4600. implproccount:=0;
  4601. end;
  4602. function timplementedinterfaces.implprocs(intfindex: longint; procindex: longint): tprocdef;
  4603. begin
  4604. checkindex(intfindex);
  4605. with timplintfentry(finterfaces.search(intfindex)) do
  4606. if assigned(procdefs) then
  4607. implprocs:=tprocdefstore(procdefs.search(procindex)).procdef
  4608. else
  4609. internalerror(200006131);
  4610. end;
  4611. function timplementedinterfaces.isimplmergepossible(intfindex, remainindex: longint; var weight: longint): boolean;
  4612. var
  4613. possible: boolean;
  4614. i: longint;
  4615. iiep1: TIndexArray;
  4616. iiep2: TIndexArray;
  4617. begin
  4618. checkindex(intfindex);
  4619. checkindex(remainindex);
  4620. iiep1:=timplintfentry(finterfaces.search(intfindex)).procdefs;
  4621. iiep2:=timplintfentry(finterfaces.search(remainindex)).procdefs;
  4622. if not assigned(iiep1) then { empty interface is mergeable :-) }
  4623. begin
  4624. possible:=true;
  4625. weight:=0;
  4626. end
  4627. else
  4628. begin
  4629. possible:=assigned(iiep2) and (iiep1.count<=iiep2.count);
  4630. i:=1;
  4631. while (possible) and (i<=iiep1.count) do
  4632. begin
  4633. possible:=
  4634. (tprocdefstore(iiep1.search(i)).procdef=tprocdefstore(iiep2.search(i)).procdef);
  4635. inc(i);
  4636. end;
  4637. if possible then
  4638. weight:=iiep1.count;
  4639. end;
  4640. isimplmergepossible:=possible;
  4641. end;
  4642. {****************************************************************************
  4643. TFORWARDDEF
  4644. ****************************************************************************}
  4645. constructor tforwarddef.create(const s:string;const pos : tfileposinfo);
  4646. begin
  4647. inherited create(forwarddef);
  4648. tosymname:=stringdup(s);
  4649. forwardpos:=pos;
  4650. end;
  4651. function tforwarddef.gettypename:string;
  4652. begin
  4653. gettypename:='unresolved forward to '+tosymname^;
  4654. end;
  4655. destructor tforwarddef.destroy;
  4656. begin
  4657. if assigned(tosymname) then
  4658. stringdispose(tosymname);
  4659. inherited destroy;
  4660. end;
  4661. {****************************************************************************
  4662. TUNDEFINEDDEF
  4663. ****************************************************************************}
  4664. constructor tundefineddef.create;
  4665. begin
  4666. inherited create(undefineddef);
  4667. end;
  4668. constructor tundefineddef.ppuload(ppufile:tcompilerppufile);
  4669. begin
  4670. inherited ppuload(undefineddef,ppufile);
  4671. end;
  4672. function tundefineddef.gettypename:string;
  4673. begin
  4674. gettypename:='<undefined type>';
  4675. end;
  4676. procedure tundefineddef.ppuwrite(ppufile:tcompilerppufile);
  4677. begin
  4678. inherited ppuwrite(ppufile);
  4679. ppufile.writeentry(ibundefineddef);
  4680. end;
  4681. {****************************************************************************
  4682. TERRORDEF
  4683. ****************************************************************************}
  4684. constructor terrordef.create;
  4685. begin
  4686. inherited create(errordef);
  4687. end;
  4688. procedure terrordef.ppuwrite(ppufile:tcompilerppufile);
  4689. begin
  4690. { Can't write errordefs to ppu }
  4691. internalerror(200411063);
  4692. end;
  4693. function terrordef.gettypename:string;
  4694. begin
  4695. gettypename:='<erroneous type>';
  4696. end;
  4697. function terrordef.getmangledparaname:string;
  4698. begin
  4699. getmangledparaname:='error';
  4700. end;
  4701. {****************************************************************************
  4702. Definition Helpers
  4703. ****************************************************************************}
  4704. function is_interfacecom(def: tdef): boolean;
  4705. begin
  4706. is_interfacecom:=
  4707. assigned(def) and
  4708. (def.deftype=objectdef) and
  4709. (tobjectdef(def).objecttype=odt_interfacecom);
  4710. end;
  4711. function is_interfacecorba(def: tdef): boolean;
  4712. begin
  4713. is_interfacecorba:=
  4714. assigned(def) and
  4715. (def.deftype=objectdef) and
  4716. (tobjectdef(def).objecttype=odt_interfacecorba);
  4717. end;
  4718. function is_interface(def: tdef): boolean;
  4719. begin
  4720. is_interface:=
  4721. assigned(def) and
  4722. (def.deftype=objectdef) and
  4723. (tobjectdef(def).objecttype in [odt_interfacecom,odt_interfacecorba]);
  4724. end;
  4725. function is_class(def: tdef): boolean;
  4726. begin
  4727. is_class:=
  4728. assigned(def) and
  4729. (def.deftype=objectdef) and
  4730. (tobjectdef(def).objecttype=odt_class);
  4731. end;
  4732. function is_object(def: tdef): boolean;
  4733. begin
  4734. is_object:=
  4735. assigned(def) and
  4736. (def.deftype=objectdef) and
  4737. (tobjectdef(def).objecttype=odt_object);
  4738. end;
  4739. function is_cppclass(def: tdef): boolean;
  4740. begin
  4741. is_cppclass:=
  4742. assigned(def) and
  4743. (def.deftype=objectdef) and
  4744. (tobjectdef(def).objecttype=odt_cppclass);
  4745. end;
  4746. function is_class_or_interface(def: tdef): boolean;
  4747. begin
  4748. is_class_or_interface:=
  4749. assigned(def) and
  4750. (def.deftype=objectdef) and
  4751. (tobjectdef(def).objecttype in [odt_class,odt_interfacecom,odt_interfacecorba]);
  4752. end;
  4753. {$ifdef x86}
  4754. function use_sse(def : tdef) : boolean;
  4755. begin
  4756. use_sse:=(is_single(def) and (aktfputype in sse_singlescalar)) or
  4757. (is_double(def) and (aktfputype in sse_doublescalar));
  4758. end;
  4759. {$endif x86}
  4760. end.