symbase.pas 13 KB

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