symbase.pas 8.9 KB

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