[cfe-commits] r131270 - in /cfe/trunk: include/clang/AST/Type.h include/clang/Basic/TokenKinds.def include/clang/Basic/TypeTraits.h lib/AST/StmtPrinter.cpp lib/AST/Type.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

Sean Hunt scshunt at csclub.uwaterloo.ca
Thu May 12 17:31:07 PDT 2011


Author: coppro
Date: Thu May 12 19:31:07 2011
New Revision: 131270

URL: http://llvm.org/viewvc/llvm-project?rev=131270&view=rev
Log:
Implement the __is_trivially_copyable type trait

Modified:
    cfe/trunk/include/clang/AST/Type.h
    cfe/trunk/include/clang/Basic/TokenKinds.def
    cfe/trunk/include/clang/Basic/TypeTraits.h
    cfe/trunk/lib/AST/StmtPrinter.cpp
    cfe/trunk/lib/AST/Type.cpp
    cfe/trunk/lib/Lex/PPMacroExpansion.cpp
    cfe/trunk/lib/Parse/ParseExpr.cpp
    cfe/trunk/lib/Parse/ParseExprCXX.cpp
    cfe/trunk/lib/Parse/ParseTentative.cpp
    cfe/trunk/lib/Sema/SemaExprCXX.cpp
    cfe/trunk/test/Lexer/has_feature_type_traits.cpp
    cfe/trunk/test/SemaCXX/type-traits.cpp

Modified: cfe/trunk/include/clang/AST/Type.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Type.h?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Type.h (original)
+++ cfe/trunk/include/clang/AST/Type.h Thu May 12 19:31:07 2011
@@ -1189,6 +1189,10 @@
   /// (C++0x [basic.types]p9)
   bool isTrivialType() const;
 
+  /// isTriviallyCopyableType - Return true if this is a trivially copyable type
+  /// (C++0x [basic.types]p9
+  bool isTriviallyCopyableType() const;
+
   /// \brief Test if this type is a standard-layout type.
   /// (C++0x [basic.type]p9)
   bool isStandardLayoutType() const;

Modified: cfe/trunk/include/clang/Basic/TokenKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/TokenKinds.def?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/TokenKinds.def (original)
+++ cfe/trunk/include/clang/Basic/TokenKinds.def Thu May 12 19:31:07 2011
@@ -346,6 +346,9 @@
 KEYWORD(__is_trivial                , KEYCXX)
 KEYWORD(__is_union                  , KEYCXX)
 
+// Clang-only C++ Type Traits
+KEYWORD(__is_trivially_copyable     , KEYCXX)
+
 // Embarcadero Expression Traits
 KEYWORD(__is_lvalue_expr            , KEYCXX)
 KEYWORD(__is_rvalue_expr            , KEYCXX)

Modified: cfe/trunk/include/clang/Basic/TypeTraits.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/TypeTraits.h?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/TypeTraits.h (original)
+++ cfe/trunk/include/clang/Basic/TypeTraits.h Thu May 12 19:31:07 2011
@@ -54,6 +54,7 @@
     UTT_IsSigned,
     UTT_IsStandardLayout,
     UTT_IsTrivial,
+    UTT_IsTriviallyCopyable,
     UTT_IsUnion,
     UTT_IsUnsigned,
     UTT_IsVoid,

Modified: cfe/trunk/lib/AST/StmtPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtPrinter.cpp?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/lib/AST/StmtPrinter.cpp (original)
+++ cfe/trunk/lib/AST/StmtPrinter.cpp Thu May 12 19:31:07 2011
@@ -1329,6 +1329,7 @@
   case UTT_IsSigned:                return "__is_signed";
   case UTT_IsStandardLayout:        return "__is_standard_layout";
   case UTT_IsTrivial:               return "__is_trivial";
