From adb1d4c18ee83249d4cffc99ef902f98e846092a Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Sun, 22 Jul 2012 23:45:10 +0000 Subject: [PATCH] PR12917: Remove incorrect assumption that lambda mangling information cannot change once it's been assigned. It can change in two ways: 1) In a template instantiation, the context declaration should be the instantiated declaration, not the declaration in the template. 2) If a lambda appears in the pattern of a variadic pack expansion, the mangling number will depend on the pack length. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@160614 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Sema/Sema.h | 5 +- lib/Sema/SemaExpr.cpp | 3 + lib/Sema/SemaLambda.cpp | 126 ++++++++++++----------- lib/Sema/SemaTemplateInstantiateDecl.cpp | 4 +- lib/Sema/TreeTransform.h | 8 +- test/CodeGenCXX/mangle-lambdas.cpp | 35 +++++-- 6 files changed, 105 insertions(+), 76 deletions(-) diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 6fa762da70..246a963082 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -4014,10 +4014,7 @@ public: SourceRange IntroducerRange, TypeSourceInfo *MethodType, SourceLocation EndLoc, - llvm::ArrayRef Params, - llvm::Optional ManglingNumber - = llvm::Optional(), - Decl *ContextDecl = 0); + llvm::ArrayRef Params); /// \brief Introduce the scope for a lambda expression. sema::LambdaScopeInfo *enterLambdaScope(CXXMethodDecl *CallOperator, diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index f2e50ba77b..9b5f003dae 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -3380,6 +3380,9 @@ ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, if (Param->hasUninstantiatedDefaultArg()) { Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); + EnterExpressionEvaluationContext EvalContext(*this, PotentiallyEvaluated, + Param); + // Instantiate the expression. MultiLevelTemplateArgumentList ArgList = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); diff --git a/lib/Sema/SemaLambda.cpp b/lib/Sema/SemaLambda.cpp index ab5640c2be..25d27f44ee 100644 --- a/lib/Sema/SemaLambda.cpp +++ b/lib/Sema/SemaLambda.cpp @@ -54,9 +54,7 @@ CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class, SourceRange IntroducerRange, TypeSourceInfo *MethodType, SourceLocation EndLoc, - llvm::ArrayRef Params, - llvm::Optional ManglingNumber, - Decl *ContextDecl) { + llvm::ArrayRef Params) { // C++11 [expr.prim.lambda]p5: // The closure type for a lambda-expression has a public inline function // call operator (13.5.4) whose parameters and return type are described by @@ -98,64 +96,76 @@ CXXMethodDecl *Sema::startLambdaDefinition(CXXRecordDecl *Class, P != PEnd; ++P) (*P)->setOwningFunction(Method); } - - // If we don't already have a mangling number for this lambda expression, - // allocate one now. - if (!ManglingNumber) { - ContextDecl = ExprEvalContexts.back().LambdaContextDecl; - - enum ContextKind { - Normal, - DefaultArgument, - DataMember, - StaticDataMember - } Kind = Normal; - - // Default arguments of member function parameters that appear in a class - // definition, as well as the initializers of data members, receive special - // treatment. Identify them. - if (ContextDecl) { - if (ParmVarDecl *Param = dyn_cast(ContextDecl)) { - if (const DeclContext *LexicalDC - = Param->getDeclContext()->getLexicalParent()) - if (LexicalDC->isRecord()) - Kind = DefaultArgument; - } else if (VarDecl *Var = dyn_cast(ContextDecl)) { - if (Var->getDeclContext()->isRecord()) - Kind = StaticDataMember; - } else if (isa(ContextDecl)) { - Kind = DataMember; - } - } - - switch (Kind) { - case Normal: - if (CurContext->isDependentContext() || isInInlineFunction(CurContext)) - ManglingNumber = Context.getLambdaManglingNumber(Method); - else - ManglingNumber = 0; - - // There is no special context for this lambda. - ContextDecl = 0; - break; - - case StaticDataMember: - if (!CurContext->isDependentContext()) { - ManglingNumber = 0; - ContextDecl = 0; - break; - } - // Fall through to assign a mangling number. - - case DataMember: - case DefaultArgument: - ManglingNumber = ExprEvalContexts.back().getLambdaMangleContext() - .getManglingNumber(Method); - break; + + // Allocate a mangling number for this lambda expression, if the ABI + // requires one. + Decl *ContextDecl = ExprEvalContexts.back().LambdaContextDecl; + + enum ContextKind { + Normal, + DefaultArgument, + DataMember, + StaticDataMember + } Kind = Normal; + + // Default arguments of member function parameters that appear in a class + // definition, as well as the initializers of data members, receive special + // treatment. Identify them. + if (ContextDecl) { + if (ParmVarDecl *Param = dyn_cast(ContextDecl)) { + if (const DeclContext *LexicalDC + = Param->getDeclContext()->getLexicalParent()) + if (LexicalDC->isRecord()) + Kind = DefaultArgument; + } else if (VarDecl *Var = dyn_cast(ContextDecl)) { + if (Var->getDeclContext()->isRecord()) + Kind = StaticDataMember; + } else if (isa(ContextDecl)) { + Kind = DataMember; } } - Class->setLambdaMangling(*ManglingNumber, ContextDecl); + // Itanium ABI [5.1.7]: + // In the following contexts [...] the one-definition rule requires closure + // types in different translation units to "correspond": + bool IsInNonspecializedTemplate = + !ActiveTemplateInstantiations.empty() || CurContext->isDependentContext(); + unsigned ManglingNumber; + switch (Kind) { + case Normal: + // -- the bodies of non-exported nonspecialized template functions + // -- the bodies of inline functions + if ((IsInNonspecializedTemplate && + !(ContextDecl && isa(ContextDecl))) || + isInInlineFunction(CurContext)) + ManglingNumber = Context.getLambdaManglingNumber(Method); + else + ManglingNumber = 0; + + // There is no special context for this lambda. + ContextDecl = 0; + break; + + case StaticDataMember: + // -- the initializers of nonspecialized static members of template classes + if (!IsInNonspecializedTemplate) { + ManglingNumber = 0; + ContextDecl = 0; + break; + } + // Fall through to assign a mangling number. + + case DataMember: + // -- the in-class initializers of class members + case DefaultArgument: + // -- default arguments appearing in class definitions + ManglingNumber = ExprEvalContexts.back().getLambdaMangleContext() + .getManglingNumber(Method); + break; + } + + Class->setLambdaMangling(ManglingNumber, ContextDecl); + return Method; } diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 5a10dfbccd..aeda975565 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -341,9 +341,9 @@ Decl *TemplateDeclInstantiator::VisitVarDecl(VarDecl *D) { // We already have an initializer in the class. } else if (D->getInit()) { if (Var->isStaticDataMember() && !D->isOutOfLine()) - SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated); + SemaRef.PushExpressionEvaluationContext(Sema::ConstantEvaluated, D); else - SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); + SemaRef.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, D); // Instantiate the initializer. ExprResult Init = SemaRef.SubstInitializer(D->getInit(), TemplateArgs, diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index b03f86bb0e..fff3b3f4bf 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -7904,15 +7904,11 @@ TreeTransform::TransformLambdaExpr(LambdaExpr *E) { Invalid = true; // Build the call operator. - // Note: Once a lambda mangling number and context declaration have been - // assigned, they never change. - unsigned ManglingNumber = E->getLambdaClass()->getLambdaManglingNumber(); - Decl *ContextDecl = E->getLambdaClass()->getLambdaContextDecl(); CXXMethodDecl *CallOperator = getSema().startLambdaDefinition(Class, E->getIntroducerRange(), - MethodTy, + MethodTy, E->getCallOperator()->getLocEnd(), - Params, ManglingNumber, ContextDecl); + Params); getDerived().transformAttrs(E->getCallOperator(), CallOperator); // FIXME: Instantiation-specific. diff --git a/test/CodeGenCXX/mangle-lambdas.cpp b/test/CodeGenCXX/mangle-lambdas.cpp index cc53b01057..16ddf4838e 100644 --- a/test/CodeGenCXX/mangle-lambdas.cpp +++ b/test/CodeGenCXX/mangle-lambdas.cpp @@ -1,5 +1,10 @@ // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-macosx10.7.0 -emit-llvm -o - %s | FileCheck %s +// CHECK: @_ZZNK7PR12917IJiiEE1nMUlvE_clEvE1n = linkonce_odr global i32 0 +// CHECK: @_ZZN7PR12917IJicdEEC1EicdEd_N1nE = linkonce_odr global i32 0 +// CHECK: @_ZZN7PR12917IJicdEEC1EicdEd0_N1nE = linkonce_odr global i32 0 +// CHECK: @_ZZN7PR12917IJicdEEC1EicdEd1_N1nE = linkonce_odr global i32 0 + // CHECK: define linkonce_odr void @_Z11inline_funci inline void inline_func(int n) { // CHECK: call i32 @_ZZ11inline_funciENKUlvE_clEv @@ -78,10 +83,10 @@ struct ST { // CHECK: define void @_Z7test_ST2STIdE void test_ST(ST st) { - // CHECK: call double @_ZZN2ST1fET_S0_Ed0_NKUlvE_clEv - // CHECK-NEXT: call double @_ZZN2ST1fET_S0_Ed0_NKUlvE0_clEv + // CHECK: call double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv + // CHECK-NEXT: call double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv // CHECK-NEXT: fadd double - // CHECK-NEXT: call double @_ZZN2ST1fET_S0_Ed_NKUlvE_clEv + // CHECK-NEXT: call double @_ZZN2STIdE1fEddEd_NKUlvE_clEv // CHECK-NEXT: call void @_ZN2STIdE1fEdd st.f(); @@ -89,11 +94,11 @@ void test_ST(ST st) { } // Check the linkage of the lambda call operators used in test_ST. -// CHECK: define linkonce_odr double @_ZZN2ST1fET_S0_Ed0_NKUlvE_clEv +// CHECK: define linkonce_odr double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv // CHECK: ret double 1 -// CHECK: define linkonce_odr double @_ZZN2ST1fET_S0_Ed0_NKUlvE0_clEv +// CHECK: define linkonce_odr double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv // CHECK: ret double 2 -// CHECK: define linkonce_odr double @_ZZN2ST1fET_S0_Ed_NKUlvE_clEv +// CHECK: define linkonce_odr double @_ZZN2STIdE1fEddEd_NKUlvE_clEv // CHECK: ret double 3 template @@ -150,6 +155,24 @@ void use_func_template() { func_template(); } + +template struct PR12917 { + PR12917(T ...t = []{ static int n = 0; return ++n; }()); + + static int n[3]; +}; +template int PR12917::n[3] = { + []{ static int n = 0; return ++n; }() +}; + +// CHECK: call i32 @_ZZN7PR12917IJicdEEC1EicdEd1_NKUlvE_clEv( +// CHECK: call i32 @_ZZN7PR12917IJicdEEC1EicdEd0_NKUlvE_clEv( +// CHECK: call i32 @_ZZN7PR12917IJicdEEC1EicdEd_NKUlvE_clEv( +// CHECK: call void @_ZN7PR12917IJicdEEC1Eicd( +PR12917 pr12917; +int *pr12917_p = PR12917::n; + + // CHECK: define linkonce_odr void @_Z1fIZZNK23TestNestedInstantiationclEvENKUlvE_clEvEUlvE_EvT_ struct Members { -- 2.40.0