symtable.pas 194 KB

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