hcgdata.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by Florian Klaempfl
  4. Routines for the code generation of data structures
  5. like VMT,Messages
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit hcgdata;
  20. interface
  21. uses
  22. symtable,aasm;
  23. { generates the message tables for a class }
  24. function genstrmsgtab(_class : pobjectdef) : plabel;
  25. function genintmsgtab(_class : pobjectdef) : plabel;
  26. { generates a VMT for _class }
  27. procedure genvmt(_class : pobjectdef);
  28. implementation
  29. uses
  30. strings,cobjects,
  31. globtype,globals,verbose,
  32. types,
  33. hcodegen;
  34. {*****************************************************************************
  35. Message
  36. *****************************************************************************}
  37. type
  38. pprocdeftree = ^tprocdeftree;
  39. tprocdeftree = record
  40. p : pprocdef;
  41. nl : plabel;
  42. l,r : pprocdeftree;
  43. end;
  44. var
  45. root : pprocdeftree;
  46. count : longint;
  47. procedure insertstr(p : pprocdeftree;var at : pprocdeftree);
  48. var
  49. i : longint;
  50. begin
  51. if at=nil then
  52. begin
  53. at:=p;
  54. inc(count);
  55. end
  56. else
  57. begin
  58. i:=strcomp(p^.p^.messageinf.str,at^.p^.messageinf.str);
  59. if i<0 then
  60. insertstr(p,at^.l)
  61. else if i>0 then
  62. insertstr(p,at^.r)
  63. else
  64. Message1(parser_e_duplicate_message_label,strpas(p^.p^.messageinf.str));
  65. end;
  66. end;
  67. procedure disposeprocdeftree(p : pprocdeftree);
  68. begin
  69. if assigned(p^.l) then
  70. disposeprocdeftree(p^.l);
  71. if assigned(p^.r) then
  72. disposeprocdeftree(p^.r);
  73. dispose(p);
  74. end;
  75. procedure insertmsgstr(p : psym);{$ifndef FPC}far;{$endif FPC}
  76. var
  77. hp : pprocdef;
  78. pt : pprocdeftree;
  79. begin
  80. if p^.typ=procsym then
  81. begin
  82. hp:=pprocsym(p)^.definition;
  83. while assigned(hp) do
  84. begin
  85. if (hp^.options and pomsgstr)<>0 then
  86. begin
  87. new(pt);
  88. pt^.p:=hp;
  89. pt^.l:=nil;
  90. pt^.r:=nil;
  91. insertstr(pt,root);
  92. end;
  93. hp:=hp^.nextoverloaded;
  94. end;
  95. end;
  96. end;
  97. procedure insertint(p : pprocdeftree;var at : pprocdeftree);
  98. var
  99. i : longint;
  100. begin
  101. if at=nil then
  102. begin
  103. at:=p;
  104. inc(count);
  105. end
  106. else
  107. begin
  108. i:=strcomp(p^.p^.messageinf.str,at^.p^.messageinf.str);
  109. if p^.p^.messageinf.i<at^.p^.messageinf.i then
  110. insertstr(p,at^.l)
  111. else if p^.p^.messageinf.i>at^.p^.messageinf.i then
  112. insertstr(p,at^.r)
  113. else
  114. Message1(parser_e_duplicate_message_label,tostr(p^.p^.messageinf.i));
  115. end;
  116. end;
  117. procedure insertmsgint(p : psym);{$ifndef FPC}far;{$endif FPC}
  118. var
  119. hp : pprocdef;
  120. pt : pprocdeftree;
  121. begin
  122. if p^.typ=procsym then
  123. begin
  124. hp:=pprocsym(p)^.definition;
  125. while assigned(hp) do
  126. begin
  127. if (hp^.options and pomsgint)<>0 then
  128. begin
  129. new(pt);
  130. pt^.p:=hp;
  131. pt^.l:=nil;
  132. pt^.r:=nil;
  133. insertint(pt,root);
  134. end;
  135. hp:=hp^.nextoverloaded;
  136. end;
  137. end;
  138. end;
  139. procedure writenames(p : pprocdeftree);
  140. begin
  141. getlabel(p^.nl);
  142. if assigned(p^.l) then
  143. writenames(p^.l);
  144. datasegment^.concat(new(pai_label,init(p^.nl)));
  145. datasegment^.concat(new(pai_const,init_8bit(strlen(p^.p^.messageinf.str))));
  146. datasegment^.concat(new(pai_string,init_pchar(p^.p^.messageinf.str)));
  147. if assigned(p^.r) then
  148. writenames(p^.r);
  149. end;
  150. procedure writestrentry(p : pprocdeftree);
  151. begin
  152. if assigned(p^.l) then
  153. writestrentry(p^.l);
  154. { write name label }
  155. datasegment^.concat(new(pai_const_symbol,init(lab2str(p^.nl))));
  156. datasegment^.concat(new(pai_const_symbol,init(p^.p^.mangledname)));
  157. maybe_concat_external(p^.p^.owner,p^.p^.mangledname);
  158. if assigned(p^.r) then
  159. writestrentry(p^.r);
  160. end;
  161. function genstrmsgtab(_class : pobjectdef) : plabel;
  162. var
  163. r : plabel;
  164. begin
  165. root:=nil;
  166. count:=0;
  167. { insert all message handlers into a tree, sorted by name }
  168. _class^.publicsyms^.foreach(insertmsgstr);
  169. { write all names }
  170. if assigned(root) then
  171. writenames(root);
  172. { now start writing of the message string table }
  173. getlabel(r);
  174. datasegment^.concat(new(pai_label,init(r)));
  175. genstrmsgtab:=r;
  176. datasegment^.concat(new(pai_const,init_32bit(count)));
  177. if assigned(root) then
  178. begin
  179. writestrentry(root);
  180. disposeprocdeftree(root);
  181. end;
  182. end;
  183. procedure writeintentry(p : pprocdeftree);
  184. begin
  185. if assigned(p^.l) then
  186. writeintentry(p^.l);
  187. { write name label }
  188. datasegment^.concat(new(pai_const,init_32bit(p^.p^.messageinf.i)));
  189. datasegment^.concat(new(pai_const_symbol,init(p^.p^.mangledname)));
  190. maybe_concat_external(p^.p^.owner,p^.p^.mangledname);
  191. if assigned(p^.r) then
  192. writeintentry(p^.r);
  193. end;
  194. function genintmsgtab(_class : pobjectdef) : plabel;
  195. var
  196. r : plabel;
  197. begin
  198. root:=nil;
  199. count:=0;
  200. { insert all message handlers into a tree, sorted by name }
  201. _class^.publicsyms^.foreach(insertmsgint);
  202. { now start writing of the message string table }
  203. getlabel(r);
  204. datasegment^.concat(new(pai_label,init(r)));
  205. genintmsgtab:=r;
  206. datasegment^.concat(new(pai_const,init_32bit(count)));
  207. if assigned(root) then
  208. begin
  209. writeintentry(root);
  210. disposeprocdeftree(root);
  211. end;
  212. end;
  213. {*****************************************************************************
  214. VMT
  215. *****************************************************************************}
  216. type
  217. pprocdefcoll = ^tprocdefcoll;
  218. tprocdefcoll = record
  219. next : pprocdefcoll;
  220. data : pprocdef;
  221. end;
  222. psymcoll = ^tsymcoll;
  223. tsymcoll = record
  224. next : psymcoll;
  225. name : pstring;
  226. data : pprocdefcoll;
  227. end;
  228. var
  229. wurzel : psymcoll;
  230. nextvirtnumber : longint;
  231. _c : pobjectdef;
  232. has_constructor,has_virtual_method : boolean;
  233. procedure eachsym(sym : psym);{$ifndef FPC}far;{$endif FPC}
  234. var
  235. procdefcoll : pprocdefcoll;
  236. hp : pprocdef;
  237. symcoll : psymcoll;
  238. _name : string;
  239. stored : boolean;
  240. { creates a new entry in the procsym list }
  241. procedure newentry;
  242. begin
  243. { if not, generate a new symbol item }
  244. new(symcoll);
  245. symcoll^.name:=stringdup(sym^.name);
  246. symcoll^.next:=wurzel;
  247. symcoll^.data:=nil;
  248. wurzel:=symcoll;
  249. hp:=pprocsym(sym)^.definition;
  250. { inserts all definitions }
  251. while assigned(hp) do
  252. begin
  253. new(procdefcoll);
  254. procdefcoll^.data:=hp;
  255. procdefcoll^.next:=symcoll^.data;
  256. symcoll^.data:=procdefcoll;
  257. { if it's a virtual method }
  258. if (hp^.options and povirtualmethod)<>0 then
  259. begin
  260. { then it gets a number ... }
  261. hp^.extnumber:=nextvirtnumber;
  262. { and we inc the number }
  263. inc(nextvirtnumber);
  264. has_virtual_method:=true;
  265. end;
  266. if (hp^.options and poconstructor)<>0 then
  267. has_constructor:=true;
  268. { check, if a method should be overridden }
  269. if (hp^.options and pooverridingmethod)<>0 then
  270. Message1(parser_e_nothing_to_be_overridden,_c^.name^+'.'+_name);
  271. { next overloaded method }
  272. hp:=hp^.nextoverloaded;
  273. end;
  274. end;
  275. begin
  276. { put only sub routines into the VMT }
  277. if sym^.typ=procsym then
  278. begin
  279. _name:=sym^.name;
  280. symcoll:=wurzel;
  281. while assigned(symcoll) do
  282. begin
  283. { does the symbol already exist in the list ? }
  284. if _name=symcoll^.name^ then
  285. begin
  286. { walk through all defs of the symbol }
  287. hp:=pprocsym(sym)^.definition;
  288. while assigned(hp) do
  289. begin
  290. { compare with all stored definitions }
  291. procdefcoll:=symcoll^.data;
  292. stored:=false;
  293. while assigned(procdefcoll) do
  294. begin
  295. { compare parameters }
  296. if equal_paras(procdefcoll^.data^.para1,hp^.para1,false) and
  297. (
  298. ((procdefcoll^.data^.options and povirtualmethod)<>0) or
  299. ((hp^.options and povirtualmethod)<>0)
  300. ) then
  301. begin
  302. { wenn sie gleich sind }
  303. { und eine davon virtual deklariert ist }
  304. { Fehler falls nur eine VIRTUAL }
  305. if (procdefcoll^.data^.options and povirtualmethod)<>
  306. (hp^.options and povirtualmethod) then
  307. begin
  308. { in classes, we hide the old method }
  309. if _c^.isclass then
  310. begin
  311. { warn only if it is the first time,
  312. we hide the method }
  313. if _c=hp^._class then
  314. Message1(parser_w_should_use_override,_c^.name^+'.'+_name);
  315. newentry;
  316. exit;
  317. end
  318. else
  319. if _c=hp^._class then
  320. begin
  321. if (procdefcoll^.data^.options and povirtualmethod)<>0 then
  322. Message1(parser_w_overloaded_are_not_both_virtual,_c^.name^+'.'+_name)
  323. else
  324. Message1(parser_w_overloaded_are_not_both_non_virtual,
  325. _c^.name^+'.'+_name);
  326. newentry;
  327. exit;
  328. end;
  329. end;
  330. { check, if the overridden directive is set }
  331. { (povirtualmethod is set! }
  332. { class ? }
  333. if _c^.isclass and
  334. ((hp^.options and pooverridingmethod)=0) then
  335. begin
  336. { warn only if it is the first time,
  337. we hide the method }
  338. if _c=hp^._class then
  339. Message1(parser_w_should_use_override,_c^.name^+'.'+_name);
  340. newentry;
  341. exit;
  342. end;
  343. { error, if the return types aren't equal }
  344. if not(is_equal(procdefcoll^.data^.retdef,hp^.retdef)) and
  345. not((procdefcoll^.data^.retdef^.deftype=objectdef) and
  346. (hp^.retdef^.deftype=objectdef) and
  347. (pobjectdef(procdefcoll^.data^.retdef)^.isclass) and
  348. (pobjectdef(hp^.retdef)^.isclass) and
  349. (pobjectdef(hp^.retdef)^.isrelated(pobjectdef(procdefcoll^.data^.retdef)))) then
  350. Message1(parser_e_overloaded_methodes_not_same_ret,_c^.name^+'.'+_name);
  351. { the flags have to match }
  352. { except abstract and override }
  353. if (procdefcoll^.data^.options and not(poabstractmethod or pooverridingmethod))<>
  354. (hp^.options and not(poabstractmethod or pooverridingmethod)) then
  355. Message1(parser_e_header_dont_match_forward,_c^.name^+'.'+_name);
  356. { now set the number }
  357. hp^.extnumber:=procdefcoll^.data^.extnumber;
  358. { and exchange }
  359. procdefcoll^.data:=hp;
  360. stored:=true;
  361. end;
  362. procdefcoll:=procdefcoll^.next;
  363. end;
  364. { if it isn't saved in the list }
  365. { we create a new entry }
  366. if not(stored) then
  367. begin
  368. new(procdefcoll);
  369. procdefcoll^.data:=hp;
  370. procdefcoll^.next:=symcoll^.data;
  371. symcoll^.data:=procdefcoll;
  372. { if the method is virtual ... }
  373. if (hp^.options and povirtualmethod)<>0 then
  374. begin
  375. { ... it will get a number }
  376. hp^.extnumber:=nextvirtnumber;
  377. inc(nextvirtnumber);
  378. end;
  379. { check, if a method should be overridden }
  380. if (hp^.options and pooverridingmethod)<>0 then
  381. Message1(parser_e_nothing_to_be_overridden,_c^.name^+'.'+_name);
  382. end;
  383. hp:=hp^.nextoverloaded;
  384. end;
  385. exit;
  386. end;
  387. symcoll:=symcoll^.next;
  388. end;
  389. newentry;
  390. end;
  391. end;
  392. procedure genvmt(_class : pobjectdef);
  393. procedure do_genvmt(p : pobjectdef);
  394. begin
  395. { start with the base class }
  396. if assigned(p^.childof) then
  397. do_genvmt(p^.childof);
  398. { walk through all public syms }
  399. _c:=_class;
  400. {$ifdef tp}
  401. p^.publicsyms^.foreach(eachsym);
  402. {$else}
  403. p^.publicsyms^.foreach(@eachsym);
  404. {$endif}
  405. end;
  406. var
  407. symcoll : psymcoll;
  408. procdefcoll : pprocdefcoll;
  409. i : longint;
  410. begin
  411. wurzel:=nil;
  412. nextvirtnumber:=0;
  413. has_constructor:=false;
  414. has_virtual_method:=false;
  415. { generates a tree of all used methods }
  416. do_genvmt(_class);
  417. if has_virtual_method and not(has_constructor) then
  418. Message1(parser_w_virtual_without_constructor,_class^.name^);
  419. { generates the VMT }
  420. { walk trough all numbers for virtual methods and search }
  421. { the method }
  422. for i:=0 to nextvirtnumber-1 do
  423. begin
  424. symcoll:=wurzel;
  425. { walk trough all symbols }
  426. while assigned(symcoll) do
  427. begin
  428. { walk trough all methods }
  429. procdefcoll:=symcoll^.data;
  430. while assigned(procdefcoll) do
  431. begin
  432. { writes the addresses to the VMT }
  433. { but only this which are declared as virtual }
  434. if procdefcoll^.data^.extnumber=i then
  435. begin
  436. if (procdefcoll^.data^.options and povirtualmethod)<>0 then
  437. begin
  438. { if a method is abstract, then is also the }
  439. { class abstract and it's not allow to }
  440. { generates an instance }
  441. if (procdefcoll^.data^.options and poabstractmethod)<>0 then
  442. begin
  443. _class^.options:=_class^.options or oo_is_abstract;
  444. datasegment^.concat(new(pai_const_symbol,
  445. init('FPC_ABSTRACTERROR')));
  446. end
  447. else
  448. begin
  449. datasegment^.concat(new(pai_const_symbol,
  450. init(procdefcoll^.data^.mangledname)));
  451. maybe_concat_external(procdefcoll^.data^.owner,
  452. procdefcoll^.data^.mangledname);
  453. end;
  454. end;
  455. end;
  456. procdefcoll:=procdefcoll^.next;
  457. end;
  458. symcoll:=symcoll^.next;
  459. end;
  460. end;
  461. { disposes the above generated tree }
  462. symcoll:=wurzel;
  463. while assigned(symcoll) do
  464. begin
  465. wurzel:=symcoll^.next;
  466. stringdispose(symcoll^.name);
  467. procdefcoll:=symcoll^.data;
  468. while assigned(procdefcoll) do
  469. begin
  470. symcoll^.data:=procdefcoll^.next;
  471. dispose(procdefcoll);
  472. procdefcoll:=symcoll^.data;
  473. end;
  474. dispose(symcoll);
  475. symcoll:=wurzel;
  476. end;
  477. end;
  478. end.
  479. {
  480. $Log$
  481. Revision 1.1 1999-03-24 23:17:00 peter
  482. * fixed bugs 212,222,225,227,229,231,233
  483. }