OSDN Git Service

8351c8766bc8b4bf7ea3b0976cadc6c4fc6281bb
[android-x86/external-webkit.git] / WebCore / bindings / scripts / CodeGeneratorObjC.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, 2010 Apple Inc. All rights reserved.
7 # Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au>
8 # Copyright (C) 2010 Google Inc.
9 # Copyright (C) Research In Motion Limited 2010. All rights reserved.
10 #
11 # This library is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU Library General Public
13 # License as published by the Free Software Foundation; either
14 # version 2 of the License, or (at your option) any later version.
15
16 # This library is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 # Library General Public License for more details.
20
21 # You should have received a copy of the GNU Library General Public License
22 # along with this library; see the file COPYING.LIB.  If not, write to
23 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 # Boston, MA 02110-1301, USA.
25 #
26
27 package CodeGeneratorObjC;
28
29 use File::stat;
30
31 # Global Variables
32 my $module = "";
33 my $outputDir = "";
34 my $writeDependencies = 0;
35 my %publicInterfaces = ();
36 my $newPublicClass = 0;
37 my $interfaceAvailabilityVersion = "";
38 my $isProtocol = 0;
39 my $noImpl = 0;
40
41 my @headerContentHeader = ();
42 my @headerContent = ();
43 my %headerForwardDeclarations = ();
44 my %headerForwardDeclarationsForProtocols = ();
45
46 my @privateHeaderContentHeader = ();
47 my @privateHeaderContent = ();
48 my %privateHeaderForwardDeclarations = ();
49 my %privateHeaderForwardDeclarationsForProtocols = ();
50
51 my @internalHeaderContent = ();
52
53 my @implContentHeader = ();
54 my @implContent = ();
55 my %implIncludes = ();
56 my @depsContent = ();
57
58 # Hashes
59 my %protocolTypeHash = ("XPathNSResolver" => 1, "EventListener" => 1, "EventTarget" => 1, "NodeFilter" => 1,
60                         "SVGLocatable" => 1, "SVGTransformable" => 1, "SVGStylable" => 1, "SVGFilterPrimitiveStandardAttributes" => 1, 
61                         "SVGTests" => 1, "SVGLangSpace" => 1, "SVGExternalResourcesRequired" => 1, "SVGURIReference" => 1,
62                         "SVGZoomAndPan" => 1, "SVGFitToViewBox" => 1, "SVGAnimatedPathData" => 1, "SVGAnimatedPoints" => 1,
63                         "ElementTimeControl" => 1);
64 my %nativeObjCTypeHash = ("URL" => 1, "Color" => 1);
65
66 # FIXME: this should be replaced with a function that recurses up the tree
67 # to find the actual base type.
68 my %baseTypeHash = ("Object" => 1, "Node" => 1, "NodeList" => 1, "NamedNodeMap" => 1, "DOMImplementation" => 1,
69                     "Event" => 1, "CSSRule" => 1, "CSSValue" => 1, "StyleSheet" => 1, "MediaList" => 1,
70                     "Counter" => 1, "Rect" => 1, "RGBColor" => 1, "XPathExpression" => 1, "XPathResult" => 1,
71                     "NodeIterator" => 1, "TreeWalker" => 1, "AbstractView" => 1, "Blob" => 1,
72                     "SVGAngle" => 1, "SVGAnimatedAngle" => 1, "SVGAnimatedBoolean" => 1, "SVGAnimatedEnumeration" => 1,
73                     "SVGAnimatedInteger" => 1, "SVGAnimatedLength" => 1, "SVGAnimatedLengthList" => 1,
74                     "SVGAnimatedNumber" => 1, "SVGAnimatedNumberList" => 1, "SVGAnimatedPoints" => 1,
75                     "SVGAnimatedPreserveAspectRatio" => 1, "SVGAnimatedRect" => 1, "SVGAnimatedString" => 1,
76                     "SVGAnimatedTransformList" => 1, "SVGLength" => 1, "SVGLengthList" => 1, "SVGMatrix" => 1,
77                     "SVGNumber" => 1, "SVGNumberList" => 1, "SVGPathSeg" => 1, "SVGPathSegList" => 1, "SVGPoint" => 1,
78                     "SVGPointList" => 1, "SVGPreserveAspectRatio" => 1, "SVGRect" => 1, "SVGRenderingIntent" => 1,
79                     "SVGStringList" => 1, "SVGTransform" => 1, "SVGTransformList" => 1, "SVGUnitTypes" => 1);
80
81 # Constants
82 my $buildingForTigerOrEarlier = 1 if $ENV{"MACOSX_DEPLOYMENT_TARGET"} and $ENV{"MACOSX_DEPLOYMENT_TARGET"} <= 10.4;
83 my $buildingForLeopardOrLater = 1 if $ENV{"MACOSX_DEPLOYMENT_TARGET"} and $ENV{"MACOSX_DEPLOYMENT_TARGET"} >= 10.5;
84 my $exceptionInit = "WebCore::ExceptionCode ec = 0;";
85 my $jsContextSetter = "WebCore::JSMainThreadNullState state;";
86 my $exceptionRaiseOnError = "WebCore::raiseOnDOMError(ec);";
87 my $assertMainThread = "{ DOM_ASSERT_MAIN_THREAD(); WebCoreThreadViolationCheckRoundOne(); }";
88
89 my %conflictMethod = (
90     # FIXME: Add C language keywords?
91     # FIXME: Add other predefined types like "id"?
92
93     "callWebScriptMethod:withArguments:" => "WebScriptObject",
94     "evaluateWebScript:" => "WebScriptObject",
95     "removeWebScriptKey:" => "WebScriptObject",
96     "setException:" => "WebScriptObject",
97     "setWebScriptValueAtIndex:value:" => "WebScriptObject",
98     "stringRepresentation" => "WebScriptObject",
99     "webScriptValueAtIndex:" => "WebScriptObject",
100
101     "autorelease" => "NSObject",
102     "awakeAfterUsingCoder:" => "NSObject",
103     "class" => "NSObject",
104     "classForCoder" => "NSObject",
105     "conformsToProtocol:" => "NSObject",
106     "copy" => "NSObject",
107     "copyWithZone:" => "NSObject",
108     "dealloc" => "NSObject",
109     "description" => "NSObject",
110     "doesNotRecognizeSelector:" => "NSObject",
111     "encodeWithCoder:" => "NSObject",
112     "finalize" => "NSObject",
113     "forwardInvocation:" => "NSObject",
114     "hash" => "NSObject",
115     "init" => "NSObject",
116     "initWithCoder:" => "NSObject",
117     "isEqual:" => "NSObject",
118     "isKindOfClass:" => "NSObject",
119     "isMemberOfClass:" => "NSObject",
120     "isProxy" => "NSObject",
121     "methodForSelector:" => "NSObject",
122     "methodSignatureForSelector:" => "NSObject",
123     "mutableCopy" => "NSObject",
124     "mutableCopyWithZone:" => "NSObject",
125     "performSelector:" => "NSObject",
126     "release" => "NSObject",
127     "replacementObjectForCoder:" => "NSObject",
128     "respondsToSelector:" => "NSObject",
129     "retain" => "NSObject",
130     "retainCount" => "NSObject",
131     "self" => "NSObject",
132     "superclass" => "NSObject",
133     "zone" => "NSObject",
134 );
135
136 my $fatalError = 0;
137
138 # Default License Templates
139 my $headerLicenseTemplate = << "EOF";
140 /*
141  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
142  * Copyright (C) 2006 Samuel Weinig <sam.weinig\@gmail.com>
143  *
144  * Redistribution and use in source and binary forms, with or without
145  * modification, are permitted provided that the following conditions
146  * are met:
147  * 1. Redistributions of source code must retain the above copyright
148  *    notice, this list of conditions and the following disclaimer.
149  * 2. Redistributions in binary form must reproduce the above copyright
150  *    notice, this list of conditions and the following disclaimer in the
151  *    documentation and/or other materials provided with the distribution.
152  *
153  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
154  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
155  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
156  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
157  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
158  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
159  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
160  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
161  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
162  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
163  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
164  */
165 EOF
166
167 my $implementationLicenseTemplate = << "EOF";
168 /*
169  * This file is part of the WebKit open source project.
170  * This file has been generated by generate-bindings.pl. DO NOT MODIFY!
171  *
172  * Redistribution and use in source and binary forms, with or without
173  * modification, are permitted provided that the following conditions
174  * are met:
175  * 1. Redistributions of source code must retain the above copyright
176  *    notice, this list of conditions and the following disclaimer.
177  * 2. Redistributions in binary form must reproduce the above copyright
178  *    notice, this list of conditions and the following disclaimer in the
179  *    documentation and/or other materials provided with the distribution.
180  *
181  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
182  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
184  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
185  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
186  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
187  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
188  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
189  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
190  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
191  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
192  */
193 EOF
194
195 # Default constructor
196 sub new
197 {
198     my $object = shift;
199     my $reference = { };
200
201     $codeGenerator = shift;
202     $outputDir = shift;
203     shift; # $outputHeadersDir
204     shift; # $useLayerOnTop
205     shift; # $preprocessor
206     $writeDependencies = shift;
207
208     bless($reference, $object);
209     return $reference;
210 }
211
212 sub finish
213 {
214     my $object = shift;
215 }
216
217 sub ReadPublicInterfaces
218 {
219     my $class = shift;
220     my $superClass = shift;
221     my $defines = shift;
222     my $isProtocol = shift;
223
224     my $found = 0;
225     my $actualSuperClass;
226     %publicInterfaces = ();
227
228     my $fileName = "WebCore/bindings/objc/PublicDOMInterfaces.h";
229     my $gccLocation = "";
230     if ($ENV{CC}) {
231         $gccLocation = $ENV{CC};
232     } elsif (($Config::Config{'osname'}) =~ /solaris/i) {
233         $gccLocation = "/usr/sfw/bin/gcc";
234     } else {
235         $gccLocation = "/usr/bin/gcc";
236     }
237     open FILE, "-|", $gccLocation, "-E", "-P", "-x", "objective-c",
238         (map { "-D$_" } split(/ +/, $defines)), "-DOBJC_CODE_GENERATION", $fileName or die "Could not open $fileName";
239     my @documentContent = <FILE>;
240     close FILE;
241
242     foreach $line (@documentContent) {
243         if (!$isProtocol && $line =~ /^\s*\@interface\s*$class\s*:\s*(\w+)\s*([A-Z0-9_]*)/) {
244             if ($superClass ne $1) {
245                 warn "Public API change. Superclass for \"$class\" differs ($1 != $superClass)";
246                 $fatalError = 1;
247             }
248
249             $interfaceAvailabilityVersion = $2 if defined $2;
250             $found = 1;
251             next;
252         } elsif ($isProtocol && $line =~ /^\s*\@protocol $class\s*<[^>]+>\s*([A-Z0-9_]*)/) {
253             $interfaceAvailabilityVersion = $1 if defined $1;
254             $found = 1;
255             next;
256         }
257
258         last if $found and $line =~ /^\s?\@end\s?$/;
259
260         if ($found) {
261             # trim whitspace
262             $line =~ s/^\s+//;
263             $line =~ s/\s+$//;
264
265             my $availabilityMacro = "";
266             $line =~ s/\s([A-Z0-9_]+)\s*;$/;/;
267             $availabilityMacro = $1 if defined $1;
268
269             $publicInterfaces{$line} = $availabilityMacro if length $line;
270         }
271     }
272
273     # If this class was not found in PublicDOMInterfaces.h then it should be considered as an entirely new public class.
274     $newPublicClass = !$found;
275     $interfaceAvailabilityVersion = "WEBKIT_VERSION_LATEST" if $newPublicClass;
276 }
277
278 sub GenerateConditionalString
279 {
280     my $node = shift;
281     my $conditional = $node->extendedAttributes->{"Conditional"};
282     if ($conditional) {
283         if ($conditional =~ /&/) {
284             return "ENABLE(" . join(") && ENABLE(", split(/&/, $conditional)) . ")";
285         } elsif ($conditional =~ /\|/) {
286             return "ENABLE(" . join(") || ENABLE(", split(/\|/, $conditional)) . ")";
287         } else {
288             return "ENABLE(" . $conditional . ")";
289         }
290     } else {
291         return "";
292     }
293 }
294
295 # Params: 'domClass' struct
296 sub GenerateInterface
297 {
298     my $object = shift;
299     my $dataNode = shift;
300     my $defines = shift;
301
302     $fatalError = 0;
303
304     my $name = $dataNode->name;
305     my $className = GetClassName($name);
306     my $parentClassName = "DOM" . GetParentImplClassName($dataNode);
307     $isProtocol = $dataNode->extendedAttributes->{ObjCProtocol};
308     $noImpl = $dataNode->extendedAttributes->{ObjCCustomImplementation} || $isProtocol;
309
310     ReadPublicInterfaces($className, $parentClassName, $defines, $isProtocol);
311
312     # Start actual generation..
313     $object->GenerateHeader($dataNode);
314     $object->GenerateImplementation($dataNode) unless $noImpl;
315
316     # Write changes.
317     $object->WriteData("DOM" . $name);
318
319     # Check for missing public API
320     if (keys %publicInterfaces > 0) {
321         my $missing = join("\n", keys %publicInterfaces);
322         warn "Public API change. There are missing public properties and/or methods from the \"$className\" class.\n$missing\n";
323         $fatalError = 1;
324     }
325
326     die if $fatalError;
327 }
328
329 # Params: 'idlDocument' struct
330 sub GenerateModule
331 {
332     my $object = shift;
333     my $dataNode = shift;
334
335     $module = $dataNode->module;
336 }
337
338 sub GetClassName
339 {
340     my $name = $codeGenerator->StripModule(shift);
341
342     # special cases
343     return "NSString" if $codeGenerator->IsStringType($name) or $name eq "SerializedScriptValue";
344     return "NS$name" if IsNativeObjCType($name);
345     return "BOOL" if $name eq "boolean";
346     return "unsigned" if $name eq "unsigned long";
347     return "int" if $name eq "long";
348     return "NSTimeInterval" if $name eq "Date";
349     return "DOMAbstractView" if $name eq "DOMWindow";
350     return $name if $codeGenerator->IsPrimitiveType($name) or $name eq "DOMImplementation" or $name eq "DOMTimeStamp";
351
352     # Default, assume Objective-C type has the same type name as
353     # idl type prefixed with "DOM".
354     return "DOM$name";
355 }
356
357 sub GetClassHeaderName
358 {
359     my $name = shift;
360
361     return "DOMDOMImplementation" if $name eq "DOMImplementation";
362     return $name;
363 }
364
365 sub GetImplClassName
366 {
367     my $name = $codeGenerator->StripModule(shift);
368
369     return "DOMImplementationFront" if $name eq "DOMImplementation";
370     return "DOMWindow" if $name eq "AbstractView";
371     return $name;
372 }
373
374 sub GetParentImplClassName
375 {
376     my $dataNode = shift;
377
378     return "Object" if @{$dataNode->parents} eq 0;
379
380     my $parent = $codeGenerator->StripModule($dataNode->parents(0));
381
382     # special cases
383     return "Object" if $parent eq "HTMLCollection";
384
385     return $parent;
386 }
387
388 sub GetParentAndProtocols
389 {
390     my $dataNode = shift;
391     my $numParents = @{$dataNode->parents};
392
393     my $parent = "";
394     my @protocols = ();
395     if ($numParents eq 0) {
396         if ($isProtocol) {
397             push(@protocols, "NSObject");
398             push(@protocols, "NSCopying") if $dataNode->name eq "EventTarget";
399         } else {
400             $parent = "DOMObject";
401         }
402     } elsif ($numParents eq 1) {
403         my $parentName = $codeGenerator->StripModule($dataNode->parents(0));
404         if ($isProtocol) {
405             die "Parents of protocols must also be protocols." unless IsProtocolType($parentName);
406             push(@protocols, "DOM" . $parentName);
407         } else {
408             if (IsProtocolType($parentName)) {
409                 push(@protocols, "DOM" . $parentName);
410             } elsif ($parentName eq "HTMLCollection") {
411                 $parent = "DOMObject";
412             } else {
413                 $parent = "DOM" . $parentName;
414             }
415         }
416     } else {
417         my @parents = @{$dataNode->parents};
418         my $firstParent = $codeGenerator->StripModule(shift(@parents));
419         if (IsProtocolType($firstParent)) {
420             push(@protocols, "DOM" . $firstParent);
421             if (!$isProtocol) {
422                 $parent = "DOMObject";
423             }
424         } else {
425             $parent = "DOM" . $firstParent;
426         }
427
428         foreach my $parentName (@parents) {
429             $parentName = $codeGenerator->StripModule($parentName);
430             die "Everything past the first class should be a protocol!" unless IsProtocolType($parentName);
431
432             push(@protocols, "DOM" . $parentName);
433         }
434     }
435
436     return ($parent, @protocols);
437 }
438
439 sub GetBaseClass
440 {
441     $parent = shift;
442
443     return $parent if $parent eq "Object" or IsBaseType($parent);
444     return "Event" if $parent eq "UIEvent";
445     return "CSSValue" if $parent eq "SVGColor" or $parent eq "CSSValueList";
446     return "Node";
447 }
448
449 sub IsBaseType
450 {
451     my $type = shift;
452
453     return 1 if $baseTypeHash{$type};
454     return 0;
455 }
456
457 sub IsProtocolType
458 {
459     my $type = shift;
460
461     return 1 if $protocolTypeHash{$type};
462     return 0;
463 }
464
465 sub IsNativeObjCType
466 {
467     my $type = shift;
468
469     return 1 if $nativeObjCTypeHash{$type};
470     return 0;
471 }
472
473 sub GetObjCType
474 {
475     my $type = shift;
476     my $name = GetClassName($type);
477
478     return "id <$name>" if IsProtocolType($type);
479     return $name if $codeGenerator->IsPrimitiveType($type) or $type eq "DOMTimeStamp";
480     return "unsigned short" if $type eq "CompareHow" or $type eq "SVGPaintType";
481     return "$name *";
482 }
483
484 sub GetPropertyAttributes
485 {
486     my $type = $codeGenerator->StripModule(shift);
487     my $readOnly = shift;
488
489     my @attributes = ();
490
491     push(@attributes, "readonly") if $readOnly;
492
493     # FIXME: <rdar://problem/5049934> Consider using 'nonatomic' on the DOM @property declarations.
494     if ($codeGenerator->IsStringType($type) || IsNativeObjCType($type)) {
495         push(@attributes, "copy");
496     } elsif ($codeGenerator->IsPodType($type) || $codeGenerator->IsSVGAnimatedType($type)) {
497         push(@attributes, "retain");
498     } elsif (!$codeGenerator->IsStringType($type) && !$codeGenerator->IsPrimitiveType($type) && $type ne "DOMTimeStamp" && $type ne "CompareHow" && $type ne "SVGPaintType") {
499         push(@attributes, "retain");
500     }
501
502     return "" unless @attributes > 0;
503     return "(" . join(", ", @attributes) . ")";
504 }
505
506 sub ConversionNeeded
507 {
508     my $type = $codeGenerator->StripModule(shift);
509
510     return !$codeGenerator->IsNonPointerType($type) && !$codeGenerator->IsStringType($type) && !IsNativeObjCType($type);
511 }
512
513 sub GetObjCTypeGetter
514 {
515     my $argName = shift;
516     my $type = $codeGenerator->StripModule(shift);
517
518     return $argName if $codeGenerator->IsPrimitiveType($type) or $codeGenerator->IsStringType($type) or IsNativeObjCType($type);
519     return $argName . "Node" if $type eq "EventTarget";
520     return "static_cast<WebCore::Range::CompareHow>($argName)" if $type eq "CompareHow";
521     return "static_cast<WebCore::SVGPaint::SVGPaintType>($argName)" if $type eq "SVGPaintType";
522     return "WTF::getPtr(nativeEventListener)" if $type eq "EventListener";
523     return "WTF::getPtr(nativeNodeFilter)" if $type eq "NodeFilter";
524     return "WTF::getPtr(nativeResolver)" if $type eq "XPathNSResolver";
525     
526     if ($type eq "SerializedScriptValue") {
527         $implIncludes{"SerializedScriptValue.h"} = 1;
528         return "WebCore::SerializedScriptValue::create(WTF::String($argName))";
529     }
530     return "core($argName)";
531 }
532
533 sub AddForwardDeclarationsForType
534 {
535     my $type = $codeGenerator->StripModule(shift);
536     my $public = shift;
537
538     return if $codeGenerator->IsNonPointerType($type) ;
539
540     my $class = GetClassName($type);
541
542     if (IsProtocolType($type)) {
543         $headerForwardDeclarationsForProtocols{$class} = 1 if $public;
544         $privateHeaderForwardDeclarationsForProtocols{$class} = 1 if !$public and !$headerForwardDeclarationsForProtocols{$class};
545         return;
546     }
547
548     $headerForwardDeclarations{$class} = 1 if $public;
549
550     # Private headers include the public header, so only add a forward declaration to the private header
551     # if the public header does not already have the same forward declaration.
552     $privateHeaderForwardDeclarations{$class} = 1 if !$public and !$headerForwardDeclarations{$class};
553 }
554
555 sub AddIncludesForType
556 {
557     my $type = $codeGenerator->StripModule(shift);
558
559     return if $codeGenerator->IsNonPointerType($type);
560
561     if (IsNativeObjCType($type)) {
562         if ($type eq "Color") {
563             $implIncludes{"ColorMac.h"} = 1;
564         }
565         return;
566     }
567
568     if ($codeGenerator->IsStringType($type)) {
569         $implIncludes{"KURL.h"} = 1;
570         return;
571     }
572
573     if ($type eq "DOMWindow") {
574         $implIncludes{"DOMAbstractViewInternal.h"} = 1;
575         $implIncludes{"DOMWindow.h"} = 1;
576         return;
577     }
578
579     if ($type eq "DOMImplementation") {
580         $implIncludes{"DOMDOMImplementationInternal.h"} = 1;
581         $implIncludes{"DOMImplementationFront.h"} = 1;
582         return;
583     }
584
585     if ($type eq "EventTarget") {
586         $implIncludes{"Node.h"} = 1;
587         $implIncludes{"DOMEventTarget.h"} = 1;
588         return;
589     }
590
591     if ($codeGenerator->IsSVGNewStyleAnimatedType($type)) {
592         $implIncludes{"${type}.h"} = 1;
593         $implIncludes{"DOM${type}Internal.h"} = 1;
594         return;
595     }
596
597     if ($codeGenerator->IsSVGAnimatedType($type)) {
598         $implIncludes{"DeprecatedSVGAnimatedTemplate.h"} = 1;
599         $implIncludes{"DOM${type}Internal.h"} = 1;
600         return;
601     }
602
603     if ($type eq "SVGPoint") {
604         $implIncludes{"FloatPoint.h"} = 1;
605         $implIncludes{"DOMSVGPointInternal.h"} = 1;
606         return;
607     }
608
609     if ($type eq "SVGMatrix") {
610         $implIncludes{"AffineTransform.h"} = 1;
611         $implIncludes{"DOMSVGMatrixInternal.h"} = 1;
612         $implIncludes{"SVGException.h"} = 1;
613         return;
614     }
615
616     if ($type eq "SVGNumber") {
617         $implIncludes{"DOMSVGNumberInternal.h"} = 1;
618         return;
619     }
620
621     if ($type =~ /(\w+)(Abs|Rel)$/) {
622         $implIncludes{"$1.h"} = 1;
623         $implIncludes{"DOM${type}Internal.h"} = 1;
624         return;
625     }
626
627     if ($type eq "NodeFilter") {
628         $implIncludes{"NodeFilter.h"} = 1;
629         $implIncludes{"ObjCNodeFilterCondition.h"} = 1;
630         return;
631     }
632
633     if ($type eq "EventListener") {
634         $implIncludes{"EventListener.h"} = 1;
635         $implIncludes{"ObjCEventListener.h"} = 1;
636         return;
637     }
638
639     if ($type eq "XPathNSResolver") {
640         $implIncludes{"DOMCustomXPathNSResolver.h"} = 1;
641         $implIncludes{"XPathNSResolver.h"} = 1;
642         return;
643     }
644
645     if ($type eq "SerializedScriptValue") {
646         $implIncludes{"SerializedScriptValue.h"} = 1;
647         return;
648     }
649
650     # FIXME: won't compile without these
651     $implIncludes{"CSSMutableStyleDeclaration.h"} = 1 if $type eq "CSSStyleDeclaration";
652     $implIncludes{"NameNodeList.h"} = 1 if $type eq "NodeList";
653
654     # Default, include the same named file (the implementation) and the same name prefixed with "DOM". 
655     $implIncludes{"$type.h"} = 1 if not $codeGenerator->AvoidInclusionOfType($type);
656     $implIncludes{"DOM${type}Internal.h"} = 1;
657 }
658
659 sub GetSVGPropertyTypes
660 {
661     my $implType = shift;
662
663     my $svgPropertyType;
664     my $svgListPropertyType;
665     my $svgNativeType;
666
667     return ($svgPropertyType, $svgListPropertyType, $svgNativeType) if not $implType =~ /SVG/;
668
669     $svgNativeType = $codeGenerator->GetSVGTypeNeedingTearOff($implType);
670     return ($svgPropertyType, $svgListPropertyType, $svgNativeType) if not $svgNativeType;
671
672     # Append space to avoid compilation errors when using  PassRefPtr<$svgNativeType>
673     $svgNativeType = "WebCore::$svgNativeType ";
674     $svgNativeType =~ s/</\<WebCore::/;
675
676     my $svgWrappedNativeType = $codeGenerator->GetSVGWrappedTypeNeedingTearOff($implType);
677     if ($svgNativeType =~ /SVGPropertyTearOff/) {
678         $svgPropertyType = "WebCore::$svgWrappedNativeType";
679         $svgPropertyType =~ s/</\<WebCore::/;
680     } elsif ($svgNativeType =~ /SVGListPropertyTearOff/) {
681         $svgListPropertyType = "WebCore::$svgWrappedNativeType";
682         $svgListPropertyType =~ s/</\<WebCore::/;
683     }
684
685     return ($svgPropertyType, $svgListPropertyType, $svgNativeType);
686 }
687
688 sub GenerateHeader
689 {
690     my $object = shift;
691     my $dataNode = shift;
692
693     my $interfaceName = $dataNode->name;
694     my $className = GetClassName($interfaceName);
695
696     my $parentName = "";
697     my @protocolsToImplement = ();
698     ($parentName, @protocolsToImplement) = GetParentAndProtocols($dataNode);
699
700     my $numConstants = @{$dataNode->constants};
701     my $numAttributes = @{$dataNode->attributes};
702     my $numFunctions = @{$dataNode->functions};
703
704     # - Add default header template
705     @headerContentHeader = split("\r", $headerLicenseTemplate);
706     push(@headerContentHeader, "\n");
707
708     # - INCLUDES -
709     my $includedWebKitAvailabilityHeader = 0;
710     unless ($isProtocol) {
711         my $parentHeaderName = GetClassHeaderName($parentName);
712         push(@headerContentHeader, "#import <WebCore/$parentHeaderName.h>\n");
713         $includedWebKitAvailabilityHeader = 1;
714     }
715
716     foreach my $parentProtocol (@protocolsToImplement) {
717         next if $parentProtocol =~ /^NS/; 
718         $parentProtocol = GetClassHeaderName($parentProtocol);
719         push(@headerContentHeader, "#import <WebCore/$parentProtocol.h>\n");
720         $includedWebKitAvailabilityHeader = 1;
721     }
722
723     # Special case needed for legacy support of DOMRange
724     if ($interfaceName eq "Range") {
725         push(@headerContentHeader, "#import <WebCore/DOMCore.h>\n");
726         push(@headerContentHeader, "#import <WebCore/DOMDocument.h>\n");
727         push(@headerContentHeader, "#import <WebCore/DOMRangeException.h>\n");
728         $includedWebKitAvailabilityHeader = 1;
729     }
730
731     push(@headerContentHeader, "#import <JavaScriptCore/WebKitAvailability.h>\n") unless $includedWebKitAvailabilityHeader;
732
733     my $interfaceAvailabilityVersionCheck = "#if WEBKIT_VERSION_MAX_ALLOWED >= $interfaceAvailabilityVersion\n\n";
734
735     push(@headerContentHeader, "\n");
736     push(@headerContentHeader, $interfaceAvailabilityVersionCheck) if length $interfaceAvailabilityVersion;
737
738     # - Add constants.
739     if ($numConstants > 0) {
740         my @headerConstants = ();
741
742         # FIXME: we need a way to include multiple enums.
743         foreach my $constant (@{$dataNode->constants}) {
744             my $constantName = $constant->name;
745             my $constantValue = $constant->value;
746
747             my $output = "    DOM_" . $constantName . " = " . $constantValue;
748             push(@headerConstants, $output);
749         }
750
751         my $combinedConstants = join(",\n", @headerConstants);
752
753         # FIXME: the formatting of the enums should line up the equal signs.
754         # FIXME: enums are unconditionally placed in the public header.
755         push(@headerContent, "enum {\n");
756         push(@headerContent, $combinedConstants);
757         push(@headerContent, "\n};\n\n");        
758     }
759
760     # - Begin @interface or @protocol
761     my $interfaceDeclaration = ($isProtocol ? "\@protocol $className" : "\@interface $className : $parentName");
762     $interfaceDeclaration .= " <" . join(", ", @protocolsToImplement) . ">" if @protocolsToImplement > 0;
763     $interfaceDeclaration .= "\n";
764
765     push(@headerContent, $interfaceDeclaration);
766
767     my @headerAttributes = ();
768     my @privateHeaderAttributes = ();
769
770     # - Add attribute getters/setters.
771     if ($numAttributes > 0) {
772         foreach my $attribute (@{$dataNode->attributes}) {
773             my $attributeName = $attribute->signature->name;
774
775             if ($attributeName eq "id" or $attributeName eq "hash" or $attributeName eq "description") {
776                 # Special case some attributes (like id and hash) to have a "Name" suffix to avoid ObjC naming conflicts.
777                 $attributeName .= "Name";
778             } elsif ($attributeName eq "frame") {
779                 # Special case attribute frame to be frameBorders.
780                 $attributeName .= "Borders";
781             }
782
783             my $attributeType = GetObjCType($attribute->signature->type);
784             my $attributeIsReadonly = ($attribute->type =~ /^readonly/);
785
786             my $property = "\@property" . GetPropertyAttributes($attribute->signature->type, $attributeIsReadonly);
787             # Some SVGFE*Element.idl use 'operator' as attribute name, rewrite as '_operator' to avoid clashes with C/C++
788             $attributeName =~ s/operator/_operator/ if ($attributeName =~ /operator/);
789             $property .= " " . $attributeType . ($attributeType =~ /\*$/ ? "" : " ") . $attributeName;
790
791             my $publicInterfaceKey = $property . ";";
792
793             my $availabilityMacro = "";
794             if (defined $publicInterfaces{$publicInterfaceKey} and length $publicInterfaces{$publicInterfaceKey}) {
795                 $availabilityMacro = $publicInterfaces{$publicInterfaceKey};
796             }
797
798             $availabilityMacro = "WEBKIT_OBJC_METHOD_ANNOTATION($availabilityMacro)" if length $availabilityMacro and $buildingForTigerOrEarlier;
799
800             my $declarationSuffix = ";\n";
801             $declarationSuffix = " $availabilityMacro;\n" if length $availabilityMacro;
802
803             my $public = (defined $publicInterfaces{$publicInterfaceKey} or $newPublicClass);
804             delete $publicInterfaces{$publicInterfaceKey};
805
806             AddForwardDeclarationsForType($attribute->signature->type, $public);
807
808             my $setterName = "set" . ucfirst($attributeName) . ":";
809
810             my $conflict = $conflictMethod{$attributeName};
811             if ($conflict) {
812                 warn "$className conflicts with $conflict method $attributeName\n";
813                 $fatalError = 1;
814             }
815
816             $conflict = $conflictMethod{$setterName};
817             if ($conflict) {
818                 warn "$className conflicts with $conflict method $setterName\n";
819                 $fatalError = 1;
820             }
821
822             if ($buildingForLeopardOrLater) {
823                 $property .= $declarationSuffix;
824                 push(@headerAttributes, $property) if $public;
825                 push(@privateHeaderAttributes, $property) unless $public;
826             } else {
827                 my $attributeConditionalString = GenerateConditionalString($attribute->signature);
828                 if ($attributeConditionalString) {
829                     push(@headerAttributes, "#if ${attributeConditionalString}\n") if $public;
830                     push(@privateHeaderAttributes, "#if ${attributeConditionalString}\n") unless $public;
831                 }
832
833                 # - GETTER
834                 my $getter = "- (" . $attributeType . ")" . $attributeName . $declarationSuffix;
835                 push(@headerAttributes, $getter) if $public;
836                 push(@privateHeaderAttributes, $getter) unless $public;
837
838                 # - SETTER
839                 if (!$attributeIsReadonly) {
840                     my $setter = "- (void)$setterName(" . $attributeType . ")new" . ucfirst($attributeName) . $declarationSuffix;
841                     push(@headerAttributes, $setter) if $public;
842                     push(@privateHeaderAttributes, $setter) unless $public;
843                 }
844
845                 if ($attributeConditionalString) {
846                     push(@headerAttributes, "#endif\n") if $public;
847                     push(@privateHeaderAttributes, "#endif\n") unless $public;
848                 }
849             }
850         }
851
852         push(@headerContent, @headerAttributes) if @headerAttributes > 0;
853     }
854
855     my @headerFunctions = ();
856     my @privateHeaderFunctions = ();
857     my @deprecatedHeaderFunctions = ();
858
859     # - Add functions.
860     if ($numFunctions > 0) {
861         foreach my $function (@{$dataNode->functions}) {
862             my $functionName = $function->signature->name;
863
864             my $returnType = GetObjCType($function->signature->type);
865             my $needsDeprecatedVersion = (@{$function->parameters} > 1 and $function->signature->extendedAttributes->{"OldStyleObjC"});
866             my $numberOfParameters = @{$function->parameters};
867             my %typesToForwardDeclare = ($function->signature->type => 1);
868
869             my $parameterIndex = 0;
870             my $functionSig = "- ($returnType)$functionName";
871             my $methodName = $functionName;
872             foreach my $param (@{$function->parameters}) {
873                 my $paramName = $param->name;
874                 my $paramType = GetObjCType($param->type);
875
876                 $typesToForwardDeclare{$param->type} = 1;
877
878                 if ($parameterIndex >= 1) {
879                     my $paramPrefix = $param->extendedAttributes->{"ObjCPrefix"};
880                     $paramPrefix = $paramName unless defined($paramPrefix);
881                     $functionSig .= " $paramPrefix";
882                     $methodName .= $paramPrefix;
883                 }
884
885                 $functionSig .= ":($paramType)$paramName";
886                 $methodName .= ":";
887
888                 $parameterIndex++;
889             }
890
891             my $publicInterfaceKey = $functionSig . ";";
892
893             my $conflict = $conflictMethod{$methodName};
894             if ($conflict) {
895                 warn "$className conflicts with $conflict method $methodName\n";
896                 $fatalError = 1;
897             }
898
899             if ($isProtocol && !$newPublicClass && !defined $publicInterfaces{$publicInterfaceKey}) {
900                 warn "Protocol method $publicInterfaceKey is not in PublicDOMInterfaces.h. Protocols require all methods to be public";
901                 $fatalError = 1;
902             }
903
904             my $availabilityMacro = "";
905             if (defined $publicInterfaces{$publicInterfaceKey} and length $publicInterfaces{$publicInterfaceKey}) {
906                 $availabilityMacro = $publicInterfaces{$publicInterfaceKey};
907             }
908
909             $availabilityMacro = "WEBKIT_OBJC_METHOD_ANNOTATION($availabilityMacro)" if length $availabilityMacro and $buildingForTigerOrEarlier;
910
911             my $functionDeclaration = $functionSig;
912             $functionDeclaration .= " " . $availabilityMacro if length $availabilityMacro;
913             $functionDeclaration .= ";\n";
914
915             my $public = (defined $publicInterfaces{$publicInterfaceKey} or $newPublicClass);
916             delete $publicInterfaces{$publicInterfaceKey};
917
918             foreach my $type (keys %typesToForwardDeclare) {
919                 # add any forward declarations to the public header if a deprecated version will be generated
920                 AddForwardDeclarationsForType($type, 1) if $needsDeprecatedVersion;
921                 AddForwardDeclarationsForType($type, $public) unless $public and $needsDeprecatedVersion;
922             }
923
924             push(@headerFunctions, $functionDeclaration) if $public;
925             push(@privateHeaderFunctions, $functionDeclaration) unless $public;
926
927             # generate the old style method names with un-named parameters, these methods are deprecated
928             if ($needsDeprecatedVersion) {
929                 my $deprecatedFunctionSig = $functionSig;
930                 $deprecatedFunctionSig =~ s/\s\w+:/ :/g; # remove parameter names
931
932                 $publicInterfaceKey = $deprecatedFunctionSig . ";";
933
934                 my $availabilityMacro = "AVAILABLE_WEBKIT_VERSION_1_3_AND_LATER_BUT_DEPRECATED_IN_WEBKIT_VERSION_3_0";
935                 if (defined $publicInterfaces{$publicInterfaceKey} and length $publicInterfaces{$publicInterfaceKey}) {
936                     $availabilityMacro = $publicInterfaces{$publicInterfaceKey};
937                 }
938
939                 $availabilityMacro = "WEBKIT_OBJC_METHOD_ANNOTATION($availabilityMacro)" if $buildingForTigerOrEarlier;
940
941                 $functionDeclaration = "$deprecatedFunctionSig $availabilityMacro;\n";
942
943                 push(@deprecatedHeaderFunctions, $functionDeclaration);
944
945                 unless (defined $publicInterfaces{$publicInterfaceKey}) {
946                     warn "Deprecated method $publicInterfaceKey is not in PublicDOMInterfaces.h. All deprecated methods need to be public, or should have the OldStyleObjC IDL attribute removed";
947                     $fatalError = 1;
948                 }
949
950                 delete $publicInterfaces{$publicInterfaceKey};
951             }
952         }
953
954         if (@headerFunctions > 0) {
955             push(@headerContent, "\n") if $buildingForLeopardOrLater and @headerAttributes > 0;
956             push(@headerContent, @headerFunctions);
957         }
958     }
959
960     if (@deprecatedHeaderFunctions > 0 && $isProtocol) {
961         push(@headerContent, @deprecatedHeaderFunctions);
962     }
963
964     # - End @interface or @protocol
965     push(@headerContent, "\@end\n");
966
967     if (@deprecatedHeaderFunctions > 0 && !$isProtocol) {
968         # - Deprecated category @interface 
969         push(@headerContent, "\n\@interface $className (" . $className . "Deprecated)\n");
970         push(@headerContent, @deprecatedHeaderFunctions);
971         push(@headerContent, "\@end\n");
972     }
973
974     push(@headerContent, "\n#endif\n") if length $interfaceAvailabilityVersion;
975
976     my %alwaysGenerateForNoSVGBuild = map { $_ => 1 } qw(DOMHTMLEmbedElement DOMHTMLObjectElement);
977
978     if (@privateHeaderAttributes > 0 or @privateHeaderFunctions > 0 or exists $alwaysGenerateForNoSVGBuild{$className}) {
979         # - Private category @interface
980         @privateHeaderContentHeader = split("\r", $headerLicenseTemplate);
981         push(@privateHeaderContentHeader, "\n");
982
983         my $classHeaderName = GetClassHeaderName($className);
984         push(@privateHeaderContentHeader, "#import <WebCore/$classHeaderName.h>\n\n");
985         push(@privateHeaderContentHeader, $interfaceAvailabilityVersionCheck) if length $interfaceAvailabilityVersion;
986
987         @privateHeaderContent = ();
988         push(@privateHeaderContent, "\@interface $className (" . $className . "Private)\n");
989         push(@privateHeaderContent, @privateHeaderAttributes) if @privateHeaderAttributes > 0;
990         push(@privateHeaderContent, "\n") if $buildingForLeopardOrLater and @privateHeaderAttributes > 0 and @privateHeaderFunctions > 0;
991         push(@privateHeaderContent, @privateHeaderFunctions) if @privateHeaderFunctions > 0;
992         push(@privateHeaderContent, "\@end\n");
993
994         push(@privateHeaderContent, "\n#endif\n") if length $interfaceAvailabilityVersion;
995     }
996
997     unless ($isProtocol) {
998         # Generate internal interfaces
999         my $podType = $dataNode->extendedAttributes->{"PODType"};
1000         my $podTypeWithNamespace;
1001         my $implClassName = GetImplClassName($interfaceName);
1002         my $implClassNameWithNamespace = "WebCore::" . $implClassName;
1003
1004         my $implType = $implClassNameWithNamespace;
1005         my ($svgPropertyType, $svgListPropertyType, $svgNativeType) = GetSVGPropertyTypes($implClassName);
1006         $implType = $svgNativeType if $svgNativeType;
1007
1008         if ($podType) {
1009             $podTypeWithNamespace = ($podType eq "float") ? "$podType" : "WebCore::$podType";
1010         }
1011  
1012         # Generate interface definitions. 
1013         @internalHeaderContent = split("\r", $implementationLicenseTemplate);
1014
1015         push(@internalHeaderContent, "\n#import <WebCore/$className.h>\n\n");
1016         push(@internalHeaderContent, "#import <WebCore/SVGAnimatedPropertyTearOff.h>\n\n") if $svgPropertyType;
1017         push(@internalHeaderContent, "#import <WebCore/SVGAnimatedListPropertyTearOff.h>\n\n") if $svgListPropertyType;
1018         push(@internalHeaderContent, $interfaceAvailabilityVersionCheck) if length $interfaceAvailabilityVersion;
1019
1020         if ($interfaceName eq "Node") {
1021             push(@internalHeaderContent, "\@protocol DOMEventTarget;\n\n");
1022         }
1023
1024         my $startedNamespace = 0;
1025
1026         if ($codeGenerator->IsSVGNewStyleAnimatedType($interfaceName)) {
1027             push(@internalHeaderContent, "#import <WebCore/$implClassName.h>\n\n");
1028         } elsif ($codeGenerator->IsSVGAnimatedType($interfaceName)) {
1029             push(@internalHeaderContent, "#import <WebCore/DeprecatedSVGAnimatedTemplate.h>\n\n");
1030         } else {
1031             push(@internalHeaderContent, "namespace WebCore {\n");
1032             $startedNamespace = 1;
1033             if ($podType and $podType ne "float") {
1034                 push(@internalHeaderContent, "    class $podType;\n");
1035             } elsif ($interfaceName eq "Node") {
1036                 push(@internalHeaderContent, "    class EventTarget;\n    class Node;\n");
1037             } else {
1038                 push(@internalHeaderContent, "    class $implClassName;\n");
1039             }
1040             push(@internalHeaderContent, "}\n\n");
1041         }
1042
1043         if ($podType) {
1044             push(@internalHeaderContent, "$podTypeWithNamespace core($className *);\n");
1045             push(@internalHeaderContent, "$className *kit($podTypeWithNamespace);\n");
1046         } else {
1047             push(@internalHeaderContent, "$implType* core($className *);\n");
1048             push(@internalHeaderContent, "$className *kit($implType*);\n");
1049         }
1050
1051         if ($dataNode->extendedAttributes->{Polymorphic}) {
1052             push(@internalHeaderContent, "Class kitClass($implType*);\n");
1053         }
1054
1055         if ($interfaceName eq "Node") {
1056             push(@internalHeaderContent, "id <DOMEventTarget> kit(WebCore::EventTarget*);\n");
1057         }
1058
1059         push(@internalHeaderContent, "\n#endif\n") if length $interfaceAvailabilityVersion;
1060     }
1061 }
1062
1063 sub GenerateImplementation
1064 {
1065     my $object = shift;
1066     my $dataNode = shift;
1067
1068     my @ancestorInterfaceNames = ();
1069
1070     if (@{$dataNode->parents} > 1) {
1071         $codeGenerator->AddMethodsConstantsAndAttributesFromParentClasses($dataNode, \@ancestorInterfaceNames);
1072     }
1073
1074     my $interfaceName = $dataNode->name;
1075     my $className = GetClassName($interfaceName);
1076     my $implClassName = GetImplClassName($interfaceName);
1077     my $parentImplClassName = GetParentImplClassName($dataNode);
1078     my $implClassNameWithNamespace = "WebCore::" . $implClassName;
1079     my $baseClass = GetBaseClass($parentImplClassName);
1080     my $classHeaderName = GetClassHeaderName($className);
1081
1082     my $numAttributes = @{$dataNode->attributes};
1083     my $numFunctions = @{$dataNode->functions};
1084
1085     my $podType = $dataNode->extendedAttributes->{"PODType"};
1086     my $podTypeWithNamespace;
1087     my $implType = $implClassNameWithNamespace;
1088
1089     my ($svgPropertyType, $svgListPropertyType, $svgNativeType) = GetSVGPropertyTypes($implClassName);
1090     $implType = $svgNativeType if $svgNativeType;
1091
1092     if ($podType) {
1093         $podTypeWithNamespace = ($podType eq "float") ? "$podType" : "WebCore::$podType";
1094     }
1095
1096     # - Add default header template.
1097     @implContentHeader = split("\r", $implementationLicenseTemplate);
1098
1099     # - INCLUDES -
1100     push(@implContentHeader, "\n#import \"config.h\"\n");
1101
1102     my $conditionalString = GenerateConditionalString($dataNode);
1103     push(@implContentHeader, "\n#if ${conditionalString}\n\n") if $conditionalString;
1104
1105     push(@implContentHeader, "#import \"DOMInternal.h\"\n\n");
1106     push(@implContentHeader, "#import \"$classHeaderName.h\"\n\n");
1107
1108     $implIncludes{"ExceptionHandlers.h"} = 1;
1109     $implIncludes{"ThreadCheck.h"} = 1;
1110     $implIncludes{"JSMainThreadExecState.h"} = 1;
1111     $implIncludes{"WebScriptObjectPrivate.h"} = 1;
1112     $implIncludes{$classHeaderName . "Internal.h"} = 1;
1113
1114     # FIXME: These includes are only needed when the class is a subclass of one of these polymorphic classes.
1115     $implIncludes{"DOMBlobInternal.h"} = 1;
1116     $implIncludes{"DOMCSSRuleInternal.h"} = 1;
1117     $implIncludes{"DOMCSSValueInternal.h"} = 1;
1118     $implIncludes{"DOMEventInternal.h"} = 1;
1119     $implIncludes{"DOMNodeInternal.h"} = 1;
1120     $implIncludes{"DOMStyleSheetInternal.h"} = 1;
1121
1122     $implIncludes{"DOMSVGPathSegInternal.h"} = 1 if $interfaceName =~ /^SVGPathSeg.+/;
1123
1124     if ($codeGenerator->IsSVGAnimatedType($interfaceName) and !$codeGenerator->IsSVGNewStyleAnimatedType($interfaceName)) {
1125         $implIncludes{"DeprecatedSVGAnimatedTemplate.h"} = 1;
1126     } elsif ($interfaceName =~ /(\w+)(Abs|Rel)$/) {
1127         $implIncludes{"$1.h"} = 1;
1128     } else {
1129         if (!$podType) {
1130             $implIncludes{"$implClassName.h"} = 1 if not $codeGenerator->AvoidInclusionOfType($implClassName);
1131         } else {
1132             $implIncludes{"$podType.h"} = 1 unless $podType eq "float";
1133         }
1134     } 
1135
1136     @implContent = ();
1137
1138     push(@implContent, "#import <wtf/GetPtr.h>\n\n");
1139
1140     # add implementation accessor
1141     if ($podType) {
1142         push(@implContent, "#define IMPL reinterpret_cast<$podTypeWithNamespace*>(_internal)\n\n");
1143     } elsif ($parentImplClassName eq "Object") {
1144         push(@implContent, "#define IMPL reinterpret_cast<$implType*>(_internal)\n\n");
1145     } else {
1146         my $baseClassWithNamespace = "WebCore::$baseClass";
1147         push(@implContent, "#define IMPL static_cast<$implClassNameWithNamespace*>(reinterpret_cast<$baseClassWithNamespace*>(_internal))\n\n");
1148     }
1149
1150     # START implementation
1151     push(@implContent, "\@implementation $className\n\n");
1152
1153     # Only generate 'dealloc' and 'finalize' methods for direct subclasses of DOMObject.
1154     if ($parentImplClassName eq "Object") {
1155         $implIncludes{"WebCoreObjCExtras.h"} = 1;
1156         push(@implContent, "- (void)dealloc\n");
1157         push(@implContent, "{\n");
1158         push(@implContent, "    if (WebCoreObjCScheduleDeallocateOnMainThread([$className class], self))\n");
1159         push(@implContent, "        return;\n");
1160         push(@implContent, "\n");
1161         if ($interfaceName eq "NodeIterator") {
1162             push(@implContent, "    if (_internal) {\n");
1163             push(@implContent, "        [self detach];\n");
1164             push(@implContent, "        IMPL->deref();\n");
1165             push(@implContent, "    };\n");
1166         } elsif ($podType) {
1167             push(@implContent, "    delete IMPL;\n");
1168         } else {
1169             push(@implContent, "    if (_internal)\n");
1170             push(@implContent, "        IMPL->deref();\n");
1171         }
1172         push(@implContent, "    [super dealloc];\n");
1173         push(@implContent, "}\n\n");
1174
1175         push(@implContent, "- (void)finalize\n");
1176         push(@implContent, "{\n");
1177         if ($interfaceName eq "NodeIterator") {
1178             push(@implContent, "    if (_internal) {\n");
1179             push(@implContent, "        [self detach];\n");
1180             push(@implContent, "        IMPL->deref();\n");
1181             push(@implContent, "    };\n");
1182         } elsif ($podType) {
1183             push(@implContent, "    delete IMPL;\n");
1184         } else {
1185             push(@implContent, "    if (_internal)\n");
1186             push(@implContent, "        IMPL->deref();\n");
1187         }
1188         push(@implContent, "    [super finalize];\n");
1189         push(@implContent, "}\n\n");
1190         
1191     }
1192
1193     %attributeNames = ();
1194
1195     # - Attributes
1196     if ($numAttributes > 0) {
1197         foreach my $attribute (@{$dataNode->attributes}) {
1198             AddIncludesForType($attribute->signature->type);
1199
1200             my $idlType = $codeGenerator->StripModule($attribute->signature->type);
1201
1202             my $attributeName = $attribute->signature->name;
1203             my $attributeType = GetObjCType($attribute->signature->type);
1204             my $attributeIsReadonly = ($attribute->type =~ /^readonly/);
1205             my $attributeClassName = GetClassName($attribute->signature->type);
1206
1207             my $attributeInterfaceName = $attributeName;
1208             if ($attributeName eq "id" or $attributeName eq "hash" or $attributeName eq "description") {
1209                 # Special case some attributes (like id and hash) to have a "Name" suffix to avoid ObjC naming conflicts.
1210                 $attributeInterfaceName .= "Name";
1211             } elsif ($attributeName eq "frame") {
1212                 # Special case attribute frame to be frameBorders.
1213                 $attributeInterfaceName .= "Borders";
1214             } elsif ($attributeName eq "operator") {
1215                 # Avoid clash with C++ keyword.
1216                 $attributeInterfaceName = "_operator";
1217             }
1218
1219             $attributeNames{$attributeInterfaceName} = 1;
1220
1221             # - GETTER
1222             my $getterSig = "- ($attributeType)$attributeInterfaceName\n";
1223
1224             my $getterExpressionPrefix = $codeGenerator->GetterExpressionPrefix(\%implIncludes, $interfaceName, $attribute);
1225
1226             # FIXME: Special case attribute ownerDocument to call document. This makes it return the
1227             # document when called on the document itself. Legacy behavior, see <https://bugs.webkit.org/show_bug.cgi?id=10889>.
1228             $getterExpressionPrefix =~ s/\bownerDocument\b/document/;
1229
1230             my $hasGetterException = @{$attribute->getterExceptions};
1231             my $getterContentHead = "IMPL->$getterExpressionPrefix";
1232             my $getterContentTail = ")";
1233
1234             if ($svgPropertyType) {
1235                 $getterContentHead = "$getterExpressionPrefix";
1236
1237                 # TODO: Handle special case for DOMSVGLength. We do need Custom code support for this.
1238                 if ($svgPropertyType eq "WebCore::SVGLength" and $attributeName eq "value") {
1239                     $getterContentHead = "value(0 /* FIXME */";
1240                 }
1241             } else {
1242                 # Special case for DOMSVGNumber
1243                 if ($podType and $podType eq "float") {
1244                     $getterContentHead = "*IMPL";
1245                     $getterContentTail = "";
1246                 }
1247             }
1248
1249             my $attributeTypeSansPtr = $attributeType;
1250             $attributeTypeSansPtr =~ s/ \*$//; # Remove trailing " *" from pointer types.
1251
1252             # special case for EventTarget protocol
1253             $attributeTypeSansPtr = "DOMNode" if $idlType eq "EventTarget";
1254
1255             # Special cases
1256             my @customGetterContent = (); 
1257             if ($attributeTypeSansPtr eq "DOMImplementation") {
1258                 # FIXME: We have to special case DOMImplementation until DOMImplementationFront is removed
1259                 $getterContentHead = "kit(implementationFront(IMPL";
1260                 $getterContentTail .= ")";
1261             } elsif ($attributeName =~ /(\w+)DisplayString$/) {
1262                 my $attributeToDisplay = $1;
1263                 $getterContentHead = "WebCore::displayString(IMPL->$attributeToDisplay(), core(self)";
1264                 $implIncludes{"HitTestResult.h"} = 1;
1265             } elsif ($attributeName =~ /^absolute(\w+)URL$/) {
1266                 my $typeOfURL = $1;
1267                 $getterContentHead = "[self _getURLAttribute:";
1268                 if ($typeOfURL eq "Link") {
1269                     $getterContentTail = "\@\"href\"]";
1270                 } elsif ($typeOfURL eq "Image") {
1271                     if ($interfaceName eq "HTMLObjectElement") {
1272                         $getterContentTail = "\@\"data\"]";
1273                     } else {
1274                         $getterContentTail = "\@\"src\"]";
1275                     }
1276                     unless ($interfaceName eq "HTMLImageElement") {
1277                         push(@customGetterContent, "    if (!IMPL->renderer() || !IMPL->renderer()->isImage())\n");
1278                         push(@customGetterContent, "        return nil;\n");
1279                         $implIncludes{"RenderObject.h"} = 1;
1280                     }
1281                 }
1282                 $implIncludes{"DOMPrivate.h"} = 1;
1283             } elsif ($attribute->signature->extendedAttributes->{"ConvertToString"}) {
1284                 $getterContentHead = "WTF::String::number(" . $getterContentHead;
1285                 $getterContentTail .= ")";
1286             } elsif ($codeGenerator->IsPodType($idlType) or $idlType eq "Date") {
1287                 $getterContentHead = "kit($getterContentHead";
1288                 $getterContentTail .= ")";
1289             } elsif ($svgPropertyType) {
1290                 $getterContentHead = "IMPL->propertyReference().$getterContentHead";
1291             } elsif ($codeGenerator->IsSVGNewStyleAnimatedType($implClassName) and $codeGenerator->IsSVGTypeNeedingTearOff($idlType)) {
1292                 my $idlTypeWithNamespace = "WebCore::" . $codeGenerator->GetSVGTypeNeedingTearOff($idlType);
1293                 $idlTypeWithNamespace =~ s/</\<WebCore::/;
1294                 $getterContentHead = "kit(static_cast<$idlTypeWithNamespace*>($getterContentHead)";
1295                 $getterContentTail .= ")";
1296             } elsif (IsProtocolType($idlType) and $idlType ne "EventTarget") {
1297                 $getterContentHead = "kit($getterContentHead";
1298                 $getterContentTail .= ")";
1299             } elsif ($idlType eq "Color") {
1300                 $getterContentHead = "WebCore::nsColor($getterContentHead";
1301                 $getterContentTail .= ")";
1302             } elsif ($attribute->signature->type eq "SerializedScriptValue") {
1303                 $getterContentHead = "$getterContentHead";
1304                 $getterContentTail .= "->toString()";                
1305             } elsif (ConversionNeeded($attribute->signature->type)) {
1306                 if ($codeGenerator->IsSVGTypeNeedingTearOff($attribute->signature->type) and not $implClassName =~ /List$/) {
1307                     my $idlType = $attribute->signature->type;
1308                     my $idlTypeWithNamespace = "WebCore::" . $codeGenerator->GetSVGTypeNeedingTearOff($idlType);
1309                     $idlTypeWithNamespace =~ s/</\<WebCore::/;
1310                     $getterContentHead = "kit(WTF::getPtr(${idlTypeWithNamespace}::create($getterContentHead";
1311                     $getterContentTail .= ")))";
1312                 } else {
1313                     $getterContentHead = "kit(WTF::getPtr($getterContentHead";
1314                     $getterContentTail .= "))";
1315                 }
1316             }
1317
1318             my $getterContent;
1319             if ($hasGetterException) {
1320                 $getterContent = $getterContentHead . "ec" . $getterContentTail;
1321             } else {
1322                 $getterContent = $getterContentHead . $getterContentTail;
1323             }
1324
1325             my $attributeConditionalString = GenerateConditionalString($attribute->signature);
1326             push(@implContent, "#if ${attributeConditionalString}\n") if $attributeConditionalString;
1327             push(@implContent, $getterSig);
1328             push(@implContent, "{\n");
1329             push(@implContent, "    $jsContextSetter\n");
1330             push(@implContent, @customGetterContent);
1331             if ($hasGetterException) {
1332                 # Differentiated between when the return type is a pointer and
1333                 # not for white space issue (ie. Foo *result vs. int result).
1334                 if ($attributeType =~ /\*$/) {
1335                     $getterContent = $attributeType . "result = " . $getterContent;
1336                 } else {
1337                     $getterContent = $attributeType . " result = " . $getterContent;
1338                 }
1339
1340                 push(@implContent, "    $exceptionInit\n");
1341                 push(@implContent, "    $getterContent;\n");
1342                 push(@implContent, "    $exceptionRaiseOnError\n");
1343                 push(@implContent, "    return result;\n");
1344             } else {
1345                 push(@implContent, "    return $getterContent;\n");
1346             }
1347             push(@implContent, "}\n");
1348
1349             # - SETTER
1350             if (!$attributeIsReadonly) {
1351                 # Exception handling
1352                 my $hasSetterException = @{$attribute->setterExceptions};
1353
1354                 my $coreSetterName = "set" . $codeGenerator->WK_ucfirst($attributeName);
1355                 my $setterName = "set" . ucfirst($attributeInterfaceName);
1356                 my $argName = "new" . ucfirst($attributeInterfaceName);
1357                 my $arg = GetObjCTypeGetter($argName, $idlType);
1358
1359                 # The definition of ConvertToString is flipped for the setter
1360                 if ($attribute->signature->extendedAttributes->{"ConvertToString"}) {
1361                     $arg = "WTF::String($arg).toInt()";
1362                 }
1363
1364                 my $setterSig = "- (void)$setterName:($attributeType)$argName\n";
1365
1366                 push(@implContent, "\n");
1367                 push(@implContent, $setterSig);
1368                 push(@implContent, "{\n");
1369                 push(@implContent, "    $jsContextSetter\n");
1370
1371                 unless ($codeGenerator->IsPrimitiveType($idlType) or $codeGenerator->IsStringType($idlType)) {
1372                     push(@implContent, "    ASSERT($argName);\n\n");
1373                 }
1374
1375                 if ($idlType eq "Date") {
1376                     $arg = "core(" . $arg . ")";
1377                 }
1378
1379                 if ($svgPropertyType) {
1380                     $getterContentHead = "$getterExpressionPrefix";
1381                     push(@implContent, "    $svgPropertyType& podImpl = IMPL->propertyReference();\n");
1382                     my $ec = $hasSetterException ? ", ec" : "";
1383                     push(@implContent, "    $exceptionInit\n") if $hasSetterException;
1384                     push(@implContent, "    podImpl.$coreSetterName($arg$ec);\n");
1385                     if ($hasSetterException) {
1386                         push(@implContent, "    if (!ec)\n");
1387                         push(@implContent, "        IMPL->commitChange();\n");
1388                         push(@implContent, "    $exceptionRaiseOnError\n");
1389                     } else {
1390                         push(@implContent, "        IMPL->commitChange();\n");
1391                     }
1392                 } elsif ($svgListPropertyType) {
1393                     $getterContentHead = "$getterExpressionPrefix";
1394                     push(@implContent, "    IMPL->$coreSetterName($arg);\n");
1395                 } elsif ($podType) {
1396                     # Special case for DOMSVGNumber
1397                     if ($podType eq "float") {
1398                         push(@implContent, "    *IMPL = $arg;\n");
1399                     } else {
1400                         push(@implContent, "    IMPL->$coreSetterName($arg);\n");
1401                     }
1402                 } else {
1403                     my $setterExpressionPrefix = $codeGenerator->SetterExpressionPrefix(\%implIncludes, $interfaceName, $attribute);
1404                     my $ec = $hasSetterException ? ", ec" : "";
1405                     push(@implContent, "    $exceptionInit\n") if $hasSetterException;
1406                     push(@implContent, "    IMPL->$setterExpressionPrefix$arg$ec);\n");
1407                     push(@implContent, "    $exceptionRaiseOnError\n") if $hasSetterException;
1408                 }
1409
1410                 push(@implContent, "}\n");
1411             }
1412
1413             push(@implContent, "#endif\n") if $attributeConditionalString;
1414             push(@implContent, "\n");
1415         }
1416     }
1417
1418     # - Functions
1419     if ($numFunctions > 0) {
1420         foreach my $function (@{$dataNode->functions}) {
1421             AddIncludesForType($function->signature->type);
1422
1423             my $functionName = $function->signature->name;
1424             my $returnType = GetObjCType($function->signature->type);
1425             my $hasParameters = @{$function->parameters};
1426             my $raisesExceptions = @{$function->raisesExceptions};
1427
1428             my @parameterNames = ();
1429             my @needsAssert = ();
1430             my %needsCustom = ();
1431
1432             my $parameterIndex = 0;
1433             my $functionSig = "- ($returnType)$functionName";
1434             foreach my $param (@{$function->parameters}) {
1435                 my $paramName = $param->name;
1436                 my $paramType = GetObjCType($param->type);
1437
1438                 # make a new parameter name if the original conflicts with a property name
1439                 $paramName = "in" . ucfirst($paramName) if $attributeNames{$paramName};
1440
1441                 AddIncludesForType($param->type);
1442
1443                 my $idlType = $codeGenerator->StripModule($param->type);
1444                 my $implGetter = GetObjCTypeGetter($paramName, $idlType);
1445
1446                 push(@parameterNames, $implGetter);
1447                 $needsCustom{"XPathNSResolver"} = $paramName if $idlType eq "XPathNSResolver";
1448                 $needsCustom{"NodeFilter"} = $paramName if $idlType eq "NodeFilter";
1449                 $needsCustom{"EventListener"} = $paramName if $idlType eq "EventListener";
1450                 $needsCustom{"EventTarget"} = $paramName if $idlType eq "EventTarget";
1451                 $needsCustom{"NodeToReturn"} = $paramName if $param->extendedAttributes->{"Return"};
1452
1453                 unless ($codeGenerator->IsPrimitiveType($idlType) or $codeGenerator->IsStringType($idlType)) {
1454                     push(@needsAssert, "    ASSERT($paramName);\n");
1455                 }
1456
1457                 if ($parameterIndex >= 1) {
1458                     my $paramPrefix = $param->extendedAttributes->{"ObjCPrefix"};
1459                     $paramPrefix = $param->name unless defined($paramPrefix);
1460                     $functionSig .= " $paramPrefix";
1461                 }
1462
1463                 $functionSig .= ":($paramType)$paramName";
1464
1465                 $parameterIndex++;
1466             }
1467
1468             my @functionContent = ();
1469             my $caller = "IMPL";
1470
1471             # special case the XPathNSResolver
1472             if (defined $needsCustom{"XPathNSResolver"}) {
1473                 my $paramName = $needsCustom{"XPathNSResolver"};
1474                 push(@functionContent, "    WebCore::XPathNSResolver* nativeResolver = 0;\n");
1475                 push(@functionContent, "    RefPtr<WebCore::XPathNSResolver> customResolver;\n");
1476                 push(@functionContent, "    if ($paramName) {\n");
1477                 push(@functionContent, "        if ([$paramName isMemberOfClass:[DOMNativeXPathNSResolver class]])\n");
1478                 push(@functionContent, "            nativeResolver = core(static_cast<DOMNativeXPathNSResolver *>($paramName));\n");
1479                 push(@functionContent, "        else {\n");
1480                 push(@functionContent, "            customResolver = WebCore::DOMCustomXPathNSResolver::create($paramName);\n");
1481                 push(@functionContent, "            nativeResolver = WTF::getPtr(customResolver);\n");
1482                 push(@functionContent, "        }\n");
1483                 push(@functionContent, "    }\n");
1484             }
1485
1486             # special case the EventTarget
1487             if (defined $needsCustom{"EventTarget"}) {
1488                 my $paramName = $needsCustom{"EventTarget"};
1489                 push(@functionContent, "    DOMNode* ${paramName}ObjC = $paramName;\n");
1490                 push(@functionContent, "    WebCore::Node* ${paramName}Node = core(${paramName}ObjC);\n");
1491                 $implIncludes{"DOMNode.h"} = 1;
1492                 $implIncludes{"Node.h"} = 1;
1493             }
1494
1495             if ($function->signature->extendedAttributes->{"UsesView"}) {
1496                 push(@functionContent, "    WebCore::DOMWindow* dv = $caller->defaultView();\n");
1497                 push(@functionContent, "    if (!dv)\n");
1498                 push(@functionContent, "        return nil;\n");
1499                 $implIncludes{"DOMWindow.h"} = 1;
1500                 $caller = "dv";
1501             }
1502
1503             # special case the EventListener
1504             if (defined $needsCustom{"EventListener"}) {
1505                 my $paramName = $needsCustom{"EventListener"};
1506                 push(@functionContent, "    RefPtr<WebCore::EventListener> nativeEventListener = WebCore::ObjCEventListener::wrap($paramName);\n");
1507             }
1508
1509             # special case the NodeFilter
1510             if (defined $needsCustom{"NodeFilter"}) {
1511                 my $paramName = $needsCustom{"NodeFilter"};
1512                 push(@functionContent, "    RefPtr<WebCore::NodeFilter> nativeNodeFilter;\n");
1513                 push(@functionContent, "    if ($paramName)\n");
1514                 push(@functionContent, "        nativeNodeFilter = WebCore::NodeFilter::create(WebCore::ObjCNodeFilterCondition::create($paramName));\n");
1515             }
1516
1517             # FIXME! We need [Custom] support for ObjC, to move these hacks into DOMSVGLength/MatrixCustom.mm
1518             my $svgMatrixRotateFromVector = ($podType and $podType eq "AffineTransform" and $functionName eq "rotateFromVector");
1519             my $svgMatrixInverse = ($podType and $podType eq "AffineTransform" and $functionName eq "inverse");
1520             my $svgLengthConvertToSpecifiedUnits = ($svgPropertyType and $svgPropertyType eq "WebCore::SVGLength" and $functionName eq "convertToSpecifiedUnits");
1521
1522             push(@parameterNames, "ec") if $raisesExceptions and !($svgMatrixRotateFromVector || $svgMatrixInverse);
1523             push(@parameterNames, "0 /* FIXME */") if $svgLengthConvertToSpecifiedUnits; 
1524
1525             # Handle arguments that are 'SVGProperty' based (SVGAngle/SVGLength). We need to convert from SVGPropertyTearOff<Type>* to Type,
1526             # to be able to call the desired WebCore function. If the conversion fails, we can't extract Type and need to raise an exception.
1527             my $currentParameter = -1;
1528             foreach my $param (@{$function->parameters}) {
1529                 $currentParameter++;
1530
1531                 my $paramName = $param->name;
1532                 my $idlType = $codeGenerator->StripModule($param->type);
1533                 next if not $codeGenerator->IsSVGTypeNeedingTearOff($idlType) or $implClassName =~ /List$/;
1534
1535                 my $implGetter = GetObjCTypeGetter($paramName, $idlType);
1536                 my $idlTypeWithNamespace = "WebCore::" . $codeGenerator->GetSVGTypeNeedingTearOff($idlType);
1537                 $idlTypeWithNamespace =~ s/</\<WebCore::/;
1538
1539                 push(@functionContent, "    $idlTypeWithNamespace* ${paramName}Core = $implGetter;\n");
1540                 push(@functionContent, "    if (!${paramName}Core) {\n");
1541                 push(@functionContent, "        WebCore::ExceptionCode ec = WebCore::TYPE_MISMATCH_ERR;\n");
1542                 push(@functionContent, "        $exceptionRaiseOnError\n");
1543                 if ($returnType eq "void") { 
1544                     push(@functionContent, "        return;\n");
1545                 } else {
1546                     push(@functionContent, "        return nil;\n");
1547                 }
1548                 push(@functionContent, "    }\n");
1549
1550                 # Replace the paramter core() getter, by the cached variable.
1551                 splice(@parameterNames, $currentParameter, 1, "${paramName}Core->propertyReference()");
1552             }
1553
1554             my $content = $codeGenerator->WK_lcfirst($functionName) . "(" . join(", ", @parameterNames) . ")"; 
1555
1556             if ($svgPropertyType) {
1557                 push(@functionContent, "    $svgPropertyType& podImpl = IMPL->propertyReference();\n");
1558                 $content = "podImpl.$content;\n    IMPL->commitChange()"; 
1559             } else {
1560                 $content = $caller . "->$content";
1561             }
1562
1563             if ($svgMatrixRotateFromVector) {
1564                 # Special case with rotateFromVector & SVGMatrix        
1565                 push(@functionContent, "    $exceptionInit\n");
1566                 push(@functionContent, "    if (x == 0.0 || y == 0.0)\n");
1567                 push(@functionContent, "        ec = WebCore::SVGException::SVG_INVALID_VALUE_ERR;\n");
1568                 push(@functionContent, "    $exceptionRaiseOnError\n");
1569                 push(@functionContent, "    return kit($content);\n");
1570             } elsif ($svgMatrixInverse) {
1571                 # Special case with inverse & SVGMatrix
1572                 push(@functionContent, "    $exceptionInit\n");
1573                 push(@functionContent, "    if (!$caller->isInvertible())\n");
1574                 push(@functionContent, "        ec = WebCore::SVGException::SVG_MATRIX_NOT_INVERTABLE;\n");
1575                 push(@functionContent, "    $exceptionRaiseOnError\n");
1576                 push(@functionContent, "    return kit($content);\n");
1577             } elsif ($returnType eq "void") {
1578                 # Special case 'void' return type.
1579                 if ($raisesExceptions) {
1580                     push(@functionContent, "    $exceptionInit\n");
1581                     push(@functionContent, "    $content;\n");
1582                     push(@functionContent, "    $exceptionRaiseOnError\n");
1583                 } else {
1584                     push(@functionContent, "    $content;\n");
1585                 }
1586             } elsif (defined $needsCustom{"NodeToReturn"}) {
1587                 # Special case the insertBefore, replaceChild, removeChild 
1588                 # and appendChild functions from DOMNode 
1589                 my $toReturn = $needsCustom{"NodeToReturn"};
1590                 if ($raisesExceptions) {
1591                     push(@functionContent, "    $exceptionInit\n");
1592                     push(@functionContent, "    if ($content)\n");
1593                     push(@functionContent, "        return $toReturn;\n");
1594                     push(@functionContent, "    $exceptionRaiseOnError\n");
1595                     push(@functionContent, "    return nil;\n");
1596                 } else {
1597                     push(@functionContent, "    if ($content)\n");
1598                     push(@functionContent, "        return $toReturn;\n");
1599                     push(@functionContent, "    return nil;\n");
1600                 }
1601             } elsif ($returnType eq "SerializedScriptValue") {
1602                 $content = "foo";
1603             } else {
1604                 if (ConversionNeeded($function->signature->type)) {
1605                     if ($codeGenerator->IsSVGTypeNeedingTearOff($function->signature->type) and not $implClassName =~ /List$/) {
1606                         my $idlType = $function->signature->type;
1607                         my $idlTypeWithNamespace = "WebCore::" . $codeGenerator->GetSVGTypeNeedingTearOff($idlType);
1608                         $idlTypeWithNamespace =~ s/</\<WebCore::/;
1609                         $content = "kit(WTF::getPtr(${idlTypeWithNamespace}::create($content)))";
1610                     } elsif ($codeGenerator->IsPodType($function->signature->type)) {
1611                         $content = "kit($content)";
1612                     } else {
1613                         $content = "kit(WTF::getPtr($content))";
1614                     }
1615                 }
1616
1617                 if ($raisesExceptions) {
1618                     # Differentiated between when the return type is a pointer and
1619                     # not for white space issue (ie. Foo *result vs. int result).
1620                     if ($returnType =~ /\*$/) {
1621                         $content = $returnType . "result = " . $content;
1622                     } else {
1623                         $content = $returnType . " result = " . $content;
1624                     }
1625
1626                     push(@functionContent, "    $exceptionInit\n");
1627                     push(@functionContent, "    $content;\n");
1628                     push(@functionContent, "    $exceptionRaiseOnError\n");
1629                     push(@functionContent, "    return result;\n");
1630                 } else {
1631                     push(@functionContent, "    return $content;\n");
1632                 }
1633             }
1634
1635             push(@implContent, "$functionSig\n");
1636             push(@implContent, "{\n");
1637             push(@implContent, "    $jsContextSetter\n");
1638             push(@implContent, @functionContent);
1639             push(@implContent, "}\n\n");
1640
1641             # generate the old style method names with un-named parameters, these methods are deprecated
1642             if (@{$function->parameters} > 1 and $function->signature->extendedAttributes->{"OldStyleObjC"}) {
1643                 my $deprecatedFunctionSig = $functionSig;
1644                 $deprecatedFunctionSig =~ s/\s\w+:/ :/g; # remove parameter names
1645
1646                 push(@implContent, "$deprecatedFunctionSig\n");
1647                 push(@implContent, "{\n");
1648                 push(@implContent, "    $jsContextSetter\n");
1649                 push(@implContent, @functionContent);
1650                 push(@implContent, "}\n\n");
1651             }
1652
1653             # Clear the hash
1654             %needsCustom = ();
1655         }
1656     }
1657
1658     # END implementation
1659     push(@implContent, "\@end\n");
1660
1661     # Generate internal interfaces
1662     if ($podType) {
1663         push(@implContent, "\n$podTypeWithNamespace core($className *wrapper)\n");
1664         push(@implContent, "{\n");
1665         push(@implContent, "    return wrapper ? *reinterpret_cast<$podTypeWithNamespace*>(wrapper->_internal) : $podTypeWithNamespace();\n");
1666         push(@implContent, "}\n\n");
1667     } else {
1668         push(@implContent, "\n$implType* core($className *wrapper)\n");
1669         push(@implContent, "{\n");
1670         push(@implContent, "    return wrapper ? reinterpret_cast<$implType*>(wrapper->_internal) : 0;\n");
1671         push(@implContent, "}\n\n");
1672     }
1673
1674     if ($podType) {
1675         # FIXME: Implement caching.
1676         push(@implContent, "$className *kit($podTypeWithNamespace value)\n");
1677         push(@implContent, "{\n");
1678         push(@implContent, "    $assertMainThread;\n");
1679         push(@implContent, "    $className *wrapper = [[$className alloc] _init];\n");
1680         push(@implContent, "    wrapper->_internal = reinterpret_cast<DOMObjectInternal*>(new $podTypeWithNamespace(value));\n");
1681         push(@implContent, "    return [wrapper autorelease];\n");
1682         push(@implContent, "}\n");
1683     } elsif ($parentImplClassName eq "Object") {        
1684         push(@implContent, "$className *kit($implType* value)\n");
1685         push(@implContent, "{\n");
1686         push(@implContent, "    $assertMainThread;\n");
1687         push(@implContent, "    if (!value)\n");
1688         push(@implContent, "        return nil;\n");
1689         push(@implContent, "    if ($className *wrapper = getDOMWrapper(value))\n");
1690         push(@implContent, "        return [[wrapper retain] autorelease];\n");
1691         if ($dataNode->extendedAttributes->{Polymorphic}) {
1692             push(@implContent, "    $className *wrapper = [[kitClass(value) alloc] _init];\n");
1693             push(@implContent, "    if (!wrapper)\n");
1694             push(@implContent, "        return nil;\n");
1695         } else {
1696             push(@implContent, "    $className *wrapper = [[$className alloc] _init];\n");
1697         }
1698         push(@implContent, "    wrapper->_internal = reinterpret_cast<DOMObjectInternal*>(value);\n");
1699         push(@implContent, "    value->ref();\n");
1700         push(@implContent, "    addDOMWrapper(wrapper, value);\n");
1701         push(@implContent, "    return [wrapper autorelease];\n");
1702         push(@implContent, "}\n");
1703     } else {
1704         push(@implContent, "$className *kit($implType* value)\n");
1705         push(@implContent, "{\n");
1706         push(@implContent, "    $assertMainThread;\n");
1707         push(@implContent, "    return static_cast<$className*>(kit(static_cast<WebCore::$baseClass*>(value)));\n");
1708         push(@implContent, "}\n");
1709     }
1710
1711     # - End the ifdef conditional if necessary
1712     push(@implContent, "\n#endif // ${conditionalString}\n") if $conditionalString;
1713
1714     # - Generate dependencies.
1715     if ($writeDependencies && @ancestorInterfaceNames) {
1716         push(@depsContent, "$className.h : ", join(" ", map { "$_.idl" } @ancestorInterfaceNames), "\n");
1717         push(@depsContent, map { "$_.idl :\n" } @ancestorInterfaceNames); 
1718     }
1719 }
1720
1721 # Internal helper
1722 sub WriteData
1723 {
1724     my $object = shift;
1725     my $name = shift;
1726
1727     # Open files for writing...
1728     my $headerFileName = "$outputDir/" . $name . ".h";
1729     my $privateHeaderFileName = "$outputDir/" . $name . "Private.h";
1730     my $implFileName = "$outputDir/" . $name . ".mm";
1731     my $internalHeaderFileName = "$outputDir/" . $name . "Internal.h";
1732     my $depsFileName = "$outputDir/" . $name . ".dep";
1733
1734     # Remove old files.
1735     unlink($headerFileName);
1736     unlink($privateHeaderFileName);
1737     unlink($implFileName);
1738     unlink($internalHeaderFileName);
1739     unlink($depsFileName);
1740
1741     # Write public header.
1742     open(HEADER, ">$headerFileName") or die "Couldn't open file $headerFileName";
1743     
1744     print HEADER @headerContentHeader;
1745     print HEADER map { "\@class $_;\n" } sort keys(%headerForwardDeclarations);
1746     print HEADER map { "\@protocol $_;\n" } sort keys(%headerForwardDeclarationsForProtocols);
1747
1748     my $hasForwardDeclarations = keys(%headerForwardDeclarations) + keys(%headerForwardDeclarationsForProtocols);
1749     print HEADER "\n" if $hasForwardDeclarations;
1750     print HEADER @headerContent;
1751
1752     close(HEADER);
1753
1754     @headerContentHeader = ();
1755     @headerContent = ();
1756     %headerForwardDeclarations = ();
1757     %headerForwardDeclarationsForProtocols = ();
1758
1759     if (@privateHeaderContent > 0) {
1760         open(PRIVATE_HEADER, ">$privateHeaderFileName") or die "Couldn't open file $privateHeaderFileName";
1761
1762         print PRIVATE_HEADER @privateHeaderContentHeader;
1763         print PRIVATE_HEADER map { "\@class $_;\n" } sort keys(%privateHeaderForwardDeclarations);
1764         print PRIVATE_HEADER map { "\@protocol $_;\n" } sort keys(%privateHeaderForwardDeclarationsForProtocols);
1765
1766         $hasForwardDeclarations = keys(%privateHeaderForwardDeclarations) + keys(%privateHeaderForwardDeclarationsForProtocols);
1767         print PRIVATE_HEADER "\n" if $hasForwardDeclarations;
1768         print PRIVATE_HEADER @privateHeaderContent;
1769
1770         close(PRIVATE_HEADER);
1771
1772         @privateHeaderContentHeader = ();
1773         @privateHeaderContent = ();
1774         %privateHeaderForwardDeclarations = ();
1775         %privateHeaderForwardDeclarationsForProtocols = ();
1776     }
1777
1778     # Write implementation file.
1779     unless ($noImpl) {
1780         open(IMPL, ">$implFileName") or die "Couldn't open file $implFileName";
1781
1782         print IMPL @implContentHeader;
1783         print IMPL map { "#import \"$_\"\n" } sort keys(%implIncludes);
1784         print IMPL @implContent;
1785
1786         close(IMPL);
1787
1788         @implContentHeader = ();
1789         @implContent = ();
1790         %implIncludes = ();
1791     }
1792     
1793     if (@internalHeaderContent > 0) {
1794        open(INTERNAL_HEADER, ">$internalHeaderFileName") or die "Couldn't open file $internalHeaderFileName";
1795
1796        print INTERNAL_HEADER @internalHeaderContent;
1797
1798        close(INTERNAL_HEADER);
1799
1800        @internalHeaderContent = ();
1801     }
1802
1803     # Write dependency file.
1804     if (@depsContent) {
1805         open(DEPS, ">$depsFileName") or die "Couldn't open file $depsFileName";
1806         print DEPS @depsContent;
1807         close(DEPS);
1808         @depsContent = ();
1809     }
1810 }
1811
1812 1;