symbase.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. stringdispose(name);
  151. stringdispose(realname);
  152. end;
  153. procedure tsymtable.freeinstance;
  154. begin
  155. dec(refcount);
  156. if refcount=0 then
  157. inherited freeinstance;
  158. end;
  159. function tsymtable.getcopy:tsymtable;
  160. begin
  161. inc(refcount);
  162. result:=self;
  163. end;
  164. {$ifdef EXTDEBUG}
  165. procedure tsymtable.dumpsym(p : TNamedIndexItem;arg:pointer);
  166. begin
  167. writeln(p.name);
  168. end;
  169. procedure tsymtable.dump;
  170. begin
  171. if assigned(name) then
  172. writeln('Symtable ',name^)
  173. else
  174. writeln('Symtable <not named>');
  175. symsearch.foreach(@dumpsym,nil);
  176. end;
  177. {$endif EXTDEBUG}
  178. function tsymtable.iscurrentunit:boolean;
  179. begin
  180. result:=false;
  181. end;
  182. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  183. begin
  184. symindex.foreach(proc2call,arg);
  185. end;
  186. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  187. begin
  188. symindex.foreach_static(proc2call,arg);
  189. end;
  190. {***********************************************
  191. Table Access
  192. ***********************************************}
  193. procedure tsymtable.clear;
  194. begin
  195. clearing:=true;
  196. symindex.clear;
  197. defindex.clear;
  198. clearing:=false;
  199. end;
  200. function tsymtable.checkduplicate(sym : tsymentry):boolean;
  201. begin
  202. result:=(speedsearch(sym.name,sym.speedvalue)<>nil);
  203. end;
  204. procedure tsymtable.insert(sym:tsymentry);
  205. begin
  206. checkduplicate(sym);
  207. sym.owner:=self;
  208. { insert in index and search hash }
  209. symindex.insert(sym);
  210. symsearch.insert(sym);
  211. end;
  212. procedure tsymtable.insertdef(def:tdefentry);
  213. begin
  214. def.owner:=self;
  215. defindex.insert(def);
  216. end;
  217. procedure tsymtable.deletedef(def:tdefentry);
  218. begin
  219. { if we are already clearing everything it will already
  220. be deleted }
  221. if clearing then
  222. exit;
  223. defindex.deleteindex(def);
  224. def.owner:=nil;
  225. end;
  226. procedure tsymtable.delete(sym:tsymentry);
  227. begin
  228. sym.owner:=nil;
  229. { remove from index and search hash }
  230. symsearch.delete(sym.name);
  231. symindex.delete(sym);
  232. end;
  233. procedure tsymtable.replace(oldsym,newsym:tsymentry);
  234. begin
  235. { Replace the entry in the dictionary, this checks
  236. the name }
  237. if not symsearch.replace(oldsym,newsym) then
  238. internalerror(200209061);
  239. { replace in index }
  240. symindex.replace(oldsym,newsym);
  241. { set owner of new symb }
  242. newsym.owner:=self;
  243. end;
  244. function tsymtable.search(const s : stringid) : tsymentry;
  245. begin
  246. search:=speedsearch(s,getspeedvalue(s));
  247. end;
  248. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  249. begin
  250. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  251. end;
  252. function tsymtable.rename(const olds,news : stringid):tsymentry;
  253. begin
  254. rename:=tsymentry(symsearch.rename(olds,news));
  255. end;
  256. function tsymtable.getsymnr(l : longint) : tsymentry;
  257. var
  258. hp : tsymentry;
  259. begin
  260. hp:=tsymentry(symindex.search(l));
  261. if hp=nil then
  262. internalerror(10999);
  263. getsymnr:=hp;
  264. end;
  265. function tsymtable.getdefnr(l : longint) : tdefentry;
  266. var
  267. hp : tdefentry;
  268. begin
  269. hp:=tdefentry(defindex.search(l));
  270. if hp=nil then
  271. internalerror(10998);
  272. getdefnr:=hp;
  273. end;
  274. end.