symbase.pas 14 KB

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