Skip to content

Commit f52d29c

Browse files
[Clang][Sema] Refactor handling of vector subscript expressions (NFC) (#92778)
1 parent 7c19058 commit f52d29c

File tree

2 files changed

+19
-30
lines changed

2 files changed

+19
-30
lines changed

clang/include/clang/AST/Type.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,7 @@ class alignas(TypeAlignment) Type : public ExtQualsTypeCommonBase {
25232523
bool isVectorType() const; // GCC vector type.
25242524
bool isExtVectorType() const; // Extended vector type.
25252525
bool isExtVectorBoolType() const; // Extended vector type with bool element.
2526+
bool isSubscriptableVectorType() const;
25262527
bool isMatrixType() const; // Matrix type.
25272528
bool isConstantMatrixType() const; // Constant matrix type.
25282529
bool isDependentAddressSpaceType() const; // value-dependent address space qualifier
@@ -7729,6 +7730,10 @@ inline bool Type::isExtVectorBoolType() const {
77297730
return cast<ExtVectorType>(CanonicalType)->getElementType()->isBooleanType();
77307731
}
77317732

7733+
inline bool Type::isSubscriptableVectorType() const {
7734+
return isVectorType() || isSveVLSBuiltinType();
7735+
}
7736+
77327737
inline bool Type::isMatrixType() const {
77337738
return isa<MatrixType>(CanonicalType);
77347739
}

clang/lib/Sema/SemaExpr.cpp

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5241,36 +5241,22 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc,
52415241
<< ResultType << BaseExpr->getSourceRange();
52425242
return ExprError();
52435243
}
5244-
} else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) {
5245-
BaseExpr = LHSExp; // vectors: V[123]
5246-
IndexExpr = RHSExp;
5247-
// We apply C++ DR1213 to vector subscripting too.
5248-
if (getLangOpts().CPlusPlus11 && LHSExp->isPRValue()) {
5249-
ExprResult Materialized = TemporaryMaterializationConversion(LHSExp);
5250-
if (Materialized.isInvalid())
5251-
return ExprError();
5252-
LHSExp = Materialized.get();
5244+
} else if (LHSTy->isSubscriptableVectorType()) {
5245+
if (LHSTy->isBuiltinType() &&
5246+
LHSTy->getAs<BuiltinType>()->isSveVLSBuiltinType()) {
5247+
const BuiltinType *BTy = LHSTy->getAs<BuiltinType>();
5248+
if (BTy->isSVEBool())
5249+
return ExprError(Diag(LLoc, diag::err_subscript_svbool_t)
5250+
<< LHSExp->getSourceRange()
5251+
<< RHSExp->getSourceRange());
5252+
ResultType = BTy->getSveEltType(Context);
5253+
} else {
5254+
const VectorType *VTy = LHSTy->getAs<VectorType>();
5255+
ResultType = VTy->getElementType();
52535256
}
5254-
VK = LHSExp->getValueKind();
5255-
if (VK != VK_PRValue)
5256-
OK = OK_VectorComponent;
5257-
5258-
ResultType = VTy->getElementType();
5259-
QualType BaseType = BaseExpr->getType();
5260-
Qualifiers BaseQuals = BaseType.getQualifiers();
5261-
Qualifiers MemberQuals = ResultType.getQualifiers();
5262-
Qualifiers Combined = BaseQuals + MemberQuals;
5263-
if (Combined != MemberQuals)
5264-
ResultType = Context.getQualifiedType(ResultType, Combined);
5265-
} else if (LHSTy->isBuiltinType() &&
5266-
LHSTy->getAs<BuiltinType>()->isSveVLSBuiltinType()) {
5267-
const BuiltinType *BTy = LHSTy->getAs<BuiltinType>();
5268-
if (BTy->isSVEBool())
5269-
return ExprError(Diag(LLoc, diag::err_subscript_svbool_t)
5270-
<< LHSExp->getSourceRange() << RHSExp->getSourceRange());
5271-
5272-
BaseExpr = LHSExp;
5257+
BaseExpr = LHSExp; // vectors: V[123]
52735258
IndexExpr = RHSExp;
5259+
// We apply C++ DR1213 to vector subscripting too.
52745260
if (getLangOpts().CPlusPlus11 && LHSExp->isPRValue()) {
52755261
ExprResult Materialized = TemporaryMaterializationConversion(LHSExp);
52765262
if (Materialized.isInvalid())
@@ -5281,8 +5267,6 @@ Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc,
52815267
if (VK != VK_PRValue)
52825268
OK = OK_VectorComponent;
52835269

5284-
ResultType = BTy->getSveEltType(Context);
5285-
52865270
QualType BaseType = BaseExpr->getType();
52875271
Qualifiers BaseQuals = BaseType.getQualifiers();
52885272
Qualifiers MemberQuals = ResultType.getQualifiers();

0 commit comments

Comments
 (0)