symbase.pas 14 KB

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