symtable.pas 78 KB

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