+  case UTT_IsTriviallyCopyable:     return "__is_trivially_copyable";
   case UTT_IsUnion:               return "__is_union";
   case UTT_IsUnsigned:              return "__is_unsigned";
   case UTT_IsVoid:                  return "__is_void";

Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Thu May 12 19:31:07 2011
@@ -964,6 +964,37 @@
   return false;
 }
 
+bool Type::isTriviallyCopyableType() const {
+  if (isDependentType())
+    return false;
+
+  // C++0x [basic.types]p9
+  //   Scalar types, trivially copyable class types, arrays of such types, and
+  //   cv-qualified versions of these types are collectively called trivial
+  //   types.
+  const Type *BaseTy = getBaseElementTypeUnsafe();
+  assert(BaseTy && "NULL element type");
+
+  // Return false for incomplete types after skipping any incomplete array types
+  // which are expressly allowed by the standard and thus our API.
+  if (BaseTy->isIncompleteType())
+    return false;
+ 
+  // As an extension, Clang treats vector types as Scalar types.
+  if (BaseTy->isScalarType() || BaseTy->isVectorType()) return true;
+  if (const RecordType *RT = BaseTy->getAs<RecordType>()) {
+    if (const CXXRecordDecl *ClassDecl =
+        dyn_cast<CXXRecordDecl>(RT->getDecl())) {
+      if (!ClassDecl->isTriviallyCopyable()) return false;
+    }
+
+    return true;
+  }
+
+  // No other types can match.
+  return false;
+}
+
 bool Type::isStandardLayoutType() const {
   if (isDependentType())
     return false;

Modified: cfe/trunk/lib/Lex/PPMacroExpansion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/PPMacroExpansion.cpp?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/PPMacroExpansion.cpp (original)
+++ cfe/trunk/lib/Lex/PPMacroExpansion.cpp Thu May 12 19:31:07 2011
@@ -597,6 +597,7 @@
            .Case("is_pod", LangOpts.CPlusPlus)
            .Case("is_polymorphic", LangOpts.CPlusPlus)
            .Case("is_trivial", LangOpts.CPlusPlus)
+           .Case("is_trivially_copyable", LangOpts.CPlusPlus)
            .Case("is_union", LangOpts.CPlusPlus)
            .Case("tls", PP.getTargetInfo().isTLSSupported())
            .Default(false);

Modified: cfe/trunk/lib/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExpr.cpp?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExpr.cpp Thu May 12 19:31:07 2011
@@ -566,6 +566,9 @@
 ///                   '__is_trivial'
 ///                   '__is_union'
 ///
+/// [Clang] unary-type-trait:
+///                   '__trivially_copyable'
+///
 ///       binary-type-trait:
 /// [GNU]             '__is_base_of'       
 /// [MS]              '__is_convertible_to'
@@ -1060,6 +1063,7 @@
   case tok::kw___is_pod:
   case tok::kw___is_polymorphic:
   case tok::kw___is_trivial:
+  case tok::kw___is_trivially_copyable:
   case tok::kw___is_union:
   case tok::kw___has_trivial_constructor:
   case tok::kw___has_trivial_copy:

Modified: cfe/trunk/lib/Parse/ParseExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExprCXX.cpp?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExprCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExprCXX.cpp Thu May 12 19:31:07 2011
@@ -1958,6 +1958,7 @@
   case tok::kw___is_signed:                  return UTT_IsSigned;
   case tok::kw___is_standard_layout:         return UTT_IsStandardLayout;
   case tok::kw___is_trivial:                 return UTT_IsTrivial;
+  case tok::kw___is_trivially_copyable:      return UTT_IsTriviallyCopyable;
   case tok::kw___is_union:                return UTT_IsUnion;
   case tok::kw___is_unsigned:                return UTT_IsUnsigned;
   case tok::kw___is_void:                    return UTT_IsVoid;

Modified: cfe/trunk/lib/Parse/ParseTentative.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseTentative.cpp?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseTentative.cpp (original)
+++ cfe/trunk/lib/Parse/ParseTentative.cpp Thu May 12 19:31:07 2011
@@ -664,6 +664,7 @@
   case tok::kw___is_pod:
   case tok::kw___is_polymorphic:
   case tok::kw___is_trivial:
+  case tok::kw___is_trivially_copyable:
   case tok::kw___is_union:
   case tok::kw___uuidof:
     return TPResult::True();

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Thu May 12 19:31:07 2011
@@ -2436,6 +2436,7 @@
     // C++0x [meta.unary.prop] Table 49 requires the following traits to be
     // applied to a complete type.
   case UTT_IsTrivial:
+  case UTT_IsTriviallyCopyable:
   case UTT_IsStandardLayout:
   case UTT_IsPOD:
   case UTT_IsLiteral:
@@ -2532,6 +2533,8 @@
     return T.isVolatileQualified();
   case UTT_IsTrivial:
     return T->isTrivialType();
+  case UTT_IsTriviallyCopyable:
+    return T->isTriviallyCopyableType();
   case UTT_IsStandardLayout:
     return T->isStandardLayoutType();
   case UTT_IsPOD:

Modified: cfe/trunk/test/Lexer/has_feature_type_traits.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/has_feature_type_traits.cpp?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/test/Lexer/has_feature_type_traits.cpp (original)
+++ cfe/trunk/test/Lexer/has_feature_type_traits.cpp Thu May 12 19:31:07 2011
@@ -94,3 +94,8 @@
 int is_standard_layout();
 #endif
 // CHECK: int is_standard_layout();
+
+#if __has_feature(is_trivially_copyable)
+int is_trivially_copyable();
+#endif
+// CHECK: int is_trivially_copyable();

Modified: cfe/trunk/test/SemaCXX/type-traits.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/type-traits.cpp?rev=131270&r1=131269&r2=131270&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/type-traits.cpp (original)
+++ cfe/trunk/test/SemaCXX/type-traits.cpp Thu May 12 19:31:07 2011
@@ -870,6 +870,15 @@
   }
 };
 
