symdef.pas 175 KB

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