symdef.pas 175 KB

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