symbase.pas 11 KB

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