XPIDL/JavaVisitor.pm untested

filter: Statementstmt 0.0% 0 / 231 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 29 totaltotal 0.0% 0 / 406 CC 47 SCAR 53.2
CC
47
Cov
0%
_java_type
600.0
visitOperation
110.0
visitRegularInterface
42.0
CRAP 204.8
1
2 package CORBA::XPIDL::JavaVisitor;
3
4 use strict;
5 use warnings;
6
7 our $VERSION = '0.20';
8
9 use File::Basename;
10
11 sub new {
12 my $proto = shift;
13 my $class = ref($proto) || $proto;
14 my $self = {};
15 bless $self, $class;
16 my ($parser) = @_;
17 $self->{srcname} = $parser->YYData->{srcname};
18 $self->{symbtab} = $parser->YYData->{symbtab};
19 my $filename;
20 if ($parser->YYData->{opt_e}) {
21 $filename = $parser->YYData->{opt_e};
22 }
23 else {
24 if ($parser->YYData->{opt_o}) {
25 $filename = $parser->YYData->{opt_o} . '.java';
26 }
27 else {
28 $filename = basename($self->{srcname}, '.idl') . '.java';
29 }
30 }
31 $self->open_stream($filename);
32 $self->{num_key} = 'num_doc_xp';
33 return $self;
34 }
35
36 sub open_stream {
37 my $self = shift;
38 my ($filename) = @_;
39 open $self->{out}, '>', $filename
40 or die "can't open $filename ($!).\n";
41 $self->{filename} = $filename;
42 }
43
44 sub _get_defn {
45 my $self = shift;
46 my ($defn) = @_;
47 if (ref $defn) {
48 return $defn;
49 }
50 else {
51 return $self->{symbtab}->Lookup($defn);
52 }
53 }
54
55 sub _classname_iid {
56 my $self = shift;
57 my ($node) = @_;
58 my $idf = $node->{idf};
59 $idf =~ s/^ns/NS_/; # backcompat naming styles
60 my $classname = uc $idf;
61 $classname .= '_IID';
62 return $classname;
63 }
64
65 sub _comment {
66 my $self = shift;
67 my ($node) = @_;
68 return q{} unless ($node->{doc});
69 my $FH = $self->{out};
70 my $indent = q{ } x 4;
71 print $FH $indent,"/**\n";
72 foreach (split /\n/, $node->{doc}) {
73 s/^\s+//;
74 next unless ($_);
75 print $FH $indent," * ",$_,"\n";
76 }
77 print $FH $indent," */\n";
78 }
79
80 sub _java_type {
81 my $self = shift;
82 my ($node) = @_;
83
84 while ($node->isa('TypeDeclarator')) {
85 $node = $self->_get_defn($node->{type});
86 }
87
88 if ($node->isa('VoidType')) {
89 return 'Object';
90 }
91 elsif ($node->isa('IntegerType')) {
92 if ($node->{value} eq 'short') {
93 return 'short';
94 }
95 elsif ($node->{value} eq 'unsigned short') {
96 return 'short';
97 }
98 elsif ($node->{value} eq 'long') {
99 return 'int';
100 }
101 elsif ($node->{value} eq 'unsigned long') {
102 return 'int';
103 }
104 elsif ($node->{value} eq 'long long') {
105 return 'long';
106 }
107 elsif ($node->{value} eq 'unsigned long long') {
108 return 'long';
109 }
110 else {
111 warn __PACKAGE__,"::_java_type (IntegerType) $node->{value}.\n";
112 }
113 }
114 elsif ($node->isa('CharType')) {
115 return 'char';
116 }
117 elsif ($node->isa('WideCharType')) {
118 return 'char';
119 }
120 elsif ($node->isa('StringType')) {
121 return 'String';
122 }
123 elsif ($node->isa('WideStringType')) {
124 return 'String';
125 }
126 elsif ($node->isa('BooleanType')) {
127 return 'boolean';
128 }
129 elsif ($node->isa('OctetType')) {
130 return 'byte';
131 }
132 elsif ($node->isa('FloatingPtType')) {
133 if ($node->{value} eq 'float') {
134 return 'float';
135 }
136 elsif ($node->{value} eq 'double') {
137 return 'double';
138 }
139 elsif ($node->{value} eq 'long double') {
140 warn __PACKAGE__," 'long double' not available at this time for Java.\n";
141 return 'double';
142 }
143 else {
144 warn __PACKAGE__,"::_java_type (FloatingType) $node->{value}.\n";
145 }
146 }
147 elsif ($node->isa('NativeType')) {
148 if ( $node->{native} eq 'void' ) {
149 return 'Object';
150 }
151 elsif ( $node->{native} eq 'nsID'
152 or $node->{native} eq 'nsIID'
153 or $node->{native} eq 'nsCID' ) {
154 # XXX: s.b test for "iid" attribute
155 # XXX: special class for nsIDs
156 return 'nsID';
157 }
158 else {
159 # XXX: special class for opaque types
160 return 'OpaqueValue';
161 }
162 }
163 elsif ($node->isa('BaseInterface')) {
164 return $node->{idf};
165 }
166 elsif ($node->isa('ForwardBaseInterface')) {
167 return $node->{idf};
168 }
169 else {
170 my $class = ref $node;
171 warn __PACKAGE__,"::_java_type unknown type ($class).\n";
172 }
173 }
174
175 #
176 # 3.5 OMG IDL Specification
177 #
178
179 sub visitSpecification {
180 my $self = shift;
181 my ($node) = @_;
182 my $FH = $self->{out};
183
184 print $FH "/*\n";
185 print $FH " * ************* DO NOT EDIT THIS FILE ***********\n";
186 print $FH " *\n";
187 print $FH " * This file was automatically generated from ",$self->{srcname},".\n";
188 print $FH " */\n";
189 print $FH "\n";
190 foreach (@{$node->{list_decl}}) {
191 $self->_get_defn($_)->visit($self);
192 }
193 print $FH "\n";
194 print $FH "/*\n";
195 print $FH " * end\n";
196 print $FH " */\n";
197 close $FH;
198 }
199
200 #
201 # 3.6 Import Declaration
202 #
203
204 sub visitImport {
205 # empty
206 }
207
208 #
209 # 3.7 Module Declaration
210 #
211
212 sub visitModules {
213 my $self = shift;
214 my ($node) = @_;
215 unless (exists $node->{$self->{num_key}}) {
216 $node->{$self->{num_key}} = 0;
217 }
218 my $module = ${$node->{list_decl}}[$node->{$self->{num_key}}];
219 $module->visit($self);
220 $node->{$self->{num_key}} ++;
221 }
222
223 sub visitModule {
224 my $self = shift;
225 my ($node) = @_;
226 foreach (@{$node->{list_decl}}) {
227 $self->_get_defn($_)->visit($self);
228 }
229 }
230
231 #
232 # 3.8 Interface Declaration
233 #
234
235 sub visitBaseInterface {
236 # empty
237 }
238
239 sub visitRegularInterface {
240 my $self = shift;
241 my ($node) = @_;
242 return unless ($self->{srcname} eq $node->{filename});
243 my $FH = $self->{out};
244
245 # Write out JavaDoc comment
246 print $FH "\n";
247 print $FH "/**\n";
248 print $FH " * Interface ",$node->{idf},"\n";
249 my $iid = $node->getProperty('uuid');
250 if (defined $iid) {
251 print $FH " *\n";
252 print $FH " * IID: 0x",$iid,"\n";
253 }
254 print $FH " */\n";
255 print $FH "\n";
256
257 # Write "public interface <foo>"
258 print $FH "public interface ",$node->{idf};
259 if (exists $node->{inheritance}) {
260 print $FH " extends ";
261 my $first = 1;
262 foreach (@{$node->{inheritance}->{list_interface}}) {
263 my $base = $self->_get_defn($_);
264 print $FH ", " unless ($first);
265 print $FH $base->{idf};
266 $first = 0;
267 }
268 }
269 print $FH "\n";
270 print $FH "{\n";
271 if (defined $iid) {
272 my $classname_iid = $self->_classname_iid($node);
273 # Write interface constants for IID
274 print $FH " public static final String ",$classname_iid,"_STRING =\n";
275 print $FH " \"",$iid,"\";\n";
276 print $FH "\n";
277 print $FH " public static final nsID ",$classname_iid," =\n";
278 print $FH " new nsID(\"",$iid,"\");\n";
279 print $FH "\n";
280 }
281
282 foreach (@{$node->{list_decl}}) {
283 $self->_get_defn($_)->visit($self);
284 }
285 print $FH "}\n";
286 print $FH "\n";
287 }
288
289 sub visitForwardBaseInterface {
290 # empty
291 }
292
293 #
294 # 3.10 Constant Declaration
295 #
296
297 sub visitConstant {
298 my $self = shift;
299 my ($node) = @_;
300 my $FH = $self->{out};
301
302 my $type = $self->_get_defn($node->{type});
303 my $java_type = $self->_java_type($type);
304 my $value = $node->{value}->{value};
305 print $FH "\n";
306 $self->_comment($node);
307 print $FH " public static final ",$java_type," ",$node->{idf}," = ",$value,";\n";
308 }
309
310 sub visitExpression {
311 # empty
312 }
313
314 #
315 # 3.11 Type Declaration
316 #
317
318 sub visitTypeDeclarators {
319 # empty
320 }
321
322 sub visitNativeType {
323 # empty
324 }
325
326 #
327 # 3.11.2 Constructed Types
328 #
329
330 sub visitStructType {
331 # empty
332 }
333
334 sub visitUnionType {
335 # empty
336 }
337
338 sub visitForwardStructType {
339 # empty
340 }
341
342 sub visitForwardUnionType {
343 # empty
344 }
345
346 # 3.11.2.4 Enumerations
347 #
348
349 sub visitEnumType {
350 # empty
351 }
352
353 #
354 # 3.12 Exception Declaration
355 #
356
357 sub visitException {
358 # empty
359 }
360
361 #
362 # 3.13 Operation Declaration
363 #
364
365 sub visitOperation {
366 my $self = shift;
367 my ($node) = @_;
368 my $FH = $self->{out};
369
370 my $method_notxpcom = $node->hasProperty('notxpcom');
371 my $method_noscript = $node->hasProperty('noscript');
372
373 print $FH "\n";
374 $self->_comment($node);
375
376 # Write beginning of method declaration
377 print $FH " ";
378 # Nonscriptable methods become package-protected
379 print $FH "public " unless ($method_noscript);
380
381 # Write return type
382 # Unlike C++ headers, Java interfaces return the declared
383 # return value; an exception indicates XPCOM method failure.
384 my $type = $self->_get_defn($node->{type});
385 if ($method_notxpcom or !$type->isa('VoidType')) {
386 print $FH $self->_java_type($type);
387 }
388 else {
389 # Check for retval attribute
390 my $retval_param;
391 foreach (@{$node->{list_param}}) {
392 if ($_->hasProperty('retval')) {
393 $retval_param = $_;
394 last;
395 }
396 }
397 if (defined $retval_param) {
398 $type = $self->_get_defn($retval_param->{type});
399 print $FH $self->_java_type($type);
400 }
401 else {
402 print $FH "void";
403 }
404 }
405
406 # Write method name
407 print $FH " ",lcfirst($node->{idf}),"(";
408
409 # Write parameters
410 my $first = 1;
411 foreach (@{$node->{list_param}}) {
412 # Skip "retval"
413 next if ($_->hasProperty('retval'));
414 print $FH ", " unless ($first);
415 # Put in type of parameter
416 $type = $self->_get_defn($_->{type});
417 print $FH $self->_java_type($type);
418 # If the parameter is out or inout, make it a Java array of the
419 # appropriate type
420 print $FH "[]" if ($_->{attr} ne "in");
421 #Put in name of parameter
422 print $FH " ",$_->{idf};
423 $first = 0;
424 }
425 print $FH ")";
426
427 if (exists $node->{list_raise}) {
428 print $FH " throws ";
429 $first = 1;
430 foreach (@{$node->{list_raise}}) { # exception
431 my $defn = $self->_get_defn($_);
432 print $FH ", " unless ($first);
433 print $FH $defn->{idf};
434 $first = 0;
435 }
436 }
437
438 print $FH ";\n";
439 }
440
441 #
442 # 3.14 Attribute Declaration
443 #
444
445 sub visitAttributes {
446 my $self = shift;
447 my ($node) = @_;
448
449 foreach (@{$node->{list_decl}}) {
450 $self->_get_defn($_)->visit($self);
451 }
452 }
453
454 sub visitAttribute {
455 my $self = shift;
456 my ($node) = @_;
457 my $FH = $self->{out};
458
459 my $method_noscript = $node->hasProperty('noscript');
460 my $type = $self->_get_defn($node->{type});
461
462 print $FH "\n";
463 $self->_comment($node);
464
465 # Write access permission ("public" unless nonscriptable)
466 print $FH " ";
467 print $FH "public " unless ($method_noscript);
468 # Write the proper Java return value for the get operation
469 print $FH $self->_java_type($type);
470 # Write the name of the accessor ("get") method.
471 print $FH " get",ucfirst($node->{idf}),"();\n";
472
473 unless (exists $node->{modifier}) { # readonly
474 # Nonscriptable methods become package-protected
475 print $FH " ";
476 print $FH "public " unless ($method_noscript);
477 # Write attribute access method name and return type
478 print $FH "void set",ucfirst($node->{idf}),"(";
479 # Write the proper Java type for the set operation
480 print $FH $self->_java_type($type);
481 # Write the name of the formal parameter.
482 print $FH " value);\n"
483 }
484 }
485
486 #
487 # 3.15 Repository Identity Related Declarations
488 #
489
490 sub visitTypeId {
491 # empty
492 }
493
494 sub visitTypePrefix {
495 # empty
496 }
497
498 #
499 # XPIDL
500 #
501
502 sub visitCodeFragment {
503 # empty
504 }
505
506 1;
507