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