symdef.pas 170 KB

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