Skip to content

Commit 0c7cd4a

Browse files
committed
[clang] NFC: refactor multiple implementations of getDecltypeForParenthesizedExpr
This cleanup patch refactors a bunch of functional duplicates of getDecltypeForParenthesizedExpr into a common implementation. Signed-off-by: Matheus Izvekov <[email protected]> Reviewed By: aaronpuchert Differential Revision: https://reviews.llvm.org/D100713
1 parent 6e9e4b5 commit 0c7cd4a

File tree

9 files changed

+33
-73
lines changed

9 files changed

+33
-73
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
16311631
QualType getTypeOfExprType(Expr *e) const;
16321632
QualType getTypeOfType(QualType t) const;
16331633

1634+
QualType getReferenceQualifiedType(const Expr *e) const;
1635+
16341636
/// C++11 decltype.
16351637
QualType getDecltypeType(Expr *e, QualType UnderlyingType) const;
16361638

clang/include/clang/Sema/Sema.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2330,7 +2330,6 @@ class Sema final {
23302330
const CXXScopeSpec &SS, QualType T,
23312331
TagDecl *OwnedTagDecl = nullptr);
23322332

2333-
QualType getDecltypeForParenthesizedExpr(Expr *E);
23342333
QualType BuildTypeofExprType(Expr *E, SourceLocation Loc);
23352334
/// If AsUnevaluated is false, E is treated as though it were an evaluated
23362335
/// context, such as when building a type for decltype(auto).

clang/lib/AST/ASTContext.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5454,6 +5454,29 @@ QualType ASTContext::getTypeOfType(QualType tofType) const {
54545454
return QualType(tot, 0);
54555455
}
54565456

5457+
/// getReferenceQualifiedType - Given an expr, will return the type for
5458+
/// that expression, as in [dcl.type.simple]p4 but without taking id-expressions
5459+
/// and class member access into account.
5460+
QualType ASTContext::getReferenceQualifiedType(const Expr *E) const {
5461+
// C++11 [dcl.type.simple]p4:
5462+
// [...]
5463+
QualType T = E->getType();
5464+
switch (E->getValueKind()) {
5465+
// - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
5466+
// type of e;
5467+
case VK_XValue:
5468+
return getRValueReferenceType(T);
5469+
// - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
5470+
// type of e;
5471+
case VK_LValue:
5472+
return getLValueReferenceType(T);
5473+
// - otherwise, decltype(e) is the type of e.
5474+
case VK_PRValue:
5475+
return T;
5476+
}
5477+
llvm_unreachable("Unknown value kind");
5478+
}
5479+
54575480
/// Unlike many "get<Type>" functions, we don't unique DecltypeType
54585481
/// nodes. This would never be helpful, since each such type has its own
54595482
/// expression, and would not give a significant memory saving, since there

clang/lib/AST/ExprObjC.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -271,20 +271,7 @@ QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const {
271271
}
272272
return QT;
273273
}
274-
275-
// Expression type might be different from an expected call return type,
276-
// as expression type would never be a reference even if call returns a
277-
// reference. Reconstruct the original expression type.
278-
QualType QT = getType();
279-
switch (getValueKind()) {
280-
case VK_LValue:
281-
return Ctx.getLValueReferenceType(QT);
282-
case VK_XValue:
283-
return Ctx.getRValueReferenceType(QT);
284-
case VK_PRValue:
285-
return QT;
286-
}
287-
llvm_unreachable("Unsupported ExprValueKind");
274+
return Ctx.getReferenceQualifiedType(this);
288275
}
289276

