symtable.pas 76 KB

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