symtable.pas 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502
  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,symppu,
  30. { assembler }
  31. aasmbase,aasmtai,aasmcpu
  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 unitsymbolused(p : TNamedIndexItem;arg:pointer);
  44. procedure varsymbolused(p : TNamedIndexItem;arg:pointer);
  45. procedure TestPrivate(p : TNamedIndexItem;arg:pointer);
  46. procedure objectprivatesymbolused(p : TNamedIndexItem;arg:pointer);
  47. {$ifdef GDB}
  48. private
  49. procedure concatstab(p : TNamedIndexItem;arg:pointer);
  50. procedure resetstab(p : TNamedIndexItem;arg:pointer);
  51. procedure concattypestab(p : TNamedIndexItem;arg:pointer);
  52. {$endif}
  53. procedure unchain_overloads(p : TNamedIndexItem;arg:pointer);
  54. procedure loaddefs(ppufile:tcompilerppufile);
  55. procedure loadsyms(ppufile:tcompilerppufile);
  56. procedure writedefs(ppufile:tcompilerppufile);
  57. procedure writesyms(ppufile:tcompilerppufile);
  58. public
  59. { load/write }
  60. procedure ppuload(ppufile:tcompilerppufile);virtual;
  61. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  62. procedure load_references(ppufile:tcompilerppufile;locals:boolean);virtual;
  63. procedure write_references(ppufile:tcompilerppufile;locals:boolean);virtual;
  64. procedure deref;virtual;
  65. procedure derefimpl;virtual;
  66. procedure insert(sym : tsymentry);override;
  67. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;override;
  68. procedure allsymbolsused;
  69. procedure allprivatesused;
  70. procedure allunitsused;
  71. procedure check_forwards;
  72. procedure checklabels;
  73. function needs_init_final : boolean;
  74. procedure unchain_overloaded;
  75. procedure chainoperators;
  76. {$ifdef GDB}
  77. procedure concatstabto(asmlist : taasmoutput);virtual;
  78. function getnewtypecount : word; override;
  79. {$endif GDB}
  80. procedure testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  81. end;
  82. tabstractrecordsymtable = class(tstoredsymtable)
  83. public
  84. datasize : longint;
  85. dataalignment : byte;
  86. constructor create(const n:string);
  87. procedure ppuload(ppufile:tcompilerppufile);override;
  88. procedure ppuwrite(ppufile:tcompilerppufile);override;
  89. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  90. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  91. procedure insertfield(sym:tvarsym;addsym:boolean);
  92. end;
  93. trecordsymtable = class(tabstractrecordsymtable)
  94. public
  95. constructor create;
  96. procedure insert_in(tsymt : trecordsymtable;offset : longint);
  97. end;
  98. tobjectsymtable = class(tabstractrecordsymtable)
  99. public
  100. constructor create(const n:string);
  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. is_stab_written : boolean;
  124. {$endif GDB}
  125. constructor create(const n : string);
  126. {$ifdef GDB}
  127. procedure concattypestabto(asmlist : taasmoutput);
  128. {$endif GDB}
  129. end;
  130. tglobalsymtable = class(tabstractunitsymtable)
  131. public
  132. unitsym : tunitsym;
  133. unittypecount : word;
  134. constructor create(const n : string);
  135. destructor destroy;override;
  136. procedure ppuload(ppufile:tcompilerppufile);override;
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  139. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  140. procedure insert(sym : tsymentry);override;
  141. {$ifdef GDB}
  142. function getnewtypecount : word; override;
  143. {$endif}
  144. end;
  145. tstaticsymtable = class(tabstractunitsymtable)
  146. public
  147. constructor create(const n : string);
  148. procedure ppuload(ppufile:tcompilerppufile);override;
  149. procedure ppuwrite(ppufile:tcompilerppufile);override;
  150. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  151. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  152. procedure insert(sym : tsymentry);override;
  153. end;
  154. twithsymtable = class(tsymtable)
  155. withrefnode : pointer; { tnode }
  156. constructor create(aowner:tdef;asymsearch:TDictionary;refnode:pointer{tnode});
  157. destructor destroy;override;
  158. procedure clear;override;
  159. end;
  160. tstt_exceptsymtable = class(tsymtable)
  161. public
  162. constructor create;
  163. end;
  164. var
  165. constsymtable : tsymtable; { symtable were the constants can be inserted }
  166. systemunit : tglobalsymtable; { pointer to the system unit }
  167. {****************************************************************************
  168. Functions
  169. ****************************************************************************}
  170. {*** Misc ***}
  171. procedure globaldef(const s : string;var t:ttype);
  172. function findunitsymtable(st:tsymtable):tsymtable;
  173. procedure duplicatesym(sym:tsym);
  174. {*** Search ***}
  175. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  176. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  177. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  178. function searchsym_in_class_by_msgint(classh:tobjectdef;i:longint):tsym;
  179. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string):tsym;
  180. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  181. function searchsysvar(const s: stringid; var srsym: tvarsym; var symowner: tsymtable): boolean;
  182. function search_class_member(pd : tobjectdef;const s : string):tsym;
  183. {*** Object Helpers ***}
  184. function search_default_property(pd : tobjectdef) : tpropertysym;
  185. {*** symtable stack ***}
  186. procedure RestoreUnitSyms;
  187. {$ifdef DEBUG}
  188. procedure test_symtablestack;
  189. procedure list_symtablestack;
  190. {$endif DEBUG}
  191. {$ifdef UNITALIASES}
  192. type
  193. punit_alias = ^tunit_alias;
  194. tunit_alias = object(TNamedIndexItem)
  195. newname : pstring;
  196. constructor init(const n:string);
  197. destructor done;virtual;
  198. end;
  199. var
  200. unitaliases : pdictionary;
  201. procedure addunitalias(const n:string);
  202. function getunitalias(const n:string):string;
  203. {$endif UNITALIASES}
  204. {*** Init / Done ***}
  205. procedure InitSymtable;
  206. procedure DoneSymtable;
  207. type
  208. toverloaded_operators = array[NOTOKEN..last_overloaded] of tprocsym;
  209. var
  210. overloaded_operators : toverloaded_operators;
  211. { unequal is not equal}
  212. const
  213. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  214. ('error',
  215. 'plus','minus','star','slash','equal',
  216. 'greater','lower','greater_or_equal',
  217. 'lower_or_equal',
  218. 'sym_diff','starstar',
  219. 'as','is','in','or',
  220. 'and','div','mod','not','shl','shr','xor',
  221. 'assign');
  222. implementation
  223. uses
  224. { global }
  225. verbose,globals,
  226. { target }
  227. systems,
  228. { symtable }
  229. symutil,
  230. { module }
  231. fmodule,
  232. {$ifdef GDB}
  233. gdb,
  234. {$endif GDB}
  235. { codegen }
  236. cgbase,tgobj
  237. ;
  238. {*****************************************************************************
  239. TStoredSymtable
  240. *****************************************************************************}
  241. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  242. begin
  243. { load definitions }
  244. loaddefs(ppufile);
  245. { load symbols }
  246. loadsyms(ppufile);
  247. end;
  248. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  249. begin
  250. { write definitions }
  251. writedefs(ppufile);
  252. { write symbols }
  253. writesyms(ppufile);
  254. end;
  255. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  256. var
  257. hp : tdef;
  258. b : byte;
  259. begin
  260. { load start of definition section, which holds the amount of defs }
  261. if ppufile.readentry<>ibstartdefs then
  262. Message(unit_f_ppu_read_error);
  263. ppufile.getlongint;
  264. { read definitions }
  265. repeat
  266. b:=ppufile.readentry;
  267. case b of
  268. ibpointerdef : hp:=tpointerdef.ppuload(ppufile);
  269. ibarraydef : hp:=tarraydef.ppuload(ppufile);
  270. iborddef : hp:=torddef.ppuload(ppufile);
  271. ibfloatdef : hp:=tfloatdef.ppuload(ppufile);
  272. ibprocdef : hp:=tprocdef.ppuload(ppufile);
  273. ibshortstringdef : hp:=tstringdef.loadshort(ppufile);
  274. iblongstringdef : hp:=tstringdef.loadlong(ppufile);
  275. ibansistringdef : hp:=tstringdef.loadansi(ppufile);
  276. ibwidestringdef : hp:=tstringdef.loadwide(ppufile);
  277. ibrecorddef : hp:=trecorddef.ppuload(ppufile);
  278. ibobjectdef : hp:=tobjectdef.ppuload(ppufile);
  279. ibenumdef : hp:=tenumdef.ppuload(ppufile);
  280. ibsetdef : hp:=tsetdef.ppuload(ppufile);
  281. ibprocvardef : hp:=tprocvardef.ppuload(ppufile);
  282. ibfiledef : hp:=tfiledef.ppuload(ppufile);
  283. ibclassrefdef : hp:=tclassrefdef.ppuload(ppufile);
  284. ibformaldef : hp:=tformaldef.ppuload(ppufile);
  285. ibvariantdef : hp:=tvariantdef.ppuload(ppufile);
  286. ibenddefs : break;
  287. ibend : Message(unit_f_ppu_read_error);
  288. else
  289. Message1(unit_f_ppu_invalid_entry,tostr(b));
  290. end;
  291. hp.owner:=self;
  292. defindex.insert(hp);
  293. until false;
  294. end;
  295. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  296. var
  297. b : byte;
  298. sym : tsym;
  299. begin
  300. { load start of definition section, which holds the amount of defs }
  301. if ppufile.readentry<>ibstartsyms then
  302. Message(unit_f_ppu_read_error);
  303. { skip amount of symbols, not used currently }
  304. ppufile.getlongint;
  305. { now read the symbols }
  306. repeat
  307. b:=ppufile.readentry;
  308. case b of
  309. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  310. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  311. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  312. ibvarsym : sym:=tvarsym.ppuload(ppufile);
  313. ibabsolutesym : sym:=tabsolutesym.ppuload(ppufile);
  314. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  315. ibtypedconstsym : sym:=ttypedconstsym.ppuload(ppufile);
  316. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  317. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  318. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  319. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  320. ibrttisym : sym:=trttisym.ppuload(ppufile);
  321. ibendsyms : break;
  322. ibend : Message(unit_f_ppu_read_error);
  323. else
  324. Message1(unit_f_ppu_invalid_entry,tostr(b));
  325. end;
  326. sym.owner:=self;
  327. symindex.insert(sym);
  328. symsearch.insert(sym);
  329. until false;
  330. end;
  331. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  332. var
  333. pd : tstoreddef;
  334. begin
  335. { each definition get a number, write then the amount of defs to the
  336. ibstartdef entry }
  337. ppufile.putlongint(defindex.count);
  338. ppufile.writeentry(ibstartdefs);
  339. { now write the definition }
  340. pd:=tstoreddef(defindex.first);
  341. while assigned(pd) do
  342. begin
  343. pd.ppuwrite(ppufile);
  344. pd:=tstoreddef(pd.indexnext);
  345. end;
  346. { write end of definitions }
  347. ppufile.writeentry(ibenddefs);
  348. end;
  349. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  350. var
  351. pd : tstoredsym;
  352. begin
  353. { each definition get a number, write then the amount of syms and the
  354. datasize to the ibsymdef entry }
  355. ppufile.putlongint(symindex.count);
  356. ppufile.writeentry(ibstartsyms);
  357. { foreach is used to write all symbols }
  358. pd:=tstoredsym(symindex.first);
  359. while assigned(pd) do
  360. begin
  361. pd.ppuwrite(ppufile);
  362. pd:=tstoredsym(pd.indexnext);
  363. end;
  364. { end of symbols }
  365. ppufile.writeentry(ibendsyms);
  366. end;
  367. procedure tstoredsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  368. var
  369. b : byte;
  370. d : tderef;
  371. sym : tstoredsym;
  372. prdef : tstoreddef;
  373. begin
  374. b:=ppufile.readentry;
  375. if b <> ibbeginsymtablebrowser then
  376. Message1(unit_f_ppu_invalid_entry,tostr(b));
  377. repeat
  378. b:=ppufile.readentry;
  379. case b of
  380. ibsymref :
  381. begin
  382. ppufile.getderef(d);
  383. sym:=tstoredsym(d.resolve);
  384. if assigned(sym) then
  385. sym.load_references(ppufile,locals);
  386. end;
  387. ibdefref :
  388. begin
  389. ppufile.getderef(d);
  390. prdef:=tstoreddef(d.resolve);
  391. if assigned(prdef) then
  392. begin
  393. if prdef.deftype<>procdef then
  394. Message(unit_f_ppu_read_error);
  395. tprocdef(prdef).load_references(ppufile,locals);
  396. end;
  397. end;
  398. ibendsymtablebrowser :
  399. break;
  400. else
  401. Message1(unit_f_ppu_invalid_entry,tostr(b));
  402. end;
  403. until false;
  404. end;
  405. procedure tstoredsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  406. var
  407. pd : tstoredsym;
  408. begin
  409. ppufile.writeentry(ibbeginsymtablebrowser);
  410. { write all symbols }
  411. pd:=tstoredsym(symindex.first);
  412. while assigned(pd) do
  413. begin
  414. pd.write_references(ppufile,locals);
  415. pd:=tstoredsym(pd.indexnext);
  416. end;
  417. ppufile.writeentry(ibendsymtablebrowser);
  418. end;
  419. procedure tstoredsymtable.deref;
  420. var
  421. hp : tdef;
  422. hs : tsym;
  423. begin
  424. { first deref the interface ttype symbols. This is needs
  425. to be done before the interface defs are derefed, because
  426. the interface defs can contain references to the type symbols
  427. which then already need to contain a resolved restype field (PFV) }
  428. hs:=tsym(symindex.first);
  429. while assigned(hs) do
  430. begin
  431. if hs.typ=typesym then
  432. hs.deref;
  433. hs:=tsym(hs.indexnext);
  434. end;
  435. { deref the interface definitions }
  436. hp:=tdef(defindex.first);
  437. while assigned(hp) do
  438. begin
  439. hp.deref;
  440. hp:=tdef(hp.indexnext);
  441. end;
  442. { deref the interface symbols }
  443. hs:=tsym(symindex.first);
  444. while assigned(hs) do
  445. begin
  446. if hs.typ<>typesym then
  447. hs.deref;
  448. hs:=tsym(hs.indexnext);
  449. end;
  450. end;
  451. procedure tstoredsymtable.derefimpl;
  452. var
  453. hp : tdef;
  454. begin
  455. { deref the implementation part of definitions }
  456. hp:=tdef(defindex.first);
  457. while assigned(hp) do
  458. begin
  459. hp.derefimpl;
  460. hp:=tdef(hp.indexnext);
  461. end;
  462. end;
  463. procedure tstoredsymtable.insert(sym:tsymentry);
  464. var
  465. hsym : tsym;
  466. begin
  467. { set owner and sym indexnb }
  468. sym.owner:=self;
  469. { check the current symtable }
  470. hsym:=tsym(search(sym.name));
  471. if assigned(hsym) then
  472. begin
  473. { in TP and Delphi you can have a local with the
  474. same name as the function, the function is then hidden for
  475. the user. (Under delphi it can still be accessed using result),
  476. but don't allow hiding of RESULT }
  477. if (m_duplicate_names in aktmodeswitches) and
  478. (sym.typ in [varsym,absolutesym]) and
  479. (vo_is_funcret in tvarsym(sym).varoptions) and
  480. not((m_result in aktmodeswitches) and
  481. (vo_is_result in tvarsym(sym).varoptions)) then
  482. sym.name:='hidden'+sym.name
  483. else
  484. begin
  485. DuplicateSym(hsym);
  486. exit;
  487. end;
  488. end;
  489. { register definition of typesym }
  490. if (sym.typ = typesym) and
  491. assigned(ttypesym(sym).restype.def) then
  492. begin
  493. if not(assigned(ttypesym(sym).restype.def.owner)) and
  494. (ttypesym(sym).restype.def.deftype<>errordef) then
  495. registerdef(ttypesym(sym).restype.def);
  496. {$ifdef GDB}
  497. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  498. (symtabletype in [globalsymtable,staticsymtable]) then
  499. begin
  500. ttypesym(sym).isusedinstab := true;
  501. {sym.concatstabto(debuglist);}
  502. end;
  503. {$endif GDB}
  504. end;
  505. { insert in index and search hash }
  506. symindex.insert(sym);
  507. symsearch.insert(sym);
  508. end;
  509. function tstoredsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  510. var
  511. hp : tstoredsym;
  512. newref : tref;
  513. begin
  514. hp:=tstoredsym(inherited speedsearch(s,speedvalue));
  515. if assigned(hp) then
  516. begin
  517. { reject non static members in static procedures }
  518. if (symtabletype=objectsymtable) and
  519. not(sp_static in hp.symoptions) and
  520. allow_only_static then
  521. Message(sym_e_only_static_in_static);
  522. { unit uses count }
  523. if (unitid<>0) and
  524. (symtabletype = globalsymtable) and
  525. assigned(tglobalsymtable(self).unitsym) then
  526. inc(tglobalsymtable(self).unitsym.refs);
  527. {$ifdef GDB}
  528. { if it is a type, we need the stabs of this type
  529. this might be the cause of the class debug problems
  530. as TCHILDCLASS.Create did not generate appropriate
  531. stabs debug info if TCHILDCLASS wasn't used anywhere else PM }
  532. if (cs_debuginfo in aktmoduleswitches) and
  533. (hp.typ=typesym) and
  534. make_ref then
  535. begin
  536. if assigned(ttypesym(hp).restype.def) then
  537. tstoreddef(ttypesym(hp).restype.def).numberstring
  538. else
  539. ttypesym(hp).isusedinstab:=true;
  540. end;
  541. {$endif GDB}
  542. { unitsym are only loaded for browsing PM }
  543. { this was buggy anyway because we could use }
  544. { unitsyms from other units in _USES !! }
  545. {if (symtabletype=unitsymtable) and (hp.typ=unitsym) and
  546. assigned(current_module) and (current_module.globalsymtable<>.load) then
  547. hp:=nil;}
  548. if make_ref and (cs_browser in aktmoduleswitches) then
  549. begin
  550. newref:=tref.create(hp.lastref,@akttokenpos);
  551. { for symbols that are in tables without browser info or syssyms }
  552. if hp.refcount=0 then
  553. begin
  554. hp.defref:=newref;
  555. hp.lastref:=newref;
  556. end
  557. else
  558. if resolving_forward and assigned(hp.defref) then
  559. { put it as second reference }
  560. begin
  561. newref.nextref:=hp.defref.nextref;
  562. hp.defref.nextref:=newref;
  563. hp.lastref.nextref:=nil;
  564. end
  565. else
  566. hp.lastref:=newref;
  567. inc(hp.refcount);
  568. end;
  569. if make_ref then
  570. inc(hp.refs);
  571. end; { value was not found }
  572. speedsearch:=hp;
  573. end;
  574. {**************************************
  575. Callbacks
  576. **************************************}
  577. procedure TStoredSymtable.check_forward(sym : TNamedIndexItem;arg:pointer);
  578. begin
  579. if tsym(sym).typ=procsym then
  580. tprocsym(sym).check_forward
  581. { check also object method table }
  582. { we needn't to test the def list }
  583. { because each object has to have a type sym }
  584. else
  585. if (tsym(sym).typ=typesym) and
  586. assigned(ttypesym(sym).restype.def) and
  587. (ttypesym(sym).restype.def.deftype=objectdef) then
  588. tobjectdef(ttypesym(sym).restype.def).check_forwards;
  589. end;
  590. procedure TStoredSymtable.labeldefined(p : TNamedIndexItem;arg:pointer);
  591. begin
  592. if (tsym(p).typ=labelsym) and
  593. not(tlabelsym(p).defined) then
  594. begin
  595. if tlabelsym(p).used then
  596. Message1(sym_e_label_used_and_not_defined,tlabelsym(p).realname)
  597. else
  598. Message1(sym_w_label_not_defined,tlabelsym(p).realname);
  599. end;
  600. end;
  601. procedure TStoredSymtable.unitsymbolused(p : TNamedIndexItem;arg:pointer);
  602. begin
  603. if (tsym(p).typ=unitsym) and
  604. (tunitsym(p).refs=0) and
  605. { do not claim for unit name itself !! }
  606. assigned(tunitsym(p).unitsymtable) and
  607. (tunitsym(p).unitsymtable.symtabletype=globalsymtable) then
  608. MessagePos2(tsym(p).fileinfo,sym_n_unit_not_used,p.name,current_module.modulename^);
  609. end;
  610. procedure TStoredSymtable.varsymbolused(p : TNamedIndexItem;arg:pointer);
  611. begin
  612. if (tsym(p).typ=varsym) and
  613. ((tsym(p).owner.symtabletype in
  614. [parasymtable,localsymtable,objectsymtable,staticsymtable])) then
  615. begin
  616. { unused symbol should be reported only if no }
  617. { error is reported }
  618. { if the symbol is in a register it is used }
  619. { also don't count the value parameters which have local copies }
  620. { also don't claim for high param of open parameters (PM) }
  621. if (Errorcount<>0) or
  622. (vo_is_self in tvarsym(p).varoptions) or
  623. (vo_is_vmt in tvarsym(p).varoptions) or
  624. (vo_is_high_value in tvarsym(p).varoptions) or
  625. (copy(p.name,1,6)='hidden') then
  626. exit;
  627. if (tvarsym(p).refs=0) then
  628. begin
  629. if (vo_is_funcret in tvarsym(p).varoptions) then
  630. begin
  631. { don't warn about the result of constructors }
  632. if (tsym(p).owner.symtabletype<>localsymtable) or
  633. (tprocdef(tsym(p).owner.defowner).proctypeoption<>potype_constructor) then
  634. MessagePos(tsym(p).fileinfo,sym_w_function_result_not_set)
  635. end
  636. else if (tsym(p).owner.symtabletype=parasymtable) then
  637. MessagePos1(tsym(p).fileinfo,sym_h_para_identifier_not_used,tsym(p).realname)
  638. else if (tsym(p).owner.symtabletype=objectsymtable) then
  639. MessagePos2(tsym(p).fileinfo,sym_n_private_identifier_not_used,tsym(p).owner.realname^,tsym(p).realname)
  640. else
  641. MessagePos1(tsym(p).fileinfo,sym_n_local_identifier_not_used,tsym(p).realname);
  642. end
  643. else if tvarsym(p).varstate=vs_assigned then
  644. begin
  645. if (tsym(p).owner.symtabletype=parasymtable) then
  646. begin
  647. if not(tvarsym(p).varspez in [vs_var,vs_out]) and
  648. not(vo_is_funcret in tvarsym(p).varoptions) then
  649. MessagePos1(tsym(p).fileinfo,sym_h_para_identifier_only_set,tsym(p).realname)
  650. end
  651. else if (tsym(p).owner.symtabletype=objectsymtable) then
  652. MessagePos2(tsym(p).fileinfo,sym_n_private_identifier_only_set,tsym(p).owner.realname^,tsym(p).realname)
  653. else if not(vo_is_exported in tvarsym(p).varoptions) and
  654. not(vo_is_funcret in tvarsym(p).varoptions) then
  655. MessagePos1(tsym(p).fileinfo,sym_n_local_identifier_only_set,tsym(p).realname);
  656. end;
  657. end
  658. else if ((tsym(p).owner.symtabletype in
  659. [objectsymtable,parasymtable,localsymtable,staticsymtable])) then
  660. begin
  661. if (Errorcount<>0) or
  662. (copy(p.name,1,3)='def') then
  663. exit;
  664. { do not claim for inherited private fields !! }
  665. if (tstoredsym(p).refs=0) and (tsym(p).owner.symtabletype=objectsymtable) then
  666. MessagePos2(tsym(p).fileinfo,sym_n_private_method_not_used,tsym(p).owner.realname^,tsym(p).realname)
  667. { units references are problematic }
  668. else
  669. begin
  670. if (tstoredsym(p).refs=0) and
  671. not(tsym(p).typ in [enumsym,unitsym]) and
  672. not(is_funcret_sym(tsym(p))) and
  673. (
  674. (tsym(p).typ<>procsym) or
  675. {$ifdef GDB}
  676. not (tprocsym(p).is_global) or
  677. {$endif GDB}
  678. { all program functions are declared global
  679. but unused should still be signaled PM }
  680. ((tsym(p).owner.symtabletype=staticsymtable) and
  681. not current_module.is_unit)
  682. ) then
  683. MessagePos2(tsym(p).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(p).typ],tsym(p).realname);
  684. end;
  685. end;
  686. end;
  687. procedure TStoredSymtable.TestPrivate(p : TNamedIndexItem;arg:pointer);
  688. begin
  689. if sp_private in tsym(p).symoptions then
  690. varsymbolused(p,arg);
  691. end;
  692. procedure TStoredSymtable.objectprivatesymbolused(p : TNamedIndexItem;arg:pointer);
  693. begin
  694. {
  695. Don't test simple object aliases PM
  696. }
  697. if (tsym(p).typ=typesym) and
  698. (ttypesym(p).restype.def.deftype=objectdef) and
  699. (ttypesym(p).restype.def.typesym=tsym(p)) then
  700. tobjectdef(ttypesym(p).restype.def).symtable.foreach({$ifdef FPCPROCVAR}@{$endif}TestPrivate,nil);
  701. end;
  702. procedure tstoredsymtable.unchain_overloads(p : TNamedIndexItem;arg:pointer);
  703. begin
  704. if tsym(p).typ=procsym then
  705. tprocsym(p).unchain_overload;
  706. end;
  707. {$ifdef GDB}
  708. procedure TStoredSymtable.concatstab(p : TNamedIndexItem;arg:pointer);
  709. begin
  710. if tsym(p).typ <> procsym then
  711. tstoredsym(p).concatstabto(TAAsmOutput(arg));
  712. end;
  713. procedure TStoredSymtable.resetstab(p : TNamedIndexItem;arg:pointer);
  714. begin
  715. if tsym(p).typ <> procsym then
  716. tstoredsym(p).isstabwritten:=false;
  717. end;
  718. procedure TStoredSymtable.concattypestab(p : TNamedIndexItem;arg:pointer);
  719. begin
  720. if tsym(p).typ = typesym then
  721. begin
  722. tstoredsym(p).isstabwritten:=false;
  723. tstoredsym(p).concatstabto(TAAsmOutput(arg));
  724. end;
  725. end;
  726. function tstoredsymtable.getnewtypecount : word;
  727. begin
  728. getnewtypecount:=pglobaltypecount^;
  729. inc(pglobaltypecount^);
  730. end;
  731. {$endif GDB}
  732. procedure tstoredsymtable.chainoperators;
  733. var
  734. t : ttoken;
  735. srsym : tsym;
  736. srsymtable,
  737. storesymtablestack : tsymtable;
  738. begin
  739. storesymtablestack:=symtablestack;
  740. symtablestack:=self;
  741. make_ref:=false;
  742. for t:=first_overloaded to last_overloaded do
  743. begin
  744. overloaded_operators[t]:=nil;
  745. { each operator has a unique lowercased internal name PM }
  746. while assigned(symtablestack) do
  747. begin
  748. searchsym(overloaded_names[t],srsym,srsymtable);
  749. if not assigned(srsym) then
  750. begin
  751. if (t=_STARSTAR) then
  752. begin
  753. symtablestack:=systemunit;
  754. searchsym('POWER',srsym,srsymtable);
  755. end;
  756. end;
  757. if assigned(srsym) then
  758. begin
  759. if (srsym.typ<>procsym) then
  760. internalerror(12344321);
  761. { remove all previous chains }
  762. tprocsym(srsym).unchain_overload;
  763. { use this procsym as start ? }
  764. if not assigned(overloaded_operators[t]) then
  765. overloaded_operators[t]:=tprocsym(srsym)
  766. else
  767. { already got a procsym, only add defs defined in the
  768. unit of the current procsym }
  769. Tprocsym(srsym).concat_procdefs_to(overloaded_operators[t]);
  770. symtablestack:=srsym.owner.next;
  771. end
  772. else
  773. begin
  774. symtablestack:=nil;
  775. end;
  776. { search for same procsym in other units }
  777. end;
  778. symtablestack:=self;
  779. end;
  780. make_ref:=true;
  781. symtablestack:=storesymtablestack;
  782. end;
  783. {***********************************************
  784. Process all entries
  785. ***********************************************}
  786. { checks, if all procsyms and methods are defined }
  787. procedure tstoredsymtable.check_forwards;
  788. begin
  789. foreach({$ifdef FPCPROCVAR}@{$endif}check_forward,nil);
  790. end;
  791. procedure tstoredsymtable.checklabels;
  792. begin
  793. foreach({$ifdef FPCPROCVAR}@{$endif}labeldefined,nil);
  794. end;
  795. procedure tstoredsymtable.allunitsused;
  796. begin
  797. foreach({$ifdef FPCPROCVAR}@{$endif}unitsymbolused,nil);
  798. end;
  799. procedure tstoredsymtable.allsymbolsused;
  800. begin
  801. foreach({$ifdef FPCPROCVAR}@{$endif}varsymbolused,nil);
  802. end;
  803. procedure tstoredsymtable.allprivatesused;
  804. begin
  805. foreach({$ifdef FPCPROCVAR}@{$endif}objectprivatesymbolused,nil);
  806. end;
  807. procedure tstoredsymtable.unchain_overloaded;
  808. begin
  809. foreach({$ifdef FPCPROCVAR}@{$endif}unchain_overloads,nil);
  810. end;
  811. {$ifdef GDB}
  812. procedure tstoredsymtable.concatstabto(asmlist : taasmoutput);
  813. begin
  814. if symtabletype in [inlineparasymtable,inlinelocalsymtable] then
  815. foreach({$ifdef FPCPROCVAR}@{$endif}resetstab,nil);
  816. foreach({$ifdef FPCPROCVAR}@{$endif}concatstab,asmlist);
  817. end;
  818. {$endif}
  819. procedure TStoredSymtable._needs_init_final(p : tnamedindexitem;arg:pointer);
  820. begin
  821. if b_needs_init_final then
  822. exit;
  823. case tsym(p).typ of
  824. varsym :
  825. begin
  826. if not(is_class(tvarsym(p).vartype.def)) and
  827. tstoreddef(tvarsym(p).vartype.def).needs_inittable then
  828. b_needs_init_final:=true;
  829. end;
  830. typedconstsym :
  831. begin
  832. if ttypedconstsym(p).is_writable and
  833. tstoreddef(ttypedconstsym(p).typedconsttype.def).needs_inittable then
  834. b_needs_init_final:=true;
  835. end;
  836. end;
  837. end;
  838. { returns true, if p contains data which needs init/final code }
  839. function tstoredsymtable.needs_init_final : boolean;
  840. begin
  841. b_needs_init_final:=false;
  842. foreach({$ifdef FPCPROCVAR}@{$endif}_needs_init_final,nil);
  843. needs_init_final:=b_needs_init_final;
  844. end;
  845. {****************************************************************************
  846. TAbstractRecordSymtable
  847. ****************************************************************************}
  848. constructor tabstractrecordsymtable.create(const n:string);
  849. begin
  850. inherited create(n);
  851. datasize:=0;
  852. dataalignment:=1;
  853. end;
  854. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  855. var
  856. storesymtable : tsymtable;
  857. begin
  858. storesymtable:=aktrecordsymtable;
  859. aktrecordsymtable:=self;
  860. inherited ppuload(ppufile);
  861. aktrecordsymtable:=storesymtable;
  862. end;
  863. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  864. var
  865. oldtyp : byte;
  866. storesymtable : tsymtable;
  867. begin
  868. storesymtable:=aktrecordsymtable;
  869. aktrecordsymtable:=self;
  870. oldtyp:=ppufile.entrytyp;
  871. ppufile.entrytyp:=subentryid;
  872. inherited ppuwrite(ppufile);
  873. ppufile.entrytyp:=oldtyp;
  874. aktrecordsymtable:=storesymtable;
  875. end;
  876. procedure tabstractrecordsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  877. var
  878. storesymtable : tsymtable;
  879. begin
  880. storesymtable:=aktrecordsymtable;
  881. aktrecordsymtable:=self;
  882. inherited load_references(ppufile,locals);
  883. aktrecordsymtable:=storesymtable;
  884. end;
  885. procedure tabstractrecordsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  886. var
  887. storesymtable : tsymtable;
  888. begin
  889. storesymtable:=aktrecordsymtable;
  890. aktrecordsymtable:=self;
  891. inherited write_references(ppufile,locals);
  892. aktrecordsymtable:=storesymtable;
  893. end;
  894. procedure tabstractrecordsymtable.insertfield(sym : tvarsym;addsym:boolean);
  895. var
  896. l,varalign : longint;
  897. vardef : tdef;
  898. begin
  899. if addsym then
  900. insert(sym);
  901. { Calculate field offset }
  902. l:=tvarsym(sym).getvaluesize;
  903. vardef:=tvarsym(sym).vartype.def;
  904. { this symbol can't be loaded to a register }
  905. exclude(tvarsym(sym).varoptions,vo_regable);
  906. exclude(tvarsym(sym).varoptions,vo_fpuregable);
  907. { get the alignment size }
  908. if (aktalignment.recordalignmax=-1) then
  909. begin
  910. varalign:=vardef.alignment;
  911. if (varalign>4) and
  912. ((varalign mod 4)<>0) and
  913. (vardef.deftype=arraydef) then
  914. Message1(sym_w_wrong_C_pack,vardef.typename);
  915. if varalign=0 then
  916. varalign:=l;
  917. if (dataalignment<aktalignment.maxCrecordalign) then
  918. begin
  919. if (varalign>16) and (dataalignment<32) then
  920. dataalignment:=32
  921. else if (varalign>12) and (dataalignment<16) then
  922. dataalignment:=16
  923. { 12 is needed for long double }
  924. else if (varalign>8) and (dataalignment<12) then
  925. dataalignment:=12
  926. else if (varalign>4) and (dataalignment<8) then
  927. dataalignment:=8
  928. else if (varalign>2) and (dataalignment<4) then
  929. dataalignment:=4
  930. else if (varalign>1) and (dataalignment<2) then
  931. dataalignment:=2;
  932. end;
  933. dataalignment:=min(dataalignment,aktalignment.maxCrecordalign);
  934. end
  935. else
  936. varalign:=vardef.alignment;
  937. if varalign=0 then
  938. varalign:=size_2_align(l);
  939. varalign:=used_align(varalign,aktalignment.recordalignmin,dataalignment);
  940. tvarsym(sym).fieldoffset:=align(datasize,varalign);
  941. datasize:=tvarsym(sym).fieldoffset+l;
  942. end;
  943. {****************************************************************************
  944. TRecordSymtable
  945. ****************************************************************************}
  946. constructor trecordsymtable.create;
  947. begin
  948. inherited create('');
  949. symtabletype:=recordsymtable;
  950. end;
  951. { this procedure is reserved for inserting case variant into
  952. a record symtable }
  953. { the offset is the location of the start of the variant
  954. and datasize and dataalignment corresponds to
  955. the complete size (see code in pdecl unit) PM }
  956. procedure trecordsymtable.insert_in(tsymt : trecordsymtable;offset : longint);
  957. var
  958. ps,nps : tvarsym;
  959. pd,npd : tdef;
  960. storesize,storealign : longint;
  961. begin
  962. storesize:=tsymt.datasize;
  963. storealign:=tsymt.dataalignment;
  964. tsymt.datasize:=offset;
  965. ps:=tvarsym(symindex.first);
  966. while assigned(ps) do
  967. begin
  968. nps:=tvarsym(ps.indexnext);
  969. { remove from current symtable }
  970. symindex.deleteindex(ps);
  971. ps.left:=nil;
  972. ps.right:=nil;
  973. { add to symt }
  974. ps.owner:=tsymt;
  975. tsymt.datasize:=ps.fieldoffset+offset;
  976. tsymt.symindex.insert(ps);
  977. tsymt.symsearch.insert(ps);
  978. { update address }
  979. ps.fieldoffset:=tsymt.datasize;
  980. { next }
  981. ps:=nps;
  982. end;
  983. pd:=tdef(defindex.first);
  984. while assigned(pd) do
  985. begin
  986. npd:=tdef(pd.indexnext);
  987. defindex.deleteindex(pd);
  988. pd.left:=nil;
  989. pd.right:=nil;
  990. tsymt.registerdef(pd);
  991. pd:=npd;
  992. end;
  993. tsymt.datasize:=storesize;
  994. tsymt.dataalignment:=storealign;
  995. end;
  996. {****************************************************************************
  997. TObjectSymtable
  998. ****************************************************************************}
  999. constructor tobjectsymtable.create(const n:string);
  1000. begin
  1001. inherited create(n);
  1002. symtabletype:=objectsymtable;
  1003. end;
  1004. procedure tobjectsymtable.insert(sym:tsymentry);
  1005. var
  1006. hsym : tsym;
  1007. begin
  1008. { check for duplicate field id in inherited classes }
  1009. if (sym.typ=varsym) and
  1010. assigned(defowner) and
  1011. (
  1012. not(m_delphi in aktmodeswitches) or
  1013. is_object(tdef(defowner))
  1014. ) then
  1015. begin
  1016. { but private ids can be reused }
  1017. hsym:=search_class_member(tobjectdef(defowner),sym.name);
  1018. if assigned(hsym) and
  1019. tstoredsym(hsym).is_visible_for_object(tobjectdef(defowner)) then
  1020. begin
  1021. DuplicateSym(hsym);
  1022. exit;
  1023. end;
  1024. end;
  1025. inherited insert(sym);
  1026. end;
  1027. {****************************************************************************
  1028. TAbstractLocalSymtable
  1029. ****************************************************************************}
  1030. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1031. var
  1032. oldtyp : byte;
  1033. begin
  1034. oldtyp:=ppufile.entrytyp;
  1035. ppufile.entrytyp:=subentryid;
  1036. { write definitions }
  1037. writedefs(ppufile);
  1038. { write symbols }
  1039. writesyms(ppufile);
  1040. ppufile.entrytyp:=oldtyp;
  1041. end;
  1042. {****************************************************************************
  1043. TLocalSymtable
  1044. ****************************************************************************}
  1045. constructor tlocalsymtable.create(level:byte);
  1046. begin
  1047. inherited create('');
  1048. symtabletype:=localsymtable;
  1049. symtablelevel:=level;
  1050. end;
  1051. procedure tlocalsymtable.insert(sym:tsymentry);
  1052. var
  1053. hsym : tsym;
  1054. begin
  1055. { need to hide function result? }
  1056. hsym:=tsym(search(sym.name));
  1057. if assigned(hsym) then
  1058. begin
  1059. { a local and the function can have the same
  1060. name in TP and Delphi, but RESULT not }
  1061. if (m_duplicate_names in aktmodeswitches) and
  1062. (hsym.typ in [absolutesym,varsym]) and
  1063. (vo_is_funcret in tvarsym(hsym).varoptions) and
  1064. not((m_result in aktmodeswitches) and
  1065. (vo_is_result in tvarsym(hsym).varoptions)) then
  1066. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1067. else
  1068. begin
  1069. DuplicateSym(hsym);
  1070. exit;
  1071. end;
  1072. end;
  1073. if assigned(next) and
  1074. (next.symtabletype=parasymtable) then
  1075. begin
  1076. { check para symtable }
  1077. hsym:=tsym(next.search(sym.name));
  1078. if assigned(hsym) then
  1079. begin
  1080. { a local and the function can have the same
  1081. name in TP and Delphi, but RESULT not }
  1082. if (m_duplicate_names in aktmodeswitches) and
  1083. (sym.typ in [absolutesym,varsym]) and
  1084. (vo_is_funcret in tvarsym(sym).varoptions) and
  1085. not((m_result in aktmodeswitches) and
  1086. (vo_is_result in tvarsym(sym).varoptions)) then
  1087. sym.name:='hidden'+sym.name
  1088. else
  1089. begin
  1090. DuplicateSym(hsym);
  1091. exit;
  1092. end;
  1093. end;
  1094. { check for duplicate id in local symtable of methods }
  1095. if assigned(next.next) and
  1096. { funcretsym is allowed !! }
  1097. (not is_funcret_sym(sym)) and
  1098. (next.next.symtabletype=objectsymtable) then
  1099. begin
  1100. hsym:=search_class_member(tobjectdef(next.next.defowner),sym.name);
  1101. if assigned(hsym) and
  1102. { private ids can be reused }
  1103. (not(sp_private in hsym.symoptions) or
  1104. (hsym.owner.defowner.owner.symtabletype<>globalsymtable)) then
  1105. begin
  1106. { delphi allows to reuse the names in a class, but not
  1107. in object (tp7 compatible) }
  1108. if not((m_delphi in aktmodeswitches) and
  1109. is_class(tdef(next.next.defowner))) then
  1110. begin
  1111. DuplicateSym(hsym);
  1112. exit;
  1113. end;
  1114. end;
  1115. end;
  1116. end;
  1117. inherited insert(sym);
  1118. end;
  1119. {****************************************************************************
  1120. TParaSymtable
  1121. ****************************************************************************}
  1122. constructor tparasymtable.create(level:byte);
  1123. begin
  1124. inherited create('');
  1125. symtabletype:=parasymtable;
  1126. symtablelevel:=level;
  1127. end;
  1128. procedure tparasymtable.insert(sym:tsymentry);
  1129. var
  1130. hsym : tsym;
  1131. begin
  1132. { check for duplicate id in para symtable of methods }
  1133. if assigned(next) and
  1134. (next.symtabletype=objectsymtable) and
  1135. { funcretsym is allowed }
  1136. (not is_funcret_sym(sym)) then
  1137. begin
  1138. hsym:=search_class_member(tobjectdef(next.defowner),sym.name);
  1139. { private ids can be reused }
  1140. if assigned(hsym) and
  1141. tstoredsym(hsym).is_visible_for_object(tobjectdef(next.defowner)) then
  1142. begin
  1143. { delphi allows to reuse the names in a class, but not
  1144. in object (tp7 compatible) }
  1145. if not((m_delphi in aktmodeswitches) and
  1146. is_class_or_interface(tobjectdef(next.defowner))) then
  1147. begin
  1148. DuplicateSym(hsym);
  1149. exit;
  1150. end;
  1151. end;
  1152. end;
  1153. inherited insert(sym);
  1154. end;
  1155. {****************************************************************************
  1156. TAbstractUnitSymtable
  1157. ****************************************************************************}
  1158. constructor tabstractunitsymtable.create(const n : string);
  1159. begin
  1160. inherited create(n);
  1161. symsearch.usehash;
  1162. {$ifdef GDB}
  1163. { reset GDB things }
  1164. prev_dbx_counter := dbx_counter;
  1165. dbx_counter := nil;
  1166. is_stab_written:=false;
  1167. dbx_count := -1;
  1168. {$endif GDB}
  1169. end;
  1170. {$ifdef GDB}
  1171. procedure tabstractunitsymtable.concattypestabto(asmlist : taasmoutput);
  1172. var prev_dbx_count : plongint;
  1173. begin
  1174. if is_stab_written then
  1175. exit;
  1176. if not assigned(name) then
  1177. name := stringdup('Main_program');
  1178. if (symtabletype = globalsymtable) and
  1179. (current_module.globalsymtable<>self) then
  1180. begin
  1181. unitid:=current_module.unitcount;
  1182. inc(current_module.unitcount);
  1183. end;
  1184. asmList.concat(tai_comment.Create(strpnew('Begin unit '+name^+' has index '+tostr(unitid))));
  1185. if cs_gdb_dbx in aktglobalswitches then
  1186. begin
  1187. if dbx_count_ok then
  1188. begin
  1189. asmList.concat(tai_comment.Create(strpnew('"repeated" unit '+name^
  1190. +' has index '+tostr(unitid)+' dbx count = '+tostr(dbx_count))));
  1191. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1192. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count))));
  1193. exit;
  1194. end
  1195. else if (current_module.globalsymtable<>self) then
  1196. begin
  1197. prev_dbx_count := dbx_counter;
  1198. dbx_counter := nil;
  1199. do_count_dbx:=false;
  1200. if (symtabletype = globalsymtable) and (unitid<>0) then
  1201. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1202. dbx_counter := @dbx_count;
  1203. dbx_count:=0;
  1204. do_count_dbx:=assigned(dbx_counter);
  1205. end;
  1206. end;
  1207. foreach({$ifdef FPCPROCVAR}@{$endif}concattypestab,asmlist);
  1208. if cs_gdb_dbx in aktglobalswitches then
  1209. begin
  1210. if (current_module.globalsymtable<>self) then
  1211. begin
  1212. dbx_counter := prev_dbx_count;
  1213. do_count_dbx:=false;
  1214. asmList.concat(tai_comment.Create(strpnew('End unit '+name^
  1215. +' has index '+tostr(unitid))));
  1216. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1217. +tostr(N_EINCL)+',0,0,0')));
  1218. do_count_dbx:=assigned(dbx_counter);
  1219. dbx_count_ok := {true}false;
  1220. end;
  1221. end;
  1222. is_stab_written:=true;
  1223. end;
  1224. {$endif GDB}
  1225. {****************************************************************************
  1226. TStaticSymtable
  1227. ****************************************************************************}
  1228. constructor tstaticsymtable.create(const n : string);
  1229. begin
  1230. inherited create(n);
  1231. symtabletype:=staticsymtable;
  1232. symtablelevel:=main_program_level;
  1233. end;
  1234. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1235. begin
  1236. aktstaticsymtable:=self;
  1237. next:=symtablestack;
  1238. symtablestack:=self;
  1239. inherited ppuload(ppufile);
  1240. { now we can deref the syms and defs }
  1241. deref;
  1242. { restore symtablestack }
  1243. symtablestack:=next;
  1244. end;
  1245. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1246. begin
  1247. aktstaticsymtable:=self;
  1248. inherited ppuwrite(ppufile);
  1249. end;
  1250. procedure tstaticsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1251. begin
  1252. aktstaticsymtable:=self;
  1253. inherited load_references(ppufile,locals);
  1254. end;
  1255. procedure tstaticsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1256. begin
  1257. aktstaticsymtable:=self;
  1258. inherited write_references(ppufile,locals);
  1259. end;
  1260. procedure tstaticsymtable.insert(sym:tsymentry);
  1261. var
  1262. hsym : tsym;
  1263. begin
  1264. { also check the global symtable }
  1265. if assigned(next) and
  1266. (next.unitid=0) then
  1267. begin
  1268. hsym:=tsym(next.search(sym.name));
  1269. if assigned(hsym) then
  1270. begin
  1271. { Delphi you can have a symbol with the same name as the
  1272. unit, the unit can then not be accessed anymore using
  1273. <unit>.<id>, so we can hide the symbol }
  1274. if (m_duplicate_names in aktmodeswitches) and
  1275. (hsym.typ=symconst.unitsym) then
  1276. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1277. else
  1278. begin
  1279. DuplicateSym(hsym);
  1280. exit;
  1281. end;
  1282. end;
  1283. end;
  1284. inherited insert(sym);
  1285. end;
  1286. {****************************************************************************
  1287. TGlobalSymtable
  1288. ****************************************************************************}
  1289. constructor tglobalsymtable.create(const n : string);
  1290. begin
  1291. inherited create(n);
  1292. symtabletype:=globalsymtable;
  1293. symtablelevel:=main_program_level;
  1294. unitid:=0;
  1295. unitsym:=nil;
  1296. {$ifdef GDB}
  1297. if cs_gdb_dbx in aktglobalswitches then
  1298. begin
  1299. dbx_count := 0;
  1300. unittypecount:=1;
  1301. pglobaltypecount := @unittypecount;
  1302. {unitid:=current_module.unitcount;}
  1303. debugList.concat(tai_comment.Create(strpnew('Global '+name^+' has index '+tostr(unitid))));
  1304. debugList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1305. {inc(current_module.unitcount);}
  1306. { we can't use dbx_vcount, because we don't know
  1307. if the object file will be loaded before or afeter PM }
  1308. dbx_count_ok:=false;
  1309. dbx_counter:=@dbx_count;
  1310. do_count_dbx:=true;
  1311. end;
  1312. {$endif GDB}
  1313. end;
  1314. destructor tglobalsymtable.destroy;
  1315. var
  1316. pus : tunitsym;
  1317. begin
  1318. pus:=unitsym;
  1319. while assigned(pus) do
  1320. begin
  1321. unitsym:=pus.prevsym;
  1322. pus.prevsym:=nil;
  1323. pus.unitsymtable:=nil;
  1324. pus:=unitsym;
  1325. end;
  1326. inherited destroy;
  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. aktglobalsymtable:=self;
  1342. next:=symtablestack;
  1343. symtablestack:=self;
  1344. inherited ppuload(ppufile);
  1345. { now we can deref the syms and defs }
  1346. deref;
  1347. { restore symtablestack }
  1348. symtablestack:=next;
  1349. { read dbx count }
  1350. {$ifdef GDB}
  1351. if (current_module.flags and uf_has_dbx)<>0 then
  1352. begin
  1353. b:=ppufile.readentry;
  1354. if b<>ibdbxcount then
  1355. Message(unit_f_ppu_dbx_count_problem)
  1356. else
  1357. dbx_count:=ppufile.getlongint;
  1358. {$IfDef EXTDEBUG}
  1359. writeln('Read dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1360. {$ENDIF EXTDEBUG}
  1361. { we can't use dbx_vcount, because we don't know
  1362. if the object file will be loaded before or afeter PM }
  1363. dbx_count_ok := {true}false;
  1364. end
  1365. else
  1366. begin
  1367. dbx_count:=-1;
  1368. dbx_count_ok:=false;
  1369. end;
  1370. {$endif GDB}
  1371. end;
  1372. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1373. begin
  1374. aktglobalsymtable:=self;
  1375. { write the symtable entries }
  1376. inherited ppuwrite(ppufile);
  1377. { write dbx count }
  1378. {$ifdef GDB}
  1379. if cs_gdb_dbx in aktglobalswitches then
  1380. begin
  1381. {$IfDef EXTDEBUG}
  1382. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1383. {$ENDIF EXTDEBUG}
  1384. ppufile.do_crc:=false;
  1385. ppufile.putlongint(dbx_count);
  1386. ppufile.writeentry(ibdbxcount);
  1387. ppufile.do_crc:=true;
  1388. end;
  1389. {$endif GDB}
  1390. end;
  1391. procedure tglobalsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1392. begin
  1393. aktglobalsymtable:=self;
  1394. inherited load_references(ppufile,locals);
  1395. end;
  1396. procedure tglobalsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1397. begin
  1398. aktglobalsymtable:=self;
  1399. inherited write_references(ppufile,locals);
  1400. end;
  1401. procedure tglobalsymtable.insert(sym:tsymentry);
  1402. var
  1403. hsym : tsym;
  1404. begin
  1405. { also check the global symtable }
  1406. if assigned(next) and
  1407. (next.unitid=0) then
  1408. begin
  1409. hsym:=tsym(next.search(sym.name));
  1410. if assigned(hsym) then
  1411. begin
  1412. { Delphi you can have a symbol with the same name as the
  1413. unit, the unit can then not be accessed anymore using
  1414. <unit>.<id>, so we can hide the symbol }
  1415. if (m_duplicate_names in aktmodeswitches) and
  1416. (hsym.typ=symconst.unitsym) then
  1417. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1418. else
  1419. begin
  1420. DuplicateSym(hsym);
  1421. exit;
  1422. end;
  1423. end;
  1424. end;
  1425. hsym:=tsym(search(sym.name));
  1426. if assigned(hsym) then
  1427. begin
  1428. { Delphi you can have a symbol with the same name as the
  1429. unit, the unit can then not be accessed anymore using
  1430. <unit>.<id>, so we can hide the symbol }
  1431. if (m_duplicate_names in aktmodeswitches) and
  1432. (hsym.typ=symconst.unitsym) then
  1433. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1434. else
  1435. begin
  1436. DuplicateSym(hsym);
  1437. exit;
  1438. end;
  1439. end;
  1440. inherited insert(sym);
  1441. end;
  1442. {$ifdef GDB}
  1443. function tglobalsymtable.getnewtypecount : word;
  1444. begin
  1445. if not (cs_gdb_dbx in aktglobalswitches) then
  1446. getnewtypecount:=inherited getnewtypecount
  1447. else
  1448. begin
  1449. getnewtypecount:=unittypecount;
  1450. inc(unittypecount);
  1451. end;
  1452. end;
  1453. {$endif}
  1454. {****************************************************************************
  1455. TWITHSYMTABLE
  1456. ****************************************************************************}
  1457. constructor twithsymtable.create(aowner:tdef;asymsearch:TDictionary;refnode:pointer{tnode});
  1458. begin
  1459. inherited create('');
  1460. symtabletype:=withsymtable;
  1461. withrefnode:=refnode;
  1462. { we don't need the symsearch }
  1463. symsearch.free;
  1464. { set the defaults }
  1465. symsearch:=asymsearch;
  1466. defowner:=aowner;
  1467. end;
  1468. destructor twithsymtable.destroy;
  1469. begin
  1470. symsearch:=nil;
  1471. inherited destroy;
  1472. end;
  1473. procedure twithsymtable.clear;
  1474. begin
  1475. { remove no entry from a withsymtable as it is only a pointer to the
  1476. recorddef or objectdef symtable }
  1477. end;
  1478. {****************************************************************************
  1479. TSTT_ExceptionSymtable
  1480. ****************************************************************************}
  1481. constructor tstt_exceptsymtable.create;
  1482. begin
  1483. inherited create('');
  1484. symtabletype:=stt_exceptsymtable;
  1485. end;
  1486. {*****************************************************************************
  1487. Helper Routines
  1488. *****************************************************************************}
  1489. function findunitsymtable(st:tsymtable):tsymtable;
  1490. begin
  1491. findunitsymtable:=nil;
  1492. repeat
  1493. if not assigned(st) then
  1494. internalerror(5566561);
  1495. case st.symtabletype of
  1496. localsymtable,
  1497. parasymtable,
  1498. staticsymtable :
  1499. exit;
  1500. globalsymtable :
  1501. begin
  1502. findunitsymtable:=st;
  1503. exit;
  1504. end;
  1505. objectsymtable :
  1506. st:=st.defowner.owner;
  1507. recordsymtable :
  1508. begin
  1509. { don't continue when the current
  1510. symtable is used for variant records }
  1511. if trecorddef(st.defowner).isunion then
  1512. begin
  1513. findunitsymtable:=nil;
  1514. exit;
  1515. end
  1516. else
  1517. st:=st.defowner.owner;
  1518. end;
  1519. else
  1520. internalerror(5566562);
  1521. end;
  1522. until false;
  1523. end;
  1524. procedure duplicatesym(sym:tsym);
  1525. var
  1526. st : tsymtable;
  1527. begin
  1528. Message1(sym_e_duplicate_id,sym.realname);
  1529. st:=findunitsymtable(sym.owner);
  1530. with sym.fileinfo do
  1531. begin
  1532. if assigned(st) and (st.unitid<>0) then
  1533. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  1534. else
  1535. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line));
  1536. end;
  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. begin
  1545. speedvalue:=getspeedvalue(s);
  1546. srsymtable:=symtablestack;
  1547. while assigned(srsymtable) do
  1548. begin
  1549. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1550. if assigned(srsym) and
  1551. (not assigned(current_procinfo) or
  1552. tstoredsym(srsym).is_visible_for_proc(current_procinfo.procdef)) then
  1553. begin
  1554. searchsym:=true;
  1555. exit;
  1556. end
  1557. else
  1558. srsymtable:=srsymtable.next;
  1559. end;
  1560. searchsym:=false;
  1561. end;
  1562. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  1563. var
  1564. srsym : tsym;
  1565. begin
  1566. { the caller have to take care if srsym=nil }
  1567. if assigned(p) then
  1568. begin
  1569. srsym:=tsym(p.search(s));
  1570. if assigned(srsym) then
  1571. begin
  1572. searchsymonlyin:=srsym;
  1573. exit;
  1574. end;
  1575. { also check in the local symtbale if it exists }
  1576. if (p=tsymtable(current_module.globalsymtable)) then
  1577. begin
  1578. srsym:=tsym(current_module.localsymtable.search(s));
  1579. if assigned(srsym) then
  1580. begin
  1581. searchsymonlyin:=srsym;
  1582. exit;
  1583. end;
  1584. end
  1585. end;
  1586. searchsymonlyin:=nil;
  1587. end;
  1588. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  1589. var
  1590. speedvalue : cardinal;
  1591. topclassh : tobjectdef;
  1592. sym : tsym;
  1593. begin
  1594. speedvalue:=getspeedvalue(s);
  1595. { when the class passed is defined in this unit we
  1596. need to use the scope of that class. This is a trick
  1597. that can be used to access protected members in other
  1598. units. At least kylix supports it this way (PFV) }
  1599. if assigned(classh) and
  1600. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1601. (classh.owner.unitid=0) then
  1602. topclassh:=classh
  1603. else
  1604. topclassh:=nil;
  1605. sym:=nil;
  1606. while assigned(classh) do
  1607. begin
  1608. sym:=tsym(classh.symtable.speedsearch(s,speedvalue));
  1609. if assigned(sym) then
  1610. begin
  1611. if assigned(topclassh) then
  1612. begin
  1613. if tstoredsym(sym).is_visible_for_object(topclassh) then
  1614. break;
  1615. end
  1616. else
  1617. begin
  1618. if (not assigned(current_procinfo) or
  1619. tstoredsym(sym).is_visible_for_proc(current_procinfo.procdef)) then
  1620. break;
  1621. end;
  1622. end;
  1623. classh:=classh.childof;
  1624. end;
  1625. searchsym_in_class:=sym;
  1626. end;
  1627. function searchsym_in_class_by_msgint(classh:tobjectdef;i:longint):tsym;
  1628. var
  1629. topclassh : tobjectdef;
  1630. def : tdef;
  1631. sym : tsym;
  1632. begin
  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. topclassh:=nil;
  1643. sym:=nil;
  1644. def:=nil;
  1645. while assigned(classh) do
  1646. begin
  1647. def:=tdef(classh.symtable.defindex.first);
  1648. while assigned(def) do
  1649. begin
  1650. if (def.deftype=procdef) and
  1651. (po_msgint in tprocdef(def).procoptions) and
  1652. (tprocdef(def).messageinf.i=i) then
  1653. begin
  1654. sym:=tprocdef(def).procsym;
  1655. if assigned(topclassh) then
  1656. begin
  1657. if tprocdef(def).is_visible_for_object(topclassh) then
  1658. break;
  1659. end
  1660. else
  1661. begin
  1662. if (not assigned(current_procinfo) or
  1663. tprocdef(def).is_visible_for_proc(current_procinfo.procdef)) then
  1664. break;
  1665. end;
  1666. end;
  1667. def:=tdef(def.indexnext);
  1668. end;
  1669. if assigned(sym) then
  1670. break;
  1671. classh:=classh.childof;
  1672. end;
  1673. searchsym_in_class_by_msgint:=sym;
  1674. end;
  1675. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string):tsym;
  1676. var
  1677. topclassh : tobjectdef;
  1678. def : tdef;
  1679. sym : tsym;
  1680. begin
  1681. { when the class passed is defined in this unit we
  1682. need to use the scope of that class. This is a trick
  1683. that can be used to access protected members in other
  1684. units. At least kylix supports it this way (PFV) }
  1685. if assigned(classh) and
  1686. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1687. (classh.owner.unitid=0) then
  1688. topclassh:=classh
  1689. else
  1690. topclassh:=nil;
  1691. sym:=nil;
  1692. def:=nil;
  1693. while assigned(classh) do
  1694. begin
  1695. def:=tdef(classh.symtable.defindex.first);
  1696. while assigned(def) do
  1697. begin
  1698. if (def.deftype=procdef) and
  1699. (po_msgstr in tprocdef(def).procoptions) and
  1700. (tprocdef(def).messageinf.str=s) then
  1701. begin
  1702. sym:=tprocdef(def).procsym;
  1703. if assigned(topclassh) then
  1704. begin
  1705. if tprocdef(def).is_visible_for_object(topclassh) then
  1706. break;
  1707. end
  1708. else
  1709. begin
  1710. if (not assigned(current_procinfo) or
  1711. tprocdef(def).is_visible_for_proc(current_procinfo.procdef)) then
  1712. break;
  1713. end;
  1714. end;
  1715. def:=tdef(def.indexnext);
  1716. end;
  1717. if assigned(sym) then
  1718. break;
  1719. classh:=classh.childof;
  1720. end;
  1721. searchsym_in_class_by_msgstr:=sym;
  1722. end;
  1723. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  1724. var
  1725. symowner: tsymtable;
  1726. begin
  1727. if not(cs_compilesystem in aktmoduleswitches) then
  1728. srsym := ttypesym(searchsymonlyin(systemunit,s))
  1729. else
  1730. searchsym(s,tsym(srsym),symowner);
  1731. searchsystype :=
  1732. assigned(srsym) and
  1733. (srsym.typ = typesym);
  1734. end;
  1735. function searchsysvar(const s: stringid; var srsym: tvarsym; var symowner: tsymtable): boolean;
  1736. begin
  1737. if not(cs_compilesystem in aktmoduleswitches) then
  1738. begin
  1739. srsym := tvarsym(searchsymonlyin(systemunit,s));
  1740. symowner := systemunit;
  1741. end
  1742. else
  1743. searchsym(s,tsym(srsym),symowner);
  1744. searchsysvar :=
  1745. assigned(srsym) and
  1746. (srsym.typ = varsym);
  1747. end;
  1748. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1749. { searches n in symtable of pd and all anchestors }
  1750. var
  1751. speedvalue : cardinal;
  1752. srsym : tsym;
  1753. begin
  1754. speedvalue:=getspeedvalue(s);
  1755. while assigned(pd) do
  1756. begin
  1757. srsym:=tsym(pd.symtable.speedsearch(s,speedvalue));
  1758. if assigned(srsym) then
  1759. begin
  1760. search_class_member:=srsym;
  1761. exit;
  1762. end;
  1763. pd:=pd.childof;
  1764. end;
  1765. search_class_member:=nil;
  1766. end;
  1767. {*****************************************************************************
  1768. Definition Helpers
  1769. *****************************************************************************}
  1770. procedure globaldef(const s : string;var t:ttype);
  1771. var st : string;
  1772. symt : tsymtable;
  1773. srsym : tsym;
  1774. srsymtable : tsymtable;
  1775. begin
  1776. srsym := nil;
  1777. if pos('.',s) > 0 then
  1778. begin
  1779. st := copy(s,1,pos('.',s)-1);
  1780. searchsym(st,srsym,srsymtable);
  1781. st := copy(s,pos('.',s)+1,255);
  1782. if assigned(srsym) then
  1783. begin
  1784. if srsym.typ = unitsym then
  1785. begin
  1786. symt := tunitsym(srsym).unitsymtable;
  1787. srsym := tsym(symt.search(st));
  1788. end else srsym := nil;
  1789. end;
  1790. end else st := s;
  1791. if srsym = nil then
  1792. searchsym(st,srsym,srsymtable);
  1793. if srsym = nil then
  1794. srsym:=searchsymonlyin(systemunit,st);
  1795. if (not assigned(srsym)) or
  1796. (srsym.typ<>typesym) then
  1797. begin
  1798. Message(type_e_type_id_expected);
  1799. t:=generrortype;
  1800. exit;
  1801. end;
  1802. t := ttypesym(srsym).restype;
  1803. end;
  1804. {****************************************************************************
  1805. Object Helpers
  1806. ****************************************************************************}
  1807. var
  1808. _defaultprop : tpropertysym;
  1809. procedure tstoredsymtable.testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  1810. begin
  1811. if (tsym(p).typ=propertysym) and
  1812. (ppo_defaultproperty in tpropertysym(p).propoptions) then
  1813. _defaultprop:=tpropertysym(p);
  1814. end;
  1815. function search_default_property(pd : tobjectdef) : tpropertysym;
  1816. { returns the default property of a class, searches also anchestors }
  1817. begin
  1818. _defaultprop:=nil;
  1819. while assigned(pd) do
  1820. begin
  1821. pd.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}tstoredsymtable(pd.symtable).testfordefaultproperty,nil);
  1822. if assigned(_defaultprop) then
  1823. break;
  1824. pd:=pd.childof;
  1825. end;
  1826. search_default_property:=_defaultprop;
  1827. end;
  1828. {$ifdef UNITALIASES}
  1829. {****************************************************************************
  1830. TUNIT_ALIAS
  1831. ****************************************************************************}
  1832. constructor tunit_alias.create(const n:string);
  1833. var
  1834. i : longint;
  1835. begin
  1836. i:=pos('=',n);
  1837. if i=0 then
  1838. fail;
  1839. inherited createname(Copy(n,1,i-1));
  1840. newname:=stringdup(Copy(n,i+1,255));
  1841. end;
  1842. destructor tunit_alias.destroy;
  1843. begin
  1844. stringdispose(newname);
  1845. inherited destroy;
  1846. end;
  1847. procedure addunitalias(const n:string);
  1848. begin
  1849. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1850. end;
  1851. function getunitalias(const n:string):string;
  1852. var
  1853. p : punit_alias;
  1854. begin
  1855. p:=punit_alias(unitaliases^.search(Upper(n)));
  1856. if assigned(p) then
  1857. getunitalias:=punit_alias(p).newname^
  1858. else
  1859. getunitalias:=n;
  1860. end;
  1861. {$endif UNITALIASES}
  1862. {****************************************************************************
  1863. Symtable Stack
  1864. ****************************************************************************}
  1865. procedure RestoreUnitSyms;
  1866. var
  1867. p : tsymtable;
  1868. begin
  1869. p:=symtablestack;
  1870. while assigned(p) do
  1871. begin
  1872. if (p.symtabletype=globalsymtable) and
  1873. assigned(tglobalsymtable(p).unitsym) and
  1874. ((tglobalsymtable(p).unitsym.owner=current_module.globalsymtable) or
  1875. (tglobalsymtable(p).unitsym.owner=current_module.localsymtable)) then
  1876. tglobalsymtable(p).unitsym.restoreunitsym;
  1877. p:=p.next;
  1878. end;
  1879. end;
  1880. {$ifdef DEBUG}
  1881. procedure test_symtablestack;
  1882. var
  1883. p : tsymtable;
  1884. i : longint;
  1885. begin
  1886. p:=symtablestack;
  1887. i:=0;
  1888. while assigned(p) do
  1889. begin
  1890. inc(i);
  1891. p:=p.next;
  1892. if i>500 then
  1893. Message(sym_f_internal_error_in_symtablestack);
  1894. end;
  1895. end;
  1896. procedure list_symtablestack;
  1897. var
  1898. p : tsymtable;
  1899. i : longint;
  1900. begin
  1901. p:=symtablestack;
  1902. i:=0;
  1903. while assigned(p) do
  1904. begin
  1905. inc(i);
  1906. writeln(i,' ',p.name^);
  1907. p:=p.next;
  1908. if i>500 then
  1909. Message(sym_f_internal_error_in_symtablestack);
  1910. end;
  1911. end;
  1912. {$endif DEBUG}
  1913. {****************************************************************************
  1914. Init/Done Symtable
  1915. ****************************************************************************}
  1916. procedure InitSymtable;
  1917. var
  1918. token : ttoken;
  1919. begin
  1920. { Reset symbolstack }
  1921. registerdef:=false;
  1922. symtablestack:=nil;
  1923. systemunit:=nil;
  1924. {$ifdef GDB}
  1925. firstglobaldef:=nil;
  1926. lastglobaldef:=nil;
  1927. globaltypecount:=1;
  1928. pglobaltypecount:=@globaltypecount;
  1929. {$endif GDB}
  1930. { defs for internal use }
  1931. voidprocdef:=tprocdef.create(unknown_level);
  1932. { create error syms and def }
  1933. generrorsym:=terrorsym.create;
  1934. generrortype.setdef(terrordef.create);
  1935. {$ifdef UNITALIASES}
  1936. { unit aliases }
  1937. unitaliases:=tdictionary.create;
  1938. {$endif}
  1939. for token:=first_overloaded to last_overloaded do
  1940. overloaded_operators[token]:=nil;
  1941. end;
  1942. procedure DoneSymtable;
  1943. begin
  1944. voidprocdef.free;
  1945. generrorsym.free;
  1946. generrortype.def.free;
  1947. {$ifdef UNITALIASES}
  1948. unitaliases.free;
  1949. {$endif}
  1950. end;
  1951. end.
  1952. {
  1953. $Log$
  1954. Revision 1.110 2003-09-23 17:56:06 peter
  1955. * locals and paras are allocated in the code generation
  1956. * tvarsym.localloc contains the location of para/local when
  1957. generating code for the current procedure
  1958. Revision 1.109 2003/08/23 22:31:08 peter
  1959. * unchain operators before adding to overloaded list
  1960. Revision 1.108 2003/06/25 18:31:23 peter
  1961. * sym,def resolving partly rewritten to support also parent objects
  1962. not directly available through the uses clause
  1963. Revision 1.107 2003/06/13 21:19:31 peter
  1964. * current_procdef removed, use current_procinfo.procdef instead
  1965. Revision 1.106 2003/06/09 18:26:27 peter
  1966. * para can be the same as function name in delphi
  1967. Revision 1.105 2003/06/08 11:40:00 peter
  1968. * check parast when inserting in localst
  1969. Revision 1.104 2003/06/07 20:26:32 peter
  1970. * re-resolving added instead of reloading from ppu
  1971. * tderef object added to store deref info for resolving
  1972. Revision 1.103 2003/05/25 11:34:17 peter
  1973. * methodpointer self pushing fixed
  1974. Revision 1.102 2003/05/23 14:27:35 peter
  1975. * remove some unit dependencies
  1976. * current_procinfo changes to store more info
  1977. Revision 1.101 2003/05/16 14:32:58 peter
  1978. * fix dup check for hiding the result varsym in localst, the result
  1979. sym was already in the localst when adding the locals
  1980. Revision 1.100 2003/05/15 18:58:53 peter
  1981. * removed selfpointer_offset, vmtpointer_offset
  1982. * tvarsym.adjusted_address
  1983. * address in localsymtable is now in the real direction
  1984. * removed some obsolete globals
  1985. Revision 1.99 2003/05/13 15:17:13 peter
  1986. * fix crash with hiding function result. The function result is now
  1987. inserted as last so the symbol that we are going to insert is the
  1988. result and needs to be renamed instead of the already existing
  1989. symbol
  1990. Revision 1.98 2003/05/11 14:45:12 peter
  1991. * tloadnode does not support objectsymtable,withsymtable anymore
  1992. * withnode cleanup
  1993. * direct with rewritten to use temprefnode
  1994. Revision 1.97 2003/04/27 11:21:34 peter
  1995. * aktprocdef renamed to current_procinfo.procdef
  1996. * procinfo renamed to current_procinfo
  1997. * procinfo will now be stored in current_module so it can be
  1998. cleaned up properly
  1999. * gen_main_procsym changed to create_main_proc and release_main_proc
  2000. to also generate a tprocinfo structure
  2001. * fixed unit implicit initfinal
  2002. Revision 1.96 2003/04/27 07:29:51 peter
  2003. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  2004. a new procdef declaration
  2005. * aktprocsym removed
  2006. * lexlevel removed, use symtable.symtablelevel instead
  2007. * implicit init/final code uses the normal genentry/genexit
  2008. * funcret state checking updated for new funcret handling
  2009. Revision 1.95 2003/04/26 00:33:07 peter
  2010. * vo_is_result flag added for the special RESULT symbol
  2011. Revision 1.94 2003/04/25 20:59:35 peter
  2012. * removed funcretn,funcretsym, function result is now in varsym
  2013. and aliases for result and function name are added using absolutesym
  2014. * vs_hidden parameter for funcret passed in parameter
  2015. * vs_hidden fixes
  2016. * writenode changed to printnode and released from extdebug
  2017. * -vp option added to generate a tree.log with the nodetree
  2018. * nicer printnode for statements, callnode
  2019. Revision 1.93 2003/04/16 07:53:11 jonas
  2020. * calculation of parameter and resultlocation offsets now depends on
  2021. tg.direction instead of if(n)def powerpc
  2022. Revision 1.92 2003/04/05 21:09:32 jonas
  2023. * several ppc/generic result offset related fixes. The "normal" result
  2024. offset seems now to be calculated correctly and a lot of duplicate
  2025. calculations have been removed. Nested functions accessing the parent's
  2026. function result don't work at all though :(
  2027. Revision 1.91 2003/03/17 18:56:49 peter
  2028. * ignore hints for default parameter values
  2029. Revision 1.90 2003/03/17 16:54:41 peter
  2030. * support DefaultHandler and anonymous inheritance fixed
  2031. for message methods
  2032. Revision 1.89 2002/12/29 14:57:50 peter
  2033. * unit loading changed to first register units and load them
  2034. afterwards. This is needed to support uses xxx in yyy correctly
  2035. * unit dependency check fixed
  2036. Revision 1.88 2002/12/27 18:07:45 peter
  2037. * fix crashes when searching symbols
  2038. Revision 1.87 2002/12/25 01:26:56 peter
  2039. * duplicate procsym-unitsym fix
  2040. Revision 1.86 2002/12/21 13:07:34 peter
  2041. * type redefine fix for tb0437
  2042. Revision 1.85 2002/12/07 14:27:10 carl
  2043. * 3% memory optimization
  2044. * changed some types
  2045. + added type checking with different size for call node and for
  2046. parameters
  2047. Revision 1.84 2002/12/06 17:51:11 peter
  2048. * merged cdecl and array fixes
  2049. Revision 1.83 2002/11/30 11:12:48 carl
  2050. + checking for symbols used with hint directives is done mostly in pexpr
  2051. only now
  2052. Revision 1.82 2002/11/29 22:31:20 carl
  2053. + unimplemented hint directive added
  2054. * hint directive parsing implemented
  2055. * warning on these directives
  2056. Revision 1.81 2002/11/27 20:04:09 peter
  2057. * tvarsym.get_push_size replaced by paramanager.push_size
  2058. Revision 1.80 2002/11/22 22:45:49 carl
  2059. + small optimization for speed
  2060. Revision 1.79 2002/11/19 16:26:33 pierre
  2061. * correct a stabs generation problem that lead to use errordef in stabs
  2062. Revision 1.78 2002/11/18 17:32:00 peter
  2063. * pass proccalloption to ret_in_xxx and push_xxx functions
  2064. Revision 1.77 2002/11/15 01:58:54 peter
  2065. * merged changes from 1.0.7 up to 04-11
  2066. - -V option for generating bug report tracing
  2067. - more tracing for option parsing
  2068. - errors for cdecl and high()
  2069. - win32 import stabs
  2070. - win32 records<=8 are returned in eax:edx (turned off by default)
  2071. - heaptrc update
  2072. - more info for temp management in .s file with EXTDEBUG
  2073. Revision 1.76 2002/11/09 15:29:28 carl
  2074. + bss / constant alignment fixes
  2075. * avoid incrementing address/datasize in local symtable for const's
  2076. Revision 1.75 2002/10/14 19:44:43 peter
  2077. * threadvars need 4 bytes extra for storing the threadvar index
  2078. Revision 1.74 2002/10/06 19:41:31 peter
  2079. * Add finalization of typed consts
  2080. * Finalization of globals in the main program
  2081. Revision 1.73 2002/10/05 12:43:29 carl
  2082. * fixes for Delphi 6 compilation
  2083. (warning : Some features do not work under Delphi)
  2084. Revision 1.72 2002/09/09 19:41:46 peter
  2085. * real fix internalerror for dup ids in union sym
  2086. Revision 1.71 2002/09/09 17:34:16 peter
  2087. * tdicationary.replace added to replace and item in a dictionary. This
  2088. is only allowed for the same name
  2089. * varsyms are inserted in symtable before the types are parsed. This
  2090. fixes the long standing "var longint : longint" bug
  2091. - consume_idlist and idstringlist removed. The loops are inserted
  2092. at the callers place and uses the symtable for duplicate id checking
  2093. Revision 1.70 2002/09/05 19:29:45 peter
  2094. * memdebug enhancements
  2095. Revision 1.69 2002/08/25 19:25:21 peter
  2096. * sym.insert_in_data removed
  2097. * symtable.insertvardata/insertconstdata added
  2098. * removed insert_in_data call from symtable.insert, it needs to be
  2099. called separatly. This allows to deref the address calculation
  2100. * procedures now calculate the parast addresses after the procedure
  2101. directives are parsed. This fixes the cdecl parast problem
  2102. * push_addr_param has an extra argument that specifies if cdecl is used
  2103. or not
  2104. Revision 1.68 2002/08/18 20:06:27 peter
  2105. * inlining is now also allowed in interface
  2106. * renamed write/load to ppuwrite/ppuload
  2107. * tnode storing in ppu
  2108. * nld,ncon,nbas are already updated for storing in ppu
  2109. Revision 1.67 2002/08/17 09:23:43 florian
  2110. * first part of procinfo rewrite
  2111. Revision 1.66 2002/08/11 13:24:15 peter
  2112. * saving of asmsymbols in ppu supported
  2113. * asmsymbollist global is removed and moved into a new class
  2114. tasmlibrarydata that will hold the info of a .a file which
  2115. corresponds with a single module. Added librarydata to tmodule
  2116. to keep the library info stored for the module. In the future the
  2117. objectfiles will also be stored to the tasmlibrarydata class
  2118. * all getlabel/newasmsymbol and friends are moved to the new class
  2119. Revision 1.65 2002/07/23 09:51:27 daniel
  2120. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2121. are worth comitting.
  2122. Revision 1.64 2002/07/16 15:34:21 florian
  2123. * exit is now a syssym instead of a keyword
  2124. Revision 1.63 2002/07/15 19:44:53 florian
  2125. * fixed crash with default parameters and stdcall calling convention
  2126. Revision 1.62 2002/07/01 18:46:28 peter
  2127. * internal linker
  2128. * reorganized aasm layer
  2129. Revision 1.61 2002/05/18 13:34:19 peter
  2130. * readded missing revisions
  2131. Revision 1.60 2002/05/16 19:46:45 carl
  2132. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2133. + try to fix temp allocation (still in ifdef)
  2134. + generic constructor calls
  2135. + start of tassembler / tmodulebase class cleanup
  2136. Revision 1.58 2002/05/12 16:53:15 peter
  2137. * moved entry and exitcode to ncgutil and cgobj
  2138. * foreach gets extra argument for passing local data to the
  2139. iterator function
  2140. * -CR checks also class typecasts at runtime by changing them
  2141. into as
  2142. * fixed compiler to cycle with the -CR option
  2143. * fixed stabs with elf writer, finally the global variables can
  2144. be watched
  2145. * removed a lot of routines from cga unit and replaced them by
  2146. calls to cgobj
  2147. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2148. u32bit then the other is typecasted also to u32bit without giving
  2149. a rangecheck warning/error.
  2150. * fixed pascal calling method with reversing also the high tree in
  2151. the parast, detected by tcalcst3 test
  2152. Revision 1.57 2002/04/04 19:06:05 peter
  2153. * removed unused units
  2154. * use tlocation.size in cg.a_*loc*() routines
  2155. Revision 1.56 2002/03/04 19:10:11 peter
  2156. * removed compiler warnings
  2157. Revision 1.55 2002/02/03 09:30:07 peter
  2158. * more fixes for protected handling
  2159. Revision 1.54 2002/01/29 21:30:25 peter
  2160. * allow also dup id in delphi mode in interfaces
  2161. Revision 1.53 2002/01/29 19:46:00 peter
  2162. * fixed recordsymtable.insert_in() for inserting variant record fields
  2163. to not used symtable.insert() because that also updates alignmentinfo
  2164. which was already set
  2165. Revision 1.52 2002/01/24 18:25:50 peter
  2166. * implicit result variable generation for assembler routines
  2167. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2168. }