symbase.pas 11 KB

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