symbase.pas 13 KB

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