symbase.pas 11 KB

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