symbase.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 symbase;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cutils,cclasses,
  24. { global }
  25. globtype,globals,
  26. { symtable }
  27. symconst
  28. ;
  29. {************************************************
  30. Some internal constants
  31. ************************************************}
  32. const
  33. hasharraysize = 256;
  34. indexgrowsize = 64;
  35. {$ifdef GDB}
  36. memsizeinc = 4096; { for long stabstrings }
  37. {$endif GDB}
  38. {************************************************
  39. Needed forward pointers
  40. ************************************************}
  41. type
  42. tsymtable = class;
  43. {************************************************
  44. TSymtableEntry
  45. ************************************************}
  46. tsymtableentry = class(TNamedIndexItem)
  47. owner : tsymtable;
  48. end;
  49. {************************************************
  50. TDefEntry
  51. ************************************************}
  52. tdefentry = class(tsymtableentry)
  53. deftype : tdeftype;
  54. end;
  55. {************************************************
  56. TSymEntry
  57. ************************************************}
  58. { this object is the base for all symbol objects }
  59. tsymentry = class(tsymtableentry)
  60. typ : tsymtyp;
  61. end;
  62. {************************************************
  63. TSymtable
  64. ************************************************}
  65. tsearchhasharray = array[0..hasharraysize-1] of tsymentry;
  66. psearchhasharray = ^tsearchhasharray;
  67. tsymtable = class
  68. {$ifdef EXTDEBUG}
  69. private
  70. procedure dumpsym(p : TNamedIndexItem;arg:pointer);
  71. {$endif EXTDEBUG}
  72. public
  73. name : pstring;
  74. realname : pstring;
  75. symindex,
  76. defindex : TIndexArray;
  77. symsearch : Tdictionary;
  78. next : tsymtable;
  79. defowner : tdefentry; { for records and objects }
  80. symtabletype : tsymtabletype;
  81. { each symtable gets a number }
  82. unitid : word;
  83. { level of symtable, used for nested procedures }
  84. symtablelevel : byte;
  85. refcount : integer;
  86. constructor Create(const s:string);
  87. destructor destroy;override;
  88. procedure freeinstance;override;
  89. function getcopy:tsymtable;
  90. procedure clear;virtual;
  91. function rename(const olds,news : stringid):tsymentry;
  92. procedure foreach(proc2call : tnamedindexcallback;arg:pointer);
  93. procedure foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  94. procedure insert(sym : tsymentry);virtual;
  95. procedure replace(oldsym,newsym:tsymentry);
  96. function search(const s : stringid) : tsymentry;
  97. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;virtual;
  98. procedure registerdef(p : tdefentry);
  99. {$ifdef EXTDEBUG}
  100. procedure dump;
  101. {$endif EXTDEBUG}
  102. function getdefnr(l : longint) : tdefentry;
  103. function getsymnr(l : longint) : tsymentry;
  104. {$ifdef GDB}
  105. function getnewtypecount : word; virtual;
  106. {$endif GDB}
  107. end;
  108. var
  109. registerdef : boolean; { true, when defs should be registered }
  110. defaultsymtablestack : tsymtable; { symtablestack after default units have been loaded }
  111. symtablestack : tsymtable; { linked list of symtables }
  112. aktrecordsymtable : tsymtable; { current record symtable }
  113. aktparasymtable : tsymtable; { current proc para symtable }
  114. aktlocalsymtable : tsymtable; { current proc local symtable }
  115. implementation
  116. uses
  117. verbose;
  118. {****************************************************************************
  119. TSYMTABLE
  120. ****************************************************************************}
  121. constructor tsymtable.Create(const s:string);
  122. begin
  123. if s<>'' then
  124. begin
  125. name:=stringdup(upper(s));
  126. realname:=stringdup(s);
  127. end
  128. else
  129. begin
  130. name:=nil;
  131. realname:=nil;
  132. end;
  133. symtabletype:=abstractsymtable;
  134. symtablelevel:=0;
  135. defowner:=nil;
  136. next:=nil;
  137. symindex:=tindexarray.create(indexgrowsize);
  138. defindex:=TIndexArray.create(indexgrowsize);
  139. symsearch:=tdictionary.create;
  140. symsearch.noclear:=true;
  141. unitid:=0;
  142. refcount:=1;
  143. end;
  144. destructor tsymtable.destroy;
  145. begin
  146. { freeinstance decreases refcount }
  147. if refcount>1 then
  148. exit;
  149. stringdispose(name);
  150. stringdispose(realname);
  151. symindex.destroy;
  152. defindex.destroy;
  153. { symsearch can already be disposed or set to nil for withsymtable }
  154. if assigned(symsearch) then
  155. begin
  156. symsearch.destroy;
  157. symsearch:=nil;
  158. end;
  159. end;
  160. procedure tsymtable.freeinstance;
  161. begin
  162. dec(refcount);
  163. if refcount=0 then
  164. inherited freeinstance;
  165. end;
  166. function tsymtable.getcopy:tsymtable;
  167. begin
  168. inc(refcount);
  169. result:=self;
  170. end;
  171. {$ifdef EXTDEBUG}
  172. procedure tsymtable.dumpsym(p : TNamedIndexItem;arg:pointer);
  173. begin
  174. writeln(p.name);
  175. end;
  176. procedure tsymtable.dump;
  177. begin
  178. if assigned(name) then
  179. writeln('Symtable ',name^)
  180. else
  181. writeln('Symtable <not named>');
  182. symsearch.foreach({$ifdef FPCPROCVAR}@{$endif}dumpsym,nil);
  183. end;
  184. {$endif EXTDEBUG}
  185. procedure tsymtable.registerdef(p : tdefentry);
  186. begin
  187. defindex.insert(p);
  188. { set def owner and indexnb }
  189. p.owner:=self;
  190. end;
  191. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  192. begin
  193. symindex.foreach(proc2call,arg);
  194. end;
  195. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  196. begin
  197. symindex.foreach_static(proc2call,arg);
  198. end;
  199. {***********************************************
  200. Table Access
  201. ***********************************************}
  202. procedure tsymtable.clear;
  203. begin
  204. symindex.clear;
  205. defindex.clear;
  206. end;
  207. procedure tsymtable.insert(sym:tsymentry);
  208. begin
  209. sym.owner:=self;
  210. { insert in index and search hash }
  211. symindex.insert(sym);
  212. symsearch.insert(sym);
  213. end;
  214. procedure tsymtable.replace(oldsym,newsym:tsymentry);
  215. begin
  216. { Replace the entry in the dictionary, this checks
  217. the name }
  218. if not symsearch.replace(oldsym,newsym) then
  219. internalerror(200209061);
  220. { replace in index }
  221. symindex.replace(oldsym,newsym);
  222. { set owner of new symb }
  223. newsym.owner:=self;
  224. end;
  225. function tsymtable.search(const s : stringid) : tsymentry;
  226. begin
  227. search:=speedsearch(s,getspeedvalue(s));
  228. end;
  229. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  230. begin
  231. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  232. end;
  233. function tsymtable.rename(const olds,news : stringid):tsymentry;
  234. begin
  235. rename:=tsymentry(symsearch.rename(olds,news));
  236. end;
  237. function tsymtable.getsymnr(l : longint) : tsymentry;
  238. var
  239. hp : tsymentry;
  240. begin
  241. hp:=tsymentry(symindex.search(l));
  242. if hp=nil then
  243. internalerror(10999);
  244. getsymnr:=hp;
  245. end;
  246. function tsymtable.getdefnr(l : longint) : tdefentry;
  247. var
  248. hp : tdefentry;
  249. begin
  250. hp:=tdefentry(defindex.search(l));
  251. if hp=nil then
  252. internalerror(10998);
  253. getdefnr:=hp;
  254. end;
  255. {$ifdef GDB}
  256. function tsymtable.getnewtypecount : word;
  257. begin
  258. getnewtypecount:=0;
  259. end;
  260. {$endif GDB}
  261. end.
  262. {
  263. $Log$
  264. Revision 1.22 2004-07-09 22:17:32 peter
  265. * revert has_localst patch
  266. * replace aktstaticsymtable/aktglobalsymtable with current_module
  267. Revision 1.21 2004/06/20 08:55:30 florian
  268. * logs truncated
  269. Revision 1.20 2004/03/02 17:32:12 florian
  270. * make cycle fixed
  271. + pic support for darwin
  272. + support of importing vars from shared libs on darwin implemented
  273. Revision 1.19 2004/02/04 22:15:15 daniel
  274. * Rtti generation moved to ncgutil
  275. * Assmtai usage of symsym removed
  276. * operator overloading cleanup up
  277. Revision 1.18 2004/01/15 15:16:18 daniel
  278. * Some minor stuff
  279. * Managed to eliminate speed effects of string compression
  280. Revision 1.17 2004/01/11 23:56:20 daniel
  281. * Experiment: Compress strings to save memory
  282. Did not save a single byte of mem; clearly the core size is boosted by
  283. temporary memory usage...
  284. }