KEYWORD(__is_abstract , KEYCXX)
KEYWORD(__is_base_of , KEYCXX)
KEYWORD(__is_class , KEYCXX)
+KEYWORD(__is_convertible_to , KEYCXX)
KEYWORD(__is_empty , KEYCXX)
KEYWORD(__is_enum , KEYCXX)
KEYWORD(__is_pod , 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.
KEYWORD(__private_extern__ , KEYALL)
/// BinaryTypeTrait - Names for the binary type traits.
enum BinaryTypeTrait {
BTT_IsBaseOf,
- BTT_TypeCompatible
+ BTT_TypeCompatible,
+ BTT_IsConvertibleTo
};
}
static const char *getTypeTraitName(BinaryTypeTrait BTT) {
switch (BTT) {
- default: llvm_unreachable("Unknown binary type trait");
case BTT_IsBaseOf: return "__is_base_of";
case BTT_TypeCompatible: return "__builtin_types_compatible_p";
+ case BTT_IsConvertibleTo: return "__is_convertible_to";
}
return "";
}
/// '__is_polymorphic'
/// '__is_union'
///
-/// [GNU] binary-type-trait:
-/// '__is_base_of' [TODO]
+/// binary-type-trait:
+/// [GNU] '__is_base_of'
+/// [MS] '__is_convertible_to'
///
ExprResult Parser::ParseCastExpression(bool isUnaryExpression,
bool isAddressOfOperand,
case tok::kw___builtin_types_compatible_p:
case tok::kw___is_base_of:
+ case tok::kw___is_convertible_to:
return ParseBinaryTypeTrait();
case tok::at: {
default: llvm_unreachable("Not a known binary type trait");
case tok::kw___is_base_of: return BTT_IsBaseOf;
case tok::kw___builtin_types_compatible_p: return BTT_TypeCompatible;
+ case tok::kw___is_convertible_to: return BTT_IsConvertibleTo;
}
}
case tok::kw___is_abstract:
case tok::kw___is_base_of:
case tok::kw___is_class:
+ case tok::kw___is_convertible_to:
case tok::kw___is_empty:
case tok::kw___is_enum:
case tok::kw___is_pod:
case BTT_TypeCompatible:
return Self.Context.typesAreCompatible(LhsT.getUnqualifiedType(),
RhsT.getUnqualifiedType());
+
+ case BTT_IsConvertibleTo: {
+ // C++0x [meta.rel]p4:
+ // Given the following function prototype:
+ //
+ // template <class T>
+ // typename add_rvalue_reference<T>::type create();
+ //
+ // the predicate condition for a template specialization
+ // is_convertible<From, To> shall be satisfied if and only if
+ // the return expression in the following code would be
+ // well-formed, including any implicit conversions to the return
+ // type of the function:
+ //
+ // To test() {
+ // return create<From>();
+ // }
+ //
+ // Access checking is performed as if in a context unrelated to To and
+ // From. Only the validity of the immediate context of the expression
+ // of the return-statement (including conversions to the return type)
+ // is considered.
+ //
+ // We model the initialization as a copy-initialization of a temporary
+ // of the appropriate type, which for this expression is identical to the
+ // return statement (since NRVO doesn't apply).
+ if (LhsT->isObjectType() || LhsT->isFunctionType())
+ LhsT = Self.Context.getRValueReferenceType(LhsT);
+
+ InitializedEntity To(InitializedEntity::InitializeTemporary(RhsT));
+ OpaqueValueExpr From(LhsT.getNonLValueExprType(Self.Context),
+ Expr::getValueKindForType(LhsT));
+ Expr *FromPtr = &From;
+ InitializationKind Kind(InitializationKind::CreateCopy(KeyLoc,
+ SourceLocation()));
+
+ // Perform the initialization within a SFINAE trap.
+ // FIXME: We don't implement the access-checking bits yet, because we don't
+ // handle access control as part of SFINAE.
+ Sema::SFINAETrap SFINAE(Self);
+ InitializationSequence Init(Self, To, Kind, &FromPtr, 1);
+ if (Init.getKind() == InitializationSequence::FailedSequence)
+ return false;
+ ExprResult Result = Init.Perform(Self, To, Kind, MultiExprArg(&FromPtr, 1));
+ return !Result.isInvalid() && !SFINAE.hasErrorOccurred();
+ }
}
llvm_unreachable("Unknown type trait or not implemented");
}
// Select trait result type.
QualType ResultType;
switch (BTT) {
- default: llvm_unreachable("Unknown type trait or not implemented");
case BTT_IsBaseOf: ResultType = Context.BoolTy; break;
case BTT_TypeCompatible: ResultType = Context.IntTy; break;
+ case BTT_IsConvertibleTo: ResultType = Context.BoolTy; break;
}
return Owned(new (Context) BinaryTypeTraitExpr(KWLoc, BTT, LhsTSInfo,
isBaseOfT<BaseA<int>, DerivedB<int> >();
isBaseOfF<DerivedB<int>, BaseA<int> >();
}
+
+struct FromInt { FromInt(int); };
+struct ToInt { operator int(); };
+typedef void Function();
+
+void is_convertible_to() {
+ int t01[T(__is_convertible_to(Int, Int))];
+ int t02[F(__is_convertible_to(Int, IntAr))];
+ int t03[F(__is_convertible_to(IntAr, IntAr))];
+ int t04[T(__is_convertible_to(void, void))];
+ int t05[T(__is_convertible_to(cvoid, void))];
+ int t06[T(__is_convertible_to(void, cvoid))];
+ int t07[T(__is_convertible_to(cvoid, cvoid))];
+ int t08[T(__is_convertible_to(int, FromInt))];
+ int t09[T(__is_convertible_to(long, FromInt))];
+ int t10[T(__is_convertible_to(double, FromInt))];
+ int t11[T(__is_convertible_to(const int, FromInt))];
+ int t12[T(__is_convertible_to(const int&, FromInt))];
+ int t13[T(__is_convertible_to(ToInt, int))];
+ int t14[T(__is_convertible_to(ToInt, const int&))];
+ int t15[T(__is_convertible_to(ToInt, long))];
+ int t16[F(__is_convertible_to(ToInt, int&))];
+ int t17[F(__is_convertible_to(ToInt, FromInt))];
+ int t18[T(__is_convertible_to(IntAr&, IntAr&))];
+ int t19[T(__is_convertible_to(IntAr&, const IntAr&))];
+ int t20[F(__is_convertible_to(const IntAr&, IntAr&))];
+ int t21[F(__is_convertible_to(Function, Function))];
+}