Index: include/clang/AST/DeclCXX.h
===================================================================
--- include/clang/AST/DeclCXX.h	(revision 181103)
+++ include/clang/AST/DeclCXX.h	(working copy)
@@ -508,9 +508,10 @@
     typedef LambdaExpr::Capture Capture;
     
     LambdaDefinitionData(CXXRecordDecl *D, TypeSourceInfo *Info, bool Dependent) 
-      : DefinitionData(D), Dependent(Dependent), NumCaptures(0), 
-        NumExplicitCaptures(0), ManglingNumber(0), ContextDecl(0), Captures(0),
-        MethodTyInfo(Info) 
+      : DefinitionData(D), Dependent(Dependent), IsGenericLambda(false), 
+        NumCaptures(0), NumExplicitCaptures(0), ManglingNumber(0), 
+        ContextDecl(0), Captures(0), MethodTyInfo(Info), CallOperator(0),
+        ConversionOperator(0), StaticInvoker(0) 
     {
       IsLambda = true;
     }
@@ -522,9 +523,11 @@
     /// within the default argument of a function template, because the
     /// lambda will have been created with the enclosing context as its
     /// declaration context, rather than function. This is an unfortunate
-    /// artifact of having to parse the default arguments before 
+    /// artifact of having to parse the default arguments before. 
     unsigned Dependent : 1;
     
+    /// \brief Whether this lambda is a generic lambda.
+    bool IsGenericLambda : 1;
     /// \brief The number of captures in this lambda.
     unsigned NumCaptures : 16;
 
@@ -547,6 +550,15 @@
 
     /// \brief The type of the call method.
     TypeSourceInfo *MethodTyInfo;
