symtable.pas 194 KB

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