+struct SuperNonTrivialStruct {
+  SuperNonTrivialStruct() { }
+  ~SuperNonTrivialStruct() { }
+};
+
+struct NonTCStruct {
+  NonTCStruct(const NonTCStruct&) {}
+};
+
 void is_trivial2()
 {
   int t01[T(__is_trivial(char))];
@@ -897,6 +906,39 @@
 
   int t30[F(__is_trivial(void))];
   int t31[F(__is_trivial(NonTrivialStruct))];
+  int t32[F(__is_trivial(SuperNonTrivialStruct))];
+  int t33[F(__is_trivial(NonTCStruct))];
+}
+
+void is_trivially_copyable2()
+{
+  int t01[T(__is_trivially_copyable(char))];
+  int t02[T(__is_trivially_copyable(int))];
+  int t03[T(__is_trivially_copyable(long))];
+  int t04[T(__is_trivially_copyable(short))];
+  int t05[T(__is_trivially_copyable(signed char))];
+  int t06[T(__is_trivially_copyable(wchar_t))];
+  int t07[T(__is_trivially_copyable(bool))];
+  int t08[T(__is_trivially_copyable(float))];
+  int t09[T(__is_trivially_copyable(double))];
+  int t10[T(__is_trivially_copyable(long double))];
+  int t11[T(__is_trivially_copyable(unsigned char))];
+  int t12[T(__is_trivially_copyable(unsigned int))];
+  int t13[T(__is_trivially_copyable(unsigned long long))];
+  int t14[T(__is_trivially_copyable(unsigned long))];
+  int t15[T(__is_trivially_copyable(unsigned short))];
+  int t16[T(__is_trivially_copyable(ClassType))];
+  int t17[T(__is_trivially_copyable(Derives))];
+  int t18[T(__is_trivially_copyable(Enum))];
+  int t19[T(__is_trivially_copyable(IntAr))];
+  int t20[T(__is_trivially_copyable(Union))];
+  int t21[T(__is_trivially_copyable(UnionAr))];
+  int t22[T(__is_trivially_copyable(TrivialStruct))];
+  int t23[T(__is_trivially_copyable(NonTrivialStruct))];
+
+  int t30[F(__is_trivially_copyable(void))];
+  int t32[F(__is_trivially_copyable(SuperNonTrivialStruct))];
+  int t31[F(__is_trivially_copyable(NonTCStruct))];
 }
 
 struct CStruct {
@@ -1474,6 +1516,51 @@
   { int arr[F(__is_trivial(cvoid))]; }
 }
 