+
+    /// \brief The Lambda call method
+    CXXMethodDecl *CallOperator;
+    
+    /// \brief The Lambda conversion operator, for non-capturing lambdas
+    CXXConversionDecl *ConversionOperator;
+    
+    /// \brief The Lambda static method invoker, for non-capturing lambdas
+    CXXMethodDecl *StaticInvoker;
   };
 
   struct DefinitionData &data() {
@@ -979,6 +991,47 @@
   /// \brief Determine whether this class describes a lambda function object.
   bool isLambda() const { return hasDefinition() && data().IsLambda; }
 
+  /// \brief Determine whether this class describes a Generic 
+  /// lambda function object.
+  bool isGenericLambda() const { return isLambda() && 
+                                            getLambdaData().IsGenericLambda; }
+  /// \brief Mark this lambda as a generic lambda.
+  void setGenericLambda(bool b) { getLambdaData().IsGenericLambda = b; }
+
+  /// \brief Assign the member call operator of the lambda. 
+  void setLambdaCallOperator(CXXMethodDecl *M) {
+    getLambdaData().CallOperator = M;
+  }
+
+  /// \brief Retrieve the lambda call operator of the closure type
+  /// if this is a closure type.
+  CXXMethodDecl* getLambdaCallOperator() const {
+    return isLambda() ? getLambdaData().CallOperator : 0;
+  }
+
+  /// \brief Assign the lambda conversion operator for lambdas with
+  /// no captures. 
+  void setLambdaConversionOperator(CXXConversionDecl *C) {
+    getLambdaData().ConversionOperator = C;
+  }
+
+  /// \brief Retrieve the lambda conversion operator. 
+  CXXConversionDecl* getLambdaConversionOperator() const {
+    return isLambda() ? getLambdaData().ConversionOperator : 0;
+  }
+  /// \brief Assign the lambda static invoker, the address of which
+  ///  is returned by the conversion operator, and the body of which
+  ///  is forwarded to the lambda call operator. 
+  void setLambdaStaticInvoker(CXXMethodDecl *M) {
+    getLambdaData().StaticInvoker = M;
+  }
+
+  /// \brief Retrieve the lambda static invoker.
+  CXXMethodDecl* getLambdaStaticInvoker() const {
+    return isLambda() ? getLambdaData().StaticInvoker : 0;
+  }
+
+
   /// \brief For a closure type, retrieve the mapping from captured
   /// variables and this to the non-static data members that store the
   /// values or references of the captures.
Index: include/clang/Sema/Lambda.h
===================================================================
--- include/clang/Sema/Lambda.h	(revision 0)
+++ include/clang/Sema/Lambda.h	(working copy)
@@ -0,0 +1,244 @@
+//===--- Lambda.h - Lambda Helper Functions --------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file provides some common utility functions for processing
+// Lambdas.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_SEMA_LAMBDA_H
+#define LLVM_CLANG_SEMA_LAMBDA_H
+
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/DeclTemplate.h"
+
+namespace clang {
+
+// The name of the static invoker function that will forward
+// to the corresponding lambda call operator.
+inline const char* getSecretLambdaStaticInvokerStringID() {
+  return "__$#lambda_invoker";
+}  
+
+
+//*
+// Return true if the Function is either a Specialization of a generic lambda call operator 
+// or a non-generic call operator.
+// 
+//    
+inline bool isNonTemplateLambdaCallOperator(FunctionDecl *F) {
+  CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(F);
+  if (MD) {
+    CXXRecordDecl *LambdaClass = MD->getParent();
+    if (!LambdaClass || !LambdaClass->isLambda()) return false;
+    CXXMethodDecl *LambdaCallOp = LambdaClass->getLambdaCallOperator(); 
+    MD = LambdaClass->isGenericLambda() ?  
+          cast<CXXMethodDecl>(MD->getTemplateInstantiationPattern()) : MD;
+    return MD && MD == LambdaCallOp;
+  }
+
+  return false;
+}
+// This function returns true if F is a specialization, or a lambda
+// call operator (either generic or non-generic)
+inline bool isLambdaCallOperator(FunctionDecl *F) {
+  CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(F);
+  if (MD)
+  {
+    CXXRecordDecl *LambdaClass = MD->getParent();
+    if (!LambdaClass || !LambdaClass->isLambda()) return false;
+    CXXMethodDecl *PrimaryLambdaCallOp = LambdaClass->getLambdaCallOperator(); 
+    // If this is a generic non-specialized call operator, or
+    // if it is a call operator of a non-generic lambda, return true;
+    if (MD == PrimaryLambdaCallOp) return true;
+    // Check to see if the primary templates of this specialization
+    // match the primary lamda call op
+    if (LambdaClass->isGenericLambda()) {
+      assert(MD->getTemplateInstantiationPattern());
+      assert(MD->isFunctionTemplateSpecialization());
+
+      CXXMethodDecl *PrimaryTemplateCallOp = 
+          cast<CXXMethodDecl>(MD->getTemplateInstantiationPattern());
+      return PrimaryLambdaCallOp == PrimaryTemplateCallOp;
+    }
+  }
+  return false;
+}
+
+
+inline bool isNonTemplateLambdaConversionOperator(FunctionDecl *F)
+{
+  CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(F);
+  if (MD)
+  {
+    CXXRecordDecl *LambdaClass = MD->getParent();
+    if (!LambdaClass || !LambdaClass->isLambda()) return false;
+    CXXMethodDecl *LambdaCallOp = LambdaClass->getLambdaConversionOperator(); 
+    MD = LambdaClass->isGenericLambda() ?  
+      cast<CXXMethodDecl>(MD->getTemplateInstantiationPattern()) : MD;
+    return MD && MD == LambdaCallOp;
+  }
+
+  return false;
+}
+
+// Check if 'F' is a non-template lambda call operator
+// or a non-template conversion operator nested
+// within a nontemplatelambdacalloperator
+// By non-template, it could be a specialzied instantiation 
+// or a non-generic lambda operator
+
+inline bool isNonTemplateNestedLambda(FunctionDecl *F) {
+  if (isNonTemplateLambdaCallOperator(F) ||
+          isNonTemplateLambdaConversionOperator(F)) {
+ 
+    CXXMethodDecl *MD = cast<CXXMethodDecl>(F);
+    CXXRecordDecl *LambdaClass = MD->getParent();
+    DeclContext   *Ctx = cast<DeclContext>(LambdaClass);
+    DeclContext   *ParentOfLambdaClass = Ctx->getParent();
+    FunctionDecl  *ParentFun = ParentOfLambdaClass 
+      ? dyn_cast<FunctionDecl>(ParentOfLambdaClass)
+      : 0;
+    return ParentFun && isNonTemplateLambdaCallOperator(ParentFun); 
+    
+  }
+  return false;
+}
+
+inline bool isGenericLambdaCallOperatorSpecialization(FunctionDecl *F) {
+  CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(F);
+  if (MD)
+  {
+    CXXRecordDecl *LambdaClass = MD->getParent();
+    if (LambdaClass && LambdaClass->isGenericLambda())
+    {
+      
+      return LambdaClass->getLambdaCallOperator() 
+              == MD->getTemplateInstantiationPattern();
+    }
+  }
+    
+  return false;
+}
+
+inline bool isGenericLambdaConversionOperatorSpecialization(FunctionDecl *F) {
+  CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(F);
+  if (MD)
+  {
+    CXXRecordDecl *LambdaClass = MD->getParent();
+    if (LambdaClass && LambdaClass->isGenericLambda())
+    {
+
+      return LambdaClass->getLambdaConversionOperator() 
+        == MD->getTemplateInstantiationPattern();
+    }
+  }
+
+  return false;
+}
+
+// Is F a specialization of a generic lambda call operator
+//  that is within a specialization of a function
+//  template<class R> struct X {
+//     template<class T> void foo(T t) {
+//         auto L = [](auto a) { return a; }
+//         L(t);
+//     }
+//  } 
+//   X<int>().foo(2); 
+//   When instantiating L(t), above during foo(2) instantiation
+//   this routine returns true - and it prevents us from going out
+//   side to the class template arguments and adding them on the stack.
+//   Remember, Generic Lambda Expressions are always transformed eagerly
+//   so, when the enclosing function is instantiated with 'int' if there
+//   is any reference to 'R' (the class template argument) it will get
+//   transformed within any lambda or nested lambda expressions
+   
+inline bool isGenericLambdaSpecWithinNonDependentFunction(
+     FunctionDecl *F) {
+  if (isGenericLambdaCallOperatorSpecialization(F))
+  {
+    CXXMethodDecl *MD = cast<CXXMethodDecl>(F);
+    CXXRecordDecl *LambdaClass = MD->getParent();
+    DeclContext   *Ctx = cast<DeclContext>(LambdaClass);
+    DeclContext   *ParentOfLambdaClass = Ctx->getParent();
+    FunctionDecl  *ParentFun = ParentOfLambdaClass 
+                      ? dyn_cast<FunctionDecl>(ParentOfLambdaClass)
+                      : 0;
+    return ParentFun && ParentFun->isTemplateInstantiation(); 
+
+  }
+  return false;
+}
+
+// Is F a specialization of a generic lambda call operator
+// that is within a specialized class (not a partial specialization)
+// template<class R> struct X {
+//     R r;
+//     X(R r) : r(r);
+//     std::function<R(R)> mem_fun = 
+//                         ([](auto a) [=](auto b) a + b)(r);
+//  };
+// 
+//  X<int>(7).mem_fun(2); 
+//   
+//  When instantiating X<int> above - [](auto a) gets called and it 
+//  returns a nested lambda - which gets instantiated during the 
+//  compilation of mem_fun(2).  During this instantiation the depth
+//  of the nested lambda must not take the template arguments of
+//  the parent class (that has been instantiated) into account.
+//   
+inline bool isGenericLambdaSpecWithinNonDependentClass(
+                                      FunctionDecl *F) {
+    if (isGenericLambdaCallOperatorSpecialization(F))
+    {
+      CXXMethodDecl *MD = cast<CXXMethodDecl>(F);
+      CXXRecordDecl *LambdaClass = MD->getParent();
+      DeclContext   *Ctx = cast<DeclContext>(LambdaClass);
+      DeclContext   *ParentCtxOfLambdaClass = Ctx->getParent();
+      CXXRecordDecl  *ParentClass = ParentCtxOfLambdaClass 
+        ? dyn_cast<CXXRecordDecl>(ParentCtxOfLambdaClass)
+        : 0;
+      if ( ParentClass ) {
+        // If this is a primary class with a template, we are dependent
+        if (ParentClass->getDescribedClassTemplate())
+          return false;
+        // If this is a partial specialization delcaration, we are dependent 
+        if (isa<ClassTemplatePartialSpecializationDecl>(ParentClass))
+          return false;
+        // otherwise we are in a specialized class that is non-dependent 
+        // or a regular non-template class.
+        return true;
+      }
+
+    }
+    return false;
+}
+
+// Check if 'DC' is the body of a specialization of a generic lambda
+// call operator and return as a Method or null if not.
+// This can be useful during certain transformations which are
+// generic lambda dependent ...
+inline CXXMethodDecl* getAsSpecializedGenericLambdaCallOperator(DeclContext *DC) {
+  // Can we convert to a C++ Method?
+  CXXMethodDecl *LambdaCallOp = dyn_cast<CXXMethodDecl>(DC);
+  if (!LambdaCallOp) return 0;
+  // If so, retrieve the lambda closure class
+  CXXRecordDecl *LambdaClass = LambdaCallOp->getParent();
+  // ensure it is a generic lambda and a specialization
+  if (LambdaClass->isGenericLambda() && 
+        LambdaCallOp->isFunctionTemplateSpecialization()) 
+    return LambdaCallOp;
+ return 0;
+}
+
+//*/
+} // clang
+
+#endif // LLVM_CLANG_SEMA_LAMBDA_H
Index: lib/AST/DeclCXX.cpp
===================================================================
--- lib/AST/DeclCXX.cpp	(revision 181103)
+++ lib/AST/DeclCXX.cpp	(working copy)
@@ -1493,7 +1493,7 @@
 
 bool CXXMethodDecl::isLambdaStaticInvoker() const {
   return getParent()->isLambda() && 
-         getIdentifier() && getIdentifier()->getName() == "__invoke";
+         getParent()->getLambdaStaticInvoker() == this;
 }
 
 
Index: lib/CodeGen/CodeGenFunction.cpp
===================================================================
--- lib/CodeGen/CodeGenFunction.cpp	(revision 181103)
+++ lib/CodeGen/CodeGenFunction.cpp	(working copy)
@@ -689,7 +689,7 @@
     EmitLambdaToBlockPointerBody(Args);
   } else if (isa<CXXMethodDecl>(FD) &&
              cast<CXXMethodDecl>(FD)->isLambdaStaticInvoker()) {
-    // The lambda "__invoke" function is special, because it forwards or
+    // The lambda static invoker function is special, because it forwards or
     // clones the body of the function call operator (but is actually static).
     EmitLambdaStaticInvokeFunction(cast<CXXMethodDecl>(FD));
   } else if (FD->isDefaulted() && isa<CXXMethodDecl>(FD) &&
Index: lib/Sema/SemaDeclCXX.cpp
===================================================================
--- lib/Sema/SemaDeclCXX.cpp	(revision 181103)
+++ lib/Sema/SemaDeclCXX.cpp	(working copy)
@@ -31,6 +31,7 @@
 #include "clang/Sema/CXXFieldCollector.h"
 #include "clang/Sema/DeclSpec.h"
 #include "clang/Sema/Initialization.h"
+#include "clang/Sema/Lambda.h"
 #include "clang/Sema/Lookup.h"
 #include "clang/Sema/ParsedTemplate.h"
 #include "clang/Sema/Scope.h"
@@ -9847,29 +9848,27 @@
        SourceLocation CurrentLocation,
        CXXConversionDecl *Conv) 
 {
-  CXXRecordDecl *Lambda = Conv->getParent();
+  CXXRecordDecl *LambdaClass = Conv->getParent();
   
   // Make sure that the lambda call operator is marked used.
-  markLambdaCallOperatorUsed(*this, Lambda);
+  markLambdaCallOperatorUsed(*this, LambdaClass);
   
   Conv->setUsed();
   
   SynthesizedFunctionScope Scope(*this, Conv);
   DiagnosticErrorTrap Trap(Diags);
-  
-  // Return the address of the __invoke function.
-  DeclarationName InvokeName = &Context.Idents.get("__invoke");
-  CXXMethodDecl *Invoke 
-    = cast<CXXMethodDecl>(Lambda->lookup(InvokeName).front());
+
+  CXXMethodDecl *Invoke = LambdaClass->getLambdaStaticInvoker();
+
   Expr *FunctionRef = BuildDeclRefExpr(Invoke, Invoke->getType(),
                                        VK_LValue, Conv->getLocation()).take();
-  assert(FunctionRef && "Can't refer to __invoke function?");
+  assert(FunctionRef && "Can't refer to lambda static invoker function?");
   Stmt *Return = ActOnReturnStmt(Conv->getLocation(), FunctionRef).take();
   Conv->setBody(new (Context) CompoundStmt(Context, Return,
                                            Conv->getLocation(),
                                            Conv->getLocation()));
     
-  // Fill in the __invoke function with a dummy implementation. IR generation
+  // Fill in the invoke function with a dummy implementation. IR generation
   // will fill in the actual details.
   Invoke->setUsed();
   Invoke->setReferenced();
Index: lib/Sema/SemaLambda.cpp
===================================================================
--- lib/Sema/SemaLambda.cpp	(revision 181103)
+++ lib/Sema/SemaLambda.cpp	(working copy)
@@ -14,6 +14,7 @@
 #include "clang/AST/ExprCXX.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Sema/Initialization.h"
+#include "clang/Sema/Lambda.h"
 #include "clang/Sema/Lookup.h"
 #include "clang/Sema/Scope.h"
 #include "clang/Sema/ScopeInfo.h"
@@ -499,7 +500,7 @@
 
   CXXMethodDecl *Method = startLambdaDefinition(Class, Intro.Range,
                                                 MethodTyInfo, EndLoc, Params);
-  
+  Class->setLambdaCallOperator(Method);
   if (ExplicitParams)
     CheckCXXDefaultArguments(Method);
   
@@ -729,10 +730,10 @@
   Conversion->setAccess(AS_public);
   Conversion->setImplicit(true);
   Class->addDecl(Conversion);
-  
-  // Add a non-static member function "__invoke" that will be the result of
-  // the conversion.
-  Name = &S.Context.Idents.get("__invoke");
+  Class->setLambdaConversionOperator(Conversion);
+  // Add a non-static member function that will be the result of
+  // the conversion with a certain unique ID.
+  Name = &S.Context.Idents.get(getSecretLambdaStaticInvokerStringIDFV());
   CXXMethodDecl *Invoke
     = CXXMethodDecl::Create(S.Context, Class, Loc, 
                             DeclarationNameInfo(Name, Loc), FunctionTy, 
@@ -756,6 +757,7 @@
   Invoke->setAccess(AS_private);
   Invoke->setImplicit(true);
   Class->addDecl(Invoke);
+  Class->setLambdaStaticInvoker(Invoke);
 }
 
 /// \brief Add a lambda's conversion to block pointer.
