hcgdata.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. symconst,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 (po_msgstr in hp^.procoptions) 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 (po_msgint in hp^.procoptions) 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^.symtable^.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^.symtable^.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 (po_virtualmethod in hp^.procoptions) 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^.proctypeoption=potype_constructor) then
  262. has_constructor:=true;
  263. { check, if a method should be overridden }
  264. if (po_overridingmethod in hp^.procoptions) 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. (po_virtualmethod in procdefcoll^.data^.procoptions) or
  294. (po_virtualmethod in hp^.procoptions)
  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 (po_virtualmethod in procdefcoll^.data^.procoptions)<>
  301. (po_virtualmethod in hp^.procoptions) then
  302. begin
  303. { in classes, we hide the old method }
  304. if _c^.is_class 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 (po_virtualmethod in procdefcoll^.data^.procoptions) 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^.proccalloptions<>hp^.proccalloptions) or
  330. (procdefcoll^.data^.proctypeoption<>hp^.proctypeoption) or
  331. ((procdefcoll^.data^.procoptions-[po_abstractmethod,po_overridingmethod])<>
  332. (hp^.procoptions-[po_abstractmethod,po_overridingmethod])) then
  333. Message1(parser_e_header_dont_match_forward,_c^.objname^+'.'+_name);
  334. { check, if the overridden directive is set }
  335. { (povirtualmethod is set! }
  336. { class ? }
  337. if _c^.is_class and
  338. not(po_overridingmethod in hp^.procoptions) then
  339. begin
  340. { warn only if it is the first time,
  341. we hide the method }
  342. if _c=hp^._class then
  343. Message1(parser_w_should_use_override,_c^.objname^+'.'+_name);
  344. newentry;
  345. exit;
  346. end;
  347. { error, if the return types aren't equal }
  348. if not(is_equal(procdefcoll^.data^.retdef,hp^.retdef)) and
  349. not((procdefcoll^.data^.retdef^.deftype=objectdef) and
  350. (hp^.retdef^.deftype=objectdef) and
  351. (pobjectdef(procdefcoll^.data^.retdef)^.is_class) and
  352. (pobjectdef(hp^.retdef)^.is_class) and
  353. (pobjectdef(hp^.retdef)^.is_related(pobjectdef(procdefcoll^.data^.retdef)))) then
  354. Message1(parser_e_overloaded_methodes_not_same_ret,_c^.objname^+'.'+_name);
  355. { now set the number }
  356. hp^.extnumber:=procdefcoll^.data^.extnumber;
  357. { and exchange }
  358. procdefcoll^.data:=hp;
  359. stored:=true;
  360. end; { same parameters }
  361. procdefcoll:=procdefcoll^.next;
  362. end;
  363. { if it isn't saved in the list }
  364. { we create a new entry }
  365. if not(stored) then
  366. begin
  367. new(procdefcoll);
  368. procdefcoll^.data:=hp;
  369. procdefcoll^.next:=symcoll^.data;
  370. symcoll^.data:=procdefcoll;
  371. { if the method is virtual ... }
  372. if (po_virtualmethod in hp^.procoptions) then
  373. begin
  374. { ... it will get a number }
  375. hp^.extnumber:=nextvirtnumber;
  376. inc(nextvirtnumber);
  377. end;
  378. { check, if a method should be overridden }
  379. if (po_overridingmethod in hp^.procoptions) then
  380. Message1(parser_e_nothing_to_be_overridden,_c^.objname^+'.'+_name);
  381. end;
  382. hp:=hp^.nextoverloaded;
  383. end;
  384. exit;
  385. end;
  386. symcoll:=symcoll^.next;
  387. end;
  388. newentry;
  389. end;
  390. end;
  391. procedure genvmt(_class : pobjectdef);
  392. procedure do_genvmt(p : pobjectdef);
  393. begin
  394. { start with the base class }
  395. if assigned(p^.childof) then
  396. do_genvmt(p^.childof);
  397. { walk through all public syms }
  398. { I had to change that to solve bug0260 (PM)}
  399. {_c:=_class;}
  400. _c:=p;
  401. { Florian, please check if you agree (PM) }
  402. p^.symtable^.foreach({$ifndef TP}@{$endif}eachsym);
  403. end;
  404. var
  405. symcoll : psymcoll;
  406. procdefcoll : pprocdefcoll;
  407. i : longint;
  408. begin
  409. wurzel:=nil;
  410. nextvirtnumber:=0;
  411. has_constructor:=false;
  412. has_virtual_method:=false;
  413. { generates a tree of all used methods }
  414. do_genvmt(_class);
  415. if has_virtual_method and not(has_constructor) then
  416. Message1(parser_w_virtual_without_constructor,_class^.objname^);
  417. { generates the VMT }
  418. { walk trough all numbers for virtual methods and search }
  419. { the method }
  420. for i:=0 to nextvirtnumber-1 do
  421. begin
  422. symcoll:=wurzel;
  423. { walk trough all symbols }
  424. while assigned(symcoll) do
  425. begin
  426. { walk trough all methods }
  427. procdefcoll:=symcoll^.data;
  428. while assigned(procdefcoll) do
  429. begin
  430. { writes the addresses to the VMT }
  431. { but only this which are declared as virtual }
  432. if procdefcoll^.data^.extnumber=i then
  433. begin
  434. if (po_virtualmethod in procdefcoll^.data^.procoptions) then
  435. begin
  436. { if a method is abstract, then is also the }
  437. { class abstract and it's not allow to }
  438. { generates an instance }
  439. if (po_abstractmethod in procdefcoll^.data^.procoptions) then
  440. begin
  441. {$ifdef INCLUDEOK}
  442. include(_class^.objectoptions,oo_has_abstract);
  443. {$else}
  444. _class^.objectoptions:=_class^.objectoptions+[oo_has_abstract];
  445. {$endif}
  446. datasegment^.concat(new(pai_const_symbol,initname('FPC_ABSTRACTERROR')));
  447. end
  448. else
  449. begin
  450. datasegment^.concat(new(pai_const_symbol,
  451. initname(procdefcoll^.data^.mangledname)));
  452. end;
  453. end;
  454. end;
  455. procdefcoll:=procdefcoll^.next;
  456. end;
  457. symcoll:=symcoll^.next;
  458. end;
  459. end;
  460. { disposes the above generated tree }
  461. symcoll:=wurzel;
  462. while assigned(symcoll) do
  463. begin
  464. wurzel:=symcoll^.next;
  465. stringdispose(symcoll^.name);
  466. procdefcoll:=symcoll^.data;
  467. while assigned(procdefcoll) do
  468. begin
  469. symcoll^.data:=procdefcoll^.next;
  470. dispose(procdefcoll);
  471. procdefcoll:=symcoll^.data;
  472. end;
  473. dispose(symcoll);
  474. symcoll:=wurzel;
  475. end;
  476. end;
  477. end.
  478. {
  479. $Log$
  480. Revision 1.14 1999-08-03 22:02:52 peter
  481. * moved bitmask constants to sets
  482. * some other type/const renamings
  483. Revision 1.13 1999/07/11 20:10:23 peter
  484. * merged
  485. Revision 1.12 1999/07/08 10:40:37 peter
  486. * merged
  487. Revision 1.11 1999/06/15 13:27:06 pierre
  488. * bug0260 fixed
  489. Revision 1.10.2.2 1999/07/11 20:07:38 peter
  490. * message crash fixed
  491. * no error if self is used with non-string message
  492. Revision 1.10.2.1 1999/07/08 10:38:32 peter
  493. * fixed insertint
  494. Revision 1.10 1999/06/02 22:44:07 pierre
  495. * previous wrong log corrected
  496. Revision 1.9 1999/06/02 22:25:33 pierre
  497. * changed $ifdef FPC @ into $ifndef TP
  498. Revision 1.8 1999/06/01 14:45:49 peter
  499. * @procvar is now always needed for FPC
  500. Revision 1.7 1999/05/27 19:44:30 peter
  501. * removed oldasm
  502. * plabel -> pasmlabel
  503. * -a switches to source writing automaticly
  504. * assembler readers OOPed
  505. * asmsymbol automaticly external
  506. * jumptables and other label fixes for asm readers
  507. Revision 1.6 1999/05/21 13:55:00 peter
  508. * NEWLAB for label as symbol
  509. Revision 1.5 1999/05/17 21:57:07 florian
  510. * new temporary ansistring handling
  511. Revision 1.4 1999/05/13 21:59:27 peter
  512. * removed oldppu code
  513. * warning if objpas is loaded from uses
  514. * first things for new deref writing
  515. Revision 1.3 1999/04/26 13:31:34 peter
  516. * release storenumber,double_checksum
  517. Revision 1.2 1999/04/21 09:43:37 peter
  518. * storenumber works
  519. * fixed some typos in double_checksum
  520. + incompatible types type1 and type2 message (with storenumber)
  521. Revision 1.1 1999/03/24 23:17:00 peter
  522. * fixed bugs 212,222,225,227,229,231,233
  523. }