symbase.pas 12 KB

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