symdef.pas 169 KB

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