symbase.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. currentvisibility : tvisibility;
  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);virtual;
  90. procedure Delete(sym:TSymEntry);virtual;
  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. currentvisibility:=vis_public;
  186. end;
  187. destructor TSymtable.destroy;
  188. begin
  189. { freeinstance decreases refcount }
  190. if refcount>1 then
  191. exit;
  192. Clear;
  193. DefList.Free;
  194. { SymList can already be disposed or set to nil for withsymtable, }
  195. { but in that case Free does nothing }
  196. SymList.Free;
  197. stringdispose(name);
  198. stringdispose(realname);
  199. end;
  200. procedure TSymtable.freeinstance;
  201. begin
  202. dec(refcount);
  203. if refcount=0 then
  204. inherited freeinstance;
  205. end;
  206. function TSymtable.getcopy:TSymtable;
  207. begin
  208. inc(refcount);
  209. result:=self;
  210. end;
  211. function TSymtable.iscurrentunit:boolean;
  212. begin
  213. result:=false;
  214. end;
  215. procedure TSymtable.clear;
  216. var
  217. i : integer;
  218. begin
  219. SymList.Clear;
  220. { Prevent recursive calls between TDef.destroy and TSymtable.Remove }
  221. if DefList.OwnsObjects then
  222. begin
  223. for i := 0 to DefList.Count-1 do
  224. TDefEntry(DefList[i]).Owner:=nil;
  225. end;
  226. DefList.Clear;
  227. end;
  228. function TSymtable.checkduplicate(var s:THashedIDString;sym:TSymEntry):boolean;
  229. begin
  230. result:=(FindWithHash(s)<>nil);
  231. end;
  232. procedure TSymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  233. var
  234. hashedid : THashedIDString;
  235. begin
  236. if checkdup then
  237. begin
  238. if sym.realname[1]='$' then
  239. hashedid.id:=Copy(sym.realname,2,255)
  240. else
  241. hashedid.id:=Upper(sym.realname);
  242. { First check for duplicates, this can change the symbol name
  243. in case of a duplicate entry }
  244. checkduplicate(hashedid,sym);
  245. end;
  246. { Now we can insert the symbol, any duplicate entries
  247. are renamed to an unique (and for users unaccessible) name }
  248. if sym.realname[1]='$' then
  249. sym.ChangeOwnerAndName(SymList,Copy(sym.realname,2,255))
  250. else
  251. sym.ChangeOwnerAndName(SymList,Upper(sym.realname));
  252. sym.Owner:=self;
  253. end;
  254. procedure TSymtable.Delete(sym:TSymEntry);
  255. begin
  256. if sym.Owner<>self then
  257. internalerror(200611121);
  258. SymList.Remove(sym);
  259. end;
  260. procedure TSymtable.insertdef(def:TDefEntry);
  261. begin
  262. DefList.Add(def);
  263. def.owner:=self;
  264. end;
  265. procedure TSymtable.deletedef(def:TDefEntry);
  266. begin
  267. if def.Owner<>self then
  268. internalerror(200611122);
  269. def.Owner:=nil;
  270. DefList.Remove(def);
  271. end;
  272. function TSymtable.Find(const s : TIDString) : TSymEntry;
  273. begin
  274. result:=TSymEntry(SymList.Find(s));
  275. end;
  276. function TSymtable.FindWithHash(const s:THashedIDString) : TSymEntry;
  277. begin
  278. result:=TSymEntry(SymList.FindWithHash(s.id,s.hash));
  279. end;
  280. {****************************************************************************
  281. Symtable Stack
  282. ****************************************************************************}
  283. constructor TSymtablestack.create;
  284. begin
  285. stack:=nil;
  286. end;
  287. destructor TSymtablestack.destroy;
  288. begin
  289. clear;
  290. end;
  291. procedure TSymtablestack.clear;
  292. var
  293. hp : psymtablestackitem;
  294. begin
  295. while assigned(stack) do
  296. begin
  297. hp:=stack;
  298. stack:=hp^.next;
  299. dispose(hp);
  300. end;
  301. end;
  302. procedure TSymtablestack.push(st:TSymtable);
  303. var
  304. hp : psymtablestackitem;
  305. begin
  306. new(hp);
  307. hp^.symtable:=st;
  308. hp^.next:=stack;
  309. stack:=hp;
  310. end;
  311. procedure TSymtablestack.pop(st:TSymtable);
  312. var
  313. hp : psymtablestackitem;
  314. begin
  315. if not assigned(stack) then
  316. internalerror(200601231);
  317. if stack^.symtable<>st then
  318. internalerror(200601232);
  319. hp:=stack;
  320. stack:=hp^.next;
  321. dispose(hp);
  322. end;
  323. function TSymtablestack.top:TSymtable;
  324. begin
  325. if not assigned(stack) then
  326. internalerror(200601233);
  327. result:=stack^.symtable;
  328. end;
  329. {$ifdef MEMDEBUG}
  330. initialization
  331. memrealnames:=TMemDebug.create('Realnames');
  332. memrealnames.stop;
  333. finalization
  334. memrealnames.free;
  335. {$endif MEMDEBUG}
  336. end.