Perl/CdrVisitor.pm untested

filter: Statementstmt 0.0% 0 / 815 Branchbran 0.0% 0 / 158 Conditioncond 0.0% 0 / 74 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 0.0% 0 / 34 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 1148 CC 80 SCAR 44.3
CC
80
Cov
0%
visitTypeDeclarator
210.0
visitUnionType
210.0
visitSequenceType
132.0
CRAP 83.8
1
2 #
3 # Interface Definition Language (OMG IDL CORBA v3.0)
4 #
5
6 package CORBA::Perl::CdrVisitor;
7
8 use strict;
9 use warnings;
10
11 our $VERSION = '0.43';
12
13 use File::Basename;
14 use POSIX qw(ctime);
15
16 # needs $node->{pl_name} $node->{pl_package} (PerlNameVisitor)
17
18 sub new {
19 my $proto = shift;
20 my $class = ref($proto) || $proto;
21 my $self = {};
22 bless $self, $class;
23 my ($parser, $pkg_prefix) = @_;
24 $self->{srcname} = $parser->YYData->{srcname};
25 $self->{srcname_size} = $parser->YYData->{srcname_size};
26 $self->{srcname_mtime} = $parser->YYData->{srcname_mtime};
27 $self->{symbtab} = $parser->YYData->{symbtab};
28 $self->{client} = 1;
29 $self->{miop} = 0;
30 $self->{use} = {};
31 if ($pkg_prefix) {
32 $self->{pkg_prefix} = $pkg_prefix;
33 $self->{pkg_prefix} =~ s/\//::/g;
34 $self->{pkg_prefix} .= '::';
35 }
36 else {
37 $self->{pkg_prefix} = q{};
38 }
39 my $filename = basename($self->{srcname}, '.idl') . '.pm';
40 $self->open_stream($filename);
41 $self->{done_hash} = {};
42 $self->{num_key} = 'num_pl_cdr';
43 $self->{pkg_modif} = 0;
44 $self->{stringify} = 1;
45 $self->{id} = 1;
46 return $self;
47 }
48
49 sub open_stream {
50 my $self = shift;
51 my ($filename) = @_;
52 open $self->{out}, '>', $filename
53 or die "can't open $filename ($!).\n";
54 $self->{filename} = $filename;
55 }
56
57 sub _insert_use {
58 my $self = shift;
59 my ($module) = @_;
60 my $FH = $self->{out};
61 $module = basename($module, '.idl');
62 unless (exists $self->{use}->{$module}) {
63 $self->{use}->{$module} = 1;
64 print $FH "use ",$self->{pkg_prefix},$module,";\n";
65 print $FH "\n";
66 }
67 }
68
69 sub _get_defn {
70 my $self = shift;
71 my ($defn) = @_;
72 if (ref $defn) {
73 return $defn;
74 }
75 else {
76 return $self->{symbtab}->Lookup($defn);
77 }
78 }
79
80 #
81 # 3.5 OMG IDL Specification (could be specialized)
82 #
83
84 sub visitSpecification {
85 my $self = shift;
86 my ($node) = @_;
87 my $FH = $self->{out};
88 $self->{pkg_modif} = 0;
89 print $FH "# ex: set ro:\n";
90 print $FH "# This file was generated (by ",$0,"). DO NOT modify it.\n";
91 print $FH "# From file : ",$self->{srcname},", ",$self->{srcname_size}," octets, ",POSIX::ctime($self->{srcname_mtime});
92 print $FH "\n";
93 print $FH "use strict;\n";
94 print $FH "use warnings;\n";
95 print $FH "\n";
96 print $FH "package main;\n";
97 print $FH "\n";
98 print $FH "use CORBA::Perl::CORBA;\n";
99 print $FH "use Carp;\n";
100 print $FH "\n";
101 if (exists $node->{list_import}) {
102 foreach (@{$node->{list_import}}) {
103 $_->visit($self);
104 }
105 }
106 foreach (@{$node->{list_decl}}) {
107 $self->_get_defn($_)->visit($self);
108 if ($self->{pkg_modif}) {
109 $self->{pkg_modif} = 0;
110 print $FH "package main;\n";
111 print $FH "\n";
112 }
113 }
114 print $FH "1;\n";
115 print $FH "\n";
116 print $FH "# end of file : ",$self->{filename},"\n";
117 print $FH "\n";
118 print $FH "# Local variables:\n";
119 print $FH "# buffer-read-only: t\n";
120 print $FH "# End:\n";
121 close $FH;
122 }
123
124 #
125 # 3.6 Import Declaration
126 #
127
128 sub visitImport {
129 my $self = shift;
130 my ($node) = @_;
131 foreach (@{$node->{list_decl}}) {
132 $self->{symbtab}->Lookup($_)->visit($self);
133 }
134 }
135
136 #
137 # 3.7 Module Declaration
138 #
139
140 sub visitModules {
141 my $self = shift;
142 my ($node) = @_;
143 unless (exists $node->{$self->{num_key}}) {
144 $node->{$self->{num_key}} = 0;
145 }
146 my $module = ${$node->{list_decl}}[$node->{$self->{num_key}}];
147 $module->visit($self);
148 $node->{$self->{num_key}} ++;
149 }
150
151 sub visitModule {
152 my $self = shift;
153 my ($node) = @_;
154 my $FH = $self->{out};
155 if ($self->{srcname} eq $node->{filename}) {
156 my $defn = $self->{symbtab}->Lookup($node->{full});
157 $self->{pkg_modif} = 0;
158 print $FH "#\n";
159 print $FH "# begin of module ",$defn->{pl_package},"\n";
160 print $FH "#\n";
161 print $FH "\n";
162 print $FH "package ",$defn->{pl_package},";\n";
163 print $FH "\n";
164 print $FH "use Carp;\n";
165 print $FH "use CORBA::Perl::CORBA;\n";
166 print $FH "\n";
167 foreach (@{$node->{list_decl}}) {
168 $self->_get_defn($_)->visit($self);
169 if ($self->{pkg_modif}) {
170 $self->{pkg_modif} = 0;
171 print $FH "package ",$defn->{pl_package},";\n";
172 print $FH "\n";
173 }
174 }
175 print $FH "\n";
176 print $FH "#\n";
177 print $FH "# end of module ",$defn->{pl_package},"\n";
178 print $FH "#\n";
179 print $FH "\n";
180 $self->{pkg_modif} = 1;
181 }
182 else {
183 $self->_insert_use($node->{filename});
184 }
185 }
186
187 #
188 # 3.8 Interface Declaration (could be specialized)
189 #
190
191 sub visitBaseInterface {
192 my $self = shift;
193 my($node) = @_;
194 if ($self->{srcname} eq $node->{filename}) {
195 my $FH = $self->{out};
196 $self->{pkg_modif} = 0;
197 print $FH "#\n";
198 print $FH "# begin of '",ref $node,"' ",$node->{pl_package},"\n";
199 print $FH "#\n";
200 print $FH "\n";
201 print $FH "package ",$node->{pl_package},";\n";
202 print $FH "\n";
203 print $FH "use CORBA::Perl::CORBA;\n";
204 print $FH "use Carp;\n";
205 print $FH "\n";
206 foreach (@{$node->{list_decl}}) {
207 my $defn = $self->_get_defn($_);
208 if ( $defn->isa('Operation')
209 or $defn->isa('Attributes')
210 or $defn->isa('Initializer')
211 or $defn->isa('StateMembers') ) {
212 next;
213 }
214 $defn->visit($self);
215 if ($self->{pkg_modif}) {
216 $self->{pkg_modif} = 0;
217 print $FH "package ",$node->{pl_package},";\n";
218 print $FH "\n";
219 }
220 }
221 print $FH "\n";
222 print $FH "#\n";
223 print $FH "# end of '",ref $node,"' ",$node->{pl_package},"\n";
224 print $FH "#\n";
225 print $FH "\n";
226 $self->{pkg_modif} = 1;
227 }
228 else {
229 $self->_insert_use($node->{filename});
230 }
231 }
232
233 sub visitForwardBaseInterface {
234 # empty
235 }
236
237 #
238 # 3.9 Value Declaration
239 #
240
241 #
242 # 3.10 Constant Declaration
243 #
244
245 sub visitConstant {
246 my $self = shift;
247 my ($node) = @_;
248 if ($self->{srcname} eq $node->{filename}) {
249 my $FH = $self->{out};
250 print $FH "# ",$node->{pl_package},"::",$node->{pl_name},"\n";
251 print $FH "sub ",$node->{pl_name}," () {\n";
252 print $FH "\treturn ",$node->{value}->{pl_literal},";\n";
253 print $FH "}\n";
254 print $FH "\n";
255 }
256 }
257
258 #
259 # 3.11 Type Declaration
260 #
261
262 sub visitTypeDeclarators {
263 my $self = shift;
264 my ($node) = @_;
265 foreach (@{$node->{list_decl}}) {
266 $self->_get_defn($_)->visit($self);
267 }
268 }
269
270 sub visitTypeDeclarator {
271 my $self = shift;
272 my ($node) = @_;
273 my $type = $self->_get_defn($node->{type});
274 if ( $type->isa('StructType')
275 or $type->isa('UnionType')
276 or $type->isa('EnumType')
277 or $type->isa('SequenceType')
278 or $type->isa('FixedPtType') ) {
279 $type->visit($self);
280 }
281 if ($self->{srcname} eq $node->{filename}) {
282 my $FH = $self->{out};
283 print $FH "# ",$node->{pl_package},"::",$node->{pl_name}," (typedef)\n";
284 if (exists $node->{array_size}) {
285 warn __PACKAGE__,"::visitTypeDecalarator $node->{idf} : empty array_size.\n"
286 unless (@{$node->{array_size}});
287 my $n;
288
289 print $FH "sub ",$node->{idf},"__marshal {\n";
290 print $FH "\tmy (\$r_buffer, \$value) = \@_;\n";
291 print $FH "\tcroak \"undefined value for '",$node->{idf},"'.\\n\"\n";
292 print $FH "\t\t\tunless (defined \$value);\n";
293 $n = 0;
294 print $FH "\tlocal \$_ = \$value;\n";
295 foreach (@{$node->{array_size}}) {
296 $n ++;
297 print $FH "\tcroak \"bad size of array '",$node->{idf},"'.\\n\"\n";
298 print $FH "\t\t\tunless (scalar(\@{\$_}) == ",$_->{pl_literal},");\n";
299 print $FH "\tforeach (\@{\$_}) {\n";
300 }
301 if (exists $type->{max}) {
302 print $FH "\t\t",$type->{pl_package},'::',$type->{pl_name},"__marshal(\$r_buffer, \$_, ",$type->{max}->{value},");\n";
303 }
304 else {
305 print $FH "\t\t",$type->{pl_package},'::',$type->{pl_name},"__marshal(\$r_buffer, \$_);\n";
306 }
307 while ($n--) {
308 print $FH "\t}\n";
309 }
310 print $FH "}\n";
311 print $FH "\n";
312
313 print $FH "sub ",$node->{idf},"__demarshal {\n";
314 print $FH "\tmy (\$r_buffer, \$r_offset, \$endian) = \@_;\n";
315 $n = 0;
316 foreach (@{$node->{array_size}}) {
317 $n ++;
318 print $FH "\tmy \@array",$n," = ();\n";
319 print $FH "\tfor (my \$idx",$n," = 0; ";
320 print $FH "\$idx",$n," < ",$_->{pl_literal},"; ";
321 print $FH "\$idx",$n,"++) {\n";
322 }
323 print $FH "\t\tpush \@array",$n,", ";
324 print $FH $type->{pl_package},'::',$type->{pl_name},"__demarshal(\$r_buffer, \$r_offset, \$endian);\n";
325 print $FH "\t}\n";
326 while ($n > 1) {
327 print $FH "\t\tpush \@array",($n - 1),", ";
328 print $FH "\\\@array",$n,";\n";
329 print $FH "\t}\n";
330 $n --;
331 }
332 print $FH "\treturn \\\@array1;\n";
333 print $FH "}\n";
334 print $FH "\n";
335
336 if ($self->{stringify}) {
337 my $type2 = $type;
338 while ( $type2->isa('TypeDeclarator')
339 and ! exists $type2->{array_size} ) {
340 $type2 = $self->_get_defn($type2->{type});
341 }
342 print $FH "sub ",$node->{idf},"__stringify {\n";
343 print $FH "\tmy (\$value, \$tab) = \@_;\n";
344 print $FH "\t\$tab = q{} unless (defined \$tab);\n";
345 print $FH "\tcroak \"undefined value for '",$node->{idf},"'.\\n\"\n";
346 print $FH "\t\t\tunless (defined \$value);\n";
347 $n = 0;
348 print $FH "\tmy \$str = '{';\n";
349 print $FH "\tlocal \$_ = \$value;\n";
350 foreach (@{$node->{array_size}}) {
351 $n ++;
352 print $FH "\tcroak \"bad size of array '",$node->{idf},"'.\\n\"\n";
353 print $FH "\t\t\tunless (scalar(\@{\$_}) == ",$_->{pl_literal},");\n";
354 print $FH "\tmy \$first",$n," = 1;\n";
355 print $FH "\tforeach (\@{\$_}) {\n";
356 print $FH "\t\tif (\$first",$n,") {\n";
357 print $FH "\t\t\t\$first",$n," = 0;\n";
358 print $FH "\t\t}\n";
359 print $FH "\t\telse {\n";
360 print $FH "\t\t\t\$str .= ',';\n";
361 print $FH "\t\t}\n";
362 unless ($type2->isa('BasicType')) {
363 print $FH "\t\t\$str .= \"\\n\$tab \";\n";
364 }
365 }
366 if (exists $type->{max}) {
367 print $FH "\t\t\$str .= ",$type->{pl_package},'::',$type->{pl_name},"__stringify(\$_, \$tab . q{ } x 2, ",$type->{max}->{value},");\n";
368 }
369 else {
370 print $FH "\t\t\$str .= ",$type->{pl_package},'::',$type->{pl_name},"__stringify(\$_, \$tab . q{ } x 2);\n";
371 }
372 while ($n--) {
373 print $FH "\t}\n";
374 unless ($type2->isa('BasicType')) {
375 print $FH "\t\$str .= \"\\n\$tab\";\n";
376 }
377 print $FH "\t\$str .= '}';\n";
378 }
379 print $FH "\treturn \$str;\n";
380 print $FH "}\n";
381 print $FH "\n";
382 }
383 }
384 else {
385 print $FH "sub ",$node->{idf},"__marshal {\n";
386 print $FH "\tmy (\$r_buffer, \$value) = \@_;\n";
387 print $FH "\tcroak \"undefined value for '",$node->{idf},"'.\\n\"\n";
388 print $FH "\t\t\tunless (defined \$value);\n";
389 if (exists $type->{max}) {
390 print $FH "\t",$type->{pl_package},"::",$type->{pl_name},"__marshal(\$r_buffer, \$value, ",$type->{max}->{value},");\n";
391 }
392 else {
393 print $FH "\t",$type->{pl_package},"::",$type->{pl_name},"__marshal(\$r_buffer, \$value);\n";
394 }
395 print $FH "}\n";
396 print $FH "\n";
397 print $FH "sub ",$node->{idf},"__demarshal {\n";
398 print $FH "\tmy (\$r_buffer, \$r_offset, \$endian) = \@_;\n";
399 print $FH "\treturn ",$type->{pl_package},"::",$type->{pl_name},"__demarshal(\$r_buffer, \$r_offset, \$endian);\n";
400 print $FH "}\n";
401 print $FH "\n";
402 if ($self->{stringify}) {
403 print $FH "sub ",$node->{idf},"__stringify {\n";
404 print $FH "\tmy (\$value, \$tab) = \@_;\n";
405 print $FH "\tcroak \"undefined value for '",$node->{idf},"'.\\n\"\n";
406 print $FH "\t\t\tunless (defined \$value);\n";
407 if (exists $type->{max}) {
408 print $FH "\treturn ",$type->{pl_package},"::",$type->{pl_name},"__stringify(\$value, \$tab, ",$type->{max}->{value},");\n";
409 }
410 else {
411 print $FH "\treturn ",$type->{pl_package},"::",$type->{pl_name},"__stringify(\$value, \$tab);\n";
412 }
413 print $FH "}\n";
414 print $FH "\n";
415 }
416 }
417 if ($self->{id}) {
418 print $FH "sub ",$node->{pl_name},"__id () {\n";
419 print $FH "\treturn \"",$node->{repos_id},"\";\n";
420 print $FH "}\n";
421 print $FH "\n";
422 }
423 }
424 }
425
426 sub visitNativeType {
427 # empty
428 }
429
430 #
431 # 3.11.2 Constructed Types
432 #
433 # 3.11.2.1 Structures
434 #
435
436 sub visitStructType {
437 my $self = shift;
438 my ($node) = @_;
439 my $name = $node->{pl_package} . '::' . $node->{pl_name};
440 return if (exists $self->{done_hash}->{$name});
441 $self->{done_hash}->{$name} = 1;
442 foreach (@{$node->{list_expr}}) {
443 my $type = $self->_get_defn($_->{type});
444 if ( $type->isa('StructType')
445 or $type->isa('UnionType')
446 or $type->isa('SequenceType')
447 or $type->isa('FixedPtType') ) {
448 $type->visit($self);
449 }
450 }
451 if ($self->{srcname} eq $node->{filename}) {
452 my $FH = $self->{out};
453 print $FH "# ",$name," (struct)\n";
454 print $FH "sub ",$node->{pl_name},"__marshal {\n";
455 print $FH "\tmy (\$r_buffer, \$value) = \@_;\n";
456 print $FH "\tcroak \"undefined value for '",$node->{idf},"'.\\n\"\n";
457 print $FH "\t\t\tunless (defined \$value);\n";
458 print $FH "\tcroak \"invalid struct for '",$node->{idf},"' (not a HASH reference).\\n\"\n";
459 print $FH "\t\t\tunless (ref \$value eq 'HASH');\n";
460 foreach (@{$node->{list_member}}) {
461 my $member = $self->_get_defn($_); # member
462 print $FH "\tcroak \"no member '",$member->{idf},"' in structure '",$node->{idf},"'.\\n\"\n";
463 print $FH "\t\t\tunless (exists \$value->{",$member->{idf},"});\n";
464 }
465 foreach (@{$node->{list_member}}) {
466 my $member = $self->_get_defn($_); # member
467 $self->_member_marshal($member, $node, "\$value->{" . $member->{idf} . "}");
468 }
469 print $FH "}\n";
470 print $FH "\n";
471 print $FH "sub ",$node->{pl_name},"__demarshal {\n";
472 print $FH "\tmy (\$r_buffer, \$r_offset, \$endian) = \@_;\n";
473 print $FH "\tmy \$value = {};\n";
474 foreach (@{$node->{list_member}}) {
475 my $member = $self->_get_defn($_); # member
476 $self->_member_demarshal($member, $node, "\$value->{" . $member->{idf} . "}");
477 }
478 print $FH "\treturn \$value;\n";
479 print $FH "}\n";
480 print $FH "\n";
481 if ($self->{stringify}) {
482 print $FH "sub ",$node->{pl_name},"__stringify {\n";
483 print $FH "\tmy (\$value, \$tab) = \@_;\n";
484 print $FH "\t\$tab = q{} unless defined (\$tab);\n";
485 print $FH "\tcroak \"undefined value for '",$node->{idf},"'.\\n\"\n";
486 print $FH "\t\t\tunless (defined \$value);\n";
487 print $FH "\tcroak \"invalid struct for '",$node->{idf},"' (not a HASH reference).\\n\"\n";
488 print $FH "\t\t\tunless (ref \$value eq 'HASH');\n";
489 foreach (@{$node->{list_member}}) {
490 my $member = $self->_get_defn($_); # member
491 print $FH "\tcroak \"no member '",$member->{idf},"' in structure '",$node->{idf},"'.\\n\"\n";
492 print $FH "\t\t\tunless (exists \$value->{",$member->{idf},"});\n";
493 }
494 print $FH "\tmy \$str = \"struct ",$node->{pl_name}," {\";\n";
495 my $idx = 0;
496 my $first = 1;
497 foreach (@{$node->{list_member}}) {
498 my $member = $self->_get_defn($_); # member
499 if ($first) {
500 $first = 0;
501 }
502 else {
503 print $FH "\t\$str .= ',';\n";
504 }
505 $self->_member_stringify($member, $node, "\$value->{" . $member->{idf} . "}", \$idx);
506 }
507 print $FH "\t\$str .= \"\\n\$tab}\";\n";
508 print $FH "\treturn \$str;\n";
509 print $FH "}\n";
510 print $FH "\n";
511 }
512 if ($self->{id}) {
513 print $FH "sub ",$node->{pl_name},"__id () {\n";
514 print $FH "\treturn \"",$node->{repos_id},"\";\n";
515 print $FH "}\n";
516 print $FH "\n";
517 }
518 }
519 }
520
521 sub _member_marshal {
522 my $self = shift;
523 my ($node, $parent, $val) = @_;
524 my $tab = $parent->isa('UnionType') ? "\t\t" : "\t";
525 my $n = 0;
526 my $type = $self->_get_defn($node->{type});
527 my $FH = $self->{out};
528 if (exists $node->{array_size}) {
529 print $FH $tab,"local \$_ = ",$val,";\n";
530 foreach (@{$node->{array_size}}) {
531 $n ++;
532 print $FH $tab,"croak \"bad size of array '",$node->{idf},"'.\\n\"\n";
533 print $FH $tab,"\t\tunless (scalar(\@{\$_}) == ",$_->{pl_literal},");\n";
534 print $FH $tab,"foreach (\@{\$_}) {\n";
535 }
536 if (exists $type->{max}) {
537 print $FH $tab,"\t",$type->{pl_package},'::',$type->{pl_name};
538 print $FH "__marshal(\$r_buffer, \$_, ",$type->{max}->{value},");\n";
539 }
540 else {
541 print $FH $tab,"\t",$type->{pl_package},'::',$type->{pl_name};
542 print $FH "__marshal(\$r_buffer, \$_);\n";
543 }
544 while ($n--) {
545 print $FH $tab,"}\n";
546 }
547 }
548 else {
549 if (exists $type->{max}) {
550 print $FH $tab,$type->{pl_package},'::',$type->{pl_name};
551 print $FH "__marshal(\$r_buffer, ",$val,", ",$type->{max}->{value},");\n";
552 }
553 else {
554 print $FH $tab,$type->{pl_package},'::',$type->{pl_name};
555 print $FH "__marshal(\$r_buffer, ",$val,");\n";
556 }
557 }
558 }
559
560 sub _member_demarshal {
561 my $self = shift;
562 my ($node, $parent, $val) = @_;
563 my $tab = $parent->isa('UnionType') ? "\t\t" : "\t";
564 my $n = 0;
565 my $FH = $self->{out};
566 my $type = $self->_get_defn($node->{type});
567 if (exists $node->{array_size}) {
568 foreach (@{$node->{array_size}}) {
569 $n ++;
570 print $FH $tab,"my \@",$node->{idf},"_array",$n," = ();\n";
571 print $FH $tab,"for (my \$idx",$n," = 0; ";
572 print $FH "\$idx",$n," < ",$_->{pl_literal},"; ";
573 print $FH "\$idx",$n,"++) {\n";
574 }
575 print $FH $tab,"\tpush \@",$node->{idf},"_array",$n,", ";
576 print $FH $type->{pl_package},'::',$type->{pl_name},"__demarshal(\$r_buffer, \$r_offset, \$endian);\n";
577 print $FH $tab,"}\n";
578 while ($n > 1) {
579 print $FH $tab,"\tpush \@",$node->{idf},"_array",($n - 1),", ";
580 print $FH "\\\@",$node->{idf},"_array",$n,";\n";
581 print $FH $tab,"}\n";
582 $n --;
583 }
584 print $FH $tab,$val," = \\\@",$node->{idf},"_array1;\n";
585 }
586 else {
587 print $FH $tab,$val," = ";
588 print $FH $type->{pl_package},'::',$type->{pl_name},"__demarshal(\$r_buffer, \$r_offset, \$endian);\n";
589 }
590 }
591
592 sub _member_stringify {
593 my $self = shift;
594 my ($node, $parent, $val, $r_idx) = @_;
595 my $tab = $parent->isa('UnionType') ? "\t\t" : "\t";
596 my $n = 0;
597 my $type = $self->_get_defn($node->{type});
598 my $array = q{};
599 if (exists $node->{array_size}) {
600 foreach (@{$node->{array_size}}) {
601 $array .= '[]';
602 }
603 }
604 my $FH = $self->{out};
605 print $FH $tab,"\$str .= \"\\n\$tab ",$type->{pl_name},$array," ",$node->{pl_name}," = \";\n";
606 if (exists $node->{array_size}) {
607 my $type2 = $type;
608 while ( $type2->isa('TypeDeclarator')
609 and ! exists $type2->{array_size} ) {
610 $type2 = $self->_get_defn($type2->{type});
611 }
612 print $FH $tab,"local \$_ = ",$val,";\n";
613 foreach (@{$node->{array_size}}) {
614 $n ++;
615 $$r_idx ++;
616 print $FH $tab,"croak \"bad size of array '",$node->{idf},"'.\\n\"\n";
617 print $FH $tab,"\t\tunless (scalar(\@{\$_}) == ",$_->{pl_literal},");\n";
618 print $FH $tab,"\$str .= \"{\";\n";
619 print $FH $tab,"my \$first",$$r_idx," = 1;\n";
620 print $FH $tab,"foreach (\@{\$_}) {\n";
621 print $FH $tab,"\tif (\$first",$$r_idx,") {\n";
622 print $FH $tab,"\t\t\$first",$$r_idx," = 0;\n";
623 print $FH $tab,"\t}\n";
624 print $FH $tab,"\telse {\n";
625 print $FH $tab,"\t\t\$str .= \",\";\n";
626 print $FH $tab,"\t}\n";
627 unless ($type2->isa('BasicType')) {
628 print $FH $tab,"\$str .= \"\\n\";\n";
629 }
630 }
631 if (exists $type->{max}) {
632 print $FH $tab,"\t\$str .= ",$type->{pl_package},'::',$type->{pl_name};
633 print $FH "__stringify(\$_, \$tab . \" \", ",$type->{max}->{value},");\n";
634 }
635 else {
636 print $FH $tab,"\t\$str .= ",$type->{pl_package},'::',$type->{pl_name};
637 print $FH "__stringify(\$_, \$tab . \" \");\n";
638 }
639 while ($n--) {
640 print $FH $tab,"}\n";
641 unless ($type2->isa('BasicType')) {
642 print $FH "\t\t\$str .= \"\\n\";\n";
643 }
644 print $FH $tab,"\$str .= \"}\";\n";
645 }
646 }
647 else {
648 if (exists $type->{max}) {
649 print $FH $tab,"\$str .= ",$type->{pl_package},'::',$type->{pl_name};
650 print $FH "__stringify(",$val,", \$tab . \" \", ",$type->{max}->{value},");\n";
651 }
652 else {
653 print $FH $tab,"\$str .= ",$type->{pl_package},'::',$type->{pl_name};
654 print $FH "__stringify(",$val,", \$tab . \" \");\n";
655 }
656 }
657 }
658
659 # 3.11.2.2 Discriminated Unions
660 #
661
662 sub visitUnionType {
663 my $self = shift;
664 my ($node) = @_;
665 my $name = $node->{pl_package} . '::' . $node->{pl_name};
666 return if (exists $self->{done_hash}->{$name});
667 $self->{done_hash}->{$name} = 1;
668 foreach (@{$node->{list_expr}}) {
669 my $type = $self->_get_defn($_->{element}->{type});
670 if ( $type->isa('StructType')
671 or $type->isa('UnionType')
672 or $type->isa('EnumType')
673 or $type->isa('SequenceType')
674 or $type->isa('FixedPtType') ) {
675 $type->visit($self);
676 }
677 }
678 if ($self->{srcname} eq $node->{filename}) {
679 my $type = $self->_get_defn($node->{type});
680 while ($type->isa('TypeDeclarator')) {
681 $type = $self->_get_defn($type->{type});
682 }
683 my $equal;
684 if ($type->isa('IntegerType')) {
685 $equal = '==';
686 }
687 else {
688 $equal = 'eq';
689 }
690 $type = $self->_get_defn($node->{type});
691 my $default = undef;
692 foreach my $case (@{$node->{list_expr}}) { # case
693 foreach (@{$case->{list_label}}) { # default or expression
694 $default = $case if ($_->isa('Default'));
695 }
696 }
697 my $FH = $self->{out};
698 print $FH "# ",$name," (union)\n";
699 print $FH "sub ",$node->{pl_name},"__marshal {\n";
700 print $FH "\tmy (\$r_buffer, \$union) = \@_;\n";
701 print $FH "\tcroak \"undefined value for '",$node->{idf},"'.\\n\"\n";
702 print $FH "\t\t\tunless (defined \$union);\n";
703 print $FH "\tcroak \"invalid union for '",$node->{idf},"' (not a ARRAY reference).\\n\"\n";
704 print $FH "\t\t\tunless (ref \$union eq 'ARRAY');\n";
705 print $FH "\tcroak \"invalid union '",$node->{idf},"'.\\n\"\n";
706 print $FH "\t\t\tunless (scalar(\@{\$union}) == 2);\n";
707 print $FH "\tmy \$d = \${\$union}[0];\n";
708 print $FH "\tmy \$value = \${\$union}[1];\n";
709 print $FH "\t",$type->{pl_package},"::",$type->{pl_name},"__marshal(\$r_buffer,\$d);\n";
710 print $FH "\tif (0) {\n";
711 print $FH "\t\t# empty\n";
712 foreach my $case (@{$node->{list_expr}}) { # case
713 foreach (@{$case->{list_label}}) { # default or expression
714 unless ($_->isa('Default')) {
715 print $FH "\t}\n";
716 print $FH "\telsif (\$d ",$equal," ",$_->{pl_literal},") {\n";
717 my $member = $self->_get_defn($case->{element}->{value});
718 $self->_member_marshal($member, $node, "\$value");
719 }
720 }
721 }
722 if (defined $default) {
723 print $FH "\t}\n";
724 print $FH "\telse {\t# default\n";
725 my $member = $self->_get_defn($default->{element}->{value});
726 $self->_member_marshal($member, $node, "\$value");
727 }
728 else {
729 print $FH "\t}\n";
730 print $FH "\telse {\n";
731 print $FH "\t\tcroak \"invalid discriminator (\$d) for '",$node->{idf},"'.\\n\";\n";
732 }
733 print $FH "\t}\n";
734 print $FH "}\n";
735 print $FH "\n";
736 print $FH "sub ",$node->{pl_name},"__demarshal {\n";
737 print $FH "\tmy (\$r_buffer, \$r_offset, \$endian) = \@_;\n";
738 print $FH "\tmy \$value = undef;\n";
739 print $FH "\tmy \$d = ",$type->{pl_package},"::",$type->{pl_name},"__demarshal(\$r_buffer,\$r_offset,\$endian);\n";
740 print $FH "\tif (0) {\n";
741 print $FH "\t\t# empty\n";
742 foreach my $case (@{$node->{list_expr}}) { # case
743 foreach (@{$case->{list_label}}) { # default or expression
744 unless ($_->isa('Default')) {
745 print $FH "\t}\n";
746 print $FH "\telsif (\$d ",$equal," ",$_->{pl_literal},") {\n";
747 my $member = $self->_get_defn($case->{element}->{value});
748 $self->_member_demarshal($member, $node, "\$value");
749 }
750 }
751 }
752 if (defined $default) {
753 print $FH "\t}\n";
754 print $FH "\telse {\t# default\n";
755 my $member = $self->_get_defn($default->{element}->{value});
756 $self->_member_demarshal($member, $node, "\$value");
757 }
758 else {
759 print $FH "\t}\n";
760 print $FH "\telse {\n";
761 print $FH "\t\tcroak \"invalid discriminator (\$d) for '",$node->{idf},"'.\\n\";\n";
762 }
763 print $FH "\t}\n";
764 print $FH "\treturn [\$d, \$value];\n";
765 print $FH "}\n";
766 print $FH "\n";
767 if ($self->{stringify}) {
768 print $FH "sub ",$node->{pl_name},"__stringify {\n";
769 print $FH "\tmy (\$union, \$tab) = \@_;\n";
770 print $FH "\t\$tab = q{} unless defined (\$tab);\n";
771 print $FH "\tcroak \"undefined value for '",$node->{idf},"'.\\n\"\n";
772 print $FH "\t\t\tunless (defined \$union);\n";
773 print $FH "\tcroak \"invalid union for '",$node->{idf},"' (not a ARRAY reference).\\n\"\n";
774 print $FH "\t\t\tunless (ref \$union eq 'ARRAY');\n";
775 print $FH "\tcroak \"invalid union '",$node->{idf},"'.\\n\"\n";
776 print $FH "\t\t\tunless (scalar(\@{\$union}) == 2);\n";
777 print $FH "\tmy \$d = \${\$union}[0];\n";
778 print $FH "\tmy \$value = \${\$union}[1];\n";
779 print $FH "\tmy \$str = \"union ",$node->{pl_name}," {\";\n";
780 print $FH "\tif (0) {\n";
781 print $FH "\t\t# empty\n";
782 my $idx = 0;
783 foreach my $case (@{$node->{list_expr}}) { # case
784 foreach (@{$case->{list_label}}) { # default or expression
785 unless ($_->isa('Default')) {
786 print $FH "\t}\n";
787 print $FH "\telsif (\$d ",$equal," ",$_->{pl_literal},") {\n";
788 my $member = $self->_get_defn($case->{element}->{value});
789 $self->_member_stringify($member, $node, "\$value", \$idx);
790 }
791 }
792 }
793 if (defined $default) {
794 print $FH "\t}\n";
795 print $FH "\telse {\t# default\n";
796 my $member = $self->_get_defn($default->{element}->{value});
797 $self->_member_stringify($member, $node, "\$value", \$idx);
798 }
799 else {
800 print $FH "\t}\n";
801 print $FH "\telse {\n";
802 print $FH "\t\tcroak \"invalid discriminator (\$d) for '",$node->{idf},"'.\\n\";\n";
803 }
804 print $FH "\t}\n";
805 print $FH "\t\$str .= \"\\n\$tab}\";\n";
806 print $FH "\treturn \$str;\n";
807 print $FH "}\n";
808 print $FH "\n";
809 }
810 if ($self->{id}) {
811 print $FH "sub ",$node->{pl_name},"__id () {\n";
812 print $FH "\treturn \"",$node->{repos_id},"\";\n";
813 print $FH "}\n";
814 print $FH "\n";
815 }
816 }
817 }
818
819 # 3.11.2.3 Constructed Recursive Types and Forward Declarations
820 #
821
822 sub visitForwardStructType {
823 # empty
824 }
825
826 sub visitForwardUnionType {
827 # empty
828 }
829
830 # 3.11.2.4 Enumerations
831 #
832
833 sub visitEnumType {
834 my $self = shift;
835 my ($node) = @_;
836 my $name = $node->{pl_package} . '::' . $node->{pl_name};
837 return if (exists $self->{done_hash}->{$name});
838 $self->{done_hash}->{$name} = 1;
839 if ($self->{srcname} eq $node->{filename}) {
840 my $FH = $self->{out};
841 print $FH "# ",$name," (enum)\n";
842 print $FH "sub ",$node->{pl_name},"__marshal {\n";
843 print $FH "\tmy (\$r_buffer, \$value) = \@_;\n";
844 print $FH "\tif (0) {\n";
845 my $idx = 0;
846 foreach (@{$node->{list_expr}}) {
847 print $FH "\t}\n";
848 print $FH "\telsif (\$value eq '",$_->{pl_name},"') {\n";
849 print $FH "\t\tCORBA::Perl::CORBA::unsigned_long__marshal(\$r_buffer, ",$idx++,");\n";
850 }
851 print $FH "\t}\n";
852 print $FH "\telse {\n";
853 print $FH "\t\tcroak \"bad value for '",$name,"'.\\n\";\n";
854 print $FH "\t}\n";
855 print $FH "}\n";
856 print $FH "\n";
857 print $FH "sub ",$node->{pl_name},"__demarshal {\n";
858 print $FH "\tmy \$value = CORBA::Perl::CORBA::unsigned_long__demarshal(\@_);\n";
859 print $FH "\tif (0) {\n";
860 $idx = 0;
861 foreach (@{$node->{list_expr}}) {
862 print $FH "\t}\n";
863 print $FH "\telsif (\$value == ",$idx++,") {\n";
864 print $FH "\t\treturn '",$_->{pl_name},"';\n";
865 }
866 print $FH "\t}\n";
867 print $FH "\telse {\n";
868 print $FH "\t\tcroak \"bad value for '",$name,"'.\\n\";\n";
869 print $FH "\t}\n";
870 print $FH "}\n";
871 print $FH "\n";
872 if ($self->{stringify}) {
873 print $FH "sub ",$node->{pl_name},"__stringify {\n";
874 print $FH "\tmy (\$value) = \@_;\n";
875 print $FH "\treturn \$value;\n";
876 print $FH "}\n";
877 print $FH "\n";
878 }
879 if ($self->{id}) {
880 print $FH "sub ",$node->{pl_name},"__id () {\n";
881 print $FH "\treturn \"",$node->{repos_id},"\";\n";
882 print $FH "}\n";
883 print $FH "\n";
884 }
885 foreach (@{$node->{list_expr}}) { # enum
886 print $FH "sub ",$_->{pl_name}," () {\n";
887 print $FH "\treturn '",$_->{pl_name},"';\n";
888 print $FH "}\n";
889 }
890 print $FH "\n";
891 }
892 }
893
894 #
895 # 3.11.3 Template Types
896 #
897
898 sub visitSequenceType {
899 my $self = shift;
900 my ($node) = @_;
901 my $name = $node->{pl_package} . '::' . $node->{pl_name};
902 return if (exists $self->{done_hash}->{$name});
903 $self->{done_hash}->{$name} = 1;
904 if ($self->{srcname} eq $node->{filename}) {
905 my $type = $self->_get_defn($node->{type});
906 if ( $type->isa('SequenceType')
907 or $type->isa('FixedPtType') ) {
908 $type->visit($self);
909 }
910 my $FH = $self->{out};
911 print $FH "# ",$name," (sequence)\n";
912 print $FH "sub ",$node->{pl_name},"__marshal {\n";
913 print $FH "\tmy (\$r_buffer, \$value, \$max) = \@_;\n";
914 print $FH "\tcroak \"undefined value for '",$node->{pl_name},"'.\\n\"\n";
915 print $FH "\t\t\tunless (defined \$value);\n";
916 if ( $type->{pl_name} eq 'char'
917 or $type->{pl_name} eq 'octet' ) {
918 print $FH "\tcroak \"value '\$value' is not a string.\\n\"\n";
919 print $FH "\t\t\tif (ref \$value);\n";
920 print $FH "\tmy \$len = length(\$value);\n";
921 print $FH "\tcroak \"too long sequence for '",$node->{pl_name},"' (max:\$max).\\n\"\n";
922 print $FH "\t\t\tif (defined \$max and \$len > \$max);\n";
923 print $FH "\tCORBA::Perl::CORBA::unsigned_long__marshal(\$r_buffer, \$len);\n";
924 print $FH "\t\$\$r_buffer .= \$value;\n";
925 }
926 else {
927 print $FH "\tmy \$len = scalar(\@{\$value});\n";
928 print $FH "\tcroak \"too long sequence for '",$node->{pl_name},"' (max:\$max).\\n\"\n";
929 print $FH "\t\t\tif (defined \$max and \$len > \$max);\n";
930 print $FH "\tCORBA::Perl::CORBA::unsigned_long__marshal(\$r_buffer, \$len);\n";
931 print $FH "\tforeach (\@{\$value}) {\n";
932 print $FH "\t\t",$type->{pl_package},"::",$type->{pl_name},"__marshal(\$r_buffer, \$_);\n";
933 print $FH "\t}\n";
934 }
935 print $FH "}\n";
936 print $FH "\n";
937 print $FH "sub ",$node->{pl_name},"__demarshal {\n";
938 print $FH "\tmy (\$r_buffer, \$r_offset, \$endian) = \@_;\n";
939 print $FH "\tmy \$len = CORBA::Perl::CORBA::unsigned_long__demarshal(\$r_buffer, \$r_offset, \$endian);\n";
940 print $FH "\tmy \@seq = ();\n";
941 if ( $type->{pl_name} eq 'char'
942 or $type->{pl_name} eq 'octet' ) {
943 print $FH "\tmy \$str = substr \$\$r_buffer, \$\$r_offset, \$len;\n";
944 print $FH "\t\$\$r_offset += \$len;\n";
945 print $FH "\treturn \$str;\n";
946 }
947 else {
948 print $FH "\twhile (\$len--) {\n";
949 print $FH "\t\tpush \@seq,",$type->{pl_package},"::",$type->{pl_name},"__demarshal(\$r_buffer, \$r_offset, \$endian);\n";
950 print $FH "\t}\n";
951 print $FH "\treturn \\\@seq;\n";
952 }
953 print $FH "}\n";
954 print $FH "\n";
955 if ($self->{stringify}) {
956 my $type2 = $type;
957 while ( $type2->isa('TypeDeclarator')
958 and ! exists $type2->{array_size} ) {
959 $type2 = $self->_get_defn($type2->{type});
960 }
961 print $FH "sub ",$node->{pl_name},"__stringify {\n";
962 print $FH "\tmy (\$value, \$tab, \$max) = \@_;\n";
963 print $FH "\t\$tab = q{} unless (defined \$tab);\n";
964 print $FH "\tcroak \"undefined value for '",$node->{pl_name},"'.\\n\"\n";
965 print $FH "\t\t\tunless (defined \$value);\n";
966 if ($type->{pl_name} eq 'char') {
967 print $FH "\tcroak \"value '\$value' is not a string.\\n\"\n";
968 print $FH "\t\t\tif (ref \$value);\n";
969 print $FH "\tmy \$len = length(\$value);\n";
970 print $FH "\tcroak \"too long sequence for '",$node->{pl_name},"' (max:\$max).\\n\"\n";
971 print $FH "\t\t\tif (defined \$max and \$len > \$max);\n";
972 print $FH "\treturn \"\$value\";\n";
973 }
974 else {
975 if ($type->{pl_name} eq 'octet') {
976 print $FH "\t\$value = [map ord, split //, \$value];\n";
977 }
978 print $FH "\tmy \$len = scalar(\@{\$value});\n";
979 print $FH "\tcroak \"too long sequence for '",$node->{pl_name},"' (max:\$max).\\n\"\n";
980 print $FH "\t\t\tif (defined \$max and \$len > \$max);\n";
981 print $FH "\tmy \$str = '{';\n";
982 print $FH "\tmy \$first = 1;\n";
983 print $FH "\tforeach (\@{\$value}) {\n";
984 print $FH "\t\tif (\$first) {\n";
985 print $FH "\t\t\t\$first = 0;\n";
986 print $FH "\t\t}\n";
987 print $FH "\t\telse {\n";
988 print $FH "\t\t\t\$str .= ',';\n";
989 print $FH "\t\t}\n";
990 unless ($type2->isa('BasicType')) {
991 print $FH "\t\t\$str .= \"\\n\$tab \";\n";
992 }
993 print $FH "\t\t\$str .= ",$type->{pl_package},"::",$type->{pl_name},"__stringify(\$_, \$tab . q{ } x 2);\n";
994 print $FH "\t}\n";
995 unless ($type2->isa('BasicType')) {
996 print $FH "\t\$str .= \"\\n\$tab\";\n";
997 }
998 print $FH "\t\$str .= '}';\n";
999 print $FH "\treturn \$str;\n";
1000 }
1001 print $FH "}\n";
1002 print $FH "\n";
1003 }
1004 }
1005 }
1006
1007 sub visitFixedPtType {
1008 # empty
1009 }
1010
1011 sub visitFixedPtConstType {
1012 # empty
1013 }
1014
1015 #
1016 # 3.12 Exception Declaration
1017 #
1018
1019 sub visitException {
1020 my $self = shift;
1021 my ($node) = @_;
1022 my $name = $node->{pl_package} . '::' . $node->{pl_name};
1023 return if (exists $self->{done_hash}->{$name});
1024 $self->{done_hash}->{$name} = 1;
1025 if (exists $node->{list_expr}) {
1026 warn __PACKAGE__,"::visitException $node->{idf} : empty list_expr.\n"
1027 unless (@{$node->{list_expr}});
1028
1029 foreach (@{$node->{list_expr}}) {
1030 my $type = $self->_get_defn($_->{type});
1031 if ( $type->isa('StructType')
1032 or $type->isa('UnionType')
1033 or $type->isa('SequenceType')
1034 or $type->isa('FixedPtType') ) {
1035 $type->visit($self);
1036 }
1037 }
1038 }
1039 if ($self->{srcname} eq $node->{filename}) {
1040 my $FH = $self->{out};
1041 print $FH "# ",$name," (exception)\n";
1042 print $FH "sub ",$node->{pl_name},"__marshal {\n";
1043 print $FH "\t\tmy (\$r_buffer,\$value) = \@_;\n";
1044 print $FH "\t\tcroak \"undefined value for '",$node->{idf},"'.\\n\"\n";
1045 print $FH "\t\t\t\tunless (defined \$value);\n";
1046 foreach (@{$node->{list_member}}) {
1047 my $member = $self->_get_defn($_); # member
1048 print $FH "\t\tcroak \"no member '",$member->{idf},"' in structure '",$node->{idf},"'.\\n\"\n";
1049 print $FH "\t\t\t\tunless (exists \$value->{",$member->{idf},"});\n";
1050 }
1051 foreach (@{$node->{list_member}}) {
1052 my $member = $self->_get_defn($_); # member
1053 $self->_member_marshal($member, $node, "\$value->{" . $member->{idf} . "}");
1054 }
1055 print $FH "}\n";
1056 print $FH "\n";
1057 print $FH "sub ",$node->{pl_name},"__demarshal {\n";
1058 print $FH "\t\tmy (\$r_buffer,\$r_offset,\$endian) = \@_;\n";
1059 print $FH "\t\tmy \$value = {};\n";
1060 foreach (@{$node->{list_member}}) {
1061 my $member = $self->_get_defn($_); # member
1062 $self->_member_demarshal($member, $node, "\$value->{" . $member->{idf} . "}");
1063 }
1064 print $FH "\t\treturn \$value;\n";
1065 print $FH "}\n";
1066 print $FH "\n";
1067 if ($self->{id}) {
1068 print $FH "sub ",$node->{pl_name},"__id () {\n";
1069 print $FH "\treturn \"",$node->{repos_id},"\";\n";
1070 print $FH "}\n";
1071 print $FH "\n";
1072 }
1073 print $FH "package ",$node->{pl_package},"::",$node->{pl_name},";\n";
1074 print $FH "\n";
1075 print $FH "use base qw(CORBA::Perl::CORBA::UserException);\n";
1076 print $FH "\n";
1077 print $FH "sub new {\n";
1078 print $FH "\tmy \$self = shift;\n";
1079 print $FH "\tlocal \$Error::Depth = \$Error::Depth + 1;\n";
1080 print $FH "\t\$self->SUPER::new(\@_);\n";
1081 print $FH "}\n";
1082 print $FH "\n";
1083 print $FH "sub stringify {\n";
1084 print $FH "\tmy \$self = shift;\n";
1085 print $FH "\tmy \$str = \$self->SUPER::stringify() . \"\\n\";\n";
1086 if (scalar(@{$node->{list_member}})) {
1087 foreach (@{$node->{list_member}}) {
1088 my $member = $self->_get_defn($_); # member
1089 print $FH "\t\$str .= \"\\t",$member->{idf}," => \$self->{",$member->{idf},"}\\n\";\n";
1090 }
1091 }
1092 else {
1093 print $FH "\t\$str .= \"\\t(no data)\";\n";
1094 }
1095 print $FH "\t\$str .= sprintf(\" at \%s line \%d.\\n\", \$self->file, \$self->line);\n";
1096 print $FH "\treturn \$str;\n";
1097 print $FH "}\n";
1098 print $FH "\n";
1099 $self->{pkg_modif} = 1;
1100 }
1101 }
1102
1103 #
1104 # 3.13 Operation Declaration (specialized)
1105 #
1106
1107 #
1108 # 3.14 Attribute Declaration
1109 #
1110
1111 sub visitAttribute {
1112 my $self = shift;
1113 my ($node) = @_;
1114 $node->{_get}->visit($self);
1115 $node->{_set}->visit($self) if (exists $node->{_set});
1116 }
1117
1118 #
1119 # 3.15 Repository Identity Related Declarations
1120 #
1121
1122 sub visitTypeId {
1123 # empty
1124 }
1125
1126 sub visitTypePrefix {
1127 # empty
1128 }
1129
1130 #
1131 # XPIDL
1132 #
1133
1134 sub visitCodeFragment {
1135 # empty
1136 }
1137
1138 1;
1139