symbase.pas 14 KB

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