| 1 | |||
| 2 | package CORBA::XPIDL::HeaderVisitor; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.21'; | ||
| 8 | |||
| 9 | use File::Basename; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my $proto = shift; | ||
| 13 | my $class = ref($proto) || $proto; | ||
| 14 | my $self = {}; | ||
| 15 | bless $self, $class; | ||
| 16 | my ($parser, $base_includes) = @_; | ||
| 17 | $self->{srcname} = $parser->YYData->{srcname}; | ||
| 18 | $self->{symbtab} = $parser->YYData->{symbtab}; | ||
| 19 | $self->{base_includes} = $base_includes; | ||
| 20 | my $filename; | ||
| 21 | if ($parser->YYData->{opt_e}) { | ||
| 22 | $filename = $parser->YYData->{opt_e}; | ||
| 23 | } | ||
| 24 | else { | ||
| 25 | if ($parser->YYData->{opt_o}) { | ||
| 26 | $filename = $parser->YYData->{opt_o} . '.h'; | ||
| 27 | } | ||
| 28 | else { | ||
| 29 | $filename = basename($self->{srcname}, '.idl') . '.h'; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | $self->open_stream($filename); | ||
| 33 | $self->{num_key} = 'num_header_xp'; | ||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub open_stream { | ||
| 38 | my $self = shift; | ||
| 39 | my ($filename) = @_; | ||
| 40 | open $self->{out}, '>', $filename | ||
| 41 | or die "can't open $filename ($!).\n"; | ||
| 42 | $self->{filename} = $filename; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _get_defn { | ||
| 46 | my $self = shift; | ||
| 47 | my ($defn) = @_; | ||
| 48 | if (ref $defn) { | ||
| 49 | return $defn; | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | return $self->{symbtab}->Lookup($defn); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _is_dipper { | ||
| 57 | my $self = shift; | ||
| 58 | my ($node) = @_; # type | ||
| 59 | return $node->hasProperty('domstring') | ||
| 60 | || $node->hasProperty('utf8string') | ||
| 61 | || $node->hasProperty('cstring') | ||
| 62 | || $node->hasProperty('astring'); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub _classname_iid { | ||
| 66 | my $self = shift; | ||
| 67 | my ($node) = @_; | ||
| 68 | my $idf = $node->{idf}; | ||
| 69 | $idf =~ s/^ns/NS_/; # backcompat naming styles | ||
| 70 | my $classname = uc $idf; | ||
| 71 | $classname .= '_IID'; | ||
| 72 | return $classname; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _doc_comments { | ||
| 76 | my $self = shift; | ||
| 77 | my ($node) = @_; | ||
| 78 | return q{} unless ($node->{doc}); | ||
| 79 | my $FH = $self->{out}; | ||
| 80 | my @doc = split /\n/, $node->{doc}; | ||
| 81 | shift @doc if ($doc[0] =~ /^\s*$/); | ||
| 82 | pop @doc if ($doc[-1] =~ /^\s*$/); | ||
| 83 | print $FH "/**\n"; | ||
| 84 | foreach (@doc) { | ||
| 85 | print $FH " *",$_,"\n"; | ||
| 86 | } | ||
| 87 | print $FH " */\n"; | ||
| 88 | } | ||
| 89 | |||
| 90 | # | ||
| 91 | # 3.5 OMG IDL Specification | ||
| 92 | # | ||
| 93 | |||
| 94 | sub visitSpecification { | ||
| 95 | my $self = shift; | ||
| 96 | my ($node) = @_; | ||
| 97 | my $FH = $self->{out}; | ||
| 98 | |||
| 99 | my $basename = basename($self->{srcname}, '.idl'); | ||
| 100 | print $FH "/*\n"; | ||
| 101 | print $FH " * DO NOT EDIT. THIS FILE IS GENERATED FROM ",$self->{srcname},"\n"; | ||
| 102 | print $FH " */\n"; | ||
| 103 | print $FH "\n"; | ||
| 104 | print $FH "#ifndef __gen_",$basename,"_h__\n"; | ||
| 105 | print $FH "#define __gen_",$basename,"_h__\n"; | ||
| 106 | |||
| 107 | if (scalar @{$self->{base_includes}}) { | ||
| 108 | print $FH "\n"; | ||
| 109 | foreach (@{$self->{base_includes}}) { | ||
| 110 | my $basename = basename($_, '.idl') ; | ||
| 111 | print $FH "\n"; | ||
| 112 | print $FH "#ifndef __gen_",$basename,"_h__\n"; | ||
| 113 | print $FH "#include \"",$basename,".h\"\n"; | ||
| 114 | print $FH "#endif\n"; | ||
| 115 | } | ||
| 116 | print $FH "\n"; | ||
| 117 | } | ||
| 118 | if (exists $node->{list_import}) { | ||
| 119 | print $FH "\n"; | ||
| 120 | foreach (@{$node->{list_import}}) { | ||
| 121 | my $basename = $_->{value}; | ||
| 122 | $basename =~ s/^:://; | ||
| 123 | $basename =~ s/::/_/g; | ||
| 124 | print $FH "\n"; | ||
| 125 | print $FH "#ifndef __gen_",$basename,"_h__\n"; | ||
| 126 | print $FH "#include \"",$basename,".h\"\n"; | ||
| 127 | print $FH "#endif\n"; | ||
| 128 | } | ||
| 129 | print $FH "\n"; | ||
| 130 | } | ||
| 131 | # Support IDL files that don't include a root IDL file that defines | ||
| 132 | # NS_NO_VTABLE. | ||
| 133 | print $FH "/* For IDL files that don't want to include root IDL files. */\n"; | ||
| 134 | print $FH "#ifndef NS_NO_VTABLE\n"; | ||
| 135 | print $FH "#define NS_NO_VTABLE\n"; | ||
| 136 | print $FH "#endif\n"; | ||
| 137 | |||
| 138 | foreach (@{$node->{list_decl}}) { | ||
| 139 | $self->_get_defn($_)->visit($self); | ||
| 140 | } | ||
| 141 | |||
| 142 | print $FH "\n"; | ||
| 143 | print $FH "#endif /* __gen_",$basename,"_h__ */\n"; | ||
| 144 | close $FH; | ||
| 145 | } | ||
| 146 | |||
| 147 | # | ||
| 148 | # 3.7 Module Declaration | ||
| 149 | # | ||
| 150 | |||
| 151 | sub visitModules { | ||
| 152 | my $self = shift; | ||
| 153 | my ($node) = @_; | ||
| 154 | unless (exists $node->{$self->{num_key}}) { | ||
| 155 | $node->{$self->{num_key}} = 0; | ||
| 156 | } | ||
| 157 | my $module = ${$node->{list_decl}}[$node->{$self->{num_key}}]; | ||
| 158 | $module->visit($self); | ||
| 159 | $node->{$self->{num_key}} ++; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub visitModule { | ||
| 163 | my $self = shift; | ||
| 164 | my ($node) = @_; | ||
| 165 | if ($self->{srcname} eq $node->{filename}) { | ||
| 166 | foreach (@{$node->{list_decl}}) { | ||
| 167 | $self->_get_defn($_)->visit($self); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | # | ||
| 173 | # 3.8 Interface Declaration | ||
| 174 | # | ||
| 175 | |||
| 176 | sub visitRegularInterface { | ||
| 177 | my $self = shift; | ||
| 178 | my ($node) = @_; | ||
| 179 | my $FH = $self->{out}; | ||
| 180 | if ($self->{srcname} eq $node->{filename}) { | ||
| 181 | my $classname = $node->{idf}; | ||
| 182 | my $indent = q{ } x 2; | ||
| 183 | print $FH "\n"; | ||
| 184 | print $FH "/* starting interface: ",$classname," */\n"; | ||
| 185 | my $name_space = $node->getProperty('namespace'); | ||
| 186 | if (defined $name_space) { | ||
| 187 | print $FH "/* namespace: ",$name_space," */\n"; | ||
| 188 | print $FH "/* fully qualified name: ",$name_space,".",$classname," */\n"; | ||
| 189 | } | ||
| 190 | my $iid = lc $node->getProperty('uuid'); | ||
| 191 | my $classname_iid = $self->_classname_iid($node); | ||
| 192 | print $FH "#define ",$classname_iid,"_STR \"",$iid,"\"\n"; | ||
| 193 | print $FH "\n"; | ||
| 194 | print $FH "#define ",$classname_iid," \\\n"; | ||
| 195 | print $FH " {0x",substr($iid,0,8),", 0x",substr($iid,9,4),", "; | ||
| 196 | print $FH "0x",substr($iid,14,4),", \\\n"; | ||
| 197 | print $FH " { 0x",substr($iid,19,2),", 0x",substr($iid,21,2),", "; | ||
| 198 | print $FH "0x",substr($iid,24,2),", 0x",substr($iid,26,2),", "; | ||
| 199 | print $FH "0x",substr($iid,28,2),", 0x",substr($iid,30,2),", "; | ||
| 200 | print $FH "0x",substr($iid,32,2),", 0x",substr($iid,34,2)," }}\n"; | ||
| 201 | print $FH "\n"; | ||
| 202 | |||
| 203 | $self->_doc_comments($node) if (exists $node->{doc}); | ||
| 204 | |||
| 205 | # NS_NO_VTABLE is defined in nsISupportsUtils.h, and defined on windows | ||
| 206 | # to __declspec(novtable) on windows. This optimization is safe | ||
| 207 | # whenever the constructor calls no virtual methods. Writing in IDL | ||
| 208 | # almost guarantees this, except for the case when a %{C++ block occurs in | ||
| 209 | # the interface. We detect that case, and emit a macro call that disables | ||
| 210 | # the optimization. | ||
| 211 | my $keepvtable; | ||
| 212 | foreach (@{$node->{list_decl}}) { | ||
| 213 | my $defn = $self->_get_defn($_); | ||
| 214 | $keepvtable = 1 if ($defn->isa('CodeFragment')); | ||
| 215 | } | ||
| 216 | |||
| 217 | # The interface declaration itself. | ||
| 218 | print $FH "class ",($keepvtable ? q{} : "NS_NO_VTABLE "),$classname; | ||
| 219 | if (exists $node->{inheritance}) { | ||
| 220 | print $FH " : "; | ||
| 221 | my $base = $self->_get_defn(${$node->{inheritance}->{list_interface}}[0]); | ||
| 222 | print $FH "public ",$base->{idf}; | ||
| 223 | } | ||
| 224 | print $FH " {\n"; | ||
| 225 | print $FH " public: \n"; | ||
| 226 | print $FH "\n"; | ||
| 227 | print $FH " NS_DEFINE_STATIC_IID_ACCESSOR(",$classname_iid,")\n"; | ||
| 228 | print $FH "\n"; | ||
| 229 | foreach (@{$node->{list_decl}}) { | ||
| 230 | $self->_get_defn($_)->visit($self); | ||
| 231 | } | ||
| 232 | print $FH "};\n"; | ||
| 233 | print $FH "\n"; | ||
| 234 | |||
| 235 | my @attribute_operation = (); | ||
| 236 | foreach (@{$node->{list_decl}}) { | ||
| 237 | my $defn = $self->_get_defn($_); | ||
| 238 | if ($defn->isa('Operation')) { | ||
| 239 | push @attribute_operation, $defn; | ||
| 240 | } | ||
| 241 | elsif ($defn->isa('Attributes')) { | ||
| 242 | foreach (@{$defn->{list_decl}}) { | ||
| 243 | my $defn = $self->_get_defn($_); | ||
| 244 | push @attribute_operation, $defn; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | } | ||
| 248 | # #define NS_DECL_NSIFOO - create method prototypes that can be used in | ||
| 249 | # class definitions that support this interface. | ||
| 250 | # | ||
| 251 | # Walk the tree explicitly to prototype a reworking of xpidl to get rid of | ||
| 252 | # the callback mechanism. | ||
| 253 | print $FH "/* Use this macro when declaring classes that implement this interface. */\n"; | ||
| 254 | print $FH "#define NS_DECL_",uc($classname)," \\\n"; | ||
| 255 | if (scalar @attribute_operation) { | ||
| 256 | my $first = 1; | ||
| 257 | foreach my $defn (@attribute_operation) { | ||
| 258 | print $FH "; \\\n" unless ($first); | ||
| 259 | if ($defn->isa('Operation')) { | ||
| 260 | print $FH $indent; | ||
| 261 | $self->_method($defn, 'AS_DECL', q{}); | ||
| 262 | } | ||
| 263 | else { | ||
| 264 | print $FH $indent; | ||
| 265 | $self->_attr_getter($defn, 'AS_DECL', q{}); | ||
| 266 | unless (exists $defn->{modifier}) { # readonly | ||
| 267 | print $FH "; \\\n"; | ||
| 268 | print $FH $indent; | ||
| 269 | $self->_attr_setter($defn, 'AS_DECL', q{}); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | $first = 0; | ||
| 273 | } | ||
| 274 | print $FH ";\n"; | ||
| 275 | } | ||
| 276 | else { | ||
| 277 | print $FH "/* no methods! */\n"; | ||
| 278 | } | ||
| 279 | print $FH "\n"; | ||
| 280 | |||
| 281 | # XXX abstract above and below into one function? | ||
| 282 | # | ||
| 283 | # #define NS_FORWARD_NSIFOO - create forwarding methods that can delegate | ||
| 284 | # behavior from in implementation to another object. As generated by | ||
| 285 | # idlc. | ||
| 286 | print $FH "/* Use this macro to declare functions that forward the behavior of this interface to another object. */\n"; | ||
| 287 | print $FH "#define NS_FORWARD_",uc($classname),"(_to) \\\n"; | ||
| 288 | if (scalar @attribute_operation) { | ||
| 289 | my $first = 1; | ||
| 290 | foreach my $defn (@attribute_operation) { | ||
| 291 | print $FH "; } \\\n" unless ($first); | ||
| 292 | if ($defn->isa('Operation')) { | ||
| 293 | print $FH $indent; | ||
| 294 | $self->_method($defn, 'AS_DECL', q{}); | ||
| 295 | print $FH " { return _to "; | ||
| 296 | $self->_method($defn, 'AS_CALL', q{}); | ||
| 297 | } | ||
| 298 | else { | ||
| 299 | print $FH $indent; | ||
| 300 | $self->_attr_getter($defn, 'AS_DECL', q{}); | ||
| 301 | print $FH " { return _to "; | ||
| 302 | $self->_attr_getter($defn, 'AS_CALL', q{}); | ||
| 303 | unless (exists $defn->{modifier}) { # readonly | ||
| 304 | print $FH "; } \\\n"; | ||
| 305 | print $FH $indent; | ||
| 306 | $self->_attr_setter($defn, 'AS_DECL', q{}); | ||
| 307 | print $FH " { return _to "; | ||
| 308 | $self->_attr_setter($defn, 'AS_CALL', q{}); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | $first = 0; | ||
| 312 | } | ||
| 313 | print $FH "; }\n"; | ||
| 314 | } | ||
| 315 | else { | ||
| 316 | print $FH "/* no methods! */\n"; | ||
| 317 | } | ||
| 318 | print $FH "\n"; | ||
| 319 | |||
| 320 | # XXX abstract above and below into one function? | ||
| 321 | # | ||
| 322 | # #define NS_FORWARD_SAFE_NSIFOO - create forwarding methods that can delegate | ||
| 323 | # behavior from in implementation to another object. As generated by | ||
| 324 | # idlc. | ||
| 325 | print $FH "/* Use this macro to declare functions that forward the behavior of this interface to another object in a safe way. */\n"; | ||
| 326 | print $FH "#define NS_FORWARD_SAFE_",uc($classname),"(_to) \\\n"; | ||
| 327 | if (scalar @attribute_operation) { | ||
| 328 | my $first = 1; | ||
| 329 | foreach my $defn (@attribute_operation) { | ||
| 330 | print $FH "; } \\\n" unless ($first); | ||
| 331 | if ($defn->isa('Operation')) { | ||
| 332 | print $FH $indent; | ||
| 333 | $self->_method($defn, 'AS_DECL', q{}); | ||
| 334 | print $FH " { return !_to ? NS_ERROR_NULL_POINTER : _to->"; | ||
| 335 | $self->_method($defn, 'AS_CALL', q{}); | ||
| 336 | } | ||
| 337 | else { | ||
| 338 | print $FH $indent; | ||
| 339 | $self->_attr_getter($defn, 'AS_DECL', q{}); | ||
| 340 | print $FH " { return !_to ? NS_ERROR_NULL_POINTER : _to->"; | ||
| 341 | $self->_attr_getter($defn, 'AS_CALL', q{}); | ||
| 342 | unless (exists $defn->{modifier}) { # readonly | ||
| 343 | print $FH "; } \\\n"; | ||
| 344 | print $FH $indent; | ||
| 345 | $self->_attr_setter($defn, 'AS_DECL', q{}); | ||
| 346 | print $FH " { return !_to ? NS_ERROR_NULL_POINTER : _to->"; | ||
| 347 | $self->_attr_setter($defn, 'AS_CALL', q{}); | ||
| 348 | } | ||
| 349 | } | ||
| 350 | $first = 0; | ||
| 351 | } | ||
| 352 | print $FH "; }\n"; | ||
| 353 | } | ||
| 354 | else { | ||
| 355 | print $FH "/* no methods! */\n"; | ||
| 356 | } | ||
| 357 | print $FH "\n"; | ||
| 358 | |||
| 359 | # Build a sample implementation template. | ||
| 360 | my $classNameImpl; | ||
| 361 | if ($classname =~ /^..I/) { | ||
| 362 | $classNameImpl = substr($classname, 0, 2) . substr($classname, 3); | ||
| 363 | } | ||
| 364 | else { | ||
| 365 | $classNameImpl = '_MYCLASS_'; | ||
| 366 | } | ||
| 367 | print $FH "#if 0\n"; | ||
| 368 | print $FH "/* Use the code below as a template for the implementation class for this interface. */\n"; | ||
| 369 | print $FH "\n"; | ||
| 370 | print $FH "/* Header file */\n"; | ||
| 371 | print $FH "class ",$classNameImpl," : public ",$classname,"\n"; | ||
| 372 | print $FH "{\n"; | ||
| 373 | print $FH "public:\n"; | ||
| 374 | print $FH $indent,"NS_DECL_ISUPPORTS\n"; | ||
| 375 | print $FH $indent,"NS_DECL_",uc($classname),"\n"; | ||
| 376 | print $FH "\n"; | ||
| 377 | print $FH $indent,$classNameImpl,"();\n"; | ||
| 378 | print $FH $indent,"virtual ~",$classNameImpl,"();\n"; | ||
| 379 | print $FH $indent,"/* additional members */\n"; | ||
| 380 | print $FH "};\n"; | ||
| 381 | print $FH "\n"; | ||
| 382 | print $FH "/* Implementation file */\n"; | ||
| 383 | print $FH "NS_IMPL_ISUPPORTS1(",$classNameImpl,", ",$classname,")\n"; | ||
| 384 | print $FH "\n"; | ||
| 385 | print $FH $classNameImpl,"::",$classNameImpl,"()\n"; | ||
| 386 | print $FH "{\n"; | ||
| 387 | print $FH $indent,"/* member initializers and constructor code */\n"; | ||
| 388 | print $FH "}\n"; | ||
| 389 | print $FH "\n"; | ||
| 390 | print $FH $classNameImpl,"::~",$classNameImpl,"()\n"; | ||
| 391 | print $FH "{\n"; | ||
| 392 | print $FH $indent,"/* destructor code */\n"; | ||
| 393 | print $FH "}\n"; | ||
| 394 | print $FH "\n"; | ||
| 395 | foreach my $defn (@attribute_operation) { | ||
| 396 | if ($defn->isa('Operation')) { | ||
| 397 | $self->_doc_comments($defn); | ||
| 398 | $self->_method($defn, 'AS_IMPL', $classNameImpl); | ||
| 399 | print $FH "\n"; | ||
| 400 | print $FH "{\n"; | ||
| 401 | print $FH $indent,$indent,"return NS_ERROR_NOT_IMPLEMENTED;\n"; | ||
| 402 | print $FH "}\n"; | ||
| 403 | print $FH "\n"; | ||
| 404 | } | ||
| 405 | else { | ||
| 406 | $self->_doc_comments($defn); | ||
| 407 | $self->_attr_getter($defn, 'AS_IMPL', $classNameImpl); | ||
| 408 | print $FH "\n"; | ||
| 409 | print $FH "{\n"; | ||
| 410 | print $FH $indent,$indent,"return NS_ERROR_NOT_IMPLEMENTED;\n"; | ||
| 411 | print $FH "}\n"; | ||
| 412 | unless (exists $defn->{modifier}) { # readonly | ||
| 413 | $self->_attr_setter($defn, 'AS_IMPL', $classNameImpl); | ||
| 414 | print $FH "\n"; | ||
| 415 | print $FH "{\n"; | ||
| 416 | print $FH $indent,$indent,"return NS_ERROR_NOT_IMPLEMENTED;\n"; | ||
| 417 | print $FH "}\n"; | ||
| 418 | } | ||
| 419 | print $FH "\n"; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | print $FH "/* End of implementation class template. */\n"; | ||
| 423 | print $FH "#endif\n"; | ||
| 424 | print $FH "\n"; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | sub visitForwardRegularInterface { | ||
| 429 | my $self = shift; | ||
| 430 | my ($node) = @_; | ||
| 431 | my $FH = $self->{out}; | ||
| 432 | if ($self->{srcname} eq $node->{filename}) { | ||
| 433 | $self->_doc_comments($node); | ||
| 434 | print $FH "class ",$node->{idf},"; /* forward declaration */\n"; | ||
| 435 | print $FH "\n"; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | |||
| 439 | sub visitBaseInterface { | ||
| 440 | # empty | ||
| 441 | } | ||
| 442 | |||
| 443 | sub visitForwardBaseInterface { | ||
| 444 | # empty | ||
| 445 | } | ||
| 446 | |||
| 447 | # | ||
| 448 | # 3.10 Constant Declaration | ||
| 449 | # | ||
| 450 | |||
| 451 | sub visitConstant { | ||
| 452 | my $self = shift; | ||
| 453 | my ($node) = @_; | ||
| 454 | my $FH = $self->{out}; | ||
| 455 | $self->_doc_comments($node); | ||
| 456 | my $indent = q{ } x 2; | ||
| 457 | my $value = $node->{value}->{value}; | ||
| 458 | my $type = $self->_get_defn($node->{type}); | ||
| 459 | while ($type->isa('TypeDeclarator')) { | ||
| 460 | $type = $self->_get_defn($type->{type}); | ||
| 461 | } | ||
| 462 | $value .= 'U' if (exists $type->{value} and $type->{value} =~ /^unsigned/); | ||
| 463 | print $FH $indent,"enum { ",$node->{idf}," = ",$value," };\n"; | ||
| 464 | print $FH "\n"; | ||
| 465 | } | ||
| 466 | |||
| 467 | # | ||
| 468 | # 3.11 Type Declaration | ||
| 469 | # | ||
| 470 | |||
| 471 | sub visitTypeDeclarators { | ||
| 472 | my $self = shift; | ||
| 473 | my ($node) = @_; | ||
| 474 | my $type = $self->_get_defn($node->{type}); | ||
| 475 | return if ($type->isa('SequenceType')); | ||
| 476 | |||
| 477 | my $FH = $self->{out}; | ||
| 478 | if ($self->{srcname} eq $node->{filename}) { | ||
| 479 | $self->_doc_comments($node); | ||
| 480 | foreach (@{$node->{list_decl}}) { | ||
| 481 | $self->_get_defn($_)->visit($self); | ||
| 482 | } | ||
| 483 | print $FH "\n"; | ||
| 484 | } | ||
| 485 | } | ||
| 486 | |||
| 487 | sub visitTypeDeclarator { | ||
| 488 | my $self = shift; | ||
| 489 | my ($node) = @_; | ||
| 490 | my $FH = $self->{out}; | ||
| 491 | my $type = $self->_get_defn($node->{type}); | ||
| 492 | print $FH "typedef ",$type->{xp_name}," ",$node->{idf}; | ||
| 493 | if (exists $node->{array_size}) { # usefull ? | ||
| 494 | foreach (@{$node->{array_size}}) { | ||
| 495 | print $FH "[",$_->{value},"]"; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | print $FH ";\n"; | ||
| 499 | } | ||
| 500 | |||
| 501 | sub visitNativeType { | ||
| 502 | # empty | ||
| 503 | } | ||
| 504 | |||
| 505 | # | ||
| 506 | # 3.11.2 Constructed Types | ||
| 507 | # | ||
| 508 | |||
| 509 | sub visitStructType { | ||
| 510 | # empty | ||
| 511 | } | ||
| 512 | |||
| 513 | sub visitUnionType { | ||
| 514 | # empty | ||
| 515 | } | ||
| 516 | |||
| 517 | sub visitForwardStructType { | ||
| 518 | # empty | ||
| 519 | } | ||
| 520 | |||
| 521 | sub visitForwardUnionType { | ||
| 522 | # empty | ||
| 523 | } | ||
| 524 | |||
| 525 | sub visitEnumType { | ||
| 526 | # empty | ||
| 527 | } | ||
| 528 | |||
| 529 | # | ||
| 530 | # 3.12 Exception Declaration | ||
| 531 | # | ||
| 532 | |||
| 533 | sub visitException { | ||
| 534 | # empty | ||
| 535 | } | ||
| 536 | |||
| 537 | # | ||
| 538 | # 3.13 Operation Declaration | ||
| 539 | # | ||
| 540 | |||
| 541 | sub visitOperation { | ||
| 542 | my $self = shift; | ||
| 543 | my ($node) = @_; | ||
| 544 | my $FH = $self->{out}; | ||
| 545 | $self->_doc_comments($node); | ||
| 546 | my $indent = q{ } x 2; | ||
| 547 | print $FH $indent; | ||
| 548 | $self->_method($node, 'AS_DECL', q{}); | ||
| 549 | print $FH " = 0;\n"; | ||
| 550 | print $FH "\n"; | ||
| 551 | } | ||
| 552 | |||
| 553 | # Shared between the interface class declaration and the NS_DECL_IFOO macro | ||
| 554 | # provided to aid declaration of implementation classes. | ||
| 555 | # mode... | ||
| 556 | # AS_DECL writes 'NS_IMETHOD foo(string bar, long sil)' | ||
| 557 | # AS_IMPL writes 'NS_IMETHODIMP className::foo(string bar, long sil)' | ||
| 558 | # AS_CALL writes 'foo(bar, sil)' | ||
| 559 | sub _method { | ||
| 560 | my $self = shift; | ||
| 561 | my ($node, $mode, $className) = @_; | ||
| 562 | my $FH = $self->{out}; | ||
| 563 | my $type = $self->_get_defn($node->{type}); | ||
| 564 | my $op_notxpcom = $node->hasProperty('notxpcom'); | ||
| 565 | |||
| 566 | if ($mode eq 'AS_DECL') { | ||
| 567 | if ($op_notxpcom) { | ||
| 568 | print $FH "NS_IMETHOD_(",$type->{xp_name},")"; | ||
| 569 | } | ||
| 570 | else { | ||
| 571 | print $FH 'NS_IMETHOD'; | ||
| 572 | } | ||
| 573 | print $FH " "; | ||
| 574 | } | ||
| 575 | elsif ($mode eq 'AS_IMPL') { | ||
| 576 | if ($op_notxpcom) { | ||
| 577 | print $FH "NS_IMETHODIMP_(",$type->{xp_name},")"; | ||
| 578 | } | ||
| 579 | else { | ||
| 580 | print $FH "NS_IMETHODIMP"; | ||
| 581 | } | ||
| 582 | print $FH " "; | ||
| 583 | } | ||
| 584 | |||
| 585 | if ($mode eq 'AS_IMPL') { | ||
| 586 | print $FH $className,"::",ucfirst($node->{idf}),"("; | ||
| 587 | } | ||
| 588 | else { | ||
| 589 | print $FH ucfirst($node->{idf}),"("; | ||
| 590 | } | ||
| 591 | |||
| 592 | my $first = 1; | ||
| 593 | foreach (@{$node->{list_param}}) { | ||
| 594 | next if ($_->isa('Ellipsis')); | ||
| 595 | print $FH ", " unless ($first); | ||
| 596 | if ($mode eq 'AS_DECL' or $mode eq 'AS_IMPL') { | ||
| 597 | $self->_param($_); | ||
| 598 | } | ||
| 599 | else { | ||
| 600 | print $FH $_->{idf}; | ||
| 601 | } | ||
| 602 | $first = 0; | ||
| 603 | } | ||
| 604 | |||
| 605 | # make IDL return value into trailing out argument | ||
| 606 | if ( !$type->isa('VoidType') and !$op_notxpcom ) { | ||
| 607 | my $fake_param = _Build CORBA::IDL::Node ( | ||
| 608 | 'attr' => 'out', | ||
| 609 | 'type' => $type, | ||
| 610 | 'idf' => '_retval', | ||
| 611 | ); | ||
| 612 | bless $fake_param, 'CORBA::IDL::Parameter'; | ||
| 613 | print $FH ", " unless ($first); | ||
| 614 | if ($mode eq 'AS_DECL' or $mode eq 'AS_IMPL') { | ||
| 615 | $self->_param($fake_param); | ||
| 616 | } | ||
| 617 | else { | ||
| 618 | print $FH "_retval"; | ||
| 619 | } | ||
| 620 | $first = 0; | ||
| 621 | } | ||
| 622 | |||
| 623 | # varargs go last | ||
| 624 | if ( scalar @{$node->{list_param}} | ||
| 625 | and ${$node->{list_param}}[-1]->isa('Ellipsis') ) { | ||
| 626 | print $FH ", " unless ($first); | ||
| 627 | if ($mode eq 'AS_DECL' or $mode eq 'AS_IMPL') { | ||
| 628 | print $FH "nsVarArgs *"; | ||
| 629 | } | ||
| 630 | print $FH "_varargs"; | ||
| 631 | $first = 0; | ||
| 632 | } | ||
| 633 | |||
| 634 | # If generated method has no arguments, output 'void' to avoid C legacy | ||
| 635 | # behavior of disabling type checking. | ||
| 636 | if ($first and $mode eq 'AS_DECL') { | ||
| 637 | print $FH "void"; | ||
| 638 | } | ||
| 639 | |||
| 640 | print $FH ")"; | ||
| 641 | } | ||
| 642 | |||
| 643 | # param generation: | ||
| 644 | # in string foo --> nsString *foo | ||
| 645 | # out string foo --> nsString **foo; | ||
| 646 | # inout string foo --> nsString **foo; | ||
| 647 | sub _param { | ||
| 648 | my $self = shift; | ||
| 649 | my ($node) = @_; | ||
| 650 | my $FH = $self->{out}; | ||
| 651 | my $type = $self->_get_defn($node->{type}); | ||
| 652 | |||
| 653 | # in string, wstring, nsid, domstring, utf8string, cstring and | ||
| 654 | # astring any explicitly marked [const] are const | ||
| 655 | if ( $node->{attr} eq 'in' | ||
| 656 | and ( $type->isa('StringType') | ||
| 657 | or $type->isa('WideStringType') | ||
| 658 | or $node->hasProperty('const') | ||
| 659 | or $type->hasProperty('nsid') | ||
| 660 | or $type->hasProperty('domstring') | ||
| 661 | or $type->hasProperty('utf8string') | ||
| 662 | or $type->hasProperty('cstring') | ||
| 663 | or $type->hasProperty('astring') ) ) { | ||
| 664 | print $FH "const "; | ||
| 665 | } | ||
| 666 | elsif ( $node->{attr} eq 'out' | ||
| 667 | and $node->hasProperty('shared') ) { | ||
| 668 | print $FH "const "; | ||
| 669 | } | ||
| 670 | |||
| 671 | print $FH $type->{xp_name}; | ||
| 672 | |||
| 673 | # unless the type ended in a *, add a space | ||
| 674 | unless ( $type->isa('StringType') | ||
| 675 | or $type->isa('WideStringType') | ||
| 676 | or $type->isa('Interface') | ||
| 677 | or $type->isa('ForwardInterface') ) { | ||
| 678 | print $FH " "; | ||
| 679 | } | ||
| 680 | |||
| 681 | # out and inout params get a bonus '*' (unless this is type that has a | ||
| 682 | # 'dipper' class that is passed in to receive 'out' data) | ||
| 683 | if ( $node->{attr} ne 'in' | ||
| 684 | and !$self->_is_dipper($type) ) { | ||
| 685 | print $FH "*"; | ||
| 686 | } | ||
| 687 | |||
| 688 | # arrays get a bonus * too | ||
| 689 | # XXX Should this be a leading '*' or a trailing "[]" ? | ||
| 690 | if ($node->hasProperty('array')) { | ||
| 691 | print $FH "*"; | ||
| 692 | } | ||
| 693 | |||
| 694 | print $FH $node->{idf}; | ||
| 695 | } | ||
| 696 | |||
| 697 | # | ||
| 698 | # 3.14 Attribute Declaration | ||
| 699 | # | ||
| 700 | |||
| 701 | sub visitAttributes { | ||
| 702 | my $self = shift; | ||
| 703 | my ($node) = @_; | ||
| 704 | foreach (@{$node->{list_decl}}) { | ||
| 705 | $self->_get_defn($_)->visit($self); | ||
| 706 | } | ||
| 707 | } | ||
| 708 | |||
| 709 | sub visitAttribute { | ||
| 710 | my $self = shift; | ||
| 711 | my ($node) = @_; | ||
| 712 | my $FH = $self->{out}; | ||
| 713 | $self->_doc_comments($node); | ||
| 714 | my $indent = q{ } x 2; | ||
| 715 | print $FH $indent; | ||
| 716 | $self->_attr_getter($node, 'AS_DECL', q{}); | ||
| 717 | print $FH " = 0;\n"; | ||
| 718 | unless (exists $node->{modifier}) { # readonly | ||
| 719 | print $FH $indent; | ||
| 720 | $self->_attr_setter($node, 'AS_DECL', q{}); | ||
| 721 | print $FH " = 0;\n"; | ||
| 722 | } | ||
| 723 | print $FH "\n"; | ||
| 724 | } | ||
| 725 | |||
| 726 | sub _attr_getter { | ||
| 727 | my $self = shift; | ||
| 728 | my ($node, $mode, $className) = @_; | ||
| 729 | my $FH = $self->{out}; | ||
| 730 | if ($mode eq 'AS_DECL') { | ||
| 731 | print $FH "NS_IMETHOD "; | ||
| 732 | } | ||
| 733 | elsif ($mode eq 'AS_IMPL') { | ||
| 734 | print $FH "NS_IMETHODIMP ",$className,"::"; | ||
| 735 | } | ||
| 736 | print $FH "Get",ucfirst($node->{idf}),"("; | ||
| 737 | if ($mode eq 'AS_DECL' or $mode eq 'AS_IMPL') { | ||
| 738 | my $type = $self->_get_defn($node->{type}); | ||
| 739 | print $FH $type->{xp_name}," "; | ||
| 740 | unless ($self->_is_dipper($type)) { | ||
| 741 | print $FH "*"; | ||
| 742 | } | ||
| 743 | } | ||
| 744 | print $FH "a",ucfirst($node->{idf}),")"; | ||
| 745 | } | ||
| 746 | |||
| 747 | sub _attr_setter { | ||
| 748 | my $self = shift; | ||
| 749 | my ($node, $mode, $className) = @_; | ||
| 750 | my $FH = $self->{out}; | ||
| 751 | if ($mode eq 'AS_DECL') { | ||
| 752 | print $FH "NS_IMETHOD "; | ||
| 753 | } | ||
| 754 | elsif ($mode eq 'AS_IMPL') { | ||
| 755 | print $FH "NS_IMETHODIMP ",$className,"::"; | ||
| 756 | } | ||
| 757 | print $FH "Set",ucfirst($node->{idf}),"("; | ||
| 758 | if ($mode eq 'AS_DECL' or $mode eq 'AS_IMPL') { | ||
| 759 | my $type = $self->_get_defn($node->{type}); | ||
| 760 | # Setters for string, wstring, nsid, domstring, utf8string, | ||
| 761 | # cstring and astring get const. | ||
| 762 | if ( $type->isa('StringType') | ||
| 763 | or $type->isa('WideStringType') | ||
| 764 | or $type->hasProperty('nsid') | ||
| 765 | or $type->hasProperty('domstring') | ||
| 766 | or $type->hasProperty('utf8string') | ||
| 767 | or $type->hasProperty('cstring') | ||
| 768 | or $type->hasProperty('astring') ) { | ||
| 769 | print $FH "const "; | ||
| 770 | } | ||
| 771 | print $FH $type->{xp_name}," "; | ||
| 772 | } | ||
| 773 | print $FH "a",ucfirst($node->{idf}),")"; | ||
| 774 | } | ||
| 775 | |||
| 776 | # | ||
| 777 | # 3.15 Repository Identity Related Declarations | ||
| 778 | # | ||
| 779 | |||
| 780 | sub visitTypeId { | ||
| 781 | # empty | ||
| 782 | } | ||
| 783 | |||
| 784 | sub visitTypePrefix { | ||
| 785 | # empty | ||
| 786 | } | ||
| 787 | |||
| 788 | # | ||
| 789 | # XPIDL | ||
| 790 | # | ||
| 791 | |||
| 792 | sub visitCodeFragment { | ||
| 793 | my $self = shift; | ||
| 794 | my ($node) = @_; | ||
| 795 | if ($self->{srcname} eq $node->{filename}) { | ||
| 796 | my @code = split /\n/, $node->{value}; | ||
| 797 | shift @code; | ||
| 798 | my $FH = $self->{out}; | ||
| 799 | foreach (@code) { | ||
| 800 | print $FH $_,"\n"; | ||
| 801 | } | ||
| 802 | } | ||
| 803 | } | ||
| 804 | |||
| 805 | 1; | ||
| 806 |