290277
SourceRange ObjCMessageExpr::getReceiverRange() const {

clang/lib/Sema/SemaConcept.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static void diagnoseUnsatisfiedRequirement(Sema &S,
461461
Expr *e = Req->getExpr();
462462
S.Diag(e->getBeginLoc(),
463463
diag::note_expr_requirement_constraints_not_satisfied_simple)
464-
<< (int)First << S.getDecltypeForParenthesizedExpr(e)
464+
<< (int)First << S.Context.getReferenceQualifiedType(e)
465465
<< ConstraintExpr->getNamedConcept();
466466
} else {
467467
S.Diag(ConstraintExpr->getBeginLoc(),

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19402,14 +19402,7 @@ ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) {
1940219402
if (ParamTypes.empty() && Proto->isVariadic()) { // the special case
1940319403
ArgTypes.reserve(E->getNumArgs());
1940419404
for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
19405-
Expr *Arg = E->getArg(i);
19406-
QualType ArgType = Arg->getType();
19407-
if (E->isLValue()) {
19408-
ArgType = S.Context.getLValueReferenceType(ArgType);
19409-
} else if (E->isXValue()) {
19410-
ArgType = S.Context.getRValueReferenceType(ArgType);
19411-
}
19412-
ArgTypes.push_back(ArgType);
19405+
ArgTypes.push_back(S.Context.getReferenceQualifiedType(E->getArg(i)));
1941319406
}
1941419407
ParamTypes = ArgTypes;
1941519408
}

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5891,10 +5891,8 @@ static bool TryClassUnification(Sema &Self, Expr *From, Expr *To,
58915891
// -- If E2 is an xvalue: E1 can be converted to match E2 if E1 can be
58925892
// implicitly converted to the type "rvalue reference to R2", subject to
58935893
// the constraint that the reference must bind directly.
5894-
if (To->isLValue() || To->isXValue()) {
5895-
QualType T = To->isLValue() ? Self.Context.getLValueReferenceType(ToType)
5896-
: Self.Context.getRValueReferenceType(ToType);
5897-
5894+
if (To->isGLValue()) {
5895+
QualType T = Self.Context.getReferenceQualifiedType(To);
58985896
InitializedEntity Entity = InitializedEntity::InitializeTemporary(T);
58995897

59005898
InitializationSequence InitSeq(Self, Entity, Kind, From);
@@ -8743,7 +8741,7 @@ Sema::BuildExprRequirement(
87438741
TemplateParameterList *TPL =
87448742
ReturnTypeRequirement.getTypeConstraintTemplateParameterList();
87458743
QualType MatchedType =
8746-
getDecltypeForParenthesizedExpr(E).getCanonicalType();
8744+
Context.getReferenceQualifiedType(E).getCanonicalType();
87478745
llvm::SmallVector<TemplateArgument, 1> Args;
87488746
Args.push_back(TemplateArgument(MatchedType));
87498747
TemplateArgumentList TAL(TemplateArgumentList::OnStack, Args);

clang/lib/Sema/SemaType.cpp

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8891,29 +8891,6 @@ QualType Sema::BuildTypeofExprType(Expr *E, SourceLocation Loc) {
88918891
return Context.getTypeOfExprType(E);
88928892
}
88938893

8894-
/// getDecltypeForParenthesizedExpr - Given an expr, will return the type for
8895-
/// that expression, as in [dcl.type.simple]p4 but without taking id-expressions
8896-
/// and class member access into account.
8897-
QualType Sema::getDecltypeForParenthesizedExpr(Expr *E) {
8898-
// C++11 [dcl.type.simple]p4:
8899-
// [...]
8900-
QualType T = E->getType();
8901-
switch (E->getValueKind()) {
8902-
// - otherwise, if e is an xvalue, decltype(e) is T&&, where T is the
8903-
// type of e;
8904-
case VK_XValue:
8905-
return Context.getRValueReferenceType(T);
8906-
// - otherwise, if e is an lvalue, decltype(e) is T&, where T is the
8907-
// type of e;
8908-
case VK_LValue:
8909-
return Context.getLValueReferenceType(T);
8910-
// - otherwise, decltype(e) is the type of e.
8911-
case VK_PRValue:
8912-
return T;
8913-
}
8914-
llvm_unreachable("Unknown value kind");
8915-
}
8916-
89178894
/// getDecltypeForExpr - Given an expr, will return the decltype for
89188895
/// that expression, according to the rules in C++11
89198896
/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
@@ -8983,7 +8960,7 @@ static QualType getDecltypeForExpr(Sema &S, Expr *E) {
89838960
}
89848961
}
89858962

8986-
return S.getDecltypeForParenthesizedExpr(E);
8963+
return S.Context.getReferenceQualifiedType(E);
89878964
}
89888965

89898966
QualType Sema::BuildDecltypeType(Expr *E, SourceLocation Loc,

clang/lib/StaticAnalyzer/Core/CallEvent.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,7 @@ QualType CallEvent::getResultType() const {
7373
const Expr *E = getOriginExpr();
7474
if (!E)
7575
return Ctx.VoidTy;
76-
assert(E);
77-
78-
QualType ResultTy = E->getType();
79-
80-
// A function that returns a reference to 'int' will have a result type
81-
// of simply 'int'. Check the origin expr's value kind to recover the
82-
// proper type.
83-
switch (E->getValueKind()) {
84-
case VK_LValue:
85-
ResultTy = Ctx.getLValueReferenceType(ResultTy);
86-
break;
87-
case VK_XValue:
88-
ResultTy = Ctx.getRValueReferenceType(ResultTy);
89-
break;
90-
case VK_PRValue:
91-
// No adjustment is necessary.
92-
break;
93-
}
94-
95-
return ResultTy;
76+
return Ctx.getReferenceQualifiedType(E);
9677
}
9778

9879
static bool isCallback(QualType T) {

0 commit comments

Comments
 (0)