symbase.pas 11 KB

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