XPIDL/XptVisitor.pm untested

filter: Statementstmt 0.0% 0 / 270 Branchbran 0.0% 0 / 116 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 519 CC 59 SCAR 66.6
CC
59
Cov
0%
_type
1722.0
_param
30.0
new
12.0
CRAP 780.3
1
2 package CORBA::XPIDL::XptVisitor;
3
4 use strict;
5 use warnings;
6
7 our $VERSION = '0.20';
8
9 use File::Basename;
10 use POSIX qw(ctime);
11 use CORBA::XPIDL::XPT;
12
13 sub new {
14 my $proto = shift;
15 my $class = ref($proto) || $proto;
16 my $self = {};
17 bless $self, $class;
18 my ($parser) = @_;
19 $self->{srcname} = $parser->YYData->{srcname};
20 $self->{symbtab} = $parser->YYData->{symbtab};
21 $self->{emit_typelib_annotations} = $parser->YYData->{opt_a};
22 $self->{typelib} = $parser->YYData->{opt_t};
23 my $filename;
24 if ($parser->YYData->{opt_e}) {
25 $filename = $parser->YYData->{opt_e};
26 }
27 else {
28 if ($parser->YYData->{opt_o}) {
29 $filename = $parser->YYData->{opt_o} . '.xpt';
30 }
31 else {
32 $filename = basename($self->{srcname}, '.idl') . '.xpt';
33 }
34 }
35 $self->{outfile} = $filename;
36 return $self;
37 }
38
39 sub _get_defn {
40 my $self = shift;
41 my ($defn) = @_;
42 if (ref $defn) {
43 return $defn;
44 }
45 else {
46 return $self->{symbtab}->Lookup($defn);
47 }
48 }
49
50 sub _is_dipper {
51 my $self = shift;
52 my ($node) = @_; # type
53 return $node->hasProperty('domstring')
54 || $node->hasProperty('utf8string')
55 || $node->hasProperty('cstring')
56 || $node->hasProperty('astring');
57 }
58
59 sub _arg_num {
60 my $self = shift;
61 my ($name, $node) = @_;
62 my $count = 0;
63 foreach (@{$node->{list_param}}) {
64 return $count
65 if ($_->{idf} eq $name);
66 $count ++;
67 }
68 warn __PACKAGE__,"::_arg_num : can't found argument ($name) in method '$node->{idf}'.\n";
69 }
70
71 #
72 # 3.5 OMG IDL Specification
73 #
74
75 sub visitSpecification {
76 my $self = shift;
77 my ($node) = @_;
78
79 my $major_version = substr($self->{typelib}, 0, 1);
80 my $minor_version = substr($self->{typelib}, 2, 1);
81 $self->{xpt} = new XPT::File(
82 magic => XPT::File::MAGIC,
83 major_version => $major_version,
84 minor_version => $minor_version,
85 );
86 foreach (@{$node->{list_decl}}) {
87 $self->_get_defn($_)->visit($self);
88 }
89 $self->{xpt}->indexe();
90
91 if ($self->{emit_typelib_annotations}) {
92 my $creator = 'xpidl ' . $CORBA::XPIDL::VERSION;
93 my $data = "Created from " . $self->{srcname} .
94 "\nCreation date: " . POSIX::ctime(time()) . "Interfaces:";
95 foreach (sort keys %{$self->{xpt}->{interface_iid}}) {
96 my $itf = ${$self->{xpt}->{interface_iid}}{$_};
97 next unless (defined $itf->{interface_descriptor});
98 $data .= q{ } . $itf->{name};
99 }
100 my $anno = new XPT::Annotation(
101 tag => 1,
102 creator => $creator,
103 private_data => $data,
104 );
105 $self->{xpt}->add_annotation($anno);
106 }
107 $self->{xpt}->terminate_annotations();
108
109 # print $self->{xpt}->stringify();
110 open my $OUT, '>', $self->{outfile}
111 or die "FAILED: can't open $self->{outfile}\n";
112 binmode $OUT, ':raw';
113 print $OUT $self->{xpt}->marshal();
114 close $OUT;
115 }
116
117 #
118 # 3.7 Module Declaration
119 #
120
121 sub visitModules {
122 my $self = shift;
123 my ($node) = @_;
124 foreach (@{$node->{list_decl}}) {
125 $self->_get_defn($_)->visit($self);
126 }
127 }
128
129 sub visitModule {
130 my $self = shift;
131 my ($node) = @_;
132 if ($self->{srcname} eq $node->{filename}) {
133 foreach (@{$node->{list_decl}}) {
134 $self->_get_defn($_)->visit($self);
135 }
136 }
137 }
138
139 #
140 # 3.8 Interface Declaration
141 #
142
143 sub visitRegularInterface {
144 my $self = shift;
145 my ($node) = @_;
146 if ($self->{srcname} eq $node->{filename}) {
147 my $parent_interface_name;
148 if (exists $node->{inheritance}) {
149 my $base = $self->_get_defn(${$node->{inheritance}->{list_interface}}[0]);
150 my $namespace = $base->getProperty('namespace') || q{};
151 $parent_interface_name = $namespace . '::' . $base->{idf};
152 $self->_add_interface($base);
153 }
154 my $interface_descriptor = new XPT::InterfaceDescriptor(
155 parent_interface => $parent_interface_name,
156 method_descriptors => [],
157 const_descriptors => [],
158 is_scriptable => $node->hasProperty('scriptable'),
159 is_function => $node->hasProperty('function'),
160 );
161 $self->{itf} = $interface_descriptor;
162 foreach (@{$node->{list_decl}}) {
163 $self->_get_defn($_)->visit($self);
164 }
165 $self->_add_interface($node, $interface_descriptor);
166 }
167 }
168
169 sub _add_interface {
170 my $self = shift;
171 my ($node, $desc) = @_;
172 my $iid = chr(0) x 16;
173 my $str_iid = $node->getProperty('uuid');
174 if (defined $str_iid) {
175 $str_iid =~ s/-//g;
176 $iid = q{};
177 while ($str_iid) {
178 $iid .= chr(hex(substr $str_iid, 0, 2));
179 $str_iid = substr $str_iid, 2;
180 }
181 }
182 my $name = $node->{idf};
183 my $namespace = $node->getProperty('namespace') || q{};
184 my $entry = new XPT::InterfaceDirectoryEntry(
185 iid => new XPT::IID($iid),
186 name => $name,
187 name_space => $namespace,
188 interface_descriptor => $desc,
189 );
190 $self->{xpt}->add_interface($entry);
191 }
192
193 sub visitForwardRegularInterface {
194 my $self = shift;
195 my ($node) = @_;
196 if ($self->{srcname} eq $node->{filename}) {
197 $self->_add_interface($node);
198 }
199 }
200
201 sub visitBaseInterface {
202 # empty
203 }
204
205 sub visitForwardBaseInterface {
206 # empty
207 }
208
209 #
210 # 3.10 Constant Declaration
211 #
212
213 sub visitConstant {
214 my $self = shift;
215 my ($node) = @_;
216 my $type = $self->_get_defn($node->{type});
217 my $desc = $self->_type($type);
218 my $value = $node->{value}->{value};
219 my $cst = new XPT::ConstDescriptor(
220 name => $node->{idf},
221 type => $desc,
222 value => $value,
223 );
224 push @{$self->{itf}->{const_descriptors}}, $cst;
225 }
226
227 #
228 # 3.11 Type Declaration
229 #
230
231 sub visitTypeDeclarators {
232 # empty
233 }
234
235 sub visitNativeType {
236 # empty
237 }
238
239 sub _type {
240 my $self = shift;
241 my ($node, $param, $method) = @_;
242
243 my $is_array = defined $method && $param->hasProperty('array');
244 my $type = $node;
245 while ($type->isa('TypeDeclarator')) {
246 $type = $self->_get_defn($type->{type});
247 }
248 my %hash;
249 my $iid_is = $param->getProperty('iid_is') if (defined $param);
250
251 if ( $type->isa('IntegerType')) {
252 if ($type->{value} eq 'short') {
253 $hash{tag} = XPT::int16;
254 }
255 elsif ($type->{value} eq 'unsigned short') {
256 $hash{tag} = XPT::uint16;
257 }
258 elsif ($type->{value} eq 'long') {
259 $hash{tag} = XPT::int32;
260 }
261 elsif ($type->{value} eq 'unsigned long') {
262 $hash{tag} = XPT::uint32;
263 }
264 elsif ($type->{value} eq 'long long') {
265 $hash{tag} = XPT::int64;
266 }
267 elsif ($type->{value} eq 'unsigned long long') {
268 $hash{tag} = XPT::uint64;
269 }
270 else {
271 warn __PACKAGE__,"::_type (IntegerType) $node->{value}.\n";
272 }
273 }
274 elsif ($type->isa('OctetType')) {
275 $hash{tag} = XPT::uint8;
276 }
277 elsif ($type->isa('FloatingPtType')) {
278 if ( $type->{value} eq 'float') {
279 $hash{tag} = XPT::float;
280 }
281 elsif ($type->{value} eq 'double') {
282 $hash{tag} = XPT::double;
283 }
284 else {
285 warn __PACKAGE__,"::_type (FloatingPtType) $node->{value}.\n";
286 }
287 }
288 elsif ($type->isa('BooleanType')) {
289 $hash{tag} = XPT::boolean;
290 }
291 elsif ($type->isa('CharType')) {
292 $hash{tag} = XPT::char;
293 }
294 elsif ($type->isa('WideCharType')) {
295 $hash{tag} = XPT::wchar_t;
296 }
297 elsif ($type->isa('StringType')) {
298 my $size_is = $param->getProperty('size_is')
299 if (defined $param);
300 if ($is_array or !defined $method or !defined $size_is) {
301 $hash{tag} = XPT::pstring;
302 $hash{is_pointer} = 1;
303 }
304 else {
305 $hash{tag} = XPT::StringWithSizeTypeDescriptor;
306 $hash{is_pointer} = 1;
307 $hash{size_is_arg_num} = $self->_arg_num($size_is, $method);
308 $hash{length_is_arg_num} = $hash{size_is_arg_num};
309 my $length_is = $param->getProperty('length_is');
310 $hash{length_is_arg_num} = $self->_arg_num($length_is, $method)
311 if (defined $length_is);
312 }
313 }
314 elsif ($type->isa('WideStringType')) {
315 my $size_is = $param->getProperty('size_is')
316 if (defined $param);
317 if ($is_array or !defined $method or !defined $size_is) {
318 $hash{tag} = XPT::pwstring;
319 $hash{is_pointer} = 1;
320 }
321 else {
322 $hash{tag} = XPT::WideStringWithSizeTypeDescriptor;
323 $hash{is_pointer} = 1;
324 $hash{size_is_arg_num} = $self->_arg_num($size_is, $method);
325 $hash{length_is_arg_num} = $hash{size_is_arg_num};
326 my $length_is = $param->getProperty('length_is');
327 $hash{length_is_arg_num} = $self->_arg_num($length_is, $method)
328 if (defined $length_is);
329 }
330 }
331 elsif ($type->isa('NativeType') and !defined $iid_is) {
332 if ($node->hasProperty('nsid')) {
333 $hash{tag} = XPT::nsIID;
334 if ($node->hasProperty('ref')) {
335 $hash{is_pointer} = 1;
336 $hash{is_reference} = 1;
337 }
338 elsif ($node->hasProperty('ptr')) {
339 $hash{is_pointer} = 1;
340 }
341 }
342 elsif ($node->hasProperty('domstring')) {
343 $hash{tag} = XPT::domstring;
344 $hash{is_pointer} = 1;
345 if ($node->hasProperty('ref')) {
346 $hash{is_reference} = 1;
347 }
348 }
349 elsif ($node->hasProperty('astring')) {
350 $hash{tag} = XPT::astring;
351 $hash{is_pointer} = 1;
352 if ($node->hasProperty('ref')) {
353 $hash{is_reference} = 1;
354 }
355 }
356 elsif ($node->hasProperty('utf8string')) {
357 $hash{tag} = XPT::utf8string;
358 $hash{is_pointer} = 1;
359 if ($node->hasProperty('ref')) {
360 $hash{is_reference} = 1;
361 }
362 }
363 elsif ($node->hasProperty('cstring')) {
364 $hash{tag} = XPT::cstring;
365 $hash{is_pointer} = 1;
366 if ($node->hasProperty('ref')) {
367 $hash{is_reference} = 1;
368 }
369 }
370 else {
371 $hash{tag} = XPT::void;
372 $hash{is_pointer} = 1;
373 }
374 }
375 elsif ( $type->isa('RegularInterface')
376 or $type->isa('ForwardRegularInterface')
377 or $type->isa('NativeType') ) {
378 if (defined $iid_is) {
379 $hash{tag} = XPT::InterfaceIsTypeDescriptor;
380 $hash{is_pointer} = 1;
381 $hash{arg_num} = $self->_arg_num($iid_is, $method);
382 }
383 else {
384 $self->_add_interface($type);
385 my $namespace = $type->getProperty('namespace') || q{};
386 $hash{interface} = $namespace . '::' . $type->{idf};
387 $hash{tag} = XPT::InterfaceTypeDescriptor;
388 $hash{is_pointer} = 1;
389 }
390 }
391 elsif ($type->isa('VoidType')) {
392 $hash{tag} = XPT::void;
393 }
394 my $desc = new XPT::TypeDescriptor( %hash );
395
396 if ($is_array) {
397 # size_is is required
398 my $size_is = $param->getProperty('size_is');
399 # die "[array] requires [size_is()].\n"
400 # unless (defined $size_is);
401 my $size_is_arg_num = $self->_arg_num($size_is, $method);
402 # length_is is optional
403 my $length_is_arg_num = $size_is_arg_num;
404 my $length_is = $param->getProperty('length_is');
405 $length_is_arg_num = $self->_arg_num($size_is, $method)
406 if (defined $length_is);
407 return new XPT::TypeDescriptor(
408 is_pointer => 1,
409 is_unique_pointer => 0,
410 is_reference => 0,
411 tag => XPT::ArrayTypeDescriptor,
412 size_is_arg_num => $size_is_arg_num,
413 length_is_arg_num => $length_is_arg_num,
414 type_descriptor => $desc,
415 );
416 }
417 else {
418 return $desc
419 }
420 }
421
422 #
423 # 3.11.2 Constructed Types
424 #
425
426 sub visitStructType {
427 # empty
428 }
429
430 sub visitUnionType {
431 # empty
432 }
433
434 sub visitEnumType {
435 # empty
436 }
437
438 #
439 # 3.12 Exception Declaration
440 #
441
442 sub visitException {
443 # empty
444 }
445
446 #
447 # 3.13 Operation Declaration
448 #
449
450 sub visitOperation {
451 my $self = shift;
452 my ($node) = @_;
453 my $notxpcom = $node->hasProperty('notxpcom');
454 my @params = ();
455 foreach (@{$node->{list_param}}) {
456 push @params, $self->_param($_, $node);
457 }
458 my $type = $self->_get_defn($node->{type});
459 my $result;
460 if ($notxpcom) {
461 $result = new XPT::ParamDescriptor(
462 in => 0,
463 out => 0,
464 retval => 1,
465 shared => 0,
466 dipper => 0,
467 type => $self->_type($type),
468 );
469 }
470 else {
471 unless ($type->isa('VoidType')) {
472 my $dipper = $self->_is_dipper($type);
473 my $desc = $self->_type($type);
474 push @params, new XPT::ParamDescriptor(
475 in => $dipper,
476 out => !$dipper,
477 retval => 1,
478 shared => 0,
479 dipper => $dipper,
480 type => $desc,
481 );
482 }
483
484 $result = $self->_ns_result();
485 }
486 my $method = new XPT::MethodDescriptor(
487 is_getter => 0,
488 is_setter => 0,
489 is_not_xpcom => $notxpcom,
490 is_constructor => 0,
491 is_hidden => $node->hasProperty('noscript'),
492 name => $node->{idf},
493 params => \@params,
494 result => $result,
495 );
496 push @{$self->{itf}->{method_descriptors}}, $method;
497 }
498
499 sub _param {
500 my $self = shift;
501 my ($node, $parent) = @_;
502 my $type = $self->_get_defn($node->{type});
503 my $in = 0;
504 my $out = 0;
505 if ($node->{attr} eq 'in') {
506 $in = 1;
507 }
508 elsif ($node->{attr} eq 'out') {
509 $out = 1;
510 }
511 elsif ($node->{attr} eq 'inout') {
512 $in = 1;
513 $out = 1;
514 }
515 # my $dipper = $self->_is_dipper($type);
516 my $dipper = $self->_is_dipper($node);
517 if ($dipper and $out) {
518 $out = 0;
519 $in = 1;
520 }
521 my $desc = $self->_type($type, $node, $parent);
522 return new XPT::ParamDescriptor(
523 in => $in,
524 out => $out,
525 retval => $node->hasProperty('retval'),
526 shared => $node->hasProperty('shared'),
527 dipper => $dipper,
528 type => $desc,
529 );
530 }
531
532 sub _ns_result {
533 my $self = shift;
534 return new XPT::ParamDescriptor(
535 in => 0,
536 out => 0,
537 retval => 0,
538 shared => 0,
539 dipper => 0,
540 type => new XPT::TypeDescriptor(
541 is_pointer => 0,
542 is_unique_pointer => 0,
543 is_reference => 0,
544 tag => XPT::uint32,
545 ),
546 );
547 }
548
549 #
550 # 3.14 Attribute Declaration
551 #
552
553 sub visitAttributes {
554 my $self = shift;
555 my ($node) = @_;
556 foreach (@{$node->{list_decl}}) {
557 $self->_get_defn($_)->visit($self);
558 }
559 }
560
561 sub visitAttribute {
562 my $self = shift;
563 my ($node) = @_;
564 my $type = $self->_get_defn($node->{type});
565 my $dipper = $self->_is_dipper($type);
566 my $getter = new XPT::MethodDescriptor(
567 is_getter => 1,
568 is_setter => 0,
569 # is_not_xpcom => $node->hasProperty('notxpcom'),
570 is_not_xpcom => 0, # functionality or bug
571 is_constructor => 0,
572 is_hidden => $node->hasProperty('noscript'),
573 name => $node->{idf},
574 params => [
575 new XPT::ParamDescriptor(
576 in => $dipper,
577 out => !$dipper,
578 retval => 1,
579 shared => 0,
580 dipper => $dipper,
581 type => $self->_type($type),
582 )
583 ],
584 result => $self->_ns_result(),
585 );
586 push @{$self->{itf}->{method_descriptors}}, $getter;
587 unless (exists $node->{modifier}) { # readonly
588 my $setter = new XPT::MethodDescriptor(
589 is_getter => 0,
590 is_setter => 1,
591 # is_not_xpcom => $node->hasProperty('notxpcom'),
592 is_not_xpcom => 0, # functionality or bug
593 is_constructor => 0,
594 is_hidden => $node->hasProperty('noscript'),
595 name => $node->{idf},
596 params => [
597 new XPT::ParamDescriptor(
598 in => 1,
599 out => 0,
600 retval => 0,
601 shared => 0,
602 dipper => 0,
603 type => $self->_type($type),
604 )
605 ],
606 result => $self->_ns_result(),
607 );
608 push @{$self->{itf}->{method_descriptors}}, $setter;
609 }
610 }
611
612 #
613 # 3.15 Repository Identity Related Declarations
614 #
615
616 sub visitTypeId {
617 # empty
618 }
619
620 sub visitTypePrefix {
621 # empty
622 }
623
624 #
625 # XPIDL
626 #
627
628 sub visitCodeFragment {
629 # empty
630 }
631
632 1;
633