From 8c592941d57097bc0ddf91ee2a97939873a9ed5a Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 4 Jun 2019 18:30:46 +0000 Subject: [PATCH] Factor out repeated code to build a DeclRefExpr and mark it referenced. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@362537 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 18 ++++++++++----- lib/Sema/SemaDeclCXX.cpp | 4 ++-- lib/Sema/SemaExpr.cpp | 46 +++++++++++++++++++-------------------- lib/Sema/SemaExprCXX.cpp | 9 +++----- lib/Sema/SemaOverload.cpp | 30 +++++++------------------ 5 files changed, 48 insertions(+), 59 deletions(-) diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index ed41143161..74a1a28a39 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -4305,16 +4305,24 @@ public: bool isAddressOfOperand, const TemplateArgumentListInfo *TemplateArgs); - ExprResult BuildDeclRefExpr(ValueDecl *D, QualType Ty, - ExprValueKind VK, - SourceLocation Loc, - const CXXScopeSpec *SS = nullptr); - ExprResult + DeclRefExpr *BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, + SourceLocation Loc, + const CXXScopeSpec *SS = nullptr); + DeclRefExpr * BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, const DeclarationNameInfo &NameInfo, const CXXScopeSpec *SS = nullptr, NamedDecl *FoundD = nullptr, + SourceLocation TemplateKWLoc = SourceLocation(), + const TemplateArgumentListInfo *TemplateArgs = nullptr); + DeclRefExpr * + BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, + const DeclarationNameInfo &NameInfo, + NestedNameSpecifierLoc NNS, + NamedDecl *FoundD = nullptr, + SourceLocation TemplateKWLoc = SourceLocation(), const TemplateArgumentListInfo *TemplateArgs = nullptr); + ExprResult BuildAnonymousStructUnionMemberReference( const CXXScopeSpec &SS, diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index 0956aff21e..354316a3cc 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -11475,7 +11475,7 @@ class RefBuilder: public ExprBuilder { public: Expr *build(Sema &S, SourceLocation Loc) const override { - return assertNotNull(S.BuildDeclRefExpr(Var, VarType, VK_LValue, Loc).get()); + return assertNotNull(S.BuildDeclRefExpr(Var, VarType, VK_LValue, Loc)); } RefBuilder(VarDecl *Var, QualType VarType) @@ -12877,7 +12877,7 @@ void Sema::DefineImplicitLambdaToFunctionPointerConversion( // Construct the body of the conversion function { return __invoke; }. Expr *FunctionRef = BuildDeclRefExpr(Invoker, Invoker->getType(), - VK_LValue, Conv->getLocation()).get(); + VK_LValue, Conv->getLocation()); assert(FunctionRef && "Can't refer to __invoke function?"); Stmt *Return = BuildReturnStmt(Conv->getLocation(), FunctionRef).get(); Conv->setBody(CompoundStmt::Create(Context, Return, Conv->getLocation(), diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 1e9164bc3a..7eb5bcbbbb 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1762,7 +1762,7 @@ Sema::ActOnStringLiteral(ArrayRef StringToks, Scope *UDLScope) { llvm_unreachable("unexpected literal operator lookup result"); } -ExprResult +DeclRefExpr * Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, SourceLocation Loc, const CXXScopeSpec *SS) { @@ -1770,36 +1770,33 @@ Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); } +DeclRefExpr * +Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, + const DeclarationNameInfo &NameInfo, + const CXXScopeSpec *SS, NamedDecl *FoundD, + SourceLocation TemplateKWLoc, + const TemplateArgumentListInfo *TemplateArgs) { + NestedNameSpecifierLoc NNS = + SS ? SS->getWithLocInContext(Context) : NestedNameSpecifierLoc(); + return BuildDeclRefExpr(D, Ty, VK, NameInfo, NNS, FoundD, TemplateKWLoc, + TemplateArgs); +} + /// BuildDeclRefExpr - Build an expression that references a /// declaration that does not require a closure capture. -ExprResult +DeclRefExpr * Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, const DeclarationNameInfo &NameInfo, - const CXXScopeSpec *SS, NamedDecl *FoundD, + NestedNameSpecifierLoc NNS, NamedDecl *FoundD, + SourceLocation TemplateKWLoc, const TemplateArgumentListInfo *TemplateArgs) { bool RefersToCapturedVariable = isa(D) && NeedToCaptureVariable(cast(D), NameInfo.getLoc()); - DeclRefExpr *E; - if (isa(D)) { - VarTemplateSpecializationDecl *VarSpec = - cast(D); - - E = DeclRefExpr::Create(Context, SS ? SS->getWithLocInContext(Context) - : NestedNameSpecifierLoc(), - VarSpec->getTemplateKeywordLoc(), D, - RefersToCapturedVariable, NameInfo.getLoc(), Ty, VK, - FoundD, TemplateArgs); - } else { - assert(!TemplateArgs && "No template arguments for non-variable" - " template specialization references"); - E = DeclRefExpr::Create(Context, SS ? SS->getWithLocInContext(Context) - : NestedNameSpecifierLoc(), - SourceLocation(), D, RefersToCapturedVariable, - NameInfo, Ty, VK, FoundD); - } - + DeclRefExpr *E = DeclRefExpr::Create(Context, NNS, TemplateKWLoc, D, + RefersToCapturedVariable, NameInfo, Ty, + VK, FoundD, TemplateArgs); MarkDeclRefReferenced(E); if (getLangOpts().ObjCWeak && isa(D) && @@ -3141,6 +3138,7 @@ ExprResult Sema::BuildDeclarationNameExpr( } return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS, FoundD, + /*FIXME: TemplateKWLoc*/ SourceLocation(), TemplateArgs); } } @@ -5615,8 +5613,8 @@ ExprResult Sema::BuildCallExpr(Scope *Scope, Expr *Fn, SourceLocation LParenLoc, } } - if (isa(NakedFn)) { - NDecl = cast(NakedFn)->getDecl(); + if (auto *DRE = dyn_cast(NakedFn)) { + NDecl = DRE->getDecl(); FunctionDecl *FDecl = dyn_cast(NDecl); if (FDecl && FDecl->getBuiltinID()) { diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 5884cf906f..1eb6a7114f 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -3633,12 +3633,9 @@ ExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar, diag::err_invalid_use_of_array_type) << ConditionVar->getSourceRange()); - ExprResult Condition = DeclRefExpr::Create( - Context, NestedNameSpecifierLoc(), SourceLocation(), ConditionVar, - /*enclosing*/ false, ConditionVar->getLocation(), - ConditionVar->getType().getNonReferenceType(), VK_LValue); - - MarkDeclRefReferenced(cast(Condition.get())); + ExprResult Condition = BuildDeclRefExpr( + ConditionVar, ConditionVar->getType().getNonReferenceType(), VK_LValue, + ConditionVar->getLocation()); switch (CK) { case ConditionKind::Boolean: diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index e5cbd1d0a8..30d809ac91 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -13864,17 +13864,10 @@ Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, TemplateArgs = &TemplateArgsBuffer; } - DeclRefExpr *DRE = DeclRefExpr::Create(Context, - ULE->getQualifierLoc(), - ULE->getTemplateKeywordLoc(), - Fn, - /*enclosing*/ false, // FIXME? - ULE->getNameLoc(), - Fn->getType(), - VK_LValue, - Found.getDecl(), - TemplateArgs); - MarkDeclRefReferenced(DRE); + DeclRefExpr *DRE = + BuildDeclRefExpr(Fn, Fn->getType(), VK_LValue, ULE->getNameInfo(), + ULE->getQualifierLoc(), Found.getDecl(), + ULE->getTemplateKeywordLoc(), TemplateArgs); DRE->setHadMultipleCandidates(ULE->getNumDecls() > 1); return DRE; } @@ -13893,17 +13886,10 @@ Expr *Sema::FixOverloadedFunctionReference(Expr *E, DeclAccessPair Found, // implicit member access, rewrite to a simple decl ref. if (MemExpr->isImplicitAccess()) { if (cast(Fn)->isStatic()) { - DeclRefExpr *DRE = DeclRefExpr::Create(Context, - MemExpr->getQualifierLoc(), - MemExpr->getTemplateKeywordLoc(), - Fn, - /*enclosing*/ false, - MemExpr->getMemberLoc(), - Fn->getType(), - VK_LValue, - Found.getDecl(), - TemplateArgs); - MarkDeclRefReferenced(DRE); + DeclRefExpr *DRE = BuildDeclRefExpr( + Fn, Fn->getType(), VK_LValue, MemExpr->getNameInfo(), + MemExpr->getQualifierLoc(), Found.getDecl(), + MemExpr->getTemplateKeywordLoc(), TemplateArgs); DRE->setHadMultipleCandidates(MemExpr->getNumDecls() > 1); return DRE; } else { -- 2.40.0