From ccf43505dbc47da041c06125f90b3bd3ac7eac97 Mon Sep 17 00:00:00 2001 From: Sebastian Redl Date: Thu, 3 Dec 2009 00:13:20 +0000 Subject: [PATCH] Introduce the notion of literal types, as specified in C++0x. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@90361 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Type.h | 4 ++++ include/clang/Basic/TokenKinds.def | 2 ++ include/clang/Basic/TypeTraits.h | 3 ++- lib/AST/ExprCXX.cpp | 1 + lib/AST/Type.cpp | 34 ++++++++++++++++++++++++++++++ lib/Parse/ParseExpr.cpp | 1 + lib/Parse/ParseExprCXX.cpp | 1 + test/SemaCXX/literal-type.cpp | 10 +++++++++ 8 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 test/SemaCXX/literal-type.cpp diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index 349487f879..16b6dd7215 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -791,6 +791,10 @@ public: /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10). bool isPODType() const; + /// isLiteralType - Return true if this is a literal type + /// (C++0x [basic.types]p10) + bool isLiteralType() const; + /// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array /// types that have a non-constant expression. This does not include "[]". bool isVariablyModifiedType() const; diff --git a/include/clang/Basic/TokenKinds.def b/include/clang/Basic/TokenKinds.def index 99422332a7..bb022f1175 100644 --- a/include/clang/Basic/TokenKinds.def +++ b/include/clang/Basic/TokenKinds.def @@ -324,6 +324,8 @@ KEYWORD(__is_enum , KEYCXX) KEYWORD(__is_pod , KEYCXX) KEYWORD(__is_polymorphic , KEYCXX) KEYWORD(__is_union , KEYCXX) +// Tentative name - there's no implementation of std::is_literal_type yet. +KEYWORD(__is_literal , KEYCXX) // FIXME: Add MS's traits, too. // Apple Extension. diff --git a/include/clang/Basic/TypeTraits.h b/include/clang/Basic/TypeTraits.h index 2a2eacc4ca..36b830069f 100644 --- a/include/clang/Basic/TypeTraits.h +++ b/include/clang/Basic/TypeTraits.h @@ -32,7 +32,8 @@ namespace clang { UTT_IsEnum, UTT_IsPOD, UTT_IsPolymorphic, - UTT_IsUnion + UTT_IsUnion, + UTT_IsLiteral }; } diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index 7a6fbdca8b..a9f96adae1 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -199,6 +199,7 @@ bool UnaryTypeTraitExpr::EvaluateTrait(ASTContext& C) const { switch(UTT) { default: assert(false && "Unknown type trait or not implemented"); case UTT_IsPOD: return QueriedType->isPODType(); + case UTT_IsLiteral: return QueriedType->isLiteralType(); case UTT_IsClass: // Fallthrough case UTT_IsUnion: if (const RecordType *Record = QueriedType->getAs()) { diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp index 5a2434da3c..70387c73a7 100644 --- a/lib/AST/Type.cpp +++ b/lib/AST/Type.cpp @@ -639,6 +639,40 @@ bool Type::isPODType() const { } } +bool Type::isLiteralType() const { + if (isIncompleteType()) + return false; + + // C++0x [basic.types]p10: + // A type is a literal type if it is: + switch (CanonicalType->getTypeClass()) { + // We're whitelisting + default: return false; + + // -- a scalar type + case Builtin: + case Complex: + case Pointer: + case MemberPointer: + case Vector: + case ExtVector: + case ObjCObjectPointer: + case Enum: + return true; + + // -- a class type with ... + case Record: + // FIXME: Do the tests + return false; + + // -- an array of literal type + // Extension: variable arrays cannot be literal types, since they're + // runtime-sized. + case ConstantArray: + return cast(CanonicalType)->getElementType()->isLiteralType(); + } +} + bool Type::isPromotableIntegerType() const { if (const BuiltinType *BT = getAs()) switch (BT->getKind()) { diff --git a/lib/Parse/ParseExpr.cpp b/lib/Parse/ParseExpr.cpp index f780cf1a60..7b8bc3e845 100644 --- a/lib/Parse/ParseExpr.cpp +++ b/lib/Parse/ParseExpr.cpp @@ -826,6 +826,7 @@ Parser::OwningExprResult Parser::ParseCastExpression(bool isUnaryExpression, case tok::kw___is_empty: case tok::kw___is_polymorphic: case tok::kw___is_abstract: + case tok::kw___is_literal: case tok::kw___has_trivial_constructor: case tok::kw___has_trivial_copy: case tok::kw___has_trivial_assign: diff --git a/lib/Parse/ParseExprCXX.cpp b/lib/Parse/ParseExprCXX.cpp index 52003e6fa1..4eb6cd5daa 100644 --- a/lib/Parse/ParseExprCXX.cpp +++ b/lib/Parse/ParseExprCXX.cpp @@ -1459,6 +1459,7 @@ static UnaryTypeTrait UnaryTypeTraitFromTokKind(tok::TokenKind kind) { case tok::kw___is_pod: return UTT_IsPOD; case tok::kw___is_polymorphic: return UTT_IsPolymorphic; case tok::kw___is_union: return UTT_IsUnion; + case tok::kw___is_literal: return UTT_IsLiteral; } } diff --git a/test/SemaCXX/literal-type.cpp b/test/SemaCXX/literal-type.cpp new file mode 100644 index 0000000000..0dca9c9a92 --- /dev/null +++ b/test/SemaCXX/literal-type.cpp @@ -0,0 +1,10 @@ +// RUN: clang-cc -fsyntax-only -verify -std=c++0x %s + +static_assert(__is_literal(int), "fail"); +static_assert(__is_literal(void*), "fail"); +enum E { E1 }; +static_assert(__is_literal(E), "fail"); +static_assert(__is_literal(decltype(E1)), "fail"); +typedef int IAR[10]; +static_assert(__is_literal(IAR), "fail"); +// FIXME: Records -- 2.40.0