Factor out repeated code to build 'this' expressions and mark them

referenced.

llvm-svn: 361588
This commit is contained in:
Richard Smith 2019-05-24 01:35:07 +00:00
parent 1293de8b17
commit 8458c9ef42
5 changed files with 25 additions and 15 deletions

View File

@ -5223,6 +5223,10 @@ public:
//// ActOnCXXThis - Parse 'this' pointer. //// ActOnCXXThis - Parse 'this' pointer.
ExprResult ActOnCXXThis(SourceLocation loc); ExprResult ActOnCXXThis(SourceLocation loc);
/// Build a CXXThisExpr and mark it referenced in the current context.
Expr *BuildCXXThisExpr(SourceLocation Loc, QualType Type, bool IsImplicit);
void MarkThisReferenced(CXXThisExpr *This);
/// Try to retrieve the type of the 'this' pointer. /// Try to retrieve the type of the 'this' pointer.
/// ///
/// \returns The type of 'this', if possible. Otherwise, returns a NULL type. /// \returns The type of 'this', if possible. Otherwise, returns a NULL type.

View File

@ -1298,10 +1298,20 @@ ExprResult Sema::ActOnCXXThis(SourceLocation Loc) {
/// which the function is called. /// which the function is called.
QualType ThisTy = getCurrentThisType(); QualType ThisTy = getCurrentThisType();
if (ThisTy.isNull()) return Diag(Loc, diag::err_invalid_this_use); if (ThisTy.isNull())
return Diag(Loc, diag::err_invalid_this_use);
return BuildCXXThisExpr(Loc, ThisTy, /*isImplicit=*/false);
}
CheckCXXThisCapture(Loc); Expr *Sema::BuildCXXThisExpr(SourceLocation Loc, QualType Type,
return new (Context) CXXThisExpr(Loc, ThisTy, /*isImplicit=*/false); bool IsImplicit) {
auto *This = new (Context) CXXThisExpr(Loc, Type, IsImplicit);
MarkThisReferenced(This);
return This;
}
void Sema::MarkThisReferenced(CXXThisExpr *This) {
CheckCXXThisCapture(This->getExprLoc());
} }
bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) { bool Sema::isThisOutsideMemberFunctionBody(QualType BaseType) {

View File

@ -1092,8 +1092,7 @@ Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType,
SourceLocation Loc = R.getNameLoc(); SourceLocation Loc = R.getNameLoc();
if (SS.getRange().isValid()) if (SS.getRange().isValid())
Loc = SS.getRange().getBegin(); Loc = SS.getRange().getBegin();
CheckCXXThisCapture(Loc); BaseExpr = BuildCXXThisExpr(Loc, BaseExprType, /*isImplicit=*/true);
BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true);
} }
// Check the use of this member. // Check the use of this member.
@ -1836,8 +1835,7 @@ Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS,
SourceLocation Loc = R.getNameLoc(); SourceLocation Loc = R.getNameLoc();
if (SS.getRange().isValid()) if (SS.getRange().isValid())
Loc = SS.getRange().getBegin(); Loc = SS.getRange().getBegin();
CheckCXXThisCapture(Loc); baseExpr = BuildCXXThisExpr(loc, ThisTy, /*isImplicit=*/true);
baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true);
} }
return BuildMemberReferenceExpr(baseExpr, ThisTy, return BuildMemberReferenceExpr(baseExpr, ThisTy,

View File

@ -13910,10 +13910,8 @@ Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found,
SourceLocation Loc = MemExpr->getMemberLoc(); SourceLocation Loc = MemExpr->getMemberLoc();
if (MemExpr->getQualifier()) if (MemExpr->getQualifier())
Loc = MemExpr->getQualifierLoc().getBeginLoc(); Loc = MemExpr->getQualifierLoc().getBeginLoc();
CheckCXXThisCapture(Loc); Base =
Base = new (Context) CXXThisExpr(Loc, BuildCXXThisExpr(Loc, MemExpr->getBaseType(), /*isImplicit=*/true);
MemExpr->getBaseType(),
/*isImplicit=*/true);
} }
} else } else
Base = MemExpr->getBase(); Base = MemExpr->getBase();

View File

@ -2697,8 +2697,7 @@ public:
ExprResult RebuildCXXThisExpr(SourceLocation ThisLoc, ExprResult RebuildCXXThisExpr(SourceLocation ThisLoc,
QualType ThisType, QualType ThisType,
bool isImplicit) { bool isImplicit) {
getSema().CheckCXXThisCapture(ThisLoc); return getSema().BuildCXXThisExpr(ThisLoc, ThisType, isImplicit);
return new (getSema().Context) CXXThisExpr(ThisLoc, ThisType, isImplicit);
} }
/// Build a new C++ throw expression. /// Build a new C++ throw expression.
@ -10355,8 +10354,9 @@ TreeTransform<Derived>::TransformCXXThisExpr(CXXThisExpr *E) {
QualType T = getSema().getCurrentThisType(); QualType T = getSema().getCurrentThisType();
if (!getDerived().AlwaysRebuild() && T == E->getType()) { if (!getDerived().AlwaysRebuild() && T == E->getType()) {
// Make sure that we capture 'this'. // Mark it referenced in the new context regardless.
getSema().CheckCXXThisCapture(E->getBeginLoc()); // FIXME: this is a bit instantiation-specific.
getSema().MarkThisReferenced(E);
return E; return E;
} }