2
0

symbase.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. { this can happen for specializations of routines that are not yet
  240. owned cause they might be thrown away again }
  241. if not assigned(st) then
  242. break;
  243. { the flag is already set, so by definition it is set in the
  244. owning symtables as well }
  245. if option in st.tableoptions then
  246. break;
  247. include(st.tableoptions,option);
  248. end;
  249. end;
  250. procedure TSymtable.clear;
  251. var
  252. i : integer;
  253. begin
  254. SymList.Clear;
  255. { Prevent recursive calls between TDef.destroy and TSymtable.Remove }
  256. if DefList.OwnsObjects then
  257. begin
  258. for i := 0 to DefList.Count-1 do
  259. TDefEntry(DefList[i]).Owner:=nil;
  260. end;
  261. DefList.Clear;
  262. end;
  263. function TSymtable.checkduplicate(var s:THashedIDString;sym:TSymEntry):boolean;
  264. begin
  265. result:=(FindWithHash(s)<>nil);
  266. end;
  267. procedure TSymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  268. var
  269. hashedid : THashedIDString;
  270. begin
  271. if checkdup then
  272. begin
  273. if sym.realname[1]='$' then
  274. hashedid.id:=Copy(sym.realname,2,255)
  275. else
  276. hashedid.id:=Upper(sym.realname);
  277. { First check for duplicates, this can change the symbol name
  278. in case of a duplicate entry }
  279. checkduplicate(hashedid,sym);
  280. end;
  281. { Now we can insert the symbol, any duplicate entries
  282. are renamed to an unique (and for users unaccessible) name }
  283. if sym.realname[1]='$' then
  284. sym.ChangeOwnerAndName(SymList,Copy(sym.realname,2,255))
  285. else
  286. sym.ChangeOwnerAndName(SymList,Upper(sym.realname));
  287. sym.Owner:=self;
  288. end;
  289. procedure TSymtable.Delete(sym:TSymEntry);
  290. begin
  291. if sym.Owner<>self then
  292. internalerror(200611121);
  293. SymList.Remove(sym);
  294. end;
  295. procedure TSymtable.insertdef(def:TDefEntry);
  296. begin
  297. DefList.Add(def);
  298. def.owner:=self;
  299. end;
  300. procedure TSymtable.deletedef(def:TDefEntry);
  301. begin
  302. if def.Owner<>self then
  303. internalerror(200611122);
  304. def.Owner:=nil;
  305. DefList.Remove(def);
  306. end;
  307. function TSymtable.Find(const s : TIDString) : TSymEntry;
  308. begin
  309. result:=TSymEntry(SymList.Find(s));
  310. end;
  311. function TSymtable.FindWithHash(const s:THashedIDString) : TSymEntry;
  312. begin
  313. result:=TSymEntry(SymList.FindWithHash(s.id,s.hash));
  314. end;
  315. {****************************************************************************
  316. Symtable Stack
  317. ****************************************************************************}
  318. constructor TSymtablestack.create;
  319. begin
  320. stack:=nil;
  321. end;
  322. destructor TSymtablestack.destroy;
  323. begin
  324. clear;
  325. end;
  326. procedure TSymtablestack.clear;
  327. var
  328. hp : psymtablestackitem;
  329. begin
  330. while assigned(stack) do
  331. begin
  332. hp:=stack;
  333. stack:=hp^.next;
  334. dispose(hp);
  335. end;
  336. end;
  337. function TSymtablestack.finditem(st: TSymtable): psymtablestackitem;
  338. begin
  339. if not assigned(stack) then
  340. internalerror(200601233);
  341. result:=stack;
  342. while assigned(result)and(result^.symtable<>st) do
  343. result:=result^.next;
  344. end;
  345. procedure TSymtablestack.push(st:TSymtable);
  346. var
  347. hp : psymtablestackitem;
  348. begin
  349. new(hp);
  350. hp^.symtable:=st;
  351. hp^.next:=stack;
  352. stack:=hp;
  353. end;
  354. procedure TSymtablestack.pushafter(st,afterst:TSymtable);
  355. var
  356. hp,afteritem: psymtablestackitem;
  357. begin
  358. afteritem:=finditem(afterst);
  359. if assigned(afteritem) then
  360. begin
  361. new(hp);
  362. hp^.symtable:=st;
  363. hp^.next:=afteritem^.next;
  364. afteritem^.next:=hp;
  365. end
  366. else
  367. internalerror(201309171);
  368. end;
  369. procedure TSymtablestack.pop(st:TSymtable);
  370. var
  371. hp : psymtablestackitem;
  372. begin
  373. if not assigned(stack) then
  374. internalerror(200601231);
  375. if stack^.symtable<>st then
  376. internalerror(200601232);
  377. hp:=stack;
  378. stack:=hp^.next;
  379. dispose(hp);
  380. end;
  381. function TSymtablestack.top:TSymtable;
  382. begin
  383. if not assigned(stack) then
  384. internalerror(200601233);
  385. result:=stack^.symtable;
  386. end;
  387. function addstitemreverse(st: TSymtablestack; finalst: tsymtable; curitem: psymtablestackitem): boolean;
  388. begin
  389. if not assigned(curitem) then
  390. begin
  391. result:=true;
  392. exit;
  393. end;
  394. if addstitemreverse(st,finalst,curitem^.next) then
  395. begin
  396. st.push(curitem^.symtable);
  397. result:=curitem^.symtable<>finalst
  398. end
  399. else
  400. result:=false
  401. end;
  402. function TSymtablestack.getcopyuntil(finalst: TSymtable): TSymtablestack;
  403. begin
  404. result:=TSymtablestack.create;
  405. addstitemreverse(result,finalst,stack);
  406. end;
  407. {$ifdef MEMDEBUG}
  408. initialization
  409. memrealnames:=TMemDebug.create('Realnames');
  410. memrealnames.stop;
  411. finalization
  412. memrealnames.free;
  413. {$endif MEMDEBUG}
  414. end.