Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / Source / WebCore / bindings / scripts / CodeGeneratorCPP.pm
1
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.
9 #
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.
14
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.
19
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.
24 #
25
26 package CodeGeneratorCPP;
27
28 use constant FileNamePrefix => "WebDOM";
29
30 # Global Variables
31 my $module = "";
32 my $outputDir = "";
33
34 my @headerContentHeader = ();
35 my @headerContent = ();
36 my %headerForwardDeclarations = ();
37
38 my @implContentHeader = ();
39 my @implContent = ();
40 my %implIncludes = ();
41
42 # Constants
43 my $exceptionInit = "WebCore::ExceptionCode ec = 0;";
44 my $exceptionRaiseOnError = "webDOMRaiseError(static_cast<WebDOMExceptionCode>(ec));";
45
46 # Default License Templates
47 my $headerLicenseTemplate = << "EOF";
48 /*
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.
53  *
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.
58  *
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.
63  *
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.
68  */
69 EOF
70
71 my $implementationLicenseTemplate = << "EOF";
72 /*
73  * This file is part of the WebKit open source project.
74  * This file has been generated by generate-bindings.pl. DO NOT MODIFY!
75  *
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.
80  *
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.
85  *
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.
90  */
91 EOF
92
93 # Default constructor
94 sub new
95 {
96     my $object = shift;
97     my $reference = { };
98
99     $codeGenerator = shift;
100     $outputDir = shift;
101     shift; # $outputHeadersDir
102     shift; # $useLayerOnTop
103     shift; # $preprocessor
104     shift; # $writeDependencies
105
106     bless($reference, $object);
107     return $reference;
108 }
109
110 # Params: 'domClass' struct
111 sub GenerateInterface
112 {
113     my $object = shift;
114     my $dataNode = shift;
115     my $defines = shift;
116
117     my $name = $dataNode->name;
118     my $className = GetClassName($name);
119     my $parentClassName = "WebDOM" . GetParentImplClassName($dataNode);
120
121     # Start actual generation.
122     $object->GenerateHeader($dataNode);
123     $object->GenerateImplementation($dataNode);
124
125     # Write changes.
126     $object->WriteData(FileNamePrefix . $name);
127 }
128
129 # Params: 'idlDocument' struct
130 sub GenerateModule
131 {
132     my $object = shift;
133     my $dataNode = shift;
134
135     $module = $dataNode->module;
136 }
137
138 sub GetClassName
139 {
140     my $name = $codeGenerator->StripModule(shift);
141
142     # special cases
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";
148
149     return "WebDOM$name";
150 }
151
152 sub GetImplClassName
153 {
154     return $codeGenerator->StripModule(shift);
155 }
156
157 sub GetParentImplClassName
158 {
159     my $dataNode = shift;
160
161     if (@{$dataNode->parents} eq 0) {
162         return "EventTarget" if $dataNode->extendedAttributes->{"EventTarget"};
163         return "Object";
164     }
165
166     return $codeGenerator->StripModule($dataNode->parents(0));
167 }
168
169 sub GetParent
170 {
171     my $dataNode = shift;
172     my $numParents = @{$dataNode->parents};
173
174     my $parent = "";
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;
181     } else {
182         my @parents = @{$dataNode->parents};
183         my $firstParent = $codeGenerator->StripModule(shift(@parents));
184         $parent = "WebDOM" . $firstParent;
185     }
186
187     return $parent;
188 }
189
190 sub SkipFunction
191 {
192     my $function = shift;
193
194     return 1 if $function->signature->extendedAttributes->{"Custom"};
195
196     # FIXME: We don't generate bindings for SVG related interfaces yet
197     return 1 if $function->signature->name =~ /getSVGDocument/;
198
199     if ($codeGenerator->GetArrayType($function->signature->type)) {
200         return 1;
201     }
202
203     if ($codeGenerator->GetSequenceType($function->signature->type)) {
204         return 1;
205     }
206
207     foreach my $param (@{$function->parameters}) {
208         return 1 if $codeGenerator->GetSequenceType($param->type);
209         return 1 if $param->extendedAttributes->{"Clamp"};
210     }
211
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"};
216 }
217
218 sub SkipAttribute
219 {
220     my $attribute = shift;
221
222     return 1 if $attribute->signature->extendedAttributes->{"Custom"}
223                 or $attribute->signature->extendedAttributes->{"CustomGetter"};
224
225     return 1 if $attribute->signature->type =~ /Constructor$/;
226
227     return 1 if $codeGenerator->IsTypedArrayType($attribute->signature->type);
228
229     if ($codeGenerator->GetArrayType($attribute->signature->type)) {
230         return 1;
231     }
232
233     if ($codeGenerator->GetSequenceType($attribute->signature->type)) {
234         return 1;
235     }
236
237     $codeGenerator->AssertNotSequenceType($attribute->signature->type);
238
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"};
243
244     return 0;
245 }
246
247 sub GetCPPType
248 {
249     my $type = shift;
250     my $useConstReference = shift;
251     my $name = GetClassName($type);
252
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";
257
258     if ($codeGenerator->IsStringType($type)) {
259         if ($useConstReference) {
260             return "const $name&";
261         }
262
263         return $name;
264     }
265
266     return $name if $codeGenerator->IsPrimitiveType($type) or $type eq "DOMTimeStamp";
267     return "const $name&" if $useConstReference;
268     return $name;
269 }
270
271 sub ConversionNeeded
272 {
273     my $type = $codeGenerator->StripModule(shift);
274     return !$codeGenerator->IsNonPointerType($type) && !$codeGenerator->IsStringType($type);
275 }
276
277 sub GetCPPTypeGetter
278 {
279     my $argName = shift;
280     my $type = $codeGenerator->StripModule(shift);
281
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)";
286 }
287
288 sub AddForwardDeclarationsForType
289 {
290     my $type = $codeGenerator->StripModule(shift);
291     my $public = shift;
292
293     return if $codeGenerator->IsNonPointerType($type) or $codeGenerator->IsStringType($type);
294
295     my $class = GetClassName($type);
296     $headerForwardDeclarations{$class} = 1 if $public;
297 }
298
299 sub AddIncludesForType
300 {
301     my $type = $codeGenerator->StripModule(shift);
302
303     return if $codeGenerator->GetSequenceType($type);
304     return if $codeGenerator->GetArrayType($type);
305     return if $codeGenerator->IsNonPointerType($type);
306     return if $type =~ /Constructor/;
307
308     if ($codeGenerator->IsStringType($type)) {
309         $implIncludes{"wtf/text/AtomicString.h"} = 1;
310         $implIncludes{"KURL.h"} = 1;
311         $implIncludes{"WebDOMString.h"} = 1;
312         return;
313     }
314
315     if ($type eq "DOMObject") {
316         $implIncludes{"WebDOMObject.h"} = 1;
317         return;
318     }
319
320     if ($type eq "EventListener") {
321         $implIncludes{"WebNativeEventListener.h"} = 1;
322         return;
323     }
324
325     if ($type eq "SerializedScriptValue") {
326         $implIncludes{"SerializedScriptValue.h"} = 1;
327         return;
328     }
329     
330     if ($type eq "VoidCallback") {
331         $implIncludes{"WebDOMCustomVoidCallback.h"} = 1;
332         return;
333     }
334     
335     # Also include CSSImportRule so that the toWebKit methods for subclasses are found
336     if ($type eq "CSSRule") {
337         $implIncludes{"WebDOMCSSImportRule.h"} = 1;
338     }
339
340     $implIncludes{"Node.h"} = 1 if $type eq "NodeList";
341     $implIncludes{"StylePropertySet.h"} = 1 if $type eq "CSSStyleDeclaration";
342
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;
346 }
347
348 sub GetNamespaceForClass
349 {
350     my $type = shift;
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"));    
355     return "WebCore";
356 }
357
358 sub GenerateHeader
359 {
360     my $object = shift;
361     my $dataNode = shift;
362
363     my $interfaceName = $dataNode->name;
364     my $className = GetClassName($interfaceName);
365     my $implClassName = GetImplClassName($interfaceName);
366     
367     my $implClassNameWithNamespace = GetNamespaceForClass($implClassName) . "::" . $implClassName;
368
369     my $parentName = "";
370     $parentName = GetParent($dataNode);
371
372     my $numConstants = @{$dataNode->constants};
373     my $numAttributes = @{$dataNode->attributes};
374     my $numFunctions = @{$dataNode->functions};
375
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");
380
381     my $conditionalString = $codeGenerator->GenerateConditionalString($dataNode);
382     push(@headerContentHeader, "#if ${conditionalString}\n\n") if $conditionalString;
383
384     # - INCLUDES -
385
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");
391     }
392
393     push(@headerContent, "class $className");
394     push(@headerContent, " : public $parentName") if $parentName;
395     push(@headerContent, " {\n");
396     push(@headerContent, "public:\n");
397
398     # Constructor
399     push(@headerContent, "    $className();\n");
400     push(@headerContent, "    explicit $className($implClassNameWithNamespace*);\n");
401
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");
406     }
407
408     # Destructor
409     if ($parentName eq "WebDOMObject") {
410         push(@headerContent, "    virtual ~$className();\n");
411     } else {
412         push(@headerContent, "    virtual ~$className() { }\n");
413     }
414
415     push(@headerContent, "\n");
416     $headerForwardDeclarations{$implClassNameWithNamespace} = 1;
417
418     # - Add constants.
419     if ($numConstants > 0) {
420         my @headerConstants = ();
421         my @constants = @{$dataNode->constants};
422         my $combinedConstants = "";
423
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];
430
431             if ($conditional) {
432                 my $conditionalString = $codeGenerator->GenerateConditionalStringFromAttributeValue($conditional);
433                 $combinedConstants .= "#if ${conditionalString}\n";
434             }
435             $combinedConstants .= "        WEBDOM_$constantName = $constantValue";
436             $combinedConstants .= "," if $notLast;
437             if ($conditional) {
438                 $combinedConstants .= "\n#endif\n";
439             } elsif ($notLast) {
440                 $combinedConstants .= "\n";
441             }
442         }
443
444         push(@headerContent, "    ");
445         push(@headerContent, "enum {\n");
446         push(@headerContent, $combinedConstants);
447         push(@headerContent, "\n    ");
448         push(@headerContent, "};\n\n");
449     }
450
451     my @headerAttributes = ();
452
453     # - Add attribute getters/setters.
454     if ($numAttributes > 0) {
455         foreach my $attribute (@{$dataNode->attributes}) {
456             next if SkipAttribute($attribute);
457
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/);
462             my $property = "";
463             
464             $property .= "#if ${attributeConditionalString}\n" if $attributeConditionalString;
465             $property .= "    " . $attributeType . ($attributeType =~ /\*$/ ? "" : " ") . $attributeName . "() const";
466
467             my $availabilityMacro = "";
468             my $declarationSuffix = ";\n";
469
470             AddForwardDeclarationsForType($attribute->signature->type, 1);
471
472             $attributeType = GetCPPType($attribute->signature->type, 1);
473             my $setterName = "set" . ucfirst($attributeName);
474
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); 
481             }
482
483             push(@headerAttributes, "#endif\n") if $attributeConditionalString;
484         }
485         push(@headerContent, @headerAttributes) if @headerAttributes > 0;
486     }
487
488     my @headerFunctions = ();
489     my @deprecatedHeaderFunctions = ();
490     my @interfaceFunctions = ();
491
492     # - Add functions.
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;
497
498             my $returnType = GetCPPType($function->signature->type, 0);
499             my $numberOfParameters = @{$function->parameters};
500             my %typesToForwardDeclare = ($function->signature->type => 1);
501
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;
509
510                 $functionSig .= ", " if $parameterIndex >= 1;
511                 $functionSig .= "$paramType $paramName";
512                 $parameterIndex++;
513             }
514             $functionSig .= ")";
515             if ($dataNode->extendedAttributes->{"CPPPureInterface"}) {
516                 push(@interfaceFunctions, "    virtual " . $functionSig . " = 0;\n");
517             }
518             my $functionDeclaration = $functionSig;
519             $functionDeclaration .= ";\n";
520
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);
524             }
525
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;
531         }
532
533         if (@headerFunctions > 0) {
534             push(@headerContent, "\n") if @headerAttributes > 0;
535             push(@headerContent, @headerFunctions);
536         }
537     }
538
539     push(@headerContent, "\n");
540     push(@headerContent, "    $implClassNameWithNamespace* impl() const;\n");
541
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");
546     }
547
548     push(@headerContent, "};\n\n");
549
550     # for CPPPureInterface classes also add the interface that the client code needs to
551     # implement
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");
561     }
562
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");
568     }
569     push(@headerContent, "\n#endif\n");
570     push(@headerContent, "#endif // ${conditionalString}\n\n") if $conditionalString;
571 }
572
573 sub AddEarlyReturnStatement
574 {
575     my $returnType = shift;
576
577     if (!defined($returnType) or $returnType eq "void") {
578         $returnType = "";
579     } elsif ($codeGenerator->IsPrimitiveType($returnType)) {
580         $returnType = " 0";
581     } elsif ($returnType eq "bool") {
582         $returnType = " false";
583     } else {
584         $returnType = " $returnType()";
585     }
586
587     # TODO: We could set exceptions here, if we want that
588     my $statement = "    if (!impl())\n";
589     $statement .=   "        return$returnType;\n\n";
590     return $statement;
591 }
592
593 sub AddReturnStatement
594 {
595     my $typeInfo = shift;
596     my $returnValue = shift;
597
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";
601     }
602
603     return "    return $returnValue;\n";
604 }
605
606 sub GenerateImplementation
607 {
608     my $object = shift;
609     my $dataNode = shift;
610
611     my @ancestorInterfaceNames = ();
612
613     if (@{$dataNode->parents} > 1) {
614         $codeGenerator->AddMethodsConstantsAndAttributesFromParentClasses($dataNode, \@ancestorInterfaceNames);
615     }
616
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"};
624
625     my $numAttributes = @{$dataNode->attributes};
626     my $numFunctions = @{$dataNode->functions};
627
628     # - Add default header template.
629     @implContentHeader = split("\r", $implementationLicenseTemplate);
630
631     # - INCLUDES -
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");
636
637     $implIncludes{"WebExceptionHandler.h"} = 1;
638     $implIncludes{"$implClassName.h"} = 1;
639     @implContent = ();
640
641     push(@implContent, "#include <wtf/GetPtr.h>\n");
642     push(@implContent, "#include <wtf/RefPtr.h>\n\n");
643
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");
653     }
654
655     # Constructor
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");
661
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");
668
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");
674
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");
681
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");
686
687         # Destructor
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");
693     } else {
694         push(@implContent, "    : ${baseClass}(impl)\n");
695         push(@implContent, "{\n");
696         push(@implContent, "}\n\n");
697
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");
702     }
703
704     # START implementation
705     %attributeNames = ();
706
707     # - Attributes
708     if ($numAttributes > 0) {
709         foreach my $attribute (@{$dataNode->attributes}) {
710             next if SkipAttribute($attribute);
711             AddIncludesForType($attribute->signature->type);
712
713             my $idlType = $codeGenerator->StripModule($attribute->signature->type);
714
715             my $attributeName = $attribute->signature->name;
716             my $attributeType = GetCPPType($attribute->signature->type, 0);
717             my $attributeIsReadonly = ($attribute->type =~ /^readonly/);
718
719             $attributeNames{$attributeName} = 1;
720
721             # - GETTER
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}";
731             } else {
732                 $functionName = "impl()->${functionName}";
733             }
734
735             # Special cases
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 = "))";
747             }
748
749             my $getterContent = "${getterContentHead}${functionName}(" . join(", ", @arguments) . ")${getterContentTail}";
750             my $attributeConditionalString = $codeGenerator->GenerateConditionalString($attribute->signature);
751             push(@implContent, "#if ${attributeConditionalString}\n") if $attributeConditionalString;
752
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;
762                 } else {
763                     $getterContent = $attributeType . " result = " . $getterContent;
764                 }
765
766                 push(@implContent, "    $exceptionInit\n");
767                 push(@implContent, "    $getterContent;\n");
768                 push(@implContent, "    $exceptionRaiseOnError\n");
769                 push(@implContent, AddReturnStatement($attribute, "result"));
770             } else {
771                 push(@implContent, AddReturnStatement($attribute, $getterContent));
772             }
773             push(@implContent, "}\n\n");
774
775             # - SETTER
776             if (!$attributeIsReadonly and !$attribute->signature->extendedAttributes->{"Replaceable"}) {
777                 # Exception handling
778                 my $hasSetterException = @{$attribute->setterExceptions};
779
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);
784
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());
789
790                 push(@implContent, "    $exceptionInit\n") if $hasSetterException;
791
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}";
800                 } else {
801                     $functionName = "impl()->${functionName}";
802                 }
803                 push(@implContent, "    ${functionName}(" . join(", ", @arguments) . ");\n");
804                 push(@implContent, "    $exceptionRaiseOnError\n") if $hasSetterException;
805                 push(@implContent, "}\n\n");
806             }
807
808             push(@implContent, "#endif\n") if $attributeConditionalString;
809         }
810     }
811
812     # - Functions
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);
818
819             my $functionName = $function->signature->name;
820             my $returnType = GetCPPType($function->signature->type, 0);
821             my $hasParameters = @{$function->parameters};
822             my $raisesExceptions = @{$function->raisesExceptions};
823
824             my @parameterNames = ();
825             my @needsAssert = ();
826             my %needsCustom = ();
827
828             my $parameterIndex = 0;
829
830             my $functionSig = "$returnType $className\:\:$functionName(";
831             foreach my $param (@{$function->parameters}) {
832                 my $paramName = $param->name;
833                 my $paramType = GetCPPType($param->type, 1);
834
835                 # make a new parameter name if the original conflicts with a property name
836                 $paramName = "in" . ucfirst($paramName) if $attributeNames{$paramName};
837
838                 AddIncludesForType($param->type);
839
840                 my $idlType = $codeGenerator->StripModule($param->type);
841                 my $implGetter = GetCPPTypeGetter($paramName, $idlType);
842
843                 push(@parameterNames, $implGetter);
844                 $needsCustom{"NodeToReturn"} = $paramName if $param->extendedAttributes->{"CustomReturn"};
845
846                 unless ($codeGenerator->IsPrimitiveType($idlType) or $codeGenerator->IsStringType($idlType)) {
847                     push(@needsAssert, "    ASSERT($paramName);\n");
848                 }
849
850                 $functionSig .= ", " if $parameterIndex >= 1;
851                 $functionSig .= "$paramType $paramName";
852                 $parameterIndex++;
853             }
854
855             $functionSig .= ")";
856
857             my @functionContent = ();
858             push(@parameterNames, "ec") if $raisesExceptions;
859
860             my $content;
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) . ")";
866             } else {
867                 $content = "impl()->" . $codeGenerator->WK_lcfirst($functionName) . "(" . join(", ", @parameterNames) . ")";
868             }
869
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");
876                 } else {
877                     push(@functionContent, "    $content;\n");
878                 }
879             } elsif (defined $needsCustom{"NodeToReturn"}) {
880                 # TODO: This is important to enable, once we care about custom code!
881
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");
891                 } else {
892                     push(@functionContent, "    if ($content)\n");
893                     push(@functionContent, "        return $toReturn;\n");
894                     push(@functionContent, "    return NULL;\n");
895                 }
896             } else {
897                 if (ConversionNeeded($function->signature->type)) {
898                     $content = "toWebKit(WTF::getPtr($content))";
899                 }
900
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;
906                     } else {
907                         $content = $returnType . " result = " . $content;
908                     }
909
910                     push(@functionContent, "    $exceptionInit\n");
911                     push(@functionContent, "    $content;\n");
912                     push(@functionContent, "    $exceptionRaiseOnError\n");
913                     push(@functionContent, "    return result;\n");
914                 } else {
915                     push(@functionContent, "    return $content;\n");
916                 }
917             }
918
919             my $conditionalString = $codeGenerator->GenerateConditionalString($function->signature);
920             push(@implContent, "\n#if ${conditionalString}\n") if $conditionalString;
921
922             push(@implContent, "$functionSig\n");
923             push(@implContent, "{\n");
924             push(@implContent, AddEarlyReturnStatement($returnType));
925             push(@implContent, @functionContent);
926             push(@implContent, "}\n\n");
927
928             push(@implContent, "#endif\n\n") if $conditionalString;
929
930             # Clear the hash
931             %needsCustom = ();
932         }
933     }
934
935     # END implementation
936
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");
943
944     push(@implContent, "$className toWebKit($namespace" . "::$implClassName* value)\n");
945     push(@implContent, "{\n");
946     push(@implContent, "    return $className(value);\n");
947     push(@implContent, "}\n");
948
949     # - End the ifdef conditional if necessary
950     push(@implContent, "\n#endif // ${conditionalString}\n") if $conditionalString;
951 }
952
953 # Internal helper
954 sub WriteData
955 {
956     my $object = shift;
957     my $name = shift;
958
959     # Open files for writing...
960     my $headerFileName = "$outputDir/" . $name . ".h";
961     my $implFileName = "$outputDir/" . $name . ".cpp";
962
963     # Update a .h file if the contents are changed.
964     my $contents = join "", @headerContentHeader;
965     $contents .= "\n";
966     foreach my $class (sort keys(%headerForwardDeclarations)) {
967         if ($class =~ /::/) {
968             my $namespacePart = $class;
969             $namespacePart =~ s/::.*//;
970
971             my $classPart = $class;
972             $classPart =~ s/${namespacePart}:://;
973
974             $contents .= "namespace $namespacePart {\nclass $classPart;\n};\n\n";
975         } else {
976             $contents .= "class $class;\n"
977         }
978     }
979
980     my $hasForwardDeclarations = keys(%headerForwardDeclarations);
981     $contents .= "\n" if $hasForwardDeclarations;
982     $contents .= join "", @headerContent;
983     $codeGenerator->UpdateFile($headerFileName, $contents);
984
985     @headerContentHeader = ();
986     @headerContent = ();
987     %headerForwardDeclarations = ();
988
989     # Update a .cpp file if the contents are changed.
990     $contents = join "", @implContentHeader;
991
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";
996     }
997
998     $contents .= join "", @implContent;
999     $codeGenerator->UpdateFile($implFileName, $contents);
1000
1001     @implContentHeader = ();
1002     @implContent = ();
1003     %implIncludes = ();
1004 }
1005
1006 1;