From 54d0e05c9eb208e28db0ef10822c6d9f01395eb1 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 27 Jun 2018 20:29:32 +0000 Subject: [PATCH] DR1213: Ignore implicit conversions when determining if an operand of an array subscript expression is an array prvalue. Also apply DR1213 to vector prvalues for consistency. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@335779 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExpr.cpp | 18 ++++++++++++++---- test/CXX/drs/dr12xx.cpp | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 88059e9466..74f84862e6 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -4385,10 +4385,13 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, // Per C++ core issue 1213, the result is an xvalue if either operand is // a non-lvalue array, and an lvalue otherwise. - if (getLangOpts().CPlusPlus11 && - ((LHSExp->getType()->isArrayType() && !LHSExp->isLValue()) || - (RHSExp->getType()->isArrayType() && !RHSExp->isLValue()))) - VK = VK_XValue; + if (getLangOpts().CPlusPlus11) { + for (auto *Op : {LHSExp, RHSExp}) { + Op = Op->IgnoreImplicit(); + if (Op->getType()->isArrayType() && !Op->isLValue()) + VK = VK_XValue; + } + } // Perform default conversions. if (!LHSExp->getType()->getAs()) { @@ -4449,6 +4452,13 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, } else if (const VectorType *VTy = LHSTy->getAs()) { BaseExpr = LHSExp; // vectors: V[123] IndexExpr = RHSExp; + // We apply C++ DR1213 to vector subscripting too. + if (getLangOpts().CPlusPlus11 && LHSExp->getValueKind() == VK_RValue) { + ExprResult Materialized = TemporaryMaterializationConversion(LHSExp); + if (Materialized.isInvalid()) + return ExprError(); + LHSExp = Materialized.get(); + } VK = LHSExp->getValueKind(); if (VK != VK_RValue) OK = OK_VectorComponent; diff --git a/test/CXX/drs/dr12xx.cpp b/test/CXX/drs/dr12xx.cpp index 24039a1cd2..1bc4c39734 100644 --- a/test/CXX/drs/dr12xx.cpp +++ b/test/CXX/drs/dr12xx.cpp @@ -3,7 +3,7 @@ // RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors // RUN: %clang_cc1 -std=c++1z %s -verify -fexceptions -fcxx-exceptions -pedantic-errors -namespace dr1213 { // dr1213: 4 +namespace dr1213 { // dr1213: 7 #if __cplusplus >= 201103L using T = int[3]; int &&r = T{}[1]; @@ -11,6 +11,19 @@ namespace dr1213 { // dr1213: 4 using T = decltype((T{})); using U = decltype((T{}[2])); using U = int &&; + + // Same thing but in a case where we consider overloaded operator[]. + struct ConvertsToInt { + operator int(); + }; + struct X { int array[1]; }; + using U = decltype(X().array[ConvertsToInt()]); + + // We apply the same rule to vector subscripting. + typedef int V4Int __attribute__((__vector_size__(sizeof(int) * 4))); + typedef int EV4Int __attribute__((__ext_vector_type__(4))); + using U = decltype(V4Int()[0]); + using U = decltype(EV4Int()[0]); #endif } -- 2.40.0