From 1b8ed882d33d996b177bfbd05a8c57f7983bbd6f Mon Sep 17 00:00:00 2001 From: Devin Coughlin Date: Thu, 4 Sep 2014 23:34:19 +0000 Subject: [PATCH] Move AvailabilityAttr::PlatformKind into its own file Swift SVN r21728 --- include/swift/AST/Attr.def | 11 ------- include/swift/AST/Attr.h | 7 +---- include/swift/AST/PlatformKind.h | 31 ++++++++++++++++++++ include/swift/AST/PlatformKinds.def | 30 ++++++++++++++++++++ lib/AST/Attr.cpp | 41 ++++++++++++++------------- lib/ClangImporter/ImportDecl.cpp | 10 +++---- lib/Serialization/Deserialization.cpp | 2 +- lib/Serialization/Serialization.cpp | 2 +- 8 files changed, 90 insertions(+), 44 deletions(-) create mode 100644 include/swift/AST/PlatformKind.h create mode 100644 include/swift/AST/PlatformKinds.def diff --git a/include/swift/AST/Attr.def b/include/swift/AST/Attr.def index 27ea015ff08..2305fa1924c 100644 --- a/include/swift/AST/Attr.def +++ b/include/swift/AST/Attr.def @@ -30,10 +30,6 @@ #define TYPE_ATTR(X) #endif -#ifndef AVAILABILITY_PLATFORM -#define AVAILABILITY_PLATFORM(X, PrettyName) -#endif - // Type attributes TYPE_ATTR(autoclosure) TYPE_ATTR(cc) @@ -198,13 +194,6 @@ DECL_ATTR(__objc_bridged, ObjCBridged, OnClass | NotSerialized, SIMPLE_DECL_ATTR(NSApplicationMain, NSApplicationMain, OnClass, 52) -// Reordering these platforms will break serialization. -AVAILABILITY_PLATFORM(iOS, "iOS") -AVAILABILITY_PLATFORM(OSX, "OS X") -AVAILABILITY_PLATFORM(iOSApplicationExtension, "iOS application extension") -AVAILABILITY_PLATFORM(OSXApplicationExtension, "OS X application extension") - -#undef AVAILABILITY_PLATFORM #undef TYPE_ATTR #undef DECL_ATTR_ALIAS #undef SIMPLE_DECL_ATTR diff --git a/include/swift/AST/Attr.h b/include/swift/AST/Attr.h index 37923cf1f46..8939ab8598d 100644 --- a/include/swift/AST/Attr.h +++ b/include/swift/AST/Attr.h @@ -21,6 +21,7 @@ #include "swift/Basic/SourceLoc.h" #include "swift/AST/Identifier.h" #include "swift/AST/Ownership.h" +#include "swift/AST/PlatformKind.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/ErrorHandling.h" @@ -586,12 +587,6 @@ enum class MinVersionComparison { /// Defines the @availability attribute. class AvailabilityAttr : public DeclAttribute { public: - /// Available platforms for the availability attribute. - enum PlatformKind { - none, -#define AVAILABILITY_PLATFORM(X, PrettyName) X, -#include "swift/AST/Attr.def" - }; #define INIT_VER_TUPLE(X)\ X(X.empty() ? Optional() : X) diff --git a/include/swift/AST/PlatformKind.h b/include/swift/AST/PlatformKind.h new file mode 100644 index 00000000000..7d0bdc813bd --- /dev/null +++ b/include/swift/AST/PlatformKind.h @@ -0,0 +1,31 @@ +//===--- PlatformKinds.h - Swift Language Platform Kinds --------*- C++ -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See http://swift.org/LICENSE.txt for license information +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// +// +// This file defines the platform kinds for API availability. +// +//===----------------------------------------------------------------------===// + +#ifndef SWIFT_AST_PLATFORM_KIND_H +#define SWIFT_AST_PLATFORM_KIND_H + +namespace swift { + +/// Available platforms for the availability attribute. + enum class PlatformKind { + none, +#define AVAILABILITY_PLATFORM(X, PrettyName) X, +#include "swift/AST/PlatformKinds.def" +}; + +} // end namespace swift + +#endif diff --git a/include/swift/AST/PlatformKinds.def b/include/swift/AST/PlatformKinds.def new file mode 100644 index 00000000000..3d12dca2f68 --- /dev/null +++ b/include/swift/AST/PlatformKinds.def @@ -0,0 +1,30 @@ +//===--- PlatformKinds.def - Swift PlatformKind Metaprogramming -*- C++ -*-===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See http://swift.org/LICENSE.txt for license information +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// +// +// This file defines macros used for macro-metaprogramming with platform kinds. +// +//===----------------------------------------------------------------------===// + +/// AVAILABILITY_PLATFORM(X, PrettyName) +/// X - The name of the platform +/// PrettyName - A string with the platform name for pretty printing +#ifndef AVAILABILITY_PLATFORM +#define AVAILABILITY_PLATFORM(X, PrettyName) +#endif + +// Reordering these platforms will break serialization. +AVAILABILITY_PLATFORM(iOS, "iOS") +AVAILABILITY_PLATFORM(OSX, "OS X") +AVAILABILITY_PLATFORM(iOSApplicationExtension, "iOS application extension") +AVAILABILITY_PLATFORM(OSXApplicationExtension, "OS X application extension") + +#undef AVAILABILITY_PLATFORM diff --git a/lib/AST/Attr.cpp b/lib/AST/Attr.cpp index 9a1f0b9ae00..008da9bcf24 100644 --- a/lib/AST/Attr.cpp +++ b/lib/AST/Attr.cpp @@ -398,52 +398,53 @@ AvailabilityAttr *AvailabilityAttr::createUnavailableAttr(ASTContext &C, } StringRef -AvailabilityAttr::platformString(AvailabilityAttr::PlatformKind platform) { +AvailabilityAttr::platformString(PlatformKind platform) { switch (platform) { - case none: return "*"; -#define AVAILABILITY_PLATFORM(X, PrettyName) case X: return #X; -#include "swift/AST/Attr.def" + case PlatformKind::none: return "*"; +#define AVAILABILITY_PLATFORM(X, PrettyName) case PlatformKind::X: return #X; +#include "swift/AST/PlatformKinds.def" } } StringRef AvailabilityAttr::prettyPlatformString( - AvailabilityAttr::PlatformKind platform) { + PlatformKind platform) { switch (platform) { - case none: return "*"; -#define AVAILABILITY_PLATFORM(X, PrettyName) case X: return PrettyName; -#include "swift/AST/Attr.def" + case PlatformKind::none: return "*"; +#define AVAILABILITY_PLATFORM(X, PrettyName) case PlatformKind::X: \ + return PrettyName; +#include "swift/AST/PlatformKinds.def" } } -Optional +Optional AvailabilityAttr::platformFromString(StringRef Name) { if (Name == "*") return PlatformKind::none; return - llvm::StringSwitch>(Name) -#define AVAILABILITY_PLATFORM(X, PrettyName) .Case(#X, X) -#include "swift/AST/Attr.def" - .Default(Optional()); + llvm::StringSwitch>(Name) +#define AVAILABILITY_PLATFORM(X, PrettyName) .Case(#X, PlatformKind::X) +#include "swift/AST/PlatformKinds.def" + .Default(Optional()); } bool AvailabilityAttr::isActivePlatform(const ASTContext &ctx) const { if (!hasPlatform()) return true; - if (Platform == OSXApplicationExtension || - Platform == iOSApplicationExtension) + if (Platform == PlatformKind::OSXApplicationExtension || + Platform == PlatformKind::iOSApplicationExtension) if (!ctx.LangOpts.EnableAppExtensionRestrictions) return false; // FIXME: This is an awful way to get the current OS. switch (Platform) { - case OSX: - case OSXApplicationExtension: + case PlatformKind::OSX: + case PlatformKind::OSXApplicationExtension: return ctx.LangOpts.getTargetConfigOption("os") == "OSX"; - case iOS: - case iOSApplicationExtension: + case PlatformKind::iOS: + case PlatformKind::iOSApplicationExtension: return ctx.LangOpts.getTargetConfigOption("os") == "iOS"; - case none: + case PlatformKind::none: llvm_unreachable("handled above"); } } diff --git a/lib/ClangImporter/ImportDecl.cpp b/lib/ClangImporter/ImportDecl.cpp index 2be7ec440cb..bba7709bae6 100644 --- a/lib/ClangImporter/ImportDecl.cpp +++ b/lib/ClangImporter/ImportDecl.cpp @@ -4702,12 +4702,12 @@ void ClangImporter::Implementation::importAttributes( // Translate from Clang platform strings to known Swift platforms. auto platformK = - llvm::StringSwitch>(Platform) - .Case("ios", AvailabilityAttr::iOS) - .Case("macosx", AvailabilityAttr::OSX) - .Case("ios_app_extension", AvailabilityAttr::iOSApplicationExtension) + llvm::StringSwitch>(Platform) + .Case("ios", PlatformKind::iOS) + .Case("macosx", PlatformKind::OSX) + .Case("ios_app_extension", PlatformKind::iOSApplicationExtension) .Case("macosx_app_extension", - AvailabilityAttr::OSXApplicationExtension) + PlatformKind::OSXApplicationExtension) .Default(Nothing); if (!platformK) continue; diff --git a/lib/Serialization/Deserialization.cpp b/lib/Serialization/Deserialization.cpp index 90b8b96dd42..56199ab754d 100644 --- a/lib/Serialization/Deserialization.cpp +++ b/lib/Serialization/Deserialization.cpp @@ -1584,7 +1584,7 @@ Decl *ModuleFile::getDecl(DeclID DID, Optional ForcedContext) { Attr = new (ctx) AvailabilityAttr( SourceLoc(), SourceRange(), - (AvailabilityAttr::PlatformKind)platform, message, rename, + (PlatformKind)platform, message, rename, Introduced, Deprecated, Obsoleted, isUnavailable, isImplicit); break; diff --git a/lib/Serialization/Serialization.cpp b/lib/Serialization/Serialization.cpp index 1af4e724737..ddf94fc91cb 100644 --- a/lib/Serialization/Serialization.cpp +++ b/lib/Serialization/Serialization.cpp @@ -1398,7 +1398,7 @@ void Serializer::writeDeclAttribute(const DeclAttribute *DA) { LIST_VER_TUPLE_PIECES(Introduced), LIST_VER_TUPLE_PIECES(Deprecated), LIST_VER_TUPLE_PIECES(Obsoleted), - theAttr->Platform, + static_cast(theAttr->Platform), theAttr->Message.size(), theAttr->Rename.size(), blob);