symdef.pas 173 KB

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