symbase.pas 11 KB

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