symdef.pas 172 KB

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