📄 codegeneratorobjc.pm
字号:
# # Copyright (C) 2005, 2006 Nikolas Zimmermann <zimmermann@kde.org># Copyright (C) 2006 Anders Carlsson <andersca@mac.com> # Copyright (C) 2006, 2007 Samuel Weinig <sam@webkit.org># Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org># Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.## This library is free software; you can redistribute it and/or# modify it under the terms of the GNU Library General Public# License as published by the Free Software Foundation; either# version 2 of the License, or (at your option) any later version.# # This library is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# Library General Public License for more details.# # You should have received a copy of the GNU Library General Public License# aint with this library; see the file COPYING.LIB. If not, write to# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,# Boston, MA 02110-1301, USA.#package CodeGeneratorObjC;use File::stat;# Global Variablesmy $module = "";my $outputDir = "";my %publicInterfaces = ();my $newPublicClass = 0;my $interfaceAvailabilityVersion = "";my $isProtocol = 0;my $noImpl = 0;my @ivars = ();my @headerContentHeader = ();my @headerContent = ();my %headerForwardDeclarations = ();my %headerForwardDeclarationsForProtocols = ();my @privateHeaderContentHeader = ();my @privateHeaderContent = ();my %privateHeaderForwardDeclarations = ();my %privateHeaderForwardDeclarationsForProtocols = ();my @internalHeaderContent = ();my @implContentHeader = ();my @implContent = ();my %implIncludes = ();# Hashesmy %protocolTypeHash = ("XPathNSResolver" => 1, "EventListener" => 1, "EventTarget" => 1, "NodeFilter" => 1, "SVGLocatable" => 1, "SVGTransformable" => 1, "SVGStylable" => 1, "SVGFilterPrimitiveStandardAttributes" => 1, "SVGTests" => 1, "SVGLangSpace" => 1, "SVGExternalResourcesRequired" => 1, "SVGURIReference" => 1, "SVGZoomAndPan" => 1, "SVGFitToViewBox" => 1, "SVGAnimatedPathData" => 1, "SVGAnimatedPoints" => 1, "ElementTimeControl" => 1);my %nativeObjCTypeHash = ("URL" => 1, "Color" => 1);# FIXME: this should be replaced with a function that recurses up the tree# to find the actual base type.my %baseTypeHash = ("Object" => 1, "Node" => 1, "NodeList" => 1, "NamedNodeMap" => 1, "DOMImplementation" => 1, "Event" => 1, "CSSRule" => 1, "CSSValue" => 1, "StyleSheet" => 1, "MediaList" => 1, "Counter" => 1, "Rect" => 1, "RGBColor" => 1, "XPathExpression" => 1, "XPathResult" => 1, "NodeIterator" => 1, "TreeWalker" => 1, "AbstractView" => 1, "SVGAngle" => 1, "SVGAnimatedAngle" => 1, "SVGAnimatedBoolean" => 1, "SVGAnimatedEnumeration" => 1, "SVGAnimatedInteger" => 1, "SVGAnimatedLength" => 1, "SVGAnimatedLengthList" => 1, "SVGAnimatedNumber" => 1, "SVGAnimatedNumberList" => 1, "SVGAnimatedPoints" => 1, "SVGAnimatedPreserveAspectRatio" => 1, "SVGAnimatedRect" => 1, "SVGAnimatedString" => 1, "SVGAnimatedTransformList" => 1, "SVGLength" => 1, "SVGLengthList" => 1, "SVGMatrix" => 1, "SVGNumber" => 1, "SVGNumberList" => 1, "SVGPathSeg" => 1, "SVGPathSegList" => 1, "SVGPoint" => 1, "SVGPointList" => 1, "SVGPreserveAspectRatio" => 1, "SVGRect" => 1, "SVGRenderingIntent" => 1, "SVGStringList" => 1, "SVGTransform" => 1, "SVGTransformList" => 1, "SVGUnitTypes" => 1);# Constantsmy $buildingForTigerOrEarlier = 1 if $ENV{"MACOSX_DEPLOYMENT_TARGET"} and $ENV{"MACOSX_DEPLOYMENT_TARGET"} <= 10.4;my $buildingForLeopardOrLater = 1 if $ENV{"MACOSX_DEPLOYMENT_TARGET"} and $ENV{"MACOSX_DEPLOYMENT_TARGET"} >= 10.5;my $exceptionInit = "WebCore::ExceptionCode ec = 0;";my $exceptionRaiseOnError = "WebCore::raiseOnDOMError(ec);";my $assertMainThread = "{ DOM_ASSERT_MAIN_THREAD(); WebCoreThreadViolationCheck(); }";my %conflictMethod = ( # FIXME: Add C language keywords? # FIXME: Add other predefined types like "id"? "callWebScriptMethod:withArguments:" => "WebScriptObject", "evaluateWebScript:" => "WebScriptObject", "removeWebScriptKey:" => "WebScriptObject", "setException:" => "WebScriptObject", "setWebScriptValueAtIndex:value:" => "WebScriptObject", "stringRepresentation" => "WebScriptObject", "webScriptValueAtIndex:" => "WebScriptObject", "autorelease" => "NSObject", "awakeAfterUsingCoder:" => "NSObject", "class" => "NSObject", "classForCoder" => "NSObject", "conformsToProtocol:" => "NSObject", "copy" => "NSObject", "copyWithZone:" => "NSObject", "dealloc" => "NSObject", "description" => "NSObject", "doesNotRecognizeSelector:" => "NSObject", "encodeWithCoder:" => "NSObject", "finalize" => "NSObject", "forwardInvocation:" => "NSObject", "hash" => "NSObject", "init" => "NSObject", "initWithCoder:" => "NSObject", "isEqual:" => "NSObject", "isKindOfClass:" => "NSObject", "isMemberOfClass:" => "NSObject", "isProxy" => "NSObject", "methodForSelector:" => "NSObject", "methodSignatureForSelector:" => "NSObject", "mutableCopy" => "NSObject", "mutableCopyWithZone:" => "NSObject", "performSelector:" => "NSObject", "release" => "NSObject", "replacementObjectForCoder:" => "NSObject", "respondsToSelector:" => "NSObject", "retain" => "NSObject", "retainCount" => "NSObject", "self" => "NSObject", "superclass" => "NSObject", "zone" => "NSObject",);my $fatalError = 0;# Default Licence Templatesmy $headerLicenceTemplate = << "EOF";/* * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. * Copyright (C) 2006 Samuel Weinig <sam.weinig\@gmail.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */EOFmy $implementationLicenceTemplate = << "EOF";/* * This file is part of the WebKit open source project. * This file has been generated by generate-bindings.pl. DO NOT MODIFY! * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */EOF# Default constructorsub new{ my $object = shift; my $reference = { }; $codeGenerator = shift; $outputDir = shift; bless($reference, $object); return $reference;}sub finish{ my $object = shift;}sub ReadPublicInterfaces{ my $class = shift; my $superClass = shift; my $defines = shift; my $isProtocol = shift; my $found = 0; my $actualSuperClass; %publicInterfaces = (); my $fileName = "WebCore/bindings/objc/PublicDOMInterfaces.h"; open FILE, "-|", "/usr/bin/gcc", "-E", "-P", "-x", "objective-c", (map { "-D$_" } split(/ /, $defines)), "-DOBJC_CODE_GENERATION", $fileName or die "Could not open $fileName"; my @documentContent = <FILE>; close FILE; foreach $line (@documentContent) { if (!$isProtocol && $line =~ /^\s*\@interface\s*$class\s*:\s*(\w+)\s*([A-Z0-9_]*)/) { if ($superClass ne $1) { warn "Public API change. Superclass for \"$class\" differs ($1 != $superClass)"; $fatalError = 1; } $interfaceAvailabilityVersion = $2 if defined $2; $found = 1; next; } elsif ($isProtocol && $line =~ /^\s*\@protocol $class\s*<[^>]+>\s*([A-Z0-9_]*)/) { $interfaceAvailabilityVersion = $1 if defined $1; $found = 1; next; } last if $found and $line =~ /^\s?\@end\s?$/; if ($found) { # trim whitspace $line =~ s/^\s+//; $line =~ s/\s+$//; my $availabilityMacro = ""; $line =~ s/\s([A-Z0-9_]+)\s*;$/;/; $availabilityMacro = $1 if defined $1; $publicInterfaces{$line} = $availabilityMacro if length $line; } } # If this class was not found in PublicDOMInterfaces.h then it should be considered as an entirely new public class. $newPublicClass = !$found; $interfaceAvailabilityVersion = "WEBKIT_VERSION_LATEST" if $newPublicClass;}# Params: 'domClass' structsub GenerateInterface{ my $object = shift; my $dataNode = shift; my $defines = shift; $fatalError = 0; my $name = $dataNode->name; my $className = GetClassName($name); my $parentClassName = "DOM" . GetParentImplClassName($dataNode); $isProtocol = $dataNode->extendedAttributes->{ObjCProtocol}; $noImpl = $dataNode->extendedAttributes->{ObjCCustomImplementation} || $isProtocol; ReadPublicInterfaces($className, $parentClassName, $defines, $isProtocol); # Start actual generation.. $object->GenerateHeader($dataNode); $object->GenerateImplementation($dataNode) unless $noImpl; # Write changes. $object->WriteData("DOM" . $name); # Check for missing public API if (keys %publicInterfaces > 0) { my $missing = join("\n", keys %publicInterfaces); warn "Public API change. There are missing public properties and/or methods from the \"$className\" class.\n$missing\n"; $fatalError = 1; } die if $fatalError;}# Params: 'idlDocument' structsub GenerateModule{ my $object = shift; my $dataNode = shift; $module = $dataNode->module;}sub GetClassName{ my $name = $codeGenerator->StripModule(shift); # special cases return "NSString" if $codeGenerator->IsStringType($name); return "NS$name" if IsNativeObjCType($name); return "BOOL" if $name eq "boolean"; return "unsigned" if $name eq "unsigned long"; return "int" if $name eq "long"; return "DOMAbstractView" if $name eq "DOMWindow"; return $name if $codeGenerator->IsPrimitiveType($name) or $name eq "DOMImplementation" or $name eq "DOMTimeStamp";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -