symbase.pas 12 KB

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