symtype.pas 14 KB

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