symtable.pas 192 KB

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