2 # Copyright (C) 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 # Copyright (C) 2006 Anders Carlsson <andersca@mac.com>
4 # Copyright (C) 2006, 2007 Samuel Weinig <sam@webkit.org>
5 # Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org>
6 # Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
7 # Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au>
8 # Copyright (C) Research In Motion Limited 2010. All rights reserved.
10 # This library is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Library General Public
12 # License as published by the Free Software Foundation; either
13 # version 2 of the License, or (at your option) any later version.
15 # This library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Library General Public License for more details.
20 # You should have received a copy of the GNU Library General Public License
21 # aint with this library; see the file COPYING.LIB. If not, write to
22 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 # Boston, MA 02110-1301, USA.
26 package CodeGeneratorCPP;
28 use constant FileNamePrefix => "WebDOM";
34 my @headerContentHeader = ();
35 my @headerContent = ();
36 my %headerForwardDeclarations = ();
38 my @implContentHeader = ();
40 my %implIncludes = ();
43 my $exceptionInit = "WebCore::ExceptionCode ec = 0;";
44 my $exceptionRaiseOnError = "webDOMRaiseError(static_cast<WebDOMExceptionCode>(ec));";
46 # Default License Templates
47 my $headerLicenseTemplate = << "EOF";
49 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
50 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
51 * Copyright (C) 2006 Samuel Weinig <sam.weinig\@gmail.com>
52 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
54 * This library is free software; you can redistribute it and/or
55 * modify it under the terms of the GNU Library General Public
56 * License as published by the Free Software Foundation; either
57 * version 2 of the License, or (at your option) any later version.
59 * This library is distributed in the hope that it will be useful,
60 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
62 * Library General Public License for more details.
64 * You should have received a copy of the GNU Library General Public License
65 * along with this library; see the file COPYING.LIB. If not, write to
66 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
67 * Boston, MA 02110-1301, USA.
71 my $implementationLicenseTemplate = << "EOF";
73 * This file is part of the WebKit open source project.
74 * This file has been generated by generate-bindings.pl. DO NOT MODIFY!
76 * This library is free software; you can redistribute it and/or
77 * modify it under the terms of the GNU Library General Public
78 * License as published by the Free Software Foundation; either
79 * version 2 of the License, or (at your option) any later version.
81 * This library is distributed in the hope that it will be useful,
82 * but WITHOUT ANY WARRANTY; without even the implied warranty of
83 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84 * Library General Public License for more details.
86 * You should have received a copy of the GNU Library General Public License
87 * along with this library; see the file COPYING.LIB. If not, write to
88 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
89 * Boston, MA 02110-1301, USA.
99 $codeGenerator = shift;
101 shift; # $outputHeadersDir
102 shift; # $useLayerOnTop
103 shift; # $preprocessor
104 shift; # $writeDependencies
106 bless($reference, $object);
110 # Params: 'domClass' struct
111 sub GenerateInterface
114 my $dataNode = shift;
117 my $name = $dataNode->name;
118 my $className = GetClassName($name);
119 my $parentClassName = "WebDOM" . GetParentImplClassName($dataNode);
121 # Start actual generation.
122 $object->GenerateHeader($dataNode);
123 $object->GenerateImplementation($dataNode);
126 $object->WriteData(FileNamePrefix . $name);
129 # Params: 'idlDocument' struct
133 my $dataNode = shift;
135 $module = $dataNode->module;
140 my $name = $codeGenerator->StripModule(shift);
143 return "WebDOMString" if $codeGenerator->IsStringType($name) or $name eq "SerializedScriptValue";
144 return "WebDOMObject" if $name eq "DOMObject";
145 return "bool" if $name eq "boolean";
146 return $name if $codeGenerator->IsPrimitiveType($name);
147 return "WebDOMCustomVoidCallback" if $name eq "VoidCallback";
149 return "WebDOM$name";
154 return $codeGenerator->StripModule(shift);
157 sub GetParentImplClassName
159 my $dataNode = shift;
161 if (@{$dataNode->parents} eq 0) {
162 return "EventTarget" if $dataNode->extendedAttributes->{"EventTarget"};
166 return $codeGenerator->StripModule($dataNode->parents(0));
171 my $dataNode = shift;
172 my $numParents = @{$dataNode->parents};
175 if ($numParents eq 0) {
176 $parent = "WebDOMObject";
177 $parent = "WebDOMEventTarget" if $dataNode->extendedAttributes->{"EventTarget"};
178 } elsif ($numParents eq 1) {
179 my $parentName = $codeGenerator->StripModule($dataNode->parents(0));
180 $parent = "WebDOM" . $parentName;
182 my @parents = @{$dataNode->parents};
183 my $firstParent = $codeGenerator->StripModule(shift(@parents));
184 $parent = "WebDOM" . $firstParent;
192 my $function = shift;
194 return 1 if $function->signature->extendedAttributes->{"Custom"};
196 # FIXME: We don't generate bindings for SVG related interfaces yet
197 return 1 if $function->signature->name =~ /getSVGDocument/;
199 if ($codeGenerator->GetArrayType($function->signature->type)) {
203 if ($codeGenerator->GetSequenceType($function->signature->type)) {
207 foreach my $param (@{$function->parameters}) {
208 return 1 if $codeGenerator->GetSequenceType($param->type);
209 return 1 if $param->extendedAttributes->{"Clamp"};
212 # FIXME: This is typically used to add script execution state arguments to the method.
213 # These functions will not compile with the C++ bindings as is, so disable them
214 # to restore compilation until a proper implementation can be developed.
215 return 1 if $function->signature->extendedAttributes->{"CallWith"};
220 my $attribute = shift;
222 return 1 if $attribute->signature->extendedAttributes->{"Custom"}
223 or $attribute->signature->extendedAttributes->{"CustomGetter"};
225 return 1 if $attribute->signature->type =~ /Constructor$/;
227 return 1 if $codeGenerator->IsTypedArrayType($attribute->signature->type);
229 if ($codeGenerator->GetArrayType($attribute->signature->type)) {
233 if ($codeGenerator->GetSequenceType($attribute->signature->type)) {
237 $codeGenerator->AssertNotSequenceType($attribute->signature->type);
239 # FIXME: This is typically used to add script execution state arguments to the method.
240 # These functions will not compile with the C++ bindings as is, so disable them
241 # to restore compilation until a proper implementation can be developed.
242 return 1 if $attribute->signature->extendedAttributes->{"CallWith"};
250 my $useConstReference = shift;
251 my $name = GetClassName($type);
253 return "int" if $type eq "long";
254 return "unsigned" if $name eq "unsigned long";
255 return "unsigned short" if $type eq "CompareHow";
256 return "double" if $name eq "Date";
258 if ($codeGenerator->IsStringType($type)) {
259 if ($useConstReference) {
260 return "const $name&";
266 return $name if $codeGenerator->IsPrimitiveType($type) or $type eq "DOMTimeStamp";
267 return "const $name&" if $useConstReference;
273 my $type = $codeGenerator->StripModule(shift);
274 return !$codeGenerator->IsNonPointerType($type) && !$codeGenerator->IsStringType($type);
280 my $type = $codeGenerator->StripModule(shift);
282 return $argName if $codeGenerator->IsPrimitiveType($type) or $codeGenerator->IsStringType($type);
283 return "static_cast<WebCore::Range::CompareHow>($argName)" if $type eq "CompareHow";
284 return "WebCore::SerializedScriptValue::create(WTF::String($argName))" if $type eq "SerializedScriptValue";
285 return "to" . GetNamespaceForClass($argName) . "($argName)";
288 sub AddForwardDeclarationsForType
290 my $type = $codeGenerator->StripModule(shift);
293 return if $codeGenerator->IsNonPointerType($type) or $codeGenerator->IsStringType($type);
295 my $class = GetClassName($type);
296 $headerForwardDeclarations{$class} = 1 if $public;
299 sub AddIncludesForType
301 my $type = $codeGenerator->StripModule(shift);
303 return if $codeGenerator->GetSequenceType($type);
304 return if $codeGenerator->GetArrayType($type);
305 return if $codeGenerator->IsNonPointerType($type);
306 return if $type =~ /Constructor/;
308 if ($codeGenerator->IsStringType($type)) {
309 $implIncludes{"wtf/text/AtomicString.h"} = 1;
310 $implIncludes{"KURL.h"} = 1;
311 $implIncludes{"WebDOMString.h"} = 1;
315 if ($type eq "DOMObject") {
316 $implIncludes{"WebDOMObject.h"} = 1;
320 if ($type eq "EventListener") {
321 $implIncludes{"WebNativeEventListener.h"} = 1;
325 if ($type eq "SerializedScriptValue") {
326 $implIncludes{"SerializedScriptValue.h"} = 1;
330 if ($type eq "VoidCallback") {
331 $implIncludes{"WebDOMCustomVoidCallback.h"} = 1;
335 # Also include CSSImportRule so that the toWebKit methods for subclasses are found
336 if ($type eq "CSSRule") {
337 $implIncludes{"WebDOMCSSImportRule.h"} = 1;
340 $implIncludes{"Node.h"} = 1 if $type eq "NodeList";
341 $implIncludes{"StylePropertySet.h"} = 1 if $type eq "CSSStyleDeclaration";
343 # Default, include the same named file (the implementation) and the same name prefixed with "WebDOM".
344 $implIncludes{"$type.h"} = 1 unless $type eq "DOMObject";
345 $implIncludes{"WebDOM$type.h"} = 1;
348 sub GetNamespaceForClass
351 return "WTF" if (($type eq "ArrayBuffer") or ($type eq "ArrayBufferView"));
352 return "WTF" if (($type eq "Uint8Array") or ($type eq "Uint8ClampedArray") or ($type eq "Uint16Array") or ($type eq "Uint32Array"));
353 return "WTF" if (($type eq "Int8Array") or ($type eq "Int16Array") or ($type eq "Int32Array"));
354 return "WTF" if (($type eq "Float32Array") or ($type eq "Float64Array"));
361 my $dataNode = shift;
363 my $interfaceName = $dataNode->name;
364 my $className = GetClassName($interfaceName);
365 my $implClassName = GetImplClassName($interfaceName);
367 my $implClassNameWithNamespace = GetNamespaceForClass($implClassName) . "::" . $implClassName;
370 $parentName = GetParent($dataNode);
372 my $numConstants = @{$dataNode->constants};
373 my $numAttributes = @{$dataNode->attributes};
374 my $numFunctions = @{$dataNode->functions};
376 # - Add default header template
377 @headerContentHeader = split("\r", $headerLicenseTemplate);
378 push(@headerContentHeader, "\n#ifndef $className" . "_h");
379 push(@headerContentHeader, "\n#define $className" . "_h\n\n");
381 my $conditionalString = $codeGenerator->GenerateConditionalString($dataNode);
382 push(@headerContentHeader, "#if ${conditionalString}\n\n") if $conditionalString;
386 my %headerIncludes = ();
387 $headerIncludes{"WebDOMString.h"} = 1;
388 $headerIncludes{"$parentName.h"} = 1;
389 foreach my $include (sort keys(%headerIncludes)) {
390 push(@headerContentHeader, "#include <$include>\n");
393 push(@headerContent, "class $className");
394 push(@headerContent, " : public $parentName") if $parentName;
395 push(@headerContent, " {\n");
396 push(@headerContent, "public:\n");
399 push(@headerContent, " $className();\n");
400 push(@headerContent, " explicit $className($implClassNameWithNamespace*);\n");
402 # Copy constructor and assignment operator on classes which have the d-ptr
403 if ($parentName eq "WebDOMObject") {
404 push(@headerContent, " $className(const $className&);\n");
405 push(@headerContent, " ${className}& operator=(const $className&);\n");
409 if ($parentName eq "WebDOMObject") {
410 push(@headerContent, " virtual ~$className();\n");
412 push(@headerContent, " virtual ~$className() { }\n");
415 push(@headerContent, "\n");
416 $headerForwardDeclarations{$implClassNameWithNamespace} = 1;
419 if ($numConstants > 0) {
420 my @headerConstants = ();
421 my @constants = @{$dataNode->constants};
422 my $combinedConstants = "";
424 # FIXME: we need a way to include multiple enums.
425 foreach my $constant (@constants) {
426 my $constantName = $constant->name;
427 my $constantValue = $constant->value;
428 my $conditional = $constant->extendedAttributes->{"Conditional"};
429 my $notLast = $constant ne $constants[-1];
432 my $conditionalString = $codeGenerator->GenerateConditionalStringFromAttributeValue($conditional);
433 $combinedConstants .= "#if ${conditionalString}\n";
435 $combinedConstants .= " WEBDOM_$constantName = $constantValue";
436 $combinedConstants .= "," if $notLast;
438 $combinedConstants .= "\n#endif\n";
440 $combinedConstants .= "\n";
444 push(@headerContent, " ");
445 push(@headerContent, "enum {\n");
446 push(@headerContent, $combinedConstants);
447 push(@headerContent, "\n ");
448 push(@headerContent, "};\n\n");
451 my @headerAttributes = ();
453 # - Add attribute getters/setters.
454 if ($numAttributes > 0) {
455 foreach my $attribute (@{$dataNode->attributes}) {
456 next if SkipAttribute($attribute);
458 my $attributeConditionalString = $codeGenerator->GenerateConditionalString($attribute->signature);
459 my $attributeName = $attribute->signature->name;
460 my $attributeType = GetCPPType($attribute->signature->type, 0);
461 my $attributeIsReadonly = ($attribute->type =~ /^readonly/);
464 $property .= "#if ${attributeConditionalString}\n" if $attributeConditionalString;
465 $property .= " " . $attributeType . ($attributeType =~ /\*$/ ? "" : " ") . $attributeName . "() const";
467 my $availabilityMacro = "";
468 my $declarationSuffix = ";\n";
470 AddForwardDeclarationsForType($attribute->signature->type, 1);
472 $attributeType = GetCPPType($attribute->signature->type, 1);
473 my $setterName = "set" . ucfirst($attributeName);
475 $property .= $declarationSuffix;
476 push(@headerAttributes, $property);
477 if (!$attributeIsReadonly and !$attribute->signature->extendedAttributes->{"Replaceable"}) {
478 $property = " void $setterName($attributeType)";
479 $property .= $declarationSuffix;
480 push(@headerAttributes, $property);
483 push(@headerAttributes, "#endif\n") if $attributeConditionalString;
485 push(@headerContent, @headerAttributes) if @headerAttributes > 0;
488 my @headerFunctions = ();
489 my @deprecatedHeaderFunctions = ();
490 my @interfaceFunctions = ();
493 if ($numFunctions > 0) {
494 foreach my $function (@{$dataNode->functions}) {
495 next if SkipFunction($function);
496 my $functionName = $function->signature->extendedAttributes->{"ImplementedAs"} || $function->signature->name;
498 my $returnType = GetCPPType($function->signature->type, 0);
499 my $numberOfParameters = @{$function->parameters};
500 my %typesToForwardDeclare = ($function->signature->type => 1);
502 my $parameterIndex = 0;
503 my $functionSig = "$returnType $functionName(";
504 my $methodName = $functionName;
505 foreach my $param (@{$function->parameters}) {
506 my $paramName = $param->name;
507 my $paramType = GetCPPType($param->type, 1);
508 $typesToForwardDeclare{$param->type} = 1;
510 $functionSig .= ", " if $parameterIndex >= 1;
511 $functionSig .= "$paramType $paramName";
515 if ($dataNode->extendedAttributes->{"CPPPureInterface"}) {
516 push(@interfaceFunctions, " virtual " . $functionSig . " = 0;\n");
518 my $functionDeclaration = $functionSig;
519 $functionDeclaration .= ";\n";
521 foreach my $type (keys %typesToForwardDeclare) {
522 # add any forward declarations to the public header if a deprecated version will be generated
523 AddForwardDeclarationsForType($type, 1);
526 my $conditionalString = $codeGenerator->GenerateConditionalString($function->signature);
527 push(@headerFunctions, "#if ${conditionalString}\n") if $conditionalString;
528 push(@headerFunctions, " ");
529 push(@headerFunctions, $functionDeclaration);
530 push(@headerFunctions, "#endif\n") if $conditionalString;
533 if (@headerFunctions > 0) {
534 push(@headerContent, "\n") if @headerAttributes > 0;
535 push(@headerContent, @headerFunctions);
539 push(@headerContent, "\n");
540 push(@headerContent, " $implClassNameWithNamespace* impl() const;\n");
542 if ($parentName eq "WebDOMObject") {
543 push(@headerContent, "\nprotected:\n");
544 push(@headerContent, " struct ${className}Private;\n");
545 push(@headerContent, " ${className}Private* m_impl;\n");
548 push(@headerContent, "};\n\n");
550 # for CPPPureInterface classes also add the interface that the client code needs to
552 if ($dataNode->extendedAttributes->{"CPPPureInterface"}) {
553 push(@headerContent, "class WebUser$interfaceName {\n");
554 push(@headerContent, "public:\n");
555 push(@headerContent, " virtual void ref() = 0;\n");
556 push(@headerContent, " virtual void deref() = 0;\n\n");
557 push(@headerContent, @interfaceFunctions);
558 push(@headerContent, "\nprotected:\n");
559 push(@headerContent, " virtual ~WebUser$interfaceName() {}\n");
560 push(@headerContent, "};\n\n");
563 my $namespace = GetNamespaceForClass($implClassName);
564 push(@headerContent, "$namespace" . "::$implClassName* toWebCore(const $className&);\n");
565 push(@headerContent, "$className toWebKit($namespace" . "::$implClassName*);\n");
566 if ($dataNode->extendedAttributes->{"CPPPureInterface"}) {
567 push(@headerContent, "$className toWebKit(WebUser$interfaceName*);\n");
569 push(@headerContent, "\n#endif\n");
570 push(@headerContent, "#endif // ${conditionalString}\n\n") if $conditionalString;
573 sub AddEarlyReturnStatement
575 my $returnType = shift;
577 if (!defined($returnType) or $returnType eq "void") {
579 } elsif ($codeGenerator->IsPrimitiveType($returnType)) {
581 } elsif ($returnType eq "bool") {
582 $returnType = " false";
584 $returnType = " $returnType()";
587 # TODO: We could set exceptions here, if we want that
588 my $statement = " if (!impl())\n";
589 $statement .= " return$returnType;\n\n";
593 sub AddReturnStatement
595 my $typeInfo = shift;
596 my $returnValue = shift;
598 # Used to invoke KURLs "const String&" operator
599 if ($codeGenerator->IsStringType($typeInfo->signature->type)) {
600 return " return static_cast<const WTF::String&>($returnValue);\n";
603 return " return $returnValue;\n";
606 sub GenerateImplementation
609 my $dataNode = shift;
611 my @ancestorInterfaceNames = ();
613 if (@{$dataNode->parents} > 1) {
614 $codeGenerator->AddMethodsConstantsAndAttributesFromParentClasses($dataNode, \@ancestorInterfaceNames);
617 my $interfaceName = $dataNode->name;
618 my $className = GetClassName($interfaceName);
619 my $implClassName = GetImplClassName($interfaceName);
620 my $parentImplClassName = GetParentImplClassName($dataNode);
621 my $implClassNameWithNamespace = GetNamespaceForClass($implClassName) . "::" . $implClassName;
622 my $baseClass = "WebDOM$parentImplClassName";
623 my $conditional = $dataNode->extendedAttributes->{"Conditional"};
625 my $numAttributes = @{$dataNode->attributes};
626 my $numFunctions = @{$dataNode->functions};
628 # - Add default header template.
629 @implContentHeader = split("\r", $implementationLicenseTemplate);
632 push(@implContentHeader, "\n#include \"config.h\"\n");
633 my $conditionalString = $codeGenerator->GenerateConditionalString($dataNode);
634 push(@implContentHeader, "\n#if ${conditionalString}\n\n") if $conditionalString;
635 push(@implContentHeader, "#include \"$className.h\"\n\n");
637 $implIncludes{"WebExceptionHandler.h"} = 1;
638 $implIncludes{"$implClassName.h"} = 1;
641 push(@implContent, "#include <wtf/GetPtr.h>\n");
642 push(@implContent, "#include <wtf/RefPtr.h>\n\n");
644 # Private datastructure, encapsulating WebCore types
645 if ($baseClass eq "WebDOMObject") {
646 push(@implContent, "struct ${className}::${className}Private {\n");
647 push(@implContent, " ${className}Private($implClassNameWithNamespace* object = 0)\n");
648 push(@implContent, " : impl(object)\n");
649 push(@implContent, " {\n");
650 push(@implContent, " }\n\n");
651 push(@implContent, " RefPtr<$implClassNameWithNamespace> impl;\n");
652 push(@implContent, "};\n\n");
656 push(@implContent, "${className}::$className()\n");
657 push(@implContent, " : ${baseClass}()\n");
658 push(@implContent, " , m_impl(0)\n") if ($baseClass eq "WebDOMObject");
659 push(@implContent, "{\n");
660 push(@implContent, "}\n\n");
662 push(@implContent, "${className}::$className($implClassNameWithNamespace* impl)\n");
663 if ($baseClass eq "WebDOMObject") {
664 push(@implContent, " : ${baseClass}()\n");
665 push(@implContent, " , m_impl(new ${className}Private(impl))\n");
666 push(@implContent, "{\n");
667 push(@implContent, "}\n\n");
669 push(@implContent, "${className}::${className}(const ${className}& copy)\n");
670 push(@implContent, " : ${baseClass}()\n");
671 push(@implContent, "{\n");
672 push(@implContent, " m_impl = copy.impl() ? new ${className}Private(copy.impl()) : 0;\n");
673 push(@implContent, "}\n\n");
675 push(@implContent, "${className}& ${className}::operator\=(const ${className}& copy)\n");
676 push(@implContent, "{\n");
677 push(@implContent, " delete m_impl;\n");
678 push(@implContent, " m_impl = copy.impl() ? new ${className}Private(copy.impl()) : 0;\n");
679 push(@implContent, " return *this;\n");
680 push(@implContent, "}\n\n");
682 push(@implContent, "$implClassNameWithNamespace* ${className}::impl() const\n");
683 push(@implContent, "{\n");
684 push(@implContent, " return m_impl ? WTF::getPtr(m_impl->impl) : 0;\n");
685 push(@implContent, "}\n\n");
688 push(@implContent, "${className}::~$className()\n");
689 push(@implContent, "{\n");
690 push(@implContent, " delete m_impl;\n");
691 push(@implContent, " m_impl = 0;\n");
692 push(@implContent, "}\n\n");
694 push(@implContent, " : ${baseClass}(impl)\n");
695 push(@implContent, "{\n");
696 push(@implContent, "}\n\n");
698 push(@implContent, "$implClassNameWithNamespace* ${className}::impl() const\n");
699 push(@implContent, "{\n");
700 push(@implContent, " return static_cast<$implClassNameWithNamespace*>(${baseClass}::impl());\n");
701 push(@implContent, "}\n\n");
704 # START implementation
705 %attributeNames = ();
708 if ($numAttributes > 0) {
709 foreach my $attribute (@{$dataNode->attributes}) {
710 next if SkipAttribute($attribute);
711 AddIncludesForType($attribute->signature->type);
713 my $idlType = $codeGenerator->StripModule($attribute->signature->type);
715 my $attributeName = $attribute->signature->name;
716 my $attributeType = GetCPPType($attribute->signature->type, 0);
717 my $attributeIsReadonly = ($attribute->type =~ /^readonly/);
719 $attributeNames{$attributeName} = 1;
722 my $getterSig = "$attributeType $className\:\:$attributeName() const\n";
723 my $hasGetterException = @{$attribute->getterExceptions};
724 my ($functionName, @arguments) = $codeGenerator->GetterExpression(\%implIncludes, $interfaceName, $attribute);
725 push(@arguments, "ec") if $hasGetterException;
726 if ($attribute->signature->extendedAttributes->{"ImplementedBy"}) {
727 my $implementedBy = $attribute->signature->extendedAttributes->{"ImplementedBy"};
728 $implIncludes{"${implementedBy}.h"} = 1;
729 unshift(@arguments, "impl()");
730 $functionName = "${implementedBy}::${functionName}";
732 $functionName = "impl()->${functionName}";
736 my $getterContentHead = "";
737 my $getterContentTail = "";
738 my @customGetterContent = ();
739 if ($attribute->signature->extendedAttributes->{"ConvertToString"}) {
740 $getterContentHead = "WTF::String::number(";
741 $getterContentTail = ")";
742 } elsif ($attribute->signature->type eq "SerializedScriptValue") {
743 $getterContentTail = "->toString()";
744 } elsif (ConversionNeeded($attribute->signature->type)) {
745 $getterContentHead = "toWebKit(WTF::getPtr(";
746 $getterContentTail = "))";
749 my $getterContent = "${getterContentHead}${functionName}(" . join(", ", @arguments) . ")${getterContentTail}";
750 my $attributeConditionalString = $codeGenerator->GenerateConditionalString($attribute->signature);
751 push(@implContent, "#if ${attributeConditionalString}\n") if $attributeConditionalString;
753 push(@implContent, $getterSig);
754 push(@implContent, "{\n");
755 push(@implContent, AddEarlyReturnStatement($attributeType));
756 push(@implContent, @customGetterContent);
757 if ($hasGetterException) {
758 # Differentiated between when the return type is a pointer and
759 # not for white space issue (ie. Foo *result vs. int result).
760 if ($attributeType =~ /\*$/) {
761 $getterContent = $attributeType . "result = " . $getterContent;
763 $getterContent = $attributeType . " result = " . $getterContent;
766 push(@implContent, " $exceptionInit\n");
767 push(@implContent, " $getterContent;\n");
768 push(@implContent, " $exceptionRaiseOnError\n");
769 push(@implContent, AddReturnStatement($attribute, "result"));
771 push(@implContent, AddReturnStatement($attribute, $getterContent));
773 push(@implContent, "}\n\n");
776 if (!$attributeIsReadonly and !$attribute->signature->extendedAttributes->{"Replaceable"}) {
778 my $hasSetterException = @{$attribute->setterExceptions};
780 my $coreSetterName = "set" . $codeGenerator->WK_ucfirst($attributeName);
781 my $setterName = "set" . ucfirst($attributeName);
782 my $argName = "new" . ucfirst($attributeName);
783 my $arg = GetCPPTypeGetter($argName, $idlType);
785 my $attributeType = GetCPPType($attribute->signature->type, 1);
786 push(@implContent, "void $className\:\:$setterName($attributeType $argName)\n");
787 push(@implContent, "{\n");
788 push(@implContent, AddEarlyReturnStatement());
790 push(@implContent, " $exceptionInit\n") if $hasSetterException;
792 my ($functionName, @arguments) = $codeGenerator->SetterExpression(\%implIncludes, $interfaceName, $attribute);
793 push(@arguments, $arg);
794 push(@arguments, "ec") if $hasSetterException;
795 if ($attribute->signature->extendedAttributes->{"ImplementedBy"}) {
796 my $implementedBy = $attribute->signature->extendedAttributes->{"ImplementedBy"};
797 $implIncludes{"${implementedBy}.h"} = 1;
798 unshift(@arguments, "impl()");
799 $functionName = "${implementedBy}::${functionName}";
801 $functionName = "impl()->${functionName}";
803 push(@implContent, " ${functionName}(" . join(", ", @arguments) . ");\n");
804 push(@implContent, " $exceptionRaiseOnError\n") if $hasSetterException;
805 push(@implContent, "}\n\n");
808 push(@implContent, "#endif\n") if $attributeConditionalString;
813 if ($numFunctions > 0) {
814 foreach my $function (@{$dataNode->functions}) {
815 # Treat CPPPureInterface as Custom as well, since the WebCore versions will take a script context as well
816 next if SkipFunction($function) || $dataNode->extendedAttributes->{"CPPPureInterface"};
817 AddIncludesForType($function->signature->type);
819 my $functionName = $function->signature->name;
820 my $returnType = GetCPPType($function->signature->type, 0);
821 my $hasParameters = @{$function->parameters};
822 my $raisesExceptions = @{$function->raisesExceptions};
824 my @parameterNames = ();
825 my @needsAssert = ();
826 my %needsCustom = ();
828 my $parameterIndex = 0;
830 my $functionSig = "$returnType $className\:\:$functionName(";
831 foreach my $param (@{$function->parameters}) {
832 my $paramName = $param->name;
833 my $paramType = GetCPPType($param->type, 1);
835 # make a new parameter name if the original conflicts with a property name
836 $paramName = "in" . ucfirst($paramName) if $attributeNames{$paramName};
838 AddIncludesForType($param->type);
840 my $idlType = $codeGenerator->StripModule($param->type);
841 my $implGetter = GetCPPTypeGetter($paramName, $idlType);
843 push(@parameterNames, $implGetter);
844 $needsCustom{"NodeToReturn"} = $paramName if $param->extendedAttributes->{"CustomReturn"};
846 unless ($codeGenerator->IsPrimitiveType($idlType) or $codeGenerator->IsStringType($idlType)) {
847 push(@needsAssert, " ASSERT($paramName);\n");
850 $functionSig .= ", " if $parameterIndex >= 1;
851 $functionSig .= "$paramType $paramName";
857 my @functionContent = ();
858 push(@parameterNames, "ec") if $raisesExceptions;
861 if ($function->signature->extendedAttributes->{"ImplementedBy"}) {
862 my $implementedBy = $function->signature->extendedAttributes->{"ImplementedBy"};
863 $implIncludes{"${implementedBy}.h"} = 1;
864 unshift(@parameterNames, "impl()");
865 $content = "WebCore::${implementedBy}::" . $codeGenerator->WK_lcfirst($functionName) . "(" . join(", ", @parameterNames) . ")";
867 $content = "impl()->" . $codeGenerator->WK_lcfirst($functionName) . "(" . join(", ", @parameterNames) . ")";
870 if ($returnType eq "void") {
871 # Special case 'void' return type.
872 if ($raisesExceptions) {
873 push(@functionContent, " $exceptionInit\n");
874 push(@functionContent, " $content;\n");
875 push(@functionContent, " $exceptionRaiseOnError\n");
877 push(@functionContent, " $content;\n");
879 } elsif (defined $needsCustom{"NodeToReturn"}) {
880 # TODO: This is important to enable, once we care about custom code!
882 # Special case the insertBefore, replaceChild, removeChild
883 # and appendChild functions from DOMNode
884 my $toReturn = $needsCustom{"NodeToReturn"};
885 if ($raisesExceptions) {
886 push(@functionContent, " $exceptionInit\n");
887 push(@functionContent, " if ($content)\n");
888 push(@functionContent, " return $toReturn;\n");
889 push(@functionContent, " $exceptionRaiseOnError\n");
890 push(@functionContent, " return $className();\n");
892 push(@functionContent, " if ($content)\n");
893 push(@functionContent, " return $toReturn;\n");
894 push(@functionContent, " return NULL;\n");
897 if (ConversionNeeded($function->signature->type)) {
898 $content = "toWebKit(WTF::getPtr($content))";
901 if ($raisesExceptions) {
902 # Differentiated between when the return type is a pointer and
903 # not for white space issue (ie. Foo *result vs. int result).
904 if ($returnType =~ /\*$/) {
905 $content = $returnType . "result = " . $content;
907 $content = $returnType . " result = " . $content;
910 push(@functionContent, " $exceptionInit\n");
911 push(@functionContent, " $content;\n");
912 push(@functionContent, " $exceptionRaiseOnError\n");
913 push(@functionContent, " return result;\n");
915 push(@functionContent, " return $content;\n");
919 my $conditionalString = $codeGenerator->GenerateConditionalString($function->signature);
920 push(@implContent, "\n#if ${conditionalString}\n") if $conditionalString;
922 push(@implContent, "$functionSig\n");
923 push(@implContent, "{\n");
924 push(@implContent, AddEarlyReturnStatement($returnType));
925 push(@implContent, @functionContent);
926 push(@implContent, "}\n\n");
928 push(@implContent, "#endif\n\n") if $conditionalString;
937 # Generate internal interfaces
938 my $namespace = GetNamespaceForClass($implClassName);
939 push(@implContent, "$namespace" . "::$implClassName* toWebCore(const $className& wrapper)\n");
940 push(@implContent, "{\n");
941 push(@implContent, " return wrapper.impl();\n");
942 push(@implContent, "}\n\n");
944 push(@implContent, "$className toWebKit($namespace" . "::$implClassName* value)\n");
945 push(@implContent, "{\n");
946 push(@implContent, " return $className(value);\n");
947 push(@implContent, "}\n");
949 # - End the ifdef conditional if necessary
950 push(@implContent, "\n#endif // ${conditionalString}\n") if $conditionalString;
959 # Open files for writing...
960 my $headerFileName = "$outputDir/" . $name . ".h";
961 my $implFileName = "$outputDir/" . $name . ".cpp";
963 # Update a .h file if the contents are changed.
964 my $contents = join "", @headerContentHeader;
966 foreach my $class (sort keys(%headerForwardDeclarations)) {
967 if ($class =~ /::/) {
968 my $namespacePart = $class;
969 $namespacePart =~ s/::.*//;
971 my $classPart = $class;
972 $classPart =~ s/${namespacePart}:://;
974 $contents .= "namespace $namespacePart {\nclass $classPart;\n};\n\n";
976 $contents .= "class $class;\n"
980 my $hasForwardDeclarations = keys(%headerForwardDeclarations);
981 $contents .= "\n" if $hasForwardDeclarations;
982 $contents .= join "", @headerContent;
983 $codeGenerator->UpdateFile($headerFileName, $contents);
985 @headerContentHeader = ();
987 %headerForwardDeclarations = ();
989 # Update a .cpp file if the contents are changed.
990 $contents = join "", @implContentHeader;
992 foreach my $include (sort keys(%implIncludes)) {
993 # "className.h" is already included right after config.h, silence check-webkit-style
994 next if $include eq "$name.h";
995 $contents .= "#include \"$include\"\n";
998 $contents .= join "", @implContent;
999 $codeGenerator->UpdateFile($implFileName, $contents);
1001 @implContentHeader = ();