It's an error to use a function declared in a class definition as a default argument before the function has been declared.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73234 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Frontend/PrintParserCallbacks.cpp b/lib/Frontend/PrintParserCallbacks.cpp
index b9fe068..026e605 100644
--- a/lib/Frontend/PrintParserCallbacks.cpp
+++ b/lib/Frontend/PrintParserCallbacks.cpp
@@ -701,7 +701,8 @@
     }
 
     virtual void ActOnParamUnparsedDefaultArgument(DeclPtrTy param,
-                                                   SourceLocation EqualLoc) {
+                                                   SourceLocation EqualLoc,
+                                                   SourceLocation ArgLoc) {
       Out << __FUNCTION__ << "\n";
     }
 
diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp
index 40fbc0c..9073c6d 100644
--- a/lib/Parse/ParseDecl.cpp
+++ b/lib/Parse/ParseDecl.cpp
@@ -2470,7 +2470,8 @@
             DefArgToks = 0;
             Actions.ActOnParamDefaultArgumentError(Param);
           } else
-            Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc);
+            Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, 
+                                                (*DefArgToks)[1].getLocation());
         } else {
           // Consume the '='.
           ConsumeToken();
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 2df1ae2..c4b3b2d 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -412,8 +412,14 @@
                                          SourceLocation EqualLoc,
                                          ExprArg defarg);
   virtual void ActOnParamUnparsedDefaultArgument(DeclPtrTy param, 
-                                                 SourceLocation EqualLoc);
+                                                 SourceLocation EqualLoc,
+                                                 SourceLocation ArgLoc);
   virtual void ActOnParamDefaultArgumentError(DeclPtrTy param);
+  
+  // Contains the locations of the beginning of unparsed default
+  // argument locations.
+  llvm::DenseMap<ParmVarDecl *,SourceLocation> UnparsedDefaultArgLocs;
+
   virtual void AddInitializerToDecl(DeclPtrTy dcl, FullExprArg init);
   void AddInitializerToDecl(DeclPtrTy dcl, ExprArg init, bool DirectInit);
   void ActOnUninitializedDecl(DeclPtrTy dcl);
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index b59ac87..8f64e78 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -108,6 +108,8 @@
 Sema::ActOnParamDefaultArgument(DeclPtrTy param, SourceLocation EqualLoc, 
                                 ExprArg defarg) {
   ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>());
+  UnparsedDefaultArgLocs.erase(Param);
+
   ExprOwningPtr<Expr> DefaultArg(this, defarg.takeAs<Expr>());
   QualType ParamType = Param->getType();
 
@@ -154,16 +156,23 @@
 /// because we're inside a class definition. Note that this default
 /// argument will be parsed later.
 void Sema::ActOnParamUnparsedDefaultArgument(DeclPtrTy param, 
-                                             SourceLocation EqualLoc) {
+                                             SourceLocation EqualLoc,
+                                             SourceLocation ArgLoc) {
   ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>());
   if (Param)
     Param->setUnparsedDefaultArg();
+  
+  UnparsedDefaultArgLocs[Param] = ArgLoc;
 }
 
 /// ActOnParamDefaultArgumentError - Parsing or semantic analysis of
 /// the default argument for the parameter param failed.
 void Sema::ActOnParamDefaultArgumentError(DeclPtrTy param) {
-  cast<ParmVarDecl>(param.getAs<Decl>())->setInvalidDecl();
+  ParmVarDecl *Param = cast<ParmVarDecl>(param.getAs<Decl>());
+  
+  Param->setInvalidDecl();
+  
+  UnparsedDefaultArgLocs.erase(Param);
 }
 
 /// CheckExtraCXXDefaultArguments - Check for any extra default
@@ -285,7 +294,7 @@
     // in a semantically valid state.
     for (p = 0; p <= LastMissingDefaultArg; ++p) {
       ParmVarDecl *Param = FD->getParamDecl(p);
-      if (Param->getDefaultArg()) {
+      if (Param->hasDefaultArg()) {
         if (!Param->hasUnparsedDefaultArg())
           Param->getDefaultArg()->Destroy(Context);
         Param->setDefaultArg(0);
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 1abd5fb..c01c812 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -2484,9 +2484,19 @@
       // Pass the argument.
       if (PerformCopyInitialization(Arg, ProtoArgType, "passing"))
         return true;
-    } else
+    } else {
+      if (FDecl->getParamDecl(i)->hasUnparsedDefaultArg()) {
+        Diag (Call->getSourceRange().getBegin(),
+              diag::err_use_of_default_argument_to_function_declared_later) <<
+        FDecl << cast<CXXRecordDecl>(FDecl->getDeclContext())->getDeclName();
+        Diag(UnparsedDefaultArgLocs[FDecl->getParamDecl(i)], 
+              diag::note_default_argument_declared_here);
+      }
+      
       // We already type-checked the argument, so we know it works.
       Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(i));
+    }
+    
     QualType ArgType = Arg->getType();
 
     Call->setArg(i, Arg);