symtype.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl, Pierre Muller
  4. This unit handles the symbol tables
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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 symtype;
  18. {$i defines.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cobjects,
  23. { global }
  24. globtype,globals,
  25. { symtable }
  26. symconst,symbase,
  27. { aasm }
  28. aasm
  29. ;
  30. type
  31. {************************************************
  32. Required Forwards
  33. ************************************************}
  34. psym = ^tsym;
  35. {************************************************
  36. TRef
  37. ************************************************}
  38. pref = ^tref;
  39. tref = object
  40. nextref : pref;
  41. posinfo : tfileposinfo;
  42. moduleindex : word;
  43. is_written : boolean;
  44. constructor init(ref:pref;pos:pfileposinfo);
  45. procedure freechain;
  46. destructor done; virtual;
  47. end;
  48. {************************************************
  49. TDef
  50. ************************************************}
  51. tgetsymtable = (gs_none,gs_record,gs_local,gs_para);
  52. pdef = ^tdef;
  53. tdef = object(tdefentry)
  54. typesym : psym; { which type the definition was generated this def }
  55. constructor init;
  56. procedure deref;virtual;
  57. function typename:string;
  58. function gettypename:string;virtual;
  59. function size:longint;virtual;abstract;
  60. function alignment:longint;virtual;abstract;
  61. function getsymtable(t:tgetsymtable):psymtable;virtual;
  62. function is_publishable:boolean;virtual;abstract;
  63. function needs_inittable:boolean;virtual;abstract;
  64. function get_rtti_label : string;virtual;abstract;
  65. end;
  66. {************************************************
  67. TSym
  68. ************************************************}
  69. { this object is the base for all symbol objects }
  70. tsym = object(tsymentry)
  71. _realname : pstring;
  72. fileinfo : tfileposinfo;
  73. symoptions : tsymoptions;
  74. constructor init(const n : string);
  75. destructor done;virtual;
  76. function realname:string;
  77. procedure prederef;virtual; { needed for ttypesym to be deref'd first }
  78. procedure deref;virtual;
  79. function gettypedef:pdef;virtual;
  80. function mangledname : string;virtual;abstract;
  81. end;
  82. {************************************************
  83. TType
  84. ************************************************}
  85. ttype = object
  86. def : pdef;
  87. sym : psym;
  88. procedure reset;
  89. procedure setdef(p:pdef);
  90. procedure setsym(p:psym);
  91. procedure load;
  92. procedure write;
  93. procedure resolve;
  94. end;
  95. {************************************************
  96. TSymList
  97. ************************************************}
  98. psymlistitem = ^tsymlistitem;
  99. tsymlistitem = record
  100. sym : psym;
  101. next : psymlistitem;
  102. end;
  103. psymlist = ^tsymlist;
  104. tsymlist = object
  105. def : pdef;
  106. firstsym,
  107. lastsym : psymlistitem;
  108. constructor init;
  109. constructor load;
  110. destructor done;
  111. function empty:boolean;
  112. procedure setdef(p:pdef);
  113. procedure addsym(p:psym);
  114. procedure clear;
  115. function getcopy:psymlist;
  116. procedure resolve;
  117. procedure write;
  118. end;
  119. { resolving }
  120. procedure resolvesym(var sym:psym);
  121. procedure resolvedef(var def:pdef);
  122. implementation
  123. uses
  124. verbose,
  125. ppu,symppu,
  126. finput,fmodule;
  127. {****************************************************************************
  128. Tdef
  129. ****************************************************************************}
  130. constructor tdef.init;
  131. begin
  132. inherited init;
  133. deftype:=abstractdef;
  134. owner := nil;
  135. typesym := nil;
  136. end;
  137. function tdef.typename:string;
  138. begin
  139. if assigned(typesym) and
  140. not(deftype=procvardef) and
  141. assigned(typesym^._realname) and
  142. (typesym^._realname^[1]<>'$') then
  143. typename:=typesym^._realname^
  144. else
  145. typename:=gettypename;
  146. end;
  147. function tdef.gettypename : string;
  148. begin
  149. gettypename:='<unknown type>'
  150. end;
  151. procedure tdef.deref;
  152. begin
  153. resolvesym(typesym);
  154. end;
  155. function tdef.getsymtable(t:tgetsymtable):psymtable;
  156. begin
  157. getsymtable:=nil;
  158. end;
  159. {****************************************************************************
  160. TSYM (base for all symtypes)
  161. ****************************************************************************}
  162. constructor tsym.init(const n : string);
  163. begin
  164. if n[1]='$' then
  165. inherited initname(copy(n,2,255))
  166. else
  167. inherited initname(upper(n));
  168. _realname:=stringdup(n);
  169. typ:=abstractsym;
  170. end;
  171. destructor tsym.done;
  172. begin
  173. stringdispose(_realname);
  174. inherited done;
  175. end;
  176. procedure tsym.prederef;
  177. begin
  178. end;
  179. procedure tsym.deref;
  180. begin
  181. end;
  182. function tsym.realname : string;
  183. begin
  184. if assigned(_realname) then
  185. realname:=_realname^
  186. else
  187. realname:=name;
  188. end;
  189. function tsym.gettypedef:pdef;
  190. begin
  191. gettypedef:=nil;
  192. end;
  193. {****************************************************************************
  194. TRef
  195. ****************************************************************************}
  196. constructor tref.init(ref :pref;pos : pfileposinfo);
  197. begin
  198. nextref:=nil;
  199. if pos<>nil then
  200. posinfo:=pos^;
  201. if assigned(current_module) then
  202. moduleindex:=current_module^.unit_index;
  203. if assigned(ref) then
  204. ref^.nextref:=@self;
  205. is_written:=false;
  206. end;
  207. procedure tref.freechain;
  208. var
  209. p,q : pref;
  210. begin
  211. p:=nextref;
  212. nextref:=nil;
  213. while assigned(p) do
  214. begin
  215. q:=p^.nextref;
  216. dispose(p,done);
  217. p:=q;
  218. end;
  219. end;
  220. destructor tref.done;
  221. var
  222. inputfile : pinputfile;
  223. begin
  224. inputfile:=get_source_file(moduleindex,posinfo.fileindex);
  225. if inputfile<>nil then
  226. dec(inputfile^.ref_count);
  227. nextref:=nil;
  228. end;
  229. {****************************************************************************
  230. TType
  231. ****************************************************************************}
  232. procedure ttype.reset;
  233. begin
  234. def:=nil;
  235. sym:=nil;
  236. end;
  237. procedure ttype.setdef(p:pdef);
  238. begin
  239. def:=p;
  240. sym:=nil;
  241. end;
  242. procedure ttype.setsym(p:psym);
  243. begin
  244. sym:=p;
  245. def:=p^.gettypedef;
  246. if not assigned(def) then
  247. internalerror(1234005);
  248. end;
  249. procedure ttype.load;
  250. begin
  251. def:=pdef(readderef);
  252. sym:=psym(readderef);
  253. end;
  254. procedure ttype.write;
  255. begin
  256. if assigned(sym) then
  257. begin
  258. writederef(nil);
  259. writederef(sym);
  260. end
  261. else
  262. begin
  263. writederef(def);
  264. writederef(nil);
  265. end;
  266. end;
  267. procedure ttype.resolve;
  268. begin
  269. if assigned(sym) then
  270. begin
  271. resolvesym(sym);
  272. setsym(sym);
  273. end
  274. else
  275. resolvedef(def);
  276. end;
  277. {****************************************************************************
  278. TSymList
  279. ****************************************************************************}
  280. constructor tsymlist.init;
  281. begin
  282. def:=nil; { needed for procedures }
  283. firstsym:=nil;
  284. lastsym:=nil;
  285. end;
  286. constructor tsymlist.load;
  287. var
  288. sym : psym;
  289. begin
  290. def:=pdef(readderef);
  291. firstsym:=nil;
  292. lastsym:=nil;
  293. repeat
  294. sym:=psym(readderef);
  295. if sym=nil then
  296. break;
  297. addsym(sym);
  298. until false;
  299. end;
  300. destructor tsymlist.done;
  301. begin
  302. clear;
  303. end;
  304. function tsymlist.empty:boolean;
  305. begin
  306. empty:=(firstsym=nil);
  307. end;
  308. procedure tsymlist.clear;
  309. var
  310. hp : psymlistitem;
  311. begin
  312. while assigned(firstsym) do
  313. begin
  314. hp:=firstsym;
  315. firstsym:=firstsym^.next;
  316. dispose(hp);
  317. end;
  318. firstsym:=nil;
  319. lastsym:=nil;
  320. def:=nil;
  321. end;
  322. procedure tsymlist.setdef(p:pdef);
  323. begin
  324. def:=p;
  325. end;
  326. procedure tsymlist.addsym(p:psym);
  327. var
  328. hp : psymlistitem;
  329. begin
  330. if not assigned(p) then
  331. exit;
  332. new(hp);
  333. hp^.sym:=p;
  334. hp^.next:=nil;
  335. if assigned(lastsym) then
  336. lastsym^.next:=hp
  337. else
  338. firstsym:=hp;
  339. lastsym:=hp;
  340. end;
  341. function tsymlist.getcopy:psymlist;
  342. var
  343. hp : psymlist;
  344. hp2 : psymlistitem;
  345. begin
  346. new(hp,init);
  347. hp^.def:=def;
  348. hp2:=firstsym;
  349. while assigned(hp2) do
  350. begin
  351. hp^.addsym(hp2^.sym);
  352. hp2:=hp2^.next;
  353. end;
  354. getcopy:=hp;
  355. end;
  356. procedure tsymlist.write;
  357. var
  358. hp : psymlistitem;
  359. begin
  360. writederef(def);
  361. hp:=firstsym;
  362. while assigned(hp) do
  363. begin
  364. writederef(hp^.sym);
  365. hp:=hp^.next;
  366. end;
  367. writederef(nil);
  368. end;
  369. procedure tsymlist.resolve;
  370. var
  371. hp : psymlistitem;
  372. begin
  373. resolvedef(def);
  374. hp:=firstsym;
  375. while assigned(hp) do
  376. begin
  377. resolvesym(hp^.sym);
  378. hp:=hp^.next;
  379. end;
  380. end;
  381. {*****************************************************************************
  382. Symbol / Definition Resolving
  383. *****************************************************************************}
  384. procedure resolvederef(var p:pderef;var st:psymtable;var idx:word);
  385. var
  386. hp : pderef;
  387. pd : pdef;
  388. begin
  389. st:=nil;
  390. idx:=0;
  391. while assigned(p) do
  392. begin
  393. case p^.dereftype of
  394. derefaktrecordindex :
  395. begin
  396. st:=aktrecordsymtable;
  397. idx:=p^.index;
  398. end;
  399. derefaktstaticindex :
  400. begin
  401. st:=aktstaticsymtable;
  402. idx:=p^.index;
  403. end;
  404. derefaktlocal :
  405. begin
  406. st:=aktlocalsymtable;
  407. idx:=p^.index;
  408. end;
  409. derefunit :
  410. begin
  411. {$ifdef NEWMAP}
  412. st:=psymtable(current_module^.map^[p^.index]^.globalsymtable);
  413. {$else NEWMAP}
  414. st:=psymtable(current_module^.map^[p^.index]);
  415. {$endif NEWMAP}
  416. end;
  417. derefrecord :
  418. begin
  419. pd:=pdef(st^.getdefnr(p^.index));
  420. st:=pd^.getsymtable(gs_record);
  421. if not assigned(st) then
  422. internalerror(556658);
  423. end;
  424. dereflocal :
  425. begin
  426. pd:=pdef(st^.getdefnr(p^.index));
  427. st:=pd^.getsymtable(gs_local);
  428. if not assigned(st) then
  429. internalerror(556658);
  430. end;
  431. derefpara :
  432. begin
  433. pd:=pdef(st^.getdefnr(p^.index));
  434. st:=pd^.getsymtable(gs_para);
  435. if not assigned(st) then
  436. internalerror(556658);
  437. end;
  438. derefindex :
  439. begin
  440. idx:=p^.index;
  441. end;
  442. else
  443. internalerror(556658);
  444. end;
  445. hp:=p;
  446. p:=p^.next;
  447. dispose(hp,done);
  448. end;
  449. end;
  450. procedure resolvedef(var def:pdef);
  451. var
  452. st : psymtable;
  453. idx : word;
  454. begin
  455. resolvederef(pderef(def),st,idx);
  456. if assigned(st) then
  457. def:=pdef(st^.getdefnr(idx))
  458. else
  459. def:=nil;
  460. end;
  461. procedure resolvesym(var sym:psym);
  462. var
  463. st : psymtable;
  464. idx : word;
  465. begin
  466. resolvederef(pderef(sym),st,idx);
  467. if assigned(st) then
  468. sym:=psym(st^.getsymnr(idx))
  469. else
  470. sym:=nil;
  471. end;
  472. end.
  473. {
  474. $Log$
  475. Revision 1.1 2000-10-31 22:02:53 peter
  476. * symtable splitted, no real code changes
  477. }