symtype.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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,
  23. { global }
  24. globtype,globals,
  25. { symtable }
  26. symconst,symbase,
  27. { aasm }
  28. aasm
  29. ;
  30. type
  31. {************************************************
  32. Required Forwards
  33. ************************************************}
  34. tsym = class;
  35. {************************************************
  36. TRef
  37. ************************************************}
  38. tref = class
  39. nextref : tref;
  40. posinfo : tfileposinfo;
  41. moduleindex : longint;
  42. is_written : boolean;
  43. constructor create(ref:tref;pos:pfileposinfo);
  44. procedure freechain;
  45. destructor destroy;override;
  46. end;
  47. {************************************************
  48. TDef
  49. ************************************************}
  50. tgetsymtable = (gs_none,gs_record,gs_local,gs_para);
  51. tdef = class(tdefentry)
  52. typesym : tsym; { which type the definition was generated this def }
  53. defoptions : tdefoptions;
  54. constructor create;
  55. procedure deref;virtual;abstract;
  56. procedure derefimpl;virtual;abstract;
  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):tsymtable;virtual;
  62. function is_publishable:boolean;virtual;abstract;
  63. function needs_inittable:boolean;virtual;abstract;
  64. end;
  65. {************************************************
  66. TSym
  67. ************************************************}
  68. { this object is the base for all symbol objects }
  69. tsym = class(tsymentry)
  70. _realname : pstring;
  71. fileinfo : tfileposinfo;
  72. symoptions : tsymoptions;
  73. constructor create(const n : string);
  74. destructor destroy;override;
  75. function realname:string;
  76. procedure deref;virtual;abstract;
  77. function gettypedef:tdef;virtual;
  78. end;
  79. {************************************************
  80. TType
  81. ************************************************}
  82. ttype = object
  83. def : tdef;
  84. sym : tsym;
  85. procedure reset;
  86. procedure setdef(p:tdef);
  87. procedure setsym(p:tsym);
  88. procedure resolve;
  89. end;
  90. {************************************************
  91. TSymList
  92. ************************************************}
  93. psymlistitem = ^tsymlistitem;
  94. tsymlistitem = record
  95. sltype : tsltype;
  96. sym : tsym;
  97. value : longint;
  98. next : psymlistitem;
  99. end;
  100. tsymlist = class
  101. def : tdef;
  102. firstsym,
  103. lastsym : psymlistitem;
  104. constructor create;
  105. destructor destroy;override;
  106. function empty:boolean;
  107. procedure setdef(p:tdef);
  108. procedure addsym(slt:tsltype;p:tsym);
  109. procedure addconst(slt:tsltype;v:longint);
  110. procedure clear;
  111. function getcopy:tsymlist;
  112. procedure resolve;
  113. end;
  114. { resolving }
  115. procedure resolvesym(var sym:tsym);
  116. procedure resolvedef(var def:tdef);
  117. implementation
  118. uses
  119. verbose,
  120. fmodule;
  121. {****************************************************************************
  122. Tdef
  123. ****************************************************************************}
  124. constructor tdef.create;
  125. begin
  126. inherited create;
  127. deftype:=abstractdef;
  128. owner := nil;
  129. typesym := nil;
  130. defoptions:=[];
  131. end;
  132. function tdef.typename:string;
  133. begin
  134. if assigned(typesym) and
  135. not(deftype=procvardef) and
  136. assigned(typesym._realname) and
  137. (typesym._realname^[1]<>'$') then
  138. typename:=typesym._realname^
  139. else
  140. typename:=gettypename;
  141. end;
  142. function tdef.gettypename : string;
  143. begin
  144. gettypename:='<unknown type>'
  145. end;
  146. function tdef.getsymtable(t:tgetsymtable):tsymtable;
  147. begin
  148. getsymtable:=nil;
  149. end;
  150. {****************************************************************************
  151. TSYM (base for all symtypes)
  152. ****************************************************************************}
  153. constructor tsym.create(const n : string);
  154. begin
  155. if n[1]='$' then
  156. inherited createname(copy(n,2,255))
  157. else
  158. inherited createname(upper(n));
  159. _realname:=stringdup(n);
  160. typ:=abstractsym;
  161. symoptions:=[];
  162. end;
  163. destructor tsym.destroy;
  164. begin
  165. stringdispose(_realname);
  166. inherited destroy;
  167. end;
  168. function tsym.realname : string;
  169. begin
  170. if assigned(_realname) then
  171. realname:=_realname^
  172. else
  173. realname:=name;
  174. end;
  175. function tsym.gettypedef:tdef;
  176. begin
  177. gettypedef:=nil;
  178. end;
  179. {****************************************************************************
  180. TRef
  181. ****************************************************************************}
  182. constructor tref.create(ref :tref;pos : pfileposinfo);
  183. begin
  184. nextref:=nil;
  185. if pos<>nil then
  186. posinfo:=pos^;
  187. if assigned(current_module) then
  188. moduleindex:=current_module.unit_index;
  189. if assigned(ref) then
  190. ref.nextref:=self;
  191. is_written:=false;
  192. end;
  193. procedure tref.freechain;
  194. var
  195. p,q : tref;
  196. begin
  197. p:=nextref;
  198. nextref:=nil;
  199. while assigned(p) do
  200. begin
  201. q:=p.nextref;
  202. p.free;
  203. p:=q;
  204. end;
  205. end;
  206. destructor tref.destroy;
  207. begin
  208. nextref:=nil;
  209. end;
  210. {****************************************************************************
  211. TType
  212. ****************************************************************************}
  213. procedure ttype.reset;
  214. begin
  215. def:=nil;
  216. sym:=nil;
  217. end;
  218. procedure ttype.setdef(p:tdef);
  219. begin
  220. def:=p;
  221. sym:=nil;
  222. end;
  223. procedure ttype.setsym(p:tsym);
  224. begin
  225. sym:=p;
  226. def:=p.gettypedef;
  227. if not assigned(def) then
  228. internalerror(1234005);
  229. end;
  230. procedure ttype.resolve;
  231. begin
  232. if assigned(sym) then
  233. begin
  234. resolvesym(sym);
  235. setsym(sym);
  236. end
  237. else
  238. resolvedef(def);
  239. end;
  240. {****************************************************************************
  241. TSymList
  242. ****************************************************************************}
  243. constructor tsymlist.create;
  244. begin
  245. def:=nil; { needed for procedures }
  246. firstsym:=nil;
  247. lastsym:=nil;
  248. end;
  249. destructor tsymlist.destroy;
  250. begin
  251. clear;
  252. end;
  253. function tsymlist.empty:boolean;
  254. begin
  255. empty:=(firstsym=nil);
  256. end;
  257. procedure tsymlist.clear;
  258. var
  259. hp : psymlistitem;
  260. begin
  261. while assigned(firstsym) do
  262. begin
  263. hp:=firstsym;
  264. firstsym:=firstsym^.next;
  265. dispose(hp);
  266. end;
  267. firstsym:=nil;
  268. lastsym:=nil;
  269. def:=nil;
  270. end;
  271. procedure tsymlist.setdef(p:tdef);
  272. begin
  273. def:=p;
  274. end;
  275. procedure tsymlist.addsym(slt:tsltype;p:tsym);
  276. var
  277. hp : psymlistitem;
  278. begin
  279. if not assigned(p) then
  280. internalerror(200110203);
  281. new(hp);
  282. hp^.sltype:=slt;
  283. hp^.sym:=p;
  284. hp^.value:=0;
  285. hp^.next:=nil;
  286. if assigned(lastsym) then
  287. lastsym^.next:=hp
  288. else
  289. firstsym:=hp;
  290. lastsym:=hp;
  291. end;
  292. procedure tsymlist.addconst(slt:tsltype;v:longint);
  293. var
  294. hp : psymlistitem;
  295. begin
  296. new(hp);
  297. hp^.sltype:=slt;
  298. hp^.sym:=nil;
  299. hp^.value:=v;
  300. hp^.next:=nil;
  301. if assigned(lastsym) then
  302. lastsym^.next:=hp
  303. else
  304. firstsym:=hp;
  305. lastsym:=hp;
  306. end;
  307. function tsymlist.getcopy:tsymlist;
  308. var
  309. hp : tsymlist;
  310. hp2 : psymlistitem;
  311. hpn : psymlistitem;
  312. begin
  313. hp:=tsymlist.create;
  314. hp.def:=def;
  315. hp2:=firstsym;
  316. while assigned(hp2) do
  317. begin
  318. new(hpn);
  319. hpn^:=hp2^;
  320. hpn^.next:=nil;
  321. if assigned(hp.lastsym) then
  322. hp.lastsym^.next:=hpn
  323. else
  324. hp.firstsym:=hpn;
  325. hp.lastsym:=hpn;
  326. hp2:=hp2^.next;
  327. end;
  328. getcopy:=hp;
  329. end;
  330. procedure tsymlist.resolve;
  331. var
  332. hp : psymlistitem;
  333. begin
  334. resolvedef(def);
  335. hp:=firstsym;
  336. while assigned(hp) do
  337. begin
  338. if assigned(hp^.sym) then
  339. resolvesym(hp^.sym);
  340. hp:=hp^.next;
  341. end;
  342. end;
  343. {*****************************************************************************
  344. Symbol / Definition Resolving
  345. *****************************************************************************}
  346. procedure resolvederef(var p:tderef;var st:tsymtable;var idx:word);
  347. var
  348. hp : tderef;
  349. pd : tdef;
  350. begin
  351. st:=nil;
  352. idx:=0;
  353. while assigned(p) do
  354. begin
  355. case p.dereftype of
  356. derefaktrecordindex :
  357. begin
  358. st:=aktrecordsymtable;
  359. idx:=p.index;
  360. end;
  361. derefaktstaticindex :
  362. begin
  363. st:=aktstaticsymtable;
  364. idx:=p.index;
  365. end;
  366. derefaktlocal :
  367. begin
  368. st:=aktlocalsymtable;
  369. idx:=p.index;
  370. end;
  371. derefunit :
  372. begin
  373. {$ifdef NEWMAP}
  374. st:=tsymtable(current_module.map^[p.index]^.globalsymtable);
  375. {$else NEWMAP}
  376. st:=tsymtable(current_module.map^[p.index]);
  377. {$endif NEWMAP}
  378. end;
  379. derefrecord :
  380. begin
  381. pd:=tdef(st.getdefnr(p.index));
  382. st:=pd.getsymtable(gs_record);
  383. if not assigned(st) then
  384. internalerror(556658);
  385. end;
  386. dereflocal :
  387. begin
  388. pd:=tdef(st.getdefnr(p.index));
  389. st:=pd.getsymtable(gs_local);
  390. if not assigned(st) then
  391. internalerror(556658);
  392. end;
  393. derefpara :
  394. begin
  395. pd:=tdef(st.getdefnr(p.index));
  396. st:=pd.getsymtable(gs_para);
  397. if not assigned(st) then
  398. internalerror(556658);
  399. end;
  400. derefindex :
  401. begin
  402. idx:=p.index;
  403. end;
  404. else
  405. internalerror(556658);
  406. end;
  407. hp:=p;
  408. p:=p.next;
  409. hp.free;
  410. end;
  411. end;
  412. procedure resolvedef(var def:tdef);
  413. var
  414. st : tsymtable;
  415. idx : word;
  416. begin
  417. resolvederef(tderef(def),st,idx);
  418. if assigned(st) then
  419. def:=tdef(st.getdefnr(idx))
  420. else
  421. def:=nil;
  422. end;
  423. procedure resolvesym(var sym:tsym);
  424. var
  425. st : tsymtable;
  426. idx : word;
  427. begin
  428. resolvederef(tderef(sym),st,idx);
  429. if assigned(st) then
  430. sym:=tsym(st.getsymnr(idx))
  431. else
  432. sym:=nil;
  433. end;
  434. end.
  435. {
  436. $Log$
  437. Revision 1.11 2001-11-02 22:58:08 peter
  438. * procsym definition rewrite
  439. Revision 1.10 2001/10/21 12:33:07 peter
  440. * array access for properties added
  441. Revision 1.9 2001/08/30 20:13:57 peter
  442. * rtti/init table updates
  443. * rttisym for reusable global rtti/init info
  444. * support published for interfaces
  445. Revision 1.8 2001/08/06 21:40:49 peter
  446. * funcret moved from tprocinfo to tprocdef
  447. Revision 1.7 2001/05/06 14:49:19 peter
  448. * ppu object to class rewrite
  449. * move ppu read and write stuff to fppu
  450. Revision 1.6 2001/04/13 01:22:17 peter
  451. * symtable change to classes
  452. * range check generation and errors fixed, make cycle DEBUG=1 works
  453. * memory leaks fixed
  454. Revision 1.5 2001/04/02 21:20:35 peter
  455. * resulttype rewrite
  456. Revision 1.4 2000/12/25 00:07:30 peter
  457. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  458. tlinkedlist objects)
  459. Revision 1.3 2000/11/29 00:30:42 florian
  460. * unused units removed from uses clause
  461. * some changes for widestrings
  462. Revision 1.2 2000/11/07 20:48:33 peter
  463. * removed ref_count from pinputfile it's not used
  464. Revision 1.1 2000/10/31 22:02:53 peter
  465. * symtable splitted, no real code changes
  466. }