symbase.pas 9.4 KB

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