symtable.pas 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484
  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,
  23. { global }
  24. cpuinfo,globtype,tokens,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,symsym,
  27. { ppu }
  28. ppu,
  29. { assembler }
  30. aasmtai,aasmdata
  31. ;
  32. {****************************************************************************
  33. Symtable types
  34. ****************************************************************************}
  35. type
  36. tstoredsymtable = class(TSymtable)
  37. private
  38. b_needs_init_final : boolean;
  39. procedure _needs_init_final(sym:TObject;arg:pointer);
  40. procedure check_forward(sym:TObject;arg:pointer);
  41. procedure labeldefined(sym:TObject;arg:pointer);
  42. procedure varsymbolused(sym:TObject;arg:pointer);
  43. procedure TestPrivate(sym:TObject;arg:pointer);
  44. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  45. procedure unchain_overloads(sym:TObject;arg:pointer);
  46. procedure loaddefs(ppufile:tcompilerppufile);
  47. procedure loadsyms(ppufile:tcompilerppufile);
  48. procedure writedefs(ppufile:tcompilerppufile);
  49. procedure writesyms(ppufile:tcompilerppufile);
  50. public
  51. { load/write }
  52. procedure ppuload(ppufile:tcompilerppufile);virtual;
  53. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  54. procedure buildderef;virtual;
  55. procedure buildderefimpl;virtual;
  56. procedure deref;virtual;
  57. procedure derefimpl;virtual;
  58. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  59. procedure reset_all_defs;virtual;
  60. procedure allsymbolsused;
  61. procedure allprivatesused;
  62. procedure check_forwards;
  63. procedure checklabels;
  64. function needs_init_final : boolean;
  65. procedure unchain_overloaded;
  66. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  67. end;
  68. {$ifdef support_llvm}
  69. tllvmshadowsymtableentry = class
  70. constructor create(def: tdef; fieldoffset: aint);
  71. private
  72. ffieldoffset: aint;
  73. fdef: tdef;
  74. public
  75. property fieldoffset: aint read ffieldoffset;
  76. property def: tdef read fdef;
  77. end;
  78. tllvmshadowsymtable = class;
  79. {$endif support_llvm}
  80. tabstractrecordsymtable = class(tstoredsymtable)
  81. public
  82. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  83. recordalignment, { alignment desired when inserting this record }
  84. fieldalignment, { alignment current alignment used when fields are inserted }
  85. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  86. {$ifdef support_llvm}
  87. llvmst: tllvmshadowsymtable;
  88. {$endif}
  89. constructor create(const n:string;usealign:shortint);
  90. destructor destroy; override;
  91. procedure ppuload(ppufile:tcompilerppufile);override;
  92. procedure ppuwrite(ppufile:tcompilerppufile);override;
  93. procedure alignrecord(fieldoffset:aint;varalign:shortint);
  94. procedure addfield(sym:tfieldvarsym);
  95. procedure insertfield(sym:tfieldvarsym);
  96. procedure addalignmentpadding;
  97. procedure insertdef(def:TDefEntry);override;
  98. function is_packed: boolean;
  99. protected
  100. procedure setdatasize(val: aint);
  101. _datasize : aint;
  102. { size in bits of the data in case of bitpacked record. Only important during construction, }
  103. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  104. databitsize : aint;
  105. { bitpacked? -> all fieldvarsym offsets are in bits instead of bytes }
  106. public
  107. property datasize : aint read _datasize write setdatasize;
  108. end;
  109. trecordsymtable = class(tabstractrecordsymtable)
  110. public
  111. constructor create(usealign:shortint);
  112. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  113. end;
  114. tObjectSymtable = class(tabstractrecordsymtable)
  115. public
  116. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  117. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  118. end;
  119. {$ifdef support_llvm}
  120. { llvm record definitions cannot contain variant/union parts, }
  121. { you have to flatten them first. the tllvmshadowsymtable }
  122. { contains a flattened version of a record/object symtable }
  123. tllvmshadowsymtable = class
  124. private
  125. equivst: tabstractrecordsymtable;
  126. curroffset: aint;
  127. public
  128. symdeflist: TFPObjectList;
  129. constructor create(st: tabstractrecordsymtable);
  130. destructor destroy; override;
  131. private
  132. // generate the table
  133. procedure generate;
  134. // helpers
  135. procedure appendsymdef(sym:tfieldvarsym);
  136. procedure findvariantstarts(item: TObject; arg: pointer);
  137. procedure addalignmentpadding(finalsize: aint);
  138. procedure buildmapping(variantstarts: tfplist);
  139. procedure buildtable(variantstarts: tfplist);
  140. end;
  141. {$endif support_llvm}
  142. { tabstractlocalsymtable }
  143. tabstractlocalsymtable = class(tstoredsymtable)
  144. public
  145. procedure ppuwrite(ppufile:tcompilerppufile);override;
  146. function count_locals:longint;
  147. end;
  148. tlocalsymtable = class(tabstractlocalsymtable)
  149. public
  150. constructor create(adefowner:tdef;level:byte);
  151. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  152. end;
  153. tparasymtable = class(tabstractlocalsymtable)
  154. public
  155. constructor create(adefowner:tdef;level:byte);
  156. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  157. end;
  158. tabstractuniTSymtable = class(tstoredsymtable)
  159. public
  160. constructor create(const n : string;id:word);
  161. function iscurrentunit:boolean;override;
  162. end;
  163. tglobalsymtable = class(tabstractuniTSymtable)
  164. public
  165. unittypecount : word;
  166. constructor create(const n : string;id:word);
  167. procedure ppuload(ppufile:tcompilerppufile);override;
  168. procedure ppuwrite(ppufile:tcompilerppufile);override;
  169. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  170. end;
  171. tstaticsymtable = class(tabstractuniTSymtable)
  172. public
  173. constructor create(const n : string;id:word);
  174. procedure ppuload(ppufile:tcompilerppufile);override;
  175. procedure ppuwrite(ppufile:tcompilerppufile);override;
  176. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  177. end;
  178. twithsymtable = class(TSymtable)
  179. withrefnode : tobject; { tnode }
  180. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  181. destructor destroy;override;
  182. procedure clear;override;
  183. procedure insertdef(def:TDefEntry);override;
  184. end;
  185. tstt_excepTSymtable = class(TSymtable)
  186. public
  187. constructor create;
  188. end;
  189. tmacrosymtable = class(tstoredsymtable)
  190. public
  191. constructor create(exported: boolean);
  192. end;
  193. var
  194. systemunit : tglobalsymtable; { pointer to the system unit }
  195. {****************************************************************************
  196. Functions
  197. ****************************************************************************}
  198. {*** Misc ***}
  199. function FullTypeName(def,otherdef:tdef):string;
  200. procedure incompatibletypes(def1,def2:tdef);
  201. procedure hidesym(sym:TSymEntry);
  202. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  203. {*** Search ***}
  204. procedure addsymref(sym:tsym);
  205. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  206. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  207. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  208. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  209. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  210. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  211. function search_system_type(const s: TIDString): ttypesym;
  212. function search_class_member(pd : tobjectdef;const s : string):tsym;
  213. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  214. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  215. {and returns it if found. Returns nil otherwise.}
  216. function search_macro(const s : string):tsym;
  217. { Additionally to searching for a macro, also checks whether it's still }
  218. { actually defined (could be disable using "undef") }
  219. function defined_macro(const s : string):boolean;
  220. {*** Object Helpers ***}
  221. procedure search_class_overloads(aprocsym : tprocsym);
  222. function search_default_property(pd : tobjectdef) : tpropertysym;
  223. {*** Macro Helpers ***}
  224. {If called initially, the following procedures manipulate macros in }
  225. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  226. {Name can be given in any case (it will be converted to upper case).}
  227. procedure def_system_macro(const name : string);
  228. procedure set_system_macro(const name, value : string);
  229. procedure set_system_compvar(const name, value : string);
  230. procedure undef_system_macro(const name : string);
  231. {*** symtable stack ***}
  232. { $ifdef DEBUG
  233. procedure test_symtablestack;
  234. procedure list_symtablestack;
  235. $endif DEBUG}
  236. {$ifdef UNITALIASES}
  237. type
  238. punit_alias = ^tunit_alias;
  239. tunit_alias = object(TNamedIndexItem)
  240. newname : pshortstring;
  241. constructor init(const n:string);
  242. destructor done;virtual;
  243. end;
  244. var
  245. unitaliases : pdictionary;
  246. procedure addunitalias(const n:string);
  247. function getunitalias(const n:string):string;
  248. {$endif UNITALIASES}
  249. {*** Init / Done ***}
  250. procedure IniTSymtable;
  251. procedure DoneSymtable;
  252. const
  253. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  254. ('error',
  255. 'plus','minus','star','slash','equal',
  256. 'greater','lower','greater_or_equal',
  257. 'lower_or_equal',
  258. 'sym_diff','starstar',
  259. 'as','is','in','or',
  260. 'and','div','mod','not','shl','shr','xor',
  261. 'assign');
  262. implementation
  263. uses
  264. { global }
  265. verbose,globals,
  266. { target }
  267. systems,
  268. { symtable }
  269. symutil,defcmp,defutil,
  270. { module }
  271. fmodule,
  272. { codegen }
  273. procinfo
  274. ;
  275. var
  276. dupnr : longint; { unique number for duplicate symbols }
  277. {*****************************************************************************
  278. TStoredSymtable
  279. *****************************************************************************}
  280. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  281. begin
  282. { load definitions }
  283. loaddefs(ppufile);
  284. { load symbols }
  285. loadsyms(ppufile);
  286. end;
  287. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  288. begin
  289. { write definitions }
  290. writedefs(ppufile);
  291. { write symbols }
  292. writesyms(ppufile);
  293. end;
  294. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  295. var
  296. def : tdef;
  297. b : byte;
  298. begin
  299. { load start of definition section, which holds the amount of defs }
  300. if ppufile.readentry<>ibstartdefs then
  301. Message(unit_f_ppu_read_error);
  302. { read definitions }
  303. repeat
  304. b:=ppufile.readentry;
  305. case b of
  306. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  307. ibarraydef : def:=tarraydef.ppuload(ppufile);
  308. iborddef : def:=torddef.ppuload(ppufile);
  309. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  310. ibprocdef : def:=tprocdef.ppuload(ppufile);
  311. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  312. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  313. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  314. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  315. ibunicodestringdef : def:=tstringdef.loadunicode(ppufile);
  316. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  317. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  318. ibenumdef : def:=tenumdef.ppuload(ppufile);
  319. ibsetdef : def:=tsetdef.ppuload(ppufile);
  320. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  321. ibfiledef : def:=tfiledef.ppuload(ppufile);
  322. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  323. ibformaldef : def:=tformaldef.ppuload(ppufile);
  324. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  325. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  326. ibenddefs : break;
  327. ibend : Message(unit_f_ppu_read_error);
  328. else
  329. Message1(unit_f_ppu_invalid_entry,tostr(b));
  330. end;
  331. InsertDef(def);
  332. until false;
  333. end;
  334. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  335. var
  336. b : byte;
  337. sym : tsym;
  338. begin
  339. { load start of definition section, which holds the amount of defs }
  340. if ppufile.readentry<>ibstartsyms then
  341. Message(unit_f_ppu_read_error);
  342. { now read the symbols }
  343. repeat
  344. b:=ppufile.readentry;
  345. case b of
  346. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  347. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  348. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  349. ibstaticvarsym : sym:=tstaticvarsym.ppuload(ppufile);
  350. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  351. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  352. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  353. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  354. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  355. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  356. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  357. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  358. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  359. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  360. ibendsyms : break;
  361. ibend : Message(unit_f_ppu_read_error);
  362. else
  363. Message1(unit_f_ppu_invalid_entry,tostr(b));
  364. end;
  365. Insert(sym,false);
  366. until false;
  367. end;
  368. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  369. var
  370. i : longint;
  371. def : tstoreddef;
  372. begin
  373. { each definition get a number, write then the amount of defs to the
  374. ibstartdef entry }
  375. ppufile.putlongint(DefList.count);
  376. ppufile.writeentry(ibstartdefs);
  377. { now write the definition }
  378. for i:=0 to DefList.Count-1 do
  379. begin
  380. def:=tstoreddef(DefList[i]);
  381. def.ppuwrite(ppufile);
  382. end;
  383. { write end of definitions }
  384. ppufile.writeentry(ibenddefs);
  385. end;
  386. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  387. var
  388. i : longint;
  389. sym : Tstoredsym;
  390. begin
  391. { each definition get a number, write then the amount of syms and the
  392. datasize to the ibsymdef entry }
  393. ppufile.putlongint(SymList.count);
  394. ppufile.writeentry(ibstartsyms);
  395. { foreach is used to write all symbols }
  396. for i:=0 to SymList.Count-1 do
  397. begin
  398. sym:=tstoredsym(SymList[i]);
  399. sym.ppuwrite(ppufile);
  400. end;
  401. { end of symbols }
  402. ppufile.writeentry(ibendsyms);
  403. end;
  404. procedure tstoredsymtable.buildderef;
  405. var
  406. i : longint;
  407. def : tstoreddef;
  408. sym : tstoredsym;
  409. begin
  410. { interface definitions }
  411. for i:=0 to DefList.Count-1 do
  412. begin
  413. def:=tstoreddef(DefList[i]);
  414. def.buildderef;
  415. end;
  416. { interface symbols }
  417. for i:=0 to SymList.Count-1 do
  418. begin
  419. sym:=tstoredsym(SymList[i]);
  420. sym.buildderef;
  421. end;
  422. end;
  423. procedure tstoredsymtable.buildderefimpl;
  424. var
  425. i : longint;
  426. def : tstoreddef;
  427. begin
  428. { implementation definitions }
  429. for i:=0 to DefList.Count-1 do
  430. begin
  431. def:=tstoreddef(DefList[i]);
  432. def.buildderefimpl;
  433. end;
  434. end;
  435. procedure tstoredsymtable.deref;
  436. var
  437. i : longint;
  438. def : tstoreddef;
  439. sym : tstoredsym;
  440. begin
  441. { first deref the interface ttype symbols. This is needs
  442. to be done before the interface defs are derefed, because
  443. the interface defs can contain references to the type symbols
  444. which then already need to contain a resolved typedef field (PFV) }
  445. for i:=0 to SymList.Count-1 do
  446. begin
  447. sym:=tstoredsym(SymList[i]);
  448. if sym.typ=typesym then
  449. sym.deref;
  450. end;
  451. { interface definitions }
  452. for i:=0 to DefList.Count-1 do
  453. begin
  454. def:=tstoreddef(DefList[i]);
  455. def.deref;
  456. end;
  457. { interface symbols }
  458. for i:=0 to SymList.Count-1 do
  459. begin
  460. sym:=tstoredsym(SymList[i]);
  461. if sym.typ<>typesym then
  462. sym.deref;
  463. end;
  464. end;
  465. procedure tstoredsymtable.derefimpl;
  466. var
  467. i : longint;
  468. def : tstoreddef;
  469. begin
  470. { implementation definitions }
  471. for i:=0 to DefList.Count-1 do
  472. begin
  473. def:=tstoreddef(DefList[i]);
  474. def.derefimpl;
  475. end;
  476. end;
  477. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  478. var
  479. hsym : tsym;
  480. begin
  481. hsym:=tsym(FindWithHash(hashedid));
  482. if assigned(hsym) then
  483. DuplicateSym(hashedid,sym,hsym);
  484. result:=assigned(hsym);
  485. end;
  486. {**************************************
  487. Callbacks
  488. **************************************}
  489. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  490. begin
  491. if tsym(sym).typ=procsym then
  492. tprocsym(sym).check_forward
  493. { check also object method table }
  494. { we needn't to test the def list }
  495. { because each object has to have a type sym,
  496. only test objects declarations, not type renamings }
  497. else
  498. if (tsym(sym).typ=typesym) and
  499. assigned(ttypesym(sym).typedef) and
  500. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  501. (ttypesym(sym).typedef.typ=objectdef) then
  502. tobjectdef(ttypesym(sym).typedef).check_forwards;
  503. end;
  504. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  505. begin
  506. if (tsym(sym).typ=labelsym) and
  507. not(tlabelsym(sym).defined) then
  508. begin
  509. if tlabelsym(sym).used then
  510. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  511. else
  512. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  513. end;
  514. end;
  515. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  516. begin
  517. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  518. ((tsym(sym).owner.symtabletype in
  519. [parasymtable,localsymtable,ObjectSymtable,staticsymtable])) then
  520. begin
  521. { unused symbol should be reported only if no }
  522. { error is reported }
  523. { if the symbol is in a register it is used }
  524. { also don't count the value parameters which have local copies }
  525. { also don't claim for high param of open parameters (PM) }
  526. if (Errorcount<>0) or
  527. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) then
  528. exit;
  529. if (tstoredsym(sym).refs=0) then
  530. begin
  531. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  532. begin
  533. { don't warn about the result of constructors }
  534. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  535. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  536. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  537. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  538. end
  539. else if (tsym(sym).owner.symtabletype=parasymtable) then
  540. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).realname)
  541. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  542. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tsym(sym).owner.realname^,tsym(sym).realname)
  543. else
  544. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).realname);
  545. end
  546. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  547. begin
  548. if (tsym(sym).owner.symtabletype=parasymtable) then
  549. begin
  550. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out]) and
  551. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  552. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).realname)
  553. end
  554. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  555. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tsym(sym).owner.realname^,tsym(sym).realname)
  556. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  557. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).realname);
  558. end
  559. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  560. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  561. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).realname)
  562. end
  563. else if ((tsym(sym).owner.symtabletype in
  564. [ObjectSymtable,parasymtable,localsymtable,staticsymtable])) then
  565. begin
  566. if (Errorcount<>0) or
  567. (sp_internal in tsym(sym).symoptions) then
  568. exit;
  569. { do not claim for inherited private fields !! }
  570. if (Tsym(sym).refs=0) and (tsym(sym).owner.symtabletype=ObjectSymtable) then
  571. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tsym(sym).owner.realname^,tsym(sym).realname)
  572. { units references are problematic }
  573. else
  574. begin
  575. if (Tsym(sym).refs=0) and
  576. not(tsym(sym).typ in [enumsym,unitsym]) and
  577. not(is_funcret_sym(tsym(sym))) and
  578. (
  579. (tsym(sym).typ<>procsym) or
  580. ((tsym(sym).owner.symtabletype=staticsymtable) and
  581. not current_module.is_unit)
  582. ) then
  583. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).realname);
  584. end;
  585. end;
  586. end;
  587. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  588. begin
  589. if sp_private in tsym(sym).symoptions then
  590. varsymbolused(sym,arg);
  591. end;
  592. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  593. begin
  594. {
  595. Don't test simple object aliases PM
  596. }
  597. if (tsym(sym).typ=typesym) and
  598. (ttypesym(sym).typedef.typ=objectdef) and
  599. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  600. tobjectdef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  601. end;
  602. procedure tstoredsymtable.unchain_overloads(sym:TObject;arg:pointer);
  603. begin
  604. if tsym(sym).typ=procsym then
  605. tprocsym(sym).unchain_overload;
  606. end;
  607. {***********************************************
  608. Process all entries
  609. ***********************************************}
  610. procedure Tstoredsymtable.reset_all_defs;
  611. var
  612. i : longint;
  613. def : tstoreddef;
  614. begin
  615. for i:=0 to DefList.Count-1 do
  616. begin
  617. def:=tstoreddef(DefList[i]);
  618. def.reset;
  619. end;
  620. end;
  621. { checks, if all procsyms and methods are defined }
  622. procedure tstoredsymtable.check_forwards;
  623. begin
  624. SymList.ForEachCall(@check_forward,nil);
  625. end;
  626. procedure tstoredsymtable.checklabels;
  627. begin
  628. SymList.ForEachCall(@labeldefined,nil);
  629. end;
  630. procedure tstoredsymtable.allsymbolsused;
  631. begin
  632. SymList.ForEachCall(@varsymbolused,nil);
  633. end;
  634. procedure tstoredsymtable.allprivatesused;
  635. begin
  636. SymList.ForEachCall(@objectprivatesymbolused,nil);
  637. end;
  638. procedure tstoredsymtable.unchain_overloaded;
  639. begin
  640. SymList.ForEachCall(@unchain_overloads,nil);
  641. end;
  642. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  643. begin
  644. if b_needs_init_final then
  645. exit;
  646. case tsym(sym).typ of
  647. fieldvarsym,
  648. staticvarsym,
  649. localvarsym,
  650. paravarsym :
  651. begin
  652. if not(is_class(tabstractvarsym(sym).vardef)) and
  653. tstoreddef(tabstractvarsym(sym).vardef).needs_inittable then
  654. b_needs_init_final:=true;
  655. end;
  656. end;
  657. end;
  658. { returns true, if p contains data which needs init/final code }
  659. function tstoredsymtable.needs_init_final : boolean;
  660. begin
  661. b_needs_init_final:=false;
  662. SymList.ForEachCall(@_needs_init_final,nil);
  663. needs_init_final:=b_needs_init_final;
  664. end;
  665. {****************************************************************************
  666. TAbstractRecordSymtable
  667. ****************************************************************************}
  668. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  669. begin
  670. inherited create(n);
  671. _datasize:=0;
  672. databitsize:=0;
  673. recordalignment:=1;
  674. usefieldalignment:=usealign;
  675. padalignment:=1;
  676. { recordalign C_alignment means C record packing, that starts
  677. with an alignment of 1 }
  678. case usealign of
  679. C_alignment,
  680. bit_alignment:
  681. fieldalignment:=1
  682. else
  683. fieldalignment:=usealign;
  684. end;
  685. end;
  686. destructor tabstractrecordsymtable.destroy;
  687. begin
  688. {$ifdef support_llvm}
  689. llvmst.free;
  690. {$endif suppor_llvm}
  691. inherited destroy;
  692. end;
  693. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  694. begin
  695. inherited ppuload(ppufile);
  696. end;
  697. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  698. var
  699. oldtyp : byte;
  700. begin
  701. oldtyp:=ppufile.entrytyp;
  702. ppufile.entrytyp:=subentryid;
  703. inherited ppuwrite(ppufile);
  704. ppufile.entrytyp:=oldtyp;
  705. end;
  706. function field2recordalignment(fieldoffs, fieldalign: aint): aint;
  707. begin
  708. { optimal alignment of the record when declaring a variable of this }
  709. { type is independent of the packrecords setting }
  710. if (fieldoffs mod fieldalign) = 0 then
  711. result:=fieldalign
  712. else if (fieldalign >= 16) and
  713. ((fieldoffs mod 16) = 0) and
  714. ((fieldalign mod 16) = 0) then
  715. result:=16
  716. else if (fieldalign >= 8) and
  717. ((fieldoffs mod 8) = 0) and
  718. ((fieldalign mod 8) = 0) then
  719. result:=8
  720. else if (fieldalign >= 4) and
  721. ((fieldoffs mod 4) = 0) and
  722. ((fieldalign mod 4) = 0) then
  723. result:=4
  724. else if (fieldalign >= 2) and
  725. ((fieldoffs mod 2) = 0) and
  726. ((fieldalign mod 2) = 0) then
  727. result:=2
  728. else
  729. result:=1;
  730. end;
  731. procedure tabstractrecordsymtable.alignrecord(fieldoffset:aint;varalign:shortint);
  732. var
  733. varalignrecord: shortint;
  734. begin
  735. if (usefieldalignment=C_alignment) then
  736. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign)
  737. else
  738. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  739. recordalignment:=max(recordalignment,varalignrecord);
  740. end;
  741. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym);
  742. var
  743. l : aint;
  744. varalignfield,
  745. varalign : shortint;
  746. vardef : tdef;
  747. begin
  748. if (sym.owner<>self) then
  749. internalerror(200602031);
  750. if sym.fieldoffset<>-1 then
  751. internalerror(200602032);
  752. { this symbol can't be loaded to a register }
  753. sym.varregable:=vr_none;
  754. { Calculate field offset }
  755. l:=sym.getsize;
  756. vardef:=sym.vardef;
  757. varalign:=vardef.alignment;
  758. if (usefieldalignment=bit_alignment) then
  759. begin
  760. { bitpacking only happens for ordinals, the rest is aligned at }
  761. { 1 byte (compatible with GPC/GCC) }
  762. if is_ordinal(vardef) then
  763. begin
  764. sym.fieldoffset:=databitsize;
  765. l:=sym.getpackedbitsize;
  766. end
  767. else
  768. begin
  769. databitsize:=_datasize*8;
  770. sym.fieldoffset:=databitsize;
  771. if (l>high(aint) div 8) then
  772. Message(sym_e_segment_too_large);
  773. l:=l*8;
  774. end;
  775. if varalign=0 then
  776. varalign:=size_2_align(l);
  777. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  778. { bit packed records are limited to high(aint) bits }
  779. { instead of bytes to avoid double precision }
  780. { arithmetic in offset calculations }
  781. if int64(l)>high(aint)-sym.fieldoffset then
  782. begin
  783. Message(sym_e_segment_too_large);
  784. _datasize:=high(aint);
  785. databitsize:=high(aint);
  786. end
  787. else
  788. begin
  789. databitsize:=sym.fieldoffset+l;
  790. _datasize:=(databitsize+7) div 8;
  791. end;
  792. { rest is not applicable }
  793. exit;
  794. end;
  795. { Calc the alignment size for C style records }
  796. if (usefieldalignment=C_alignment) then
  797. begin
  798. if (varalign>4) and
  799. ((varalign mod 4)<>0) and
  800. (vardef.typ=arraydef) then
  801. Message1(sym_w_wrong_C_pack,vardef.typename);
  802. if varalign=0 then
  803. varalign:=l;
  804. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  805. begin
  806. if (varalign>16) and (fieldalignment<32) then
  807. fieldalignment:=32
  808. else if (varalign>12) and (fieldalignment<16) then
  809. fieldalignment:=16
  810. { 12 is needed for long double }
  811. else if (varalign>8) and (fieldalignment<12) then
  812. fieldalignment:=12
  813. else if (varalign>4) and (fieldalignment<8) then
  814. fieldalignment:=8
  815. else if (varalign>2) and (fieldalignment<4) then
  816. fieldalignment:=4
  817. else if (varalign>1) and (fieldalignment<2) then
  818. fieldalignment:=2;
  819. end;
  820. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  821. end;
  822. if varalign=0 then
  823. varalign:=size_2_align(l);
  824. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  825. sym.fieldoffset:=align(_datasize,varalignfield);
  826. if l>high(aint)-sym.fieldoffset then
  827. begin
  828. Message(sym_e_segment_too_large);
  829. _datasize:=high(aint);
  830. end
  831. else
  832. _datasize:=sym.fieldoffset+l;
  833. { Calc alignment needed for this record }
  834. alignrecord(sym.fieldoffset,varalign);
  835. end;
  836. procedure tabstractrecordsymtable.insertfield(sym:tfieldvarsym);
  837. begin
  838. insert(sym);
  839. addfield(sym);
  840. end;
  841. procedure tabstractrecordsymtable.addalignmentpadding;
  842. begin
  843. { make the record size aligned correctly so it can be
  844. used as elements in an array. For C records we
  845. use the fieldalignment, because that is updated with the
  846. used alignment. }
  847. if (padalignment = 1) then
  848. case usefieldalignment of
  849. C_alignment:
  850. padalignment:=fieldalignment;
  851. { bitpacked }
  852. bit_alignment:
  853. padalignment:=1;
  854. { default/no packrecords specified }
  855. 0:
  856. padalignment:=recordalignment
  857. { specific packrecords setting -> use as upper limit }
  858. else
  859. padalignment:=min(recordalignment,usefieldalignment);
  860. end;
  861. _datasize:=align(_datasize,padalignment);
  862. end;
  863. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  864. begin
  865. { Enums must also be available outside the record scope,
  866. insert in the owner of this symtable }
  867. if def.typ=enumdef then
  868. defowner.owner.insertdef(def)
  869. else
  870. inherited insertdef(def);
  871. end;
  872. function tabstractrecordsymtable.is_packed: boolean;
  873. begin
  874. result:=usefieldalignment=bit_alignment;
  875. end;
  876. procedure tabstractrecordsymtable.setdatasize(val: aint);
  877. begin
  878. _datasize:=val;
  879. if (usefieldalignment=bit_alignment) then
  880. { can overflow in non bitpacked records }
  881. databitsize:=val*8;
  882. end;
  883. {****************************************************************************
  884. TRecordSymtable
  885. ****************************************************************************}
  886. constructor trecordsymtable.create(usealign:shortint);
  887. begin
  888. inherited create('',usealign);
  889. symtabletype:=recordsymtable;
  890. end;
  891. { this procedure is reserved for inserting case variant into
  892. a record symtable }
  893. { the offset is the location of the start of the variant
  894. and datasize and dataalignment corresponds to
  895. the complete size (see code in pdecl unit) PM }
  896. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  897. var
  898. sym : tsym;
  899. def : tdef;
  900. i : integer;
  901. varalignrecord,varalign,
  902. storesize,storealign : aint;
  903. bitsize: aint;
  904. begin
  905. storesize:=_datasize;
  906. storealign:=fieldalignment;
  907. _datasize:=offset;
  908. if (usefieldalignment=bit_alignment) then
  909. databitsize:=offset*8;
  910. { We move the ownership of the defs and symbols to the new recordsymtable.
  911. The old unionsymtable keeps the references, but doesn't own the
  912. objects anymore }
  913. unionst.DefList.OwnsObjects:=false;
  914. unionst.SymList.OwnsObjects:=false;
  915. { copy symbols }
  916. for i:=0 to unionst.SymList.Count-1 do
  917. begin
  918. sym:=TSym(unionst.SymList[i]);
  919. if sym.typ<>fieldvarsym then
  920. internalerror(200601272);
  921. { add to this record symtable }
  922. // unionst.SymList.List.List^[i].Data:=nil;
  923. sym.ChangeOwner(self);
  924. varalign:=tfieldvarsym(sym).vardef.alignment;
  925. if varalign=0 then
  926. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  927. { retrieve size }
  928. if (usefieldalignment=bit_alignment) then
  929. begin
  930. { bit packed records are limited to high(aint) bits }
  931. { instead of bytes to avoid double precision }
  932. { arithmetic in offset calculations }
  933. if is_ordinal(tfieldvarsym(sym).vardef) then
  934. bitsize:=tfieldvarsym(sym).getpackedbitsize
  935. else
  936. begin
  937. bitsize:=tfieldvarsym(sym).getsize;
  938. if (bitsize>high(aint) div 8) then
  939. Message(sym_e_segment_too_large);
  940. bitsize:=bitsize*8;
  941. end;
  942. if bitsize>high(aint)-databitsize then
  943. begin
  944. Message(sym_e_segment_too_large);
  945. _datasize:=high(aint);
  946. databitsize:=high(aint);
  947. end
  948. else
  949. begin
  950. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  951. _datasize:=(databitsize+7) div 8;
  952. end;
  953. tfieldvarsym(sym).fieldoffset:=databitsize;
  954. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  955. end
  956. else
  957. begin
  958. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  959. begin
  960. Message(sym_e_segment_too_large);
  961. _datasize:=high(aint);
  962. end
  963. else
  964. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  965. { update address }
  966. tfieldvarsym(sym).fieldoffset:=_datasize;
  967. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  968. end;
  969. { update alignment of this record }
  970. if (usefieldalignment<>C_alignment) then
  971. recordalignment:=max(recordalignment,varalignrecord);
  972. end;
  973. { update alignment for C records }
  974. if (usefieldalignment=C_alignment) then
  975. recordalignment:=max(recordalignment,unionst.recordalignment);
  976. { Register defs in the new record symtable }
  977. for i:=0 to unionst.DefList.Count-1 do
  978. begin
  979. def:=TDef(unionst.DefList[i]);
  980. def.ChangeOwner(self);
  981. end;
  982. _datasize:=storesize;
  983. fieldalignment:=storealign;
  984. end;
  985. {****************************************************************************
  986. TObjectSymtable
  987. ****************************************************************************}
  988. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  989. begin
  990. inherited create(n,usealign);
  991. symtabletype:=ObjectSymtable;
  992. defowner:=adefowner;
  993. end;
  994. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  995. var
  996. hsym : tsym;
  997. begin
  998. result:=false;
  999. if not assigned(defowner) then
  1000. internalerror(200602061);
  1001. { procsym and propertysym have special code
  1002. to override values in inherited classes. For other
  1003. symbols check for duplicates }
  1004. if not(sym.typ in [procsym,propertysym]) then
  1005. begin
  1006. { but private ids can be reused }
  1007. hsym:=search_class_member(tobjectdef(defowner),hashedid.id);
  1008. if assigned(hsym) and
  1009. (
  1010. (not(m_delphi in current_settings.modeswitches) and
  1011. tsym(hsym).is_visible_for_object(tobjectdef(defowner),tobjectdef(defowner))
  1012. ) or
  1013. (
  1014. { In Delphi, you can repeat members of a parent class. You can't }
  1015. { do this for objects however, and you (obviouly) can't }
  1016. { declare two fields with the same name in a single class }
  1017. (m_delphi in current_settings.modeswitches) and
  1018. (
  1019. is_object(tdef(defowner)) or
  1020. (hsym.owner = self)
  1021. )
  1022. )
  1023. ) then
  1024. begin
  1025. DuplicateSym(hashedid,sym,hsym);
  1026. result:=true;
  1027. end;
  1028. end
  1029. else
  1030. begin
  1031. if not(m_duplicate_names in current_settings.modeswitches) then
  1032. result:=inherited checkduplicate(hashedid,sym);
  1033. end;
  1034. end;
  1035. {$ifdef support_llvm}
  1036. {****************************************************************************
  1037. tLlvmShadowSymtableEntry
  1038. ****************************************************************************}
  1039. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1040. begin
  1041. fdef:=def;
  1042. ffieldoffset:=fieldoffset;
  1043. end;
  1044. {****************************************************************************
  1045. TLlvmShadowSymtable
  1046. ****************************************************************************}
  1047. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1048. begin
  1049. equivst:=st;
  1050. curroffset:=0;
  1051. symdeflist:=tfpobjectlist.create(false);
  1052. generate;
  1053. end;
  1054. destructor tllvmshadowsymtable.destroy;
  1055. begin
  1056. symdeflist.free;
  1057. end;
  1058. procedure tllvmshadowsymtable.appendsymdef(sym:tfieldvarsym);
  1059. var
  1060. sizectr,
  1061. tmpsize: aint;
  1062. begin
  1063. case equivst.usefieldalignment of
  1064. C_alignment:
  1065. { default for llvm, don't add explicit padding }
  1066. symdeflist.add(tllvmshadowsymtableentry.create(sym.vardef,sym.fieldoffset));
  1067. bit_alignment:
  1068. begin
  1069. { curoffset: bit address after the previous field. }
  1070. { llvm has no special support for bitfields in records, }
  1071. { so we replace them with plain bytes. }
  1072. { as soon as a single bit of a byte is allocated, we }
  1073. { allocate the byte in the llvm shadow record }
  1074. if (sym.fieldoffset>curroffset) then
  1075. curroffset:=align(curroffset,8);
  1076. { fields in bitpacked records always start either right }
  1077. { after the previous one, or at the next byte boundary. }
  1078. if (curroffset<>sym.fieldoffset) then
  1079. internalerror(2008051002);
  1080. if is_ordinal(sym.vardef) and
  1081. (sym.getpackedbitsize mod 8 <> 0) then
  1082. begin
  1083. tmpsize:=sym.getpackedbitsize;
  1084. sizectr:=tmpsize+7;
  1085. repeat
  1086. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,sym.fieldoffset+(tmpsize+7)-sizectr));
  1087. dec(sizectr,8);
  1088. until (sizectr<=0);
  1089. inc(curroffset,tmpsize);
  1090. end
  1091. else
  1092. begin
  1093. symdeflist.add(tllvmshadowsymtableentry.create(sym.vardef,sym.fieldoffset));
  1094. inc(curroffset,sym.vardef.size*8);
  1095. end;
  1096. end
  1097. else
  1098. begin
  1099. { curoffset: address right after the previous field }
  1100. while (sym.fieldoffset>curroffset) do
  1101. begin
  1102. symdeflist.add(tllvmshadowsymtableentry.create(s8inttype,curroffset));
  1103. inc(curroffset);
  1104. end;
  1105. symdeflist.add(tllvmshadowsymtableentry.create(sym.vardef,sym.fieldoffset));
  1106. inc(curroffset,sym.vardef.size);
  1107. end
  1108. end
  1109. end;
  1110. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1111. begin
  1112. case equivst.usefieldalignment of
  1113. { already correct in this case }
  1114. bit_alignment,
  1115. { handled by llvm }
  1116. C_alignment:
  1117. ;
  1118. else
  1119. begin
  1120. { add padding fields }
  1121. while (finalsize>curroffset) do
  1122. begin
  1123. symdeflist.add(tllvmshadowsymtableentry.create(s8inttype,curroffset));
  1124. inc(curroffset);
  1125. end;
  1126. end;
  1127. end;
  1128. end;
  1129. procedure tllvmshadowsymtable.findvariantstarts(item: TObject; arg: pointer);
  1130. var
  1131. variantstarts: tfplist absolute arg;
  1132. sym: tfieldvarsym absolute item;
  1133. lastoffset: aint;
  1134. newalignment: aint;
  1135. begin
  1136. if (tsym(item).typ<>fieldvarsym) then
  1137. exit;
  1138. { a "better" algorithm might be to use the largest }
  1139. { variant in case of (bit)packing, since then }
  1140. { alignment doesn't matter }
  1141. if (vo_is_first_field in sym.varoptions) then
  1142. begin
  1143. { we assume that all fields are processed in order. }
  1144. { the most deeply nested variant always comes last }
  1145. { in Pascal }
  1146. if (variantstarts.count<>0) then
  1147. lastoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  1148. else
  1149. lastoffset:=-1;
  1150. if (lastoffset=sym.fieldoffset) then
  1151. begin
  1152. if (equivst.fieldalignment<>bit_alignment) then
  1153. newalignment:=used_align(sym.vardef.alignment,current_settings.alignment.recordalignmin,equivst.fieldalignment)
  1154. else
  1155. newalignment:=1;
  1156. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  1157. tfieldvarsym(variantstarts[variantstarts.count-1]):=sym
  1158. end
  1159. else if (lastoffset<sym.fieldoffset) then
  1160. variantstarts.add(sym)
  1161. else
  1162. internalerror(2008051003);
  1163. end;
  1164. end;
  1165. procedure tllvmshadowsymtable.buildtable(variantstarts: tfplist);
  1166. var
  1167. lastvaroffsetprocessed: aint;
  1168. i, equivcount, varcount: longint;
  1169. begin
  1170. { if it's an object/class, the first entry is the parent (if there is one) }
  1171. if (equivst.symtabletype=objectsymtable) and
  1172. assigned(tobjectdef(equivst.defowner).childof) then
  1173. symdeflist.add(tllvmshadowsymtableentry.create(tobjectdef(equivst.defowner).childof,0));
  1174. equivcount:=equivst.symlist.count;
  1175. varcount:=0;
  1176. i:=0;
  1177. lastvaroffsetprocessed:=-1;
  1178. while (i<equivcount) do
  1179. begin
  1180. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1181. begin
  1182. inc(i);
  1183. continue;
  1184. end;
  1185. { start of a new variant? }
  1186. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1187. begin
  1188. { if we want to process the same variant offset twice, it means that we }
  1189. { got to the end and are trying to process the next variant part -> stop }
  1190. if (tfieldvarsym(equivst.symlist[i]).fieldoffset=lastvaroffsetprocessed) then
  1191. break;
  1192. if (varcount>=variantstarts.count) or
  1193. (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1194. internalerror(2008051005);
  1195. { new variant part -> use the one with the biggest alignment }
  1196. i:=equivst.symlist.indexof(tobject(variantstarts[varcount]));
  1197. lastvaroffsetprocessed:=tfieldvarsym(equivst.symlist[i]).fieldoffset;
  1198. inc(varcount);
  1199. if (i<0) then
  1200. internalerror(2008051004);
  1201. end;
  1202. appendsymdef(tfieldvarsym(equivst.symlist[i]));
  1203. inc(i);
  1204. end;
  1205. addalignmentpadding(equivst.datasize);
  1206. end;
  1207. procedure tllvmshadowsymtable.buildmapping(variantstarts: tfplist);
  1208. var
  1209. i, varcount: longint;
  1210. shadowindex: longint;
  1211. equivcount : longint;
  1212. begin
  1213. varcount:=0;
  1214. shadowindex:=0;
  1215. equivcount:=equivst.symlist.count;
  1216. i:=0;
  1217. while (i < equivcount) do
  1218. begin
  1219. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1220. begin
  1221. inc(i);
  1222. continue;
  1223. end;
  1224. { start of a new variant? }
  1225. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1226. begin
  1227. { the variant can either come after the current one, or }
  1228. { be at the same level }
  1229. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1230. dec(varcount);
  1231. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1232. internalerror(2008051006);
  1233. { reset the shadowindex to the start of this variant. }
  1234. { in case the llvmfieldnr is not (yet) set for this }
  1235. { field, shadowindex will simply be reset to zero and }
  1236. { we'll start searching from the start of the record }
  1237. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  1238. if (varcount<pred(variantstarts.count)) then
  1239. inc(varcount);
  1240. end;
  1241. { find the last shadowfield whose offset <= the current field's offset }
  1242. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<tfieldvarsym(equivst.symlist[i]).fieldoffset) and
  1243. (shadowindex<symdeflist.count-1) and
  1244. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset>=tfieldvarsym(equivst.symlist[i]).fieldoffset) do
  1245. inc(shadowindex);
  1246. { set the field number and potential offset from that field (in case }
  1247. { of overlapping variants) }
  1248. tfieldvarsym(equivst.symlist[i]).llvmfieldnr:=shadowindex;
  1249. tfieldvarsym(equivst.symlist[i]).offsetfromllvmfield:=
  1250. tfieldvarsym(equivst.symlist[i]).fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  1251. inc(i);
  1252. end;
  1253. end;
  1254. procedure tllvmshadowsymtable.generate;
  1255. var
  1256. variantstarts: tfplist;
  1257. begin
  1258. variantstarts:=tfplist.create;
  1259. { first go through the entire record and }
  1260. { store the fieldvarsyms of the variants }
  1261. { with the highest alignment }
  1262. equivst.symlist.foreachcall(@findvariantstarts,pointer(variantstarts));
  1263. { now go through the regular fields and the selected variants, }
  1264. { and add them to the llvm shadow record symtable }
  1265. buildtable(variantstarts);
  1266. { finally map all original fields to the llvm definition }
  1267. buildmapping(variantstarts);
  1268. variantstarts.free;
  1269. end;
  1270. {$endif support_llvm}
  1271. {****************************************************************************
  1272. TAbstractLocalSymtable
  1273. ****************************************************************************}
  1274. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1275. var
  1276. oldtyp : byte;
  1277. begin
  1278. oldtyp:=ppufile.entrytyp;
  1279. ppufile.entrytyp:=subentryid;
  1280. { write definitions }
  1281. writedefs(ppufile);
  1282. { write symbols }
  1283. writesyms(ppufile);
  1284. ppufile.entrytyp:=oldtyp;
  1285. end;
  1286. function tabstractlocalsymtable.count_locals:longint;
  1287. var
  1288. i : longint;
  1289. sym : tsym;
  1290. begin
  1291. result:=0;
  1292. for i:=0 to SymList.Count-1 do
  1293. begin
  1294. sym:=tsym(SymList[i]);
  1295. { Count only varsyms, but ignore the funcretsym }
  1296. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1297. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1298. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1299. (tstoredsym(sym).refs>0)) then
  1300. inc(result);
  1301. end;
  1302. end;
  1303. {****************************************************************************
  1304. TLocalSymtable
  1305. ****************************************************************************}
  1306. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1307. begin
  1308. inherited create('');
  1309. defowner:=adefowner;
  1310. symtabletype:=localsymtable;
  1311. symtablelevel:=level;
  1312. end;
  1313. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1314. var
  1315. hsym : tsym;
  1316. begin
  1317. if not assigned(defowner) or
  1318. (defowner.typ<>procdef) then
  1319. internalerror(200602042);
  1320. result:=false;
  1321. hsym:=tsym(FindWithHash(hashedid));
  1322. if assigned(hsym) then
  1323. begin
  1324. { a local and the function can have the same
  1325. name in TP and Delphi, but RESULT not }
  1326. if (m_duplicate_names in current_settings.modeswitches) and
  1327. (hsym.typ in [absolutevarsym,localvarsym]) and
  1328. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1329. not((m_result in current_settings.modeswitches) and
  1330. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1331. HideSym(hsym)
  1332. else
  1333. DuplicateSym(hashedid,sym,hsym);
  1334. result:=true;
  1335. exit;
  1336. end;
  1337. { check also parasymtable, this needs to be done here becuase
  1338. of the special situation with the funcret sym that needs to be
  1339. hidden for tp and delphi modes }
  1340. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1341. if assigned(hsym) then
  1342. begin
  1343. { a local and the function can have the same
  1344. name in TP and Delphi, but RESULT not }
  1345. if (m_duplicate_names in current_settings.modeswitches) and
  1346. (sym.typ in [absolutevarsym,localvarsym]) and
  1347. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1348. not((m_result in current_settings.modeswitches) and
  1349. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1350. Hidesym(sym)
  1351. else
  1352. DuplicateSym(hashedid,sym,hsym);
  1353. result:=true;
  1354. exit;
  1355. end;
  1356. { check ObjectSymtable, skip this for funcret sym because
  1357. that will always be positive because it has the same name
  1358. as the procsym }
  1359. if not is_funcret_sym(sym) and
  1360. (defowner.typ=procdef) and
  1361. assigned(tprocdef(defowner)._class) and
  1362. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1363. (
  1364. not(m_delphi in current_settings.modeswitches) or
  1365. is_object(tprocdef(defowner)._class)
  1366. ) then
  1367. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1368. end;
  1369. {****************************************************************************
  1370. TParaSymtable
  1371. ****************************************************************************}
  1372. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1373. begin
  1374. inherited create('');
  1375. defowner:=adefowner;
  1376. symtabletype:=parasymtable;
  1377. symtablelevel:=level;
  1378. end;
  1379. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1380. begin
  1381. result:=inherited checkduplicate(hashedid,sym);
  1382. if result then
  1383. exit;
  1384. if not(m_duplicate_names in current_settings.modeswitches) and
  1385. (defowner.typ=procdef) and
  1386. assigned(tprocdef(defowner)._class) and
  1387. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1388. (
  1389. not(m_delphi in current_settings.modeswitches) or
  1390. is_object(tprocdef(defowner)._class)
  1391. ) then
  1392. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1393. end;
  1394. {****************************************************************************
  1395. TAbstractUniTSymtable
  1396. ****************************************************************************}
  1397. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1398. begin
  1399. inherited create(n);
  1400. moduleid:=id;
  1401. end;
  1402. function tabstractuniTSymtable.iscurrentunit:boolean;
  1403. begin
  1404. result:=assigned(current_module) and
  1405. (
  1406. (current_module.globalsymtable=self) or
  1407. (current_module.localsymtable=self)
  1408. );
  1409. end;
  1410. {****************************************************************************
  1411. TStaticSymtable
  1412. ****************************************************************************}
  1413. constructor tstaticsymtable.create(const n : string;id:word);
  1414. begin
  1415. inherited create(n,id);
  1416. symtabletype:=staticsymtable;
  1417. symtablelevel:=main_program_level;
  1418. end;
  1419. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1420. begin
  1421. inherited ppuload(ppufile);
  1422. { now we can deref the syms and defs }
  1423. deref;
  1424. end;
  1425. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1426. begin
  1427. inherited ppuwrite(ppufile);
  1428. end;
  1429. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1430. var
  1431. hsym : tsym;
  1432. begin
  1433. result:=false;
  1434. hsym:=tsym(FindWithHash(hashedid));
  1435. if assigned(hsym) then
  1436. begin
  1437. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1438. unit, the unit can then not be accessed anymore using
  1439. <unit>.<id>, so we can hide the symbol }
  1440. if (m_delphi in current_settings.modeswitches) and
  1441. (hsym.typ=symconst.unitsym) then
  1442. HideSym(hsym)
  1443. else
  1444. DuplicateSym(hashedid,sym,hsym);
  1445. result:=true;
  1446. exit;
  1447. end;
  1448. if (current_module.localsymtable=self) and
  1449. assigned(current_module.globalsymtable) then
  1450. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1451. end;
  1452. {****************************************************************************
  1453. TGlobalSymtable
  1454. ****************************************************************************}
  1455. constructor tglobalsymtable.create(const n : string;id:word);
  1456. begin
  1457. inherited create(n,id);
  1458. symtabletype:=globalsymtable;
  1459. symtablelevel:=main_program_level;
  1460. end;
  1461. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1462. begin
  1463. inherited ppuload(ppufile);
  1464. { now we can deref the syms and defs }
  1465. deref;
  1466. end;
  1467. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1468. begin
  1469. { write the symtable entries }
  1470. inherited ppuwrite(ppufile);
  1471. end;
  1472. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1473. var
  1474. hsym : tsym;
  1475. begin
  1476. result:=false;
  1477. hsym:=tsym(FindWithHash(hashedid));
  1478. if assigned(hsym) then
  1479. begin
  1480. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1481. unit, the unit can then not be accessed anymore using
  1482. <unit>.<id>, so we can hide the symbol }
  1483. if (m_delphi in current_settings.modeswitches) and
  1484. (hsym.typ=symconst.unitsym) then
  1485. HideSym(hsym)
  1486. else
  1487. DuplicateSym(hashedid,sym,hsym);
  1488. result:=true;
  1489. exit;
  1490. end;
  1491. end;
  1492. {****************************************************************************
  1493. TWITHSYMTABLE
  1494. ****************************************************************************}
  1495. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1496. begin
  1497. inherited create('');
  1498. symtabletype:=withsymtable;
  1499. withrefnode:=refnode;
  1500. { Replace SymList with the passed symlist }
  1501. SymList.free;
  1502. SymList:=ASymList;
  1503. defowner:=aowner;
  1504. end;
  1505. destructor twithsymtable.destroy;
  1506. begin
  1507. withrefnode.free;
  1508. { Disable SymList because we don't Own it }
  1509. SymList:=nil;
  1510. inherited destroy;
  1511. end;
  1512. procedure twithsymtable.clear;
  1513. begin
  1514. { remove no entry from a withsymtable as it is only a pointer to the
  1515. recorddef or objectdef symtable }
  1516. end;
  1517. procedure twithsymtable.insertdef(def:TDefEntry);
  1518. begin
  1519. { Definitions can't be registered in the withsymtable
  1520. because the withsymtable is removed after the with block.
  1521. We can't easily solve it here because the next symtable in the
  1522. stack is not known. }
  1523. internalerror(200602046);
  1524. end;
  1525. {****************************************************************************
  1526. TSTT_ExceptionSymtable
  1527. ****************************************************************************}
  1528. constructor tstt_excepTSymtable.create;
  1529. begin
  1530. inherited create('');
  1531. symtabletype:=stt_excepTSymtable;
  1532. end;
  1533. {****************************************************************************
  1534. TMacroSymtable
  1535. ****************************************************************************}
  1536. constructor tmacrosymtable.create(exported: boolean);
  1537. begin
  1538. inherited create('');
  1539. if exported then
  1540. symtabletype:=exportedmacrosymtable
  1541. else
  1542. symtabletype:=localmacrosymtable;
  1543. symtablelevel:=main_program_level;
  1544. end;
  1545. {*****************************************************************************
  1546. Helper Routines
  1547. *****************************************************************************}
  1548. function FullTypeName(def,otherdef:tdef):string;
  1549. var
  1550. s1,s2 : string;
  1551. begin
  1552. s1:=def.typename;
  1553. { When the names are the same try to include the unit name }
  1554. if assigned(otherdef) and
  1555. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1556. begin
  1557. s2:=otherdef.typename;
  1558. if upper(s1)=upper(s2) then
  1559. s1:=def.owner.realname^+'.'+s1;
  1560. end;
  1561. FullTypeName:=s1;
  1562. end;
  1563. procedure incompatibletypes(def1,def2:tdef);
  1564. begin
  1565. { When there is an errordef there is already an error message show }
  1566. if (def2.typ=errordef) or
  1567. (def1.typ=errordef) then
  1568. exit;
  1569. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1570. end;
  1571. procedure hidesym(sym:TSymEntry);
  1572. begin
  1573. sym.realname:='$hidden'+sym.realname;
  1574. include(tsym(sym).symoptions,sp_hidden);
  1575. end;
  1576. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1577. var
  1578. st : TSymtable;
  1579. begin
  1580. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1581. { Write hint where the original symbol was found }
  1582. st:=finduniTSymtable(origsym.owner);
  1583. with tsym(origsym).fileinfo do
  1584. begin
  1585. if assigned(st) and
  1586. (st.symtabletype=globalsymtable) and
  1587. st.iscurrentunit then
  1588. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1589. else if assigned(st.name) then
  1590. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1591. end;
  1592. { Rename duplicate sym to an unreachable name, but it can be
  1593. inserted in the symtable without errors }
  1594. inc(dupnr);
  1595. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1596. if assigned(dupsym) then
  1597. include(tsym(dupsym).symoptions,sp_implicitrename);
  1598. end;
  1599. {*****************************************************************************
  1600. Search
  1601. *****************************************************************************}
  1602. procedure addsymref(sym:tsym);
  1603. begin
  1604. { symbol uses count }
  1605. sym.IncRefCount;
  1606. { unit uses count }
  1607. if assigned(current_module) and
  1608. (sym.owner.symtabletype=globalsymtable) then
  1609. begin
  1610. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1611. internalerror(200501152);
  1612. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1613. end;
  1614. end;
  1615. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1616. var
  1617. hashedid : THashedIDString;
  1618. topclass : tobjectdef;
  1619. context : tobjectdef;
  1620. stackitem : psymtablestackitem;
  1621. begin
  1622. result:=false;
  1623. hashedid.id:=s;
  1624. stackitem:=symtablestack.stack;
  1625. while assigned(stackitem) do
  1626. begin
  1627. srsymtable:=stackitem^.symtable;
  1628. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1629. if assigned(srsym) then
  1630. begin
  1631. topclass:=nil;
  1632. { use the class from withsymtable only when it is
  1633. defined in this unit }
  1634. if (srsymtable.symtabletype=withsymtable) and
  1635. assigned(srsymtable.defowner) and
  1636. (srsymtable.defowner.typ=objectdef) and
  1637. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1638. (srsymtable.defowner.owner.iscurrentunit) then
  1639. topclass:=tobjectdef(srsymtable.defowner)
  1640. else
  1641. begin
  1642. if assigned(current_procinfo) then
  1643. topclass:=current_procinfo.procdef._class;
  1644. end;
  1645. if assigned(current_procinfo) then
  1646. context:=current_procinfo.procdef._class
  1647. else
  1648. context:=nil;
  1649. if tsym(srsym).is_visible_for_object(topclass,context) then
  1650. begin
  1651. { we need to know if a procedure references symbols
  1652. in the static symtable, because then it can't be
  1653. inlined from outside this unit }
  1654. if assigned(current_procinfo) and
  1655. (srsym.owner.symtabletype=staticsymtable) then
  1656. include(current_procinfo.flags,pi_uses_static_symtable);
  1657. addsymref(srsym);
  1658. result:=true;
  1659. exit;
  1660. end;
  1661. end;
  1662. stackitem:=stackitem^.next;
  1663. end;
  1664. srsym:=nil;
  1665. srsymtable:=nil;
  1666. end;
  1667. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1668. var
  1669. hashedid : THashedIDString;
  1670. stackitem : psymtablestackitem;
  1671. begin
  1672. result:=false;
  1673. hashedid.id:=s;
  1674. stackitem:=symtablestack.stack;
  1675. while assigned(stackitem) do
  1676. begin
  1677. {
  1678. It is not possible to have type symbols in:
  1679. records
  1680. objects
  1681. parameters
  1682. Exception are generic definitions and specializations
  1683. that have the parameterized types inserted in the symtable.
  1684. }
  1685. srsymtable:=stackitem^.symtable;
  1686. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1687. (assigned(srsymtable.defowner) and
  1688. (
  1689. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1690. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1691. ) then
  1692. begin
  1693. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1694. if assigned(srsym) and
  1695. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1696. (not assigned(current_procinfo) or
  1697. tsym(srsym).is_visible_for_object(current_procinfo.procdef._class,current_procinfo.procdef._class)) then
  1698. begin
  1699. { we need to know if a procedure references symbols
  1700. in the static symtable, because then it can't be
  1701. inlined from outside this unit }
  1702. if assigned(current_procinfo) and
  1703. (srsym.owner.symtabletype=staticsymtable) then
  1704. include(current_procinfo.flags,pi_uses_static_symtable);
  1705. addsymref(srsym);
  1706. result:=true;
  1707. exit;
  1708. end;
  1709. end;
  1710. stackitem:=stackitem^.next;
  1711. end;
  1712. result:=false;
  1713. srsym:=nil;
  1714. srsymtable:=nil;
  1715. end;
  1716. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1717. var
  1718. pmod : tmodule;
  1719. begin
  1720. pmod:=tmodule(pm);
  1721. result:=false;
  1722. if assigned(pmod.globalsymtable) then
  1723. begin
  1724. srsym:=tsym(pmod.globalsymtable.Find(s));
  1725. if assigned(srsym) then
  1726. begin
  1727. srsymtable:=pmod.globalsymtable;
  1728. addsymref(srsym);
  1729. result:=true;
  1730. exit;
  1731. end;
  1732. end;
  1733. { If the module is the current unit we also need
  1734. to search the local symtable }
  1735. if (pmod=current_module) and
  1736. assigned(pmod.localsymtable) then
  1737. begin
  1738. srsym:=tsym(pmod.localsymtable.Find(s));
  1739. if assigned(srsym) then
  1740. begin
  1741. srsymtable:=pmod.localsymtable;
  1742. addsymref(srsym);
  1743. result:=true;
  1744. exit;
  1745. end;
  1746. end;
  1747. srsym:=nil;
  1748. srsymtable:=nil;
  1749. end;
  1750. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1751. var
  1752. hashedid : THashedIDString;
  1753. currentclassh : tobjectdef;
  1754. begin
  1755. result:=false;
  1756. hashedid.id:=s;
  1757. if assigned(current_procinfo) and assigned(current_procinfo.procdef) then
  1758. currentclassh:=current_procinfo.procdef._class
  1759. else
  1760. currentclassh:=nil;
  1761. while assigned(classh) do
  1762. begin
  1763. srsymtable:=classh.symtable;
  1764. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1765. if assigned(srsym) and
  1766. tsym(srsym).is_visible_for_object(contextclassh,currentclassh) then
  1767. begin
  1768. addsymref(srsym);
  1769. result:=true;
  1770. exit;
  1771. end;
  1772. classh:=classh.childof;
  1773. end;
  1774. srsym:=nil;
  1775. srsymtable:=nil;
  1776. end;
  1777. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1778. var
  1779. def : tdef;
  1780. i : longint;
  1781. begin
  1782. result:=false;
  1783. def:=nil;
  1784. while assigned(classh) do
  1785. begin
  1786. for i:=0 to classh.symtable.DefList.Count-1 do
  1787. begin
  1788. def:=tstoreddef(classh.symtable.DefList[i]);
  1789. { Find also all hidden private methods to
  1790. be compatible with delphi, see tw6203 (PFV) }
  1791. if (def.typ=procdef) and
  1792. (po_msgint in tprocdef(def).procoptions) and
  1793. (tprocdef(def).messageinf.i=msgid) then
  1794. begin
  1795. srdef:=def;
  1796. srsym:=tprocdef(def).procsym;
  1797. srsymtable:=classh.symtable;
  1798. addsymref(srsym);
  1799. result:=true;
  1800. exit;
  1801. end;
  1802. end;
  1803. classh:=classh.childof;
  1804. end;
  1805. srdef:=nil;
  1806. srsym:=nil;
  1807. srsymtable:=nil;
  1808. end;
  1809. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1810. var
  1811. def : tdef;
  1812. i : longint;
  1813. begin
  1814. result:=false;
  1815. def:=nil;
  1816. while assigned(classh) do
  1817. begin
  1818. for i:=0 to classh.symtable.DefList.Count-1 do
  1819. begin
  1820. def:=tstoreddef(classh.symtable.DefList[i]);
  1821. { Find also all hidden private methods to
  1822. be compatible with delphi, see tw6203 (PFV) }
  1823. if (def.typ=procdef) and
  1824. (po_msgstr in tprocdef(def).procoptions) and
  1825. (tprocdef(def).messageinf.str^=s) then
  1826. begin
  1827. srsym:=tprocdef(def).procsym;
  1828. srsymtable:=classh.symtable;
  1829. addsymref(srsym);
  1830. result:=true;
  1831. exit;
  1832. end;
  1833. end;
  1834. classh:=classh.childof;
  1835. end;
  1836. srsym:=nil;
  1837. srsymtable:=nil;
  1838. end;
  1839. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1840. var
  1841. sym : Tprocsym;
  1842. hashedid : THashedIDString;
  1843. curreq,
  1844. besteq : tequaltype;
  1845. currpd,
  1846. bestpd : tprocdef;
  1847. stackitem : psymtablestackitem;
  1848. begin
  1849. hashedid.id:='assign';
  1850. besteq:=te_incompatible;
  1851. bestpd:=nil;
  1852. stackitem:=symtablestack.stack;
  1853. while assigned(stackitem) do
  1854. begin
  1855. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1856. if sym<>nil then
  1857. begin
  1858. if sym.typ<>procsym then
  1859. internalerror(200402031);
  1860. { if the source type is an alias then this is only the second choice,
  1861. if you mess with this code, check tw4093 }
  1862. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1863. if curreq>besteq then
  1864. begin
  1865. besteq:=curreq;
  1866. bestpd:=currpd;
  1867. if (besteq=te_exact) then
  1868. break;
  1869. end;
  1870. end;
  1871. stackitem:=stackitem^.next;
  1872. end;
  1873. result:=bestpd;
  1874. end;
  1875. function search_system_type(const s: TIDString): ttypesym;
  1876. var
  1877. sym : tsym;
  1878. begin
  1879. sym:=tsym(systemunit.Find(s));
  1880. if not assigned(sym) or
  1881. (sym.typ<>typesym) then
  1882. cgmessage1(cg_f_unknown_system_type,s);
  1883. result:=ttypesym(sym);
  1884. end;
  1885. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1886. { searches n in symtable of pd and all anchestors }
  1887. var
  1888. hashedid : THashedIDString;
  1889. srsym : tsym;
  1890. begin
  1891. hashedid.id:=s;
  1892. while assigned(pd) do
  1893. begin
  1894. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1895. if assigned(srsym) then
  1896. begin
  1897. search_class_member:=srsym;
  1898. exit;
  1899. end;
  1900. pd:=pd.childof;
  1901. end;
  1902. search_class_member:=nil;
  1903. end;
  1904. function search_macro(const s : string):tsym;
  1905. var
  1906. stackitem : psymtablestackitem;
  1907. hashedid : THashedIDString;
  1908. srsym : tsym;
  1909. begin
  1910. hashedid.id:=s;
  1911. { First search the localmacrosymtable before searching the
  1912. global macrosymtables from the units }
  1913. if assigned(current_module) then
  1914. begin
  1915. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1916. if assigned(srsym) then
  1917. begin
  1918. result:= srsym;
  1919. exit;
  1920. end;
  1921. end;
  1922. stackitem:=macrosymtablestack.stack;
  1923. while assigned(stackitem) do
  1924. begin
  1925. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1926. if assigned(srsym) then
  1927. begin
  1928. result:= srsym;
  1929. exit;
  1930. end;
  1931. stackitem:=stackitem^.next;
  1932. end;
  1933. result:= nil;
  1934. end;
  1935. function defined_macro(const s : string):boolean;
  1936. var
  1937. mac: tmacro;
  1938. begin
  1939. mac:=tmacro(search_macro(s));
  1940. if assigned(mac) then
  1941. begin
  1942. mac.is_used:=true;
  1943. defined_macro:=mac.defined;
  1944. end
  1945. else
  1946. defined_macro:=false;
  1947. end;
  1948. {****************************************************************************
  1949. Object Helpers
  1950. ****************************************************************************}
  1951. procedure search_class_overloads(aprocsym : tprocsym);
  1952. { searches n in symtable of pd and all anchestors }
  1953. var
  1954. hashedid : THashedIDString;
  1955. srsym : tprocsym;
  1956. objdef : tobjectdef;
  1957. begin
  1958. if aprocsym.overloadchecked then
  1959. exit;
  1960. aprocsym.overloadchecked:=true;
  1961. if (aprocsym.owner.symtabletype<>ObjectSymtable) then
  1962. internalerror(200111021);
  1963. objdef:=tobjectdef(aprocsym.owner.defowner);
  1964. { we start in the parent }
  1965. if not assigned(objdef.childof) then
  1966. exit;
  1967. objdef:=objdef.childof;
  1968. hashedid.id:=aprocsym.name;
  1969. while assigned(objdef) do
  1970. begin
  1971. srsym:=tprocsym(objdef.symtable.FindWithHash(hashedid));
  1972. if assigned(srsym) then
  1973. begin
  1974. if (srsym.typ<>procsym) then
  1975. internalerror(200111022);
  1976. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner),tobjectdef(aprocsym.owner.defowner)) then
  1977. begin
  1978. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1979. { we can stop if the overloads were already added
  1980. for the found symbol }
  1981. if srsym.overloadchecked then
  1982. break;
  1983. end;
  1984. end;
  1985. { next parent }
  1986. objdef:=objdef.childof;
  1987. end;
  1988. end;
  1989. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  1990. begin
  1991. if (tsym(sym).typ=propertysym) and
  1992. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  1993. ppointer(arg)^:=sym;
  1994. end;
  1995. function search_default_property(pd : tobjectdef) : tpropertysym;
  1996. { returns the default property of a class, searches also anchestors }
  1997. var
  1998. _defaultprop : tpropertysym;
  1999. begin
  2000. _defaultprop:=nil;
  2001. while assigned(pd) do
  2002. begin
  2003. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2004. if assigned(_defaultprop) then
  2005. break;
  2006. pd:=pd.childof;
  2007. end;
  2008. search_default_property:=_defaultprop;
  2009. end;
  2010. {****************************************************************************
  2011. Macro Helpers
  2012. ****************************************************************************}
  2013. procedure def_system_macro(const name : string);
  2014. var
  2015. mac : tmacro;
  2016. s: string;
  2017. begin
  2018. if name = '' then
  2019. internalerror(2004121202);
  2020. s:= upper(name);
  2021. mac:=tmacro(search_macro(s));
  2022. if not assigned(mac) then
  2023. begin
  2024. mac:=tmacro.create(s);
  2025. if assigned(current_module) then
  2026. current_module.localmacrosymtable.insert(mac)
  2027. else
  2028. initialmacrosymtable.insert(mac);
  2029. end;
  2030. if not mac.defined then
  2031. Message1(parser_c_macro_defined,mac.name);
  2032. mac.defined:=true;
  2033. end;
  2034. procedure set_system_macro(const name, value : string);
  2035. var
  2036. mac : tmacro;
  2037. s: string;
  2038. begin
  2039. if name = '' then
  2040. internalerror(2004121203);
  2041. s:= upper(name);
  2042. mac:=tmacro(search_macro(s));
  2043. if not assigned(mac) then
  2044. begin
  2045. mac:=tmacro.create(s);
  2046. if assigned(current_module) then
  2047. current_module.localmacrosymtable.insert(mac)
  2048. else
  2049. initialmacrosymtable.insert(mac);
  2050. end
  2051. else
  2052. begin
  2053. mac.is_compiler_var:=false;
  2054. if assigned(mac.buftext) then
  2055. freemem(mac.buftext,mac.buflen);
  2056. end;
  2057. Message2(parser_c_macro_set_to,mac.name,value);
  2058. mac.buflen:=length(value);
  2059. getmem(mac.buftext,mac.buflen);
  2060. move(value[1],mac.buftext^,mac.buflen);
  2061. mac.defined:=true;
  2062. end;
  2063. procedure set_system_compvar(const name, value : string);
  2064. var
  2065. mac : tmacro;
  2066. s: string;
  2067. begin
  2068. if name = '' then
  2069. internalerror(2004121204);
  2070. s:= upper(name);
  2071. mac:=tmacro(search_macro(s));
  2072. if not assigned(mac) then
  2073. begin
  2074. mac:=tmacro.create(s);
  2075. mac.is_compiler_var:=true;
  2076. if assigned(current_module) then
  2077. current_module.localmacrosymtable.insert(mac)
  2078. else
  2079. initialmacrosymtable.insert(mac);
  2080. end
  2081. else
  2082. begin
  2083. mac.is_compiler_var:=true;
  2084. if assigned(mac.buftext) then
  2085. freemem(mac.buftext,mac.buflen);
  2086. end;
  2087. Message2(parser_c_macro_set_to,mac.name,value);
  2088. mac.buflen:=length(value);
  2089. getmem(mac.buftext,mac.buflen);
  2090. move(value[1],mac.buftext^,mac.buflen);
  2091. mac.defined:=true;
  2092. end;
  2093. procedure undef_system_macro(const name : string);
  2094. var
  2095. mac : tmacro;
  2096. s: string;
  2097. begin
  2098. if name = '' then
  2099. internalerror(2004121205);
  2100. s:= upper(name);
  2101. mac:=tmacro(search_macro(s));
  2102. if not assigned(mac) then
  2103. {If not found, then it's already undefined.}
  2104. else
  2105. begin
  2106. if mac.defined then
  2107. Message1(parser_c_macro_undefined,mac.name);
  2108. mac.defined:=false;
  2109. mac.is_compiler_var:=false;
  2110. { delete old definition }
  2111. if assigned(mac.buftext) then
  2112. begin
  2113. freemem(mac.buftext,mac.buflen);
  2114. mac.buftext:=nil;
  2115. end;
  2116. end;
  2117. end;
  2118. {$ifdef UNITALIASES}
  2119. {****************************************************************************
  2120. TUNIT_ALIAS
  2121. ****************************************************************************}
  2122. constructor tunit_alias.create(const n:string);
  2123. var
  2124. i : longint;
  2125. begin
  2126. i:=pos('=',n);
  2127. if i=0 then
  2128. fail;
  2129. inherited createname(Copy(n,1,i-1));
  2130. newname:=stringdup(Copy(n,i+1,255));
  2131. end;
  2132. destructor tunit_alias.destroy;
  2133. begin
  2134. stringdispose(newname);
  2135. inherited destroy;
  2136. end;
  2137. procedure addunitalias(const n:string);
  2138. begin
  2139. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2140. end;
  2141. function getunitalias(const n:string):string;
  2142. var
  2143. p : punit_alias;
  2144. begin
  2145. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2146. if assigned(p) then
  2147. getunitalias:=punit_alias(p).newname^
  2148. else
  2149. getunitalias:=n;
  2150. end;
  2151. {$endif UNITALIASES}
  2152. {****************************************************************************
  2153. Init/Done Symtable
  2154. ****************************************************************************}
  2155. procedure InitSymtable;
  2156. begin
  2157. { Reset symbolstack }
  2158. symtablestack:=nil;
  2159. systemunit:=nil;
  2160. { create error syms and def }
  2161. generrorsym:=terrorsym.create;
  2162. generrordef:=terrordef.create;
  2163. { macros }
  2164. initialmacrosymtable:=tmacrosymtable.create(false);
  2165. macrosymtablestack:=TSymtablestack.create;
  2166. macrosymtablestack.push(initialmacrosymtable);
  2167. {$ifdef UNITALIASES}
  2168. { unit aliases }
  2169. unitaliases:=TFPHashObjectList.create;
  2170. {$endif}
  2171. { set some global vars to nil, might be important for the ide }
  2172. class_tobject:=nil;
  2173. interface_iunknown:=nil;
  2174. rec_tguid:=nil;
  2175. aktobjectdef:=nil;
  2176. dupnr:=0;
  2177. end;
  2178. procedure DoneSymtable;
  2179. begin
  2180. generrorsym.owner:=nil;
  2181. generrorsym.free;
  2182. generrordef.owner:=nil;
  2183. generrordef.free;
  2184. initialmacrosymtable.free;
  2185. macrosymtablestack.free;
  2186. {$ifdef UNITALIASES}
  2187. unitaliases.free;
  2188. {$endif}
  2189. end;
  2190. end.