kalyptusCxxToPas.pm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. package kalyptusCxxToPas;
  2. use File::Path;
  3. use File::Basename;
  4. use Carp;
  5. use Ast;
  6. use kdocAstUtil;
  7. use kdocUtil;
  8. use Iter;
  9. use kalyptusDataDict;
  10. use strict;
  11. no strict "subs";
  12. use vars qw/ @clist $host $who $now $gentext %functionId $docTop @functions
  13. $lib $rootnode $outputdir $opt $debug $typeprefix $eventHandlerCount $constructorCount *CLASS *HEADER *QTCTYPES *KDETYPES /;
  14. my @qtcfunctions;
  15. my %inheritance;
  16. my @typeenums;
  17. my %pasopmap = (
  18. '<<' => ' shl ',
  19. '>>' => ' shr ',
  20. '|' => ' or ',
  21. '&' => ' and '
  22. );
  23. BEGIN
  24. {
  25. @clist = ();
  26. # Page footer
  27. $who = kdocUtil::userName();
  28. $host = kdocUtil::hostName();
  29. $now = localtime;
  30. $gentext = "$who\@$host on $now, using kalyptus $main::Version.";
  31. $docTop =<<EOF
  32. -------------------
  33. begin : $now
  34. copyright : (C) 2000-2001 Lost Highway Ltd. All rights reserved.
  35. email : Lost_Highway\@tipitina.demon.co.uk
  36. generated by : $gentext
  37. ***************************************************************************
  38. ***************************************************************************
  39. * *
  40. * This library is free software; you can redistribute it and/or modify *
  41. * it under the terms of the GNU Library General Public License as *
  42. * published by the Free Software Foundation; either version 2 of the *
  43. * License, or (at your option) any later version. *
  44. * *
  45. ***************************************************************************
  46. EOF
  47. }
  48. sub writeDoc
  49. {
  50. ( $lib, $rootnode, $outputdir, $opt ) = @_;
  51. $debug = $main::debuggen;
  52. mkpath( $outputdir ) unless -f $outputdir;
  53. my $file = "$outputdir/qt.pp";
  54. open( QTCTYPES, ">$file" ) || die "Couldn't create $file\n";
  55. print QTCTYPES "{***************************************************************************\n";
  56. print QTCTYPES $docTop,"}\n\n";
  57. print QTCTYPES "unit qt;\n\n";
  58. print QTCTYPES " interface\n\n";
  59. print QTCTYPES " {\$i qt_extra.inc}\n\n";
  60. print QTCTYPES "// typedef void (*qt_UserDataCallback)(void *, void *);\n";
  61. print QTCTYPES "// typedef int (*qt_eventFilter)(qt_QObject*,qt_QEvent*);\n";
  62. print QTCTYPES "// typedef int (*qt_EventDelegate)(void *, char *, void *, char *);\n";
  63. print QTCTYPES "// extern qt_EventDelegate Qt_EventDelegate;\n";
  64. $file = "$outputdir/kde.pp";
  65. open( KDETYPES, ">$file" ) || die "Couldn't create $file\n";
  66. print KDETYPES "{***************************************************************************\n";
  67. print KDETYPES " kde_types.pas - description\n";
  68. print KDETYPES $docTop,"}\n\n";
  69. print KDETYPES "unit kde;\n\n";
  70. print KDETYPES " interface\n\n";
  71. print KDETYPES " type\n";
  72. # Document all compound nodes
  73. Iter::LocalCompounds( $rootnode, sub { writeClassDoc( shift ); } );
  74. # write all classes sorted by inheritance
  75. # my @inheritance_sorted = sort {
  76. # if ($inheritance{$a} eq "") {
  77. # if ($inheritance{$b} eq "") {
  78. # return 0;
  79. # } else {
  80. # return -1;
  81. # }
  82. # } else {
  83. # if ($inheritance{$b} eq "") {
  84. # return 1;
  85. # }
  86. # }
  87. # my $parent=$inheritance{$a};
  88. # while ($parent ne "") {
  89. # if ($parent eq $b) {
  90. # return 1;
  91. # }
  92. # $parent=$inheritance{$parent};
  93. # }
  94. # $parent=$inheritance{$b};
  95. # while ($parent ne "") {
  96. # if ($parent eq $a) {
  97. # return -1;
  98. # }
  99. # $parent=$inheritance{$parent};
  100. # }
  101. # return 0;
  102. # } keys %inheritance;
  103. # for my $key (@inheritance_sorted) {
  104. print "Start writing classes\n";
  105. while (keys %inheritance>0) {
  106. my $key;
  107. my $value;
  108. while ( ($key, $value) = each %inheritance) {
  109. if (!(exists $inheritance{$value}) || ($value eq "")) {
  110. if ($value eq "") {
  111. print QTCTYPES " ",$key,"H = class end;\n";
  112. } else {
  113. print QTCTYPES " ",$key,"H = class(",$value,"H) end;\n";
  114. }
  115. delete $inheritance{$key};
  116. }
  117. }
  118. }
  119. print "Finished writing classes\n";
  120. print QTCTYPES "\n";
  121. # write enums
  122. for my $enum (@typeenums)
  123. {
  124. print QTCTYPES $enum;
  125. }
  126. print QTCTYPES "\n";
  127. for my $func (@qtcfunctions)
  128. {
  129. print QTCTYPES $func,"\n";
  130. }
  131. print QTCTYPES "\nimplementation\nend.\n";
  132. print KDETYPES "\nimplementation\n\nend.\n";
  133. close QTCTYPES;
  134. close KDETYPES;
  135. }
  136. =head2 writeClassDoc
  137. Write documentation for one compound node.
  138. =cut
  139. sub writeClassDoc
  140. {
  141. my( $node ) = @_;
  142. print "Enter: $node->{astNodeName}\n" if $debug;
  143. if( exists $node->{ExtSource} ) {
  144. warn "Trying to write doc for ".$node->{AstNodeName}.
  145. " from ".$node->{ExtSource}."\n";
  146. return;
  147. }
  148. my $typeName = $node->{astNodeName}."*";
  149. if ( kalyptusDataDict::pastypemap($typeName) eq "" ) {
  150. $typeprefix = ($typeName =~ /^Q/ ? "qt_" : "kde_");
  151. kalyptusDataDict::setpastypemap($typeName, $typeprefix.$node->{astNodeName}."*");
  152. print "'$typeName' => '$typeprefix$typeName',\n";
  153. } elsif ( kalyptusDataDict::ctypemap($typeName) =~ /^qt_/ ) {
  154. $typeprefix = "qt_";
  155. } elsif ( kalyptusDataDict::ctypemap($typeName) =~ /^kde_/ ) {
  156. $typeprefix = "kde_";
  157. } else {
  158. $typeprefix = "";
  159. }
  160. my $file = "$outputdir/".join("__", kdocAstUtil::heritage($node))."h.inc";
  161. my $docnode = $node->{DocNode};
  162. my @list = ();
  163. my $version = undef;
  164. my $author = undef;
  165. # if( $#{$node->{Kids}} < 0 || $node->{Access} eq "private" || exists $node->{Tmpl} ) {
  166. if( $#{$node->{Kids}} < 0 || $node->{Access} eq "private") {
  167. return;
  168. }
  169. open( HEADER, ">".lc("$file") ) || die "Couldn't create $file\n";
  170. $file =~ s/\h.inc/.cpp/;
  171. open( CLASS, ">".lc("$file") ) || die "Couldn't create $file\n";
  172. # Header
  173. my $short = "";
  174. my $extra = "";
  175. # ancestors
  176. my @ancestors = ();
  177. my $parent = "";
  178. Iter::Ancestors( $node, $rootnode, undef, undef,
  179. sub { # print
  180. my ( $ances, $name, $type, $template ) = @_;
  181. push @ancestors, $name;
  182. },
  183. undef
  184. );
  185. if ($#ancestors >= 0) {
  186. # nested classes aren't possible in Object Pascal
  187. # they are moved to level 1
  188. @ancestors[0] =~ s/[^:]*::([^:]*)/$1/;
  189. $inheritance{$node->{astNodeName}}=@ancestors[0];
  190. } else {
  191. $inheritance{$node->{astNodeName}}="";
  192. }
  193. if ( kalyptusDataDict::pastypemap($typeName) =~ /^kde_/ ) {
  194. # @qtcfunctions[$#qtcfunctions+1]=" {\$i ".$node->{astNodeName}."h.inc}\n";
  195. } else {
  196. @qtcfunctions[$#qtcfunctions+1]=" {\$i ".$node->{astNodeName}."h.inc}\n";
  197. }
  198. print HEADER "{***************************************************************************\n";
  199. print HEADER " ", $node->{astNodeName},".pas - description\n";
  200. print HEADER $docTop,"}\n\n";
  201. print CLASS "/***************************************************************************\n";
  202. print CLASS " ", $typeprefix, $node->{astNodeName}, ".cpp - description\n";
  203. print CLASS $docTop,"*/\n\n";
  204. print CLASS "extern \"C\" {\n#include \"", $typeprefix, $node->{astNodeName}, ".h\"\n}\n\n";
  205. my $sourcename = $node->{Source}->{astNodeName};
  206. if ( $sourcename =~ m!.*(dom|kabc|kdeprint|kdesu|kio|kjs|kparts|ktexteditor|libkmid)/([^/]*$)! ) {
  207. $sourcename = $1."/".$2;
  208. } else {
  209. $sourcename =~ s!.*/([^/]*$)!$1!;
  210. }
  211. print CLASS "#include <",$sourcename , ">\n\n";
  212. $constructorCount = 0;
  213. Iter::MembersByType ( $node,
  214. sub { print HEADER "", $_[0], ""; print CLASS "", $_[0], ""; },
  215. sub { my ($node, $kid ) = @_;
  216. preParseMember( $node, $kid );
  217. },
  218. sub { print HEADER ""; print CLASS ""; }
  219. );
  220. if ( ! exists $node->{Pure} && $constructorCount > 0 ) {
  221. print CLASS "class ", $node->{astNodeName}, "Bridge : public ", kalyptusDataDict::addNamespace($node->{astNodeName}), "\n{\npublic:\n";
  222. Iter::MembersByType ( $node,
  223. sub { print HEADER "", $_[0], ""; print CLASS "", $_[0], ""; },
  224. sub { my ($node, $kid ) = @_;
  225. generateBridgeClass( $node, $kid );
  226. },
  227. sub { print HEADER ""; print CLASS ""; }
  228. );
  229. generateBridgeEventHandlers($node);
  230. print CLASS "};\n\n";
  231. }
  232. %functionId = ();
  233. $eventHandlerCount = 0;
  234. Iter::MembersByType ( $node,
  235. sub { print HEADER "", $_[0], ""; print CLASS "", $_[0], ""; },
  236. sub { my ($node, $kid ) = @_;
  237. listMember( $node, $kid );
  238. },
  239. sub { print HEADER ""; print CLASS ""; }
  240. );
  241. if ( $#ancestors > 0 ) {
  242. # 'type transfer' functions to cast for correct use of multiple inheritance
  243. foreach my $ancestor (@ancestors) {
  244. print HEADER "\n{\*\* Casts a '$typeprefix", $node->{astNodeName}, " *' to a '", kalyptusDataDict::pastypemap($ancestor."\*"), "' \}\n";
  245. print HEADER "function ", $typeprefix, $node->{astNodeName}, "_", $ancestor;
  246. print HEADER "(", $typeprefix, "instPointer : ",$node->{astNodeName}, "H) : ",kalyptusDataDict::pastypemap($ancestor."\*"),";cdecl;\n";
  247. print CLASS kalyptusDataDict::ctypemap($ancestor."\*"), " ", $typeprefix, $node->{astNodeName}, "_", $ancestor;
  248. print CLASS "(", $typeprefix, $node->{astNodeName}, "* instPointer){\n";
  249. print CLASS "\treturn (", kalyptusDataDict::ctypemap($ancestor."\*"), ") (", $ancestor, " *) (", $node->{astNodeName}, " *) instPointer;\n}\n";
  250. }
  251. }
  252. $file =~ s/\.cpp/.inc/;
  253. open(BODY, ">".lc("$file") ) || die "Couldn't create $file\n";
  254. for my $func (@functions)
  255. {
  256. print BODY $func,"\n";
  257. }
  258. @functions=();
  259. close BODY;
  260. close CLASS;
  261. close HEADER;
  262. }
  263. sub preParseMember
  264. {
  265. my( $class, $m ) = @_;
  266. my $name = $m->{astNodeName};
  267. if( $m->{NodeType} eq "method" ) {
  268. # A JBridge class will only be generated if there is at least one
  269. # public or protected constructor
  270. if ( $name eq $class->{astNodeName} && $m->{Access} ne "private" ) {
  271. $constructorCount++;
  272. }
  273. }
  274. }
  275. sub generateBridgeEventHandlers
  276. {
  277. my( $node ) = @_;
  278. my %allmem = ();
  279. my $key;
  280. my $m;
  281. my $name;
  282. kdocAstUtil::allMembers( \%allmem, $node );
  283. foreach $key (keys (%allmem)) {
  284. $m = $allmem{$key};
  285. $name = $m->{astNodeName} ;
  286. my $type = $m->{NodeType};
  287. my $docnode = $m->{DocNode};
  288. my $pasparams = $m->{Params};
  289. my $parent = $m->{Parent};
  290. my $cplusplusparams;
  291. if( $type eq "method" && $m->{Access} eq "protected" && $name =~ /.*Event$/
  292. && $name !~ /qwsEvent/ && $name !~ /x11Event/ && $name !~ /winEvent/ && $name !~ /macEvent/ && $name !~ /movableDropEvent/ ) {
  293. $pasparams =~ s/=\s*[-\"\w]*//g;
  294. $pasparams =~ s/\s+/ /g;
  295. $pasparams =~ s/\s*([,\*\&])\s*/$1 /g;
  296. $pasparams =~ s/^\s*void\s*$//;
  297. my $argId = 0;
  298. my @cargs = kdocUtil::splitUnnested(",", $pasparams);
  299. my $cplusplusargs = "";
  300. foreach my $arg ( @cargs ) {
  301. my $argType;
  302. my $cargType;
  303. $arg =~ s/\s*([^\s].*[^\s])\s*/$1/;
  304. if ( $arg =~ /(.*)\s+(\w+)$/ ) {
  305. $argType = $1;
  306. $arg = $2;
  307. } else {
  308. $argType = $arg;
  309. $argId++;
  310. $arg = "arg".$argId;
  311. }
  312. $cplusplusparams .= $argType." ".$arg.", ";
  313. $cplusplusargs .= $arg.", ";
  314. }
  315. $pasparams =~ s/; $//;
  316. $cplusplusparams =~ s/, $//;
  317. $cplusplusargs =~ s/, $//;
  318. $eventHandlerCount++;
  319. my $eventType = $cplusplusparams;
  320. $eventType =~ s/(.*)\*.*$/$1/;
  321. print CLASS "\tvoid $name(", $cplusplusparams, ") {\n",
  322. "\t\tif (Qt_EventDelegate == 0L || !(*Qt_EventDelegate)(this, \"", $name, "\", $cplusplusargs, \"$eventType\")) {\n",
  323. "\t\t\t", $parent->{astNodeName}, "::", $name, "($cplusplusargs);\n",
  324. "\t\t}\n",
  325. "\t\treturn;\n\t}\n";
  326. }
  327. }
  328. }
  329. sub changehex($)
  330. {
  331. my $value = @_[0];
  332. $value =~ s/0x/\$/;
  333. return $value;
  334. }
  335. sub generateBridgeClass
  336. {
  337. my( $class, $m ) = @_;
  338. my $name;
  339. my $function;
  340. $name = $m->{astNodeName} ;
  341. my $type = $m->{NodeType};
  342. my $docnode = $m->{DocNode};
  343. if( $type eq "method" && $m->{Access} ne "private" && $m->{Access} ne "private_slots" && $m->{Access} ne "signals" ) {
  344. if ( $m->{ReturnType} =~ /[<>]/ || $m->{Params} =~ /[<>]/ || $m->{Params} =~ /Impl/) {
  345. # print "template based method not converted: ", $m->{ReturnType}, " ", $m->{Params}, "\n";
  346. return;
  347. }
  348. my $returnType = $m->{ReturnType};
  349. my $pasparams = $m->{Params};
  350. my $cplusplusparams;
  351. # TODO port to $m->{ParamList}
  352. $pasparams =~ s/=\s*(("[^\"]*")|(\'.\')|(([-\w:.]*)\s*(\|\s*[-\w]*)*(\(\w*\))?))//g;
  353. $pasparams =~ s/\s+/ /g;
  354. $pasparams =~ s/\s*([,\*\&])\s*/$1 /g;
  355. $pasparams =~ s/^\s*void\s*$//;
  356. $pasparams =~ s/^\s*$//;
  357. my $argId = 0;
  358. my @cargs = kdocUtil::splitUnnested(",", $pasparams);
  359. $pasparams = "";
  360. foreach my $arg ( @cargs ) {
  361. my $argType;
  362. my $cargType;
  363. $arg =~ s/\s*([^\s].*[^\s])\s*/$1/;
  364. if ( $arg =~ /(.*)\s+(\w+)$/ ) {
  365. $argType = $1;
  366. $arg = $2;
  367. } else {
  368. $argType = $arg;
  369. $argId++;
  370. $arg = "arg".$argId;
  371. }
  372. $cplusplusparams .= $argType." ".$arg.", ";
  373. $pasparams .= $arg.", ";
  374. }
  375. $pasparams =~ s/, $//;
  376. $cplusplusparams =~ s/, $//;
  377. my $flags = $m->{Flags};
  378. if ( !defined $flags ) {
  379. warn "Method ".$m->{astNodeName}. " has no flags\n";
  380. }
  381. my $extra = "";
  382. $extra .= "static " if $flags =~ "s";
  383. if ( $name =~ /operator/ ) {
  384. return;
  385. }
  386. if ( $name eq $class->{astNodeName} ) {
  387. if ( $returnType =~ "~" ) {
  388. print CLASS "\t~", $name, "Bridge() {}\n";
  389. } else {
  390. print CLASS $extra,
  391. "\t", $name, "Bridge(", $cplusplusparams, ") : $name($pasparams) {}\n";
  392. }
  393. } elsif( $type eq "method" && $m->{Access} eq "protected" && $name =~ /.*Event$/ ) {
  394. ;
  395. } elsif( $m->{Access} =~ /^protected/ ){
  396. if ( $returnType =~ "void" ) {
  397. print CLASS "\tvoid protected_$name(", $cplusplusparams, ") {\n",
  398. "\t\t", $class->{astNodeName}, "::$name($pasparams);\n",
  399. "\t\treturn;\n\t}\n";
  400. } else {
  401. print CLASS "\t$returnType protected_$name(", $cplusplusparams, ") {\n",
  402. "\t\treturn ($returnType) ", $class->{astNodeName}, "::$name($pasparams);\n\t}\n";
  403. }
  404. }
  405. }
  406. }
  407. sub listMember
  408. {
  409. my( $class, $m ) = @_;
  410. my $name;
  411. my $function;
  412. $name = $m->{astNodeName} ;
  413. my $type = $m->{NodeType};
  414. my $docnode = $m->{DocNode};
  415. if ( $m->{ReturnType} =~ /~/ ) {
  416. $name = "~".$name;
  417. }
  418. if ( $functionId{$name} eq "" ) {
  419. $functionId{$name} = 0;
  420. $function = $name;
  421. } else {
  422. $functionId{$name}++;
  423. $function = $name.$functionId{$name};
  424. }
  425. $function =~ s/~//;
  426. if ($m->{ReturnType} eq "typedef") {
  427. } elsif( $type eq "method" && $m->{Access} ne "private" && $m->{Access} ne "private_slots" && $m->{Access} ne "signals" ) {
  428. if ( $m->{ReturnType} =~ /[<>]/ || $m->{Params} =~ /[<>]/ || $m->{Params} =~ /\.\.\./ || $m->{Params} =~ /Impl/
  429. || $m->{ReturnType} =~ /QAuBucket/ || $m->{Params} =~ /QAuBucket/
  430. || $m->{ReturnType} =~ /QMember/ || $m->{Params} =~ /QMember/ ) {
  431. return;
  432. }
  433. my $returnType = $m->{ReturnType};
  434. $returnType =~ s/const\s*//;
  435. $returnType =~ s/\s*([,\*\&])\s*/$1/;
  436. $returnType =~ s/^\s*//;
  437. $returnType =~ s/\s*$//;
  438. # map result type
  439. my $cplusplusreturntype=$returnType;
  440. if (kalyptusDataDict::pastypemap($returnType) ne "") {
  441. $cplusplusreturntype=kalyptusDataDict::ctypemap($returnType)
  442. }
  443. if ( $returnType ne "" && kalyptusDataDict::pastypemap($returnType) eq "" ) {
  444. print "'$returnType' => '$typeprefix$returnType',\n";
  445. } else {
  446. $returnType = kalyptusDataDict::pastypemap($returnType);
  447. }
  448. $returnType =~ s/var /P/;
  449. if ($returnType eq "var" || $returnType eq "const") {
  450. $returnType="pointer";
  451. }
  452. # TODO port to $m->{ParamList}
  453. my $pasparams = $m->{Params};
  454. my $cplusplusparams;
  455. my $argMod = "";
  456. my $cplusplusargs = "";
  457. $pasparams =~ s/\s+/ /g;
  458. $pasparams =~ s/\s*([,\*\&])\s*/$1 /g;
  459. $pasparams =~ s/^\s*void\s*$//;
  460. my $argId = 0;
  461. my @cargs = kdocUtil::splitUnnested(",", $pasparams);
  462. $pasparams = "";
  463. foreach my $arg ( @cargs ) {
  464. my $argType;
  465. my $cargType;
  466. if ( $arg =~ /^\s*$/ ) {
  467. next;
  468. }
  469. # A '<arg> = <value>' default parameter
  470. $arg =~ s/\s*([^\s].*[^\s])\s*/$1/;
  471. $arg =~ s/(\w+)\[\]/\* $1/;
  472. $arg =~ s/=\s*(("[^\"]*")|(\'.\')|(([-\w:.]*)\s*(\|\s*[-\w]*)*(\(\w*\))?))//;
  473. if ( $arg =~ /^(.*)\s+(\w+)\s*$/ ) {
  474. $argType = $1;
  475. # prepend with _ to avoid name conflicts
  476. $arg = "_".$2;
  477. } else {
  478. $argType = $arg;
  479. $argId++;
  480. $arg = "arg".$argId;
  481. }
  482. $arg =~ s/^id$/identifier/;
  483. $argType =~ s/\s*([^\s].*[^\s])\s*/$1/;
  484. $argType =~ s/\s*const//g;
  485. $argType =~ s/^\s*//;
  486. $argType =~ s/([\*\&])\s*([\*\&])/$1$2/;
  487. # print $argType."\n";
  488. $cargType = kalyptusDataDict::pastypemap($argType);
  489. # print $cargType."\n";
  490. if ( $argType =~ /^[A-Z][^:]*$/ && kalyptusDataDict::ctypemap($argType) eq "int" &&
  491. kalyptusDataDict::ctypemap($class->{astNodeName}."::".$argType) ne "" ) {
  492. $cplusplusargs .= "(".$class->{astNodeName}."::".$argType.")";
  493. } elsif ( $argType =~ /^\s*WFlags\s*$/ ) {
  494. $cplusplusargs .= "(QWidget::WFlags)";
  495. } elsif ( $argType =~ /^\s*ArrowType\s*$/ ) {
  496. $cplusplusargs .= "(Qt::ArrowType)";
  497. } elsif ( $argType =~ /^\s*Orientation\s*$/ ) {
  498. $cplusplusargs .= "(Qt::Orientation)";
  499. } elsif ( $argType =~ /^\s*BrushStyle\s*$/ ) {
  500. $cplusplusargs .= "(Qt::BrushStyle)";
  501. } elsif ( $argType =~ /^\s*BGMode\s*$/ ) {
  502. $cplusplusargs .= "(Qt::BGMode)";
  503. } elsif ( $argType =~ /^\s*PenCapStyle\s*$/ ) {
  504. $cplusplusargs .= "(Qt::PenCapStyle)";
  505. } elsif ( $argType =~ /^\s*PenStyle\s*$/ ) {
  506. $cplusplusargs .= "(Qt::PenStyle)";
  507. } elsif ( $argType =~ /^\s*PenJoinStyle\s*$/ ) {
  508. $cplusplusargs .= "(Qt::PenJoinStyle)";
  509. } elsif ( $argType =~ /^\s*RasterOp\s*$/ ) {
  510. $cplusplusargs .= "(Qt::RasterOp)";
  511. } elsif ( $argType =~ /^\s*TextFormat\s*$/ ) {
  512. $cplusplusargs .= "(Qt::TextFormat)";
  513. } elsif ( $argType =~ /^\s*QDragMode\s*$/ ) {
  514. $cplusplusargs .= "(QDragObject::DragMode)";
  515. } elsif ( $argType =~ /^\s*GUIStyle\s*$/ ) {
  516. $cplusplusargs .= "(Qt::GUIStyle)";
  517. } elsif ( $argType =~ /^\s*Type\s*$/ ) {
  518. $cplusplusargs .= "(QEvent::Type)";
  519. } else {
  520. $cplusplusargs .= "(".kalyptusDataDict::addNamespace($argType).")";
  521. }
  522. if ( $cargType eq "" ) {
  523. print "'$argType' => '$typeprefix$argType',\n";
  524. $argType =~ s/\&.*$//;
  525. $pasparams .= $argMod." ".$arg." : ".$argType."; ";
  526. $cplusplusparams .= $argType." ".$arg.", ";
  527. } else {
  528. $cplusplusparams .= kalyptusDataDict::ctypemap($argType)." ".$arg.", ";
  529. my $pasargType=kalyptusDataDict::pastypemap($argType);
  530. if ($pasargType =~ s/^var//) {
  531. $argMod="var";
  532. } else {
  533. $argMod="";
  534. }
  535. # formal parameter?
  536. if ($pasargType eq "" && ($argMod eq "var" || $argMod eq "const")) {
  537. $pasparams .= $argMod." ".$arg."; ";
  538. } else {
  539. $pasparams .= $argMod." ".$arg." : ".$pasargType."; ";
  540. }
  541. }
  542. if ( ( $cargType =~ /^qt_.*\*/ || $cargType =~ /^kde_.*\*/ ) && $argType =~ /^[^\*]*$/ ) {
  543. $argType =~ s/^(.*)\&.*$/$1/;
  544. $cplusplusargs .= "* (".kalyptusDataDict::addNamespace($argType)."*)".$arg.", ";
  545. } else {
  546. $cplusplusargs .= $arg.", ";
  547. }
  548. }
  549. $pasparams =~ s/; $//;
  550. $cplusplusparams =~ s/, $//;
  551. $cplusplusargs =~ s/, $//;
  552. my $flags = $m->{Flags};
  553. if ( !defined $flags ) {
  554. warn "Method ".$m->{astNodeName}. " has no flags\n";
  555. }
  556. my $extra = "";
  557. $extra .= "static " if $flags =~ "s";
  558. if ( $name =~ /operator/ ) {
  559. return;
  560. }
  561. if ( $m->{Access} =~ /protected/ && $name ne $class->{astNodeName} ) {
  562. if ( $class->{Pure} ) {
  563. return;
  564. }
  565. $name = "protected_".$name;
  566. }
  567. if ( $name eq $class->{astNodeName} && $class->{Pure} ) {
  568. return;
  569. }
  570. if ( defined $docnode ) {
  571. if ( defined $docnode->{Text} ) {
  572. print HEADER "\n{* ";
  573. my $node;
  574. my $line;
  575. foreach $node ( @{$docnode->{Text}} ) {
  576. next if $node->{NodeType} ne "DocText";
  577. $line = $node->{astNodeName};
  578. print HEADER $line, "\n";
  579. }
  580. print HEADER "}\n";
  581. }
  582. }
  583. # constructor
  584. if ( $name eq $class->{astNodeName} ) {
  585. print HEADER $extra,
  586. "function ", $typeprefix, "new_", $function,
  587. "(", $pasparams, ") : ".$name."H;cdecl;\n";
  588. @functions[$#functions+1]="function ". $typeprefix. "new_". $function.
  589. "(".$pasparams.") : ".$name."H;cdecl;external name ".
  590. "'".$typeprefix. "new_". $function. "';";
  591. print CLASS $extra,
  592. $typeprefix, $name, " * ", $typeprefix, "new_", $function,
  593. "(", $cplusplusparams, "){\n",
  594. "\treturn (", $typeprefix, $name, " *) new ", $name, "Bridge(", $cplusplusargs, ");\n}\n";
  595. # destructor
  596. } elsif ( $returnType =~ /~/ ) {
  597. print HEADER $extra,
  598. "procedure ", $typeprefix, "del_", $function,
  599. "(p : ", $class->{astNodeName}, "H);cdecl;\n";
  600. @functions[$#functions+1]="procedure ".$typeprefix. "del_". $function.
  601. "(p : ".$class->{astNodeName}."H);cdecl;external name ".
  602. "'". $typeprefix. "del_". $function."';";
  603. if (exists $class->{Pure} || $constructorCount == 0) {
  604. print CLASS $extra,
  605. "void ", $typeprefix, "del_", $function,
  606. "( ", $typeprefix, $class->{astNodeName}, "* p ){\n\tdelete (", kalyptusDataDict::addNamespace($class->{astNodeName}), "*) p;\n}\n";
  607. } else {
  608. print CLASS $extra,
  609. "void ", $typeprefix, "del_", $function,
  610. "( ", $typeprefix, $class->{astNodeName}, "* p ){\n\tdelete (", $class->{astNodeName}, "Bridge*) p;\n}\n";
  611. }
  612. } else {
  613. if ( $name =~ /.*Event$/ ) {
  614. return;
  615. }
  616. # Class or instance method
  617. my $selfstring;
  618. if ( $extra =~ /static/ ) {
  619. if ( exists $class->{Pure} || $constructorCount == 0 ) {
  620. $selfstring = kalyptusDataDict::addNamespace($class->{astNodeName})."::";
  621. } else {
  622. $selfstring = $class->{astNodeName}."Bridge::";
  623. }
  624. if ($returnType eq "void") {
  625. print HEADER "procedure ",
  626. $class->{astNodeName}, "_", $function,
  627. "(", $pasparams, ");cdecl;\n";
  628. @functions[$#functions+1]="procedure ".
  629. $class->{astNodeName}."_".$function.
  630. "(".$pasparams.");cdecl;external name ".
  631. "'".$typeprefix . $class->{astNodeName} . "_".$function."';";
  632. } else {
  633. print HEADER "function ",
  634. $class->{astNodeName}, "_", $function,
  635. "(", $pasparams, ") : ".$returnType.";cdecl;\n";
  636. @functions[$#functions+1]="function ".
  637. $class->{astNodeName}."_".$function.
  638. "(".$pasparams.") : ".$returnType.";cdecl;external name ".
  639. "'".$typeprefix . $class->{astNodeName} . "_".$function."';";
  640. }
  641. print CLASS $cplusplusreturntype,
  642. " ", $typeprefix, $class->{astNodeName}, "_", $function,
  643. "( ", $cplusplusparams, "){\n";
  644. } else {
  645. if ( exists $class->{Pure} || $constructorCount == 0 ) {
  646. $selfstring = "((".kalyptusDataDict::addNamespace($class->{astNodeName})."*)instPointer)->";
  647. } else {
  648. $selfstring = "((".$class->{astNodeName}."Bridge*)instPointer)->";
  649. }
  650. if ($returnType eq "void") {
  651. print HEADER "procedure ",
  652. $class->{astNodeName}, "_", $function,
  653. "(", "instPointer : ", $class->{astNodeName}, "H", ($pasparams eq "" ? "" : ";"), $pasparams, ");cdecl;\n";
  654. @functions[$#functions+1]="procedure ".
  655. $class->{astNodeName}."_".$function.
  656. "("."instPointer : ".$class->{astNodeName}."H".($pasparams eq "" ? "" : ";").$pasparams.
  657. ");cdecl;external name ".
  658. "'".$typeprefix . $class->{astNodeName} . "_".$function."';";
  659. } else {
  660. print HEADER "function ",
  661. $class->{astNodeName}, "_", $function,
  662. "(", "instPointer : ", $class->{astNodeName}, "H", ($pasparams eq "" ? "" : ";"), $pasparams, ") : ",
  663. $returnType,";cdecl;\n";
  664. @functions[$#functions+1]="function ".
  665. $class->{astNodeName}."_".$function.
  666. "("."instPointer : ".$class->{astNodeName}."H".($pasparams eq "" ? "" : ";").$pasparams.
  667. ") : ".$returnType.";cdecl;external name ".
  668. "'".$typeprefix . $class->{astNodeName} . "_".$function."';";
  669. }
  670. print CLASS $cplusplusreturntype,
  671. " ", $typeprefix, $class->{astNodeName}, "_", $function,
  672. "( ", $typeprefix, $class->{astNodeName}, "* instPointer", ($cplusplusparams eq "" ? "" : ","), $cplusplusparams, "){\n";
  673. }
  674. if ( $cplusplusreturntype =~ /^\s*void\s*$/ ) {
  675. print CLASS "\t", $selfstring, $name, "(", $cplusplusargs, ");\n\treturn;\n}\n" ;
  676. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QBrush\s*$/ ) {
  677. print CLASS "\tQBrush _b= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  678. print CLASS "\treturn (", $cplusplusreturntype, ")new QBrush(_b.color(),_b.style());\n}\n" ;
  679. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QColorGroup\s*$/ ) {
  680. print CLASS "\tQColorGroup _c= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  681. print CLASS "\treturn (", $cplusplusreturntype, ")new QColorGroup(_c.foreground(),_c.background(),_c.light(),_c.dark(),_c.mid(),_c.text(),_c.base());\n}\n" ;
  682. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QDateTime\s*$/ ) {
  683. print CLASS "\tQDateTime _dt= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  684. print CLASS "\treturn (", $cplusplusreturntype, ")new QDateTime (_dt.date(),_dt.time());\n}\n" ;
  685. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QDate\s*$/ ) {
  686. print CLASS "\tQDate _d= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  687. print CLASS "\treturn (", $cplusplusreturntype, ")new QDate(_d.year(),_d.month(),_d.day());\n}\n" ;
  688. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QPen\s*$/ ) {
  689. print CLASS "\tQPen _b= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  690. print CLASS "\treturn (", $cplusplusreturntype, ")new QPen(_b.color(),_b.width(),_b.style());\n}\n" ;
  691. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QPoint\s*\&?\s*$/ ) {
  692. print CLASS "\tQPoint _p= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  693. print CLASS "\treturn (", $cplusplusreturntype, ")new QPoint(_p.x(),_p.y());\n}\n" ;
  694. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QRect\s*$/ ) {
  695. print CLASS "\tQRect _r= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  696. print CLASS "\treturn (", $cplusplusreturntype, ")new QRect(_r.left(),_r.top(),_r.width(),_r.height());\n}\n" ;
  697. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QSizePolicy\s*$/ ) {
  698. print CLASS "\tQSizePolicy _s= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  699. print CLASS "\treturn (", $cplusplusreturntype, ")new QSizePolicy(_s.horData(),_s.verData(),_s.hasHeightForWidth());\n}\n" ;
  700. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QSize\s*$/ ) {
  701. print CLASS "\tQSize _s= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  702. print CLASS "\treturn (", $cplusplusreturntype, ")new QSize(_s.width(),_s.height());\n}\n" ;
  703. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QStyle\s*$/ ) {
  704. print CLASS "\tQStyle * _s= \&", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  705. print CLASS "\treturn (", $cplusplusreturntype, ") _s;\n}\n" ;
  706. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QTime\s*$/ ) {
  707. print CLASS "\tQTime _t= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  708. print CLASS "\treturn (", $cplusplusreturntype, ")new QTime(_t.hour(),_t.minute(),_t.second(),_t.msec());\n}\n" ;
  709. } elsif ( $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?QWMatrix\s*$/ ) {
  710. print CLASS "\tQWMatrix _m= ", $selfstring, $name, "(", $cplusplusargs, ");\n" ;
  711. print CLASS "\treturn (", $cplusplusreturntype, ")new QWMatrix(_m.m11(),_m.m12(),_m.m21(),_m.m22(),_m.dx(),_m.dy());\n}\n" ;
  712. } elsif ( ($cplusplusreturntype =~ /qt_/ || $returnType =~ /kde_/)
  713. && $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?(\w*)\s*$/ )
  714. {
  715. my $valueType = kalyptusDataDict::addNamespace($3);
  716. print CLASS "\treturn (", $cplusplusreturntype, ")new $valueType(", $selfstring, $name, "(", $cplusplusargs, "));\n}\n"; ;
  717. } elsif ( ($cplusplusreturntype =~ /qt_/ || $cplusplusreturntype =~ /kde_/)
  718. && $m->{ReturnType} =~ /^\s*(inline)?\s*(const)?\s*?(\w*)\s*\&?\s*$/ )
  719. {
  720. my $constOpt = $2;
  721. my $valueType = kalyptusDataDict::addNamespace($3);
  722. print CLASS "\treturn (", $cplusplusreturntype, ") ($constOpt $valueType *)\&", $selfstring, $name, "(", $cplusplusargs, ");\n}\n"; ;
  723. } else {
  724. print CLASS "\treturn (", $cplusplusreturntype, ") ", $selfstring, $name, "(", $cplusplusargs, ");\n}\n" ;
  725. }
  726. }
  727. } elsif( $type eq "enum" ) {
  728. # Convert each enum value to '#define <uppercased class name>_<enum name> <enum value>'
  729. my $enum = $m->{astNodeName};
  730. my $enumname = $enum;
  731. my %enumMap = ();
  732. # Add a C++ to C type mapping for this enum - ie an int in C
  733. $enum =~ s/\s//g;
  734. kalyptusDataDict::setpastypemap($enum, $class->{astNodeName}.$enumname);
  735. kalyptusDataDict::setctypemap($enum, 'int');
  736. $enum = $class->{astNodeName}."::".$enum;
  737. kalyptusDataDict::setpastypemap($enum, $class->{astNodeName}.$enumname);
  738. # add C mapping as well
  739. kalyptusDataDict::setctypemap($enum, 'int');
  740. @typeenums[$#typeenums+1]= " ".$class->{astNodeName}.$enumname." = (\n";
  741. my @enums = split(",", $m->{Params});
  742. my $first = 1;
  743. foreach my $enum ( @enums ) {
  744. if ($first!=1) {
  745. @typeenums[$#typeenums+1]=",\n";
  746. }
  747. $first=0;
  748. $enum =~ s/\s//g;
  749. if ( $enum =~ /(.*)=(.*)\s*(\||\&|>>|<<|\+)\s*(.*)/ ) {
  750. # !!!! needs to be evaluted here
  751. # or'd, and'd or shifted pair of values
  752. @typeenums[$#typeenums+1]=" ".$class->{astNodeName}.$enumname."_".$1; # !!!!. "\t:= ".
  753. #!!!! ($enumMap{$2} eq "" ? $2 : "dword(".$enumMap{$2}.")").$pasopmap{$3}. ($enumMap{$4} eq "" ? $4 : "dword(".$enumMap{$4}.")");
  754. $enumMap{$1} = $class->{astNodeName}.$enumname."_".$1;
  755. } elsif ( $enum =~ /(.*)=(.*)/ ) {
  756. @typeenums[$#typeenums+1]=" ".kalyptusDataDict::pasenummap($class->{astNodeName}.
  757. $enumname."_".$1)."\t:= ".($enumMap{$2} eq "" ? changehex($2) : $enumMap{$2});
  758. $enumMap{$1} = $class->{astNodeName}.$enumname."_".$1;
  759. } else {
  760. @typeenums[$#typeenums+1]=" ".$class->{astNodeName}.$enumname."_".$enum;
  761. $enumMap{$enum} = $class->{astNodeName}.$enumname."_".$enum;
  762. }
  763. }
  764. @typeenums[$#typeenums+1]="\n );\n\n";
  765. }
  766. }
  767. 1;
  768. #