+void is_trivially_copyable()
+{
+  { int arr[T(__is_trivially_copyable(int))]; }
+  { int arr[T(__is_trivially_copyable(Enum))]; }
+  { int arr[T(__is_trivially_copyable(POD))]; }
+  { int arr[T(__is_trivially_copyable(Int))]; }
+  { int arr[T(__is_trivially_copyable(IntAr))]; }
+  { int arr[T(__is_trivially_copyable(IntArNB))]; }
+  { int arr[T(__is_trivially_copyable(Statics))]; }
+  { int arr[T(__is_trivially_copyable(Empty))]; }
+  { int arr[T(__is_trivially_copyable(EmptyUnion))]; }
+  { int arr[T(__is_trivially_copyable(Union))]; }
+  { int arr[T(__is_trivially_copyable(Derives))]; }
+  { int arr[T(__is_trivially_copyable(DerivesAr))]; }
+  { int arr[T(__is_trivially_copyable(DerivesArNB))]; }
+  { int arr[T(__is_trivially_copyable(DerivesEmpty))]; }
+  { int arr[T(__is_trivially_copyable(HasFunc))]; }
+  { int arr[T(__is_trivially_copyable(HasOp))]; }
+  { int arr[T(__is_trivially_copyable(HasConv))]; }
+  { int arr[T(__is_trivially_copyable(HasAssign))]; }
+  { int arr[T(__is_trivially_copyable(HasAnonymousUnion))]; }
+  { int arr[T(__is_trivially_copyable(HasPriv))]; }
+  { int arr[T(__is_trivially_copyable(HasProt))]; }
+  { int arr[T(__is_trivially_copyable(DerivesHasPriv))]; }
+  { int arr[T(__is_trivially_copyable(DerivesHasProt))]; }
+  { int arr[T(__is_trivially_copyable(Vector))]; }
+  { int arr[T(__is_trivially_copyable(VectorExt))]; }
+  { int arr[T(__is_trivially_copyable(HasCons))]; }
+  { int arr[T(__is_trivially_copyable(HasRef))]; }
+  { int arr[T(__is_trivially_copyable(HasNonPOD))]; }
+  { int arr[T(__is_trivially_copyable(DerivesHasCons))]; }
+  { int arr[T(__is_trivially_copyable(DerivesHasRef))]; }
+
+  { int arr[F(__is_trivially_copyable(HasCopyAssign))]; }
+  { int arr[F(__is_trivially_copyable(HasMoveAssign))]; }
+  { int arr[F(__is_trivially_copyable(HasDest))]; }
+  { int arr[F(__is_trivially_copyable(HasVirt))]; }
+  { int arr[F(__is_trivially_copyable(DerivesHasCopyAssign))]; }
+  { int arr[F(__is_trivially_copyable(DerivesHasMoveAssign))]; }
+  { int arr[F(__is_trivially_copyable(DerivesHasDest))]; }
+  { int arr[F(__is_trivially_copyable(DerivesHasVirt))]; }
+  { int arr[F(__is_trivially_copyable(void))]; }
+  { int arr[F(__is_trivially_copyable(cvoid))]; }
+}
+
 void array_rank() {
   int t01[T(__array_rank(IntAr) == 1)];
   int t02[T(__array_rank(ConstIntArAr) == 2)];





More information about the cfe-commits mailing list