symdef.pas 168 KB

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