symdef.pas 172 KB

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