symtable.pas 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  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 appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  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.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  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(vardef,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 (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<>fieldoffset) then
  1079. internalerror(2008051002);
  1080. if is_ordinal(vardef) and
  1081. (vardef.packedbitsize mod 8 <> 0) then
  1082. begin
  1083. tmpsize:=vardef.packedbitsize;
  1084. sizectr:=tmpsize+7;
  1085. repeat
  1086. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,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(vardef,fieldoffset));
  1094. if not(derefclass) then
  1095. inc(curroffset,vardef.size*8)
  1096. else
  1097. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1098. end;
  1099. end
  1100. else
  1101. begin
  1102. { curoffset: address right after the previous field }
  1103. while (fieldoffset>curroffset) do
  1104. begin
  1105. symdeflist.add(tllvmshadowsymtableentry.create(s8inttype,curroffset));
  1106. inc(curroffset);
  1107. end;
  1108. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1109. if not(derefclass) then
  1110. inc(curroffset,vardef.size)
  1111. else
  1112. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1113. end
  1114. end
  1115. end;
  1116. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1117. begin
  1118. case equivst.usefieldalignment of
  1119. { already correct in this case }
  1120. bit_alignment,
  1121. { handled by llvm }
  1122. C_alignment:
  1123. ;
  1124. else
  1125. begin
  1126. { add padding fields }
  1127. while (finalsize>curroffset) do
  1128. begin
  1129. symdeflist.add(tllvmshadowsymtableentry.create(s8inttype,curroffset));
  1130. inc(curroffset);
  1131. end;
  1132. end;
  1133. end;
  1134. end;
  1135. procedure tllvmshadowsymtable.findvariantstarts(item: TObject; arg: pointer);
  1136. var
  1137. variantstarts: tfplist absolute arg;
  1138. sym: tfieldvarsym absolute item;
  1139. lastoffset: aint;
  1140. newalignment: aint;
  1141. begin
  1142. if (tsym(item).typ<>fieldvarsym) then
  1143. exit;
  1144. { a "better" algorithm might be to use the largest }
  1145. { variant in case of (bit)packing, since then }
  1146. { alignment doesn't matter }
  1147. if (vo_is_first_field in sym.varoptions) then
  1148. begin
  1149. { we assume that all fields are processed in order. }
  1150. { the most deeply nested variant always comes last }
  1151. { in Pascal }
  1152. if (variantstarts.count<>0) then
  1153. lastoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  1154. else
  1155. lastoffset:=-1;
  1156. if (lastoffset=sym.fieldoffset) then
  1157. begin
  1158. if (equivst.fieldalignment<>bit_alignment) then
  1159. newalignment:=used_align(sym.vardef.alignment,current_settings.alignment.recordalignmin,equivst.fieldalignment)
  1160. else
  1161. newalignment:=1;
  1162. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  1163. tfieldvarsym(variantstarts[variantstarts.count-1]):=sym
  1164. end
  1165. else if (lastoffset<sym.fieldoffset) then
  1166. variantstarts.add(sym)
  1167. else
  1168. internalerror(2008051003);
  1169. end;
  1170. end;
  1171. procedure tllvmshadowsymtable.buildtable(variantstarts: tfplist);
  1172. var
  1173. lastvaroffsetprocessed: aint;
  1174. i, equivcount, varcount: longint;
  1175. begin
  1176. { if it's an object/class, the first entry is the parent (if there is one) }
  1177. if (equivst.symtabletype=objectsymtable) and
  1178. assigned(tobjectdef(equivst.defowner).childof) then
  1179. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class(tobjectdef(equivst.defowner).childof));
  1180. equivcount:=equivst.symlist.count;
  1181. varcount:=0;
  1182. i:=0;
  1183. lastvaroffsetprocessed:=-1;
  1184. while (i<equivcount) do
  1185. begin
  1186. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1187. begin
  1188. inc(i);
  1189. continue;
  1190. end;
  1191. { start of a new variant? }
  1192. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1193. begin
  1194. { if we want to process the same variant offset twice, it means that we }
  1195. { got to the end and are trying to process the next variant part -> stop }
  1196. if (tfieldvarsym(equivst.symlist[i]).fieldoffset=lastvaroffsetprocessed) then
  1197. break;
  1198. if (varcount>=variantstarts.count) or
  1199. (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1200. internalerror(2008051005);
  1201. { new variant part -> use the one with the biggest alignment }
  1202. i:=equivst.symlist.indexof(tobject(variantstarts[varcount]));
  1203. lastvaroffsetprocessed:=tfieldvarsym(equivst.symlist[i]).fieldoffset;
  1204. inc(varcount);
  1205. if (i<0) then
  1206. internalerror(2008051004);
  1207. end;
  1208. appenddefoffset(tfieldvarsym(equivst.symlist[i]).vardef,tfieldvarsym(equivst.symlist[i]).fieldoffset,false);
  1209. inc(i);
  1210. end;
  1211. addalignmentpadding(equivst.datasize);
  1212. end;
  1213. procedure tllvmshadowsymtable.buildmapping(variantstarts: tfplist);
  1214. var
  1215. i, varcount: longint;
  1216. shadowindex: longint;
  1217. equivcount : longint;
  1218. begin
  1219. varcount:=0;
  1220. shadowindex:=0;
  1221. equivcount:=equivst.symlist.count;
  1222. i:=0;
  1223. while (i < equivcount) do
  1224. begin
  1225. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1226. begin
  1227. inc(i);
  1228. continue;
  1229. end;
  1230. { start of a new variant? }
  1231. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1232. begin
  1233. { the variant can either come after the current one, or }
  1234. { be at the same level }
  1235. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1236. dec(varcount);
  1237. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1238. internalerror(2008051006);
  1239. { reset the shadowindex to the start of this variant. }
  1240. { in case the llvmfieldnr is not (yet) set for this }
  1241. { field, shadowindex will simply be reset to zero and }
  1242. { we'll start searching from the start of the record }
  1243. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  1244. if (varcount<pred(variantstarts.count)) then
  1245. inc(varcount);
  1246. end;
  1247. { find the last shadowfield whose offset <= the current field's offset }
  1248. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<tfieldvarsym(equivst.symlist[i]).fieldoffset) and
  1249. (shadowindex<symdeflist.count-1) and
  1250. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset>=tfieldvarsym(equivst.symlist[i]).fieldoffset) do
  1251. inc(shadowindex);
  1252. { set the field number and potential offset from that field (in case }
  1253. { of overlapping variants) }
  1254. tfieldvarsym(equivst.symlist[i]).llvmfieldnr:=shadowindex;
  1255. tfieldvarsym(equivst.symlist[i]).offsetfromllvmfield:=
  1256. tfieldvarsym(equivst.symlist[i]).fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  1257. inc(i);
  1258. end;
  1259. end;
  1260. procedure tllvmshadowsymtable.generate;
  1261. var
  1262. variantstarts: tfplist;
  1263. begin
  1264. variantstarts:=tfplist.create;
  1265. { first go through the entire record and }
  1266. { store the fieldvarsyms of the variants }
  1267. { with the highest alignment }
  1268. equivst.symlist.foreachcall(@findvariantstarts,pointer(variantstarts));
  1269. { now go through the regular fields and the selected variants, }
  1270. { and add them to the llvm shadow record symtable }
  1271. buildtable(variantstarts);
  1272. { finally map all original fields to the llvm definition }
  1273. buildmapping(variantstarts);
  1274. variantstarts.free;
  1275. end;
  1276. {$endif support_llvm}
  1277. {****************************************************************************
  1278. TAbstractLocalSymtable
  1279. ****************************************************************************}
  1280. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1281. var
  1282. oldtyp : byte;
  1283. begin
  1284. oldtyp:=ppufile.entrytyp;
  1285. ppufile.entrytyp:=subentryid;
  1286. { write definitions }
  1287. writedefs(ppufile);
  1288. { write symbols }
  1289. writesyms(ppufile);
  1290. ppufile.entrytyp:=oldtyp;
  1291. end;
  1292. function tabstractlocalsymtable.count_locals:longint;
  1293. var
  1294. i : longint;
  1295. sym : tsym;
  1296. begin
  1297. result:=0;
  1298. for i:=0 to SymList.Count-1 do
  1299. begin
  1300. sym:=tsym(SymList[i]);
  1301. { Count only varsyms, but ignore the funcretsym }
  1302. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1303. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1304. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1305. (tstoredsym(sym).refs>0)) then
  1306. inc(result);
  1307. end;
  1308. end;
  1309. {****************************************************************************
  1310. TLocalSymtable
  1311. ****************************************************************************}
  1312. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1313. begin
  1314. inherited create('');
  1315. defowner:=adefowner;
  1316. symtabletype:=localsymtable;
  1317. symtablelevel:=level;
  1318. end;
  1319. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1320. var
  1321. hsym : tsym;
  1322. begin
  1323. if not assigned(defowner) or
  1324. (defowner.typ<>procdef) then
  1325. internalerror(200602042);
  1326. result:=false;
  1327. hsym:=tsym(FindWithHash(hashedid));
  1328. if assigned(hsym) then
  1329. begin
  1330. { a local and the function can have the same
  1331. name in TP and Delphi, but RESULT not }
  1332. if (m_duplicate_names in current_settings.modeswitches) and
  1333. (hsym.typ in [absolutevarsym,localvarsym]) and
  1334. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1335. not((m_result in current_settings.modeswitches) and
  1336. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1337. HideSym(hsym)
  1338. else
  1339. DuplicateSym(hashedid,sym,hsym);
  1340. result:=true;
  1341. exit;
  1342. end;
  1343. { check also parasymtable, this needs to be done here becuase
  1344. of the special situation with the funcret sym that needs to be
  1345. hidden for tp and delphi modes }
  1346. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1347. if assigned(hsym) then
  1348. begin
  1349. { a local and the function can have the same
  1350. name in TP and Delphi, but RESULT not }
  1351. if (m_duplicate_names in current_settings.modeswitches) and
  1352. (sym.typ in [absolutevarsym,localvarsym]) and
  1353. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1354. not((m_result in current_settings.modeswitches) and
  1355. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1356. Hidesym(sym)
  1357. else
  1358. DuplicateSym(hashedid,sym,hsym);
  1359. result:=true;
  1360. exit;
  1361. end;
  1362. { check ObjectSymtable, skip this for funcret sym because
  1363. that will always be positive because it has the same name
  1364. as the procsym }
  1365. if not is_funcret_sym(sym) and
  1366. (defowner.typ=procdef) and
  1367. assigned(tprocdef(defowner)._class) and
  1368. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1369. (
  1370. not(m_delphi in current_settings.modeswitches) or
  1371. is_object(tprocdef(defowner)._class)
  1372. ) then
  1373. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1374. end;
  1375. {****************************************************************************
  1376. TParaSymtable
  1377. ****************************************************************************}
  1378. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1379. begin
  1380. inherited create('');
  1381. defowner:=adefowner;
  1382. symtabletype:=parasymtable;
  1383. symtablelevel:=level;
  1384. end;
  1385. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1386. begin
  1387. result:=inherited checkduplicate(hashedid,sym);
  1388. if result then
  1389. exit;
  1390. if not(m_duplicate_names in current_settings.modeswitches) and
  1391. (defowner.typ=procdef) and
  1392. assigned(tprocdef(defowner)._class) and
  1393. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1394. (
  1395. not(m_delphi in current_settings.modeswitches) or
  1396. is_object(tprocdef(defowner)._class)
  1397. ) then
  1398. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1399. end;
  1400. {****************************************************************************
  1401. TAbstractUniTSymtable
  1402. ****************************************************************************}
  1403. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1404. begin
  1405. inherited create(n);
  1406. moduleid:=id;
  1407. end;
  1408. function tabstractuniTSymtable.iscurrentunit:boolean;
  1409. begin
  1410. result:=assigned(current_module) and
  1411. (
  1412. (current_module.globalsymtable=self) or
  1413. (current_module.localsymtable=self)
  1414. );
  1415. end;
  1416. {****************************************************************************
  1417. TStaticSymtable
  1418. ****************************************************************************}
  1419. constructor tstaticsymtable.create(const n : string;id:word);
  1420. begin
  1421. inherited create(n,id);
  1422. symtabletype:=staticsymtable;
  1423. symtablelevel:=main_program_level;
  1424. end;
  1425. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1426. begin
  1427. inherited ppuload(ppufile);
  1428. { now we can deref the syms and defs }
  1429. deref;
  1430. end;
  1431. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1432. begin
  1433. inherited ppuwrite(ppufile);
  1434. end;
  1435. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1436. var
  1437. hsym : tsym;
  1438. begin
  1439. result:=false;
  1440. hsym:=tsym(FindWithHash(hashedid));
  1441. if assigned(hsym) then
  1442. begin
  1443. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1444. unit, the unit can then not be accessed anymore using
  1445. <unit>.<id>, so we can hide the symbol }
  1446. if (m_delphi in current_settings.modeswitches) and
  1447. (hsym.typ=symconst.unitsym) then
  1448. HideSym(hsym)
  1449. else
  1450. DuplicateSym(hashedid,sym,hsym);
  1451. result:=true;
  1452. exit;
  1453. end;
  1454. if (current_module.localsymtable=self) and
  1455. assigned(current_module.globalsymtable) then
  1456. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1457. end;
  1458. {****************************************************************************
  1459. TGlobalSymtable
  1460. ****************************************************************************}
  1461. constructor tglobalsymtable.create(const n : string;id:word);
  1462. begin
  1463. inherited create(n,id);
  1464. symtabletype:=globalsymtable;
  1465. symtablelevel:=main_program_level;
  1466. end;
  1467. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1468. begin
  1469. inherited ppuload(ppufile);
  1470. { now we can deref the syms and defs }
  1471. deref;
  1472. end;
  1473. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1474. begin
  1475. { write the symtable entries }
  1476. inherited ppuwrite(ppufile);
  1477. end;
  1478. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1479. var
  1480. hsym : tsym;
  1481. begin
  1482. result:=false;
  1483. hsym:=tsym(FindWithHash(hashedid));
  1484. if assigned(hsym) then
  1485. begin
  1486. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1487. unit, the unit can then not be accessed anymore using
  1488. <unit>.<id>, so we can hide the symbol }
  1489. if (m_delphi in current_settings.modeswitches) and
  1490. (hsym.typ=symconst.unitsym) then
  1491. HideSym(hsym)
  1492. else
  1493. DuplicateSym(hashedid,sym,hsym);
  1494. result:=true;
  1495. exit;
  1496. end;
  1497. end;
  1498. {****************************************************************************
  1499. TWITHSYMTABLE
  1500. ****************************************************************************}
  1501. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1502. begin
  1503. inherited create('');
  1504. symtabletype:=withsymtable;
  1505. withrefnode:=refnode;
  1506. { Replace SymList with the passed symlist }
  1507. SymList.free;
  1508. SymList:=ASymList;
  1509. defowner:=aowner;
  1510. end;
  1511. destructor twithsymtable.destroy;
  1512. begin
  1513. withrefnode.free;
  1514. { Disable SymList because we don't Own it }
  1515. SymList:=nil;
  1516. inherited destroy;
  1517. end;
  1518. procedure twithsymtable.clear;
  1519. begin
  1520. { remove no entry from a withsymtable as it is only a pointer to the
  1521. recorddef or objectdef symtable }
  1522. end;
  1523. procedure twithsymtable.insertdef(def:TDefEntry);
  1524. begin
  1525. { Definitions can't be registered in the withsymtable
  1526. because the withsymtable is removed after the with block.
  1527. We can't easily solve it here because the next symtable in the
  1528. stack is not known. }
  1529. internalerror(200602046);
  1530. end;
  1531. {****************************************************************************
  1532. TSTT_ExceptionSymtable
  1533. ****************************************************************************}
  1534. constructor tstt_excepTSymtable.create;
  1535. begin
  1536. inherited create('');
  1537. symtabletype:=stt_excepTSymtable;
  1538. end;
  1539. {****************************************************************************
  1540. TMacroSymtable
  1541. ****************************************************************************}
  1542. constructor tmacrosymtable.create(exported: boolean);
  1543. begin
  1544. inherited create('');
  1545. if exported then
  1546. symtabletype:=exportedmacrosymtable
  1547. else
  1548. symtabletype:=localmacrosymtable;
  1549. symtablelevel:=main_program_level;
  1550. end;
  1551. {*****************************************************************************
  1552. Helper Routines
  1553. *****************************************************************************}
  1554. function FullTypeName(def,otherdef:tdef):string;
  1555. var
  1556. s1,s2 : string;
  1557. begin
  1558. s1:=def.typename;
  1559. { When the names are the same try to include the unit name }
  1560. if assigned(otherdef) and
  1561. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1562. begin
  1563. s2:=otherdef.typename;
  1564. if upper(s1)=upper(s2) then
  1565. s1:=def.owner.realname^+'.'+s1;
  1566. end;
  1567. FullTypeName:=s1;
  1568. end;
  1569. procedure incompatibletypes(def1,def2:tdef);
  1570. begin
  1571. { When there is an errordef there is already an error message show }
  1572. if (def2.typ=errordef) or
  1573. (def1.typ=errordef) then
  1574. exit;
  1575. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1576. end;
  1577. procedure hidesym(sym:TSymEntry);
  1578. begin
  1579. sym.realname:='$hidden'+sym.realname;
  1580. include(tsym(sym).symoptions,sp_hidden);
  1581. end;
  1582. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1583. var
  1584. st : TSymtable;
  1585. begin
  1586. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1587. { Write hint where the original symbol was found }
  1588. st:=finduniTSymtable(origsym.owner);
  1589. with tsym(origsym).fileinfo do
  1590. begin
  1591. if assigned(st) and
  1592. (st.symtabletype=globalsymtable) and
  1593. st.iscurrentunit then
  1594. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1595. else if assigned(st.name) then
  1596. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1597. end;
  1598. { Rename duplicate sym to an unreachable name, but it can be
  1599. inserted in the symtable without errors }
  1600. inc(dupnr);
  1601. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1602. if assigned(dupsym) then
  1603. include(tsym(dupsym).symoptions,sp_implicitrename);
  1604. end;
  1605. {*****************************************************************************
  1606. Search
  1607. *****************************************************************************}
  1608. procedure addsymref(sym:tsym);
  1609. begin
  1610. { symbol uses count }
  1611. sym.IncRefCount;
  1612. { unit uses count }
  1613. if assigned(current_module) and
  1614. (sym.owner.symtabletype=globalsymtable) then
  1615. begin
  1616. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1617. internalerror(200501152);
  1618. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1619. end;
  1620. end;
  1621. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1622. var
  1623. hashedid : THashedIDString;
  1624. topclass : tobjectdef;
  1625. context : tobjectdef;
  1626. stackitem : psymtablestackitem;
  1627. begin
  1628. result:=false;
  1629. hashedid.id:=s;
  1630. stackitem:=symtablestack.stack;
  1631. while assigned(stackitem) do
  1632. begin
  1633. srsymtable:=stackitem^.symtable;
  1634. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1635. if assigned(srsym) then
  1636. begin
  1637. topclass:=nil;
  1638. { use the class from withsymtable only when it is
  1639. defined in this unit }
  1640. if (srsymtable.symtabletype=withsymtable) and
  1641. assigned(srsymtable.defowner) and
  1642. (srsymtable.defowner.typ=objectdef) and
  1643. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1644. (srsymtable.defowner.owner.iscurrentunit) then
  1645. topclass:=tobjectdef(srsymtable.defowner)
  1646. else
  1647. begin
  1648. if assigned(current_procinfo) then
  1649. topclass:=current_procinfo.procdef._class;
  1650. end;
  1651. if assigned(current_procinfo) then
  1652. context:=current_procinfo.procdef._class
  1653. else
  1654. context:=nil;
  1655. if tsym(srsym).is_visible_for_object(topclass,context) then
  1656. begin
  1657. { we need to know if a procedure references symbols
  1658. in the static symtable, because then it can't be
  1659. inlined from outside this unit }
  1660. if assigned(current_procinfo) and
  1661. (srsym.owner.symtabletype=staticsymtable) then
  1662. include(current_procinfo.flags,pi_uses_static_symtable);
  1663. addsymref(srsym);
  1664. result:=true;
  1665. exit;
  1666. end;
  1667. end;
  1668. stackitem:=stackitem^.next;
  1669. end;
  1670. srsym:=nil;
  1671. srsymtable:=nil;
  1672. end;
  1673. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1674. var
  1675. hashedid : THashedIDString;
  1676. stackitem : psymtablestackitem;
  1677. begin
  1678. result:=false;
  1679. hashedid.id:=s;
  1680. stackitem:=symtablestack.stack;
  1681. while assigned(stackitem) do
  1682. begin
  1683. {
  1684. It is not possible to have type symbols in:
  1685. records
  1686. objects
  1687. parameters
  1688. Exception are generic definitions and specializations
  1689. that have the parameterized types inserted in the symtable.
  1690. }
  1691. srsymtable:=stackitem^.symtable;
  1692. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1693. (assigned(srsymtable.defowner) and
  1694. (
  1695. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1696. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1697. ) then
  1698. begin
  1699. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1700. if assigned(srsym) and
  1701. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1702. (not assigned(current_procinfo) or
  1703. tsym(srsym).is_visible_for_object(current_procinfo.procdef._class,current_procinfo.procdef._class)) then
  1704. begin
  1705. { we need to know if a procedure references symbols
  1706. in the static symtable, because then it can't be
  1707. inlined from outside this unit }
  1708. if assigned(current_procinfo) and
  1709. (srsym.owner.symtabletype=staticsymtable) then
  1710. include(current_procinfo.flags,pi_uses_static_symtable);
  1711. addsymref(srsym);
  1712. result:=true;
  1713. exit;
  1714. end;
  1715. end;
  1716. stackitem:=stackitem^.next;
  1717. end;
  1718. result:=false;
  1719. srsym:=nil;
  1720. srsymtable:=nil;
  1721. end;
  1722. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1723. var
  1724. pmod : tmodule;
  1725. begin
  1726. pmod:=tmodule(pm);
  1727. result:=false;
  1728. if assigned(pmod.globalsymtable) then
  1729. begin
  1730. srsym:=tsym(pmod.globalsymtable.Find(s));
  1731. if assigned(srsym) then
  1732. begin
  1733. srsymtable:=pmod.globalsymtable;
  1734. addsymref(srsym);
  1735. result:=true;
  1736. exit;
  1737. end;
  1738. end;
  1739. { If the module is the current unit we also need
  1740. to search the local symtable }
  1741. if (pmod=current_module) and
  1742. assigned(pmod.localsymtable) then
  1743. begin
  1744. srsym:=tsym(pmod.localsymtable.Find(s));
  1745. if assigned(srsym) then
  1746. begin
  1747. srsymtable:=pmod.localsymtable;
  1748. addsymref(srsym);
  1749. result:=true;
  1750. exit;
  1751. end;
  1752. end;
  1753. srsym:=nil;
  1754. srsymtable:=nil;
  1755. end;
  1756. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1757. var
  1758. hashedid : THashedIDString;
  1759. currentclassh : tobjectdef;
  1760. begin
  1761. result:=false;
  1762. hashedid.id:=s;
  1763. if assigned(current_procinfo) and assigned(current_procinfo.procdef) then
  1764. currentclassh:=current_procinfo.procdef._class
  1765. else
  1766. currentclassh:=nil;
  1767. while assigned(classh) do
  1768. begin
  1769. srsymtable:=classh.symtable;
  1770. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1771. if assigned(srsym) and
  1772. tsym(srsym).is_visible_for_object(contextclassh,currentclassh) then
  1773. begin
  1774. addsymref(srsym);
  1775. result:=true;
  1776. exit;
  1777. end;
  1778. classh:=classh.childof;
  1779. end;
  1780. srsym:=nil;
  1781. srsymtable:=nil;
  1782. end;
  1783. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1784. var
  1785. def : tdef;
  1786. i : longint;
  1787. begin
  1788. result:=false;
  1789. def:=nil;
  1790. while assigned(classh) do
  1791. begin
  1792. for i:=0 to classh.symtable.DefList.Count-1 do
  1793. begin
  1794. def:=tstoreddef(classh.symtable.DefList[i]);
  1795. { Find also all hidden private methods to
  1796. be compatible with delphi, see tw6203 (PFV) }
  1797. if (def.typ=procdef) and
  1798. (po_msgint in tprocdef(def).procoptions) and
  1799. (tprocdef(def).messageinf.i=msgid) then
  1800. begin
  1801. srdef:=def;
  1802. srsym:=tprocdef(def).procsym;
  1803. srsymtable:=classh.symtable;
  1804. addsymref(srsym);
  1805. result:=true;
  1806. exit;
  1807. end;
  1808. end;
  1809. classh:=classh.childof;
  1810. end;
  1811. srdef:=nil;
  1812. srsym:=nil;
  1813. srsymtable:=nil;
  1814. end;
  1815. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1816. var
  1817. def : tdef;
  1818. i : longint;
  1819. begin
  1820. result:=false;
  1821. def:=nil;
  1822. while assigned(classh) do
  1823. begin
  1824. for i:=0 to classh.symtable.DefList.Count-1 do
  1825. begin
  1826. def:=tstoreddef(classh.symtable.DefList[i]);
  1827. { Find also all hidden private methods to
  1828. be compatible with delphi, see tw6203 (PFV) }
  1829. if (def.typ=procdef) and
  1830. (po_msgstr in tprocdef(def).procoptions) and
  1831. (tprocdef(def).messageinf.str^=s) then
  1832. begin
  1833. srsym:=tprocdef(def).procsym;
  1834. srsymtable:=classh.symtable;
  1835. addsymref(srsym);
  1836. result:=true;
  1837. exit;
  1838. end;
  1839. end;
  1840. classh:=classh.childof;
  1841. end;
  1842. srsym:=nil;
  1843. srsymtable:=nil;
  1844. end;
  1845. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1846. var
  1847. sym : Tprocsym;
  1848. hashedid : THashedIDString;
  1849. curreq,
  1850. besteq : tequaltype;
  1851. currpd,
  1852. bestpd : tprocdef;
  1853. stackitem : psymtablestackitem;
  1854. begin
  1855. hashedid.id:='assign';
  1856. besteq:=te_incompatible;
  1857. bestpd:=nil;
  1858. stackitem:=symtablestack.stack;
  1859. while assigned(stackitem) do
  1860. begin
  1861. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1862. if sym<>nil then
  1863. begin
  1864. if sym.typ<>procsym then
  1865. internalerror(200402031);
  1866. { if the source type is an alias then this is only the second choice,
  1867. if you mess with this code, check tw4093 }
  1868. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1869. if curreq>besteq then
  1870. begin
  1871. besteq:=curreq;
  1872. bestpd:=currpd;
  1873. if (besteq=te_exact) then
  1874. break;
  1875. end;
  1876. end;
  1877. stackitem:=stackitem^.next;
  1878. end;
  1879. result:=bestpd;
  1880. end;
  1881. function search_system_type(const s: TIDString): ttypesym;
  1882. var
  1883. sym : tsym;
  1884. begin
  1885. sym:=tsym(systemunit.Find(s));
  1886. if not assigned(sym) or
  1887. (sym.typ<>typesym) then
  1888. cgmessage1(cg_f_unknown_system_type,s);
  1889. result:=ttypesym(sym);
  1890. end;
  1891. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1892. { searches n in symtable of pd and all anchestors }
  1893. var
  1894. hashedid : THashedIDString;
  1895. srsym : tsym;
  1896. begin
  1897. hashedid.id:=s;
  1898. while assigned(pd) do
  1899. begin
  1900. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1901. if assigned(srsym) then
  1902. begin
  1903. search_class_member:=srsym;
  1904. exit;
  1905. end;
  1906. pd:=pd.childof;
  1907. end;
  1908. search_class_member:=nil;
  1909. end;
  1910. function search_macro(const s : string):tsym;
  1911. var
  1912. stackitem : psymtablestackitem;
  1913. hashedid : THashedIDString;
  1914. srsym : tsym;
  1915. begin
  1916. hashedid.id:=s;
  1917. { First search the localmacrosymtable before searching the
  1918. global macrosymtables from the units }
  1919. if assigned(current_module) then
  1920. begin
  1921. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1922. if assigned(srsym) then
  1923. begin
  1924. result:= srsym;
  1925. exit;
  1926. end;
  1927. end;
  1928. stackitem:=macrosymtablestack.stack;
  1929. while assigned(stackitem) do
  1930. begin
  1931. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1932. if assigned(srsym) then
  1933. begin
  1934. result:= srsym;
  1935. exit;
  1936. end;
  1937. stackitem:=stackitem^.next;
  1938. end;
  1939. result:= nil;
  1940. end;
  1941. function defined_macro(const s : string):boolean;
  1942. var
  1943. mac: tmacro;
  1944. begin
  1945. mac:=tmacro(search_macro(s));
  1946. if assigned(mac) then
  1947. begin
  1948. mac.is_used:=true;
  1949. defined_macro:=mac.defined;
  1950. end
  1951. else
  1952. defined_macro:=false;
  1953. end;
  1954. {****************************************************************************
  1955. Object Helpers
  1956. ****************************************************************************}
  1957. procedure search_class_overloads(aprocsym : tprocsym);
  1958. { searches n in symtable of pd and all anchestors }
  1959. var
  1960. hashedid : THashedIDString;
  1961. srsym : tprocsym;
  1962. objdef : tobjectdef;
  1963. begin
  1964. if aprocsym.overloadchecked then
  1965. exit;
  1966. aprocsym.overloadchecked:=true;
  1967. if (aprocsym.owner.symtabletype<>ObjectSymtable) then
  1968. internalerror(200111021);
  1969. objdef:=tobjectdef(aprocsym.owner.defowner);
  1970. { we start in the parent }
  1971. if not assigned(objdef.childof) then
  1972. exit;
  1973. objdef:=objdef.childof;
  1974. hashedid.id:=aprocsym.name;
  1975. while assigned(objdef) do
  1976. begin
  1977. srsym:=tprocsym(objdef.symtable.FindWithHash(hashedid));
  1978. if assigned(srsym) then
  1979. begin
  1980. if (srsym.typ<>procsym) then
  1981. internalerror(200111022);
  1982. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner),tobjectdef(aprocsym.owner.defowner)) then
  1983. begin
  1984. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1985. { we can stop if the overloads were already added
  1986. for the found symbol }
  1987. if srsym.overloadchecked then
  1988. break;
  1989. end;
  1990. end;
  1991. { next parent }
  1992. objdef:=objdef.childof;
  1993. end;
  1994. end;
  1995. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  1996. begin
  1997. if (tsym(sym).typ=propertysym) and
  1998. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  1999. ppointer(arg)^:=sym;
  2000. end;
  2001. function search_default_property(pd : tobjectdef) : tpropertysym;
  2002. { returns the default property of a class, searches also anchestors }
  2003. var
  2004. _defaultprop : tpropertysym;
  2005. begin
  2006. _defaultprop:=nil;
  2007. while assigned(pd) do
  2008. begin
  2009. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2010. if assigned(_defaultprop) then
  2011. break;
  2012. pd:=pd.childof;
  2013. end;
  2014. search_default_property:=_defaultprop;
  2015. end;
  2016. {****************************************************************************
  2017. Macro Helpers
  2018. ****************************************************************************}
  2019. procedure def_system_macro(const name : string);
  2020. var
  2021. mac : tmacro;
  2022. s: string;
  2023. begin
  2024. if name = '' then
  2025. internalerror(2004121202);
  2026. s:= upper(name);
  2027. mac:=tmacro(search_macro(s));
  2028. if not assigned(mac) then
  2029. begin
  2030. mac:=tmacro.create(s);
  2031. if assigned(current_module) then
  2032. current_module.localmacrosymtable.insert(mac)
  2033. else
  2034. initialmacrosymtable.insert(mac);
  2035. end;
  2036. if not mac.defined then
  2037. Message1(parser_c_macro_defined,mac.name);
  2038. mac.defined:=true;
  2039. end;
  2040. procedure set_system_macro(const name, value : string);
  2041. var
  2042. mac : tmacro;
  2043. s: string;
  2044. begin
  2045. if name = '' then
  2046. internalerror(2004121203);
  2047. s:= upper(name);
  2048. mac:=tmacro(search_macro(s));
  2049. if not assigned(mac) then
  2050. begin
  2051. mac:=tmacro.create(s);
  2052. if assigned(current_module) then
  2053. current_module.localmacrosymtable.insert(mac)
  2054. else
  2055. initialmacrosymtable.insert(mac);
  2056. end
  2057. else
  2058. begin
  2059. mac.is_compiler_var:=false;
  2060. if assigned(mac.buftext) then
  2061. freemem(mac.buftext,mac.buflen);
  2062. end;
  2063. Message2(parser_c_macro_set_to,mac.name,value);
  2064. mac.buflen:=length(value);
  2065. getmem(mac.buftext,mac.buflen);
  2066. move(value[1],mac.buftext^,mac.buflen);
  2067. mac.defined:=true;
  2068. end;
  2069. procedure set_system_compvar(const name, value : string);
  2070. var
  2071. mac : tmacro;
  2072. s: string;
  2073. begin
  2074. if name = '' then
  2075. internalerror(2004121204);
  2076. s:= upper(name);
  2077. mac:=tmacro(search_macro(s));
  2078. if not assigned(mac) then
  2079. begin
  2080. mac:=tmacro.create(s);
  2081. mac.is_compiler_var:=true;
  2082. if assigned(current_module) then
  2083. current_module.localmacrosymtable.insert(mac)
  2084. else
  2085. initialmacrosymtable.insert(mac);
  2086. end
  2087. else
  2088. begin
  2089. mac.is_compiler_var:=true;
  2090. if assigned(mac.buftext) then
  2091. freemem(mac.buftext,mac.buflen);
  2092. end;
  2093. Message2(parser_c_macro_set_to,mac.name,value);
  2094. mac.buflen:=length(value);
  2095. getmem(mac.buftext,mac.buflen);
  2096. move(value[1],mac.buftext^,mac.buflen);
  2097. mac.defined:=true;
  2098. end;
  2099. procedure undef_system_macro(const name : string);
  2100. var
  2101. mac : tmacro;
  2102. s: string;
  2103. begin
  2104. if name = '' then
  2105. internalerror(2004121205);
  2106. s:= upper(name);
  2107. mac:=tmacro(search_macro(s));
  2108. if not assigned(mac) then
  2109. {If not found, then it's already undefined.}
  2110. else
  2111. begin
  2112. if mac.defined then
  2113. Message1(parser_c_macro_undefined,mac.name);
  2114. mac.defined:=false;
  2115. mac.is_compiler_var:=false;
  2116. { delete old definition }
  2117. if assigned(mac.buftext) then
  2118. begin
  2119. freemem(mac.buftext,mac.buflen);
  2120. mac.buftext:=nil;
  2121. end;
  2122. end;
  2123. end;
  2124. {$ifdef UNITALIASES}
  2125. {****************************************************************************
  2126. TUNIT_ALIAS
  2127. ****************************************************************************}
  2128. constructor tunit_alias.create(const n:string);
  2129. var
  2130. i : longint;
  2131. begin
  2132. i:=pos('=',n);
  2133. if i=0 then
  2134. fail;
  2135. inherited createname(Copy(n,1,i-1));
  2136. newname:=stringdup(Copy(n,i+1,255));
  2137. end;
  2138. destructor tunit_alias.destroy;
  2139. begin
  2140. stringdispose(newname);
  2141. inherited destroy;
  2142. end;
  2143. procedure addunitalias(const n:string);
  2144. begin
  2145. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2146. end;
  2147. function getunitalias(const n:string):string;
  2148. var
  2149. p : punit_alias;
  2150. begin
  2151. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2152. if assigned(p) then
  2153. getunitalias:=punit_alias(p).newname^
  2154. else
  2155. getunitalias:=n;
  2156. end;
  2157. {$endif UNITALIASES}
  2158. {****************************************************************************
  2159. Init/Done Symtable
  2160. ****************************************************************************}
  2161. procedure InitSymtable;
  2162. begin
  2163. { Reset symbolstack }
  2164. symtablestack:=nil;
  2165. systemunit:=nil;
  2166. { create error syms and def }
  2167. generrorsym:=terrorsym.create;
  2168. generrordef:=terrordef.create;
  2169. { macros }
  2170. initialmacrosymtable:=tmacrosymtable.create(false);
  2171. macrosymtablestack:=TSymtablestack.create;
  2172. macrosymtablestack.push(initialmacrosymtable);
  2173. {$ifdef UNITALIASES}
  2174. { unit aliases }
  2175. unitaliases:=TFPHashObjectList.create;
  2176. {$endif}
  2177. { set some global vars to nil, might be important for the ide }
  2178. class_tobject:=nil;
  2179. interface_iunknown:=nil;
  2180. rec_tguid:=nil;
  2181. aktobjectdef:=nil;
  2182. dupnr:=0;
  2183. end;
  2184. procedure DoneSymtable;
  2185. begin
  2186. generrorsym.owner:=nil;
  2187. generrorsym.free;
  2188. generrordef.owner:=nil;
  2189. generrordef.free;
  2190. initialmacrosymtable.free;
  2191. macrosymtablestack.free;
  2192. {$ifdef UNITALIASES}
  2193. unitaliases.free;
  2194. {$endif}
  2195. end;
  2196. end.