[cfe-commits] r104081 - in /cfe/trunk: lib/Sema/SemaExprCXX.cpp lib/Sema/SemaOverload.cpp lib/Sema/SemaOverload.h test/SemaCXX/vector.cpp
Douglas Gregor
dgregor at apple.com
Tue May 18 15:42:18 PDT 2010
Author: dgregor
Date: Tue May 18 17:42:18 2010
New Revision: 104081
URL: http://llvm.org/viewvc/llvm-project?rev=104081&view=rev
Log:
Implement C++ support for vector and extended vector types. This
involves extending implicit conversion sequences to model vector
conversions and vector splats, along with teaching the C++ conditional
operator-checking code about vector types.
Fixes <rdar://problem/7983501>.
Added:
cfe/trunk/test/SemaCXX/vector.cpp
Modified:
cfe/trunk/lib/Sema/SemaExprCXX.cpp
cfe/trunk/lib/Sema/SemaOverload.cpp
cfe/trunk/lib/Sema/SemaOverload.h
Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=104081&r1=104080&r2=104081&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Tue May 18 17:42:18 2010
@@ -1777,10 +1777,6 @@
ImpCastExprToType(From, ToType, CastExpr::CK_FloatingToIntegral);
break;
- case ICK_Complex_Real:
- ImpCastExprToType(From, ToType, CastExpr::CK_Unknown);
- break;
-
case ICK_Compatible_Conversion:
ImpCastExprToType(From, ToType, CastExpr::CK_NoOp);
break;
@@ -1841,7 +1837,23 @@
break;
}
- default:
+ case ICK_Vector_Conversion:
+ ImpCastExprToType(From, ToType, CastExpr::CK_BitCast);
+ break;
+
+ case ICK_Vector_Splat:
+ ImpCastExprToType(From, ToType, CastExpr::CK_VectorSplat);
+ break;
+
+ case ICK_Complex_Real:
+ ImpCastExprToType(From, ToType, CastExpr::CK_Unknown);
+ break;
+
+ case ICK_Lvalue_To_Rvalue:
+ case ICK_Array_To_Pointer:
+ case ICK_Function_To_Pointer:
+ case ICK_Qualification:
+ case ICK_Num_Conversion_Kinds:
assert(false && "Improper second standard conversion");
break;
}
@@ -1864,7 +1876,7 @@
break;
default:
- assert(false && "Improper second standard conversion");
+ assert(false && "Improper third standard conversion");
break;
}
@@ -2256,6 +2268,10 @@
if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy))
return LTy;
+ // Extension: conditional operator involving vector types.
+ if (LTy->isVectorType() || RTy->isVectorType())
+ return CheckVectorOperands(QuestionLoc, LHS, RHS);
+
// -- The second and third operands have arithmetic or enumeration type;
// the usual arithmetic conversions are performed to bring them to a
// common type, and the result is of that type.
Modified: cfe/trunk/lib/Sema/SemaOverload.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.cpp?rev=104081&r1=104080&r2=104081&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.cpp (original)
+++ cfe/trunk/lib/Sema/SemaOverload.cpp Tue May 18 17:42:18 2010
@@ -52,6 +52,8 @@
ICC_Conversion,
ICC_Conversion,
ICC_Conversion,
+ ICC_Conversion,
+ ICC_Conversion,
ICC_Conversion
};
return Category[(int)Kind];
@@ -80,6 +82,8 @@
ICR_Conversion,
ICR_Conversion,
ICR_Conversion,
+ ICR_Conversion,
+ ICR_Conversion,
ICR_Complex_Real_Conversion
};
return Rank[(int)Kind];
@@ -102,12 +106,14 @@
"Floating conversion",
"Complex conversion",
"Floating-integral conversion",
- "Complex-real conversion",
"Pointer conversion",
"Pointer-to-member conversion",
"Boolean conversion",
"Compatible-types conversion",
- "Derived-to-base conversion"
+ "Derived-to-base conversion",
+ "Vector conversion",
+ "Vector splat",
+ "Complex-real conversion"
};
return Name[Kind];
}
@@ -773,6 +779,48 @@
ResultTy = FromType;
return true;
}
+
+/// \brief Determine whether the conversion from FromType to ToType is a valid
+/// vector conversion.
+///
+/// \param ICK Will be set to the vector conversion kind, if this is a vector
+/// conversion.
+static bool IsVectorConversion(ASTContext &Context, QualType FromType,
+ QualType ToType, ImplicitConversionKind &ICK) {
+ // We need at least one of these types to be a vector type to have a vector
+ // conversion.
+ if (!ToType->isVectorType() && !FromType->isVectorType())
+ return false;
+
+ // Identical types require no conversions.
+ if (Context.hasSameUnqualifiedType(FromType, ToType))
+ return false;
+
+ // There are no conversions between extended vector types, only identity.
+ if (ToType->isExtVectorType()) {
+ // There are no conversions between extended vector types other than the
+ // identity conversion.
+ if (FromType->isExtVectorType())
+ return false;
+
+ // Vector splat from any arithmetic type to a vector.
+ if (!FromType->isVectorType() && FromType->isArithmeticType()) {
+ ICK = ICK_Vector_Splat;
+ return true;
+ }
+ }
+
+ // If lax vector conversions are permitted and the vector types are of the
+ // same size, we can perform the conversion.
+ if (Context.getLangOptions().LaxVectorConversions &&
+ FromType->isVectorType() && ToType->isVectorType() &&
+ Context.getTypeSize(FromType) == Context.getTypeSize(ToType)) {
+ ICK = ICK_Vector_Conversion;
+ return true;
+ }
+
+ return false;
+}
/// IsStandardConversion - Determines whether there is a standard
/// conversion sequence (C++ [conv], C++ [over.ics.scs]) from the
@@ -895,6 +943,7 @@
// For overloading in C, this can also be a "compatible-type"
// conversion.
bool IncompatibleObjC = false;
+ ImplicitConversionKind SecondICK = ICK_Identity;
if (Context.hasSameUnqualifiedType(FromType, ToType)) {
// The unqualified versions of the types are the same: there's no
// conversion to do.
@@ -956,10 +1005,14 @@
// Boolean conversions (C++ 4.12).
SCS.Second = ICK_Boolean_Conversion;
FromType = Context.BoolTy;
+ } else if (IsVectorConversion(Context, FromType, ToType, SecondICK)) {
+ SCS.Second = SecondICK;
+ FromType = ToType.getUnqualifiedType();
} else if (!getLangOptions().CPlusPlus &&
Context.typesAreCompatible(ToType, FromType)) {
// Compatible conversions (Clang extension for C function overloading)
SCS.Second = ICK_Compatible_Conversion;
+ FromType = ToType.getUnqualifiedType();
} else if (IsNoReturnConversion(Context, FromType, ToType, FromType)) {
// Treat a conversion that strips "noreturn" as an identity conversion.
SCS.Second = ICK_NoReturn_Adjustment;
Modified: cfe/trunk/lib/Sema/SemaOverload.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaOverload.h?rev=104081&r1=104080&r2=104081&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaOverload.h (original)
+++ cfe/trunk/lib/Sema/SemaOverload.h Tue May 18 17:42:18 2010
@@ -62,6 +62,8 @@
ICK_Boolean_Conversion, ///< Boolean conversions (C++ 4.12)
ICK_Compatible_Conversion, ///< Conversions between compatible types in C99
ICK_Derived_To_Base, ///< Derived-to-base (C++ [over.best.ics])
+ ICK_Vector_Conversion, ///< Vector conversions
+ ICK_Vector_Splat, ///< A vector splat from an arithmetic type
ICK_Complex_Real, ///< Complex-real conversions (C99 6.3.1.7)
ICK_Num_Conversion_Kinds ///< The number of conversion kinds
};
Added: cfe/trunk/test/SemaCXX/vector.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/vector.cpp?rev=104081&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/vector.cpp (added)
+++ cfe/trunk/test/SemaCXX/vector.cpp Tue May 18 17:42:18 2010
@@ -0,0 +1,102 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fsyntax-only -verify %s
+typedef char char16 __attribute__ ((__vector_size__ (16)));
+typedef long long longlong16 __attribute__ ((__vector_size__ (16)));
+typedef char char16_e __attribute__ ((__ext_vector_type__ (16)));
+typedef long long longlong16_e __attribute__ ((__ext_vector_type__ (2)));
+
+#if 1
+// Test overloading and function calls with vector types.
+void f0(char16);
+
+void f0_test(char16 c16, longlong16 ll16, char16_e c16e, longlong16_e ll16e) {
+ f0(c16);
+ f0(ll16);
+ f0(c16e);
+ f0(ll16e);
+}
+
+int &f1(char16); // expected-note 2{{candidate function}}
+float &f1(longlong16); // expected-note 2{{candidate function}}
+
+void f1_test(char16 c16, longlong16 ll16, char16_e c16e, longlong16_e ll16e) {
+ int &ir1 = f1(c16);
+ float &fr1 = f1(ll16);
+ f1(c16e); // expected-error{{call to 'f1' is ambiguous}}
+ f1(ll16e); // expected-error{{call to 'f1' is ambiguous}}
+}
+
+void f2(char16_e); // expected-note{{no known conversion from 'longlong16_e' to 'char16_e' for 1st argument}}
+
+void f2_test(char16 c16, longlong16 ll16, char16_e c16e, longlong16_e ll16e) {
+ f2(c16);
+ f2(ll16);
+ f2(c16e);
+ f2(ll16e); // expected-error{{no matching function}}
+ f2('a');
+ f2(17);
+}
+#endif
+
+// Test the conditional operator with vector types.
+void conditional(bool Cond, char16 c16, longlong16 ll16, char16_e c16e,
+ longlong16_e ll16e) {
+ // Conditional operators with the same type.
+ __typeof__(Cond? c16 : c16) *c16p1 = &c16;
+ __typeof__(Cond? ll16 : ll16) *ll16p1 = &ll16;
+ __typeof__(Cond? c16e : c16e) *c16ep1 = &c16e;
+ __typeof__(Cond? ll16e : ll16e) *ll16ep1 = &ll16e;
+
+ // Conditional operators with similar types.
+ __typeof__(Cond? c16 : c16e) *c16ep2 = &c16e;
+ __typeof__(Cond? c16e : c16) *c16ep3 = &c16e;
+ __typeof__(Cond? ll16 : ll16e) *ll16ep2 = &ll16e;
+ __typeof__(Cond? ll16e : ll16) *ll16ep3 = &ll16e;
+
+ // Conditional operators with incompatible types.
+ (void)(Cond? c16 : ll16); // expected-error{{can't convert between vector values}}
+ (void)(Cond? ll16e : c16e); // expected-error{{can't convert between vector values}}
+ (void)(Cond? ll16e : c16); // expected-error{{can't convert between vector values}}
+}
+
+// Test C++ cast'ing of vector types.
+void casts(longlong16 ll16, longlong16_e ll16e) {
+ // C-style casts.
+ (void)(char16)ll16;
+ (void)(char16_e)ll16;
+ (void)(longlong16)ll16;
+ (void)(longlong16_e)ll16;
+ (void)(char16)ll16e;
+ (void)(char16_e)ll16e;
+ (void)(longlong16)ll16e;
+ (void)(longlong16_e)ll16e;
+
+ // Function-style casts.
+ (void)char16(ll16);
+ (void)char16_e(ll16);
+ (void)longlong16(ll16);
+ (void)longlong16_e(ll16);
+ (void)char16(ll16e);
+ (void)char16_e(ll16e);
+ (void)longlong16(ll16e);
+ (void)longlong16_e(ll16e);
+
+ // static_cast
+ (void)static_cast<char16>(ll16);
+ (void)static_cast<char16_e>(ll16);
+ (void)static_cast<longlong16>(ll16);
+ (void)static_cast<longlong16_e>(ll16);
+ (void)static_cast<char16>(ll16e);
+ (void)static_cast<char16_e>(ll16e); // expected-error{{static_cast from 'longlong16_e' to 'char16_e' is not allowed}}
+ (void)static_cast<longlong16>(ll16e);
+ (void)static_cast<longlong16_e>(ll16e);
+
+ // reinterpret_cast
+ (void)reinterpret_cast<char16>(ll16);
+ (void)reinterpret_cast<char16_e>(ll16);
+ (void)reinterpret_cast<longlong16>(ll16);
+ (void)reinterpret_cast<longlong16_e>(ll16);
+ (void)reinterpret_cast<char16>(ll16e);
+ (void)reinterpret_cast<char16_e>(ll16e);
+ (void)reinterpret_cast<longlong16>(ll16e);
+ (void)reinterpret_cast<longlong16_e>(ll16e);
+}
More information about the cfe-commits
mailing list