hcgdata.pas 21 KB

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