symbase.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. {************************************************
  35. Needed forward pointers
  36. ************************************************}
  37. type
  38. tsymtable = class;
  39. {************************************************
  40. TSymtableEntry
  41. ************************************************}
  42. tsymtableentry = class(TNamedIndexItem)
  43. owner : tsymtable;
  44. end;
  45. {************************************************
  46. TDefEntry
  47. ************************************************}
  48. tdefentry = class(tsymtableentry)
  49. deftype : tdeftype;
  50. end;
  51. {************************************************
  52. TSymEntry
  53. ************************************************}
  54. { this object is the base for all symbol objects }
  55. tsymentry = class(tsymtableentry)
  56. typ : tsymtyp;
  57. end;
  58. {************************************************
  59. TSymtable
  60. ************************************************}
  61. tsearchhasharray = array[0..hasharraysize-1] of tsymentry;
  62. psearchhasharray = ^tsearchhasharray;
  63. tsymtable = class
  64. private
  65. clearing : boolean;
  66. {$ifdef EXTDEBUG}
  67. procedure dumpsym(p : TNamedIndexItem;arg:pointer);
  68. {$endif EXTDEBUG}
  69. public
  70. name : pstring;
  71. realname : pstring;
  72. symindex,
  73. defindex : TIndexArray;
  74. symsearch : Tdictionary;
  75. defowner : tdefentry; { for records and objects }
  76. symtabletype : tsymtabletype;
  77. { level of symtable, used for nested procedures }
  78. symtablelevel : byte;
  79. moduleid : longint;
  80. refcount : integer;
  81. constructor Create(const s:string);
  82. destructor destroy;override;
  83. procedure freeinstance;override;
  84. function getcopy:tsymtable;
  85. procedure clear;virtual;
  86. function rename(const olds,news : stringid):tsymentry;
  87. procedure foreach(proc2call : tnamedindexcallback;arg:pointer);
  88. procedure foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  89. function checkduplicate(sym : tsymentry):boolean;virtual;
  90. procedure insert(sym : tsymentry);virtual;
  91. procedure delete(sym:tsymentry);
  92. procedure replace(oldsym,newsym:tsymentry);
  93. function search(const s : stringid) : tsymentry;
  94. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;virtual;
  95. procedure insertdef(def:tdefentry);virtual;
  96. procedure deletedef(def:tdefentry);virtual;
  97. function iscurrentunit:boolean;virtual;
  98. {$ifdef EXTDEBUG}
  99. procedure dump;
  100. {$endif EXTDEBUG}
  101. function getdefnr(l : longint) : tdefentry;
  102. function getsymnr(l : longint) : tsymentry;
  103. end;
  104. var
  105. aktrecordsymtable : tsymtable; { current record symtable }
  106. aktparasymtable : tsymtable; { current proc para symtable }
  107. aktlocalsymtable : tsymtable; { current proc local symtable }
  108. initialmacrosymtable: tsymtable; { macros initially defined by the compiler or
  109. given on the command line. Is common
  110. for all files compiled and do not change. }
  111. implementation
  112. uses
  113. verbose;
  114. {****************************************************************************
  115. TSYMTABLE
  116. ****************************************************************************}
  117. constructor tsymtable.Create(const s:string);
  118. begin
  119. if s<>'' then
  120. begin
  121. name:=stringdup(upper(s));
  122. realname:=stringdup(s);
  123. end
  124. else
  125. begin
  126. name:=nil;
  127. realname:=nil;
  128. end;
  129. symtabletype:=abstractsymtable;
  130. symtablelevel:=0;
  131. defowner:=nil;
  132. symindex:=tindexarray.create(indexgrowsize);
  133. defindex:=TIndexArray.create(indexgrowsize);
  134. symsearch:=tdictionary.create;
  135. symsearch.noclear:=true;
  136. refcount:=1;
  137. end;
  138. destructor tsymtable.destroy;
  139. begin
  140. { freeinstance decreases refcount }
  141. if refcount>1 then
  142. exit;
  143. { symsearch can already be disposed or set to nil for withsymtable }
  144. if assigned(symsearch) then
  145. begin
  146. symsearch.destroy;
  147. symsearch:=nil;
  148. end;
  149. clear;
  150. symindex.free;
  151. defindex.free;
  152. stringdispose(name);
  153. stringdispose(realname);
  154. end;
  155. procedure tsymtable.freeinstance;
  156. begin
  157. dec(refcount);
  158. if refcount=0 then
  159. inherited freeinstance;
  160. end;
  161. function tsymtable.getcopy:tsymtable;
  162. begin
  163. inc(refcount);
  164. result:=self;
  165. end;
  166. {$ifdef EXTDEBUG}
  167. procedure tsymtable.dumpsym(p : TNamedIndexItem;arg:pointer);
  168. begin
  169. writeln(p.name);
  170. end;
  171. procedure tsymtable.dump;
  172. begin
  173. if assigned(name) then
  174. writeln('Symtable ',name^)
  175. else
  176. writeln('Symtable <not named>');
  177. symsearch.foreach(@dumpsym,nil);
  178. end;
  179. {$endif EXTDEBUG}
  180. function tsymtable.iscurrentunit:boolean;
  181. begin
  182. result:=false;
  183. end;
  184. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  185. begin
  186. symindex.foreach(proc2call,arg);
  187. end;
  188. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  189. begin
  190. symindex.foreach_static(proc2call,arg);
  191. end;
  192. {***********************************************
  193. Table Access
  194. ***********************************************}
  195. procedure tsymtable.clear;
  196. begin
  197. clearing:=true;
  198. symindex.clear;
  199. defindex.clear;
  200. clearing:=false;
  201. end;
  202. function tsymtable.checkduplicate(sym : tsymentry):boolean;
  203. begin
  204. result:=(speedsearch(sym.name,sym.speedvalue)<>nil);
  205. end;
  206. procedure tsymtable.insert(sym:tsymentry);
  207. begin
  208. checkduplicate(sym);
  209. sym.owner:=self;
  210. { insert in index and search hash }
  211. symindex.insert(sym);
  212. symsearch.insert(sym);
  213. end;
  214. procedure tsymtable.insertdef(def:tdefentry);
  215. begin
  216. def.owner:=self;
  217. defindex.insert(def);
  218. end;
  219. procedure tsymtable.deletedef(def:tdefentry);
  220. begin
  221. { if we are already clearing everything it will already
  222. be deleted }
  223. if clearing then
  224. exit;
  225. defindex.deleteindex(def);
  226. def.owner:=nil;
  227. end;
  228. procedure tsymtable.delete(sym:tsymentry);
  229. begin
  230. sym.owner:=nil;
  231. { remove from index and search hash }
  232. symsearch.delete(sym.name);
  233. symindex.delete(sym);
  234. end;
  235. procedure tsymtable.replace(oldsym,newsym:tsymentry);
  236. begin
  237. { Replace the entry in the dictionary, this checks
  238. the name }
  239. if not symsearch.replace(oldsym,newsym) then
  240. internalerror(200209061);
  241. { replace in index }
  242. symindex.replace(oldsym,newsym);
  243. { set owner of new symb }
  244. newsym.owner:=self;
  245. end;
  246. function tsymtable.search(const s : stringid) : tsymentry;
  247. begin
  248. search:=speedsearch(s,getspeedvalue(s));
  249. end;
  250. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  251. begin
  252. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  253. end;
  254. function tsymtable.rename(const olds,news : stringid):tsymentry;
  255. begin
  256. rename:=tsymentry(symsearch.rename(olds,news));
  257. end;
  258. function tsymtable.getsymnr(l : longint) : tsymentry;
  259. var
  260. hp : tsymentry;
  261. begin
  262. hp:=tsymentry(symindex.search(l));
  263. if hp=nil then
  264. internalerror(10999);
  265. getsymnr:=hp;
  266. end;
  267. function tsymtable.getdefnr(l : longint) : tdefentry;
  268. var
  269. hp : tdefentry;
  270. begin
  271. hp:=tdefentry(defindex.search(l));
  272. if hp=nil then
  273. internalerror(10998);
  274. getdefnr:=hp;
  275. end;
  276. end.