symbase.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symbase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,
  23. { global }
  24. globtype,globals,
  25. { symtable }
  26. symconst
  27. ;
  28. {************************************************
  29. Some internal constants
  30. ************************************************}
  31. const
  32. hasharraysize = 256;
  33. indexgrowsize = 64;
  34. {$ifdef GDB}
  35. memsizeinc = 4096; { for long stabstrings }
  36. {$endif GDB}
  37. {************************************************
  38. Needed forward pointers
  39. ************************************************}
  40. type
  41. tsymtable = class;
  42. {************************************************
  43. TSymtableEntry
  44. ************************************************}
  45. tsymtableentry = class(TNamedIndexItem)
  46. owner : tsymtable;
  47. end;
  48. {************************************************
  49. TDefEntry
  50. ************************************************}
  51. tdefentry = class(tsymtableentry)
  52. deftype : tdeftype;
  53. end;
  54. {************************************************
  55. TSymEntry
  56. ************************************************}
  57. { this object is the base for all symbol objects }
  58. tsymentry = class(tsymtableentry)
  59. typ : tsymtyp;
  60. end;
  61. {************************************************
  62. TSymtable
  63. ************************************************}
  64. tsearchhasharray = array[0..hasharraysize-1] of tsymentry;
  65. psearchhasharray = ^tsearchhasharray;
  66. tsymtable = class
  67. {$ifdef EXTDEBUG}
  68. private
  69. procedure dumpsym(p : TNamedIndexItem;arg:pointer);
  70. {$endif EXTDEBUG}
  71. public
  72. name : pstring;
  73. realname : pstring;
  74. symindex,
  75. defindex : TIndexArray;
  76. symsearch : Tdictionary;
  77. next : tsymtable;
  78. defowner : tdefentry; { for records and objects }
  79. symtabletype : tsymtabletype;
  80. { level of symtable, used for nested procedures }
  81. symtablelevel : byte;
  82. moduleid : longint;
  83. refcount : integer;
  84. constructor Create(const s:string);
  85. destructor destroy;override;
  86. procedure freeinstance;override;
  87. function getcopy:tsymtable;
  88. procedure clear;virtual;
  89. function rename(const olds,news : stringid):tsymentry;
  90. procedure foreach(proc2call : tnamedindexcallback;arg:pointer);
  91. procedure foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  92. procedure insert(sym : tsymentry);virtual;
  93. { deletes a tsymentry and removes it from the tsymtable}
  94. procedure delete(sym:tsymentry);
  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. function iscurrentunit:boolean;virtual;
  100. {$ifdef EXTDEBUG}
  101. procedure dump;
  102. {$endif EXTDEBUG}
  103. function getdefnr(l : longint) : tdefentry;
  104. function getsymnr(l : longint) : tsymentry;
  105. {$ifdef GDB}
  106. function getnewtypecount : word; virtual;
  107. {$endif GDB}
  108. end;
  109. var
  110. registerdef : boolean; { true, when defs should be registered }
  111. defaultsymtablestack : tsymtable; { symtablestack after default units have been loaded }
  112. symtablestack : tsymtable; { linked list of symtables }
  113. defaultmacrosymtablestack : tsymtable;{ macrosymtablestack after default units have been loaded }
  114. macrosymtablestack: tsymtable; { linked list of macro symtables }
  115. aktrecordsymtable : tsymtable; { current record symtable }
  116. aktparasymtable : tsymtable; { current proc para symtable }
  117. aktlocalsymtable : tsymtable; { current proc local symtable }
  118. initialmacrosymtable: tsymtable; { macros initially defined by the compiler or
  119. given on the command line. Is common
  120. for all files compiled and do not change. }
  121. implementation
  122. uses
  123. verbose;
  124. {****************************************************************************
  125. TSYMTABLE
  126. ****************************************************************************}
  127. constructor tsymtable.Create(const s:string);
  128. begin
  129. if s<>'' then
  130. begin
  131. name:=stringdup(upper(s));
  132. realname:=stringdup(s);
  133. end
  134. else
  135. begin
  136. name:=nil;
  137. realname:=nil;
  138. end;
  139. symtabletype:=abstractsymtable;
  140. symtablelevel:=0;
  141. defowner:=nil;
  142. next:=nil;
  143. symindex:=tindexarray.create(indexgrowsize);
  144. defindex:=TIndexArray.create(indexgrowsize);
  145. symsearch:=tdictionary.create;
  146. symsearch.noclear:=true;
  147. refcount:=1;
  148. end;
  149. destructor tsymtable.destroy;
  150. begin
  151. { freeinstance decreases refcount }
  152. if refcount>1 then
  153. exit;
  154. stringdispose(name);
  155. stringdispose(realname);
  156. symindex.destroy;
  157. defindex.destroy;
  158. { symsearch can already be disposed or set to nil for withsymtable }
  159. if assigned(symsearch) then
  160. begin
  161. symsearch.destroy;
  162. symsearch:=nil;
  163. end;
  164. end;
  165. procedure tsymtable.freeinstance;
  166. begin
  167. dec(refcount);
  168. if refcount=0 then
  169. inherited freeinstance;
  170. end;
  171. function tsymtable.getcopy:tsymtable;
  172. begin
  173. inc(refcount);
  174. result:=self;
  175. end;
  176. {$ifdef EXTDEBUG}
  177. procedure tsymtable.dumpsym(p : TNamedIndexItem;arg:pointer);
  178. begin
  179. writeln(p.name);
  180. end;
  181. procedure tsymtable.dump;
  182. begin
  183. if assigned(name) then
  184. writeln('Symtable ',name^)
  185. else
  186. writeln('Symtable <not named>');
  187. symsearch.foreach(@dumpsym,nil);
  188. end;
  189. {$endif EXTDEBUG}
  190. procedure tsymtable.registerdef(p : tdefentry);
  191. begin
  192. defindex.insert(p);
  193. { set def owner and indexnb }
  194. p.owner:=self;
  195. end;
  196. function tsymtable.iscurrentunit:boolean;
  197. begin
  198. result:=false;
  199. end;
  200. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  201. begin
  202. symindex.foreach(proc2call,arg);
  203. end;
  204. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  205. begin
  206. symindex.foreach_static(proc2call,arg);
  207. end;
  208. {***********************************************
  209. Table Access
  210. ***********************************************}
  211. procedure tsymtable.clear;
  212. begin
  213. symindex.clear;
  214. defindex.clear;
  215. end;
  216. procedure tsymtable.insert(sym:tsymentry);
  217. begin
  218. sym.owner:=self;
  219. { insert in index and search hash }
  220. symindex.insert(sym);
  221. symsearch.insert(sym);
  222. end;
  223. procedure tsymtable.delete(sym:tsymentry);
  224. begin
  225. sym.owner:=nil;
  226. { remove from index and search hash }
  227. symsearch.delete(sym.name);
  228. symindex.delete(sym);
  229. end;
  230. procedure tsymtable.replace(oldsym,newsym:tsymentry);
  231. begin
  232. { Replace the entry in the dictionary, this checks
  233. the name }
  234. if not symsearch.replace(oldsym,newsym) then
  235. internalerror(200209061);
  236. { replace in index }
  237. symindex.replace(oldsym,newsym);
  238. { set owner of new symb }
  239. newsym.owner:=self;
  240. end;
  241. function tsymtable.search(const s : stringid) : tsymentry;
  242. begin
  243. search:=speedsearch(s,getspeedvalue(s));
  244. end;
  245. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  246. begin
  247. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  248. end;
  249. function tsymtable.rename(const olds,news : stringid):tsymentry;
  250. begin
  251. rename:=tsymentry(symsearch.rename(olds,news));
  252. end;
  253. function tsymtable.getsymnr(l : longint) : tsymentry;
  254. var
  255. hp : tsymentry;
  256. begin
  257. hp:=tsymentry(symindex.search(l));
  258. if hp=nil then
  259. internalerror(10999);
  260. getsymnr:=hp;
  261. end;
  262. function tsymtable.getdefnr(l : longint) : tdefentry;
  263. var
  264. hp : tdefentry;
  265. begin
  266. hp:=tdefentry(defindex.search(l));
  267. if hp=nil then
  268. internalerror(10998);
  269. getdefnr:=hp;
  270. end;
  271. {$ifdef GDB}
  272. function tsymtable.getnewtypecount : word;
  273. begin
  274. getnewtypecount:=0;
  275. end;
  276. {$endif GDB}
  277. end.