symbase.pas 13 KB

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