OSDN Git Service

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