C/SkeletonVisitor.pm untested

filter: Statementstmt 0.0% 0 / 176 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 297 CC 22 SCAR 26.5
CC
22
Cov
0%
parse
42.0
visitOperation
42.0
merge
20.0
CRAP 14.1
1
2 #
3 # Interface Definition Language (OMG IDL CORBA v3.0)
4 #
5 # C Language Mapping Specification, New Edition June 1999
6 #
7
8 package CORBA::C::SkeletonVisitor;
9
10 use strict;
11 use warnings;
12
13 our $VERSION = '2.60';
14
15 use File::Basename;
16 use POSIX qw(ctime);
17
18 # needs $node->{c_name} (CnameVisitor) and $node->{c_arg} (CincludeVisitor)
19
20 sub new {
21 my $proto = shift;
22 my $class = ref($proto) || $proto;
23 my $self = {};
24 bless $self, $class;
25 my ($parser, $prefix) = @_;
26 $prefix = 'skel_' if (!defined $prefix);
27 $self->{prefix} = $prefix;
28 $self->{srcname} = $parser->YYData->{srcname};
29 $self->{srcname_size} = $parser->YYData->{srcname_size};
30 $self->{srcname_mtime} = $parser->YYData->{srcname_mtime};
31 $self->{symbtab} = $parser->YYData->{symbtab};
32 my $filename = $prefix . basename($self->{srcname}, '.idl') . '.c';
33 $self->parse($filename);
34 $self->open_stream($filename);
35 $self->{done_hash} = {};
36 $self->{num_key} = 'num_skel_c';
37 return $self;
38 }
39
40 sub parse {
41 my $self = shift;
42 my ($filename) = @_;
43 $self->{merge} = {};
44 $self->{merge_comment} = {};
45 return unless ( -r $filename);
46 open my $IN, '<', $filename
47 or die "can't open $filename ($!).\n";
48 while (<$IN>) {
49 if (/\/\* START_EDIT (\(([^\)]+)\) )?\*\//) {
50 my $key = $2 || $self->{srcname};
51 my $code = q{};
52 while (<$IN>) {
53 last if (/\/\* STOP_EDIT/);
54 $code .= $_;
55 }
56 $self->{merge}->{$key} = $code;
57 }
58 elsif (/\/\* START_COMMENT (\(([^\)]+)\) )?\*\//) {
59 my $key = $2 || $self->{srcname};
60 my $code = q{};
61 while (<$IN>) {
62 last if (/\/\* STOP_COMMENT/);
63 $code .= $_;
64 }
65 $self->{merge_comment}->{$key} = $code;
66 }
67 }
68 close $IN;
69 }
70
71 sub merge {
72 my $self = shift;
73 my ($key, $default) = @_;
74 $default = q{} unless ($default);
75 $key = $self->{srcname} unless ($key);
76 if (exists $self->{merge}->{$key}) {
77 return $self->{merge}->{$key};
78 }
79 else {
80 return $default . "\n"
81 }
82 }
83
84 sub merge_comment {
85 my $self = shift;
86 my ($key) = @_;
87 $key = $self->{srcname} unless ($key);
88 if (exists $self->{merge_comment}->{$key}) {
89 return $self->{merge_comment}->{$key};
90 }
91 else {
92 return q{};
93 }
94 }
95
96 sub open_stream {
97 my $self = shift;
98 my ($filename) = @_;
99 open $self->{out}, '>', $filename
100 or die "can't open $filename ($!).\n";
101 $self->{filename} = $filename;
102 }
103
104 sub _get_defn {
105 my $self = shift;
106 my ($defn) = @_;
107 if (ref $defn) {
108 return $defn;
109 }
110 else {
111 return $self->{symbtab}->Lookup($defn);
112 }
113 }
114
115 #
116 # 3.5 OMG IDL Specification
117 #
118
119 sub visitSpecification {
120 my $self = shift;
121 my ($node) = @_;
122 my $filename = $self->{prefix} . basename($self->{srcname}, '.idl') . '.h';
123 my $FH = $self->{out};
124 print $FH "/* This file was partialy generated (by ",basename($0),").*/\n";
125 print $FH "/* From file : ",$self->{srcname},", ",$self->{srcname_size}," octets, ",POSIX::ctime($self->{srcname_mtime});
126 print $FH " */\n";
127 print $FH "\n";
128 print $FH "/* START_EDIT */\n";
129 print $FH $self->merge();
130 print $FH "/* STOP_EDIT */\n";
131 print $FH "\n";
132 print $FH "#include \"",$filename,"\"\n";
133 print $FH "\n";
134 foreach (@{$node->{list_decl}}) {
135 $self->_get_defn($_)->visit($self);
136 }
137 print $FH "\n";
138 print $FH "/* end of file : ",$self->{filename}," */\n";
139 close $FH;
140 }
141
142 #
143 # 3.7 Module Declaration
144 #
145
146 sub visitModules {
147 my $self = shift;
148 my ($node) = @_;
149 unless (exists $node->{$self->{num_key}}) {
150 $node->{$self->{num_key}} = 0;
151 }
152 my $module = ${$node->{list_decl}}[$node->{$self->{num_key}}];
153 $module->visit($self);
154 $node->{$self->{num_key}} ++;
155 }
156
157 sub visitModule {
158 my $self = shift;
159 my ($node) = @_;
160 if ($self->{srcname} eq $node->{filename}) {
161 my $defn = $self->{symbtab}->Lookup($node->{full});
162 my $FH = $self->{out};
163 print $FH "/*\n";
164 print $FH " * begin of module ",$defn->{c_name},"\n";
165 print $FH " */\n";
166 foreach (@{$node->{list_decl}}) {
167 $self->_get_defn($_)->visit($self);
168 }
169 print $FH "/*\n";
170 print $FH " * end of module ",$defn->{c_name},"\n";
171 print $FH " */\n";
172 }
173 }
174
175 #
176 # 3.8 Interface Declaration
177 #
178
179 sub visitRegularInterface {
180 my $self = shift;
181 my ($node) = @_;
182 if ($self->{srcname} eq $node->{filename}) {
183 my $FH = $self->{out};
184 print $FH "/* START_EDIT (",$node->{c_name},") */\n";
185 print $FH $self->merge($node->{c_name});
186 print $FH "/* STOP_EDIT (",$node->{c_name},") */\n";
187 print $FH "\n";
188 print $FH "/*\n";
189 print $FH " * begin of interface ",$node->{c_name},"\n";
190 print $FH " */\n";
191 $self->{itf} = $node->{c_name};
192 foreach (sort keys %{$node->{hash_attribute_operation}}) {
193 my $elt = ${$node->{hash_attribute_operation}}{$_};
194 $self->_get_defn($elt)->visit($self);
195 }
196 print $FH "/*\n";
197 print $FH " * end of interface ",$node->{c_name},"\n";
198 print $FH " */\n";
199 }
200 }
201
202 sub visitForwardRegularInterface {
203 # empty
204 }
205
206 sub visitBaseInterface {
207 # C mapping is aligned with CORBA 2.1
208 }
209
210 sub visitForwardBaseInterface {
211 # C mapping is aligned with CORBA 2.1
212 }
213
214 #
215 # 3.10 Constant Declaration
216 #
217
218 sub visitConstant {
219 # empty
220 }
221
222 #
223 # 3.11 Type Declaration
224 #
225
226 sub visitTypeDeclarators {
227 # empty
228 }
229
230 sub visitTypeDeclarator {
231 # empty
232 }
233
234 sub visitNativeType {
235 # C mapping is aligned with CORBA 2.1
236 }
237
238 sub visitStructType {
239 # empty
240 }
241
242 sub visitUnionType {
243 # empty
244 }
245
246 sub visitForwardStructType {
247 # empty
248 }
249
250 sub visitForwardUnionType {
251 # empty
252 }
253
254 sub visitEnumType {
255 # empty
256 }
257
258 #
259 # 3.12 Exception Declaration
260 #
261
262 sub visitException {
263 # empty
264 }
265
266 #
267 # 3.13 Operation Declaration
268 #
269
270 sub visitOperation {
271 my $self = shift;
272 my ($node) = @_;
273 my $name = $self->{prefix} . $self->{itf} . '_' . $node->{c_name};
274 my $FH = $self->{out};
275 print $FH "\n";
276 print $FH "/* START_COMMENT (",$name,") */\n";
277 print $FH $self->merge_comment($name);
278 print $FH "/* STOP_COMMENT (",$name,") */\n";
279 print $FH "/* ARGSUSED */\n";
280 if (exists $node->{modifier}) {
281 print $FH $node->{c_arg}," // oneway\n";
282 }
283 else {
284 print $FH $node->{c_arg},"\n";
285 }
286 print $FH $name,"(\n";
287 print $FH "\t",$self->{itf}," _o,\n";
288 foreach (@{$node->{list_param}}) { # parameter
289 my $type = $self->_get_defn($_->{type});
290 print $FH "\t",$_->{c_arg},", // ",$_->{attr};
291 print $FH " (variable length)\n" if (defined $type->{length});
292 print $FH " (fixed length)\n" unless (defined $type->{length});
293 }
294 if (exists $node->{list_context}) {
295 print $FH "\tCORBA_Context _ctx,\n";
296 }
297 print $FH "\tCORBA_Environment * _ev\n";
298 print $FH ")\n";
299 print $FH "{\n";
300 print $FH "/* START_EDIT (",$name,") */\n";
301 my $except;
302 if (exists $node->{list_raise}) {
303 foreach (@{$node->{list_raise}}) { # exception
304 my $defn = $self->_get_defn($_);
305 $except = "\tstatic " . $defn->{c_name} . " _" . $defn->{c_name} . ";\n";
306 }
307 }
308 print $FH $self->merge($name, $except);
309 print $FH "/* STOP_EDIT (",$name,") */\n";
310 print $FH "}\n";
311 print $FH "\n";
312 }
313
314 #
315 # 3.14 Attribute Declaration
316 #
317
318 sub visitAttribute {
319 my $self = shift;
320 my ($node) = @_;
321 $node->{_get}->visit($self);
322 $node->{_set}->visit($self) if (exists $node->{_set});
323 }
324
325 #
326 # 3.15 Repository Identity Related Declarations
327 #
328
329 sub visitTypeId {
330 # empty
331 }
332
333 sub visitTypePrefix {
334 # empty
335 }
336
337 1;
338