From 1af391df138f39decde88b9517c741496fed7dd8 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Fri, 11 Mar 2016 18:59:12 +0000 Subject: Allow sizeof(UnrelatedClass::field) in C++11 class template methods This feature works outside of templates by forming a DeclRefExpr to a FieldDecl instead of a MemberExpr, which requires a base object in addition to the FieldDecl. Previously, while building up the template AST before instantiation, we formed a CXXDependentScopeMemberExpr, which always instantiates to a MemberExpr. Now, in unevaluated contexts we form a DependentScopeDeclRefExpr, which is a more flexible node that can instantiate to either a MemberExpr or a DeclRefExpr depending on lookup results. Fixes PR26893. llvm-svn: 263279 --- clang/lib/Sema/SemaTemplate.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'clang/lib/Sema/SemaTemplate.cpp') diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 3b85b98abdd..0b4b083ffbd 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -414,9 +414,22 @@ Sema::ActOnDependentIdExpression(const CXXScopeSpec &SS, const TemplateArgumentListInfo *TemplateArgs) { DeclContext *DC = getFunctionLevelDeclContext(); - if (!isAddressOfOperand && - isa(DC) && - cast(DC)->isInstance()) { + // C++11 [expr.prim.general]p12: + // An id-expression that denotes a non-static data member or non-static + // member function of a class can only be used: + // (...) + // - if that id-expression denotes a non-static data member and it + // appears in an unevaluated operand. + // + // If this might be the case, form a DependentScopeDeclRefExpr instead of a + // CXXDependentScopeMemberExpr. The former can instantiate to either + // DeclRefExpr or MemberExpr depending on lookup results, while the latter is + // always a MemberExpr. + bool MightBeCxx11UnevalField = + getLangOpts().CPlusPlus11 && isUnevaluatedContext(); + + if (!MightBeCxx11UnevalField && !isAddressOfOperand && + isa(DC) && cast(DC)->isInstance()) { QualType ThisType = cast(DC)->getThisType(Context); // Since the 'this' expression is synthesized, we don't need to -- cgit v1.2.3