]> granicus.if.org Git - clang/commitdiff
Implement support for the __is_final type trait, to determine whether
authorDouglas Gregor <dgregor@apple.com>
Sat, 3 Dec 2011 18:14:24 +0000 (18:14 +0000)
committerDouglas Gregor <dgregor@apple.com>
Sat, 3 Dec 2011 18:14:24 +0000 (18:14 +0000)
a class is marked 'final', from Alberto Ganesh Barbati! Fixes
PR11462.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@145775 91177308-0d34-0410-b5e6-96231b3b80d8

docs/LanguageExtensions.html
include/clang/Basic/TokenKinds.def
include/clang/Basic/TypeTraits.h
lib/AST/StmtPrinter.cpp
lib/Lex/PPMacroExpansion.cpp
lib/Parse/ParseExpr.cpp
lib/Parse/ParseExprCXX.cpp
lib/Parse/ParseTentative.cpp
lib/Sema/SemaExprCXX.cpp
test/Lexer/has_feature_type_traits.cpp
test/SemaCXX/type-traits.cpp

index c8e70202d5322374c721f61681721d9dffefad68..79b96f6ad50ec9e8e2c8a4b590fd51e4d95ef68d 100644 (file)
@@ -724,6 +724,7 @@ struct is_convertible_to {
   <li><code>__is_polymorphic</code> (GNU, Microsoft)</li>
   <li><code>__is_union</code> (GNU, Microsoft)</li>
   <li><code>__is_literal(type)</code>: Determines whether the given type is a literal type</li>
+  <li><code>__is_final</code>: Determines whether the given type is declared with a <code>final</code> class-virt-specifier.</li>
   <li><code>__underlying_type(type)</code>: Retrieves the underlying type for a given <code>enum</code> type. This trait is required to implement the C++11 standard library.</li>
 </ul>
 
index 07cb4ce30026314350e4fb24d117fd3933f7aa8f..99ccc9ac4e0b33b18357473cad399785c56bed41 100644 (file)
@@ -353,6 +353,7 @@ KEYWORD(__is_class                  , KEYCXX)
 KEYWORD(__is_convertible_to         , KEYCXX)
 KEYWORD(__is_empty                  , KEYCXX)
 KEYWORD(__is_enum                   , KEYCXX)
+KEYWORD(__is_final                  , KEYCXX)
 // Tentative name - there's no implementation of std::is_literal_type yet.
 KEYWORD(__is_literal                , KEYCXX)
 // Name for GCC 4.6 compatibility - people have already written libraries using
index a7a45bded82076e3939569940ac30712265cfa2e..3ae56afae593b4102d86c1d684484712af9a141a 100644 (file)
@@ -35,6 +35,7 @@ namespace clang {
     UTT_IsConst,
     UTT_IsEmpty,
     UTT_IsEnum,
+    UTT_IsFinal,
     UTT_IsFloatingPoint,
     UTT_IsFunction,
     UTT_IsFundamental,
index 220e50a058a7043a82f133c0a1af0dcdeb0ec24f..6d9139c000af4fc038ddc10cc17bbf907041720a 100644 (file)
@@ -1409,6 +1409,7 @@ static const char *getTypeTraitName(UnaryTypeTrait UTT) {
   case UTT_IsConst:                 return "__is_const";
   case UTT_IsEmpty:               return "__is_empty";
   case UTT_IsEnum:                return "__is_enum";
+  case UTT_IsFinal:                 return "__is_final";
   case UTT_IsFloatingPoint:         return "__is_floating_point";
   case UTT_IsFunction:              return "__is_function";
   case UTT_IsFundamental:           return "__is_fundamental";
index 2e2dd71b087e6ee7af396c198ec6c554afc7062f..20ebb40f534b96f26dfa9a880fcc6dca78838777 100644 (file)
@@ -675,6 +675,7 @@ static bool HasFeature(const Preprocessor &PP, const IdentifierInfo *II) {
                  PP.getIdentifierInfo("__is_empty")->getTokenID()
                                                             != tok::identifier)
            .Case("is_enum", LangOpts.CPlusPlus)
+           .Case("is_final", LangOpts.CPlusPlus)
            .Case("is_literal", LangOpts.CPlusPlus)
            .Case("is_standard_layout", LangOpts.CPlusPlus)
            // __is_pod is available only if the horrible
index 4b8fd76269abef71560e2f57aa393b748b6f0c75..75fc9be5aa43e1e85b62eea4331c7cfe9e09db27 100644 (file)
@@ -565,6 +565,7 @@ ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
 ///                   '__is_class'
 ///                   '__is_empty'                            [TODO]
 ///                   '__is_enum'
+///                   '__is_final'
 ///                   '__is_pod'
 ///                   '__is_polymorphic'
 ///                   '__is_trivial'
@@ -1087,6 +1088,7 @@ ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
   case tok::kw___is_trivial:
   case tok::kw___is_trivially_copyable:
   case tok::kw___is_union:
+  case tok::kw___is_final:
   case tok::kw___has_trivial_constructor:
   case tok::kw___has_trivial_copy:
   case tok::kw___has_trivial_assign:
index dfc77e17d4959362f86fc1ba095cbd9db009868a..d9983d4167695cb0a7902fa66add17d002caf5a7 100644 (file)
@@ -2241,6 +2241,7 @@ static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) {
   case tok::kw___is_const:                   return UTT_IsConst;
   case tok::kw___is_empty:                return UTT_IsEmpty;
   case tok::kw___is_enum:                 return UTT_IsEnum;
+  case tok::kw___is_final:                 return UTT_IsFinal;
   case tok::kw___is_floating_point:          return UTT_IsFloatingPoint;
   case tok::kw___is_function:                return UTT_IsFunction;
   case tok::kw___is_fundamental:             return UTT_IsFundamental;
index d53839f3cb0137239079832049deca9b8aaaa6cb..e682cb421895390edb222f7eb015cdb073be1be8 100644 (file)
@@ -670,6 +670,7 @@ Parser::isExpressionOrTypeSpecifierSimple(tok::TokenKind Kind) {
   case tok::kw___is_convertible_to:
   case tok::kw___is_empty:
   case tok::kw___is_enum:
+  case tok::kw___is_final:
   case tok::kw___is_literal:
   case tok::kw___is_literal_type:
   case tok::kw___is_pod:
index 973e92d214ec2f204e46b9996a182d5a171912c8..06837fef8471498a19b91a8a2cf8ad4ca2cf1b65 100644 (file)
@@ -2650,6 +2650,9 @@ static bool CheckUnaryTypeTraitTypeCompleteness(Sema &S,
   case UTT_IsAbstract:
     // Fall-through
 
+  // These traits require a complete type.
+  case UTT_IsFinal:
+
     // These trait expressions are designed to help implement predicates in
     // [meta.unary.prop] despite not being named the same. They are specified
     // by both GCC and the Embarcadero C++ compiler, and require the complete
@@ -2775,6 +2778,10 @@ static bool EvaluateUnaryTypeTrait(Sema &Self, UnaryTypeTrait UTT,
     if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
       return RD->isAbstract();
     return false;
+  case UTT_IsFinal:
+    if (const CXXRecordDecl *RD = T->getAsCXXRecordDecl())
+      return RD->hasAttr<FinalAttr>();
+    return false;
   case UTT_IsSigned:
     return T->isSignedIntegerType();
   case UTT_IsUnsigned:
index 53056a02b72a58e0e3566359317b61c7166fb72f..0c2cfa56c4ad040e1e28029bd782846ab2ae96d4 100644 (file)
@@ -70,6 +70,11 @@ int is_enum();
 #endif
 // CHECK: int is_enum();
 
+#if __has_feature(is_final)
+int is_final();
+#endif
+// CHECK: int is_final();
+
 #if __has_feature(is_pod)
 int is_pod();
 #endif
index 0914c7cf94e32a62444c28813ee5a5727f36ccfa..2b8714b5e054212e44b0e090986c062d3fbd74eb 100644 (file)
@@ -235,6 +235,37 @@ void is_enum()
   { int arr[F(__is_enum(HasAnonymousUnion))]; }
 }
 
+struct FinalClass final {
+};
+
+template<typename T> 
+struct PotentiallyFinal { };
+
+template<typename T>
+struct PotentiallyFinal<T*> final { };
+
+template<>
+struct PotentiallyFinal<int> final { };
+
+void is_final()
+{
+       { int arr[T(__is_final(FinalClass))]; }
+       { int arr[T(__is_final(PotentiallyFinal<float*>))]; }
+       { int arr[T(__is_final(PotentiallyFinal<int>))]; }
+
+       { int arr[F(__is_final(int))]; }
+       { int arr[F(__is_final(Union))]; }
+       { int arr[F(__is_final(Int))]; }
+       { int arr[F(__is_final(IntAr))]; }
+       { int arr[F(__is_final(UnionAr))]; }
+       { int arr[F(__is_final(Derives))]; }
+       { int arr[F(__is_final(ClassType))]; }
+       { int arr[F(__is_final(cvoid))]; }
+       { int arr[F(__is_final(IntArNB))]; }
+       { int arr[F(__is_final(HasAnonymousUnion))]; }
+       { int arr[F(__is_final(PotentiallyFinal<float>))]; }
+}
+
 typedef HasVirt Polymorph;
 struct InheritPolymorph : Polymorph {};