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 Apple Inc. All rights reserved.
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Library General Public
10 # License as published by the Free Software Foundation; either
11 # version 2 of the License, or (at your option) any later version.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Library General Public License for more details.
18 # You should have received a copy of the GNU Library General Public License
19 # aint with this library; see the file COPYING.LIB. If not, write to
20 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 # Boston, MA 02110-1301, USA.
24 package CodeGeneratorObjC;
31 my %publicInterfaces = ();
32 my $newPublicClass = 0;
37 my @headerContentHeader = ();
38 my @headerContent = ();
39 my %headerForwardDeclarations = ();
40 my %headerForwardDeclarationsForProtocols = ();
42 my @privateHeaderContentHeader = ();
43 my @privateHeaderContent = ();
44 my %privateHeaderForwardDeclarations = ();
45 my %privateHeaderForwardDeclarationsForProtocols = ();
47 my @internalHeaderContent = ();
49 my @implContentHeader = ();
51 my %implIncludes = ();
54 my %protocolTypeHash = ("XPathNSResolver" => 1, "EventListener" => 1, "EventTarget" => 1, "NodeFilter" => 1,
55 "SVGLocatable" => 1, "SVGTransformable" => 1, "SVGStylable" => 1, "SVGFilterPrimitiveStandardAttributes" => 1,
56 "SVGTests" => 1, "SVGLangSpace" => 1, "SVGExternalResourcesRequired" => 1, "SVGURIReference" => 1,
57 "SVGZoomAndPan" => 1, "SVGFitToViewBox" => 1, "SVGAnimatedPathData" => 1, "SVGAnimatedPoints" => 1);
58 my %nativeObjCTypeHash = ("URL" => 1, "Color" => 1);
60 # FIXME: this should be replaced with a function that recurses up the tree
61 # to find the actual base type.
62 my %baseTypeHash = ("Object" => 1, "Node" => 1, "NodeList" => 1, "NamedNodeMap" => 1, "DOMImplementation" => 1,
63 "Event" => 1, "CSSRule" => 1, "CSSValue" => 1, "StyleSheet" => 1, "MediaList" => 1,
64 "Counter" => 1, "Rect" => 1, "RGBColor" => 1, "XPathExpression" => 1, "XPathResult" => 1,
65 "NodeIterator" => 1, "TreeWalker" => 1, "AbstractView" => 1,
66 "SVGAngle" => 1, "SVGAnimatedAngle" => 1, "SVGAnimatedBoolean" => 1, "SVGAnimatedEnumeration" => 1,
67 "SVGAnimatedInteger" => 1, "SVGAnimatedLength" => 1, "SVGAnimatedLengthList" => 1,
68 "SVGAnimatedNumber" => 1, "SVGAnimatedNumberList" => 1, "SVGAnimatedPoints" => 1,
69 "SVGAnimatedPreserveAspectRatio" => 1, "SVGAnimatedRect" => 1, "SVGAnimatedString" => 1,
70 "SVGAnimatedTransformList" => 1, "SVGLength" => 1, "SVGLengthList" => 1, "SVGMatrix" => 1,
71 "SVGNumber" => 1, "SVGNumberList" => 1, "SVGPathSeg" => 1, "SVGPathSegList" => 1, "SVGPoint" => 1,
72 "SVGPointList" => 1, "SVGPreserveAspectRatio" => 1, "SVGRect" => 1, "SVGRenderingIntent" => 1,
73 "SVGStringList" => 1, "SVGTransform" => 1, "SVGTransformList" => 1, "SVGUnitTypes" => 1);
76 my $buildingForTigerOrEarlier = 1 if $ENV{"MACOSX_DEPLOYMENT_TARGET"} and $ENV{"MACOSX_DEPLOYMENT_TARGET"} <= 10.4;
77 my $buildingForLeopardOrLater = 1 if $ENV{"MACOSX_DEPLOYMENT_TARGET"} and $ENV{"MACOSX_DEPLOYMENT_TARGET"} >= 10.5;
78 my $exceptionInit = "WebCore::ExceptionCode ec = 0;";
79 my $exceptionRaiseOnError = "WebCore::raiseOnDOMError(ec);";
80 my $assertMainThread = "{ DOM_ASSERT_MAIN_THREAD(); WebCoreThreadViolationCheck(); }";
82 my %conflictMethod = (
83 # FIXME: Add C language keywords?
84 # FIXME: Add other predefined types like "id"?
86 "callWebScriptMethod:withArguments:" => "WebScriptObject",
87 "evaluateWebScript:" => "WebScriptObject",
88 "removeWebScriptKey:" => "WebScriptObject",
89 "setException:" => "WebScriptObject",
90 "setWebScriptValueAtIndex:value:" => "WebScriptObject",
91 "stringRepresentation" => "WebScriptObject",
92 "webScriptValueAtIndex:" => "WebScriptObject",
94 "autorelease" => "NSObject",
95 "awakeAfterUsingCoder:" => "NSObject",
96 "class" => "NSObject",
97 "classForCoder" => "NSObject",
98 "conformsToProtocol:" => "NSObject",
100 "copyWithZone:" => "NSObject",
101 "dealloc" => "NSObject",
102 "description" => "NSObject",
103 "doesNotRecognizeSelector:" => "NSObject",
104 "encodeWithCoder:" => "NSObject",
105 "finalize" => "NSObject",
106 "forwardInvocation:" => "NSObject",
107 "hash" => "NSObject",
108 "init" => "NSObject",
109 "initWithCoder:" => "NSObject",
110 "isEqual:" => "NSObject",
111 "isKindOfClass:" => "NSObject",
112 "isMemberOfClass:" => "NSObject",
113 "isProxy" => "NSObject",
114 "methodForSelector:" => "NSObject",
115 "methodSignatureForSelector:" => "NSObject",
116 "mutableCopy" => "NSObject",
117 "mutableCopyWithZone:" => "NSObject",
118 "performSelector:" => "NSObject",
119 "release" => "NSObject",
120 "replacementObjectForCoder:" => "NSObject",
121 "respondsToSelector:" => "NSObject",
122 "retain" => "NSObject",
123 "retainCount" => "NSObject",
124 "self" => "NSObject",
125 "superclass" => "NSObject",
126 "zone" => "NSObject",
131 # Default Licence Templates
132 my $headerLicenceTemplate = << "EOF";
134 * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
135 * Copyright (C) 2006 Samuel Weinig <sam.weinig\@gmail.com>
137 * Redistribution and use in source and binary forms, with or without
138 * modification, are permitted provided that the following conditions
140 * 1. Redistributions of source code must retain the above copyright
141 * notice, this list of conditions and the following disclaimer.
142 * 2. Redistributions in binary form must reproduce the above copyright
143 * notice, this list of conditions and the following disclaimer in the
144 * documentation and/or other materials provided with the distribution.
146 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
147 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
148 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
149 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
150 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
151 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
152 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
153 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
154 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
155 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
156 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
160 my $implementationLicenceTemplate = << "EOF";
162 * This file is part of the WebKit open source project.
163 * This file has been generated by generate-bindings.pl. DO NOT MODIFY!
165 * Redistribution and use in source and binary forms, with or without
166 * modification, are permitted provided that the following conditions
168 * 1. Redistributions of source code must retain the above copyright
169 * notice, this list of conditions and the following disclaimer.
170 * 2. Redistributions in binary form must reproduce the above copyright
171 * notice, this list of conditions and the following disclaimer in the
172 * documentation and/or other materials provided with the distribution.
174 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
175 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
176 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
177 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
178 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
179 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
180 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
181 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
182 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
183 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
184 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
188 # Default constructor
194 $codeGenerator = shift;
197 bless($reference, $object);
206 # Uppercase the first letter, while respecting WebKit style guidelines.
207 # E.g., xmlEncoding becomes XMLEncoding, but xmllang becomes Xmllang.
211 my $ret = ucfirst($param);
212 $ret =~ s/Xml/XML/ if $ret =~ /^Xml[^a-z]/;
216 sub ReadPublicInterfaces
219 my $superClass = shift;
221 my $isProtocol = shift;
224 my $actualSuperClass;
225 %publicInterfaces = ();
227 my $fileName = "WebCore/bindings/objc/PublicDOMInterfaces.h";
228 open FILE, "-|", "/usr/bin/gcc", "-E", "-P", "-x", "objective-c",
229 (map { "-D$_" } split(/ /, $defines)), "-DOBJC_CODE_GENERATION", $fileName or die "Could not open $fileName";
230 my @documentContent = <FILE>;
233 foreach $line (@documentContent) {
234 if (!$isProtocol && $line =~ /^\s*\@interface\s*$class\s*:\s*(\w+)\s*/) {
235 if ($superClass ne $1) {
236 warn "Public API change. Superclass for \"$class\" differs ($1 != $superClass)";
242 } elsif ($isProtocol && $line =~ /^\s*\@protocol $class\s*/) {
247 last if $found and $line =~ /^\s?\@end\s?$/;
253 $publicInterfaces{$line} = 1 if length($line);
257 # If this class was not found in PublicDOMInterfaces.h then it should be considered as an entirely new public class.
258 $newPublicClass = ! $found;
261 # Params: 'domClass' struct
262 sub GenerateInterface
265 my $dataNode = shift;
270 my $name = $dataNode->name;
271 my $className = GetClassName($name);
272 my $parentClassName = "DOM" . GetParentImplClassName($dataNode);
273 $isProtocol = $dataNode->extendedAttributes->{ObjCProtocol};
274 $noImpl = $dataNode->extendedAttributes->{ObjCCustomImplementation} || $isProtocol;
276 ReadPublicInterfaces($className, $parentClassName, $defines, $isProtocol);
278 # Start actual generation..
279 $object->GenerateHeader($dataNode);
280 $object->GenerateImplementation($dataNode) unless $noImpl;
283 $object->WriteData("DOM" . $name);
285 # Check for missing public API
286 if (keys %publicInterfaces > 0) {
287 my $missing = join("\n", keys %publicInterfaces);
288 warn "Public API change. There are missing public properties and/or methods from the \"$className\" class.\n$missing\n";
295 # Params: 'idlDocument' struct
299 my $dataNode = shift;
301 $module = $dataNode->module;
306 my $name = $codeGenerator->StripModule(shift);
309 return "NSString" if $codeGenerator->IsStringType($name);
310 return "NS$name" if IsNativeObjCType($name);
311 return "BOOL" if $name eq "boolean";
312 return "unsigned" if $name eq "unsigned long";
313 return "int" if $name eq "long";
314 return "DOMAbstractView" if $name eq "DOMWindow";
315 return $name if $codeGenerator->IsPrimitiveType($name) or $name eq "DOMImplementation" or $name eq "DOMTimeStamp";
317 # Default, assume Objective-C type has the same type name as
318 # idl type prefixed with "DOM".
322 sub GetClassHeaderName
326 return "DOMDOMImplementation" if $name eq "DOMImplementation";
332 my $name = $codeGenerator->StripModule(shift);
334 return "DOMImplementationFront" if $name eq "DOMImplementation";
335 return "DOMWindow" if $name eq "AbstractView";
339 sub GetParentImplClassName
341 my $dataNode = shift;
343 return "Object" if @{$dataNode->parents} eq 0;
345 my $parent = $codeGenerator->StripModule($dataNode->parents(0));
348 return "Node" if $parent eq "EventTargetNode";
349 return "Object" if $parent eq "HTMLCollection";
354 sub GetParentAndProtocols
356 my $dataNode = shift;
357 my $numParents = @{$dataNode->parents};
361 if ($numParents eq 0) {
363 push(@protocols, "NSObject");
364 push(@protocols, "NSCopying") if $dataNode->name eq "EventTarget";
366 $parent = "DOMObject";
368 } elsif ($numParents eq 1) {
369 my $parentName = $codeGenerator->StripModule($dataNode->parents(0));
371 die "Parents of protocols must also be protocols." unless IsProtocolType($parentName);
372 push(@protocols, "DOM" . $parentName);
374 if (IsProtocolType($parentName)) {
375 push(@protocols, "DOM" . $parentName);
376 } elsif ($parentName eq "EventTargetNode") {
378 } elsif ($parentName eq "HTMLCollection") {
379 $parent = "DOMObject";
381 $parent = "DOM" . $parentName;
385 my @parents = @{$dataNode->parents};
386 my $firstParent = $codeGenerator->StripModule(shift(@parents));
387 if (IsProtocolType($firstParent)) {
388 push(@protocols, "DOM" . $firstParent);
390 $parent = "DOMObject";
393 $parent = "DOM" . $firstParent;
396 foreach my $parentName (@parents) {
397 $parentName = $codeGenerator->StripModule($parentName);
398 die "Everything past the first class should be a protocol!" unless IsProtocolType($parentName);
400 push(@protocols, "DOM" . $parentName);
404 return ($parent, @protocols);
411 return $parent if $parent eq "Object" or IsBaseType($parent);
412 return "Event" if $parent eq "UIEvent";
413 return "CSSValue" if $parent eq "SVGColor";
421 return 1 if $baseTypeHash{$type};
429 return 1 if $protocolTypeHash{$type};
437 return 1 if $nativeObjCTypeHash{$type};
444 my $name = GetClassName($type);
446 return "id <$name>" if IsProtocolType($type);
447 return $name if $codeGenerator->IsPrimitiveType($type) or $type eq "DOMTimeStamp";
448 return "unsigned short" if $type eq "CompareHow" or $type eq "SVGPaintType";
452 sub GetPropertyAttributes
454 my $type = $codeGenerator->StripModule(shift);
455 my $readOnly = shift;
459 push(@attributes, "readonly") if $readOnly;
461 # FIXME: uncomment these lines once <rdar://problem/4996504> is fixed.
462 # unless ($readOnly) {
463 if ($codeGenerator->IsStringType($type) || IsNativeObjCType($type)) {
464 push(@attributes, "copy");
465 } elsif ($codeGenerator->IsPodType($type) || $codeGenerator->IsSVGAnimatedType($type)) {
466 push(@attributes, "retain");
467 } elsif (!$codeGenerator->IsStringType($type) && !$codeGenerator->IsPrimitiveType($type) && $type ne "DOMTimeStamp" && $type ne "CompareHow" && $type ne "SVGPaintType") {
468 push(@attributes, "retain");
472 return "" unless @attributes > 0;
473 return "(" . join(", ", @attributes) . ")";
478 my $type = $codeGenerator->StripModule(shift);
480 return "" if $codeGenerator->IsNonPointerType($type) or $codeGenerator->IsStringType($type) or IsNativeObjCType($type);
481 return "_wrapAbstractView" if $type eq "DOMWindow";
485 sub GetObjCTypeGetterName
487 my $type = $codeGenerator->StripModule(shift);
490 if ($type =~ /^(HTML|CSS|SVG)/ or $type eq "DOMImplementation" or $type eq "CDATASection") {
492 } elsif ($type =~ /^XPath(.+)/) {
493 $typeGetter = "xpath" . $1;
494 } elsif ($type eq "DOMWindow") {
495 $typeGetter = "abstractView";
497 $typeGetter = lcfirst($type);
500 # put into the form "_fooBar" for type FooBar.
501 return "_" . $typeGetter;
504 sub GetObjCTypeGetter
507 my $type = $codeGenerator->StripModule(shift);
509 return $argName if $codeGenerator->IsPrimitiveType($type) or $codeGenerator->IsStringType($type) or IsNativeObjCType($type);
510 return $argName . "EventTarget" if $type eq "EventTarget";
511 return "static_cast<WebCore::Range::CompareHow>($argName)" if $type eq "CompareHow";
512 return "static_cast<WebCore::SVGPaint::SVGPaintType>($argName)" if $type eq "SVGPaintType";
514 my $typeGetterMethodName = GetObjCTypeGetterName($type);
516 return "nativeResolver" if $type eq "XPathNSResolver";
517 return "[$argName $typeGetterMethodName]";
520 sub AddForwardDeclarationsForType
522 my $type = $codeGenerator->StripModule(shift);
525 return if $codeGenerator->IsNonPointerType($type) ;
527 my $class = GetClassName($type);
529 if (IsProtocolType($type)) {
530 $headerForwardDeclarationsForProtocols{$class} = 1 if $public;
531 $privateHeaderForwardDeclarationsForProtocols{$class} = 1 if !$public and !$headerForwardDeclarationsForProtocols{$class};
535 $headerForwardDeclarations{$class} = 1 if $public;
537 # Private headers include the public header, so only add a forward declaration to the private header
538 # if the public header does not already have the same forward declaration.
539 $privateHeaderForwardDeclarations{$class} = 1 if !$public and !$headerForwardDeclarations{$class};
542 sub AddIncludesForType
544 my $type = $codeGenerator->StripModule(shift);
546 return if $codeGenerator->IsNonPointerType($type) or IsNativeObjCType($type);
548 if ($codeGenerator->IsStringType($type)) {
549 $implIncludes{"PlatformString.h"} = 1;
553 if ($type eq "RGBColor") {
554 $implIncludes{"Color.h"} = 1;
555 $implIncludes{"DOM$type.h"} = 1;
559 if ($type eq "DOMWindow") {
560 $implIncludes{"DOMAbstractView.h"} = 1;
561 $implIncludes{"$type.h"} = 1;
565 if ($type eq "DOMImplementation") {
566 $implIncludes{"DOMImplementationFront.h"} = 1;
567 $implIncludes{"DOM$type.h"} = 1;
571 if ($type eq "EventTarget") {
572 $implIncludes{"EventTargetNode.h"} = 1;
573 $implIncludes{"DOM$type.h"} = 1;
577 if ($codeGenerator->IsSVGAnimatedType($type)) {
578 $implIncludes{"SVGAnimatedTemplate.h"} = 1;
579 $implIncludes{"DOM$type.h"} = 1;
583 if ($type eq "SVGRect") {
584 $implIncludes{"FloatRect.h"} = 1;
585 $implIncludes{"DOM$type.h"} = 1;
589 if ($type eq "SVGPoint") {
590 $implIncludes{"FloatPoint.h"} = 1;
591 $implIncludes{"DOM$type.h"} = 1;
595 if ($type eq "SVGMatrix") {
596 $implIncludes{"AffineTransform.h"} = 1;
597 $implIncludes{"DOM$type.h"} = 1;
598 $implIncludes{"SVGException.h"} = 1;
602 if ($type eq "SVGNumber") {
603 $implIncludes{"DOM$type.h"} = 1;
607 if ($type =~ /(\w+)(Abs|Rel)$/) {
608 $implIncludes{"$1.h"} = 1;
609 $implIncludes{"DOM$type.h"} = 1;
613 if ($type eq "XPathNSResolver") {
614 $implIncludes{"DOMCustomXPathNSResolver.h"} = 1;
617 # FIXME: won't compile without these
618 $implIncludes{"CSSMutableStyleDeclaration.h"} = 1 if $type eq "CSSStyleDeclaration";
619 $implIncludes{"NamedAttrMap.h"} = 1 if $type eq "NamedNodeMap";
620 $implIncludes{"NameNodeList.h"} = 1 if $type eq "NodeList";
622 # Default, include the same named file (the implementation) and the same name prefixed with "DOM".
623 $implIncludes{"$type.h"} = 1;
624 $implIncludes{"DOM$type.h"} = 1;
630 my $dataNode = shift;
632 # We only support multiple parents with SVG (for now).
633 if (@{$dataNode->parents} > 1) {
634 die "A class can't have more than one parent" unless $module eq "svg";
637 my $interfaceName = $dataNode->name;
638 my $className = GetClassName($interfaceName);
641 my @protocolsToImplement = ();
642 ($parentName, @protocolsToImplement) = GetParentAndProtocols($dataNode);
644 my $numConstants = @{$dataNode->constants};
645 my $numAttributes = @{$dataNode->attributes};
646 my $numFunctions = @{$dataNode->functions};
648 # - Add default header template
649 @headerContentHeader = split("\r", $headerLicenceTemplate);
650 push(@headerContentHeader, "\n");
653 unless ($isProtocol) {
654 my $parentHeaderName = GetClassHeaderName($parentName);
655 push(@headerContentHeader, "#import <WebCore/$parentHeaderName.h>\n");
657 foreach my $parentProtocol (@protocolsToImplement) {
658 next if $parentProtocol =~ /^NS/;
659 $parentProtocol = GetClassHeaderName($parentProtocol);
660 push(@headerContentHeader, "#import <WebCore/$parentProtocol.h>\n");
663 # Special case needed for legacy support of DOMRange
664 if ($interfaceName eq "Range") {
665 push(@headerContentHeader, "#import <WebCore/DOMCore.h>\n");
666 push(@headerContentHeader, "#import <WebCore/DOMDocument.h>\n");
667 push(@headerContentHeader, "#import <WebCore/DOMRangeException.h>\n");
670 push(@headerContentHeader, "\n");
673 if ($numConstants > 0) {
674 my @headerConstants = ();
676 # FIXME: we need a way to include multiple enums.
677 foreach my $constant (@{$dataNode->constants}) {
678 my $constantName = $constant->name;
679 my $constantValue = $constant->value;
681 my $output = " DOM_" . $constantName . " = " . $constantValue;
682 push(@headerConstants, $output);
685 my $combinedConstants = join(",\n", @headerConstants);
687 # FIXME: the formatting of the enums should line up the equal signs.
688 # FIXME: enums are unconditionally placed in the public header.
689 push(@headerContent, "enum {\n");
690 push(@headerContent, $combinedConstants);
691 push(@headerContent, "\n};\n\n");
694 # - Begin @interface or @protocol
696 my $parentProtocols = join(", ", @protocolsToImplement);
697 push(@headerContent, "\@protocol $className <$parentProtocols>\n");
699 if (@protocolsToImplement eq 0) {
700 push(@headerContent, "\@interface $className : $parentName\n");
702 my $parentProtocols = join(", ", @protocolsToImplement);
703 push(@headerContent, "\@interface $className : $parentName <$parentProtocols>\n");
707 my @headerAttributes = ();
708 my @privateHeaderAttributes = ();
710 # - Add attribute getters/setters.
711 if ($numAttributes > 0) {
712 # Add ivars, if any, first
714 foreach my $attribute (@{$dataNode->attributes}) {
715 push(@ivars, $attribute) if $attribute->signature->extendedAttributes->{"ObjCIvar"};
719 push(@headerContent, "{\n");
720 foreach my $attribute (@ivars) {
721 my $type = GetObjCType($attribute->signature->type);;
722 my $name = "m_" . $attribute->signature->name;
723 my $ivarDeclaration = "$type $name";
724 push(@headerContent, " $ivarDeclaration;\n");
726 push(@headerContent, "}\n");
729 foreach my $attribute (@{$dataNode->attributes}) {
730 my $attributeName = $attribute->signature->name;
732 if ($attributeName eq "id" or $attributeName eq "hash") {
733 # Special case attributes id and hash to be idName and hashName to avoid ObjC naming conflict.
734 $attributeName .= "Name";
735 } elsif ($attributeName eq "frame") {
736 # Special case attribute frame to be frameBorders.
737 $attributeName .= "Borders";
740 my $attributeType = GetObjCType($attribute->signature->type);
741 my $attributeIsReadonly = ($attribute->type =~ /^readonly/);
743 my $property = "\@property" . GetPropertyAttributes($attribute->signature->type, $attributeIsReadonly);
744 $property .= " " . $attributeType . ($attributeType =~ /\*$/ ? "" : " ") . $attributeName . ";";
746 my $public = ($publicInterfaces{$property} or $newPublicClass);
747 delete $publicInterfaces{$property};
749 AddForwardDeclarationsForType($attribute->signature->type, $public);
751 my $setterName = "set" . ucfirst($attributeName) . ":";
753 my $conflict = $conflictMethod{$attributeName};
755 warn "$className conflicts with $conflict method $attributeName\n";
759 $conflict = $conflictMethod{$setterName};
761 warn "$className conflicts with $conflict method $setterName\n";
765 if ($buildingForLeopardOrLater) {
767 push(@headerAttributes, $property) if $public;
768 push(@privateHeaderAttributes, $property) unless $public;
771 my $getter = "- (" . $attributeType . ")" . $attributeName . ";\n";
772 push(@headerAttributes, $getter) if $public;
773 push(@privateHeaderAttributes, $getter) unless $public;
776 if (!$attributeIsReadonly) {
777 my $setter = "- (void)$setterName(" . $attributeType . ")new" . ucfirst($attributeName) . ";\n";
778 push(@headerAttributes, $setter) if $public;
779 push(@privateHeaderAttributes, $setter) unless $public;
784 push(@headerContent, @headerAttributes) if @headerAttributes > 0;
787 my @headerFunctions = ();
788 my @privateHeaderFunctions = ();
789 my @deprecatedHeaderFunctions = ();
792 if ($numFunctions > 0) {
793 foreach my $function (@{$dataNode->functions}) {
794 my $functionName = $function->signature->name;
796 my $returnType = GetObjCType($function->signature->type);
797 my $needsDeprecatedVersion = (@{$function->parameters} > 1 and $function->signature->extendedAttributes->{"OldStyleObjC"});
798 my $numberOfParameters = @{$function->parameters};
799 my %typesToForwardDeclare = ($function->signature->type => 1);
801 my $parameterIndex = 0;
802 my $functionSig = "- ($returnType)$functionName";
803 my $methodName = $functionName;
804 foreach my $param (@{$function->parameters}) {
805 my $paramName = $param->name;
806 my $paramType = GetObjCType($param->type);
808 $typesToForwardDeclare{$param->type} = 1;
810 if ($parameterIndex >= 1) {
811 my $paramPrefix = $param->extendedAttributes->{"ObjCPrefix"};
812 $paramPrefix = $paramName unless defined($paramPrefix);
813 $functionSig .= " $paramPrefix";
814 $methodName .= $paramPrefix;
817 $functionSig .= ":($paramType)$paramName";
825 my $conflict = $conflictMethod{$methodName};
827 warn "$className conflicts with $conflict method $methodName\n";
831 if ($isProtocol && !$newPublicClass && !defined $publicInterfaces{$functionSig}) {
832 warn "Protocol method $functionSig is not in PublicDOMInterfaces.h. Protocols require all methods to be public";
836 my $public = ($publicInterfaces{$functionSig} or $newPublicClass);
837 delete $publicInterfaces{$functionSig};
839 $functionSig .= "\n";
841 foreach my $type (keys %typesToForwardDeclare) {
842 # add any forward declarations to the public header if a deprecated version will be generated
843 AddForwardDeclarationsForType($type, 1) if $needsDeprecatedVersion;
844 AddForwardDeclarationsForType($type, $public) unless $public and $needsDeprecatedVersion;
847 push(@headerFunctions, $functionSig) if $public;
848 push(@privateHeaderFunctions, $functionSig) unless $public;
850 # generate the old style method names with un-named parameters, these methods are deprecated
851 if ($needsDeprecatedVersion) {
852 my $deprecatedFunctionSig = $functionSig;
853 $deprecatedFunctionSig =~ s/\s\w+:/ :/g; # remove parameter names
854 my $deprecatedFunctionKey = $deprecatedFunctionSig;
856 $deprecatedFunctionSig =~ s/;\n$/ DEPRECATED_IN_MAC_OS_X_VERSION_10_5_AND_LATER;\n/ if $buildingForLeopardOrLater;
857 push(@deprecatedHeaderFunctions, $deprecatedFunctionSig);
859 $deprecatedFunctionKey =~ s/\n$//; # remove the newline
861 unless (defined $publicInterfaces{$deprecatedFunctionKey}) {
862 warn "Deprecated method $deprecatedFunctionKey is not in PublicDOMInterfaces.h. All deprecated methods need to be public, or should have the OldStyleObjC IDL attribute removed";
866 delete $publicInterfaces{$deprecatedFunctionKey};
870 if (@headerFunctions > 0) {
871 push(@headerContent, "\n") if $buildingForLeopardOrLater and @headerAttributes > 0;
872 push(@headerContent, @headerFunctions);
876 if (@deprecatedHeaderFunctions > 0 && $isProtocol) {
877 push(@headerContent, @deprecatedHeaderFunctions);
880 # - End @interface or @protocol
881 push(@headerContent, "\@end\n");
883 if (@deprecatedHeaderFunctions > 0 && !$isProtocol) {
884 # - Deprecated category @interface
885 push(@headerContent, "\n\@interface $className (" . $className . "Deprecated)\n");
886 push(@headerContent, @deprecatedHeaderFunctions);
887 push(@headerContent, "\@end\n");
890 if (@privateHeaderAttributes > 0 or @privateHeaderFunctions > 0) {
891 # - Private category @interface
892 @privateHeaderContentHeader = split("\r", $headerLicenceTemplate);
893 push(@headerContentHeader, "\n");
895 my $classHeaderName = GetClassHeaderName($className);
896 push(@privateHeaderContentHeader, "#import <WebCore/$classHeaderName.h>\n\n");
898 @privateHeaderContent = ();
899 push(@privateHeaderContent, "\@interface $className (" . $className . "Private)\n");
900 push(@privateHeaderContent, @privateHeaderAttributes) if @privateHeaderAttributes > 0;
901 push(@privateHeaderContent, "\n") if $buildingForLeopardOrLater and @privateHeaderAttributes > 0 and @privateHeaderFunctions > 0;
902 push(@privateHeaderContent, @privateHeaderFunctions) if @privateHeaderFunctions > 0;
903 push(@privateHeaderContent, "\@end\n");
907 sub GenerateImplementation
910 my $dataNode = shift;
912 # We only support multiple parents with SVG (for now).
913 if (@{$dataNode->parents} > 1) {
914 die "A class can't have more than one parent" unless $module eq "svg";
915 $codeGenerator->AddMethodsConstantsAndAttributesFromParentClasses($dataNode);
918 my $interfaceName = $dataNode->name;
919 my $className = GetClassName($interfaceName);
920 my $implClassName = GetImplClassName($interfaceName);
921 my $parentImplClassName = GetParentImplClassName($dataNode);
922 my $implClassNameWithNamespace = "WebCore::" . $implClassName;
923 my $baseClass = GetBaseClass($parentImplClassName);
924 my $classHeaderName = GetClassHeaderName($className);
925 my $conditional = $dataNode->extendedAttributes->{"Conditional"};
927 my $numAttributes = @{$dataNode->attributes};
928 my $numFunctions = @{$dataNode->functions};
930 my $podType = $dataNode->extendedAttributes->{"PODType"};
931 my $podTypeWithNamespace;
934 $podTypeWithNamespace = ($podType eq "float") ? "$podType" : "WebCore::$podType";
937 # - Add default header template.
938 @implContentHeader = split("\r", $implementationLicenceTemplate);
941 push(@implContentHeader, "\n#import \"config.h\"\n");
943 my $conditionalString;
945 $conditionalString = "ENABLE(" . join(") && ENABLE(", split(/&/, $conditional)) . ")";
946 push(@implContentHeader, "\n#if ${conditionalString}\n");
949 push(@implContentHeader, "\n#import \"$classHeaderName.h\"\n\n");
951 push(@implContentHeader, "#import \"ThreadCheck.h\"\n");
952 push(@implContentHeader, "#import <wtf/GetPtr.h>\n\n");
954 if ($codeGenerator->IsSVGAnimatedType($interfaceName)) {
955 $implIncludes{"SVGAnimatedTemplate.h"} = 1;
956 } elsif ($interfaceName =~ /(\w+)(Abs|Rel)$/) {
957 $implIncludes{"$1.h"} = 1;
960 $implIncludes{"$implClassName.h"} = 1;
962 $implIncludes{"$podType.h"} = 1 unless $podType eq "float";
966 $implIncludes{"DOMInternal.h"} = 1;
967 $implIncludes{"ExceptionHandlers.h"} = 1;
971 # add implementation accessor
973 push(@implContent, "#define IMPL reinterpret_cast<$podTypeWithNamespace*>(_internal)\n\n");
974 } elsif ($parentImplClassName eq "Object") {
975 push(@implContent, "#define IMPL reinterpret_cast<$implClassNameWithNamespace*>(_internal)\n\n");
977 my $baseClassWithNamespace = "WebCore::$baseClass";
978 push(@implContent, "#define IMPL static_cast<$implClassNameWithNamespace*>(reinterpret_cast<$baseClassWithNamespace*>(_internal))\n\n");
981 # START implementation
982 push(@implContent, "\@implementation $className\n\n");
984 # Only generate 'dealloc' and 'finalize' methods for direct subclasses of DOMObject.
985 if ($parentImplClassName eq "Object") {
986 my @ivarsToRelease = ();
988 foreach $attribute (@ivars) {
989 my $name = "m_" . $attribute->signature->name;
990 push(@ivarsToRelease, " [$name release];\n");
994 push(@implContent, "- (void)dealloc\n");
995 push(@implContent, "{\n");
996 push(@implContent, " $assertMainThread\n");
997 push(@implContent, @ivarsToRelease);
998 if ($interfaceName eq "NodeIterator") {
999 push(@implContent, " if (_internal) {\n");
1000 push(@implContent, " [self detach];\n");
1001 push(@implContent, " IMPL->deref();\n");
1002 push(@implContent, " };\n");
1003 } elsif ($podType) {
1004 push(@implContent, " delete IMPL;\n");
1006 push(@implContent, " if (_internal)\n");
1007 push(@implContent, " IMPL->deref();\n");
1009 push(@implContent, " [super dealloc];\n");
1010 push(@implContent, "}\n\n");
1012 push(@implContent, "- (void)finalize\n");
1013 push(@implContent, "{\n");
1014 if ($interfaceName eq "NodeIterator") {
1015 push(@implContent, " if (_internal) {\n");
1016 push(@implContent, " [self detach];\n");
1017 push(@implContent, " IMPL->deref();\n");
1018 push(@implContent, " };\n");
1019 } elsif ($podType) {
1020 push(@implContent, " delete IMPL;\n");
1022 push(@implContent, " if (_internal)\n");
1023 push(@implContent, " IMPL->deref();\n");
1025 push(@implContent, " [super finalize];\n");
1026 push(@implContent, "}\n\n");
1030 %attributeNames = ();
1033 if ($numAttributes > 0) {
1034 foreach my $attribute (@{$dataNode->attributes}) {
1035 AddIncludesForType($attribute->signature->type);
1037 my $idlType = $codeGenerator->StripModule($attribute->signature->type);
1039 my $attributeName = $attribute->signature->name;
1040 my $attributeType = GetObjCType($attribute->signature->type);
1041 my $attributeIsReadonly = ($attribute->type =~ /^readonly/);
1042 my $attributeClassName = GetClassName($attribute->signature->type);
1044 my $attributeInterfaceName = $attributeName;
1045 if ($attributeName eq "id" or $attributeName eq "hash") {
1046 # Special case attributes id and hash to be idName and hashName to avoid ObjC naming conflict.
1047 $attributeInterfaceName .= "Name";
1048 } elsif ($attributeName eq "frame") {
1049 # Special case attribute frame to be frameBorders.
1050 $attributeInterfaceName .= "Borders";
1051 } elsif ($attributeName eq "ownerDocument") {
1052 # FIXME: for now special case attribute ownerDocument to call document, this is incorrect
1053 # legacy behavior. (see http://bugs.webkit.org/show_bug.cgi?id=10889)
1054 $attributeName = "document";
1055 } elsif ($codeGenerator->IsSVGAnimatedType($idlType)) {
1056 # Special case for animated types.
1057 $attributeName .= "Animated";
1060 $attributeNames{$attributeInterfaceName} = 1;
1063 my $getterSig = "- ($attributeType)$attributeInterfaceName\n";
1064 my $hasGetterException = @{$attribute->getterExceptions};
1065 my $getterContentHead = "IMPL->$attributeName(";
1066 my $getterContentTail = ")";
1068 # Special case for DOMSVGNumber
1069 if ($podType and $podType eq "float") {
1070 $getterContentHead = "*IMPL";
1071 $getterContentTail = "";
1074 my $attributeTypeSansPtr = $attributeType;
1075 $attributeTypeSansPtr =~ s/ \*$//; # Remove trailing " *" from pointer types.
1077 # special case for EventTarget protocol
1078 $attributeTypeSansPtr = "DOMNode" if $idlType eq "EventTarget";
1080 my $typeMaker = GetObjCTypeMaker($attribute->signature->type);
1083 my @customGetterContent = ();
1084 if ($attributeTypeSansPtr eq "DOMImplementation") {
1085 # FIXME: We have to special case DOMImplementation until DOMImplementationFront is removed
1086 $getterContentHead = "[$attributeTypeSansPtr $typeMaker:implementationFront(IMPL";
1087 $getterContentTail .= "]";
1088 } elsif ($attributeName =~ /(\w+)DisplayString$/) {
1089 my $attributeToDisplay = $1;
1090 $getterContentHead = "WebCore::displayString(IMPL->$attributeToDisplay(), [self _element]";
1091 } elsif ($attributeName =~ /^absolute(\w+)URL$/) {
1093 $getterContentHead = "[self _getURLAttribute:";
1094 if ($typeOfURL eq "Link") {
1095 $getterContentTail = "\@\"href\"]";
1096 } elsif ($typeOfURL eq "Image") {
1097 if ($interfaceName eq "HTMLObjectElement") {
1098 $getterContentTail = "\@\"data\"]";
1100 $getterContentTail = "\@\"src\"]";
1102 unless ($interfaceName eq "HTMLImageElement") {
1103 push(@customGetterContent, " if (!IMPL->renderer() || !IMPL->renderer()->isImage())\n");
1104 push(@customGetterContent, " return nil;\n");
1105 $implIncludes{"RenderObject.h"} = 1;
1108 $implIncludes{"DOMPrivate.h"} = 1;
1109 } elsif ($idlType eq "NodeFilter") {
1110 push(@customGetterContent, " if (m_filter)\n");
1111 push(@customGetterContent, " // This node iterator was created from the Objective-C side.\n");
1112 push(@customGetterContent, " return [[m_filter retain] autorelease];\n\n");
1113 push(@customGetterContent, " // This node iterator was created from the C++ side.\n");
1114 $getterContentHead = "[$attributeClassName $typeMaker:WTF::getPtr(" . $getterContentHead;
1115 $getterContentTail .= ")]";
1116 } elsif ($attribute->signature->extendedAttributes->{"ConvertToString"}) {
1117 $getterContentHead = "WebCore::String::number(" . $getterContentHead;
1118 $getterContentTail .= ")";
1119 } elsif ($attribute->signature->extendedAttributes->{"ConvertFromString"}) {
1120 $getterContentTail .= ".toInt()";
1121 } elsif ($codeGenerator->IsPodType($idlType)) {
1122 $getterContentHead = "[$attributeTypeSansPtr $typeMaker:" . $getterContentHead;
1123 $getterContentTail .= "]";
1124 } elsif ($typeMaker ne "") {
1125 # Surround getter with TypeMaker
1126 $getterContentHead = "[$attributeTypeSansPtr $typeMaker:WTF::getPtr(" . $getterContentHead;
1127 $getterContentTail .= ")]";
1131 if ($hasGetterException) {
1132 $getterContent = $getterContentHead . "ec" . $getterContentTail;
1134 $getterContent = $getterContentHead . $getterContentTail;
1137 push(@implContent, $getterSig);
1138 push(@implContent, "{\n");
1139 push(@implContent, @customGetterContent);
1140 if ($hasGetterException) {
1141 # Differentiated between when the return type is a pointer and
1142 # not for white space issue (ie. Foo *result vs. int result).
1143 if ($attributeType =~ /\*$/) {
1144 $getterContent = $attributeType . "result = " . $getterContent;
1146 $getterContent = $attributeType . " result = " . $getterContent;
1149 push(@implContent, " $exceptionInit\n");
1150 push(@implContent, " $getterContent;\n");
1151 push(@implContent, " $exceptionRaiseOnError\n");
1152 push(@implContent, " return result;\n");
1154 push(@implContent, " return $getterContent;\n");
1156 push(@implContent, "}\n\n");
1159 if (!$attributeIsReadonly) {
1160 # Exception handling
1161 my $hasSetterException = @{$attribute->setterExceptions};
1163 $attributeName = "set" . WK_ucfirst($attributeName);
1164 my $setterName = "set" . ucfirst($attributeInterfaceName);
1165 my $argName = "new" . ucfirst($attributeInterfaceName);
1166 my $arg = GetObjCTypeGetter($argName, $idlType);
1168 # The definition of ConvertFromString and ConvertToString is flipped for the setter
1169 if ($attribute->signature->extendedAttributes->{"ConvertFromString"}) {
1170 $arg = "WebCore::String::number($arg)";
1171 } elsif ($attribute->signature->extendedAttributes->{"ConvertToString"}) {
1172 $arg = "WebCore::String($arg).toInt()";
1175 my $setterSig = "- (void)$setterName:($attributeType)$argName\n";
1177 push(@implContent, $setterSig);
1178 push(@implContent, "{\n");
1180 unless ($codeGenerator->IsPrimitiveType($idlType) or $codeGenerator->IsStringType($idlType)) {
1181 push(@implContent, " ASSERT($argName);\n\n");
1185 # Special case for DOMSVGNumber
1186 if ($podType eq "float") {
1187 push(@implContent, " *IMPL = $arg;\n");
1189 push(@implContent, " IMPL->$attributeName($arg);\n");
1191 } elsif ($hasSetterException) {
1192 push(@implContent, " $exceptionInit\n");
1193 push(@implContent, " IMPL->$attributeName($arg, ec);\n");
1194 push(@implContent, " $exceptionRaiseOnError\n");
1196 push(@implContent, " IMPL->$attributeName($arg);\n");
1199 push(@implContent, "}\n\n");
1204 my @deprecatedFunctions = ();
1207 if ($numFunctions > 0) {
1208 foreach my $function (@{$dataNode->functions}) {
1209 AddIncludesForType($function->signature->type);
1211 my $functionName = $function->signature->name;
1212 my $returnType = GetObjCType($function->signature->type);
1213 my $hasParameters = @{$function->parameters};
1214 my $raisesExceptions = @{$function->raisesExceptions};
1216 my @parameterNames = ();
1217 my @needsAssert = ();
1218 my %needsCustom = ();
1220 my $parameterIndex = 0;
1221 my $functionSig = "- ($returnType)$functionName";
1222 foreach my $param (@{$function->parameters}) {
1223 my $paramName = $param->name;
1224 my $paramType = GetObjCType($param->type);
1226 # make a new parameter name if the original conflicts with a property name
1227 $paramName = "in" . ucfirst($paramName) if $attributeNames{$paramName};
1229 AddIncludesForType($param->type);
1231 my $idlType = $codeGenerator->StripModule($param->type);
1232 my $implGetter = GetObjCTypeGetter($paramName, $idlType);
1234 push(@parameterNames, $implGetter);
1235 $needsCustom{"XPathNSResolver"} = $paramName if $idlType eq "XPathNSResolver";
1236 $needsCustom{"EventTarget"} = $paramName if $idlType eq "EventTarget";
1237 $needsCustom{"NodeToReturn"} = $paramName if $param->extendedAttributes->{"Return"};
1239 unless ($codeGenerator->IsPrimitiveType($idlType) or $codeGenerator->IsStringType($idlType)) {
1240 push(@needsAssert, " ASSERT($paramName);\n");
1243 if ($parameterIndex >= 1) {
1244 my $paramPrefix = $param->extendedAttributes->{"ObjCPrefix"};
1245 $paramPrefix = $param->name unless defined($paramPrefix);
1246 $functionSig .= " $paramPrefix";
1249 $functionSig .= ":($paramType)$paramName";
1254 my @functionContent = ();
1255 my $caller = "IMPL";
1257 # special case the XPathNSResolver
1258 if (defined $needsCustom{"XPathNSResolver"}) {
1259 my $paramName = $needsCustom{"XPathNSResolver"};
1260 push(@functionContent, " WebCore::XPathNSResolver* nativeResolver = 0;\n");
1261 push(@functionContent, " RefPtr<WebCore::XPathNSResolver> customResolver;\n");
1262 push(@functionContent, " if ($paramName) {\n");
1263 push(@functionContent, " if ([$paramName isMemberOfClass:[DOMNativeXPathNSResolver class]])\n");
1264 push(@functionContent, " nativeResolver = [(DOMNativeXPathNSResolver *)$paramName _xpathNSResolver];\n");
1265 push(@functionContent, " else {\n");
1266 push(@functionContent, " customResolver = new WebCore::DOMCustomXPathNSResolver($paramName);\n");
1267 push(@functionContent, " nativeResolver = customResolver.get();\n");
1268 push(@functionContent, " }\n");
1269 push(@functionContent, " }\n");
1272 # special case the EventTarget
1273 if (defined $needsCustom{"EventTarget"}) {
1274 my $paramName = $needsCustom{"EventTarget"};
1275 push(@functionContent, " DOMNode* ${paramName}ObjC = $paramName;\n");
1276 push(@functionContent, " WebCore::Node* ${paramName}Node = [${paramName}ObjC _node];\n");
1277 push(@functionContent, " WebCore::EventTargetNode* ${paramName}EventTarget = (${paramName}Node && ${paramName}Node->isEventTargetNode()) ? static_cast<WebCore::EventTargetNode*>(${paramName}Node) : 0;\n\n");
1278 $implIncludes{"DOMNode.h"} = 1;
1279 $implIncludes{"Node.h"} = 1;
1282 if ($function->signature->extendedAttributes->{"UsesView"}) {
1283 push(@functionContent, " WebCore::DOMWindow* dv = $caller->defaultView();\n");
1284 push(@functionContent, " if (!dv)\n");
1285 push(@functionContent, " return nil;\n");
1286 $implIncludes{"DOMWindow.h"} = 1;
1290 # FIXME! We need [Custom] support for ObjC, to move these hacks into DOMSVGMatrixCustom.mm
1291 my $svgMatrixRotateFromVector = ($podType and $podType eq "AffineTransform" and $functionName eq "rotateFromVector");
1292 my $svgMatrixInverse = ($podType and $podType eq "AffineTransform" and $functionName eq "inverse");
1294 push(@parameterNames, "ec") if $raisesExceptions and !($svgMatrixRotateFromVector || $svgMatrixInverse);
1295 my $content = $caller . "->" . $functionName . "(" . join(", ", @parameterNames) . ")";
1297 if ($svgMatrixRotateFromVector) {
1298 # Special case with rotateFromVector & SVGMatrix
1299 push(@functionContent, " $exceptionInit\n");
1300 push(@functionContent, " if (x == 0.0 || y == 0.0)\n");
1301 push(@functionContent, " ec = WebCore::SVG_INVALID_VALUE_ERR;\n");
1302 push(@functionContent, " $exceptionRaiseOnError\n");
1303 push(@functionContent, " return [DOMSVGMatrix _wrapSVGMatrix:$content];\n");
1304 } elsif ($svgMatrixInverse) {
1305 # Special case with inverse & SVGMatrix
1306 push(@functionContent, " $exceptionInit\n");
1307 push(@functionContent, " if (!$caller->isInvertible())\n");
1308 push(@functionContent, " ec = WebCore::SVG_MATRIX_NOT_INVERTABLE;\n");
1309 push(@functionContent, " $exceptionRaiseOnError\n");
1310 push(@functionContent, " return [DOMSVGMatrix _wrapSVGMatrix:$content];\n");
1311 } elsif ($returnType eq "void") {
1312 # Special case 'void' return type.
1313 if ($raisesExceptions) {
1314 push(@functionContent, " $exceptionInit\n");
1315 push(@functionContent, " $content;\n");
1316 push(@functionContent, " $exceptionRaiseOnError\n");
1318 push(@functionContent, " $content;\n");
1320 } elsif (defined $needsCustom{"NodeToReturn"}) {
1321 # Special case the insertBefore, replaceChild, removeChild
1322 # and appendChild functions from DOMNode
1323 my $toReturn = $needsCustom{"NodeToReturn"};
1324 if ($raisesExceptions) {
1325 push(@functionContent, " $exceptionInit\n");
1326 push(@functionContent, " if ($content)\n");
1327 push(@functionContent, " return $toReturn;\n");
1328 push(@functionContent, " $exceptionRaiseOnError\n");
1329 push(@functionContent, " return nil;\n");
1331 push(@functionContent, " if ($content)\n");
1332 push(@functionContent, " return $toReturn;\n");
1333 push(@functionContent, " return nil;\n");
1336 my $typeMaker = GetObjCTypeMaker($function->signature->type);
1337 unless ($typeMaker eq "") {
1338 my $returnTypeClass = "";
1339 if ($function->signature->type eq "XPathNSResolver") {
1340 # Special case XPathNSResolver
1341 $returnTypeClass = "DOMNativeXPathNSResolver";
1343 # Remove trailing " *" from pointer types.
1344 $returnTypeClass = $returnType;
1345 $returnTypeClass =~ s/ \*$//;
1348 # Surround getter with TypeMaker
1349 my $idlType = $returnTypeClass;
1350 $idlType =~ s/^DOM//;
1352 if ($codeGenerator->IsPodType($idlType)) {
1353 $content = "[$returnTypeClass $typeMaker:" . $content . "]";
1355 $content = "[$returnTypeClass $typeMaker:WTF::getPtr(" . $content . ")]";
1359 if ($raisesExceptions) {
1360 # Differentiated between when the return type is a pointer and
1361 # not for white space issue (ie. Foo *result vs. int result).
1362 if ($returnType =~ /\*$/) {
1363 $content = $returnType . "result = " . $content;
1365 $content = $returnType . " result = " . $content;
1368 push(@functionContent, " $exceptionInit\n");
1369 push(@functionContent, " $content;\n");
1370 push(@functionContent, " $exceptionRaiseOnError\n");
1371 push(@functionContent, " return result;\n");
1373 push(@functionContent, " return $content;\n");
1377 push(@implContent, "$functionSig\n");
1378 push(@implContent, "{\n");
1379 push(@implContent, @functionContent);
1380 push(@implContent, "}\n\n");
1382 # generate the old style method names with un-named parameters, these methods are deprecated
1383 if (@{$function->parameters} > 1 and $function->signature->extendedAttributes->{"OldStyleObjC"}) {
1384 my $deprecatedFunctionSig = $functionSig;
1385 $deprecatedFunctionSig =~ s/\s\w+:/ :/g; # remove parameter names
1387 push(@deprecatedFunctions, "$deprecatedFunctionSig\n");
1388 push(@deprecatedFunctions, "{\n");
1389 push(@deprecatedFunctions, @functionContent);
1390 push(@deprecatedFunctions, "}\n\n");
1398 # END implementation
1399 push(@implContent, "\@end\n");
1401 if (@deprecatedFunctions > 0) {
1402 # - Deprecated category @implementation
1403 push(@implContent, "\n\@implementation $className (" . $className . "Deprecated)\n\n");
1404 push(@implContent, @deprecatedFunctions);
1405 push(@implContent, "\@end\n");
1409 # Generate internal interfaces
1412 # - (WebCore::FooBar *)_fooBar for implementation class FooBar
1413 my $typeGetterName = GetObjCTypeGetterName($interfaceName);
1414 my $typeGetterSig = "- " . ($podType ? "($podTypeWithNamespace)" : "($implClassNameWithNamespace *)") . $typeGetterName;
1416 my @ivarsToRetain = ();
1417 my $ivarsToInit = "";
1418 my $typeMakerSigAddition = "";
1420 my @ivarsInitSig = ();
1421 my @ivarsInitCall = ();
1422 foreach $attribute (@ivars) {
1423 my $name = $attribute->signature->name;
1424 my $memberName = "m_" . $name;
1425 my $varName = "in" . $name;
1426 my $type = GetObjCType($attribute->signature->type);
1427 push(@ivarsInitSig, "$name:($type)$varName");
1428 push(@ivarsInitCall, "$name:$varName");
1429 push(@ivarsToRetain, " $memberName = [$varName retain];\n");
1431 $ivarsToInit = " " . join(" ", @ivarsInitCall);
1432 $typeMakerSigAddition = " " . join(" ", @ivarsInitSig);
1436 my $typeMakerName = GetObjCTypeMaker($interfaceName);
1437 my $typeMakerSig = "+ ($className *)$typeMakerName:($implClassNameWithNamespace *)impl" . $typeMakerSigAddition;
1438 $typeMakerSig = "+ ($className *)$typeMakerName:($podTypeWithNamespace)impl" . $typeMakerSigAddition if $podType;
1440 # Generate interface definitions.
1441 @internalHeaderContent = split("\r", $implementationLicenceTemplate);
1442 push(@internalHeaderContent, "\n#import <WebCore/$className.h>\n");
1443 if ($interfaceName eq "Node") {
1444 push(@internalHeaderContent, "\n\@protocol DOMEventTarget;\n");
1446 if ($codeGenerator->IsSVGAnimatedType($interfaceName)) {
1447 push(@internalHeaderContent, "#import <WebCore/SVGAnimatedTemplate.h>\n\n");
1449 if ($podType and $podType ne "float") {
1450 push(@internalHeaderContent, "\nnamespace WebCore { class $podType; }\n\n");
1451 } elsif ($interfaceName eq "Node") {
1452 push(@internalHeaderContent, "\nnamespace WebCore { class Node; class EventTarget; }\n\n");
1454 push(@internalHeaderContent, "\nnamespace WebCore { class $implClassName; }\n\n");
1458 push(@internalHeaderContent, "\@interface $className (WebCoreInternal)\n");
1459 push(@internalHeaderContent, $typeGetterSig . ";\n");
1460 push(@internalHeaderContent, $typeMakerSig . ";\n");
1461 if ($interfaceName eq "Node") {
1462 push(@internalHeaderContent, "+ (id <DOMEventTarget>)_wrapEventTarget:(WebCore::EventTarget *)eventTarget;\n");
1464 push(@internalHeaderContent, "\@end\n");
1466 unless ($dataNode->extendedAttributes->{ObjCCustomInternalImpl}) {
1467 # - BEGIN WebCoreInternal category @implementation
1468 push(@implContent, "\n\@implementation $className (WebCoreInternal)\n\n");
1470 push(@implContent, "$typeGetterSig\n");
1471 push(@implContent, "{\n");
1474 push(@implContent, " return *IMPL;\n");
1476 push(@implContent, " return IMPL;\n");
1479 push(@implContent, "}\n\n");
1482 # - (id)_initWithFooBar:(WebCore::FooBar)impl for implementation class FooBar
1483 my $initWithImplName = "_initWith" . $implClassName;
1484 my $initWithSig = "- (id)$initWithImplName:($podTypeWithNamespace)impl" . $typeMakerSigAddition;
1486 # FIXME: Implement Caching
1487 push(@implContent, "$initWithSig\n");
1488 push(@implContent, "{\n");
1489 push(@implContent, " $assertMainThread;\n");
1490 push(@implContent, " [super _init];\n");
1491 push(@implContent, " $podTypeWithNamespace* _impl = new $podTypeWithNamespace(impl);\n");
1492 push(@implContent, " _internal = reinterpret_cast<DOMObjectInternal*>(_impl);\n");
1493 push(@implContent, " return self;\n");
1494 push(@implContent, "}\n\n");
1496 # - (DOMFooBar)_wrapFooBar:(WebCore::FooBar)impl for implementation class FooBar
1497 push(@implContent, "$typeMakerSig\n");
1498 push(@implContent, "{\n");
1499 push(@implContent, " $assertMainThread;\n");
1500 push(@implContent, " return [[[self alloc] $initWithImplName:impl] autorelease];\n");
1501 push(@implContent, "}\n\n");
1502 } elsif ($parentImplClassName eq "Object") {
1503 # - (id)_initWithFooBar:(WebCore::FooBar *)impl for implementation class FooBar
1504 my $initWithImplName = "_initWith" . $implClassName;
1505 my $initWithSig = "- (id)$initWithImplName:($implClassNameWithNamespace *)impl" . $typeMakerSigAddition;
1507 push(@implContent, "$initWithSig\n");
1508 push(@implContent, "{\n");
1509 push(@implContent, " $assertMainThread;\n");
1510 push(@implContent, " [super _init];\n");
1511 push(@implContent, " _internal = reinterpret_cast<DOMObjectInternal*>(impl);\n");
1512 push(@implContent, " impl->ref();\n");
1513 push(@implContent, " WebCore::addDOMWrapper(self, impl);\n");
1514 push(@implContent, @ivarsToRetain);
1515 push(@implContent, " return self;\n");
1516 push(@implContent, "}\n\n");
1518 # - (DOMFooBar)_wrapFooBar:(WebCore::FooBar *)impl for implementation class FooBar
1519 push(@implContent, "$typeMakerSig\n");
1520 push(@implContent, "{\n");
1521 push(@implContent, " $assertMainThread;\n");
1522 push(@implContent, " if (!impl)\n");
1523 push(@implContent, " return nil;\n");
1524 push(@implContent, " id cachedInstance;\n");
1525 push(@implContent, " cachedInstance = WebCore::getDOMWrapper(impl);\n");
1526 push(@implContent, " if (cachedInstance)\n");
1527 push(@implContent, " return [[cachedInstance retain] autorelease];\n");
1528 push(@implContent, " return [[[self alloc] $initWithImplName:impl" . $ivarsToInit . "] autorelease];\n");
1529 push(@implContent, "}\n\n");
1531 my $internalBaseType = "DOM$baseClass";
1532 my $internalBaseTypeMaker = GetObjCTypeMaker($baseClass);
1534 # - (DOMFooBar)_wrapFooBar:(WebCore::FooBar *)impl for implementation class FooBar
1535 push(@implContent, "$typeMakerSig\n");
1536 push(@implContent, "{\n");
1537 push(@implContent, " $assertMainThread;\n");
1538 push(@implContent, " return static_cast<$className*>([$internalBaseType $internalBaseTypeMaker:impl]);\n");
1539 push(@implContent, "}\n\n");
1542 # END WebCoreInternal category
1543 push(@implContent, "\@end\n");
1546 # - End the ifdef conditional if necessary
1547 push(@implContent, "\n#endif // ${conditionalString}\n") if $conditional;
1556 # Open files for writing...
1557 my $headerFileName = "$outputDir/" . $name . ".h";
1558 my $privateHeaderFileName = "$outputDir/" . $name . "Private.h";
1559 my $implFileName = "$outputDir/" . $name . ".mm";
1560 my $internalHeaderFileName = "$outputDir/" . $name . "Internal.h";
1563 unlink($headerFileName);
1564 unlink($privateHeaderFileName);
1565 unlink($implFileName);
1566 unlink($internalHeaderFileName);
1568 # Write public header.
1569 open(HEADER, ">$headerFileName") or die "Couldn't open file $headerFileName";
1571 print HEADER @headerContentHeader;
1572 print HEADER map { "\@class $_;\n" } sort keys(%headerForwardDeclarations);
1573 print HEADER map { "\@protocol $_;\n" } sort keys(%headerForwardDeclarationsForProtocols);
1575 my $hasForwardDeclarations = keys(%headerForwardDeclarations) + keys(%headerForwardDeclarationsForProtocols);
1576 print HEADER "\n" if $hasForwardDeclarations;
1577 print HEADER @headerContent;
1581 @headerContentHeader = ();
1582 @headerContent = ();
1583 %headerForwardDeclarations = ();
1584 %headerForwardDeclarationsForProtocols = ();
1586 if (@privateHeaderContent > 0) {
1587 open(PRIVATE_HEADER, ">$privateHeaderFileName") or die "Couldn't open file $privateHeaderFileName";
1589 print PRIVATE_HEADER @privateHeaderContentHeader;
1590 print PRIVATE_HEADER map { "\@class $_;\n" } sort keys(%privateHeaderForwardDeclarations);
1591 print PRIVATE_HEADER map { "\@protocol $_;\n" } sort keys(%privateHeaderForwardDeclarationsForProtocols);
1593 $hasForwardDeclarations = keys(%privateHeaderForwardDeclarations) + keys(%privateHeaderForwardDeclarationsForProtocols);
1594 print PRIVATE_HEADER "\n" if $hasForwardDeclarations;
1595 print PRIVATE_HEADER @privateHeaderContent;
1597 close(PRIVATE_HEADER);
1599 @privateHeaderContentHeader = ();
1600 @privateHeaderContent = ();
1601 %privateHeaderForwardDeclarations = ();
1602 %privateHeaderForwardDeclarationsForProtocols = ();
1605 # Write implementation file.
1607 open(IMPL, ">$implFileName") or die "Couldn't open file $implFileName";
1609 print IMPL @implContentHeader;
1610 print IMPL map { "#import \"$_\"\n" } sort keys(%implIncludes);
1612 print IMPL "\n" if keys(%implIncludes);
1613 print IMPL @implContent;
1617 @implContentHeader = ();
1622 if (@internalHeaderContent > 0) {
1623 open(INTERNAL_HEADER, ">$internalHeaderFileName") or die "Couldn't open file $internalHeaderFileName";
1625 print INTERNAL_HEADER @internalHeaderContent;
1627 close(INTERNAL_HEADER);
1629 @internalHeaderContent = ();