Perl/CdrVisitor.pm untested

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