symtable.pas 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403
  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) then
  1185. begin
  1186. case p.deftype of
  1187. procdef :
  1188. if assigned(tprocdef(p).localst) then
  1189. dowritestabs(asmlist,tprocdef(p).localst);
  1190. objectdef :
  1191. dowritestabs(asmlist,tobjectdef(p).symtable);
  1192. end;
  1193. end;
  1194. if (p.stab_state=stab_state_used) then
  1195. p.concatstabto(asmlist);
  1196. p:=tstoreddef(p.indexnext);
  1197. end;
  1198. end;
  1199. var
  1200. old_writing_def_stabs : boolean;
  1201. prev_dbx_count : plongint;
  1202. begin
  1203. if not assigned(name) then
  1204. name := stringdup('Main_program');
  1205. asmList.concat(tai_comment.Create(strpnew('Begin unit '+name^+' has index '+tostr(unitid))));
  1206. if cs_gdb_dbx in aktglobalswitches then
  1207. begin
  1208. if dbx_count_ok then
  1209. begin
  1210. asmList.concat(tai_comment.Create(strpnew('"repeated" unit '+name^
  1211. +' has index '+tostr(unitid)+' dbx count = '+tostr(dbx_count))));
  1212. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1213. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count))));
  1214. exit;
  1215. end
  1216. else if (current_module.globalsymtable<>self) then
  1217. begin
  1218. prev_dbx_count := dbx_counter;
  1219. dbx_counter := nil;
  1220. do_count_dbx:=false;
  1221. if (symtabletype = globalsymtable) and (unitid<>0) then
  1222. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1223. dbx_counter := @dbx_count;
  1224. dbx_count:=0;
  1225. do_count_dbx:=assigned(dbx_counter);
  1226. end;
  1227. end;
  1228. old_writing_def_stabs:=writing_def_stabs;
  1229. writing_def_stabs:=true;
  1230. dowritestabs(asmlist,self);
  1231. writing_def_stabs:=old_writing_def_stabs;
  1232. if cs_gdb_dbx in aktglobalswitches then
  1233. begin
  1234. if (current_module.globalsymtable<>self) then
  1235. begin
  1236. dbx_counter := prev_dbx_count;
  1237. do_count_dbx:=false;
  1238. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1239. +tostr(N_EINCL)+',0,0,0')));
  1240. do_count_dbx:=assigned(dbx_counter);
  1241. dbx_count_ok := {true}false;
  1242. end;
  1243. end;
  1244. asmList.concat(tai_comment.Create(strpnew('End unit '+name^+' has index '+tostr(unitid))));
  1245. end;
  1246. {$endif GDB}
  1247. {****************************************************************************
  1248. TStaticSymtable
  1249. ****************************************************************************}
  1250. constructor tstaticsymtable.create(const n : string);
  1251. begin
  1252. inherited create(n);
  1253. symtabletype:=staticsymtable;
  1254. symtablelevel:=main_program_level;
  1255. end;
  1256. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1257. begin
  1258. next:=symtablestack;
  1259. symtablestack:=self;
  1260. inherited ppuload(ppufile);
  1261. { now we can deref the syms and defs }
  1262. deref;
  1263. { restore symtablestack }
  1264. symtablestack:=next;
  1265. end;
  1266. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1267. begin
  1268. inherited ppuwrite(ppufile);
  1269. end;
  1270. procedure tstaticsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1271. begin
  1272. inherited load_references(ppufile,locals);
  1273. end;
  1274. procedure tstaticsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1275. begin
  1276. inherited write_references(ppufile,locals);
  1277. end;
  1278. procedure tstaticsymtable.insert(sym:tsymentry);
  1279. var
  1280. hsym : tsym;
  1281. begin
  1282. { also check the global symtable }
  1283. if assigned(next) and
  1284. (next.unitid=0) then
  1285. begin
  1286. hsym:=tsym(next.search(sym.name));
  1287. if assigned(hsym) then
  1288. begin
  1289. { Delphi you can have a symbol with the same name as the
  1290. unit, the unit can then not be accessed anymore using
  1291. <unit>.<id>, so we can hide the symbol }
  1292. if (m_duplicate_names in aktmodeswitches) and
  1293. (hsym.typ=symconst.unitsym) then
  1294. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1295. else
  1296. DuplicateSym(sym,hsym);
  1297. end;
  1298. end;
  1299. inherited insert(sym);
  1300. end;
  1301. {****************************************************************************
  1302. TGlobalSymtable
  1303. ****************************************************************************}
  1304. constructor tglobalsymtable.create(const n : string);
  1305. begin
  1306. inherited create(n);
  1307. symtabletype:=globalsymtable;
  1308. symtablelevel:=main_program_level;
  1309. unitid:=0;
  1310. {$ifdef GDB}
  1311. if cs_gdb_dbx in aktglobalswitches then
  1312. begin
  1313. dbx_count := 0;
  1314. unittypecount:=1;
  1315. pglobaltypecount := @unittypecount;
  1316. {unitid:=current_module.unitcount;}
  1317. {debugList.concat(tai_comment.Create(strpnew('Global '+name^+' has index '+tostr(unitid))));
  1318. debugList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));}
  1319. {inc(current_module.unitcount);}
  1320. { we can't use dbx_vcount, because we don't know
  1321. if the object file will be loaded before or afeter PM }
  1322. dbx_count_ok:=false;
  1323. dbx_counter:=@dbx_count;
  1324. do_count_dbx:=true;
  1325. end;
  1326. {$endif GDB}
  1327. end;
  1328. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1329. {$ifdef GDB}
  1330. var
  1331. b : byte;
  1332. {$endif GDB}
  1333. begin
  1334. {$ifdef GDB}
  1335. if cs_gdb_dbx in aktglobalswitches then
  1336. begin
  1337. UnitTypeCount:=1;
  1338. PglobalTypeCount:=@UnitTypeCount;
  1339. end;
  1340. {$endif GDB}
  1341. next:=symtablestack;
  1342. symtablestack:=self;
  1343. inherited ppuload(ppufile);
  1344. { now we can deref the syms and defs }
  1345. deref;
  1346. { restore symtablestack }
  1347. symtablestack:=next;
  1348. { read dbx count }
  1349. {$ifdef GDB}
  1350. if (current_module.flags and uf_has_dbx)<>0 then
  1351. begin
  1352. b:=ppufile.readentry;
  1353. if b<>ibdbxcount then
  1354. Message(unit_f_ppu_dbx_count_problem)
  1355. else
  1356. dbx_count:=ppufile.getlongint;
  1357. {$IfDef EXTDEBUG}
  1358. writeln('Read dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1359. {$ENDIF EXTDEBUG}
  1360. { we can't use dbx_vcount, because we don't know
  1361. if the object file will be loaded before or afeter PM }
  1362. dbx_count_ok := {true}false;
  1363. end
  1364. else
  1365. begin
  1366. dbx_count:=-1;
  1367. dbx_count_ok:=false;
  1368. end;
  1369. {$endif GDB}
  1370. end;
  1371. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1372. begin
  1373. { write the symtable entries }
  1374. inherited ppuwrite(ppufile);
  1375. { write dbx count }
  1376. {$ifdef GDB}
  1377. if cs_gdb_dbx in aktglobalswitches then
  1378. begin
  1379. {$IfDef EXTDEBUG}
  1380. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1381. {$ENDIF EXTDEBUG}
  1382. ppufile.do_crc:=false;
  1383. ppufile.putlongint(dbx_count);
  1384. ppufile.writeentry(ibdbxcount);
  1385. ppufile.do_crc:=true;
  1386. end;
  1387. {$endif GDB}
  1388. end;
  1389. procedure tglobalsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1390. begin
  1391. inherited load_references(ppufile,locals);
  1392. end;
  1393. procedure tglobalsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1394. begin
  1395. inherited write_references(ppufile,locals);
  1396. end;
  1397. procedure tglobalsymtable.insert(sym:tsymentry);
  1398. var
  1399. hsym : tsym;
  1400. begin
  1401. { also check the global symtable }
  1402. if assigned(next) and
  1403. (next.unitid=0) then
  1404. begin
  1405. hsym:=tsym(next.search(sym.name));
  1406. if assigned(hsym) then
  1407. begin
  1408. { Delphi you can have a symbol with the same name as the
  1409. unit, the unit can then not be accessed anymore using
  1410. <unit>.<id>, so we can hide the symbol }
  1411. if (m_duplicate_names in aktmodeswitches) and
  1412. (hsym.typ=symconst.unitsym) then
  1413. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1414. else
  1415. DuplicateSym(sym,hsym);
  1416. end;
  1417. end;
  1418. hsym:=tsym(search(sym.name));
  1419. if assigned(hsym) then
  1420. begin
  1421. { Delphi you can have a symbol with the same name as the
  1422. unit, the unit can then not be accessed anymore using
  1423. <unit>.<id>, so we can hide the symbol }
  1424. if (m_duplicate_names in aktmodeswitches) and
  1425. (hsym.typ=symconst.unitsym) then
  1426. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1427. else
  1428. DuplicateSym(sym,hsym);
  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. function FullTypeName(def,otherdef:tdef):string;
  1516. var
  1517. s1,s2 : string;
  1518. begin
  1519. s1:=def.typename;
  1520. { When the names are the same try to include the unit name }
  1521. if assigned(otherdef) and
  1522. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1523. begin
  1524. s2:=otherdef.typename;
  1525. if upper(s1)=upper(s2) then
  1526. s1:=def.owner.realname^+'.'+s1;
  1527. end;
  1528. FullTypeName:=s1;
  1529. end;
  1530. procedure incompatibletypes(def1,def2:tdef);
  1531. begin
  1532. { When there is an errordef there is already an error message show }
  1533. if (def2.deftype=errordef) or
  1534. (def1.deftype=errordef) then
  1535. exit;
  1536. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1537. end;
  1538. {*****************************************************************************
  1539. Search
  1540. *****************************************************************************}
  1541. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1542. var
  1543. speedvalue : cardinal;
  1544. topclass : tobjectdef;
  1545. begin
  1546. speedvalue:=getspeedvalue(s);
  1547. srsymtable:=symtablestack;
  1548. while assigned(srsymtable) do
  1549. begin
  1550. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1551. if assigned(srsym) then
  1552. begin
  1553. topclass:=nil;
  1554. { use the class from withsymtable only when it is
  1555. defined in this unit }
  1556. if (srsymtable.symtabletype=withsymtable) and
  1557. assigned(srsymtable.defowner) and
  1558. (srsymtable.defowner.deftype=objectdef) and
  1559. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1560. (srsymtable.defowner.owner.unitid=0) then
  1561. topclass:=tobjectdef(srsymtable.defowner)
  1562. else
  1563. begin
  1564. if assigned(current_procinfo) then
  1565. topclass:=current_procinfo.procdef._class;
  1566. end;
  1567. if Tsym(srsym).is_visible_for_object(topclass) then
  1568. begin
  1569. { we need to know if a procedure references symbols
  1570. in the static symtable, because then it can't be
  1571. inlined from outside this unit }
  1572. if assigned(current_procinfo) and
  1573. (srsym.owner.symtabletype=staticsymtable) then
  1574. include(current_procinfo.flags,pi_uses_static_symtable);
  1575. searchsym:=true;
  1576. exit;
  1577. end;
  1578. end;
  1579. srsymtable:=srsymtable.next;
  1580. end;
  1581. searchsym:=false;
  1582. end;
  1583. function searchsym_type(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1584. var
  1585. speedvalue : cardinal;
  1586. begin
  1587. speedvalue:=getspeedvalue(s);
  1588. srsymtable:=symtablestack;
  1589. while assigned(srsymtable) do
  1590. begin
  1591. {
  1592. It is not possible to have type defintions in:
  1593. records
  1594. objects
  1595. parameters
  1596. }
  1597. if not(srsymtable.symtabletype in [recordsymtable,objectsymtable,parasymtable]) then
  1598. begin
  1599. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1600. if assigned(srsym) and
  1601. (not assigned(current_procinfo) or
  1602. Tsym(srsym).is_visible_for_object(current_procinfo.procdef._class)) then
  1603. begin
  1604. result:=true;
  1605. exit;
  1606. end
  1607. end;
  1608. srsymtable:=srsymtable.next;
  1609. end;
  1610. result:=false;
  1611. end;
  1612. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  1613. var
  1614. srsym : tsym;
  1615. begin
  1616. { the caller have to take care if srsym=nil }
  1617. if assigned(p) then
  1618. begin
  1619. srsym:=tsym(p.search(s));
  1620. if assigned(srsym) then
  1621. begin
  1622. searchsymonlyin:=srsym;
  1623. exit;
  1624. end;
  1625. { also check in the local symtbale if it exists }
  1626. if (p=tsymtable(current_module.globalsymtable)) then
  1627. begin
  1628. srsym:=tsym(current_module.localsymtable.search(s));
  1629. if assigned(srsym) then
  1630. begin
  1631. searchsymonlyin:=srsym;
  1632. exit;
  1633. end;
  1634. end
  1635. end;
  1636. searchsymonlyin:=nil;
  1637. end;
  1638. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  1639. var
  1640. speedvalue : cardinal;
  1641. topclassh : tobjectdef;
  1642. sym : tsym;
  1643. begin
  1644. speedvalue:=getspeedvalue(s);
  1645. { when the class passed is defined in this unit we
  1646. need to use the scope of that class. This is a trick
  1647. that can be used to access protected members in other
  1648. units. At least kylix supports it this way (PFV) }
  1649. if assigned(classh) and
  1650. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1651. (classh.owner.unitid=0) then
  1652. topclassh:=classh
  1653. else
  1654. begin
  1655. if assigned(current_procinfo) then
  1656. topclassh:=current_procinfo.procdef._class
  1657. else
  1658. topclassh:=nil;
  1659. end;
  1660. sym:=nil;
  1661. while assigned(classh) do
  1662. begin
  1663. sym:=tsym(classh.symtable.speedsearch(s,speedvalue));
  1664. if assigned(sym) and
  1665. Tsym(sym).is_visible_for_object(topclassh) then
  1666. break;
  1667. classh:=classh.childof;
  1668. end;
  1669. searchsym_in_class:=sym;
  1670. end;
  1671. function searchsym_in_class_by_msgint(classh:tobjectdef;i:longint):tsym;
  1672. var
  1673. topclassh : tobjectdef;
  1674. def : tdef;
  1675. sym : tsym;
  1676. begin
  1677. { when the class passed is defined in this unit we
  1678. need to use the scope of that class. This is a trick
  1679. that can be used to access protected members in other
  1680. units. At least kylix supports it this way (PFV) }
  1681. if assigned(classh) and
  1682. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1683. (classh.owner.unitid=0) then
  1684. topclassh:=classh
  1685. else
  1686. begin
  1687. if assigned(current_procinfo) then
  1688. topclassh:=current_procinfo.procdef._class
  1689. else
  1690. topclassh:=nil;
  1691. end;
  1692. sym:=nil;
  1693. def:=nil;
  1694. while assigned(classh) do
  1695. begin
  1696. def:=tdef(classh.symtable.defindex.first);
  1697. while assigned(def) do
  1698. begin
  1699. if (def.deftype=procdef) and
  1700. (po_msgint in tprocdef(def).procoptions) and
  1701. (tprocdef(def).messageinf.i=i) then
  1702. begin
  1703. sym:=tprocdef(def).procsym;
  1704. if assigned(topclassh) then
  1705. begin
  1706. if tprocdef(def).is_visible_for_object(topclassh) then
  1707. break;
  1708. end
  1709. else
  1710. break;
  1711. end;
  1712. def:=tdef(def.indexnext);
  1713. end;
  1714. if assigned(sym) then
  1715. break;
  1716. classh:=classh.childof;
  1717. end;
  1718. searchsym_in_class_by_msgint:=sym;
  1719. end;
  1720. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string):tsym;
  1721. var
  1722. topclassh : tobjectdef;
  1723. def : tdef;
  1724. sym : tsym;
  1725. begin
  1726. { when the class passed is defined in this unit we
  1727. need to use the scope of that class. This is a trick
  1728. that can be used to access protected members in other
  1729. units. At least kylix supports it this way (PFV) }
  1730. if assigned(classh) and
  1731. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1732. (classh.owner.unitid=0) then
  1733. topclassh:=classh
  1734. else
  1735. begin
  1736. if assigned(current_procinfo) then
  1737. topclassh:=current_procinfo.procdef._class
  1738. else
  1739. topclassh:=nil;
  1740. end;
  1741. sym:=nil;
  1742. def:=nil;
  1743. while assigned(classh) do
  1744. begin
  1745. def:=tdef(classh.symtable.defindex.first);
  1746. while assigned(def) do
  1747. begin
  1748. if (def.deftype=procdef) and
  1749. (po_msgstr in tprocdef(def).procoptions) and
  1750. (tprocdef(def).messageinf.str=s) then
  1751. begin
  1752. sym:=tprocdef(def).procsym;
  1753. if assigned(topclassh) then
  1754. begin
  1755. if tprocdef(def).is_visible_for_object(topclassh) then
  1756. break;
  1757. end
  1758. else
  1759. break;
  1760. end;
  1761. def:=tdef(def.indexnext);
  1762. end;
  1763. if assigned(sym) then
  1764. break;
  1765. classh:=classh.childof;
  1766. end;
  1767. searchsym_in_class_by_msgstr:=sym;
  1768. end;
  1769. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1770. var st:Tsymtable;
  1771. sym:Tprocsym;
  1772. sv:cardinal;
  1773. begin
  1774. st:=symtablestack;
  1775. sv:=getspeedvalue('assign');
  1776. while st<>nil do
  1777. begin
  1778. sym:=Tprocsym(st.speedsearch('assign',sv));
  1779. if sym<>nil then
  1780. begin
  1781. if sym.typ<>procsym then
  1782. internalerror(200402031);
  1783. search_assignment_operator:=sym.search_procdef_assignment_operator(from_def,to_def);
  1784. if search_assignment_operator<>nil then
  1785. break;
  1786. end;
  1787. st:=st.next;
  1788. end;
  1789. end;
  1790. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  1791. var
  1792. symowner: tsymtable;
  1793. begin
  1794. if not(cs_compilesystem in aktmoduleswitches) then
  1795. srsym := ttypesym(searchsymonlyin(systemunit,s))
  1796. else
  1797. searchsym(s,tsym(srsym),symowner);
  1798. searchsystype :=
  1799. assigned(srsym) and
  1800. (srsym.typ = typesym);
  1801. end;
  1802. {$ifdef notused}
  1803. function searchsysvar(const s: stringid; var srsym: tsym; var symowner: tsymtable): boolean;
  1804. begin
  1805. if not(cs_compilesystem in aktmoduleswitches) then
  1806. begin
  1807. srsym := searchsymonlyin(systemunit,s);
  1808. symowner := systemunit;
  1809. end
  1810. else
  1811. searchsym(s,tsym(srsym),symowner);
  1812. searchsysvar :=
  1813. assigned(srsym) and
  1814. (srsym.typ = globalvarsym);
  1815. end;
  1816. {$endif notused}
  1817. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1818. { searches n in symtable of pd and all anchestors }
  1819. var
  1820. speedvalue : cardinal;
  1821. srsym : tsym;
  1822. begin
  1823. speedvalue:=getspeedvalue(s);
  1824. while assigned(pd) do
  1825. begin
  1826. srsym:=tsym(pd.symtable.speedsearch(s,speedvalue));
  1827. if assigned(srsym) then
  1828. begin
  1829. search_class_member:=srsym;
  1830. exit;
  1831. end;
  1832. pd:=pd.childof;
  1833. end;
  1834. search_class_member:=nil;
  1835. end;
  1836. {*****************************************************************************
  1837. Definition Helpers
  1838. *****************************************************************************}
  1839. procedure globaldef(const s : string;var t:ttype);
  1840. var st : string;
  1841. symt : tsymtable;
  1842. srsym : tsym;
  1843. srsymtable : tsymtable;
  1844. begin
  1845. srsym := nil;
  1846. if pos('.',s) > 0 then
  1847. begin
  1848. st := copy(s,1,pos('.',s)-1);
  1849. searchsym(st,srsym,srsymtable);
  1850. st := copy(s,pos('.',s)+1,255);
  1851. if assigned(srsym) then
  1852. begin
  1853. if srsym.typ = unitsym then
  1854. begin
  1855. symt := tunitsym(srsym).unitsymtable;
  1856. srsym := tsym(symt.search(st));
  1857. end else srsym := nil;
  1858. end;
  1859. end else st := s;
  1860. if srsym = nil then
  1861. searchsym(st,srsym,srsymtable);
  1862. if srsym = nil then
  1863. srsym:=searchsymonlyin(systemunit,st);
  1864. if (not assigned(srsym)) or
  1865. (srsym.typ<>typesym) then
  1866. begin
  1867. Message(type_e_type_id_expected);
  1868. t:=generrortype;
  1869. exit;
  1870. end;
  1871. t := ttypesym(srsym).restype;
  1872. end;
  1873. {****************************************************************************
  1874. Object Helpers
  1875. ****************************************************************************}
  1876. procedure search_class_overloads(aprocsym : tprocsym);
  1877. { searches n in symtable of pd and all anchestors }
  1878. var
  1879. speedvalue : cardinal;
  1880. srsym : tprocsym;
  1881. s : string;
  1882. objdef : tobjectdef;
  1883. begin
  1884. if aprocsym.overloadchecked then
  1885. exit;
  1886. aprocsym.overloadchecked:=true;
  1887. if (aprocsym.owner.symtabletype<>objectsymtable) then
  1888. internalerror(200111021);
  1889. objdef:=tobjectdef(aprocsym.owner.defowner);
  1890. { we start in the parent }
  1891. if not assigned(objdef.childof) then
  1892. exit;
  1893. objdef:=objdef.childof;
  1894. s:=aprocsym.name;
  1895. speedvalue:=getspeedvalue(s);
  1896. while assigned(objdef) do
  1897. begin
  1898. srsym:=tprocsym(objdef.symtable.speedsearch(s,speedvalue));
  1899. if assigned(srsym) then
  1900. begin
  1901. if (srsym.typ<>procsym) then
  1902. internalerror(200111022);
  1903. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner)) then
  1904. begin
  1905. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1906. { we can stop if the overloads were already added
  1907. for the found symbol }
  1908. if srsym.overloadchecked then
  1909. break;
  1910. end;
  1911. end;
  1912. { next parent }
  1913. objdef:=objdef.childof;
  1914. end;
  1915. end;
  1916. procedure tstoredsymtable.testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  1917. begin
  1918. if (tsym(p).typ=propertysym) and
  1919. (ppo_defaultproperty in tpropertysym(p).propoptions) then
  1920. ppointer(arg)^:=p;
  1921. end;
  1922. function search_default_property(pd : tobjectdef) : tpropertysym;
  1923. { returns the default property of a class, searches also anchestors }
  1924. var
  1925. _defaultprop : tpropertysym;
  1926. begin
  1927. _defaultprop:=nil;
  1928. while assigned(pd) do
  1929. begin
  1930. pd.symtable.foreach(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1931. if assigned(_defaultprop) then
  1932. break;
  1933. pd:=pd.childof;
  1934. end;
  1935. search_default_property:=_defaultprop;
  1936. end;
  1937. {$ifdef UNITALIASES}
  1938. {****************************************************************************
  1939. TUNIT_ALIAS
  1940. ****************************************************************************}
  1941. constructor tunit_alias.create(const n:string);
  1942. var
  1943. i : longint;
  1944. begin
  1945. i:=pos('=',n);
  1946. if i=0 then
  1947. fail;
  1948. inherited createname(Copy(n,1,i-1));
  1949. newname:=stringdup(Copy(n,i+1,255));
  1950. end;
  1951. destructor tunit_alias.destroy;
  1952. begin
  1953. stringdispose(newname);
  1954. inherited destroy;
  1955. end;
  1956. procedure addunitalias(const n:string);
  1957. begin
  1958. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1959. end;
  1960. function getunitalias(const n:string):string;
  1961. var
  1962. p : punit_alias;
  1963. begin
  1964. p:=punit_alias(unitaliases^.search(Upper(n)));
  1965. if assigned(p) then
  1966. getunitalias:=punit_alias(p).newname^
  1967. else
  1968. getunitalias:=n;
  1969. end;
  1970. {$endif UNITALIASES}
  1971. {****************************************************************************
  1972. Symtable Stack
  1973. ****************************************************************************}
  1974. {$ifdef DEBUG}
  1975. procedure test_symtablestack;
  1976. var
  1977. p : tsymtable;
  1978. i : longint;
  1979. begin
  1980. p:=symtablestack;
  1981. i:=0;
  1982. while assigned(p) do
  1983. begin
  1984. inc(i);
  1985. p:=p.next;
  1986. if i>500 then
  1987. Message(sym_f_internal_error_in_symtablestack);
  1988. end;
  1989. end;
  1990. procedure list_symtablestack;
  1991. var
  1992. p : tsymtable;
  1993. i : longint;
  1994. begin
  1995. p:=symtablestack;
  1996. i:=0;
  1997. while assigned(p) do
  1998. begin
  1999. inc(i);
  2000. writeln(i,' ',p.name^);
  2001. p:=p.next;
  2002. if i>500 then
  2003. Message(sym_f_internal_error_in_symtablestack);
  2004. end;
  2005. end;
  2006. {$endif DEBUG}
  2007. {****************************************************************************
  2008. Init/Done Symtable
  2009. ****************************************************************************}
  2010. procedure InitSymtable;
  2011. begin
  2012. { Reset symbolstack }
  2013. registerdef:=false;
  2014. symtablestack:=nil;
  2015. systemunit:=nil;
  2016. {$ifdef GDB}
  2017. globaltypecount:=1;
  2018. pglobaltypecount:=@globaltypecount;
  2019. {$endif GDB}
  2020. { create error syms and def }
  2021. generrorsym:=terrorsym.create;
  2022. generrortype.setdef(terrordef.create);
  2023. {$ifdef UNITALIASES}
  2024. { unit aliases }
  2025. unitaliases:=tdictionary.create;
  2026. {$endif}
  2027. dupnr:=0;
  2028. end;
  2029. procedure DoneSymtable;
  2030. begin
  2031. generrorsym.free;
  2032. generrortype.def.free;
  2033. {$ifdef UNITALIASES}
  2034. unitaliases.free;
  2035. {$endif}
  2036. end;
  2037. end.
  2038. {
  2039. $Log$
  2040. Revision 1.167 2004-12-27 16:35:48 peter
  2041. * set flag if a procedure references a symbol in staticsymtable
  2042. Revision 1.166 2004/12/21 08:38:16 michael
  2043. + Enable local debug info in methods
  2044. Revision 1.165 2004/12/15 15:59:54 peter
  2045. * fix visibility of protected/private
  2046. Revision 1.164 2004/11/15 23:35:31 peter
  2047. * tparaitem removed, use tparavarsym instead
  2048. * parameter order is now calculated from paranr value in tparavarsym
  2049. Revision 1.163 2004/11/09 23:10:22 peter
  2050. * use helper call to retrieve address of input/output to reduce
  2051. code that is generated in the main program for loading the
  2052. threadvar
  2053. Revision 1.162 2004/11/08 22:09:59 peter
  2054. * tvarsym splitted
  2055. Revision 1.161 2004/11/05 21:16:55 peter
  2056. * rename duplicate symbols and insert with unique name in the
  2057. symtable
  2058. Revision 1.160 2004/11/01 23:30:11 peter
  2059. * support > 32bit accesses for x86_64
  2060. * rewrote array size checking to support 64bit
  2061. Revision 1.159 2004/10/15 09:14:17 mazen
  2062. - remove $IFDEF DELPHI and related code
  2063. - remove $IFDEF FPCPROCVAR and related code
  2064. Revision 1.158 2004/10/12 19:51:13 peter
  2065. * all checking for visibility is now done by is_visible_for_object
  2066. Revision 1.157 2004/10/11 15:48:15 peter
  2067. * small regvar for para fixes
  2068. * function tvarsym.is_regvar added
  2069. * tvarsym.getvaluesize removed, use getsize instead
  2070. Revision 1.156 2004/10/08 17:09:43 peter
  2071. * tvarsym.varregable added, split vo_regable from varoptions
  2072. Revision 1.155 2004/08/17 16:29:21 jonas
  2073. + padalgingment field for recordsymtables (saved by recorddefs)
  2074. + support for Macintosh PowerPC alignment (if the first field of a record
  2075. or union has an alignment > 4, then the record or union size must be
  2076. padded to a multiple of this size)
  2077. Revision 1.154 2004/08/15 15:05:16 peter
  2078. * fixed padding of records to alignment
  2079. Revision 1.153 2004/08/15 13:30:18 florian
  2080. * fixed alignment of variant records
  2081. * more alignment problems fixed
  2082. Revision 1.152 2004/07/09 22:17:32 peter
  2083. * revert has_localst patch
  2084. * replace aktstaticsymtable/aktglobalsymtable with current_module
  2085. Revision 1.151 2004/06/23 16:22:45 peter
  2086. * include unit name in error messages when types are the same
  2087. Revision 1.150 2004/06/20 08:55:30 florian
  2088. * logs truncated
  2089. Revision 1.149 2004/06/16 20:07:09 florian
  2090. * dwarf branch merged
  2091. Revision 1.148 2004/05/25 18:50:50 peter
  2092. * check for 2gb limit when inserting record fields
  2093. Revision 1.147 2004/05/23 20:56:14 peter
  2094. * don't generate incompatible types when there is an errordef
  2095. Revision 1.146 2004/05/22 23:34:28 peter
  2096. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  2097. Revision 1.145 2004/04/29 19:56:37 daniel
  2098. * Prepare compiler infrastructure for multiple ansistring types
  2099. }