symtable.pas 76 KB

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