symbase.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. aktstaticsymtable : tsymtable; { current static symtable }
  114. aktglobalsymtable : tsymtable; { current global symtable }
  115. aktparasymtable : tsymtable; { current proc para symtable }
  116. aktlocalsymtable : tsymtable; { current proc local symtable }
  117. implementation
  118. uses
  119. verbose;
  120. {****************************************************************************
  121. TSYMTABLE
  122. ****************************************************************************}
  123. constructor tsymtable.Create(const s:string);
  124. begin
  125. if s<>'' then
  126. begin
  127. name:=stringdup(upper(s));
  128. realname:=stringdup(s);
  129. end
  130. else
  131. begin
  132. name:=nil;
  133. realname:=nil;
  134. end;
  135. symtabletype:=abstractsymtable;
  136. symtablelevel:=0;
  137. defowner:=nil;
  138. next:=nil;
  139. symindex:=tindexarray.create(indexgrowsize);
  140. defindex:=TIndexArray.create(indexgrowsize);
  141. symsearch:=tdictionary.create;
  142. symsearch.noclear:=true;
  143. unitid:=0;
  144. refcount:=1;
  145. end;
  146. destructor tsymtable.destroy;
  147. begin
  148. { freeinstance decreases refcount }
  149. if refcount>1 then
  150. exit;
  151. stringdispose(name);
  152. stringdispose(realname);
  153. symindex.destroy;
  154. defindex.destroy;
  155. { symsearch can already be disposed or set to nil for withsymtable }
  156. if assigned(symsearch) then
  157. begin
  158. symsearch.destroy;
  159. symsearch:=nil;
  160. end;
  161. end;
  162. procedure tsymtable.freeinstance;
  163. begin
  164. dec(refcount);
  165. if refcount=0 then
  166. inherited freeinstance;
  167. end;
  168. function tsymtable.getcopy:tsymtable;
  169. begin
  170. inc(refcount);
  171. result:=self;
  172. end;
  173. {$ifdef EXTDEBUG}
  174. procedure tsymtable.dumpsym(p : TNamedIndexItem;arg:pointer);
  175. begin
  176. writeln(p.name);
  177. end;
  178. procedure tsymtable.dump;
  179. begin
  180. if assigned(name) then
  181. writeln('Symtable ',name^)
  182. else
  183. writeln('Symtable <not named>');
  184. symsearch.foreach({$ifdef FPCPROCVAR}@{$endif}dumpsym,nil);
  185. end;
  186. {$endif EXTDEBUG}
  187. procedure tsymtable.registerdef(p : tdefentry);
  188. begin
  189. defindex.insert(p);
  190. { set def owner and indexnb }
  191. p.owner:=self;
  192. end;
  193. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  194. begin
  195. symindex.foreach(proc2call,arg);
  196. end;
  197. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  198. begin
  199. symindex.foreach_static(proc2call,arg);
  200. end;
  201. {***********************************************
  202. Table Access
  203. ***********************************************}
  204. procedure tsymtable.clear;
  205. begin
  206. symindex.clear;
  207. defindex.clear;
  208. end;
  209. procedure tsymtable.insert(sym:tsymentry);
  210. begin
  211. sym.owner:=self;
  212. { insert in index and search hash }
  213. symindex.insert(sym);
  214. symsearch.insert(sym);
  215. end;
  216. procedure tsymtable.replace(oldsym,newsym:tsymentry);
  217. begin
  218. { Replace the entry in the dictionary, this checks
  219. the name }
  220. if not symsearch.replace(oldsym,newsym) then
  221. internalerror(200209061);
  222. { replace in index }
  223. symindex.replace(oldsym,newsym);
  224. { set owner of new symb }
  225. newsym.owner:=self;
  226. end;
  227. function tsymtable.search(const s : stringid) : tsymentry;
  228. begin
  229. search:=speedsearch(s,getspeedvalue(s));
  230. end;
  231. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  232. begin
  233. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  234. end;
  235. function tsymtable.rename(const olds,news : stringid):tsymentry;
  236. begin
  237. rename:=tsymentry(symsearch.rename(olds,news));
  238. end;
  239. function tsymtable.getsymnr(l : longint) : tsymentry;
  240. var
  241. hp : tsymentry;
  242. begin
  243. hp:=tsymentry(symindex.search(l));
  244. if hp=nil then
  245. internalerror(10999);
  246. getsymnr:=hp;
  247. end;
  248. function tsymtable.getdefnr(l : longint) : tdefentry;
  249. var
  250. hp : tdefentry;
  251. begin
  252. hp:=tdefentry(defindex.search(l));
  253. if hp=nil then
  254. internalerror(10998);
  255. getdefnr:=hp;
  256. end;
  257. {$ifdef GDB}
  258. function tsymtable.getnewtypecount : word;
  259. begin
  260. getnewtypecount:=0;
  261. end;
  262. {$endif GDB}
  263. end.
  264. {
  265. $Log$
  266. Revision 1.21 2004-06-20 08:55:30 florian
  267. * logs truncated
  268. Revision 1.20 2004/03/02 17:32:12 florian
  269. * make cycle fixed
  270. + pic support for darwin
  271. + support of importing vars from shared libs on darwin implemented
  272. Revision 1.19 2004/02/04 22:15:15 daniel
  273. * Rtti generation moved to ncgutil
  274. * Assmtai usage of symsym removed
  275. * operator overloading cleanup up
  276. Revision 1.18 2004/01/15 15:16:18 daniel
  277. * Some minor stuff
  278. * Managed to eliminate speed effects of string compression
  279. Revision 1.17 2004/01/11 23:56:20 daniel
  280. * Experiment: Compress strings to save memory
  281. Did not save a single byte of mem; clearly the core size is boosted by
  282. temporary memory usage...
  283. }