symtable.pas 168 KB

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