Patch to implement new-operators with default args.
Fixes pr5547.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89370 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index 462bf13..209d306 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -326,7 +326,7 @@
   QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, &DInfo);
   if (D.isInvalidType())
     return ExprError();
-
+    
   return BuildCXXNew(StartLoc, UseGlobal,
                      PlacementLParen,
                      move(PlacementArgs),
@@ -394,7 +394,7 @@
   FunctionDecl *OperatorDelete = 0;
   Expr **PlaceArgs = (Expr**)PlacementArgs.get();
   unsigned NumPlaceArgs = PlacementArgs.size();
-    
+  
   if (!AllocType->isDependentType() &&
       !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) &&
       FindAllocationFunctions(StartLoc,
@@ -402,7 +402,35 @@
                               UseGlobal, AllocType, ArraySize, PlaceArgs,
                               NumPlaceArgs, OperatorNew, OperatorDelete))
     return ExprError();
-
+  llvm::SmallVector<Expr *, 4> AllPlaceArgs;
+  if (OperatorNew) {
+    // Add default arguments, if any.
+    const FunctionProtoType *Proto = 
+      OperatorNew->getType()->getAs<FunctionProtoType>();
+    unsigned NumArgsInProto = Proto->getNumArgs();
+    for (unsigned i = 1; i != NumArgsInProto; i++) {
+      QualType ProtoArgType = Proto->getArgType(i);
+    
+      Expr *Arg;
+      if (i <= NumPlaceArgs) {
+        AllPlaceArgs.push_back(PlaceArgs[i-1]);
+        continue;
+      }
+      ParmVarDecl *Param = OperatorNew->getParamDecl(i);
+    
+      OwningExprResult ArgExpr =
+        BuildCXXDefaultArgExpr(StartLoc, OperatorNew, Param);
+      if (ArgExpr.isInvalid())
+        return ExprError();
+    
+      Arg = ArgExpr.takeAs<Expr>();
+      AllPlaceArgs.push_back(Arg);
+    }
+    NumPlaceArgs = AllPlaceArgs.size();
+    if (NumPlaceArgs > 0)
+      PlaceArgs = &AllPlaceArgs[0];
+  }
+  
   bool Init = ConstructorLParen.isValid();
   // --- Choosing a constructor ---
   // C++ 5.3.4p15