symtable.pas 84 KB

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