symdef.pas 172 KB

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