symdef.pas 172 KB

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