symbase.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. procedure Delete(sym:TSymEntry);
  91. function Find(const s:TIDString) : TSymEntry;
  92. function FindWithHash(const s:THashedIDString) : TSymEntry;virtual;
  93. procedure insertdef(def:TDefEntry);virtual;
  94. procedure deletedef(def:TDefEntry);
  95. function iscurrentunit:boolean;virtual;
  96. end;
  97. psymtablestackitem = ^TSymtablestackitem;
  98. TSymtablestackitem = record
  99. symtable : TSymtable;
  100. next : psymtablestackitem;
  101. end;
  102. TSymtablestack = class
  103. stack : psymtablestackitem;
  104. constructor create;
  105. destructor destroy;override;
  106. procedure clear;
  107. procedure push(st:TSymtable);
  108. procedure pop(st:TSymtable);
  109. function top:TSymtable;
  110. end;
  111. var
  112. initialmacrosymtable: TSymtable; { macros initially defined by the compiler or
  113. given on the command line. Is common
  114. for all files compiled and do not change. }
  115. macrosymtablestack,
  116. symtablestack : TSymtablestack;
  117. {$ifdef MEMDEBUG}
  118. var
  119. memrealnames : tmemdebug;
  120. {$endif MEMDEBUG}
  121. implementation
  122. uses
  123. verbose;
  124. {****************************************************************************
  125. THashedIDString
  126. ****************************************************************************}
  127. procedure THashedIDString.SetId(const s:TIDString);
  128. begin
  129. FId:=s;
  130. FHash:=FPHash(s);
  131. end;
  132. {****************************************************************************
  133. TSymEntry
  134. ****************************************************************************}
  135. destructor TSymEntry.destroy;
  136. begin
  137. {$ifdef MEMDEBUG}
  138. memrealnames.start;
  139. {$endif MEMDEBUG}
  140. stringdispose(Frealname);
  141. {$ifdef MEMDEBUG}
  142. memrealnames.stop;
  143. {$endif MEMDEBUG}
  144. inherited destroy;
  145. end;
  146. function TSymEntry.GetRealname:shortstring;
  147. begin
  148. if not assigned(FRealname) then
  149. internalerror(200611011);
  150. result:=FRealname^;
  151. end;
  152. procedure TSymEntry.SetRealname(const ANewName:shortstring);
  153. begin
  154. stringdispose(FRealname);
  155. FRealname:=stringdup(ANewName);
  156. if Hash<>$ffffffff then
  157. begin
  158. if FRealname^[1]='$' then
  159. Rename(Copy(FRealname^,2,255))
  160. else
  161. Rename(Upper(FRealname^));
  162. end;
  163. end;
  164. {****************************************************************************
  165. TSymtable
  166. ****************************************************************************}
  167. constructor TSymtable.Create(const s:string);
  168. begin
  169. if s<>'' then
  170. begin
  171. name:=stringdup(upper(s));
  172. realname:=stringdup(s);
  173. end
  174. else
  175. begin
  176. name:=nil;
  177. realname:=nil;
  178. end;
  179. symtabletype:=abstracTSymtable;
  180. symtablelevel:=0;
  181. defowner:=nil;
  182. DefList:=TFPObjectList.Create(true);
  183. SymList:=TFPHashObjectList.Create(true);
  184. refcount:=1;
  185. end;
  186. destructor TSymtable.destroy;
  187. begin
  188. { freeinstance decreases refcount }
  189. if refcount>1 then
  190. exit;
  191. Clear;
  192. DefList.Free;
  193. { SymList can already be disposed or set to nil for withsymtable }
  194. if assigned(SymList) then
  195. SymList.Free;
  196. stringdispose(name);
  197. stringdispose(realname);
  198. end;
  199. procedure TSymtable.freeinstance;
  200. begin
  201. dec(refcount);
  202. if refcount=0 then
  203. inherited freeinstance;
  204. end;
  205. function TSymtable.getcopy:TSymtable;
  206. begin
  207. inc(refcount);
  208. result:=self;
  209. end;
  210. function TSymtable.iscurrentunit:boolean;
  211. begin
  212. result:=false;
  213. end;
  214. procedure TSymtable.clear;
  215. begin
  216. clearing:=true;
  217. SymList.Clear;
  218. DefList.Clear;
  219. clearing:=false;
  220. end;
  221. function TSymtable.checkduplicate(var s:THashedIDString;sym:TSymEntry):boolean;
  222. begin
  223. result:=(FindWithHash(s)<>nil);
  224. end;
  225. procedure TSymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  226. var
  227. hashedid : THashedIDString;
  228. begin
  229. if checkdup then
  230. begin
  231. if sym.realname[1]='$' then
  232. hashedid.id:=Copy(sym.realname,2,255)
  233. else
  234. hashedid.id:=Upper(sym.realname);
  235. { First check for duplicates, this can change the symbol name
  236. in case of a duplicate entry }
  237. checkduplicate(hashedid,sym);
  238. end;
  239. { Now we can insert the symbol, any duplicate entries
  240. are renamed to an unique (and for users unaccessible) name }
  241. if sym.realname[1]='$' then
  242. sym.ChangeOwnerAndName(SymList,Copy(sym.realname,2,255))
  243. else
  244. sym.ChangeOwnerAndName(SymList,Upper(sym.realname));
  245. sym.Owner:=self;
  246. end;
  247. procedure TSymtable.Delete(sym:TSymEntry);
  248. begin
  249. if sym.Owner<>self then
  250. internalerror(200611121);
  251. SymList.Remove(sym);
  252. end;
  253. procedure TSymtable.insertdef(def:TDefEntry);
  254. begin
  255. DefList.Add(def);
  256. def.owner:=self;
  257. end;
  258. procedure TSymtable.deletedef(def:TDefEntry);
  259. begin
  260. if def.Owner<>self then
  261. internalerror(200611122);
  262. DefList.Remove(def);
  263. end;
  264. function TSymtable.Find(const s : TIDString) : TSymEntry;
  265. begin
  266. result:=TSymEntry(SymList.Find(s));
  267. end;
  268. function TSymtable.FindWithHash(const s:THashedIDString) : TSymEntry;
  269. begin
  270. result:=TSymEntry(SymList.FindWithHash(s.id,s.hash));
  271. end;
  272. {****************************************************************************
  273. Symtable Stack
  274. ****************************************************************************}
  275. constructor TSymtablestack.create;
  276. begin
  277. stack:=nil;
  278. end;
  279. destructor TSymtablestack.destroy;
  280. begin
  281. clear;
  282. end;
  283. procedure TSymtablestack.clear;
  284. var
  285. hp : psymtablestackitem;
  286. begin
  287. while assigned(stack) do
  288. begin
  289. hp:=stack;
  290. stack:=hp^.next;
  291. dispose(hp);
  292. end;
  293. end;
  294. procedure TSymtablestack.push(st:TSymtable);
  295. var
  296. hp : psymtablestackitem;
  297. begin
  298. new(hp);
  299. hp^.symtable:=st;
  300. hp^.next:=stack;
  301. stack:=hp;
  302. end;
  303. procedure TSymtablestack.pop(st:TSymtable);
  304. var
  305. hp : psymtablestackitem;
  306. begin
  307. if not assigned(stack) then
  308. internalerror(200601231);
  309. if stack^.symtable<>st then
  310. internalerror(200601232);
  311. hp:=stack;
  312. stack:=hp^.next;
  313. dispose(hp);
  314. end;
  315. function TSymtablestack.top:TSymtable;
  316. begin
  317. if not assigned(stack) then
  318. internalerror(200601233);
  319. result:=stack^.symtable;
  320. end;
  321. {$ifdef MEMDEBUG}
  322. initialization
  323. memrealnames:=TMemDebug.create('Realnames');
  324. memrealnames.stop;
  325. finalization
  326. memrealnames.free;
  327. {$endif MEMDEBUG}
  328. end.