symbase.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. Needed forward pointers
  30. ************************************************}
  31. type
  32. TSymtable = class;
  33. { THashedIDString }
  34. THashedIDString=object
  35. private
  36. FId : TIDString;
  37. FHash : Longword;
  38. procedure SetId(const s:TIDString);
  39. public
  40. property Id:TIDString read FId write SetId;
  41. property Hash:longword read FHash;
  42. end;
  43. {************************************************
  44. TDefEntry
  45. ************************************************}
  46. TDefEntry = class
  47. typ : tdeftyp;
  48. defid : longint;
  49. owner : TSymtable;
  50. end;
  51. {************************************************
  52. TSymEntry
  53. ************************************************}
  54. { this object is the base for all symbol objects }
  55. TSymEntry = class(TFPHashObject)
  56. private
  57. FRealName : pshortstring;
  58. function GetRealname:shortstring;
  59. procedure SetRealname(const ANewName:shortstring);
  60. public
  61. typ : tsymtyp;
  62. SymId : longint;
  63. Owner : TSymtable;
  64. destructor destroy;override;
  65. property RealName:shortstring read GetRealName write SetRealName;
  66. end;
  67. {************************************************
  68. TSymtable
  69. ************************************************}
  70. TSymtable = class
  71. public
  72. clearing : boolean;
  73. name : pshortstring;
  74. realname : pshortstring;
  75. DefList : TFPObjectList;
  76. SymList : TFPHashObjectList;
  77. defowner : TDefEntry; { for records and objects }
  78. moduleid : longint;
  79. refcount : smallint;
  80. { level of symtable, used for nested procedures }
  81. symtablelevel : byte;
  82. symtabletype : TSymtabletype;
  83. constructor Create(const s:string);
  84. destructor destroy;override;
  85. procedure freeinstance;override;
  86. function getcopy:TSymtable;
  87. procedure clear;virtual;
  88. function checkduplicate(var s:THashedIDString;sym:TSymEntry):boolean;virtual;
  89. procedure insert(sym:TSymEntry;checkdup:boolean=true);
  90. function Find(const s:TIDString) : TSymEntry;
  91. function FindWithHash(const s:THashedIDString) : TSymEntry;virtual;
  92. procedure insertdef(def:TDefEntry);virtual;
  93. function iscurrentunit:boolean;virtual;
  94. end;
  95. psymtablestackitem = ^TSymtablestackitem;
  96. TSymtablestackitem = record
  97. symtable : TSymtable;
  98. next : psymtablestackitem;
  99. end;
  100. TSymtablestack = class
  101. stack : psymtablestackitem;
  102. constructor create;
  103. destructor destroy;override;
  104. procedure clear;
  105. procedure push(st:TSymtable);
  106. procedure pop(st:TSymtable);
  107. function top:TSymtable;
  108. end;
  109. var
  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. macrosymtablestack,
  114. symtablestack : TSymtablestack;
  115. {$ifdef MEMDEBUG}
  116. var
  117. memrealnames : tmemdebug;
  118. {$endif MEMDEBUG}
  119. implementation
  120. uses
  121. verbose;
  122. {****************************************************************************
  123. THashedIDString
  124. ****************************************************************************}
  125. procedure THashedIDString.SetId(const s:TIDString);
  126. begin
  127. FId:=s;
  128. FHash:=FPHash(s);
  129. end;
  130. {****************************************************************************
  131. TSymEntry
  132. ****************************************************************************}
  133. destructor TSymEntry.destroy;
  134. begin
  135. {$ifdef MEMDEBUG}
  136. memrealnames.start;
  137. {$endif MEMDEBUG}
  138. stringdispose(Frealname);
  139. {$ifdef MEMDEBUG}
  140. memrealnames.stop;
  141. {$endif MEMDEBUG}
  142. inherited destroy;
  143. end;
  144. function TSymEntry.GetRealname:shortstring;
  145. begin
  146. if not assigned(FRealname) then
  147. internalerror(200611011);
  148. result:=FRealname^;
  149. end;
  150. procedure TSymEntry.SetRealname(const ANewName:shortstring);
  151. begin
  152. stringdispose(FRealname);
  153. FRealname:=stringdup(ANewName);
  154. if Hash<>$ffffffff then
  155. begin
  156. if FRealname^[1]='$' then
  157. Rename(Copy(FRealname^,2,255))
  158. else
  159. Rename(Upper(FRealname^));
  160. end;
  161. end;
  162. {****************************************************************************
  163. TSymtable
  164. ****************************************************************************}
  165. constructor TSymtable.Create(const s:string);
  166. begin
  167. if s<>'' then
  168. begin
  169. name:=stringdup(upper(s));
  170. realname:=stringdup(s);
  171. end
  172. else
  173. begin
  174. name:=nil;
  175. realname:=nil;
  176. end;
  177. symtabletype:=abstracTSymtable;
  178. symtablelevel:=0;
  179. defowner:=nil;
  180. DefList:=TFPObjectList.Create(true);
  181. SymList:=TFPHashObjectList.Create(true);
  182. refcount:=1;
  183. end;
  184. destructor TSymtable.destroy;
  185. begin
  186. { freeinstance decreases refcount }
  187. if refcount>1 then
  188. exit;
  189. Clear;
  190. DefList.Free;
  191. { SymList can already be disposed or set to nil for withsymtable }
  192. if assigned(SymList) then
  193. SymList.Free;
  194. stringdispose(name);
  195. stringdispose(realname);
  196. end;
  197. procedure TSymtable.freeinstance;
  198. begin
  199. dec(refcount);
  200. if refcount=0 then
  201. inherited freeinstance;
  202. end;
  203. function TSymtable.getcopy:TSymtable;
  204. begin
  205. inc(refcount);
  206. result:=self;
  207. end;
  208. function TSymtable.iscurrentunit:boolean;
  209. begin
  210. result:=false;
  211. end;
  212. procedure TSymtable.clear;
  213. begin
  214. clearing:=true;
  215. SymList.Clear;
  216. DefList.Clear;
  217. clearing:=false;
  218. end;
  219. function TSymtable.checkduplicate(var s:THashedIDString;sym:TSymEntry):boolean;
  220. begin
  221. result:=(FindWithHash(s)<>nil);
  222. end;
  223. procedure TSymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  224. var
  225. hashedid : THashedIDString;
  226. begin
  227. if checkdup then
  228. begin
  229. if sym.realname[1]='$' then
  230. hashedid.id:=Copy(sym.realname,2,255)
  231. else
  232. hashedid.id:=Upper(sym.realname);
  233. { First check for duplicates, this can change the symbol name
  234. in case of a duplicate entry }
  235. checkduplicate(hashedid,sym);
  236. end;
  237. { Now we can insert the symbol, any duplicate entries
  238. are renamed to an unique (and for users unaccessible) name }
  239. if sym.realname[1]='$' then
  240. sym.ChangeOwnerAndName(SymList,Copy(sym.realname,2,255))
  241. else
  242. sym.ChangeOwnerAndName(SymList,Upper(sym.realname));
  243. sym.Owner:=self;
  244. end;
  245. procedure TSymtable.insertdef(def:TDefEntry);
  246. begin
  247. DefList.Add(def);
  248. def.owner:=self;
  249. end;
  250. function TSymtable.Find(const s : TIDString) : TSymEntry;
  251. begin
  252. result:=TSymEntry(SymList.Find(s));
  253. end;
  254. function TSymtable.FindWithHash(const s:THashedIDString) : TSymEntry;
  255. begin
  256. result:=TSymEntry(SymList.FindWithHash(s.id,s.hash));
  257. end;
  258. {****************************************************************************
  259. Symtable Stack
  260. ****************************************************************************}
  261. constructor TSymtablestack.create;
  262. begin
  263. stack:=nil;
  264. end;
  265. destructor TSymtablestack.destroy;
  266. begin
  267. clear;
  268. end;
  269. procedure TSymtablestack.clear;
  270. var
  271. hp : psymtablestackitem;
  272. begin
  273. while assigned(stack) do
  274. begin
  275. hp:=stack;
  276. stack:=hp^.next;
  277. dispose(hp);
  278. end;
  279. end;
  280. procedure TSymtablestack.push(st:TSymtable);
  281. var
  282. hp : psymtablestackitem;
  283. begin
  284. new(hp);
  285. hp^.symtable:=st;
  286. hp^.next:=stack;
  287. stack:=hp;
  288. end;
  289. procedure TSymtablestack.pop(st:TSymtable);
  290. var
  291. hp : psymtablestackitem;
  292. begin
  293. if not assigned(stack) then
  294. internalerror(200601231);
  295. if stack^.symtable<>st then
  296. internalerror(200601232);
  297. hp:=stack;
  298. stack:=hp^.next;
  299. dispose(hp);
  300. end;
  301. function TSymtablestack.top:TSymtable;
  302. begin
  303. if not assigned(stack) then
  304. internalerror(200601233);
  305. result:=stack^.symtable;
  306. end;
  307. {$ifdef MEMDEBUG}
  308. initialization
  309. memrealnames:=TMemDebug.create('Realnames');
  310. memrealnames.stop;
  311. finalization
  312. memrealnames.free;
  313. {$endif MEMDEBUG}
  314. end.