hcgdata.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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) : pasmlabel;
  25. function genintmsgtab(_class : pobjectdef) : pasmlabel;
  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 : pasmlabel;
  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 : pnamedindexobject);{$ifndef FPC}far;{$endif FPC}
  76. var
  77. hp : pprocdef;
  78. pt : pprocdeftree;
  79. begin
  80. if psym(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. begin
  99. if at=nil then
  100. begin
  101. at:=p;
  102. inc(count);
  103. end
  104. else
  105. begin
  106. if p^.p^.messageinf.i<at^.p^.messageinf.i then
  107. insertint(p,at^.l)
  108. else if p^.p^.messageinf.i>at^.p^.messageinf.i then
  109. insertint(p,at^.r)
  110. else
  111. Message1(parser_e_duplicate_message_label,tostr(p^.p^.messageinf.i));
  112. end;
  113. end;
  114. procedure insertmsgint(p : pnamedindexobject);{$ifndef FPC}far;{$endif FPC}
  115. var
  116. hp : pprocdef;
  117. pt : pprocdeftree;
  118. begin
  119. if psym(p)^.typ=procsym then
  120. begin
  121. hp:=pprocsym(p)^.definition;
  122. while assigned(hp) do
  123. begin
  124. if (hp^.options and pomsgint)<>0 then
  125. begin
  126. new(pt);
  127. pt^.p:=hp;
  128. pt^.l:=nil;
  129. pt^.r:=nil;
  130. insertint(pt,root);
  131. end;
  132. hp:=hp^.nextoverloaded;
  133. end;
  134. end;
  135. end;
  136. procedure writenames(p : pprocdeftree);
  137. begin
  138. getdatalabel(p^.nl);
  139. if assigned(p^.l) then
  140. writenames(p^.l);
  141. datasegment^.concat(new(pai_label,init(p^.nl)));
  142. datasegment^.concat(new(pai_const,init_8bit(strlen(p^.p^.messageinf.str))));
  143. datasegment^.concat(new(pai_string,init_pchar(p^.p^.messageinf.str)));
  144. if assigned(p^.r) then
  145. writenames(p^.r);
  146. end;
  147. procedure writestrentry(p : pprocdeftree);
  148. begin
  149. if assigned(p^.l) then
  150. writestrentry(p^.l);
  151. { write name label }
  152. datasegment^.concat(new(pai_const_symbol,init(p^.nl)));
  153. datasegment^.concat(new(pai_const_symbol,initname(p^.p^.mangledname)));
  154. if assigned(p^.r) then
  155. writestrentry(p^.r);
  156. end;
  157. function genstrmsgtab(_class : pobjectdef) : pasmlabel;
  158. var
  159. r : pasmlabel;
  160. begin
  161. root:=nil;
  162. count:=0;
  163. { insert all message handlers into a tree, sorted by name }
  164. _class^.publicsyms^.foreach({$ifndef TP}@{$endif}insertmsgstr);
  165. { write all names }
  166. if assigned(root) then
  167. writenames(root);
  168. { now start writing of the message string table }
  169. getdatalabel(r);
  170. datasegment^.concat(new(pai_label,init(r)));
  171. genstrmsgtab:=r;
  172. datasegment^.concat(new(pai_const,init_32bit(count)));
  173. if assigned(root) then
  174. begin
  175. writestrentry(root);
  176. disposeprocdeftree(root);
  177. end;
  178. end;
  179. procedure writeintentry(p : pprocdeftree);
  180. begin
  181. if assigned(p^.l) then
  182. writeintentry(p^.l);
  183. { write name label }
  184. datasegment^.concat(new(pai_const,init_32bit(p^.p^.messageinf.i)));
  185. datasegment^.concat(new(pai_const_symbol,initname(p^.p^.mangledname)));
  186. if assigned(p^.r) then
  187. writeintentry(p^.r);
  188. end;
  189. function genintmsgtab(_class : pobjectdef) : pasmlabel;
  190. var
  191. r : pasmlabel;
  192. begin
  193. root:=nil;
  194. count:=0;
  195. { insert all message handlers into a tree, sorted by name }
  196. _class^.publicsyms^.foreach({$ifndef TP}@{$endif}insertmsgint);
  197. { now start writing of the message string table }
  198. getdatalabel(r);
  199. datasegment^.concat(new(pai_label,init(r)));
  200. genintmsgtab:=r;
  201. datasegment^.concat(new(pai_const,init_32bit(count)));
  202. if assigned(root) then
  203. begin
  204. writeintentry(root);
  205. disposeprocdeftree(root);
  206. end;
  207. end;
  208. {*****************************************************************************
  209. VMT
  210. *****************************************************************************}
  211. type
  212. pprocdefcoll = ^tprocdefcoll;
  213. tprocdefcoll = record
  214. next : pprocdefcoll;
  215. data : pprocdef;
  216. end;
  217. psymcoll = ^tsymcoll;
  218. tsymcoll = record
  219. next : psymcoll;
  220. name : pstring;
  221. data : pprocdefcoll;
  222. end;
  223. var
  224. wurzel : psymcoll;
  225. nextvirtnumber : longint;
  226. _c : pobjectdef;
  227. has_constructor,has_virtual_method : boolean;
  228. procedure eachsym(sym : pnamedindexobject);{$ifndef FPC}far;{$endif FPC}
  229. var
  230. procdefcoll : pprocdefcoll;
  231. hp : pprocdef;
  232. symcoll : psymcoll;
  233. _name : string;
  234. stored : boolean;
  235. { creates a new entry in the procsym list }
  236. procedure newentry;
  237. begin
  238. { if not, generate a new symbol item }
  239. new(symcoll);
  240. symcoll^.name:=stringdup(sym^.name);
  241. symcoll^.next:=wurzel;
  242. symcoll^.data:=nil;
  243. wurzel:=symcoll;
  244. hp:=pprocsym(sym)^.definition;
  245. { inserts all definitions }
  246. while assigned(hp) do
  247. begin
  248. new(procdefcoll);
  249. procdefcoll^.data:=hp;
  250. procdefcoll^.next:=symcoll^.data;
  251. symcoll^.data:=procdefcoll;
  252. { if it's a virtual method }
  253. if (hp^.options and povirtualmethod)<>0 then
  254. begin
  255. { then it gets a number ... }
  256. hp^.extnumber:=nextvirtnumber;
  257. { and we inc the number }
  258. inc(nextvirtnumber);
  259. has_virtual_method:=true;
  260. end;
  261. if (hp^.options and poconstructor)<>0 then
  262. has_constructor:=true;
  263. { check, if a method should be overridden }
  264. if (hp^.options and pooverridingmethod)<>0 then
  265. Message1(parser_e_nothing_to_be_overridden,_c^.objname^+'.'+_name);
  266. { next overloaded method }
  267. hp:=hp^.nextoverloaded;
  268. end;
  269. end;
  270. begin
  271. { put only sub routines into the VMT }
  272. if psym(sym)^.typ=procsym then
  273. begin
  274. _name:=sym^.name;
  275. symcoll:=wurzel;
  276. while assigned(symcoll) do
  277. begin
  278. { does the symbol already exist in the list ? }
  279. if _name=symcoll^.name^ then
  280. begin
  281. { walk through all defs of the symbol }
  282. hp:=pprocsym(sym)^.definition;
  283. while assigned(hp) do
  284. begin
  285. { compare with all stored definitions }
  286. procdefcoll:=symcoll^.data;
  287. stored:=false;
  288. while assigned(procdefcoll) do
  289. begin
  290. { compare parameters }
  291. if equal_paras(procdefcoll^.data^.para1,hp^.para1,false) and
  292. (
  293. ((procdefcoll^.data^.options and povirtualmethod)<>0) or
  294. ((hp^.options and povirtualmethod)<>0)
  295. ) then
  296. begin { same parameters }
  297. { wenn sie gleich sind }
  298. { und eine davon virtual deklariert ist }
  299. { Fehler falls nur eine VIRTUAL }
  300. if (procdefcoll^.data^.options and povirtualmethod)<>
  301. (hp^.options and povirtualmethod) then
  302. begin
  303. { in classes, we hide the old method }
  304. if _c^.isclass then
  305. begin
  306. { warn only if it is the first time,
  307. we hide the method }
  308. if _c=hp^._class then
  309. Message1(parser_w_should_use_override,_c^.objname^+'.'+_name);
  310. newentry;
  311. exit;
  312. end
  313. else
  314. if _c=hp^._class then
  315. begin
  316. if (procdefcoll^.data^.options and povirtualmethod)<>0 then
  317. Message1(parser_w_overloaded_are_not_both_virtual,_c^.objname^+'.'+_name)
  318. else
  319. Message1(parser_w_overloaded_are_not_both_non_virtual,
  320. _c^.objname^+'.'+_name);
  321. newentry;
  322. exit;
  323. end;
  324. end
  325. else
  326. { the flags have to match }
  327. { except abstract and override }
  328. { only if both are virtual !! }
  329. if (procdefcoll^.data^.options and not(poabstractmethod or pooverridingmethod))<>
  330. (hp^.options and not(poabstractmethod or pooverridingmethod)) then
  331. Message1(parser_e_header_dont_match_forward,_c^.objname^+'.'+_name);
  332. { check, if the overridden directive is set }
  333. { (povirtualmethod is set! }
  334. { class ? }
  335. if _c^.isclass and
  336. ((hp^.options and pooverridingmethod)=0) then
  337. begin
  338. { warn only if it is the first time,
  339. we hide the method }
  340. if _c=hp^._class then
  341. Message1(parser_w_should_use_override,_c^.objname^+'.'+_name);
  342. newentry;
  343. exit;
  344. end;
  345. { error, if the return types aren't equal }
  346. if not(is_equal(procdefcoll^.data^.retdef,hp^.retdef)) and
  347. not((procdefcoll^.data^.retdef^.deftype=objectdef) and
  348. (hp^.retdef^.deftype=objectdef) and
  349. (pobjectdef(procdefcoll^.data^.retdef)^.isclass) and
  350. (pobjectdef(hp^.retdef)^.isclass) and
  351. (pobjectdef(hp^.retdef)^.isrelated(pobjectdef(procdefcoll^.data^.retdef)))) then
  352. Message1(parser_e_overloaded_methodes_not_same_ret,_c^.objname^+'.'+_name);
  353. { now set the number }
  354. hp^.extnumber:=procdefcoll^.data^.extnumber;
  355. { and exchange }
  356. procdefcoll^.data:=hp;
  357. stored:=true;
  358. end; { same parameters }
  359. procdefcoll:=procdefcoll^.next;
  360. end;
  361. { if it isn't saved in the list }
  362. { we create a new entry }
  363. if not(stored) then
  364. begin
  365. new(procdefcoll);
  366. procdefcoll^.data:=hp;
  367. procdefcoll^.next:=symcoll^.data;
  368. symcoll^.data:=procdefcoll;
  369. { if the method is virtual ... }
  370. if (hp^.options and povirtualmethod)<>0 then
  371. begin
  372. { ... it will get a number }
  373. hp^.extnumber:=nextvirtnumber;
  374. inc(nextvirtnumber);
  375. end;
  376. { check, if a method should be overridden }
  377. if (hp^.options and pooverridingmethod)<>0 then
  378. Message1(parser_e_nothing_to_be_overridden,_c^.objname^+'.'+_name);
  379. end;
  380. hp:=hp^.nextoverloaded;
  381. end;
  382. exit;
  383. end;
  384. symcoll:=symcoll^.next;
  385. end;
  386. newentry;
  387. end;
  388. end;
  389. procedure genvmt(_class : pobjectdef);
  390. procedure do_genvmt(p : pobjectdef);
  391. begin
  392. { start with the base class }
  393. if assigned(p^.childof) then
  394. do_genvmt(p^.childof);
  395. { walk through all public syms }
  396. { I had to change that to solve bug0260 (PM)}
  397. {_c:=_class;}
  398. _c:=p;
  399. { Florian, please check if you agree (PM) }
  400. p^.publicsyms^.foreach({$ifndef TP}@{$endif}eachsym);
  401. end;
  402. var
  403. symcoll : psymcoll;
  404. procdefcoll : pprocdefcoll;
  405. i : longint;
  406. begin
  407. wurzel:=nil;
  408. nextvirtnumber:=0;
  409. has_constructor:=false;
  410. has_virtual_method:=false;
  411. { generates a tree of all used methods }
  412. do_genvmt(_class);
  413. if has_virtual_method and not(has_constructor) then
  414. Message1(parser_w_virtual_without_constructor,_class^.objname^);
  415. { generates the VMT }
  416. { walk trough all numbers for virtual methods and search }
  417. { the method }
  418. for i:=0 to nextvirtnumber-1 do
  419. begin
  420. symcoll:=wurzel;
  421. { walk trough all symbols }
  422. while assigned(symcoll) do
  423. begin
  424. { walk trough all methods }
  425. procdefcoll:=symcoll^.data;
  426. while assigned(procdefcoll) do
  427. begin
  428. { writes the addresses to the VMT }
  429. { but only this which are declared as virtual }
  430. if procdefcoll^.data^.extnumber=i then
  431. begin
  432. if (procdefcoll^.data^.options and povirtualmethod)<>0 then
  433. begin
  434. { if a method is abstract, then is also the }
  435. { class abstract and it's not allow to }
  436. { generates an instance }
  437. if (procdefcoll^.data^.options and poabstractmethod)<>0 then
  438. begin
  439. _class^.options:=_class^.options or oo_is_abstract;
  440. datasegment^.concat(new(pai_const_symbol,
  441. initname('FPC_ABSTRACTERROR')));
  442. end
  443. else
  444. begin
  445. datasegment^.concat(new(pai_const_symbol,
  446. initname(procdefcoll^.data^.mangledname)));
  447. end;
  448. end;
  449. end;
  450. procdefcoll:=procdefcoll^.next;
  451. end;
  452. symcoll:=symcoll^.next;
  453. end;
  454. end;
  455. { disposes the above generated tree }
  456. symcoll:=wurzel;
  457. while assigned(symcoll) do
  458. begin
  459. wurzel:=symcoll^.next;
  460. stringdispose(symcoll^.name);
  461. procdefcoll:=symcoll^.data;
  462. while assigned(procdefcoll) do
  463. begin
  464. symcoll^.data:=procdefcoll^.next;
  465. dispose(procdefcoll);
  466. procdefcoll:=symcoll^.data;
  467. end;
  468. dispose(symcoll);
  469. symcoll:=wurzel;
  470. end;
  471. end;
  472. end.
  473. {
  474. $Log$
  475. Revision 1.13 1999-07-11 20:10:23 peter
  476. * merged
  477. Revision 1.12 1999/07/08 10:40:37 peter
  478. * merged
  479. Revision 1.11 1999/06/15 13:27:06 pierre
  480. * bug0260 fixed
  481. Revision 1.10.2.2 1999/07/11 20:07:38 peter
  482. * message crash fixed
  483. * no error if self is used with non-string message
  484. Revision 1.10.2.1 1999/07/08 10:38:32 peter
  485. * fixed insertint
  486. Revision 1.10 1999/06/02 22:44:07 pierre
  487. * previous wrong log corrected
  488. Revision 1.9 1999/06/02 22:25:33 pierre
  489. * changed $ifdef FPC @ into $ifndef TP
  490. Revision 1.8 1999/06/01 14:45:49 peter
  491. * @procvar is now always needed for FPC
  492. Revision 1.7 1999/05/27 19:44:30 peter
  493. * removed oldasm
  494. * plabel -> pasmlabel
  495. * -a switches to source writing automaticly
  496. * assembler readers OOPed
  497. * asmsymbol automaticly external
  498. * jumptables and other label fixes for asm readers
  499. Revision 1.6 1999/05/21 13:55:00 peter
  500. * NEWLAB for label as symbol
  501. Revision 1.5 1999/05/17 21:57:07 florian
  502. * new temporary ansistring handling
  503. Revision 1.4 1999/05/13 21:59:27 peter
  504. * removed oldppu code
  505. * warning if objpas is loaded from uses
  506. * first things for new deref writing
  507. Revision 1.3 1999/04/26 13:31:34 peter
  508. * release storenumber,double_checksum
  509. Revision 1.2 1999/04/21 09:43:37 peter
  510. * storenumber works
  511. * fixed some typos in double_checksum
  512. + incompatible types type1 and type2 message (with storenumber)
  513. Revision 1.1 1999/03/24 23:17:00 peter
  514. * fixed bugs 212,222,225,227,229,231,233
  515. }