symtable.pas 154 KB

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