symtable.pas 84 KB

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