symtable.pas 76 KB

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