symtable.pas 193 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  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 symtable;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,globtype,tokens,
  23. { symtable }
  24. symconst,symbase,symtype,symdef,symsym;
  25. {****************************************************************************
  26. Symtable types
  27. ****************************************************************************}
  28. type
  29. { tstoredsymtable }
  30. tstoredsymtable = class(TSymtable)
  31. private
  32. init_final_check_done : boolean;
  33. deref_built : boolean;
  34. derefimpl_built : boolean;
  35. procedure _needs_init_final(sym:TObject;arg:pointer);
  36. procedure do_init_final_check;
  37. procedure check_forward(sym:TObject;arg:pointer);
  38. procedure check_block_valid(def: TObject;arg:pointer);
  39. procedure register_defs(def:tobject;arg:pointer);
  40. procedure register_syms(sym:tobject;arg:pointer);
  41. procedure labeldefined(sym:TObject;arg:pointer);
  42. procedure varsymbolused(sym:TObject;arg:pointer);
  43. procedure TestPrivate(sym:TObject;arg:pointer);
  44. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  45. procedure loaddefs(ppufile:tcompilerppufile);
  46. procedure loadsyms(ppufile:tcompilerppufile);
  47. procedure writedefs(ppufile:tcompilerppufile);
  48. procedure writesyms(ppufile:tcompilerppufile);
  49. public
  50. constructor create(const s:string);
  51. procedure insertsym(sym:TSymEntry;checkdup:boolean=true);override;
  52. procedure deletesym(sym:TSymEntry);override;
  53. { load/write }
  54. procedure ppuload(ppufile:tcompilerppufile);virtual;
  55. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  56. procedure buildderef;
  57. procedure buildderefimpl;
  58. { buildderef but only for (recursively) used symbols/defs }
  59. procedure buildderef_registered;
  60. procedure deref(only_registered: boolean);virtual;
  61. procedure derefimpl(only_registered: boolean);virtual;
  62. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  63. procedure allsymbolsused;
  64. procedure allprivatesused;
  65. procedure check_forwards;
  66. procedure checklabels;
  67. function needs_init_final : boolean; virtual;
  68. function has_non_trivial_init:boolean;virtual;
  69. function is_derefimpl_built: boolean;
  70. function is_deref_built: boolean;
  71. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  72. procedure register_children;
  73. end;
  74. {$ifdef llvm}
  75. tllvmshadowsymtableentry = class
  76. constructor create(def: tdef; fieldoffset: aint);
  77. private
  78. ffieldoffset: aint;
  79. fdef: tdef;
  80. public
  81. property fieldoffset: aint read ffieldoffset;
  82. property def: tdef read fdef;
  83. end;
  84. tllvmshadowsymtable = class;
  85. {$endif llvm}
  86. tmanagementoperator_offset_entry = record
  87. pd : tprocdef;
  88. offset : asizeint;
  89. end;
  90. pmanagementoperator_offset_entry = ^tmanagementoperator_offset_entry;
  91. tabstractrecordsymtable = class(tstoredsymtable)
  92. {$ifdef llvm}
  93. private
  94. fllvmst: tllvmshadowsymtable;
  95. function getllvmshadowsymtabll: tllvmshadowsymtable;
  96. {$endif llvm}
  97. public
  98. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  99. recordalignment, { alignment desired when inserting this record }
  100. fieldalignment, { alignment current alignment used when fields are inserted }
  101. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  102. recordalignmin: shortint; { local equivalentsof global settings, so that records can be created with custom settings internally }
  103. has_fields_with_mop : tmanagementoperators; { whether any of the fields has the need for a management operator (or one of the field's fields) }
  104. constructor create(const n:string;usealign,recordminalign:shortint);
  105. destructor destroy;override;
  106. procedure ppuload(ppufile:tcompilerppufile);override;
  107. procedure ppuwrite(ppufile:tcompilerppufile);override;
  108. procedure alignrecord(fieldoffset:asizeint;varalign:shortint);
  109. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  110. procedure addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  111. { returns the field closest to this offset (may not be exact because
  112. of padding; internalerrors for variant records, assumes fields are
  113. ordered by increasing offset) }
  114. function findfieldbyoffset(offset:asizeint): tfieldvarsym;
  115. procedure addalignmentpadding;
  116. procedure insertdef(def:TDefEntry);override;
  117. function is_packed: boolean;
  118. function has_single_field(out def:tdef): boolean;
  119. function has_double_field(out def1,def2:tdef; out offset:integer): integer;
  120. { collects all management operators of the specified type in list (which
  121. is not cleared); the entries are copies and thus must be freed by the
  122. caller }
  123. procedure get_managementoperator_offset_list(mop:tmanagementoperator;list:tfplist);
  124. protected
  125. { size in bytes including padding }
  126. _datasize : asizeint;
  127. { size in bits of the data in case of bitpacked record. Only important during construction, }
  128. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  129. databitsize : asizeint;
  130. { size in bytes of padding }
  131. _paddingsize : word;
  132. { array of tmanagementoperator_offset_entry lists; only assigned if
  133. they had been queried once by get_management_operator_list }
  134. mop_list : array[tmanagementoperator] of tfplist;
  135. procedure setdatasize(val: asizeint);
  136. function getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  137. procedure do_get_managementoperator_offset_list(data:tobject;arg:pointer);
  138. public
  139. function iscurrentunit: boolean; override;
  140. property datasize : asizeint read _datasize write setdatasize;
  141. property paddingsize: word read _paddingsize write _paddingsize;
  142. {$ifdef llvm}
  143. property llvmst: tllvmshadowsymtable read getllvmshadowsymtabll;
  144. {$endif llvm}
  145. end;
  146. trecordsymtable = class(tabstractrecordsymtable)
  147. public
  148. { maybe someday is worth to move managementoperators to }
  149. { tabstractrecordsymtable to perform management class operators for }
  150. { object/classes. In XE5 and newer is possible to use class operator }
  151. { for classes (like for Delphi .NET before) only for Delphi NEXTGEN }
  152. managementoperators : tmanagementoperators;
  153. constructor create(const n:string;usealign,recordminalign:shortint);
  154. procedure insertunionst(unionst : trecordsymtable;offset : asizeint);
  155. procedure includemanagementoperator(mop:tmanagementoperator);
  156. end;
  157. tObjectSymtable = class(tabstractrecordsymtable)
  158. public
  159. constructor create(adefowner:tdef;const n:string;usealign,recordminalign:shortint);
  160. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  161. end;
  162. {$ifdef llvm}
  163. { llvm record definitions cannot contain variant/union parts, }
  164. { you have to flatten them first. the tllvmshadowsymtable }
  165. { contains a flattened version of a record/object symtable }
  166. tllvmshadowsymtable = class
  167. private
  168. equivst: tabstractrecordsymtable;
  169. curroffset: aint;
  170. function get(f: tfieldvarsym): tllvmshadowsymtableentry;
  171. function get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  172. public
  173. symdeflist: TFPObjectList;
  174. constructor create(st: tabstractrecordsymtable);
  175. destructor destroy; override;
  176. property entries[index: tfieldvarsym]: tllvmshadowsymtableentry read get; default;
  177. { warning: do not call this with field.llvmfieldnr, as
  178. field.llvmfieldnr will only be initialised when the llvm shadow
  179. symtable is accessed for the first time. Use the default/entries
  180. property instead in this case }
  181. property entries_by_llvm_index[index: longint]: tllvmshadowsymtableentry read get_by_llvm_index;
  182. private
  183. // generate the table
  184. procedure generate;
  185. // helpers
  186. procedure appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  187. procedure preprocess(out tempsymlist, variantstarts: tfplist);
  188. procedure addalignmentpadding(finalsize: aint);
  189. procedure buildmapping(tempsymlist, variantstarts: tfplist);
  190. procedure buildtable(tempsymlist, variantstarts: tfplist);
  191. end;
  192. {$endif llvm}
  193. { tabstractsubsymtable }
  194. tabstractsubsymtable = class(tstoredsymtable)
  195. public
  196. procedure ppuwrite(ppufile:tcompilerppufile);override;
  197. end;
  198. { tabstractlocalsymtable }
  199. tabstractlocalsymtable = class(tabstractsubsymtable)
  200. public
  201. function count_locals:longint;
  202. function iscurrentunit: boolean; override;
  203. end;
  204. tlocalsymtable = class(tabstractlocalsymtable)
  205. public
  206. constructor create(adefowner:tdef;level:byte);
  207. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  208. end;
  209. { tparasymtable }
  210. tparasymtable = class(tabstractlocalsymtable)
  211. public
  212. constructor create(adefowner:tdef;level:byte);
  213. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  214. end;
  215. tabstractuniTSymtable = class(tstoredsymtable)
  216. public
  217. constructor create(const n : string;id:word);
  218. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  219. function findnamespace(const n:string):TSymEntry;virtual;
  220. function iscurrentunit:boolean;override;
  221. function needs_init_final: boolean; override;
  222. procedure insertunit(sym:TSymEntry);
  223. function has_class_condestructors: boolean;
  224. end;
  225. tglobalsymtable = class(tabstractuniTSymtable)
  226. public
  227. unittypecount : word;
  228. constructor create(const n : string;id:word);
  229. procedure ppuload(ppufile:tcompilerppufile);override;
  230. procedure ppuwrite(ppufile:tcompilerppufile);override;
  231. end;
  232. tstaticsymtable = class(tabstractuniTSymtable)
  233. public
  234. constructor create(const n : string;id:word);
  235. procedure ppuload(ppufile:tcompilerppufile);override;
  236. procedure ppuwrite(ppufile:tcompilerppufile);override;
  237. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  238. function findnamespace(const n:string):TSymEntry;override;
  239. end;
  240. tspecializesymtable = class(tglobalsymtable)
  241. public
  242. constructor create(const n : string;id:word);
  243. function iscurrentunit:boolean;override;
  244. end;
  245. twithsymtable = class(TSymtable)
  246. withrefnode : tobject; { tnode }
  247. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  248. destructor destroy;override;
  249. procedure clear;override;
  250. procedure insertdef(def:TDefEntry);override;
  251. end;
  252. tstt_exceptsymtable = class(TSymtable)
  253. public
  254. constructor create;
  255. end;
  256. tmacrosymtable = class(tstoredsymtable)
  257. public
  258. constructor create(exported: boolean);
  259. end;
  260. { tenumsymtable }
  261. tenumsymtable = class(tabstractsubsymtable)
  262. public
  263. procedure insertsym(sym: TSymEntry; checkdup: boolean = true); override;
  264. constructor create(adefowner:tdef);
  265. end;
  266. { tarraysymtable }
  267. tarraysymtable = class(tabstractsubsymtable)
  268. public
  269. procedure insertdef(def:TDefEntry);override;
  270. constructor create(adefowner:tdef);
  271. end;
  272. var
  273. systemunit : tglobalsymtable; { pointer to the system unit }
  274. type
  275. tsymbol_search_flag = (
  276. ssf_search_option,
  277. ssf_search_helper,
  278. ssf_has_inherited,
  279. ssf_no_addsymref,
  280. ssf_unit_or_namespace_only
  281. );
  282. tsymbol_search_flags = set of tsymbol_search_flag;
  283. {****************************************************************************
  284. Functions
  285. ****************************************************************************}
  286. {*** Misc ***}
  287. function FullTypeName(def,otherdef:tdef):string;
  288. function generate_nested_name(symtable:tsymtable;const delimiter:string):string;
  289. { def is the extended type of a helper }
  290. function generate_objectpascal_helper_key(def:tdef):TSymStr;
  291. procedure incompatibletypes(def1,def2:tdef);
  292. procedure hidesym(sym:TSymEntry);
  293. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym:TSymEntry; warn: boolean);
  294. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  295. { writes all declarations for the specified system unit symbol }
  296. procedure write_system_parameter_lists(const name:string);
  297. {*** Search ***}
  298. procedure addsymref(sym:tsym);inline;
  299. procedure addsymref(sym:tsym;def:tdef);
  300. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  301. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  302. function defs_belong_to_same_generic(def1,def2:tdef):boolean;
  303. function get_generic_in_hierarchy_by_name(srsym:tsym;def:tdef):tdef;
  304. function return_specialization_of_generic(nesteddef,genericdef:tdef;out resultdef:tdef):boolean;
  305. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  306. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  307. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  308. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  309. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  310. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  311. { searches for a symbol with the given name that has the given option in
  312. symoptions set }
  313. function searchsym_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;option:tsymoption):boolean;
  314. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  315. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  316. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  317. function searchsym_in_class(classh: tobjectdef; contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  318. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  319. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  320. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  321. { searches symbols inside of a helper's implementation }
  322. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  323. function search_system_type(const s: TIDString): ttypesym;
  324. function try_search_system_type(const s: TIDString): ttypesym;
  325. function try_search_current_module_type(const s: TIDString): ttypesym;
  326. function search_system_proc(const s: TIDString): tprocdef;
  327. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  328. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  329. function search_struct_member_no_helper(pd : tabstractrecorddef;const s : string):tsym;
  330. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  331. function search_enumerator_operator(from_def,to_def:Tdef):Tprocdef;
  332. function search_management_operator(mop:tmanagementoperator;pd:Tdef):Tprocdef;
  333. { searches for the helper definition that's currently active for pd }
  334. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  335. { searches whether the symbol s is available in the currently active }
  336. { helper for pd }
  337. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  338. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  339. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  340. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  341. {and returns it if found. Returns nil otherwise.}
  342. function search_macro(const s : string):tsym;
  343. { Additionally to searching for a macro, also checks whether it's still }
  344. { actually defined (could be disable using "undef") }
  345. function defined_macro(const s : string):boolean;
  346. { Look for a system procedure (no overloads supported) }
  347. { returns a list of helpers in the current module for the def }
  348. function get_objectpascal_helpers(pd : tdef):TFPObjectList;
  349. {*** Object Helpers ***}
  350. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  351. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  352. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  353. {*** Macro Helpers ***}
  354. {If called initially, the following procedures manipulate macros in }
  355. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  356. {Name can be given in any case (it will be converted to upper case).}
  357. procedure def_system_macro(const name : string);
  358. procedure set_system_macro(const name, value : string);
  359. procedure set_system_compvar(const name, value : string);
  360. procedure undef_system_macro(const name : string);
  361. {*** symtable stack ***}
  362. { $ifdef DEBUG
  363. procedure test_symtablestack;
  364. procedure list_symtablestack;
  365. $endif DEBUG}
  366. {$ifdef UNITALIASES}
  367. type
  368. punit_alias = ^tunit_alias;
  369. tunit_alias = object(TNamedIndexItem)
  370. newname : pshortstring;
  371. constructor init(const n:string);
  372. destructor done;virtual;
  373. end;
  374. var
  375. unitaliases : pdictionary;
  376. procedure addunitalias(const n:string);
  377. function getunitalias(const n:string):string;
  378. {$endif UNITALIASES}
  379. {*** Init / Done ***}
  380. procedure IniTSymtable;
  381. procedure DoneSymtable;
  382. const
  383. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] = (
  384. { NOTOKEN } 'error',
  385. { _PLUS } 'plus',
  386. { _MINUS } 'minus',
  387. { _STAR } 'star',
  388. { _SLASH } 'slash',
  389. { _EQ } 'equal',
  390. { _GT } 'greater',
  391. { _LT } 'lower',
  392. { _GTE } 'greater_or_equal',
  393. { _LTE } 'lower_or_equal',
  394. { _NE } 'not_equal',
  395. { _SYMDIF } 'sym_diff',
  396. { _STARSTAR } 'starstar',
  397. { _OP_AS } 'as',
  398. { _OP_IN } 'in',
  399. { _OP_IS } 'is',
  400. { _OP_OR } 'or',
  401. { _OP_AND } 'and',
  402. { _OP_DIV } 'div',
  403. { _OP_MOD } 'mod',
  404. { _OP_NOT } 'not',
  405. { _OP_SHL } 'shl',
  406. { _OP_SHR } 'shr',
  407. { _OP_XOR } 'xor',
  408. { _ASSIGNMENT } 'assign',
  409. { _OP_EXPLICIT } 'explicit',
  410. { _OP_ENUMERATOR } 'enumerator',
  411. { _OP_INITIALIZE } 'initialize',
  412. { _OP_FINALIZE } 'finalize',
  413. { _OP_ADDREF } 'addref',
  414. { _OP_COPY } 'copy',
  415. { _OP_INC } 'inc',
  416. { _OP_DEC } 'dec');
  417. managementoperator2tok:array[tmanagementoperator] of ttoken = (
  418. { mop_none } NOTOKEN,
  419. { mop_initialize } _OP_INITIALIZE,
  420. { mop_finalize } _OP_FINALIZE,
  421. { mop_addref } _OP_ADDREF,
  422. { mop_copy } _OP_COPY
  423. );
  424. implementation
  425. uses
  426. { global }
  427. verbose,globals,systems,
  428. { symtable }
  429. symutil,defutil,defcmp,objcdef,
  430. { module }
  431. fmodule,
  432. { codegen }
  433. procinfo,
  434. { ppu }
  435. entfile,ppu,
  436. { parser }
  437. scanner
  438. ;
  439. var
  440. dupnr : longint; { unique number for duplicate symbols }
  441. {*****************************************************************************
  442. TStoredSymtable
  443. *****************************************************************************}
  444. constructor tstoredsymtable.create(const s:string);
  445. begin
  446. inherited create(s);
  447. { Note: this happens for the initial macro symtable, so no error here }
  448. if not assigned(current_module) then
  449. comment(v_debug,'Current module not available for module id')
  450. else
  451. moduleid:=current_module.moduleid;
  452. end;
  453. procedure tstoredsymtable.insertsym(sym:TSymEntry;checkdup:boolean=true);
  454. begin
  455. inherited insertsym(sym,checkdup);
  456. init_final_check_done:=false;
  457. end;
  458. procedure tstoredsymtable.deletesym(sym:TSymEntry);
  459. begin
  460. inherited deletesym(sym);
  461. init_final_check_done:=false;
  462. end;
  463. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  464. begin
  465. { load the table's flags }
  466. if ppufile.readentry<>ibsymtableoptions then
  467. Message(unit_f_ppu_read_error);
  468. ppufile.getset(tppuset1(tableoptions));
  469. { load definitions }
  470. loaddefs(ppufile);
  471. { load symbols }
  472. loadsyms(ppufile);
  473. init_final_check_done:=true;
  474. end;
  475. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  476. begin
  477. { ensure that we have the sto_needs_init_final flag set if needed }
  478. if not init_final_check_done then
  479. needs_init_final;
  480. { write the table's flags }
  481. ppufile.putset(tppuset1(tableoptions));
  482. ppufile.writeentry(ibsymtableoptions);
  483. { write definitions }
  484. writedefs(ppufile);
  485. { write symbols }
  486. writesyms(ppufile);
  487. end;
  488. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  489. var
  490. def : tdef;
  491. b : byte;
  492. begin
  493. { load start of definition section, which holds the amount of defs }
  494. if ppufile.readentry<>ibstartdefs then
  495. Message(unit_f_ppu_read_error);
  496. { read definitions }
  497. repeat
  498. b:=ppufile.readentry;
  499. def:=nil;
  500. case b of
  501. ibpointerdef : def:=cpointerdef.ppuload(ppufile);
  502. ibarraydef : def:=carraydef.ppuload(ppufile);
  503. iborddef : def:=corddef.ppuload(ppufile);
  504. ibfloatdef : def:=cfloatdef.ppuload(ppufile);
  505. ibprocdef : def:=cprocdef.ppuload(ppufile);
  506. ibshortstringdef : def:=cstringdef.loadshort(ppufile);
  507. iblongstringdef : def:=cstringdef.loadlong(ppufile);
  508. ibansistringdef : def:=cstringdef.loadansi(ppufile);
  509. ibwidestringdef : def:=cstringdef.loadwide(ppufile);
  510. ibunicodestringdef : def:=cstringdef.loadunicode(ppufile);
  511. ibrecorddef : def:=crecorddef.ppuload(ppufile);
  512. ibobjectdef : def:=cobjectdef.ppuload(ppufile);
  513. ibenumdef : def:=cenumdef.ppuload(ppufile);
  514. ibsetdef : def:=csetdef.ppuload(ppufile);
  515. ibprocvardef : def:=cprocvardef.ppuload(ppufile);
  516. ibfiledef : def:=cfiledef.ppuload(ppufile);
  517. ibclassrefdef : def:=cclassrefdef.ppuload(ppufile);
  518. ibformaldef : def:=cformaldef.ppuload(ppufile);
  519. ibvariantdef : def:=cvariantdef.ppuload(ppufile);
  520. ibundefineddef : def:=cundefineddef.ppuload(ppufile);
  521. ibenddefs : break;
  522. ibend : Message(unit_f_ppu_read_error);
  523. else
  524. Message1(unit_f_ppu_invalid_entry,tostr(b));
  525. end;
  526. if assigned(def) then
  527. tstoreddef(def).ppuload_subentries(ppufile);
  528. InsertDef(def);
  529. until false;
  530. end;
  531. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  532. var
  533. b : byte;
  534. sym : tsym;
  535. begin
  536. { load start of definition section, which holds the amount of defs }
  537. if ppufile.readentry<>ibstartsyms then
  538. Message(unit_f_ppu_read_error);
  539. { now read the symbols }
  540. repeat
  541. sym:=nil;
  542. b:=ppufile.readentry;
  543. case b of
  544. ibtypesym : sym:=ctypesym.ppuload(ppufile);
  545. ibprocsym : sym:=cprocsym.ppuload(ppufile);
  546. ibconstsym : sym:=cconstsym.ppuload(ppufile);
  547. ibstaticvarsym : sym:=cstaticvarsym.ppuload(ppufile);
  548. iblocalvarsym : sym:=clocalvarsym.ppuload(ppufile);
  549. ibparavarsym : sym:=cparavarsym.ppuload(ppufile);
  550. ibfieldvarsym : sym:=cfieldvarsym.ppuload(ppufile);
  551. ibabsolutevarsym : sym:=cabsolutevarsym.ppuload(ppufile);
  552. ibenumsym : sym:=cenumsym.ppuload(ppufile);
  553. ibpropertysym : sym:=cpropertysym.ppuload(ppufile);
  554. ibunitsym : sym:=cunitsym.ppuload(ppufile);
  555. iblabelsym : sym:=clabelsym.ppuload(ppufile);
  556. ibsyssym : sym:=csyssym.ppuload(ppufile);
  557. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  558. ibnamespacesym : sym:=cnamespacesym.ppuload(ppufile);
  559. ibendsyms : break;
  560. ibend : Message(unit_f_ppu_read_error);
  561. else
  562. Message1(unit_f_ppu_invalid_entry,tostr(b));
  563. end;
  564. if assigned(sym) then
  565. tstoredsym(sym).ppuload_subentries(ppufile);
  566. InsertSym(sym,false);
  567. until false;
  568. end;
  569. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  570. var
  571. defcount,
  572. i : longint;
  573. def : tstoreddef;
  574. begin
  575. defcount:=0;
  576. for i:=0 to DefList.Count-1 do
  577. if tstoreddef(DefList[i]).is_registered then
  578. inc(defcount);
  579. { each definition get a number, write then the amount of defs to the
  580. ibstartdef entry }
  581. ppufile.putlongint(defcount);
  582. ppufile.writeentry(ibstartdefs);
  583. { now write the definition }
  584. for i:=0 to DefList.Count-1 do
  585. begin
  586. def:=tstoreddef(DefList[i]);
  587. if def.is_registered then
  588. begin
  589. def.ppuwrite(ppufile);
  590. def.ppuwrite_subentries(ppufile);
  591. end;
  592. end;
  593. { write end of definitions }
  594. ppufile.writeentry(ibenddefs);
  595. end;
  596. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  597. var
  598. symcount,
  599. i : longint;
  600. sym : Tstoredsym;
  601. begin
  602. symcount:=0;
  603. for i:=0 to SymList.Count-1 do
  604. if tstoredsym(SymList[i]).is_registered then
  605. inc(symcount);
  606. { each definition get a number, write then the amount of syms and the
  607. datasize to the ibsymdef entry }
  608. ppufile.putlongint(symcount);
  609. ppufile.writeentry(ibstartsyms);
  610. { foreach is used to write all symbols }
  611. for i:=0 to SymList.Count-1 do
  612. begin
  613. sym:=tstoredsym(SymList[i]);
  614. if sym.is_registered then
  615. begin
  616. sym.ppuwrite(ppufile);
  617. sym.ppuwrite_subentries(ppufile);
  618. end;
  619. end;
  620. { end of symbols }
  621. ppufile.writeentry(ibendsyms);
  622. end;
  623. function tstoredsymtable.is_deref_built: boolean;
  624. begin
  625. Result:=deref_built;
  626. end;
  627. function tstoredsymtable.is_derefimpl_built: boolean;
  628. begin
  629. Result:=derefimpl_built;
  630. end;
  631. procedure tstoredsymtable.buildderef;
  632. var
  633. i : longint;
  634. def : tstoreddef;
  635. sym : tstoredsym;
  636. begin
  637. { interface definitions }
  638. for i:=0 to DefList.Count-1 do
  639. begin
  640. def:=tstoreddef(DefList[i]);
  641. def.buildderef;
  642. end;
  643. { interface symbols }
  644. for i:=0 to SymList.Count-1 do
  645. begin
  646. sym:=tstoredsym(SymList[i]);
  647. sym.buildderef;
  648. end;
  649. deref_built:=True;
  650. end;
  651. procedure tstoredsymtable.buildderefimpl;
  652. var
  653. i : longint;
  654. def : tstoreddef;
  655. begin
  656. { implementation definitions }
  657. for i:=0 to DefList.Count-1 do
  658. begin
  659. def:=tstoreddef(DefList[i]);
  660. def.buildderefimpl;
  661. end;
  662. derefimpl_built:=True;
  663. end;
  664. procedure tstoredsymtable.buildderef_registered;
  665. var
  666. def : tstoreddef;
  667. sym : tstoredsym;
  668. i : longint;
  669. defidmax,
  670. symidmax: longint;
  671. newbuiltdefderefs,
  672. builtdefderefs,
  673. builtsymderefs: array of boolean;
  674. changed: boolean;
  675. begin
  676. newbuiltdefderefs:=nil;
  677. builtdefderefs:=nil;
  678. builtsymderefs:=nil;
  679. { tdefs for which we already built the deref }
  680. setlength(builtdefderefs,deflist.count);
  681. { tdefs for which we built the deref in this iteration }
  682. setlength(newbuiltdefderefs,deflist.count);
  683. { syms for which we already built the deref }
  684. setlength(builtsymderefs,symlist.count);
  685. repeat
  686. { we only have to store the defs (recursively) referred by wpo info
  687. or inlined routines in the static symbtable }
  688. { current number of registered defs/syms }
  689. defidmax:=current_module.deflist.count;
  690. symidmax:=current_module.symlist.count;
  691. changed:=false;
  692. { build the derefs for the registered defs we haven't processed yet }
  693. for i:=0 to DefList.Count-1 do
  694. begin
  695. if not builtdefderefs[i] then
  696. begin
  697. def:=tstoreddef(DefList[i]);
  698. if def.is_registered then
  699. begin
  700. def.buildderef;
  701. newbuiltdefderefs[i]:=true;
  702. builtdefderefs[i]:=true;
  703. changed:=true;
  704. end;
  705. end;
  706. end;
  707. { same for the syms }
  708. for i:=0 to SymList.Count-1 do
  709. begin
  710. if not builtsymderefs[i] then
  711. begin
  712. sym:=tstoredsym(SymList[i]);
  713. if sym.is_registered then
  714. begin
  715. sym.buildderef;
  716. builtsymderefs[i]:=true;
  717. changed:=true;
  718. end;
  719. end;
  720. end;
  721. { now buildderefimpl for the defs we processed in this iteration }
  722. for i:=0 to DefList.Count-1 do
  723. begin
  724. if newbuiltdefderefs[i] then
  725. begin
  726. newbuiltdefderefs[i]:=false;
  727. tstoreddef(DefList[i]).buildderefimpl;
  728. changed:=true;
  729. end;
  730. end;
  731. { stop when no new defs or syms have been registered while processing
  732. the currently registered ones (defs/syms get added to the module's
  733. deflist/symlist when they are registered) }
  734. until not changed and
  735. (defidmax=current_module.deflist.count) and
  736. (symidmax=current_module.symlist.count);
  737. end;
  738. procedure tstoredsymtable.deref(only_registered: boolean);
  739. var
  740. i : longint;
  741. def : tstoreddef;
  742. sym : tstoredsym;
  743. begin
  744. { first deref the interface ttype symbols. This is needs
  745. to be done before the interface defs are derefed, because
  746. the interface defs can contain references to the type symbols
  747. which then already need to contain a resolved typedef field (PFV) }
  748. for i:=0 to SymList.Count-1 do
  749. begin
  750. sym:=tstoredsym(SymList[i]);
  751. if (sym.typ=typesym) and
  752. (not only_registered or
  753. sym.is_registered) then
  754. sym.deref;
  755. end;
  756. { interface definitions }
  757. for i:=0 to DefList.Count-1 do
  758. begin
  759. def:=tstoreddef(DefList[i]);
  760. if not only_registered or
  761. def.is_registered then
  762. def.deref;
  763. end;
  764. { interface symbols }
  765. for i:=0 to SymList.Count-1 do
  766. begin
  767. sym:=tstoredsym(SymList[i]);
  768. if (not only_registered or
  769. sym.is_registered) and
  770. (sym.typ<>typesym) then
  771. sym.deref;
  772. end;
  773. end;
  774. procedure tstoredsymtable.derefimpl(only_registered: boolean);
  775. var
  776. i : longint;
  777. def : tstoreddef;
  778. begin
  779. { implementation definitions }
  780. for i:=0 to DefList.Count-1 do
  781. begin
  782. def:=tstoreddef(DefList[i]);
  783. if not only_registered or
  784. def.is_registered then
  785. def.derefimpl;
  786. end;
  787. end;
  788. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  789. var
  790. hsym : tsym;
  791. begin
  792. hsym:=tsym(FindWithHash(hashedid));
  793. if assigned(hsym) then
  794. DuplicateSym(hashedid,sym,hsym,false);
  795. result:=assigned(hsym);
  796. end;
  797. {**************************************
  798. Callbacks
  799. **************************************}
  800. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  801. begin
  802. if tsym(sym).typ=procsym then
  803. tprocsym(sym).check_forward
  804. { check also object method table }
  805. { we needn't to test the def list }
  806. { because each object has to have a type sym,
  807. only test objects declarations, not type renamings }
  808. else
  809. if (tsym(sym).typ=typesym) and
  810. assigned(ttypesym(sym).typedef) and
  811. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  812. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) then
  813. tabstractrecorddef(ttypesym(sym).typedef).check_forwards;
  814. end;
  815. procedure tstoredsymtable.check_block_valid(def: TObject; arg: pointer);
  816. var
  817. founderrordef: tdef;
  818. begin
  819. { all parameters passed to a block must be handled by the Objective-C
  820. runtime }
  821. if is_block(tdef(def)) and
  822. not objcchecktype(tdef(def),founderrordef) then
  823. if assigned(tdef(def).typesym) then
  824. MessagePos1(tdef(def).typesym.fileinfo,type_e_objc_type_unsupported,founderrordef.typename)
  825. else
  826. Message1(type_e_objc_type_unsupported,tprocvardef(def).typename)
  827. end;
  828. procedure tstoredsymtable.register_syms(sym:tobject;arg:pointer);
  829. begin
  830. tsym(sym).register_sym;
  831. end;
  832. procedure tstoredsymtable.register_defs(def:tobject;arg:pointer);
  833. begin
  834. tdef(def).register_def;
  835. end;
  836. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  837. begin
  838. if (tsym(sym).typ=labelsym) and
  839. not(tlabelsym(sym).defined) then
  840. begin
  841. if tlabelsym(sym).used then
  842. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  843. else
  844. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  845. end;
  846. end;
  847. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  848. begin
  849. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  850. ((tsym(sym).owner.symtabletype in
  851. [parasymtable,localsymtable,ObjectSymtable,recordsymtable,staticsymtable])) then
  852. begin
  853. { unused symbol should be reported only if no }
  854. { error is reported }
  855. { if the symbol is in a register it is used }
  856. { also don't count the value parameters which have local copies }
  857. { also don't claim for high param of open parameters (PM) }
  858. { also don't complain about unused symbols in generic procedures }
  859. { and methods }
  860. { and neither in abstract methods }
  861. if (Errorcount<>0) or
  862. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) or
  863. (sp_internal in tsym(sym).symoptions) or
  864. ((assigned(tsym(sym).owner.defowner) and
  865. (tsym(sym).owner.defowner.typ=procdef) and
  866. ((df_generic in tprocdef(tsym(sym).owner.defowner).defoptions) or
  867. (po_abstractmethod in tprocdef(tsym(sym).owner.defowner).procoptions)))) then
  868. exit;
  869. if (tstoredsym(sym).refs=0) then
  870. begin
  871. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  872. begin
  873. { don't warn about the result of constructors }
  874. { or the synthetic helper functions for class-attributes }
  875. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  876. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  877. not (po_noreturn in tprocdef(tsym(sym).owner.defowner).procoptions) and
  878. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  879. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  880. end
  881. else if (tsym(sym).owner.symtabletype=parasymtable) then
  882. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  883. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  884. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  885. else
  886. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  887. end
  888. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  889. begin
  890. if (tsym(sym).owner.symtabletype=parasymtable) then
  891. begin
  892. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out,vs_constref]) and
  893. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  894. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  895. end
  896. else if (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  897. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname)
  898. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  899. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  900. end
  901. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  902. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  903. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  904. end
  905. else if ((tsym(sym).owner.symtabletype in
  906. [ObjectSymtable,parasymtable,localsymtable,staticsymtable,recordsymtable])) then
  907. begin
  908. if (Errorcount<>0) or
  909. (sp_internal in tsym(sym).symoptions) then
  910. exit;
  911. { do not claim for inherited private fields !! }
  912. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype in [ObjectSymtable,recordsymtable]) then
  913. case tsym(sym).typ of
  914. typesym:
  915. MessagePos2(tsym(sym).fileinfo,sym_n_private_type_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  916. constsym:
  917. MessagePos2(tsym(sym).fileinfo,sym_n_private_const_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  918. propertysym:
  919. MessagePos2(tsym(sym).fileinfo,sym_n_private_property_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  920. else
  921. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tabstractrecorddef(tsym(sym).owner.defowner).GetTypeName,tsym(sym).prettyname);
  922. end
  923. { units references are problematic }
  924. else
  925. begin
  926. if (tsym(sym).refs=0) and
  927. not(tsym(sym).typ in [enumsym,unitsym,namespacesym]) and
  928. not(is_funcret_sym(tsym(sym))) and
  929. { don't complain about compiler generated syms for specializations, see also #13405 }
  930. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  931. (pos('$',ttypesym(sym).Realname)<>0)) and
  932. (
  933. (tsym(sym).typ<>procsym) or
  934. ((tsym(sym).owner.symtabletype=staticsymtable) and
  935. not current_module.is_unit)
  936. ) and
  937. { don't complain about alias for hidden _cmd parameter to
  938. obj-c methods }
  939. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  940. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  941. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  942. end;
  943. end;
  944. end;
  945. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  946. begin
  947. if tsym(sym).visibility in [vis_private,vis_strictprivate] then
  948. varsymbolused(sym,arg);
  949. end;
  950. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  951. begin
  952. {
  953. Don't test simple object aliases PM
  954. }
  955. if (tsym(sym).typ=typesym) and
  956. (ttypesym(sym).typedef.typ in [objectdef,recorddef]) and
  957. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  958. tabstractrecorddef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  959. end;
  960. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  961. begin
  962. if (tsym(sym).typ=propertysym) and
  963. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  964. ppointer(arg)^:=sym;
  965. end;
  966. procedure tstoredsymtable.register_children;
  967. begin
  968. SymList.ForEachCall(@register_syms,nil);
  969. DefList.ForEachCall(@register_defs,nil);
  970. end;
  971. {***********************************************
  972. Process all entries
  973. ***********************************************}
  974. { checks, if all procsyms and methods are defined }
  975. procedure tstoredsymtable.check_forwards;
  976. begin
  977. SymList.ForEachCall(@check_forward,nil);
  978. { check whether all block definitions contain valid Objective-C types
  979. (now that all forward definitions have been resolved) }
  980. DefList.ForEachCall(@check_block_valid,nil);
  981. end;
  982. procedure tstoredsymtable.checklabels;
  983. begin
  984. SymList.ForEachCall(@labeldefined,nil);
  985. end;
  986. procedure tstoredsymtable.allsymbolsused;
  987. begin
  988. SymList.ForEachCall(@varsymbolused,nil);
  989. end;
  990. procedure tstoredsymtable.allprivatesused;
  991. begin
  992. SymList.ForEachCall(@objectprivatesymbolused,nil);
  993. end;
  994. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  995. begin
  996. if [sto_needs_init_final,sto_has_non_trivial_init] <= tableoptions then
  997. exit;
  998. { don't check static symbols - they can be present in structures only and
  999. always have a reference to a symbol defined on unit level }
  1000. if sp_static in tsym(sym).symoptions then
  1001. exit;
  1002. case tsym(sym).typ of
  1003. fieldvarsym,
  1004. staticvarsym,
  1005. localvarsym,
  1006. paravarsym :
  1007. begin
  1008. if assigned(tabstractvarsym(sym).vardef) and
  1009. is_managed_type(tabstractvarsym(sym).vardef) then
  1010. include(tableoptions,sto_needs_init_final);
  1011. if is_record((tabstractvarsym(sym).vardef)) and
  1012. (mop_initialize in trecordsymtable(trecorddef(tabstractvarsym(sym).vardef).symtable).managementoperators) then
  1013. include(tableoptions,sto_has_non_trivial_init);
  1014. end;
  1015. else
  1016. ;
  1017. end;
  1018. end;
  1019. procedure tstoredsymtable.do_init_final_check;
  1020. begin
  1021. if not init_final_check_done then
  1022. begin
  1023. exclude(tableoptions,sto_needs_init_final);
  1024. exclude(tableoptions,sto_has_non_trivial_init);
  1025. SymList.ForEachCall(@_needs_init_final,nil);
  1026. init_final_check_done:=true;
  1027. end;
  1028. end;
  1029. { returns true, if p contains data which needs init/final code }
  1030. function tstoredsymtable.needs_init_final : boolean;
  1031. begin
  1032. do_init_final_check;
  1033. result:=sto_needs_init_final in tableoptions;
  1034. end;
  1035. function tstoredsymtable.has_non_trivial_init:boolean;
  1036. begin
  1037. do_init_final_check;
  1038. result:=sto_has_non_trivial_init in tableoptions;
  1039. end;
  1040. {****************************************************************************
  1041. TAbstractRecordSymtable
  1042. ****************************************************************************}
  1043. {$ifdef llvm}
  1044. function tabstractrecordsymtable.getllvmshadowsymtabll: tllvmshadowsymtable;
  1045. begin
  1046. if not assigned(fllvmst) then
  1047. fllvmst:=tllvmshadowsymtable.create(self);
  1048. result:=fllvmst;
  1049. end;
  1050. {$endif llvm}
  1051. constructor tabstractrecordsymtable.create(const n:string;usealign,recordminalign:shortint);
  1052. begin
  1053. inherited create(n);
  1054. _datasize:=0;
  1055. databitsize:=0;
  1056. recordalignment:=1;
  1057. usefieldalignment:=usealign;
  1058. recordalignmin:=recordminalign;
  1059. padalignment:=1;
  1060. { recordalign C_alignment means C record packing, that starts
  1061. with an alignment of 1 }
  1062. case usealign of
  1063. C_alignment,
  1064. bit_alignment:
  1065. fieldalignment:=1;
  1066. mac68k_alignment:
  1067. fieldalignment:=2;
  1068. else
  1069. fieldalignment:=usealign;
  1070. end;
  1071. end;
  1072. destructor tabstractrecordsymtable.destroy;
  1073. var
  1074. mop : tmanagementoperator;
  1075. begin
  1076. if refcount>1 then
  1077. exit;
  1078. {$ifdef llvm}
  1079. fllvmst.free;
  1080. {$endif llvm}
  1081. for mop:=low(tmanagementoperator) to high(tmanagementoperator) do
  1082. TFPList.FreeAndNilDisposing(mop_list[mop],TypeInfo(tmanagementoperator_offset_entry));
  1083. inherited destroy;
  1084. end;
  1085. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  1086. begin
  1087. if ppufile.readentry<>ibrecsymtableoptions then
  1088. Message(unit_f_ppu_read_error);
  1089. recordalignment:=shortint(ppufile.getbyte);
  1090. usefieldalignment:=shortint(ppufile.getbyte);
  1091. recordalignmin:=shortint(ppufile.getbyte);
  1092. if (usefieldalignment=C_alignment) then
  1093. fieldalignment:=shortint(ppufile.getbyte);
  1094. ppufile.getset(tppuset1(has_fields_with_mop));
  1095. inherited ppuload(ppufile);
  1096. end;
  1097. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  1098. var
  1099. oldtyp : byte;
  1100. begin
  1101. oldtyp:=ppufile.entrytyp;
  1102. ppufile.entrytyp:=subentryid;
  1103. { in case of classes using C alignment, the alignment of the parent
  1104. affects the alignment of fields of the childs }
  1105. ppufile.putbyte(byte(recordalignment));
  1106. ppufile.putbyte(byte(usefieldalignment));
  1107. ppufile.putbyte(byte(recordalignmin));
  1108. if (usefieldalignment=C_alignment) then
  1109. ppufile.putbyte(byte(fieldalignment));
  1110. { it's not really a "symtableoption", but loading this from the record
  1111. def requires storing the set in the recorddef at least between
  1112. ppuload and deref/derefimpl }
  1113. ppufile.putset(tppuset1(has_fields_with_mop));
  1114. ppufile.writeentry(ibrecsymtableoptions);
  1115. inherited ppuwrite(ppufile);
  1116. ppufile.entrytyp:=oldtyp;
  1117. end;
  1118. function field2recordalignment(fieldoffs, fieldalign: asizeint): asizeint;
  1119. begin
  1120. { optimal alignment of the record when declaring a variable of this }
  1121. { type is independent of the packrecords setting }
  1122. if (fieldoffs mod fieldalign) = 0 then
  1123. result:=fieldalign
  1124. else if (fieldalign >= 16) and
  1125. ((fieldoffs mod 16) = 0) and
  1126. ((fieldalign mod 16) = 0) then
  1127. result:=16
  1128. else if (fieldalign >= 8) and
  1129. ((fieldoffs mod 8) = 0) and
  1130. ((fieldalign mod 8) = 0) then
  1131. result:=8
  1132. else if (fieldalign >= 4) and
  1133. ((fieldoffs mod 4) = 0) and
  1134. ((fieldalign mod 4) = 0) then
  1135. result:=4
  1136. else if (fieldalign >= 2) and
  1137. ((fieldoffs mod 2) = 0) and
  1138. ((fieldalign mod 2) = 0) then
  1139. result:=2
  1140. else
  1141. result:=1;
  1142. end;
  1143. procedure tabstractrecordsymtable.alignrecord(fieldoffset:asizeint;varalign:shortint);
  1144. var
  1145. varalignrecord: shortint;
  1146. begin
  1147. case usefieldalignment of
  1148. C_alignment:
  1149. varalignrecord:=used_align(varalign,recordalignmin,current_settings.alignment.maxCrecordalign);
  1150. mac68k_alignment:
  1151. varalignrecord:=2;
  1152. else
  1153. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  1154. end;
  1155. recordalignment:=max(recordalignment,varalignrecord);
  1156. end;
  1157. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  1158. var
  1159. l : asizeint;
  1160. varalign : shortint;
  1161. vardef : tdef;
  1162. begin
  1163. if (sym.owner<>self) then
  1164. internalerror(200602031);
  1165. if sym.fieldoffset<>-1 then
  1166. internalerror(200602032);
  1167. { set visibility for the symbol }
  1168. sym.visibility:=vis;
  1169. { this symbol can't be loaded to a register }
  1170. sym.varregable:=vr_none;
  1171. { management operators }
  1172. if sym.vardef.typ in [recorddef,objectdef] then
  1173. has_fields_with_mop:=has_fields_with_mop + tabstractrecordsymtable(tabstractrecorddef(sym.vardef).symtable).has_fields_with_mop;
  1174. if sym.vardef.typ=recorddef then
  1175. has_fields_with_mop:=has_fields_with_mop + trecordsymtable(trecorddef(sym.vardef).symtable).managementoperators;
  1176. { Calculate field offset }
  1177. l:=sym.getsize;
  1178. vardef:=sym.vardef;
  1179. varalign:=vardef.structalignment;
  1180. case usefieldalignment of
  1181. bit_alignment:
  1182. begin
  1183. { bitpacking only happens for ordinals, the rest is aligned at }
  1184. { 1 byte (compatible with GPC/GCC) }
  1185. if is_ordinal(vardef) then
  1186. begin
  1187. sym.fieldoffset:=databitsize;
  1188. l:=sym.getpackedbitsize;
  1189. end
  1190. else
  1191. begin
  1192. databitsize:=_datasize*8;
  1193. sym.fieldoffset:=databitsize;
  1194. if (l>high(asizeint) div 8) then
  1195. Message(sym_e_segment_too_large);
  1196. l:=l*8;
  1197. end;
  1198. if varalign=0 then
  1199. varalign:=size_2_align(l);
  1200. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  1201. { bit packed records are limited to high(aint) bits }
  1202. { instead of bytes to avoid double precision }
  1203. { arithmetic in offset calculations }
  1204. if int64(l)>high(asizeint)-sym.fieldoffset then
  1205. begin
  1206. Message(sym_e_segment_too_large);
  1207. _datasize:=high(asizeint);
  1208. databitsize:=high(asizeint);
  1209. end
  1210. else
  1211. begin
  1212. databitsize:=sym.fieldoffset+l;
  1213. _datasize:=(databitsize+7) div 8;
  1214. end;
  1215. { rest is not applicable }
  1216. exit;
  1217. end;
  1218. else
  1219. begin
  1220. sym.fieldoffset:=getfieldoffset(sym,_datasize,fieldalignment);
  1221. if l>high(asizeint)-sym.fieldoffset then
  1222. begin
  1223. Message(sym_e_segment_too_large);
  1224. _datasize:=high(asizeint);
  1225. end
  1226. else
  1227. _datasize:=sym.fieldoffset+l;
  1228. { Calc alignment needed for this record }
  1229. alignrecord(sym.fieldoffset,varalign);
  1230. end;
  1231. end;
  1232. end;
  1233. function field_alignment_compare(item1, item2: pointer): integer;
  1234. var
  1235. field1: tfieldvarsym absolute item1;
  1236. field2: tfieldvarsym absolute item2;
  1237. begin
  1238. { we don't care about static fields, those become global variables }
  1239. if (sp_static in field1.symoptions) or
  1240. (sp_static in field2.symoptions) then
  1241. exit(0);
  1242. { sort from large to small alignment, and in case of the same alignment
  1243. in declaration order (items declared close together are possibly
  1244. also related and hence possibly used together -> putting them next
  1245. to each other can improve cache behaviour) }
  1246. result:=field2.vardef.alignment-field1.vardef.alignment;
  1247. if result=0 then
  1248. result:=field1.fieldoffset-field2.fieldoffset;
  1249. end;
  1250. procedure tabstractrecordsymtable.addfieldlist(list: tfpobjectlist; maybereorder: boolean);
  1251. var
  1252. fieldvs, insertfieldvs: tfieldvarsym;
  1253. base, fieldoffset, space, insertfieldsize, insertfieldoffset, bestinsertfieldoffset, bestspaceleft: asizeint;
  1254. i, j, bestfieldindex: longint;
  1255. globalfieldalignment,
  1256. prevglobalfieldalignment,
  1257. newfieldalignment: shortint;
  1258. changed: boolean;
  1259. begin
  1260. if maybereorder and
  1261. (cs_opt_reorder_fields in current_settings.optimizerswitches) and
  1262. (list.count>1) then
  1263. begin
  1264. { assign dummy field offsets so we can know their order in the
  1265. sorting routine }
  1266. for i:=0 to list.count-1 do
  1267. begin
  1268. fieldvs:=tfieldvarsym(list[i]);
  1269. if sp_static in fieldvs.symoptions then
  1270. continue;
  1271. fieldvs.fieldoffset:=i;
  1272. end;
  1273. { sort the non-class fields to minimise losses due to alignment }
  1274. list.sort(@field_alignment_compare);
  1275. { now fill up gaps caused by alignment skips with smaller fields
  1276. where possible }
  1277. repeat
  1278. i:=0;
  1279. base:=_datasize;
  1280. globalfieldalignment:=fieldalignment;
  1281. changed:=false;
  1282. while i<list.count do
  1283. begin
  1284. fieldvs:=tfieldvarsym(list[i]);
  1285. if sp_static in fieldvs.symoptions then
  1286. begin
  1287. inc(i);
  1288. continue;
  1289. end;
  1290. prevglobalfieldalignment:=globalfieldalignment;
  1291. fieldoffset:=getfieldoffset(fieldvs,base,globalfieldalignment);
  1292. newfieldalignment:=globalfieldalignment;
  1293. { size of the gap between the end of the previous field and
  1294. the start of the current one }
  1295. space:=fieldoffset-base;
  1296. bestspaceleft:=space;
  1297. while space>0 do
  1298. begin
  1299. bestfieldindex:=-1;
  1300. bestinsertfieldoffset:=-1;
  1301. for j:=i+1 to list.count-1 do
  1302. begin
  1303. insertfieldvs:=tfieldvarsym(list[j]);
  1304. if sp_static in insertfieldvs.symoptions then
  1305. continue;
  1306. insertfieldsize:=insertfieldvs.getsize;
  1307. { can the new field fit possibly in the gap? }
  1308. if insertfieldsize<=space then
  1309. begin
  1310. { restore globalfieldalignment to situation before
  1311. the original field was inserted }
  1312. globalfieldalignment:=prevglobalfieldalignment;
  1313. { at what offset would it be inserted? (this new
  1314. field has its own alignment requirements, which
  1315. may make it impossible to fit after all) }
  1316. insertfieldoffset:=getfieldoffset(insertfieldvs,base,globalfieldalignment);
  1317. globalfieldalignment:=prevglobalfieldalignment;
  1318. { taking into account the alignment, does it still
  1319. fit and if so, does it fit better than the
  1320. previously found best fit? }
  1321. if (insertfieldoffset+insertfieldsize<=fieldoffset) and
  1322. (fieldoffset-insertfieldoffset-insertfieldsize<bestspaceleft) then
  1323. begin
  1324. { new best fit }
  1325. bestfieldindex:=j;
  1326. bestinsertfieldoffset:=insertfieldoffset;
  1327. bestspaceleft:=fieldoffset-insertfieldoffset-insertfieldsize;
  1328. if bestspaceleft=0 then
  1329. break;
  1330. end;
  1331. end;
  1332. end;
  1333. { if we didn't find any field to fit, stop trying for this
  1334. gap }
  1335. if bestfieldindex=-1 then
  1336. break;
  1337. changed:=true;
  1338. { we found a field to insert -> adjust the new base
  1339. address }
  1340. base:=bestinsertfieldoffset+tfieldvarsym(list[bestfieldindex]).getsize;
  1341. { update globalfieldalignment for this newly inserted
  1342. field }
  1343. getfieldoffset(tfieldvarsym(list[bestfieldindex]),base,globalfieldalignment);
  1344. { move the new field before the current one }
  1345. list.move(bestfieldindex,i);
  1346. { and skip the new field (which is now at position i) }
  1347. inc(i);
  1348. { there may be more space left -> continue }
  1349. space:=bestspaceleft;
  1350. end;
  1351. if base>fieldoffset then
  1352. internalerror(2012071302);
  1353. { check the next field }
  1354. base:=fieldoffset+fieldvs.getsize;
  1355. { since the original field had the same or greater alignment
  1356. than anything we inserted before it, the global field
  1357. alignment is still the same now as it was originally after
  1358. inserting that field }
  1359. globalfieldalignment:=newfieldalignment;
  1360. inc(i);
  1361. end;
  1362. { there may be small gaps left *before* inserted fields }
  1363. until not changed;
  1364. end;
  1365. { reset the dummy field offsets }
  1366. for i:=0 to list.count-1 do
  1367. begin
  1368. fieldvs:=tfieldvarsym(list[i]);
  1369. if sp_static in fieldvs.symoptions then
  1370. continue;
  1371. fieldvs.fieldoffset:=-1;
  1372. end;
  1373. { finally, set the actual field offsets }
  1374. for i:=0 to list.count-1 do
  1375. begin
  1376. fieldvs:=tfieldvarsym(list[i]);
  1377. { static data fields are already inserted in the globalsymtable }
  1378. if not(sp_static in fieldvs.symoptions) then
  1379. begin
  1380. { read_record_fields already set the visibility of the fields,
  1381. because a single list can contain symbols with different
  1382. visibility }
  1383. addfield(fieldvs,fieldvs.visibility);
  1384. end;
  1385. end;
  1386. end;
  1387. function tabstractrecordsymtable.findfieldbyoffset(offset: asizeint): tfieldvarsym;
  1388. var
  1389. i: longint;
  1390. sym: tsym;
  1391. begin
  1392. { there could be multiple fields in case of a variant record }
  1393. if (defowner.typ=recorddef) and
  1394. trecorddef(defowner).isunion then
  1395. internalerror(2014090403);
  1396. for i:=0 to SymList.count-1 do
  1397. begin
  1398. sym:=tsym(symlist[i]);
  1399. if is_normal_fieldvarsym(sym) and
  1400. (tfieldvarsym(sym).fieldoffset>=offset) then
  1401. begin
  1402. result:=tfieldvarsym(sym);
  1403. exit;
  1404. end;
  1405. end;
  1406. result:=nil;
  1407. end;
  1408. procedure tabstractrecordsymtable.addalignmentpadding;
  1409. var
  1410. padded_datasize: asizeint;
  1411. begin
  1412. { make the record size aligned correctly so it can be
  1413. used as elements in an array. For C records we
  1414. use the fieldalignment, because that is updated with the
  1415. used alignment. }
  1416. if (padalignment = 1) then
  1417. case usefieldalignment of
  1418. C_alignment:
  1419. padalignment:=fieldalignment;
  1420. { bitpacked }
  1421. bit_alignment:
  1422. padalignment:=1;
  1423. { mac68k: always round to multiple of 2 }
  1424. mac68k_alignment:
  1425. padalignment:=2;
  1426. { default/no packrecords specified }
  1427. 0:
  1428. padalignment:=recordalignment
  1429. { specific packrecords setting -> use as upper limit }
  1430. else
  1431. padalignment:=min(recordalignment,usefieldalignment);
  1432. end;
  1433. padded_datasize:=align(_datasize,padalignment);
  1434. _paddingsize:=padded_datasize-_datasize;
  1435. _datasize:=padded_datasize;
  1436. end;
  1437. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  1438. begin
  1439. { Enums must also be available outside the record scope,
  1440. insert in the owner of this symtable }
  1441. if def.typ=enumdef then
  1442. defowner.owner.insertdef(def)
  1443. else
  1444. inherited insertdef(def);
  1445. end;
  1446. function tabstractrecordsymtable.is_packed: boolean;
  1447. begin
  1448. result:=usefieldalignment=bit_alignment;
  1449. end;
  1450. function tabstractrecordsymtable.has_double_field(out def1,def2:tdef; out offset:integer): integer;
  1451. var
  1452. i,cnt: longint;
  1453. currentsymlist: TFPHashObjectList;
  1454. currentdef: tdef;
  1455. sym: tfieldvarsym;
  1456. begin
  1457. has_double_field := 0;
  1458. offset := 0;
  1459. if (defowner.typ=recorddef) and
  1460. trecorddef(defowner).isunion then
  1461. exit;
  1462. currentsymlist:=symlist;
  1463. if currentsymlist = nil then
  1464. exit;
  1465. if currentsymlist.Count <> 2 then
  1466. exit;
  1467. currentdef := nil;
  1468. if is_normal_fieldvarsym(tsym(currentsymlist[0])) then
  1469. begin
  1470. sym:=tfieldvarsym(currentsymlist[0]);
  1471. def1 := sym.vardef;
  1472. end
  1473. else
  1474. exit;
  1475. if is_normal_fieldvarsym(tsym(currentsymlist[1])) then
  1476. begin
  1477. sym:=tfieldvarsym(currentsymlist[1]);
  1478. def2 := sym.vardef;
  1479. end
  1480. else
  1481. exit;
  1482. offset := sym.fieldoffset;
  1483. if(def2.typ = def1.typ)then
  1484. cnt := 2
  1485. else
  1486. cnt := 1;
  1487. if((offset = 0))then
  1488. cnt := 0;
  1489. has_double_field := cnt;
  1490. end;
  1491. function tabstractrecordsymtable.has_single_field(out def:tdef): boolean;
  1492. var
  1493. i: longint;
  1494. currentsymlist: TFPHashObjectList;
  1495. currentdef: tdef;
  1496. sym: tfieldvarsym;
  1497. begin
  1498. result:=false;
  1499. def:=generrordef;
  1500. { If a record contains a union, it does not contain a "single
  1501. non-composite field" in the context of certain ABIs requiring
  1502. special treatment for such records }
  1503. if (defowner.typ=recorddef) and
  1504. trecorddef(defowner).isunion then
  1505. exit;
  1506. { a record/object can contain other things than fields }
  1507. currentsymlist:=symlist;
  1508. { recurse in arrays and records }
  1509. repeat
  1510. sym:=nil;
  1511. { record has one field? }
  1512. for i:=0 to currentsymlist.Count-1 do
  1513. begin
  1514. if is_normal_fieldvarsym(tsym(currentsymlist[i])) then
  1515. begin
  1516. if result then
  1517. begin
  1518. result:=false;
  1519. exit;
  1520. end;
  1521. result:=true;
  1522. sym:=tfieldvarsym(currentsymlist[i]);
  1523. end;
  1524. end;
  1525. if assigned(sym) then
  1526. begin
  1527. { if the field is an array, does it contain one element? }
  1528. currentdef:=sym.vardef;
  1529. while (currentdef.typ=arraydef) and
  1530. not is_special_array(currentdef) do
  1531. begin
  1532. if tarraydef(currentdef).elecount<>1 then
  1533. begin
  1534. result:=false;
  1535. exit;
  1536. end;
  1537. currentdef:=tarraydef(currentdef).elementdef;
  1538. end;
  1539. { if the array element is again a record, continue descending }
  1540. if currentdef.typ=recorddef then
  1541. begin
  1542. { the record might be empty, so reset the result until we've
  1543. really found something }
  1544. result:=false;
  1545. currentsymlist:=trecorddef(currentdef).symtable.SymList
  1546. end
  1547. else
  1548. begin
  1549. { otherwise we found the type of the single element }
  1550. def:=currentdef;
  1551. exit;
  1552. end;
  1553. end
  1554. else
  1555. exit
  1556. until false;
  1557. end;
  1558. procedure tabstractrecordsymtable.do_get_managementoperator_offset_list(data:tobject;arg:pointer);
  1559. var
  1560. sym : tsym absolute data;
  1561. fsym : tfieldvarsym absolute data;
  1562. mop : tmanagementoperator;
  1563. entry : pmanagementoperator_offset_entry;
  1564. sublist : tfplist;
  1565. i : longint;
  1566. begin
  1567. if not is_normal_fieldvarsym(sym) then
  1568. exit;
  1569. if not is_record(fsym.vardef) and not is_object(fsym.vardef) and not is_cppclass(fsym.vardef) then
  1570. exit;
  1571. mop:=tmanagementoperator(ptruint(arg));
  1572. if not assigned(mop_list[mop]) then
  1573. internalerror(2018082303);
  1574. if is_record(fsym.vardef) then
  1575. begin
  1576. if mop in trecordsymtable(trecorddef(fsym.vardef).symtable).managementoperators then
  1577. begin
  1578. new(entry);
  1579. entry^.pd:=search_management_operator(mop,fsym.vardef);
  1580. if not assigned(entry^.pd) then
  1581. internalerror(2018082302);
  1582. entry^.offset:=fsym.fieldoffset;
  1583. mop_list[mop].add(entry);
  1584. end;
  1585. end;
  1586. sublist:=tfplist.create;
  1587. tabstractrecordsymtable(tabstractrecorddef(fsym.vardef).symtable).get_managementoperator_offset_list(mop,sublist);
  1588. mop_list[mop].capacity:=mop_list[mop].count+sublist.count;
  1589. for i:=0 to sublist.count-1 do
  1590. begin
  1591. entry:=pmanagementoperator_offset_entry(sublist[i]);
  1592. entry^.offset:=entry^.offset+fsym.fieldoffset;
  1593. mop_list[mop].add(entry);
  1594. end;
  1595. { we don't need to remove the entries as they become part of list }
  1596. sublist.free;
  1597. end;
  1598. procedure tabstractrecordsymtable.get_managementoperator_offset_list(mop:tmanagementoperator;list:tfplist);
  1599. var
  1600. i : longint;
  1601. entry,entrycopy : pmanagementoperator_offset_entry;
  1602. begin
  1603. if not assigned(list) then
  1604. internalerror(2018082301);
  1605. if mop=mop_none then
  1606. exit;
  1607. if not (mop in has_fields_with_mop) then
  1608. { none of the fields or one of the field's fields has the requested operator }
  1609. exit;
  1610. if not assigned(mop_list[mop]) then
  1611. begin
  1612. mop_list[mop]:=tfplist.create;
  1613. SymList.ForEachCall(@do_get_managementoperator_offset_list,pointer(ptruint(mop)));
  1614. end;
  1615. for i:=0 to mop_list[mop].count-1 do
  1616. begin
  1617. entry:=pmanagementoperator_offset_entry(mop_list[mop][i]);
  1618. New(entrycopy);
  1619. entrycopy^:=entry^;
  1620. list.add(entrycopy);
  1621. end;
  1622. end;
  1623. procedure tabstractrecordsymtable.setdatasize(val: asizeint);
  1624. begin
  1625. _datasize:=val;
  1626. if (usefieldalignment=bit_alignment) then
  1627. { can overflow in non bitpacked records }
  1628. databitsize:=val*8;
  1629. end;
  1630. function tabstractrecordsymtable.getfieldoffset(sym: tfieldvarsym; base: asizeint; var globalfieldalignment: shortint): asizeint;
  1631. var
  1632. l : asizeint;
  1633. varalignfield,
  1634. varalign : shortint;
  1635. vardef : tdef;
  1636. begin
  1637. { Calculate field offset }
  1638. l:=sym.getsize;
  1639. vardef:=sym.vardef;
  1640. varalign:=vardef.structalignment;
  1641. case usefieldalignment of
  1642. bit_alignment:
  1643. { has to be handled separately }
  1644. internalerror(2012071301);
  1645. C_alignment:
  1646. begin
  1647. { Calc the alignment size for C style records }
  1648. if (varalign>4) and
  1649. ((varalign mod 4)<>0) and
  1650. (vardef.typ=arraydef) then
  1651. Message1(sym_w_wrong_C_pack,vardef.typename);
  1652. if varalign=0 then
  1653. varalign:=l;
  1654. if (globalfieldalignment<current_settings.alignment.maxCrecordalign) then
  1655. begin
  1656. if (varalign>16) and (globalfieldalignment<32) then
  1657. globalfieldalignment:=32
  1658. else if (varalign>12) and (globalfieldalignment<16) then
  1659. globalfieldalignment:=16
  1660. { 12 is needed for long double }
  1661. else if (varalign>8) and (globalfieldalignment<12) then
  1662. globalfieldalignment:=12
  1663. else if (varalign>4) and (globalfieldalignment<8) then
  1664. globalfieldalignment:=8
  1665. else if (varalign>2) and (globalfieldalignment<4) then
  1666. globalfieldalignment:=4
  1667. else if (varalign>1) and (globalfieldalignment<2) then
  1668. globalfieldalignment:=2;
  1669. end;
  1670. globalfieldalignment:=min(globalfieldalignment,current_settings.alignment.maxCrecordalign);
  1671. end;
  1672. mac68k_alignment:
  1673. begin
  1674. { mac68k alignment (C description):
  1675. * char is aligned to 1 byte
  1676. * everything else (except vector) is aligned to 2 bytes
  1677. * vector is aligned to 16 bytes
  1678. }
  1679. if l>1 then
  1680. globalfieldalignment:=2
  1681. else
  1682. globalfieldalignment:=1;
  1683. varalign:=2;
  1684. end;
  1685. end;
  1686. if varalign=0 then
  1687. varalign:=size_2_align(l);
  1688. varalignfield:=used_align(varalign,recordalignmin,globalfieldalignment);
  1689. result:=align(base,varalignfield);
  1690. end;
  1691. function tabstractrecordsymtable.iscurrentunit: boolean;
  1692. begin
  1693. Result:=assigned(current_module)and(current_module.moduleid=moduleid);
  1694. end;
  1695. {****************************************************************************
  1696. TRecordSymtable
  1697. ****************************************************************************}
  1698. constructor trecordsymtable.create(const n:string;usealign,recordminalign:shortint);
  1699. begin
  1700. inherited create(n,usealign,recordminalign);
  1701. symtabletype:=recordsymtable;
  1702. end;
  1703. { this procedure is reserved for inserting case variant into
  1704. a record symtable }
  1705. { the offset is the location of the start of the variant
  1706. and datasize and dataalignment corresponds to
  1707. the complete size (see code in pdecl unit) PM }
  1708. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : asizeint);
  1709. var
  1710. sym : tsym;
  1711. def : tdef;
  1712. i : integer;
  1713. varalignrecord,varalign,
  1714. storesize,storealign : asizeint;
  1715. bitsize: tcgint;
  1716. begin
  1717. storesize:=_datasize;
  1718. storealign:=fieldalignment;
  1719. _datasize:=offset;
  1720. if (usefieldalignment=bit_alignment) then
  1721. databitsize:=offset*8;
  1722. { We move the ownership of the defs and symbols to the new recordsymtable.
  1723. The old unionsymtable keeps the references, but doesn't own the
  1724. objects anymore }
  1725. unionst.DefList.OwnsObjects:=false;
  1726. unionst.SymList.OwnsObjects:=false;
  1727. { copy symbols }
  1728. for i:=0 to unionst.SymList.Count-1 do
  1729. begin
  1730. sym:=TSym(unionst.SymList[i]);
  1731. if not is_normal_fieldvarsym(sym) then
  1732. internalerror(200601272);
  1733. if tfieldvarsym(sym).fieldoffset=0 then
  1734. include(tfieldvarsym(sym).varoptions,vo_is_first_field);
  1735. { add to this record symtable, checking for duplicate names }
  1736. // unionst.SymList.List.List^[i].Data:=nil;
  1737. insertsym(sym);
  1738. varalign:=tfieldvarsym(sym).vardef.alignment;
  1739. if varalign=0 then
  1740. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  1741. { retrieve size }
  1742. if (usefieldalignment=bit_alignment) then
  1743. begin
  1744. { bit packed records are limited to high(aint) bits }
  1745. { instead of bytes to avoid double precision }
  1746. { arithmetic in offset calculations }
  1747. if is_ordinal(tfieldvarsym(sym).vardef) then
  1748. bitsize:=tfieldvarsym(sym).getpackedbitsize
  1749. else
  1750. begin
  1751. bitsize:=tfieldvarsym(sym).getsize;
  1752. if (bitsize>high(asizeint) div 8) then
  1753. Message(sym_e_segment_too_large);
  1754. bitsize:=bitsize*8;
  1755. end;
  1756. if bitsize>high(asizeint)-databitsize then
  1757. begin
  1758. Message(sym_e_segment_too_large);
  1759. _datasize:=high(asizeint);
  1760. databitsize:=high(asizeint);
  1761. end
  1762. else
  1763. begin
  1764. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  1765. _datasize:=(databitsize+7) div 8;
  1766. end;
  1767. tfieldvarsym(sym).fieldoffset:=databitsize;
  1768. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  1769. end
  1770. else
  1771. begin
  1772. if tfieldvarsym(sym).getsize>high(asizeint)-_datasize then
  1773. begin
  1774. Message(sym_e_segment_too_large);
  1775. _datasize:=high(asizeint);
  1776. end
  1777. else
  1778. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  1779. { update address }
  1780. tfieldvarsym(sym).fieldoffset:=_datasize;
  1781. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  1782. end;
  1783. { update alignment of this record }
  1784. if (usefieldalignment<>C_alignment) and
  1785. (usefieldalignment<>mac68k_alignment) then
  1786. recordalignment:=max(recordalignment,varalignrecord);
  1787. end;
  1788. { update alignment for C records }
  1789. if (usefieldalignment=C_alignment) and
  1790. (usefieldalignment<>mac68k_alignment) then
  1791. recordalignment:=max(recordalignment,unionst.recordalignment);
  1792. { Register defs in the new record symtable }
  1793. for i:=0 to unionst.DefList.Count-1 do
  1794. begin
  1795. def:=TDef(unionst.DefList[i]);
  1796. def.ChangeOwner(self);
  1797. end;
  1798. _datasize:=storesize;
  1799. fieldalignment:=storealign;
  1800. { If a record contains a union, it does not contain a "single
  1801. non-composite field" in the context of certain ABIs requiring
  1802. special treatment for such records }
  1803. if defowner.typ=recorddef then
  1804. trecorddef(defowner).isunion:=true;
  1805. end;
  1806. procedure trecordsymtable.includemanagementoperator(mop:tmanagementoperator);
  1807. begin
  1808. if mop in managementoperators then
  1809. exit;
  1810. include(managementoperators,mop);
  1811. end;
  1812. {****************************************************************************
  1813. TObjectSymtable
  1814. ****************************************************************************}
  1815. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign,recordminalign:shortint);
  1816. begin
  1817. inherited create(n,usealign,recordminalign);
  1818. symtabletype:=ObjectSymtable;
  1819. defowner:=adefowner;
  1820. end;
  1821. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1822. var
  1823. hsym: tsym;
  1824. warn: boolean;
  1825. begin
  1826. result:=false;
  1827. if not assigned(defowner) then
  1828. internalerror(200602061);
  1829. { procsym and propertysym have special code
  1830. to override values in inherited classes. For other
  1831. symbols check for duplicates (but for internal symbols only in this
  1832. symtable, not the whole hierarchy) }
  1833. if not(sym.typ in [procsym,propertysym]) and
  1834. not (sp_internal in tsym(sym).symoptions) then
  1835. begin
  1836. { but private ids can be reused }
  1837. hsym:=search_struct_member(tobjectdef(defowner),hashedid.id);
  1838. if assigned(hsym) and
  1839. (
  1840. (
  1841. not(m_delphi in current_settings.modeswitches) and
  1842. is_visible_for_object(hsym,tobjectdef(defowner))
  1843. ) or
  1844. (
  1845. { In Delphi, you can repeat members of a parent class. You can't }
  1846. { do this for objects however, and you (obviouly) can't }
  1847. { declare two fields with the same name in a single class }
  1848. (m_delphi in current_settings.modeswitches) and
  1849. (
  1850. is_object(tdef(defowner)) or
  1851. (hsym.owner = self)
  1852. )
  1853. )
  1854. ) then
  1855. begin
  1856. { only warn when a parameter/local variable in a method
  1857. conflicts with a category method, because this can easily
  1858. happen due to all possible categories being imported via
  1859. CocoaAll }
  1860. warn:=
  1861. (is_objccategory(tdef(hsym.owner.defowner)) or
  1862. is_classhelper(tdef(hsym.owner.defowner))) and
  1863. (sym.typ in [paravarsym,localvarsym,fieldvarsym]);
  1864. DuplicateSym(hashedid,sym,hsym,warn);
  1865. result:=true;
  1866. end;
  1867. end
  1868. else
  1869. result:=inherited checkduplicate(hashedid,sym);
  1870. end;
  1871. {$ifdef llvm}
  1872. {****************************************************************************
  1873. tLlvmShadowSymtableEntry
  1874. ****************************************************************************}
  1875. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1876. begin
  1877. fdef:=def;
  1878. ffieldoffset:=fieldoffset;
  1879. end;
  1880. {****************************************************************************
  1881. TLlvmShadowSymtable
  1882. ****************************************************************************}
  1883. function tllvmshadowsymtable.get(f: tfieldvarsym): tllvmshadowsymtableentry;
  1884. begin
  1885. result:=get_by_llvm_index(f.llvmfieldnr)
  1886. end;
  1887. function tllvmshadowsymtable.get_by_llvm_index(index: longint): tllvmshadowsymtableentry;
  1888. begin
  1889. result:=tllvmshadowsymtableentry(symdeflist[index]);
  1890. end;
  1891. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1892. begin
  1893. equivst:=st;
  1894. curroffset:=0;
  1895. symdeflist:=tfpobjectlist.create(true);
  1896. generate;
  1897. end;
  1898. destructor tllvmshadowsymtable.destroy;
  1899. begin
  1900. symdeflist.free;
  1901. end;
  1902. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1903. var
  1904. sizectr,
  1905. tmpsize: aint;
  1906. begin
  1907. case equivst.usefieldalignment of
  1908. bit_alignment:
  1909. begin
  1910. { curoffset: bit address after the previous field. }
  1911. { llvm has no special support for bitfields in records, }
  1912. { so we replace them with plain bytes. }
  1913. { as soon as a single bit of a byte is allocated, we }
  1914. { allocate the byte in the llvm shadow record }
  1915. if (fieldoffset>curroffset) then
  1916. curroffset:=align(curroffset,8);
  1917. { fields in bitpacked records always start either right }
  1918. { after the previous one, or at the next byte boundary. }
  1919. if (curroffset<>fieldoffset) then
  1920. internalerror(2008051002);
  1921. if is_ordinal(vardef) then
  1922. begin
  1923. tmpsize:=vardef.packedbitsize;
  1924. sizectr:=((curroffset+tmpsize+7) shr 3)-((curroffset+7) shr 3);
  1925. inc(curroffset,tmpsize);
  1926. tmpsize:=0;
  1927. while sizectr<>0 do
  1928. begin
  1929. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+tmpsize*8));
  1930. dec(sizectr);
  1931. inc(tmpsize);
  1932. end;
  1933. end
  1934. else
  1935. begin
  1936. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1937. if not(derefclass) then
  1938. inc(curroffset,vardef.size*8)
  1939. else
  1940. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1941. end;
  1942. end
  1943. else if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1944. begin
  1945. { curoffset: address right after the previous field }
  1946. while (fieldoffset>curroffset) do
  1947. begin
  1948. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1949. inc(curroffset);
  1950. end;
  1951. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1952. if not(derefclass) then
  1953. inc(curroffset,vardef.size)
  1954. else
  1955. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1956. end
  1957. else
  1958. { default for llvm, don't add explicit padding }
  1959. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1960. end
  1961. end;
  1962. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1963. begin
  1964. if not(df_llvm_no_struct_packing in tdef(equivst.defowner).defoptions) then
  1965. begin
  1966. if equivst.usefieldalignment=bit_alignment then
  1967. curroffset:=align(curroffset,8) div 8;
  1968. { add padding fields }
  1969. while (finalsize>curroffset) do
  1970. begin
  1971. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,curroffset));
  1972. inc(curroffset);
  1973. end;
  1974. end;
  1975. end;
  1976. function field_offset_compare(item1, item2: pointer): integer;
  1977. var
  1978. field1: tfieldvarsym absolute item1;
  1979. field2: tfieldvarsym absolute item2;
  1980. begin
  1981. result:=field1.fieldoffset-field2.fieldoffset;
  1982. end;
  1983. procedure tllvmshadowsymtable.preprocess(out tempsymlist, variantstarts: tfplist);
  1984. var
  1985. fieldvs: tfieldvarsym;
  1986. lastvariantstartoffset, prevfieldoffset: aint;
  1987. newalignment: aint;
  1988. i, j: longint;
  1989. sorttempsymlist: boolean;
  1990. begin
  1991. i:=0;
  1992. variantstarts:=nil;
  1993. tempsymlist:=tfplist.create;
  1994. sorttempsymlist:=false;
  1995. prevfieldoffset:=-1;
  1996. while (i<equivst.symlist.count) do
  1997. begin
  1998. if not is_normal_fieldvarsym(tsym(equivst.symlist[i])) then
  1999. begin
  2000. inc(i);
  2001. continue;
  2002. end;
  2003. fieldvs:=tfieldvarsym(equivst.symlist[i]);
  2004. tempsymlist.Add(fieldvs);
  2005. { a "better" algorithm might be to use the largest }
  2006. { variant in case of (bit)packing, since then }
  2007. { alignment doesn't matter }
  2008. if (vo_is_first_field in fieldvs.varoptions) then
  2009. begin
  2010. { we assume that all fields are processed in order. }
  2011. if assigned(variantstarts) then
  2012. lastvariantstartoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  2013. else
  2014. begin
  2015. lastvariantstartoffset:=-1;
  2016. variantstarts:=tfplist.create;
  2017. end;
  2018. { new variant at same level as last one: use if higher alignment }
  2019. if (lastvariantstartoffset=fieldvs.fieldoffset) then
  2020. begin
  2021. if (equivst.usefieldalignment<>bit_alignment) then
  2022. newalignment:=used_align(fieldvs.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  2023. else
  2024. newalignment:=1;
  2025. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  2026. variantstarts[variantstarts.count-1]:=fieldvs;
  2027. end
  2028. { variant at deeper level than last one -> add }
  2029. else if (lastvariantstartoffset<fieldvs.fieldoffset) then
  2030. variantstarts.add(fieldvs)
  2031. else
  2032. begin
  2033. { a variant at a less deep level, so backtrack }
  2034. j:=variantstarts.count-2;
  2035. while (j>=0) do
  2036. begin
  2037. if (tfieldvarsym(variantstarts[j]).fieldoffset=fieldvs.fieldoffset) then
  2038. break;
  2039. dec(j);
  2040. end;
  2041. if (j<0) then
  2042. internalerror(2008051003);
  2043. { new variant has higher alignment? }
  2044. if (equivst.fieldalignment<>bit_alignment) then
  2045. newalignment:=used_align(fieldvs.vardef.alignment,equivst.recordalignmin,equivst.fieldalignment)
  2046. else
  2047. newalignment:=1;
  2048. { yes, replace and remove previous nested variants }
  2049. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  2050. begin
  2051. variantstarts[j]:=fieldvs;
  2052. variantstarts.count:=j+1;
  2053. end
  2054. { no, skip this variant }
  2055. else
  2056. begin
  2057. inc(i);
  2058. while (i<equivst.symlist.count) and
  2059. (not is_normal_fieldvarsym(tsym(equivst.symlist[i])) or
  2060. (tfieldvarsym(equivst.symlist[i]).fieldoffset>fieldvs.fieldoffset)) do
  2061. begin
  2062. if is_normal_fieldvarsym(tsym(equivst.symlist[i])) then
  2063. tempsymlist.Add(equivst.symlist[i]);
  2064. inc(i);
  2065. end;
  2066. continue;
  2067. end;
  2068. end;
  2069. end;
  2070. if not assigned(variantstarts) and
  2071. (fieldvs.fieldoffset<prevfieldoffset) then
  2072. sorttempsymlist:=true;
  2073. prevfieldoffset:=fieldvs.fieldoffset;
  2074. inc(i);
  2075. end;
  2076. if sorttempsymlist then
  2077. tempsymlist.Sort(@field_offset_compare);
  2078. end;
  2079. procedure tllvmshadowsymtable.buildtable(tempsymlist, variantstarts: tfplist);
  2080. var
  2081. lastvaroffsetprocessed: aint;
  2082. i, symcount, varcount: longint;
  2083. fieldvs: tfieldvarsym;
  2084. begin
  2085. { if it's an object/class, the first entry is the parent (if there is one) }
  2086. if (equivst.symtabletype=objectsymtable) and
  2087. assigned(tobjectdef(equivst.defowner).childof) then
  2088. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  2089. symcount:=tempsymlist.count;
  2090. varcount:=0;
  2091. i:=0;
  2092. lastvaroffsetprocessed:=-1;
  2093. while (i<symcount) do
  2094. begin
  2095. fieldvs:=tfieldvarsym(tempsymlist[i]);
  2096. { start of a new variant? }
  2097. if (vo_is_first_field in fieldvs.varoptions) then
  2098. begin
  2099. { if we want to process the same variant offset twice, it means that we }
  2100. { got to the end and are trying to process the next variant part -> stop }
  2101. if (fieldvs.fieldoffset<=lastvaroffsetprocessed) then
  2102. break;
  2103. if (varcount>=variantstarts.count) then
  2104. internalerror(2008051005);
  2105. { new variant part -> use the one with the biggest alignment }
  2106. fieldvs:=tfieldvarsym(variantstarts[varcount]);
  2107. i:=tempsymlist.indexof(fieldvs);
  2108. lastvaroffsetprocessed:=fieldvs.fieldoffset;
  2109. inc(varcount);
  2110. if (i<0) then
  2111. internalerror(2008051004);
  2112. end;
  2113. appenddefoffset(fieldvs.vardef,fieldvs.fieldoffset,false);
  2114. inc(i);
  2115. end;
  2116. addalignmentpadding(equivst.datasize);
  2117. end;
  2118. procedure tllvmshadowsymtable.buildmapping(tempsymlist, variantstarts: tfplist);
  2119. var
  2120. fieldvs: tfieldvarsym;
  2121. i, varcount: longint;
  2122. shadowindex: longint;
  2123. symcount : longint;
  2124. begin
  2125. varcount:=0;
  2126. shadowindex:=0;
  2127. symcount:=tempsymlist.count;
  2128. i:=0;
  2129. while (i<symcount) do
  2130. begin
  2131. fieldvs:=tfieldvarsym(tempsymlist[i]);
  2132. { start of a new variant? }
  2133. if (vo_is_first_field in fieldvs.varoptions) then
  2134. begin
  2135. { back up to a less deeply nested variant level? }
  2136. while fieldvs.fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset do
  2137. dec(varcount);
  2138. { it's possible that some variants are more deeply nested than the
  2139. one we recorded in the shadowsymtable (since we recorded the one
  2140. with the biggest alignment, not necessarily the biggest one in size
  2141. }
  2142. if fieldvs.fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset then
  2143. varcount:=variantstarts.count-1
  2144. else if fieldvs.fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset then
  2145. internalerror(2008051006);
  2146. { reset the shadowindex to the start of this variant. }
  2147. { in case the llvmfieldnr is not (yet) set for this }
  2148. { field, shadowindex will simply be reset to zero and }
  2149. { we'll start searching from the start of the record }
  2150. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  2151. if (varcount<pred(variantstarts.count)) then
  2152. inc(varcount);
  2153. end;
  2154. { find the last shadowfield whose offset <= the current field's offset }
  2155. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<fieldvs.fieldoffset) and
  2156. (shadowindex<symdeflist.count-1) and
  2157. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset<=fieldvs.fieldoffset) do
  2158. inc(shadowindex);
  2159. { set the field number and potential offset from that field (in case }
  2160. { of overlapping variants) }
  2161. fieldvs.llvmfieldnr:=shadowindex;
  2162. fieldvs.offsetfromllvmfield:=
  2163. fieldvs.fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  2164. inc(i);
  2165. end;
  2166. end;
  2167. procedure tllvmshadowsymtable.generate;
  2168. var
  2169. variantstarts, tempsymlist: tfplist;
  2170. begin
  2171. { first go through the entire record and }
  2172. { store the fieldvarsyms of the variants }
  2173. { with the highest alignment }
  2174. preprocess(tempsymlist, variantstarts);
  2175. { now go through the regular fields and the selected variants, }
  2176. { and add them to the llvm shadow record symtable }
  2177. buildtable(tempsymlist, variantstarts);
  2178. { finally map all original fields to the llvm definition }
  2179. buildmapping(tempsymlist, variantstarts);
  2180. variantstarts.free;
  2181. tempsymlist.free;
  2182. end;
  2183. {$endif llvm}
  2184. {****************************************************************************
  2185. TAbstractSubSymtable
  2186. ****************************************************************************}
  2187. procedure tabstractsubsymtable.ppuwrite(ppufile:tcompilerppufile);
  2188. var
  2189. oldtyp : byte;
  2190. begin
  2191. oldtyp:=ppufile.entrytyp;
  2192. ppufile.entrytyp:=subentryid;
  2193. inherited ppuwrite(ppufile);
  2194. ppufile.entrytyp:=oldtyp;
  2195. end;
  2196. {****************************************************************************
  2197. TAbstractLocalSymtable
  2198. ****************************************************************************}
  2199. function tabstractlocalsymtable.count_locals:longint;
  2200. var
  2201. i : longint;
  2202. sym : tsym;
  2203. begin
  2204. result:=0;
  2205. for i:=0 to SymList.Count-1 do
  2206. begin
  2207. sym:=tsym(SymList[i]);
  2208. { Count only varsyms, but ignore the funcretsym }
  2209. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  2210. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  2211. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  2212. (tstoredsym(sym).refs>0)) then
  2213. inc(result);
  2214. end;
  2215. end;
  2216. function tabstractlocalsymtable.iscurrentunit: boolean;
  2217. begin
  2218. Result:=
  2219. assigned(defowner) and
  2220. defowner.owner.iscurrentunit;
  2221. end;
  2222. {****************************************************************************
  2223. TLocalSymtable
  2224. ****************************************************************************}
  2225. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  2226. begin
  2227. inherited create('');
  2228. defowner:=adefowner;
  2229. symtabletype:=localsymtable;
  2230. symtablelevel:=level;
  2231. end;
  2232. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2233. var
  2234. hsym : tsym;
  2235. begin
  2236. if not assigned(defowner) or
  2237. (defowner.typ<>procdef) then
  2238. internalerror(200602042);
  2239. result:=false;
  2240. hsym:=tsym(FindWithHash(hashedid));
  2241. if assigned(hsym) then
  2242. begin
  2243. { a local and the function can have the same
  2244. name in TP and Delphi, but RESULT not }
  2245. if (m_duplicate_names in current_settings.modeswitches) and
  2246. (hsym.typ in [absolutevarsym,localvarsym]) and
  2247. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  2248. not((m_result in current_settings.modeswitches) and
  2249. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  2250. HideSym(hsym)
  2251. else
  2252. DuplicateSym(hashedid,sym,hsym,false);
  2253. result:=true;
  2254. exit;
  2255. end;
  2256. { check also parasymtable, this needs to be done here because
  2257. of the special situation with the funcret sym that needs to be
  2258. hidden for tp and delphi modes }
  2259. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  2260. if assigned(hsym) then
  2261. begin
  2262. { a local and the function can have the same
  2263. name in TP and Delphi, but RESULT not }
  2264. if (m_duplicate_names in current_settings.modeswitches) and
  2265. (sym.typ in [absolutevarsym,localvarsym]) and
  2266. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  2267. not((m_result in current_settings.modeswitches) and
  2268. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  2269. Hidesym(sym)
  2270. else
  2271. DuplicateSym(hashedid,sym,hsym,false);
  2272. result:=true;
  2273. exit;
  2274. end;
  2275. { check ObjectSymtable, skip this for funcret sym because
  2276. that will always be positive because it has the same name
  2277. as the procsym }
  2278. if not is_funcret_sym(sym) and
  2279. (defowner.typ=procdef) and
  2280. assigned(tprocdef(defowner).struct) and
  2281. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2282. (
  2283. not(m_duplicate_names in current_settings.modeswitches) or
  2284. is_object(tprocdef(defowner).struct)
  2285. ) then
  2286. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2287. end;
  2288. {****************************************************************************
  2289. TParaSymtable
  2290. ****************************************************************************}
  2291. constructor tparasymtable.create(adefowner:tdef;level:byte);
  2292. begin
  2293. inherited create('');
  2294. defowner:=adefowner;
  2295. symtabletype:=parasymtable;
  2296. symtablelevel:=level;
  2297. end;
  2298. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2299. begin
  2300. result:=inherited checkduplicate(hashedid,sym);
  2301. if result then
  2302. exit;
  2303. if not(m_duplicate_names in current_settings.modeswitches) and
  2304. assigned(defowner) and (defowner.typ=procdef) and
  2305. assigned(tprocdef(defowner).struct) and
  2306. assigned(tprocdef(defowner).owner) and
  2307. (tprocdef(defowner).owner.defowner=tprocdef(defowner).struct) and
  2308. (
  2309. not(m_delphi in current_settings.modeswitches) or
  2310. is_object(tprocdef(defowner).struct)
  2311. ) then
  2312. result:=tprocdef(defowner).struct.symtable.checkduplicate(hashedid,sym);
  2313. end;
  2314. {****************************************************************************
  2315. TAbstractUniTSymtable
  2316. ****************************************************************************}
  2317. constructor tabstractuniTSymtable.create(const n : string;id:word);
  2318. begin
  2319. inherited create(n);
  2320. moduleid:=id;
  2321. end;
  2322. function tabstractuniTSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2323. var
  2324. hsym : tsym;
  2325. begin
  2326. result:=false;
  2327. hsym:=tsym(FindWithHash(hashedid));
  2328. if assigned(hsym) then
  2329. begin
  2330. if (sym is tstoredsym) and handle_generic_dummysym(hsym,tstoredsym(sym).symoptions) then
  2331. exit;
  2332. if hsym.typ=symconst.namespacesym then
  2333. begin
  2334. case sym.typ of
  2335. symconst.namespacesym:;
  2336. symconst.unitsym:
  2337. begin
  2338. HideSym(sym); { if we add a unit and there is a namespace with the same name then hide the unit name and not the namespace }
  2339. tnamespacesym(hsym).unitsym:=tsym(sym);
  2340. end
  2341. else
  2342. HideSym(hsym);
  2343. end;
  2344. end
  2345. else
  2346. { In delphi (contrary to TP) you can have a symbol with the same name as the
  2347. unit, the unit can then not be accessed anymore using
  2348. <unit>.<id>, so we can hide the symbol.
  2349. Do the same if we add a namespace and there is a unit with the same name }
  2350. if (hsym.typ=symconst.unitsym) and
  2351. ((m_delphi in current_settings.modeswitches) or (sym.typ=symconst.namespacesym)) then
  2352. begin
  2353. HideSym(hsym);
  2354. if sym.typ=symconst.namespacesym then
  2355. tnamespacesym(sym).unitsym:=tsym(hsym);
  2356. end
  2357. { iso mode program parameters: staticvarsyms might have the same name as a program parameters,
  2358. in this case, copy the isoindex and make the original symbol invisible }
  2359. else if (m_isolike_program_para in current_settings.modeswitches) and (hsym.typ=programparasym) and (sym.typ=staticvarsym)
  2360. and (tprogramparasym(hsym).isoindex<>0) then
  2361. begin
  2362. HideSym(hsym);
  2363. tstaticvarsym(sym).isoindex:=tprogramparasym(hsym).isoindex;
  2364. end
  2365. else if (m_iso in current_settings.modeswitches) and (hsym.typ=unitsym) then
  2366. HideSym(hsym)
  2367. else
  2368. DuplicateSym(hashedid,sym,hsym,false);
  2369. result:=true;
  2370. exit;
  2371. end;
  2372. end;
  2373. function tabstractuniTSymtable.findnamespace(const n:string):TSymEntry;
  2374. begin
  2375. result:=find(n);
  2376. if assigned(result)and(result.typ<>namespacesym)then
  2377. result:=nil;
  2378. end;
  2379. function tabstractuniTSymtable.iscurrentunit:boolean;
  2380. begin
  2381. result:=assigned(current_module) and
  2382. (
  2383. (current_module.globalsymtable=self) or
  2384. (current_module.localsymtable=self)
  2385. );
  2386. end;
  2387. function tabstractuniTSymtable.needs_init_final: boolean;
  2388. begin
  2389. if not init_final_check_done then
  2390. begin
  2391. result:=inherited needs_init_final;
  2392. if not result then
  2393. begin
  2394. result:=has_class_condestructors;
  2395. if result then
  2396. include(tableoptions,sto_needs_init_final);
  2397. end;
  2398. end;
  2399. result:=sto_needs_init_final in tableoptions;
  2400. end;
  2401. procedure tabstractuniTSymtable.insertunit(sym:TSymEntry);
  2402. var
  2403. p:integer;
  2404. n,ns:string;
  2405. oldsym:TSymEntry;
  2406. begin
  2407. insertsym(sym);
  2408. n:=sym.realname;
  2409. p:=pos('.',n);
  2410. ns:='';
  2411. while p>0 do
  2412. begin
  2413. if ns='' then
  2414. ns:=copy(n,1,p-1)
  2415. else
  2416. ns:=ns+'.'+copy(n,1,p-1);
  2417. system.delete(n,1,p);
  2418. oldsym:=findnamespace(upper(ns));
  2419. if not assigned(oldsym) then
  2420. insertsym(cnamespacesym.create(ns));
  2421. p:=pos('.',n);
  2422. end;
  2423. end;
  2424. procedure CheckForClassConDestructors(p:TObject;arg:pointer);
  2425. var
  2426. result: pboolean absolute arg;
  2427. begin
  2428. if result^ then
  2429. exit;
  2430. if (tdef(p).typ in [objectdef,recorddef]) and
  2431. not (df_generic in tdef(p).defoptions) then
  2432. begin
  2433. { first check the class... }
  2434. if ([oo_has_class_constructor,oo_has_class_destructor] * tabstractrecorddef(p).objectoptions <> []) then
  2435. result^:=true;
  2436. { ... and then also check all subclasses }
  2437. if not result^ then
  2438. tabstractrecorddef(p).symtable.deflist.foreachcall(@CheckForClassConDestructors,arg);
  2439. end;
  2440. end;
  2441. function tabstractuniTSymtable.has_class_condestructors: boolean;
  2442. begin
  2443. result:=false;
  2444. deflist.foreachcall(@CheckForClassConDestructors,@result);
  2445. end;
  2446. {****************************************************************************
  2447. TStaticSymtable
  2448. ****************************************************************************}
  2449. constructor tstaticsymtable.create(const n : string;id:word);
  2450. begin
  2451. inherited create(n,id);
  2452. symtabletype:=staticsymtable;
  2453. symtablelevel:=main_program_level;
  2454. currentvisibility:=vis_private;
  2455. end;
  2456. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  2457. begin
  2458. inherited ppuload(ppufile);
  2459. { now we can deref the syms and defs }
  2460. deref(false);
  2461. end;
  2462. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  2463. begin
  2464. inherited ppuwrite(ppufile);
  2465. end;
  2466. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  2467. begin
  2468. result:=inherited checkduplicate(hashedid,sym);
  2469. if not result and
  2470. (current_module.localsymtable=self) and
  2471. assigned(current_module.globalsymtable) then
  2472. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  2473. end;
  2474. function tstaticsymtable.findnamespace(const n:string):TSymEntry;
  2475. begin
  2476. result:=inherited findnamespace(n);
  2477. if not assigned(result) and
  2478. (current_module.localsymtable=self) and
  2479. assigned(current_module.globalsymtable) then
  2480. result:=tglobalsymtable(current_module.globalsymtable).findnamespace(n);
  2481. end;
  2482. {****************************************************************************
  2483. TGlobalSymtable
  2484. ****************************************************************************}
  2485. constructor tglobalsymtable.create(const n : string;id:word);
  2486. begin
  2487. inherited create(n,id);
  2488. symtabletype:=globalsymtable;
  2489. symtablelevel:=main_program_level;
  2490. end;
  2491. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  2492. begin
  2493. inherited ppuload(ppufile);
  2494. { now we can deref the syms and defs }
  2495. deref(false);
  2496. end;
  2497. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  2498. begin
  2499. { write the symtable entries }
  2500. inherited ppuwrite(ppufile);
  2501. end;
  2502. {*****************************************************************************
  2503. tspecializesymtable
  2504. *****************************************************************************}
  2505. constructor tspecializesymtable.create(const n : string;id:word);
  2506. begin
  2507. inherited create(n,id);
  2508. { the specialize symtable does not own the syms and defs as they are all
  2509. moved to a different symtable before the symtable is destroyed; this
  2510. avoids calls to "extract" }
  2511. symlist.ownsobjects:=false;
  2512. deflist.ownsobjects:=false;
  2513. end;
  2514. function tspecializesymtable.iscurrentunit: boolean;
  2515. begin
  2516. Result:=true;
  2517. end;
  2518. {****************************************************************************
  2519. TWITHSYMTABLE
  2520. ****************************************************************************}
  2521. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  2522. begin
  2523. inherited create('');
  2524. symtabletype:=withsymtable;
  2525. withrefnode:=refnode;
  2526. { Replace SymList with the passed symlist }
  2527. SymList.free;
  2528. SymList:=ASymList;
  2529. defowner:=aowner;
  2530. end;
  2531. destructor twithsymtable.destroy;
  2532. begin
  2533. if refcount>1 then
  2534. exit;
  2535. withrefnode.free;
  2536. { Disable SymList because we don't Own it }
  2537. SymList:=nil;
  2538. inherited destroy;
  2539. end;
  2540. procedure twithsymtable.clear;
  2541. begin
  2542. { remove no entry from a withsymtable as it is only a pointer to the
  2543. recorddef or objectdef symtable }
  2544. end;
  2545. procedure twithsymtable.insertdef(def:TDefEntry);
  2546. begin
  2547. { Definitions can't be registered in the withsymtable
  2548. because the withsymtable is removed after the with block.
  2549. We can't easily solve it here because the next symtable in the
  2550. stack is not known. }
  2551. internalerror(200602046);
  2552. end;
  2553. {****************************************************************************
  2554. TSTT_ExceptionSymtable
  2555. ****************************************************************************}
  2556. constructor tstt_excepTSymtable.create;
  2557. begin
  2558. inherited create('');
  2559. symtabletype:=exceptsymtable;
  2560. end;
  2561. {****************************************************************************
  2562. TMacroSymtable
  2563. ****************************************************************************}
  2564. constructor tmacrosymtable.create(exported: boolean);
  2565. begin
  2566. inherited create('');
  2567. if exported then
  2568. symtabletype:=exportedmacrosymtable
  2569. else
  2570. symtabletype:=localmacrosymtable;
  2571. symtablelevel:=main_program_level;
  2572. end;
  2573. {****************************************************************************
  2574. TEnumSymtable
  2575. ****************************************************************************}
  2576. procedure tenumsymtable.insertsym(sym: TSymEntry; checkdup: boolean);
  2577. var
  2578. value: longint;
  2579. def: tenumdef;
  2580. begin
  2581. // defowner = nil only when we are loading from ppu
  2582. if defowner<>nil then
  2583. begin
  2584. { First entry? Then we need to set the minval }
  2585. value:=tenumsym(sym).value;
  2586. def:=tenumdef(defowner);
  2587. if SymList.count=0 then
  2588. begin
  2589. if value>0 then
  2590. def.has_jumps:=true;
  2591. def.setmin(value);
  2592. def.setmax(value);
  2593. end
  2594. else
  2595. begin
  2596. { check for jumps }
  2597. if value>def.max+1 then
  2598. def.has_jumps:=true;
  2599. { update low and high }
  2600. if def.min>value then
  2601. def.setmin(value);
  2602. if def.max<value then
  2603. def.setmax(value);
  2604. end;
  2605. end;
  2606. inherited insertsym(sym, checkdup);
  2607. end;
  2608. constructor tenumsymtable.create(adefowner: tdef);
  2609. begin
  2610. inherited Create('');
  2611. symtabletype:=enumsymtable;
  2612. defowner:=adefowner;
  2613. end;
  2614. {****************************************************************************
  2615. TArraySymtable
  2616. ****************************************************************************}
  2617. procedure tarraysymtable.insertdef(def: TDefEntry);
  2618. begin
  2619. { Enums must also be available outside the record scope,
  2620. insert in the owner of this symtable }
  2621. if def.typ=enumdef then
  2622. defowner.owner.insertdef(def)
  2623. else
  2624. inherited insertdef(def);
  2625. end;
  2626. constructor tarraysymtable.create(adefowner: tdef);
  2627. begin
  2628. inherited Create('');
  2629. symtabletype:=arraysymtable;
  2630. defowner:=adefowner;
  2631. end;
  2632. {*****************************************************************************
  2633. Helper Routines
  2634. *****************************************************************************}
  2635. function FullTypeName(def,otherdef:tdef):string;
  2636. var
  2637. s1,s2 : string;
  2638. begin
  2639. if def.typ in [objectdef,recorddef] then
  2640. s1:=tabstractrecorddef(def).RttiName
  2641. else
  2642. s1:=def.typename;
  2643. { When the names are the same try to include the unit name }
  2644. if assigned(otherdef) and
  2645. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  2646. begin
  2647. s2:=otherdef.typename;
  2648. if upper(s1)=upper(s2) then
  2649. s1:=def.owner.realname^+'.'+s1;
  2650. end;
  2651. FullTypeName:=s1;
  2652. end;
  2653. function generate_nested_name(symtable:tsymtable;const delimiter:string):string;
  2654. begin
  2655. result:='';
  2656. while assigned(symtable) and (symtable.symtabletype in [ObjectSymtable,recordsymtable]) do
  2657. begin
  2658. if (result='') then
  2659. if symtable.name<>nil then
  2660. result:=symtable.name^
  2661. else
  2662. else
  2663. if symtable.name<>nil then
  2664. result:=symtable.name^+delimiter+result
  2665. else
  2666. result:=delimiter+result;
  2667. symtable:=symtable.defowner.owner;
  2668. end;
  2669. end;
  2670. function generate_objectpascal_helper_key(def:tdef):TSymStr;
  2671. begin
  2672. if not assigned(def) then
  2673. internalerror(2013020501);
  2674. if def.typ in [recorddef,objectdef] then
  2675. result:=make_mangledname('',tabstractrecorddef(def).symtable,'')
  2676. else
  2677. result:=make_mangledname('',def.owner,def.typesym.name);
  2678. end;
  2679. procedure incompatibletypes(def1,def2:tdef);
  2680. begin
  2681. { When there is an errordef there is already an error message show }
  2682. if (def2.typ=errordef) or
  2683. (def1.typ=errordef) then
  2684. exit;
  2685. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  2686. end;
  2687. procedure hidesym(sym:TSymEntry);
  2688. begin
  2689. sym.realname:='$hidden'+sym.realname;
  2690. tsym(sym).visibility:=vis_hidden;
  2691. end;
  2692. procedure duplicatesym(var hashedid: THashedIDString; dupsym, origsym: TSymEntry; warn: boolean);
  2693. var
  2694. st : TSymtable;
  2695. filename : TIDString;
  2696. begin
  2697. if not warn then
  2698. Message1(sym_e_duplicate_id,tsym(origsym).realname)
  2699. else
  2700. Message1(sym_w_duplicate_id,tsym(origsym).realname);
  2701. { Write hint where the original symbol was found }
  2702. st:=finduniTSymtable(origsym.owner);
  2703. with tsym(origsym).fileinfo do
  2704. begin
  2705. if assigned(st) and
  2706. (st.symtabletype=globalsymtable) and
  2707. st.iscurrentunit then
  2708. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  2709. else if assigned(st.name) then
  2710. begin
  2711. filename:=find_module_from_symtable(st).sourcefiles.get_file_name(fileindex);
  2712. if filename<>'' then
  2713. Message2(sym_h_duplicate_id_where,'unit '+st.name^+': '+filename,tostr(line))
  2714. else
  2715. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  2716. end;
  2717. end;
  2718. { Rename duplicate sym to an unreachable name, but it can be
  2719. inserted in the symtable without errors }
  2720. inc(dupnr);
  2721. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  2722. if assigned(dupsym) then
  2723. include(tsym(dupsym).symoptions,sp_implicitrename);
  2724. end;
  2725. function handle_generic_dummysym(sym:TSymEntry;var symoptions:tsymoptions):boolean;
  2726. begin
  2727. result:=false;
  2728. if not assigned(sym) or not (sym is tstoredsym) then
  2729. Internalerror(2011081101);
  2730. { For generics a dummy symbol without the parameter count is created
  2731. if such a symbol not yet exists so that different parts of the
  2732. parser can find that symbol. If that symbol is still a
  2733. undefineddef we replace the generic dummy symbol's
  2734. name with a "dup" name and use the new symbol as the generic dummy
  2735. symbol }
  2736. if (sp_generic_dummy in tstoredsym(sym).symoptions) and
  2737. (sym.typ=typesym) and (ttypesym(sym).typedef.typ=undefineddef) and
  2738. (m_delphi in current_settings.modeswitches) then
  2739. begin
  2740. inc(dupnr);
  2741. sym.Owner.SymList.Rename(upper(sym.realname),'dup_'+tostr(dupnr)+sym.realname);
  2742. include(tsym(sym).symoptions,sp_implicitrename);
  2743. { we need to find the new symbol now if checking for a dummy }
  2744. include(symoptions,sp_generic_dummy);
  2745. result:=true;
  2746. end;
  2747. end;
  2748. procedure check_systemunit_loaded; inline;
  2749. begin
  2750. if systemunit=nil then
  2751. Message(sym_f_systemunitnotloaded);
  2752. end;
  2753. procedure write_system_parameter_lists(const name:string);
  2754. var
  2755. srsym:tprocsym;
  2756. begin
  2757. check_systemunit_loaded;
  2758. srsym:=tprocsym(systemunit.find(name));
  2759. if not assigned(srsym) or not (srsym.typ=procsym) then
  2760. internalerror(2016060302);
  2761. srsym.write_parameter_lists(nil);
  2762. end;
  2763. {*****************************************************************************
  2764. Search
  2765. *****************************************************************************}
  2766. procedure addsymref(sym:tsym;def:tdef);
  2767. var
  2768. owner,procowner : tsymtable;
  2769. begin
  2770. { for symbols used in preprocessor expressions, we don't want to
  2771. increase references count (for smaller final binaries) }
  2772. if not assigned(current_scanner) then
  2773. internalerror(2017050601);
  2774. if current_scanner.in_preproc_comp_expr then
  2775. exit;
  2776. { symbol uses count }
  2777. sym.IncRefCount;
  2778. owner:=sym.owner;
  2779. while owner.symtabletype in [objectsymtable,recordsymtable,enumsymtable] do
  2780. owner:=tdef(owner.defowner).owner;
  2781. if assigned(current_module) and
  2782. (owner.symtabletype=globalsymtable) then
  2783. begin
  2784. if tglobalsymtable(owner).moduleid>=current_module.unitmapsize then
  2785. internalerror(200501152);
  2786. { unit uses count }
  2787. inc(current_module.unitmap[tglobalsymtable(owner).moduleid].refs);
  2788. { Note: don't check the symtable directly as owner might be
  2789. a specialize symtable which is a globalsymtable as well }
  2790. if (
  2791. assigned(current_module.globalsymtable) and
  2792. (current_module.globalsymtable.moduleid<>owner.moduleid)
  2793. ) or (
  2794. assigned(current_module.localsymtable) and
  2795. (current_module.localsymtable.moduleid<>owner.moduleid)
  2796. ) then
  2797. { symbol is imported from another unit }
  2798. current_module.addimportedsym(sym);
  2799. end;
  2800. { static symbols that are used in public functions must be exported
  2801. for packages as well }
  2802. if ([tf_supports_packages,tf_supports_hidden_symbols]<=target_info.flags) and
  2803. (owner.symtabletype=staticsymtable) and
  2804. assigned(current_procinfo) and
  2805. (
  2806. (
  2807. (sym.typ=staticvarsym) and
  2808. ([vo_is_public,vo_has_global_ref]*tstaticvarsym(sym).varoptions=[])
  2809. ) or (
  2810. (sym.typ=localvarsym) and
  2811. assigned(tlocalvarsym(sym).defaultconstsym) and
  2812. ([vo_is_public,vo_has_global_ref]*tstaticvarsym(tlocalvarsym(sym).defaultconstsym).varoptions=[])
  2813. ) or (
  2814. (sym.typ=procsym) and
  2815. assigned(def) and
  2816. (def.typ=procdef) and
  2817. not (df_has_global_ref in def.defoptions) and
  2818. not (po_public in tprocdef(def).procoptions)
  2819. )
  2820. ) then
  2821. begin
  2822. procowner:=current_procinfo.procdef.owner;
  2823. while procowner.symtabletype in [objectsymtable,recordsymtable] do
  2824. procowner:=tdef(procowner.defowner).owner;
  2825. if procowner.symtabletype=globalsymtable then
  2826. begin
  2827. if sym.typ=procsym then
  2828. current_procinfo.add_local_ref_def(def)
  2829. else if sym.typ=staticvarsym then
  2830. current_procinfo.add_local_ref_sym(sym)
  2831. else
  2832. current_procinfo.add_local_ref_sym(tlocalvarsym(sym).defaultconstsym);
  2833. end;
  2834. end;
  2835. end;
  2836. procedure addsymref(sym:tsym);
  2837. begin
  2838. addsymref(sym,nil);
  2839. end;
  2840. function is_owned_by(nesteddef,ownerdef:tdef):boolean;
  2841. begin
  2842. result:=nesteddef=ownerdef;
  2843. if not result and
  2844. { types declared locally in a record method are not defined in the
  2845. record itself }
  2846. not(nesteddef.owner.symtabletype in [localsymtable,parasymtable]) and
  2847. assigned(nesteddef.owner.defowner) then
  2848. result:=is_owned_by(tdef(nesteddef.owner.defowner),ownerdef);
  2849. end;
  2850. function sym_is_owned_by(childsym:tsym;symtable:tsymtable):boolean;
  2851. begin
  2852. result:=assigned(childsym) and (childsym.owner=symtable);
  2853. if not result and assigned(childsym) and
  2854. (childsym.owner.symtabletype in [objectsymtable,recordsymtable]) then
  2855. result:=sym_is_owned_by(tabstractrecorddef(childsym.owner.defowner).typesym,symtable);
  2856. end;
  2857. function defs_belong_to_same_generic(def1, def2: tdef): boolean;
  2858. begin
  2859. result:=false;
  2860. if not assigned(def1) or not assigned(def2) then
  2861. exit;
  2862. { for both defs walk to the topmost generic }
  2863. while assigned(def1.owner.defowner) and (df_generic in tstoreddef(def1.owner.defowner).defoptions) do
  2864. def1:=tdef(def1.owner.defowner);
  2865. while assigned(def2.owner.defowner) and (df_generic in tstoreddef(def2.owner.defowner).defoptions) do
  2866. def2:=tdef(def2.owner.defowner);
  2867. result:=def1=def2;
  2868. end;
  2869. function get_generic_in_hierarchy_by_name(srsym: tsym; def: tdef): tdef;
  2870. var
  2871. uname : string;
  2872. begin
  2873. { TODO : check regarding arrays and records declared as their type }
  2874. if not (def.typ in [recorddef,objectdef]) then
  2875. internalerror(2012051501);
  2876. uname:=upper(srsym.realname);
  2877. repeat
  2878. if uname=copy(tabstractrecorddef(def).objname^,1,pos('$',tabstractrecorddef(def).objname^)-1) then
  2879. begin
  2880. result:=def;
  2881. exit;
  2882. end;
  2883. def:=tdef(def.owner.defowner);
  2884. until not assigned(def) or not (def.typ in [recorddef,objectdef]);
  2885. result:=nil;
  2886. end;
  2887. function return_specialization_of_generic(nesteddef,genericdef:tdef; out resultdef:tdef):boolean;
  2888. begin
  2889. { TODO : check regarding arrays and records declared as their type }
  2890. if not (nesteddef.typ in [recorddef,objectdef]) then
  2891. internalerror(2012051601);
  2892. repeat
  2893. if tstoreddef(nesteddef).genericdef=genericdef then
  2894. begin
  2895. resultdef:=nesteddef;
  2896. result:=true;
  2897. exit;
  2898. end;
  2899. nesteddef:=tdef(nesteddef.owner.defowner);
  2900. until not assigned(nesteddef) or not (nesteddef.typ in [recorddef,objectdef]);
  2901. resultdef:=nil;
  2902. result:=false;
  2903. end;
  2904. { symst: symboltable that contains the symbol (-> symowner def: record/objectdef in which the symbol is defined)
  2905. symvisibility: visibility of the symbol
  2906. contextobjdef: via which def the symbol is accessed, e.g.:
  2907. fieldname:=1 -> contextobjdef = current_structdef
  2908. objfield.fieldname:=1 -> contextobjdef = def of objfield
  2909. }
  2910. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tabstractrecorddef):boolean;
  2911. var
  2912. curstruct : tabstractrecorddef;
  2913. function is_current_unit(st:tsymtable):boolean;
  2914. begin
  2915. result :=
  2916. (
  2917. (
  2918. assigned(curstruct) and
  2919. (st.moduleid=curstruct.symtable.moduleid)
  2920. ) or
  2921. (
  2922. not assigned(curstruct) and
  2923. st.iscurrentunit
  2924. )
  2925. );
  2926. end;
  2927. var
  2928. orgcontextobjdef,
  2929. orgsymownerdef,
  2930. symownerdef : tabstractrecorddef;
  2931. nonlocalst : tsymtable;
  2932. isspezproc : boolean;
  2933. function check_strict_private:boolean;
  2934. begin
  2935. result:=assigned(curstruct) and
  2936. is_owned_by(curstruct,symownerdef);
  2937. end;
  2938. function check_strict_protected:boolean;
  2939. function is_childof(child, potentialparent: tdef):boolean;
  2940. begin
  2941. result:=true;
  2942. if def_is_related(child, potentialparent) then
  2943. exit;
  2944. if (child.typ=objectdef) and
  2945. (potentialparent.typ=objectdef) and
  2946. (tobjectdef(potentialparent).defoptions*[df_generic,df_specialization]=[df_generic]) then
  2947. begin
  2948. repeat
  2949. if tobjectdef(child).genericdef<>nil then
  2950. begin
  2951. if tobjectdef(child).genericdef.typ<>objectdef then
  2952. break;
  2953. child:=tobjectdef(child).genericdef as tobjectdef
  2954. end
  2955. else
  2956. child:=tobjectdef(child).childof;
  2957. if (child<>nil) and equal_defs(child, potentialparent) then
  2958. exit;
  2959. until child=nil;
  2960. end;
  2961. result:=false;
  2962. end;
  2963. function owner_hierarchy_related(nested,check:tabstractrecorddef):boolean;
  2964. var
  2965. owner:tabstractrecorddef;
  2966. begin
  2967. result:=true;
  2968. repeat
  2969. if is_childof(nested,check) then
  2970. exit;
  2971. if nested.owner.symtabletype in [recordsymtable,objectsymtable] then
  2972. nested:=tabstractrecorddef(nested.owner.defowner)
  2973. else
  2974. break;
  2975. until not assigned(nested);
  2976. result:=false;
  2977. end;
  2978. begin
  2979. result:=(
  2980. { access from nested class (specialization case) }
  2981. assigned(curstruct) and
  2982. is_owned_by(curstruct,symownerdef)
  2983. ) or
  2984. (
  2985. { access from nested class (non-specialization case) }
  2986. (orgsymownerdef<>symownerdef) and
  2987. assigned(curstruct) and
  2988. is_owned_by(curstruct,orgsymownerdef)
  2989. ) or
  2990. (
  2991. { access from child class (specialization case) }
  2992. assigned(contextobjdef) and
  2993. assigned(curstruct) and
  2994. owner_hierarchy_related(contextobjdef,symownerdef) and
  2995. is_childof(curstruct,contextobjdef)
  2996. ) or
  2997. (
  2998. { access from child class (non-specialization case) }
  2999. assigned(orgcontextobjdef) and
  3000. (
  3001. (orgcontextobjdef<>contextobjdef) or
  3002. (orgsymownerdef<>symownerdef)
  3003. ) and
  3004. assigned(curstruct) and
  3005. owner_hierarchy_related(orgcontextobjdef,orgsymownerdef) and
  3006. is_childof(curstruct,orgcontextobjdef)
  3007. ) or
  3008. (
  3009. { helpers can access strict protected symbols }
  3010. is_objectpascal_helper(contextobjdef) and
  3011. is_childof(tobjectdef(contextobjdef).extendeddef,symownerdef)
  3012. ) or
  3013. (
  3014. { same as above, but from context of call node inside
  3015. helper method }
  3016. is_objectpascal_helper(curstruct) and
  3017. is_childof(tobjectdef(curstruct).extendeddef,symownerdef)
  3018. );
  3019. end;
  3020. begin
  3021. result:=false;
  3022. { Get objdectdef owner of the symtable for the is_related checks }
  3023. if not assigned(symst) or
  3024. not (symst.symtabletype in [objectsymtable,recordsymtable]) then
  3025. internalerror(200810285);
  3026. symownerdef:=tabstractrecorddef(symst.defowner);
  3027. orgsymownerdef:=symownerdef;
  3028. orgcontextobjdef:=contextobjdef;
  3029. { for specializations we need to check the visibility of the generic,
  3030. not the specialization (at least when comparing outside of the
  3031. specialization }
  3032. if df_specialization in symownerdef.defoptions then
  3033. begin
  3034. if not assigned(symownerdef.genericdef) then
  3035. internalerror(2024041201);
  3036. if not (symownerdef.genericdef.typ in [objectdef,recorddef]) then
  3037. internalerror(2024020901);
  3038. orgsymownerdef:=symownerdef;
  3039. symownerdef:=tabstractrecorddef(symownerdef.genericdef);
  3040. end;
  3041. if assigned(contextobjdef) and (df_specialization in contextobjdef.defoptions) then
  3042. begin
  3043. if not assigned(contextobjdef.genericdef) then
  3044. internalerror(2024041202);
  3045. if not (contextobjdef.genericdef.typ in [objectdef,recorddef]) then
  3046. internalerror(2024020902);
  3047. orgcontextobjdef:=contextobjdef;
  3048. contextobjdef:=tabstractrecorddef(contextobjdef.genericdef);
  3049. end;
  3050. if assigned(current_structdef) and (df_specialization in current_structdef.defoptions) then
  3051. begin
  3052. if not assigned(current_structdef.genericdef) then
  3053. internalerror(2024041203);
  3054. if not (current_structdef.genericdef.typ in [objectdef,recorddef]) then
  3055. internalerror(2024030903);
  3056. curstruct:=tabstractrecorddef(current_structdef.genericdef)
  3057. end
  3058. else
  3059. curstruct:=current_structdef;
  3060. { specializations might belong to a localsymtable or parasymtable }
  3061. nonlocalst:=symownerdef.owner;
  3062. if tstoreddef(symownerdef).is_specialization then
  3063. while nonlocalst.symtabletype in [localsymtable,parasymtable] do
  3064. nonlocalst:=nonlocalst.defowner.owner;
  3065. isspezproc:=false;
  3066. if assigned(current_procinfo) then
  3067. begin
  3068. if current_procinfo.procdef.is_specialization and
  3069. assigned(current_procinfo.procdef.struct) then
  3070. isspezproc:=true;
  3071. end;
  3072. case symvisibility of
  3073. vis_private :
  3074. begin
  3075. { private symbols are allowed when we are in the same
  3076. module as they are defined }
  3077. result:=check_strict_private or
  3078. (
  3079. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  3080. is_current_unit(nonlocalst)
  3081. ) or
  3082. ( // the case of specialize inside the generic declaration and nested types
  3083. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  3084. (
  3085. assigned(curstruct) and
  3086. (
  3087. (curstruct=symownerdef) or
  3088. (curstruct.owner.moduleid=symownerdef.symtable.moduleid)
  3089. )
  3090. ) or
  3091. (
  3092. not assigned(curstruct) and
  3093. (symownerdef.owner.iscurrentunit)
  3094. ) or
  3095. { access from a generic method that belongs to the class
  3096. but that is specialized elsewere }
  3097. (
  3098. isspezproc and
  3099. (current_procinfo.procdef.struct=curstruct)
  3100. ) or
  3101. { specializations may access private symbols that their
  3102. generics are allowed to access }
  3103. (
  3104. assigned(curstruct) and
  3105. (df_specialization in curstruct.defoptions) and
  3106. (symst.moduleid=curstruct.genericdef.owner.moduleid)
  3107. )
  3108. );
  3109. end;
  3110. vis_strictprivate :
  3111. begin
  3112. result:=check_strict_private;
  3113. end;
  3114. vis_strictprotected :
  3115. begin
  3116. result:=check_strict_protected;
  3117. end;
  3118. vis_protected :
  3119. begin
  3120. { protected symbols are visible in the module that defines them and
  3121. also visible to related objects. The related object must be defined
  3122. in the current module }
  3123. result:=check_strict_protected or
  3124. (
  3125. (
  3126. (nonlocalst.symtabletype in [globalsymtable,staticsymtable]) and
  3127. is_current_unit(nonlocalst)
  3128. ) or
  3129. (
  3130. { context object is inside the current unit and related to
  3131. the symbol owner (specialization case) }
  3132. assigned(contextobjdef) and
  3133. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable,localsymtable]) and
  3134. is_current_unit(contextobjdef.owner) and
  3135. def_is_related(contextobjdef,symownerdef)
  3136. ) or
  3137. (
  3138. { context object is inside the current unit and related to
  3139. the symbol owner (non-specialization case) }
  3140. assigned(orgcontextobjdef) and
  3141. (
  3142. (orgcontextobjdef<>contextobjdef) or
  3143. (orgsymownerdef<>symownerdef)
  3144. ) and
  3145. (orgcontextobjdef.owner.symtabletype in [globalsymtable,staticsymtable,ObjectSymtable,recordsymtable,localsymtable]) and
  3146. is_current_unit(orgcontextobjdef.owner) and
  3147. def_is_related(orgcontextobjdef,orgsymownerdef)
  3148. ) or
  3149. ( // the case of specialize inside the generic declaration and nested types
  3150. (nonlocalst.symtabletype in [objectsymtable,recordsymtable]) and
  3151. (
  3152. assigned(curstruct) and
  3153. (
  3154. (curstruct=symownerdef) or
  3155. (curstruct.owner.moduleid=symownerdef.symtable.moduleid)
  3156. )
  3157. ) or
  3158. (
  3159. not assigned(curstruct) and
  3160. (symownerdef.owner.iscurrentunit)
  3161. )
  3162. ) or
  3163. { access from a generic method that belongs to the class
  3164. but that is specialized elsewere }
  3165. (
  3166. isspezproc and
  3167. (current_procinfo.procdef.struct=curstruct)
  3168. ) or
  3169. { specializations may access private symbols that their
  3170. generics are allowed to access }
  3171. (
  3172. assigned(curstruct) and
  3173. (df_specialization in curstruct.defoptions) and
  3174. (symst.moduleid=curstruct.genericdef.owner.moduleid)
  3175. )
  3176. );
  3177. end;
  3178. vis_public,
  3179. vis_published :
  3180. result:=true;
  3181. else
  3182. internalerror(2019050702);
  3183. end;
  3184. if not result then
  3185. begin
  3186. { capturers have access to anything as we assume checks were done
  3187. before the procdef was inserted into the capturer }
  3188. result:=assigned(curstruct) and
  3189. (curstruct.typ=objectdef) and
  3190. (oo_is_capturer in tobjectdef(curstruct).objectoptions);
  3191. end;
  3192. end;
  3193. function is_visible_for_object(pd:tprocdef;contextobjdef:tabstractrecorddef):boolean;
  3194. begin
  3195. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  3196. end;
  3197. function is_visible_for_object(sym:tsym;contextobjdef:tabstractrecorddef):boolean;
  3198. var
  3199. i : longint;
  3200. pd : tprocdef;
  3201. begin
  3202. if sym.typ=procsym then
  3203. begin
  3204. result:=false;
  3205. { A procsym is visible, when there is at least one of the procdefs visible }
  3206. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  3207. begin
  3208. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  3209. if (pd.owner=sym.owner) and
  3210. is_visible_for_object(pd,contextobjdef) then
  3211. begin
  3212. result:=true;
  3213. exit;
  3214. end;
  3215. end;
  3216. { check dummy sym visbility by following associated procsyms }
  3217. if tprocsym(sym).could_be_implicitly_specialized then
  3218. begin
  3219. for i:=0 to tprocsym(sym).genprocsymovlds.count-1 do
  3220. if is_visible_for_object(tsym(tprocsym(sym).genprocsymovlds[i]),contextobjdef) then
  3221. begin
  3222. result:=true;
  3223. exit;
  3224. end;
  3225. end;
  3226. if (tprocsym(sym).procdeflist.count=0) and (sp_generic_dummy in tprocsym(sym).symoptions) then
  3227. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  3228. end
  3229. else
  3230. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  3231. end;
  3232. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3233. begin
  3234. case s[1] of
  3235. internal_macro_escape_unit_namespace_name:
  3236. result:=searchsym_maybe_with_symoption(copy(s,2,length(s)-1),srsym,srsymtable,[ssf_unit_or_namespace_only],sp_none)
  3237. else
  3238. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[],sp_none);
  3239. end
  3240. end;
  3241. function searchsym_with_flags(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3242. begin
  3243. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,flags,sp_none);
  3244. end;
  3245. function searchsym_maybe_with_symoption(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags;option:tsymoption):boolean;
  3246. var
  3247. hashedid: THashedIDString;
  3248. contextstructdef: tabstractrecorddef;
  3249. stackitem: psymtablestackitem;
  3250. begin
  3251. result:=false;
  3252. hashedid.id:=s;
  3253. stackitem:=symtablestack.stack;
  3254. while assigned(stackitem) do
  3255. begin
  3256. srsymtable:=stackitem^.symtable;
  3257. if not(ssf_unit_or_namespace_only in flags) and
  3258. (srsymtable.symtabletype=objectsymtable) then
  3259. begin
  3260. { TODO : implement the search for an option in classes as well }
  3261. if ssf_search_option in flags then
  3262. begin
  3263. result:=false;
  3264. exit;
  3265. end;
  3266. if searchsym_in_class(tobjectdef(srsymtable.defowner),tobjectdef(srsymtable.defowner),s,srsym,srsymtable,flags+[ssf_search_helper]) then
  3267. begin
  3268. result:=true;
  3269. exit;
  3270. end;
  3271. end
  3272. else if not((srsymtable.symtabletype=withsymtable) and assigned(srsymtable.defowner) and
  3273. (srsymtable.defowner.typ=undefineddef)) then
  3274. begin
  3275. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3276. { First check if it is a unit/namespace symbol.
  3277. They are visible only if they are from the current unit or
  3278. unit of generic of currently processed specialization. }
  3279. if assigned(srsym) and
  3280. (not(ssf_unit_or_namespace_only in flags) or
  3281. (srsym.typ in [unitsym,namespacesym])) and
  3282. (
  3283. not(srsym.typ in [unitsym,namespacesym]) or
  3284. srsymtable.iscurrentunit or
  3285. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid)) or
  3286. (
  3287. assigned(current_procinfo) and
  3288. (df_specialization in current_procinfo.procdef.defoptions) and
  3289. (current_procinfo.procdef.genericdef.owner.moduleid=srsymtable.moduleid)
  3290. )
  3291. ) and
  3292. (not (ssf_search_option in flags) or (option in srsym.symoptions))then
  3293. begin
  3294. { use the class from withsymtable only when it is
  3295. defined in this unit }
  3296. if (srsymtable.symtabletype=withsymtable) and
  3297. assigned(srsymtable.defowner) and
  3298. (srsymtable.defowner.typ in [recorddef,objectdef]) and
  3299. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable,objectsymtable,recordsymtable]) and
  3300. (srsymtable.defowner.owner.iscurrentunit) then
  3301. contextstructdef:=tabstractrecorddef(srsymtable.defowner)
  3302. else
  3303. contextstructdef:=current_structdef;
  3304. if not(srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or
  3305. is_visible_for_object(srsym,contextstructdef) then
  3306. begin
  3307. { we need to know if a procedure references symbols
  3308. in the static symtable, because then it can't be
  3309. inlined from outside this unit }
  3310. if assigned(current_procinfo) and
  3311. (srsym.owner.symtabletype=staticsymtable) then
  3312. include(current_procinfo.flags,pi_uses_static_symtable);
  3313. if not (ssf_no_addsymref in flags) then
  3314. addsymref(srsym);
  3315. result:=true;
  3316. exit;
  3317. end;
  3318. end;
  3319. end;
  3320. stackitem:=stackitem^.next;
  3321. end;
  3322. srsym:=nil;
  3323. srsymtable:=nil;
  3324. end;
  3325. function searchsym_with_symoption(const s: TIDString;out srsym:tsym;out
  3326. srsymtable:TSymtable;option:tsymoption):boolean;
  3327. begin
  3328. result:=searchsym_maybe_with_symoption(s,srsym,srsymtable,[ssf_search_option],option);
  3329. end;
  3330. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3331. var
  3332. hashedid : THashedIDString;
  3333. stackitem : psymtablestackitem;
  3334. classh : tobjectdef;
  3335. begin
  3336. result:=false;
  3337. hashedid.id:=s;
  3338. stackitem:=symtablestack.stack;
  3339. while assigned(stackitem) do
  3340. begin
  3341. {
  3342. It is not possible to have type symbols in:
  3343. parameters
  3344. Exception are classes, objects, records, generic definitions and specializations
  3345. that have the parameterized types inserted in the symtable.
  3346. }
  3347. srsymtable:=stackitem^.symtable;
  3348. if (srsymtable.symtabletype=ObjectSymtable) then
  3349. begin
  3350. classh:=tobjectdef(srsymtable.defowner);
  3351. while assigned(classh) do
  3352. begin
  3353. srsymtable:=classh.symtable;
  3354. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3355. if assigned(srsym) and
  3356. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3357. is_visible_for_object(srsym,current_structdef) then
  3358. begin
  3359. addsymref(srsym);
  3360. result:=true;
  3361. exit;
  3362. end;
  3363. classh:=classh.childof;
  3364. end;
  3365. end
  3366. else
  3367. begin
  3368. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3369. if assigned(srsym) and
  3370. (
  3371. not(srsym.typ in [unitsym,namespacesym]) or
  3372. srsymtable.iscurrentunit or
  3373. (assigned(current_specializedef)and(current_specializedef.genericdef.owner.moduleid=srsymtable.moduleid))
  3374. ) and
  3375. not(srsym.typ in [fieldvarsym,paravarsym,propertysym,procsym,labelsym]) and
  3376. (not (srsym.owner.symtabletype in [objectsymtable,recordsymtable]) or is_visible_for_object(srsym,current_structdef)) then
  3377. begin
  3378. { we need to know if a procedure references symbols
  3379. in the static symtable, because then it can't be
  3380. inlined from outside this unit }
  3381. if assigned(current_procinfo) and
  3382. (srsym.owner.symtabletype=staticsymtable) then
  3383. include(current_procinfo.flags,pi_uses_static_symtable);
  3384. addsymref(srsym);
  3385. result:=true;
  3386. exit;
  3387. end;
  3388. end;
  3389. stackitem:=stackitem^.next;
  3390. end;
  3391. result:=false;
  3392. srsym:=nil;
  3393. srsymtable:=nil;
  3394. end;
  3395. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3396. var
  3397. pmod : tmodule;
  3398. begin
  3399. result:=false;
  3400. if not assigned(pm) then exit;
  3401. pmod:=tmodule(pm);
  3402. if assigned(pmod.globalsymtable) then
  3403. begin
  3404. srsym:=tsym(pmod.globalsymtable.Find(s));
  3405. if assigned(srsym) then
  3406. begin
  3407. srsymtable:=pmod.globalsymtable;
  3408. addsymref(srsym);
  3409. result:=true;
  3410. exit;
  3411. end;
  3412. end;
  3413. { If the module is the current unit we also need
  3414. to search the local symtable }
  3415. if (pmod=current_module) and
  3416. assigned(pmod.localsymtable) then
  3417. begin
  3418. srsym:=tsym(pmod.localsymtable.Find(s));
  3419. if assigned(srsym) then
  3420. begin
  3421. srsymtable:=pmod.localsymtable;
  3422. addsymref(srsym);
  3423. result:=true;
  3424. exit;
  3425. end;
  3426. end;
  3427. srsym:=nil;
  3428. srsymtable:=nil;
  3429. end;
  3430. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  3431. var
  3432. stackitem : psymtablestackitem;
  3433. begin
  3434. result:=false;
  3435. stackitem:=symtablestack.stack;
  3436. while assigned(stackitem) do
  3437. begin
  3438. srsymtable:=stackitem^.symtable;
  3439. if (srsymtable.symtabletype=globalsymtable) and
  3440. (srsymtable.name^=unitname) then
  3441. begin
  3442. srsym:=tsym(srsymtable.find(symname));
  3443. if not assigned(srsym) then
  3444. break;
  3445. result:=true;
  3446. exit;
  3447. end;
  3448. stackitem:=stackitem^.next;
  3449. end;
  3450. { If the module is the current unit we also need
  3451. to search the local symtable }
  3452. if assigned(current_module.localsymtable) and
  3453. (current_module.localsymtable.name^=unitname) then
  3454. begin
  3455. srsymtable:=current_module.localsymtable;
  3456. srsym:=tsym(srsymtable.find(symname));
  3457. if assigned(srsym) then
  3458. begin
  3459. result:=true;
  3460. exit;
  3461. end;
  3462. end;
  3463. end;
  3464. function maybe_find_real_class_definition(pd: tdef; erroronfailure: boolean): tdef;
  3465. begin
  3466. result:=pd;
  3467. if pd.typ<>objectdef then
  3468. exit;
  3469. result:=find_real_class_definition(tobjectdef(pd),erroronfailure);
  3470. end;
  3471. function find_real_class_definition(pd: tobjectdef; erroronfailure: boolean): tobjectdef;
  3472. var
  3473. hashedid : THashedIDString;
  3474. stackitem : psymtablestackitem;
  3475. srsymtable : tsymtable;
  3476. srsym : tsym;
  3477. formalname,
  3478. foundname : shortstring;
  3479. formalnameptr,
  3480. foundnameptr: pshortstring;
  3481. begin
  3482. while pd.is_unique_objpasdef do
  3483. begin
  3484. pd:=pd.childof;
  3485. end;
  3486. { not a formal definition -> return it }
  3487. if not(oo_is_formal in pd.objectoptions) then
  3488. begin
  3489. result:=pd;
  3490. exit;
  3491. end;
  3492. hashedid.id:=pd.typesym.name;
  3493. stackitem:=symtablestack.stack;
  3494. while assigned(stackitem) do
  3495. begin
  3496. srsymtable:=stackitem^.symtable;
  3497. { ObjC classes can't appear in generics or as nested class
  3498. definitions. Java classes can. }
  3499. if not(srsymtable.symtabletype in [recordsymtable,parasymtable]) or
  3500. (is_java_class_or_interface(pd) and
  3501. (srsymtable.symtabletype=ObjectSymtable)) then
  3502. begin
  3503. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3504. if assigned(srsym) and
  3505. (srsym.typ=typesym) and
  3506. (ttypesym(srsym).typedef.typ=objectdef) and
  3507. (tobjectdef(ttypesym(srsym).typedef).objecttype=pd.objecttype) and
  3508. not(oo_is_formal in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3509. begin
  3510. if not(oo_is_forward in tobjectdef(ttypesym(srsym).typedef).objectoptions) then
  3511. begin
  3512. { the external name for the formal and the real
  3513. definition must match }
  3514. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) or
  3515. assigned(pd.import_lib) then
  3516. begin
  3517. if assigned(pd.import_lib) then
  3518. formalname:=pd.import_lib^+'.'
  3519. else
  3520. formalname:='';
  3521. formalname:=formalname+pd.objextname^;
  3522. if assigned(tobjectdef(ttypesym(srsym).typedef).import_lib) then
  3523. foundname:=tobjectdef(ttypesym(srsym).typedef).import_lib^+'.'
  3524. else
  3525. foundname:='';
  3526. foundname:=foundname+tobjectdef(ttypesym(srsym).typedef).objextname^;
  3527. formalnameptr:=@formalname;
  3528. foundnameptr:=@foundname;
  3529. end
  3530. else
  3531. begin
  3532. formalnameptr:=pd.objextname;
  3533. foundnameptr:=tobjectdef(ttypesym(srsym).typedef).objextname;
  3534. end;
  3535. if foundnameptr^<>formalnameptr^ then
  3536. begin
  3537. MessagePos2(pd.typesym.fileinfo,sym_e_external_class_name_mismatch1,formalnameptr^,pd.typename);
  3538. MessagePos1(srsym.fileinfo,sym_e_external_class_name_mismatch2,foundnameptr^);
  3539. end;
  3540. end;
  3541. result:=tobjectdef(ttypesym(srsym).typedef);
  3542. if assigned(current_procinfo) and
  3543. (srsym.owner.symtabletype=staticsymtable) then
  3544. include(current_procinfo.flags,pi_uses_static_symtable);
  3545. addsymref(srsym);
  3546. exit;
  3547. end;
  3548. end;
  3549. stackitem:=stackitem^.next;
  3550. end;
  3551. { nothing found: optionally give an error and return the original
  3552. (empty) one }
  3553. if erroronfailure then
  3554. Message1(sym_e_formal_class_not_resolved,pd.objrealname^);
  3555. result:=pd;
  3556. end;
  3557. function searchsym_in_class(classh: tobjectdef;contextclassh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3558. var
  3559. hashedid : THashedIDString;
  3560. orgclass : tobjectdef;
  3561. i : longint;
  3562. begin
  3563. orgclass:=classh;
  3564. { in case this is a formal class, first find the real definition }
  3565. if assigned(classh) then
  3566. begin
  3567. if (oo_is_formal in classh.objectoptions) then
  3568. classh:=find_real_class_definition(classh,true);
  3569. { The contextclassh is used for visibility. The classh must be equal to
  3570. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  3571. parent or a class helper. }
  3572. if not (def_is_related(contextclassh,classh) or
  3573. (is_classhelper(contextclassh) and
  3574. assigned(tobjectdef(contextclassh).extendeddef) and
  3575. (tobjectdef(contextclassh).extendeddef.typ=objectdef) and
  3576. def_is_related(tobjectdef(contextclassh).extendeddef,classh))) then
  3577. internalerror(200811161);
  3578. end;
  3579. result:=false;
  3580. hashedid.id:=s;
  3581. { an Objective-C protocol or Java interface can inherit from multiple
  3582. other protocols/interfaces -> use ImplementedInterfaces instead }
  3583. if is_objcprotocol(classh) or
  3584. is_javainterface(classh) then
  3585. begin
  3586. srsymtable:=classh.symtable;
  3587. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3588. if assigned(srsym) and
  3589. is_visible_for_object(srsym,contextclassh) then
  3590. begin
  3591. if not (ssf_no_addsymref in flags) then
  3592. addsymref(srsym);
  3593. result:=true;
  3594. exit;
  3595. end;
  3596. for i:=0 to classh.ImplementedInterfaces.count-1 do
  3597. begin
  3598. if searchsym_in_class(TImplementedInterface(classh.ImplementedInterfaces[i]).intfdef,contextclassh,s,srsym,srsymtable,flags-[ssf_search_helper]) then
  3599. begin
  3600. result:=true;
  3601. exit;
  3602. end;
  3603. end;
  3604. end
  3605. else
  3606. if is_objectpascal_helper(classh) then
  3607. begin
  3608. { helpers have their own obscure search logic... }
  3609. result:=searchsym_in_helper(classh,tobjectdef(contextclassh),s,srsym,srsymtable,flags-[ssf_has_inherited]);
  3610. if result then
  3611. exit;
  3612. end
  3613. else
  3614. begin
  3615. while assigned(classh) do
  3616. begin
  3617. { search for a class helper method first if this is an Object
  3618. Pascal class and we haven't yet found a helper symbol }
  3619. if (classh.objecttype in objecttypes_with_helpers) and
  3620. (ssf_search_helper in flags) then
  3621. begin
  3622. result:=search_objectpascal_helper(classh,contextclassh,s,srsym,srsymtable);
  3623. { an eventual overload inside the extended type's hierarchy
  3624. will be found by tcallcandidates }
  3625. if result then
  3626. exit;
  3627. end;
  3628. srsymtable:=classh.symtable;
  3629. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3630. if assigned(srsym) and
  3631. is_visible_for_object(srsym,contextclassh) then
  3632. begin
  3633. if not (ssf_no_addsymref in flags) then
  3634. addsymref(srsym);
  3635. result:=true;
  3636. exit;
  3637. end;
  3638. classh:=classh.childof;
  3639. end;
  3640. end;
  3641. if is_objcclass(orgclass) then
  3642. result:=search_objc_helper(orgclass,s,srsym,srsymtable)
  3643. else
  3644. begin
  3645. srsym:=nil;
  3646. srsymtable:=nil;
  3647. end;
  3648. end;
  3649. function searchsym_in_record(recordh:tabstractrecorddef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3650. var
  3651. hashedid : THashedIDString;
  3652. begin
  3653. result:=false;
  3654. hashedid.id:=s;
  3655. { search for a record helper method first }
  3656. result:=search_objectpascal_helper(recordh,recordh,s,srsym,srsymtable);
  3657. if result then
  3658. { an eventual overload inside the extended type's hierarchy
  3659. will be found by tcallcandidates }
  3660. exit;
  3661. srsymtable:=recordh.symtable;
  3662. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3663. if assigned(srsym) and is_visible_for_object(srsym,recordh) then
  3664. begin
  3665. addsymref(srsym);
  3666. result:=true;
  3667. exit;
  3668. end;
  3669. srsym:=nil;
  3670. srsymtable:=nil;
  3671. end;
  3672. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3673. var
  3674. def : tdef;
  3675. i : longint;
  3676. begin
  3677. { in case this is a formal class, first find the real definition }
  3678. if assigned(classh) and
  3679. (oo_is_formal in classh.objectoptions) then
  3680. classh:=find_real_class_definition(classh,true);
  3681. result:=false;
  3682. def:=nil;
  3683. while assigned(classh) do
  3684. begin
  3685. for i:=0 to classh.symtable.DefList.Count-1 do
  3686. begin
  3687. def:=tstoreddef(classh.symtable.DefList[i]);
  3688. { Find also all hidden private methods to
  3689. be compatible with delphi, see tw6203 (PFV) }
  3690. if (def.typ=procdef) and
  3691. (po_msgint in tprocdef(def).procoptions) and
  3692. (tprocdef(def).messageinf.i=msgid) then
  3693. begin
  3694. srdef:=def;
  3695. srsym:=tprocdef(def).procsym;
  3696. srsymtable:=classh.symtable;
  3697. addsymref(srsym);
  3698. result:=true;
  3699. exit;
  3700. end;
  3701. end;
  3702. classh:=classh.childof;
  3703. end;
  3704. srdef:=nil;
  3705. srsym:=nil;
  3706. srsymtable:=nil;
  3707. end;
  3708. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  3709. var
  3710. def : tdef;
  3711. i : longint;
  3712. begin
  3713. { in case this is a formal class, first find the real definition }
  3714. if assigned(classh) and
  3715. (oo_is_formal in classh.objectoptions) then
  3716. classh:=find_real_class_definition(classh,true);
  3717. result:=false;
  3718. def:=nil;
  3719. while assigned(classh) do
  3720. begin
  3721. for i:=0 to classh.symtable.DefList.Count-1 do
  3722. begin
  3723. def:=tstoreddef(classh.symtable.DefList[i]);
  3724. { Find also all hidden private methods to
  3725. be compatible with delphi, see tw6203 (PFV) }
  3726. if (def.typ=procdef) and
  3727. (po_msgstr in tprocdef(def).procoptions) and
  3728. (tprocdef(def).messageinf.str^=s) then
  3729. begin
  3730. srsym:=tprocdef(def).procsym;
  3731. srsymtable:=classh.symtable;
  3732. addsymref(srsym);
  3733. result:=true;
  3734. exit;
  3735. end;
  3736. end;
  3737. classh:=classh.childof;
  3738. end;
  3739. srsym:=nil;
  3740. srsymtable:=nil;
  3741. end;
  3742. function search_best_objectpascal_helper(const name: string;pd : tdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable):boolean;forward;
  3743. function searchsym_in_helper(classh,contextclassh:tobjectdef;const s: TIDString;out srsym:tsym;out srsymtable:TSymtable;flags:tsymbol_search_flags):boolean;
  3744. var
  3745. hashedid : THashedIDString;
  3746. parentclassh : tobjectdef;
  3747. begin
  3748. result:=false;
  3749. if not is_objectpascal_helper(classh) then
  3750. Internalerror(2011030101);
  3751. hashedid.id:=s;
  3752. { in a helper things are a bit more complex:
  3753. 1. search the symbol in the helper (if not "inherited")
  3754. 2. search the symbol in the extended type
  3755. 3. search the symbol in the parent helpers
  3756. 4. only classes: search the symbol in the parents of the extended type
  3757. }
  3758. if not (ssf_has_inherited in flags) then
  3759. begin
  3760. { search in the helper itself }
  3761. srsymtable:=classh.symtable;
  3762. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3763. if assigned(srsym) and
  3764. is_visible_for_object(srsym,contextclassh) then
  3765. begin
  3766. if not (ssf_no_addsymref in flags) then
  3767. addsymref(srsym);
  3768. result:=true;
  3769. exit;
  3770. end;
  3771. end;
  3772. { now search in the extended type itself }
  3773. { Note: the extendeddef might be Nil if we are currently parsing the
  3774. extended type itself and the identifier was not found }
  3775. if assigned(classh.extendeddef) and (classh.extendeddef.typ in [recorddef,objectdef]) then
  3776. begin
  3777. srsymtable:=tabstractrecorddef(classh.extendeddef).symtable;
  3778. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3779. if assigned(srsym) and
  3780. is_visible_for_object(srsym,contextclassh) then
  3781. begin
  3782. if not (ssf_no_addsymref in flags) then
  3783. addsymref(srsym);
  3784. result:=true;
  3785. exit;
  3786. end;
  3787. end;
  3788. { now search in the parent helpers }
  3789. parentclassh:=classh.childof;
  3790. while assigned(parentclassh) do
  3791. begin
  3792. srsymtable:=parentclassh.symtable;
  3793. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  3794. if assigned(srsym) and
  3795. is_visible_for_object(srsym,contextclassh) then
  3796. begin
  3797. if not (ssf_no_addsymref in flags) then
  3798. addsymref(srsym);
  3799. result:=true;
  3800. exit;
  3801. end;
  3802. parentclassh:=parentclassh.childof;
  3803. end;
  3804. { now search in the parents of the extended class (with helpers!) }
  3805. if is_class(classh.extendeddef) then
  3806. begin
  3807. result:=searchsym_in_class(tobjectdef(classh.extendeddef).childof,contextclassh,s,srsym,srsymtable,flags+[ssf_search_helper]);
  3808. { addsymref is already called by searchsym_in_class }
  3809. if result then
  3810. exit;
  3811. end;
  3812. { now search all helpers using the extendeddef as the starting point }
  3813. if (m_multi_helpers in current_settings.modeswitches) and
  3814. (
  3815. (current_structdef<>classh) or
  3816. assigned(classh.extendeddef)
  3817. ) then
  3818. begin
  3819. { this is only allowed if classh is currently parsed }
  3820. if not assigned(classh.extendeddef) then
  3821. internalerror(2019110101);
  3822. result:=search_best_objectpascal_helper(s,classh.extendeddef,contextclassh,srsym,srsymtable);
  3823. end;
  3824. end;
  3825. function search_specific_assignment_operator(assignment_type:ttoken;from_def,to_def:Tdef):Tprocdef;
  3826. var
  3827. sym : Tprocsym;
  3828. hashedid : THashedIDString;
  3829. curreq,
  3830. besteq : tequaltype;
  3831. currpd,
  3832. bestpd : tprocdef;
  3833. stackitem : psymtablestackitem;
  3834. shortstringcount : longint;
  3835. isexplicit,
  3836. checkshortstring : boolean;
  3837. begin
  3838. hashedid.id:=overloaded_names[assignment_type];
  3839. besteq:=te_incompatible;
  3840. bestpd:=nil;
  3841. stackitem:=symtablestack.stack;
  3842. { special handling for assignments to shortstrings with a specific length:
  3843. - if we get an operator to ShortString we use that
  3844. - if we get only a single String[x] operator we use that
  3845. - otherwise it's a nogo }
  3846. isexplicit:=assignment_type=_OP_EXPLICIT;
  3847. shortstringcount:=0;
  3848. checkshortstring:=not isexplicit and is_shortstring(to_def) and (tstringdef(to_def).len<>255);
  3849. while assigned(stackitem) do
  3850. begin
  3851. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3852. if sym<>nil then
  3853. begin
  3854. if sym.typ<>procsym then
  3855. internalerror(200402031);
  3856. { if the source type is an alias then this is only the second choice,
  3857. if you mess with this code, check tw4093 }
  3858. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq,isexplicit);
  3859. { we found a ShortString overload, use that and be done }
  3860. if checkshortstring and
  3861. assigned(currpd) and
  3862. is_shortstring(currpd.returndef) and
  3863. (tstringdef(currpd.returndef).len=255) then
  3864. begin
  3865. besteq:=curreq;
  3866. bestpd:=currpd;
  3867. break;
  3868. end;
  3869. { independently of the operator being better count if we encountered
  3870. multpile String[x] operators }
  3871. if checkshortstring and assigned(currpd) and is_shortstring(currpd.returndef) then
  3872. inc(shortstringcount);
  3873. if curreq>besteq then
  3874. begin
  3875. besteq:=curreq;
  3876. bestpd:=currpd;
  3877. { don't stop searching if we have a String[x] operator cause
  3878. we might find a ShortString one or multiple ones (which
  3879. leads to no operator use) }
  3880. if (besteq=te_exact) and not checkshortstring then
  3881. break;
  3882. end;
  3883. end;
  3884. stackitem:=stackitem^.next;
  3885. end;
  3886. if checkshortstring and (shortstringcount>1) then
  3887. bestpd:=nil;
  3888. result:=bestpd;
  3889. end;
  3890. function search_assignment_operator(from_def,to_def:Tdef;explicit:boolean):Tprocdef;
  3891. begin
  3892. { search record/object symtable first for a suitable operator }
  3893. if from_def.typ in [recorddef,objectdef] then
  3894. symtablestack.push(tabstractrecorddef(from_def).symtable);
  3895. if to_def.typ in [recorddef,objectdef] then
  3896. symtablestack.push(tabstractrecorddef(to_def).symtable);
  3897. { if type conversion is explicit then search first for explicit
  3898. operator overload and if not found then use implicit operator }
  3899. if explicit then
  3900. result:=search_specific_assignment_operator(_OP_EXPLICIT,from_def,to_def)
  3901. else
  3902. result:=nil;
  3903. if result=nil then
  3904. result:=search_specific_assignment_operator(_ASSIGNMENT,from_def,to_def);
  3905. { restore symtable stack }
  3906. if to_def.typ in [recorddef,objectdef] then
  3907. symtablestack.pop(tabstractrecorddef(to_def).symtable);
  3908. if from_def.typ in [recorddef,objectdef] then
  3909. symtablestack.pop(tabstractrecorddef(from_def).symtable);
  3910. end;
  3911. function search_enumerator_operator(from_def,to_def:Tdef): Tprocdef;
  3912. var
  3913. sym : Tprocsym;
  3914. hashedid : THashedIDString;
  3915. curreq,
  3916. besteq : tequaltype;
  3917. currpd,
  3918. bestpd : tprocdef;
  3919. stackitem : psymtablestackitem;
  3920. begin
  3921. hashedid.id:='enumerator';
  3922. besteq:=te_incompatible;
  3923. bestpd:=nil;
  3924. stackitem:=symtablestack.stack;
  3925. while assigned(stackitem) do
  3926. begin
  3927. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  3928. if sym<>nil then
  3929. begin
  3930. if sym.typ<>procsym then
  3931. internalerror(200910241);
  3932. { if the source type is an alias then this is only the second choice,
  3933. if you mess with this code, check tw4093 }
  3934. currpd:=sym.find_procdef_enumerator_operator(from_def,to_def,curreq);
  3935. if curreq>besteq then
  3936. begin
  3937. besteq:=curreq;
  3938. bestpd:=currpd;
  3939. if (besteq=te_exact) then
  3940. break;
  3941. end;
  3942. end;
  3943. stackitem:=stackitem^.next;
  3944. end;
  3945. result:=bestpd;
  3946. end;
  3947. function search_management_operator(mop:tmanagementoperator;pd:Tdef):Tprocdef;
  3948. var
  3949. sym : Tprocsym;
  3950. hashedid : THashedIDString;
  3951. optoken: ttoken;
  3952. begin
  3953. optoken := managementoperator2tok[mop];
  3954. if (optoken<first_managment_operator) or
  3955. (optoken>last_managment_operator) then
  3956. internalerror(201602280);
  3957. hashedid.id:=overloaded_names[optoken];
  3958. if not (pd.typ in [recorddef]) then
  3959. internalerror(201602281);
  3960. sym:=Tprocsym(tabstractrecorddef(pd).symtable.FindWithHash(hashedid));
  3961. if sym<>nil then
  3962. begin
  3963. if sym.typ<>procsym then
  3964. internalerror(201602282);
  3965. result:=sym.find_procdef_bytype(potype_operator);
  3966. end
  3967. else
  3968. result:=nil;
  3969. end;
  3970. function search_system_type(const s: TIDString): ttypesym;
  3971. var
  3972. sym : tsym;
  3973. begin
  3974. check_systemunit_loaded;
  3975. sym:=tsym(systemunit.Find(s));
  3976. if not assigned(sym) or
  3977. (sym.typ<>typesym) then
  3978. message1(cg_f_unknown_system_type,s);
  3979. result:=ttypesym(sym);
  3980. end;
  3981. function try_search_system_type(const s: TIDString): ttypesym;
  3982. var
  3983. sym : tsym;
  3984. begin
  3985. check_systemunit_loaded;
  3986. sym:=tsym(systemunit.Find(s));
  3987. if not assigned(sym) then
  3988. result:=nil
  3989. else
  3990. begin
  3991. if sym.typ<>typesym then
  3992. message1(cg_f_unknown_system_type,s);
  3993. result:=ttypesym(sym);
  3994. end;
  3995. end;
  3996. function try_search_current_module_type(const s: TIDString): ttypesym;
  3997. var
  3998. found: boolean;
  3999. srsymtable: tsymtable;
  4000. srsym: tsym;
  4001. begin
  4002. if s[1]='$' then
  4003. found:=searchsym_in_module(current_module,copy(s,2,length(s)),srsym,srsymtable)
  4004. else
  4005. found:=searchsym_in_module(current_module,s,srsym,srsymtable);
  4006. if found then
  4007. begin
  4008. if (srsym.typ<>typesym) then
  4009. internalerror(2014091207);
  4010. result:=ttypesym(srsym);
  4011. end
  4012. else
  4013. result:=nil;
  4014. end;
  4015. function search_system_proc(const s: TIDString): tprocdef;
  4016. var
  4017. srsym: tsym;
  4018. begin
  4019. check_systemunit_loaded;
  4020. srsym:=tsym(systemunit.find(s));
  4021. if not assigned(srsym) and
  4022. (cs_compilesystem in current_settings.moduleswitches) then
  4023. srsym:=tsym(systemunit.Find(upper(s)));
  4024. if not assigned(srsym) or
  4025. (srsym.typ<>procsym) then
  4026. message1(cg_f_unknown_compilerproc,s);
  4027. result:=tprocdef(tprocsym(srsym).procdeflist[0]);
  4028. end;
  4029. function search_named_unit_globaltype(const unitname, typename: TIDString; throwerror: boolean): ttypesym;
  4030. var
  4031. srsymtable: tsymtable;
  4032. sym: tsym;
  4033. begin
  4034. sym:=nil;
  4035. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  4036. (sym.typ=typesym) then
  4037. begin
  4038. result:=ttypesym(sym);
  4039. exit;
  4040. end
  4041. else
  4042. begin
  4043. if throwerror then
  4044. message2(cg_f_unknown_type_in_unit,typename,unitname);
  4045. result:=nil;
  4046. end;
  4047. end;
  4048. function search_sym_in_helperdef(const s: string;classh : tobjectdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable): boolean;
  4049. var
  4050. hashedid : THashedIDString;
  4051. pdef : tprocdef;
  4052. i : integer;
  4053. begin
  4054. hashedid.id:=s;
  4055. result:=false;
  4056. repeat
  4057. srsymtable:=classh.symtable;
  4058. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  4059. if srsym<>nil then
  4060. begin
  4061. case srsym.typ of
  4062. procsym:
  4063. begin
  4064. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  4065. begin
  4066. pdef:=tprocdef(tprocsym(srsym).procdeflist[i]);
  4067. if not is_visible_for_object(pdef.owner,pdef.visibility,contextclassh) then
  4068. continue;
  4069. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  4070. srsymtable:=srsym.owner;
  4071. result:=true;
  4072. exit;
  4073. end;
  4074. if (sp_generic_dummy in tprocsym(srsym).symoptions) and
  4075. (tprocsym(srsym).procdeflist.count=0) and
  4076. is_visible_for_object(srsym.owner,srsym.visibility,contextclassh) then
  4077. begin
  4078. srsymtable:=srsym.owner;
  4079. result:=true;
  4080. exit;
  4081. end;
  4082. end;
  4083. typesym,
  4084. fieldvarsym,
  4085. constsym,
  4086. enumsym,
  4087. undefinedsym,
  4088. propertysym:
  4089. begin
  4090. result:=true;
  4091. exit;
  4092. end;
  4093. else
  4094. internalerror(2014041101);
  4095. end;
  4096. end;
  4097. { try the helper parent if available }
  4098. classh:=classh.childof;
  4099. until classh=nil;
  4100. end;
  4101. function get_objectpascal_helpers(pd : tdef):TFPObjectList;
  4102. var
  4103. s : TSymStr;
  4104. st : tsymtable;
  4105. begin
  4106. result:=nil;
  4107. { when there are no helpers active currently then we don't need to do
  4108. anything }
  4109. if current_module.extendeddefs.count=0 then
  4110. exit;
  4111. if (df_genconstraint in pd.defoptions) then
  4112. begin
  4113. { if we have a constraint for a class type or a single interface we
  4114. use that to resolve helpers at declaration time of the generic,
  4115. otherwise there can't be any helpers as the type isn't known yet }
  4116. if pd.typ=objectdef then
  4117. pd:=tobjectdef(pd).getparentdef
  4118. else
  4119. exit;
  4120. end;
  4121. { no helpers for anonymous types }
  4122. if ((pd.typ in [recorddef,objectdef]) and
  4123. (
  4124. not assigned(tabstractrecorddef(pd).objrealname) or
  4125. (tabstractrecorddef(pd).objrealname^='')
  4126. )
  4127. ) or
  4128. not assigned(pd.typesym) then
  4129. exit;
  4130. { if pd is defined inside a procedure we must not use make_mangledname
  4131. (as a helper may not be defined in a procedure this is no problem...)}
  4132. st:=pd.owner;
  4133. while st.symtabletype in [objectsymtable,recordsymtable] do
  4134. st:=st.defowner.owner;
  4135. if st.symtabletype=localsymtable then
  4136. exit;
  4137. { the mangled name is used as the key for tmodule.extendeddefs }
  4138. s:=generate_objectpascal_helper_key(pd);
  4139. result:=TFPObjectList(current_module.extendeddefs.Find(s));
  4140. end;
  4141. function search_best_objectpascal_helper(const name: string;pd : tdef;contextclassh : tabstractrecorddef;out srsym: tsym;out srsymtable: tsymtable):boolean;
  4142. var
  4143. list : TFPObjectList;
  4144. i : integer;
  4145. odef : tobjectdef;
  4146. begin
  4147. result:=false;
  4148. list:=get_objectpascal_helpers(pd);
  4149. if assigned(list) and (list.count>0) then
  4150. begin
  4151. i:=list.count-1;
  4152. repeat
  4153. odef:=tobjectdef(list[i]);
  4154. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  4155. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  4156. if result then
  4157. result:=search_sym_in_helperdef(name,odef,contextclassh,srsym,srsymtable);
  4158. dec(i);
  4159. until result or (i<0);
  4160. end;
  4161. end;
  4162. function search_last_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;out odef : tobjectdef):boolean;
  4163. var
  4164. list : TFPObjectList;
  4165. i : integer;
  4166. begin
  4167. result:=false;
  4168. odef:=nil;
  4169. list:=get_objectpascal_helpers(pd);
  4170. if assigned(list) and (list.count>0) then
  4171. begin
  4172. i:=list.count-1;
  4173. repeat
  4174. odef:=tobjectdef(list[list.count-1]);
  4175. result:=(odef.owner.symtabletype in [staticsymtable,globalsymtable]) or
  4176. is_visible_for_object(tobjectdef(list[i]).typesym,contextclassh);
  4177. dec(i);
  4178. until result or (i<0);
  4179. if not result then
  4180. { just to be sure that noone uses odef }
  4181. odef:=nil;
  4182. end;
  4183. end;
  4184. function search_objectpascal_helper(pd : tdef;contextclassh : tabstractrecorddef;const s: string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  4185. var
  4186. classh : tobjectdef;
  4187. begin
  4188. result:=false;
  4189. { if there is no class helper for the class then there is no need to
  4190. search further }
  4191. if m_multi_helpers in current_settings.modeswitches then
  4192. result:=search_best_objectpascal_helper(s,pd,contextclassh,srsym,srsymtable)
  4193. else
  4194. begin
  4195. if search_last_objectpascal_helper(pd,contextclassh,classh) and
  4196. search_sym_in_helperdef(s,classh,contextclassh,srsym,srsymtable) then
  4197. result:=true;
  4198. end;
  4199. if result then
  4200. begin
  4201. { we need to know if a procedure references symbols
  4202. in the static symtable, because then it can't be
  4203. inlined from outside this unit }
  4204. if (srsym.typ=procsym) and
  4205. assigned(current_procinfo) and
  4206. (srsym.owner.symtabletype=staticsymtable) then
  4207. include(current_procinfo.flags,pi_uses_static_symtable);
  4208. addsymref(srsym);
  4209. end
  4210. else
  4211. begin
  4212. srsym:=nil;
  4213. srsymtable:=nil;
  4214. end;
  4215. end;
  4216. function search_objc_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  4217. var
  4218. searchst : tsymtable;
  4219. searchsym : tsym;
  4220. hashedid : THashedIDString;
  4221. stackitem : psymtablestackitem;
  4222. i : longint;
  4223. founddefowner,
  4224. defowner : tobjectdef;
  4225. begin
  4226. hashedid.id:=class_helper_prefix+s;
  4227. stackitem:=symtablestack.stack;
  4228. result:=false;
  4229. srsym:=nil;
  4230. srsymtable:=nil;
  4231. founddefowner:=nil;
  4232. while assigned(stackitem) do
  4233. begin
  4234. searchst:=stackitem^.symtable;
  4235. searchsym:=tsym(searchst.FindWithHash(hashedid));
  4236. if assigned(searchsym) then
  4237. begin
  4238. if not(searchst.symtabletype in [globalsymtable,staticsymtable]) or
  4239. not(searchsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  4240. (searchsym.typ<>procsym) then
  4241. internalerror(2009111505);
  4242. { check whether this procsym includes a helper for this particular class }
  4243. for i:=0 to tprocsym(searchsym).procdeflist.count-1 do
  4244. begin
  4245. { does pd inherit from (or is the same as) the class
  4246. that this method's category extended?
  4247. Warning: this list contains both category and objcclass methods
  4248. (for id.randommethod), so only check category methods here
  4249. }
  4250. defowner:=tobjectdef(tprocdef(tprocsym(searchsym).procdeflist[i]).owner.defowner);
  4251. if is_objccategory(defowner) and
  4252. def_is_related(pd,defowner.childof) then
  4253. begin
  4254. { we need to know if a procedure references symbols
  4255. in the static symtable, because then it can't be
  4256. inlined from outside this unit }
  4257. if assigned(current_procinfo) and
  4258. (searchsym.owner.symtabletype=staticsymtable) then
  4259. include(current_procinfo.flags,pi_uses_static_symtable);
  4260. { Stop looking if this is a category that extends the specified
  4261. class itself. There might be other categories that extend this,
  4262. but that doesn't matter. If it extens a parent, keep looking
  4263. in case we find the symbol in a category that extends this class
  4264. (or a closer parent).
  4265. }
  4266. if not result or
  4267. def_is_related(defowner.childof,founddefowner) then
  4268. begin
  4269. founddefowner:=defowner.childof;
  4270. srsym:=tprocdef(tprocsym(searchsym).procdeflist[i]).procsym;
  4271. srsymtable:=srsym.owner;
  4272. result:=true;
  4273. if pd=founddefowner then
  4274. begin
  4275. addsymref(srsym);
  4276. exit;
  4277. end;
  4278. end;
  4279. end;
  4280. end;
  4281. end;
  4282. stackitem:=stackitem^.next;
  4283. end;
  4284. if result then
  4285. begin
  4286. addsymref(srsym);
  4287. exit;
  4288. end;
  4289. end;
  4290. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  4291. var
  4292. hashedid : THashedIDString;
  4293. stackitem : psymtablestackitem;
  4294. i : longint;
  4295. begin
  4296. hashedid.id:=class_helper_prefix+s;
  4297. stackitem:=symtablestack.stack;
  4298. while assigned(stackitem) do
  4299. begin
  4300. srsymtable:=stackitem^.symtable;
  4301. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  4302. if assigned(srsym) then
  4303. begin
  4304. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  4305. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  4306. (srsym.typ<>procsym) then
  4307. internalerror(2009112005);
  4308. { check whether this procsym includes a helper for this particular class }
  4309. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  4310. begin
  4311. { we need to know if a procedure references symbols
  4312. in the static symtable, because then it can't be
  4313. inlined from outside this unit }
  4314. if assigned(current_procinfo) and
  4315. (srsym.owner.symtabletype=staticsymtable) then
  4316. include(current_procinfo.flags,pi_uses_static_symtable);
  4317. { no need to keep looking. There might be other
  4318. methods with the same name, but that doesn't matter
  4319. as far as the basic procsym is concerned.
  4320. }
  4321. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  4322. { We need the symtable in which the classhelper-like sym
  4323. is located, not the objectdef. The reason is that the
  4324. callnode will climb the symtablestack until it encounters
  4325. this symtable to start looking for overloads (and it won't
  4326. find the objectsymtable in which this method sym is
  4327. located
  4328. srsymtable:=srsym.owner;
  4329. }
  4330. addsymref(srsym);
  4331. result:=true;
  4332. exit;
  4333. end;
  4334. end;
  4335. stackitem:=stackitem^.next;
  4336. end;
  4337. srsym:=nil;
  4338. srsymtable:=nil;
  4339. result:=false;
  4340. end;
  4341. function search_struct_member(pd : tabstractrecorddef;const s : string):tsym;
  4342. { searches n in symtable of pd and all anchestors }
  4343. var
  4344. srsymtable : tsymtable;
  4345. begin
  4346. { in case this is a formal class, first find the real definition }
  4347. if (oo_is_formal in pd.objectoptions) then
  4348. pd:=find_real_class_definition(tobjectdef(pd),true);
  4349. if search_objectpascal_helper(pd, pd, s, result, srsymtable) then
  4350. exit;
  4351. result:=search_struct_member_no_helper(pd,s);
  4352. if assigned(result) then
  4353. exit;
  4354. { not found, now look for class helpers }
  4355. if is_objcclass(pd) then
  4356. search_objc_helper(tobjectdef(pd),s,result,srsymtable)
  4357. end;
  4358. function search_struct_member_no_helper(pd: tabstractrecorddef; const s: string): tsym;
  4359. var
  4360. hashedid : THashedIDString;
  4361. srsym : tsym;
  4362. begin
  4363. hashedid.id:=s;
  4364. while assigned(pd) do
  4365. begin
  4366. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  4367. if assigned(srsym) then
  4368. begin
  4369. result:=srsym;
  4370. exit;
  4371. end;
  4372. if pd.typ=objectdef then
  4373. pd:=tobjectdef(pd).childof
  4374. else
  4375. pd:=nil;
  4376. end;
  4377. result:=nil;
  4378. end;
  4379. function search_macro(const s : string):tsym;
  4380. var
  4381. stackitem : psymtablestackitem;
  4382. hashedid : THashedIDString;
  4383. srsym : tsym;
  4384. begin
  4385. hashedid.id:=s;
  4386. { First search the localmacrosymtable before searching the
  4387. global macrosymtables from the units }
  4388. if assigned(current_module) then
  4389. begin
  4390. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  4391. if assigned(srsym) then
  4392. begin
  4393. result:= srsym;
  4394. exit;
  4395. end;
  4396. end;
  4397. stackitem:=macrosymtablestack.stack;
  4398. while assigned(stackitem) do
  4399. begin
  4400. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  4401. if assigned(srsym) then
  4402. begin
  4403. result:= srsym;
  4404. exit;
  4405. end;
  4406. stackitem:=stackitem^.next;
  4407. end;
  4408. result:= nil;
  4409. end;
  4410. function defined_macro(const s : string):boolean;
  4411. var
  4412. mac: tmacro;
  4413. begin
  4414. mac:=tmacro(search_macro(s));
  4415. if assigned(mac) then
  4416. begin
  4417. mac.is_used:=true;
  4418. defined_macro:=mac.defined;
  4419. end
  4420. else
  4421. defined_macro:=false;
  4422. end;
  4423. {****************************************************************************
  4424. Object Helpers
  4425. ****************************************************************************}
  4426. function search_default_property(pd : tabstractrecorddef) : tpropertysym;
  4427. { returns the default property of a class, searches also anchestors }
  4428. var
  4429. _defaultprop : tpropertysym;
  4430. helperpd : tobjectdef;
  4431. begin
  4432. _defaultprop:=nil;
  4433. { first search in helper's hierarchy }
  4434. if search_last_objectpascal_helper(pd,nil,helperpd) then
  4435. while assigned(helperpd) do
  4436. begin
  4437. helperpd.symtable.SymList.ForEachCall(@tstoredsymtable(helperpd.symtable).testfordefaultproperty,@_defaultprop);
  4438. if assigned(_defaultprop) then
  4439. break;
  4440. helperpd:=helperpd.childof;
  4441. end;
  4442. if assigned(_defaultprop) then
  4443. begin
  4444. search_default_property:=_defaultprop;
  4445. exit;
  4446. end;
  4447. { now search in the type's hierarchy itself }
  4448. while assigned(pd) do
  4449. begin
  4450. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  4451. if assigned(_defaultprop) then
  4452. break;
  4453. if (pd.typ=objectdef) then
  4454. pd:=tobjectdef(pd).childof
  4455. else
  4456. break;
  4457. end;
  4458. search_default_property:=_defaultprop;
  4459. end;
  4460. {****************************************************************************
  4461. Macro Helpers
  4462. ****************************************************************************}
  4463. procedure def_system_macro(const name : string);
  4464. var
  4465. mac : tmacro;
  4466. s: string;
  4467. begin
  4468. if name = '' then
  4469. internalerror(2004121202);
  4470. s:= upper(name);
  4471. mac:=tmacro(search_macro(s));
  4472. if not assigned(mac) then
  4473. begin
  4474. mac:=tmacro.create(s);
  4475. if assigned(current_module) then
  4476. current_module.localmacrosymtable.insertsym(mac)
  4477. else
  4478. initialmacrosymtable.insertsym(mac);
  4479. end;
  4480. Message1(parser_c_macro_defined,mac.name);
  4481. mac.defined:=true;
  4482. end;
  4483. procedure set_system_macro(const name, value : string);
  4484. var
  4485. mac : tmacro;
  4486. s: string;
  4487. begin
  4488. if name = '' then
  4489. internalerror(2004121203);
  4490. s:= upper(name);
  4491. mac:=tmacro(search_macro(s));
  4492. if not assigned(mac) then
  4493. begin
  4494. mac:=tmacro.create(s);
  4495. if assigned(current_module) then
  4496. current_module.localmacrosymtable.insertsym(mac)
  4497. else
  4498. initialmacrosymtable.insertsym(mac);
  4499. end
  4500. else
  4501. begin
  4502. mac.is_compiler_var:=false;
  4503. if assigned(mac.buftext) then
  4504. freemem(mac.buftext,mac.buflen);
  4505. end;
  4506. Message2(parser_c_macro_set_to,mac.name,value);
  4507. mac.buflen:=length(value);
  4508. getmem(mac.buftext,mac.buflen);
  4509. move(value[1],mac.buftext^,mac.buflen);
  4510. mac.defined:=true;
  4511. end;
  4512. procedure set_system_compvar(const name, value : string);
  4513. var
  4514. mac : tmacro;
  4515. s: string;
  4516. begin
  4517. if name = '' then
  4518. internalerror(2004121204);
  4519. s:= upper(name);
  4520. mac:=tmacro(search_macro(s));
  4521. if not assigned(mac) then
  4522. begin
  4523. mac:=tmacro.create(s);
  4524. mac.is_compiler_var:=true;
  4525. if assigned(current_module) then
  4526. current_module.localmacrosymtable.insertsym(mac)
  4527. else
  4528. initialmacrosymtable.insertsym(mac);
  4529. end
  4530. else
  4531. begin
  4532. mac.is_compiler_var:=true;
  4533. if assigned(mac.buftext) then
  4534. freemem(mac.buftext,mac.buflen);
  4535. end;
  4536. Message2(parser_c_macro_set_to,mac.name,value);
  4537. mac.buflen:=length(value);
  4538. getmem(mac.buftext,mac.buflen);
  4539. move(value[1],mac.buftext^,mac.buflen);
  4540. mac.defined:=true;
  4541. end;
  4542. procedure undef_system_macro(const name : string);
  4543. var
  4544. mac : tmacro;
  4545. s: string;
  4546. begin
  4547. if name = '' then
  4548. internalerror(2004121205);
  4549. s:= upper(name);
  4550. mac:=tmacro(search_macro(s));
  4551. if not assigned(mac) then
  4552. {If not found, then it's already undefined.}
  4553. else
  4554. begin
  4555. Message1(parser_c_macro_undefined,mac.name);
  4556. mac.defined:=false;
  4557. mac.is_compiler_var:=false;
  4558. { delete old definition }
  4559. if assigned(mac.buftext) then
  4560. begin
  4561. freemem(mac.buftext,mac.buflen);
  4562. mac.buftext:=nil;
  4563. end;
  4564. end;
  4565. end;
  4566. {$ifdef UNITALIASES}
  4567. {****************************************************************************
  4568. TUNIT_ALIAS
  4569. ****************************************************************************}
  4570. constructor tunit_alias.create(const n:string);
  4571. var
  4572. i : longint;
  4573. begin
  4574. i:=pos('=',n);
  4575. if i=0 then
  4576. fail;
  4577. inherited createname(Copy(n,1,i-1));
  4578. newname:=stringdup(Copy(n,i+1,255));
  4579. end;
  4580. destructor tunit_alias.destroy;
  4581. begin
  4582. stringdispose(newname);
  4583. inherited destroy;
  4584. end;
  4585. procedure addunitalias(const n:string);
  4586. begin
  4587. unitaliases^.insert(tunit_alias,init(Upper(n))));
  4588. end;
  4589. function getunitalias(const n:string):string;
  4590. var
  4591. p : punit_alias;
  4592. begin
  4593. p:=punit_alias(unitaliases^.Find(Upper(n)));
  4594. if assigned(p) then
  4595. getunitalias:=punit_alias(p).newname^
  4596. else
  4597. getunitalias:=n;
  4598. end;
  4599. {$endif UNITALIASES}
  4600. {****************************************************************************
  4601. Init/Done Symtable
  4602. ****************************************************************************}
  4603. procedure InitSymtable;
  4604. begin
  4605. { Reset symbolstack }
  4606. symtablestack:=nil;
  4607. systemunit:=nil;
  4608. { create error syms and def }
  4609. generrorsym:=terrorsym.create;
  4610. generrordef:=cerrordef.create;
  4611. { macros }
  4612. initialmacrosymtable:=tmacrosymtable.create(false);
  4613. macrosymtablestack:=TSymtablestack.create;
  4614. macrosymtablestack.push(initialmacrosymtable);
  4615. {$ifdef UNITALIASES}
  4616. { unit aliases }
  4617. unitaliases:=TFPHashObjectList.create;
  4618. {$endif}
  4619. dupnr:=0;
  4620. end;
  4621. procedure DoneSymtable;
  4622. begin
  4623. generrorsym.owner:=nil;
  4624. generrorsym.free;
  4625. generrordef.owner:=nil;
  4626. generrordef.free;
  4627. initialmacrosymtable.free;
  4628. macrosymtablestack.free;
  4629. {$ifdef UNITALIASES}
  4630. unitaliases.free;
  4631. {$endif}
  4632. end;
  4633. end.