symbase.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. { level of symtable, used for nested procedures }
  82. symtablelevel : byte;
  83. moduleid : longint;
  84. refcount : integer;
  85. constructor Create(const s:string);
  86. destructor destroy;override;
  87. procedure freeinstance;override;
  88. function getcopy:tsymtable;
  89. procedure clear;virtual;
  90. function rename(const olds,news : stringid):tsymentry;
  91. procedure foreach(proc2call : tnamedindexcallback;arg:pointer);
  92. procedure foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  93. procedure insert(sym : tsymentry);virtual;
  94. { deletes a tsymentry and removes it from the tsymtable}
  95. procedure delete(sym:tsymentry);
  96. procedure replace(oldsym,newsym:tsymentry);
  97. function search(const s : stringid) : tsymentry;
  98. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;virtual;
  99. procedure registerdef(p : tdefentry);
  100. function iscurrentunit:boolean;virtual;
  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. refcount:=1;
  149. end;
  150. destructor tsymtable.destroy;
  151. begin
  152. { freeinstance decreases refcount }
  153. if refcount>1 then
  154. exit;
  155. stringdispose(name);
  156. stringdispose(realname);
  157. symindex.destroy;
  158. defindex.destroy;
  159. { symsearch can already be disposed or set to nil for withsymtable }
  160. if assigned(symsearch) then
  161. begin
  162. symsearch.destroy;
  163. symsearch:=nil;
  164. end;
  165. end;
  166. procedure tsymtable.freeinstance;
  167. begin
  168. dec(refcount);
  169. if refcount=0 then
  170. inherited freeinstance;
  171. end;
  172. function tsymtable.getcopy:tsymtable;
  173. begin
  174. inc(refcount);
  175. result:=self;
  176. end;
  177. {$ifdef EXTDEBUG}
  178. procedure tsymtable.dumpsym(p : TNamedIndexItem;arg:pointer);
  179. begin
  180. writeln(p.name);
  181. end;
  182. procedure tsymtable.dump;
  183. begin
  184. if assigned(name) then
  185. writeln('Symtable ',name^)
  186. else
  187. writeln('Symtable <not named>');
  188. symsearch.foreach(@dumpsym,nil);
  189. end;
  190. {$endif EXTDEBUG}
  191. procedure tsymtable.registerdef(p : tdefentry);
  192. begin
  193. defindex.insert(p);
  194. { set def owner and indexnb }
  195. p.owner:=self;
  196. end;
  197. function tsymtable.iscurrentunit:boolean;
  198. begin
  199. result:=false;
  200. end;
  201. procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
  202. begin
  203. symindex.foreach(proc2call,arg);
  204. end;
  205. procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
  206. begin
  207. symindex.foreach_static(proc2call,arg);
  208. end;
  209. {***********************************************
  210. Table Access
  211. ***********************************************}
  212. procedure tsymtable.clear;
  213. begin
  214. symindex.clear;
  215. defindex.clear;
  216. end;
  217. procedure tsymtable.insert(sym:tsymentry);
  218. begin
  219. sym.owner:=self;
  220. { insert in index and search hash }
  221. symindex.insert(sym);
  222. symsearch.insert(sym);
  223. end;
  224. procedure tsymtable.delete(sym:tsymentry);
  225. begin
  226. sym.owner:=nil;
  227. { remove from index and search hash }
  228. symsearch.delete(sym.name);
  229. symindex.delete(sym);
  230. end;
  231. procedure tsymtable.replace(oldsym,newsym:tsymentry);
  232. begin
  233. { Replace the entry in the dictionary, this checks
  234. the name }
  235. if not symsearch.replace(oldsym,newsym) then
  236. internalerror(200209061);
  237. { replace in index }
  238. symindex.replace(oldsym,newsym);
  239. { set owner of new symb }
  240. newsym.owner:=self;
  241. end;
  242. function tsymtable.search(const s : stringid) : tsymentry;
  243. begin
  244. search:=speedsearch(s,getspeedvalue(s));
  245. end;
  246. function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  247. begin
  248. speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
  249. end;
  250. function tsymtable.rename(const olds,news : stringid):tsymentry;
  251. begin
  252. rename:=tsymentry(symsearch.rename(olds,news));
  253. end;
  254. function tsymtable.getsymnr(l : longint) : tsymentry;
  255. var
  256. hp : tsymentry;
  257. begin
  258. hp:=tsymentry(symindex.search(l));
  259. if hp=nil then
  260. internalerror(10999);
  261. getsymnr:=hp;
  262. end;
  263. function tsymtable.getdefnr(l : longint) : tdefentry;
  264. var
  265. hp : tdefentry;
  266. begin
  267. hp:=tdefentry(defindex.search(l));
  268. if hp=nil then
  269. internalerror(10998);
  270. getdefnr:=hp;
  271. end;
  272. {$ifdef GDB}
  273. function tsymtable.getnewtypecount : word;
  274. begin
  275. getnewtypecount:=0;
  276. end;
  277. {$endif GDB}
  278. end.
  279. {
  280. $Log$
  281. Revision 1.25 2005-01-19 22:19:41 peter
  282. * unit mapping rewrite
  283. * new derefmap added
  284. Revision 1.24 2005/01/09 20:24:43 olle
  285. * rework of macro subsystem
  286. + exportable macros for mode macpas
  287. Revision 1.23 2004/10/15 09:14:17 mazen
  288. - remove $IFDEF DELPHI and related code
  289. - remove $IFDEF FPCPROCVAR and related code
  290. Revision 1.22 2004/07/09 22:17:32 peter
  291. * revert has_localst patch
  292. * replace aktstaticsymtable/aktglobalsymtable with current_module
  293. Revision 1.21 2004/06/20 08:55:30 florian
  294. * logs truncated
  295. Revision 1.20 2004/03/02 17:32:12 florian
  296. * make cycle fixed
  297. + pic support for darwin
  298. + support of importing vars from shared libs on darwin implemented
  299. Revision 1.19 2004/02/04 22:15:15 daniel
  300. * Rtti generation moved to ncgutil
  301. * Assmtai usage of symsym removed
  302. * operator overloading cleanup up
  303. Revision 1.18 2004/01/15 15:16:18 daniel
  304. * Some minor stuff
  305. * Managed to eliminate speed effects of string compression
  306. Revision 1.17 2004/01/11 23:56:20 daniel
  307. * Experiment: Compress strings to save memory
  308. Did not save a single byte of mem; clearly the core size is boosted by
  309. temporary memory usage...
  310. }