symdef.pas 175 KB

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