symbase.pas 13 KB

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