]> granicus.if.org Git - clang/commitdiff
Support for use of default argument in constructors.
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 5 Aug 2009 00:26:10 +0000 (00:26 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 5 Aug 2009 00:26:10 +0000 (00:26 +0000)
work in progress.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@78132 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ExprCXX.h
lib/AST/ExprCXX.cpp
lib/Sema/SemaDeclCXX.cpp

index ae1c04a175a01d144d6024fcad287305ec5bed2b..bee9b2989678d2911edc78e3b71a54e22848668a 100644 (file)
@@ -511,6 +511,12 @@ public:
 
   unsigned getNumArgs() const { return NumArgs; }
 
+  /// setArg - Set the specified argument.
+  void setArg(unsigned Arg, Expr *ArgExpr) {
+    assert(Arg < NumArgs && "Arg access out of range!");
+    Args[Arg] = ArgExpr;
+  }
+
   virtual SourceRange getSourceRange() const { return SourceRange(); }
 
   static bool classof(const Stmt *T) { 
index df67bea6a6ee94d3773dce6d47ed2bfc7994229e..d8e069d0002c97396e09535757951c68a5a896fc 100644 (file)
@@ -395,9 +395,13 @@ CXXConstructExpr::CXXConstructExpr(ASTContext &C, StmtClass SC, QualType T,
        (T->isDependentType() ||
         CallExpr::hasAnyValueDependentArguments(args, numargs))),
   Constructor(D), Elidable(elidable), Args(0), NumArgs(numargs) {
+    // leave room for default arguments;
+    FunctionDecl *FDecl = cast<FunctionDecl>(D);
+    unsigned NumArgsInProto = FDecl->param_size();
+    NumArgs += (NumArgsInProto - numargs);
     if (NumArgs > 0) {
       Args = new (C) Stmt*[NumArgs];
-      for (unsigned i = 0; i < NumArgs; ++i)
+      for (unsigned i = 0; i < numargs; ++i)
         Args[i] = args[i];
     }
 }
index a550e662c471b41d34f49ad6d7d9d3bd6f16f738..76bd71cdce4b653ac5708cf8071c911d693b050c 100644 (file)
@@ -2360,8 +2360,29 @@ void Sema::InitializeVarWithConstructor(VarDecl *VD,
                                         CXXConstructorDecl *Constructor,
                                         QualType DeclInitType, 
                                         Expr **Exprs, unsigned NumExprs) {
-  Expr *Temp = CXXConstructExpr::Create(Context, DeclInitType, Constructor, 
-                                        false, Exprs, NumExprs);
+  CXXConstructExpr *Temp = CXXConstructExpr::Create(Context, DeclInitType, 
+                                                    Constructor, 
+                                                    false, Exprs, NumExprs);
+  // default arguments must be added to constructor call expression.
+  FunctionDecl *FDecl = cast<FunctionDecl>(Constructor);
+  unsigned NumArgsInProto = FDecl->param_size();
+  for (unsigned j = NumExprs; j != NumArgsInProto; j++) {
+    Expr *DefaultExpr = FDecl->getParamDecl(j)->getDefaultArg();
+    
+    // If the default expression creates temporaries, we need to
+    // push them to the current stack of expression temporaries so they'll
+    // be properly destroyed.
+    if (CXXExprWithTemporaries *E 
+        = dyn_cast_or_null<CXXExprWithTemporaries>(DefaultExpr)) {
+      assert(!E->shouldDestroyTemporaries() && 
+             "Can't destroy temporaries in a default argument expr!");
+      for (unsigned I = 0, N = E->getNumTemporaries(); I != N; ++I)
+        ExprTemporaries.push_back(E->getTemporary(I));
+    }
+    Expr *Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(j));
+    Temp->setArg(j, Arg);
+  }
+  
   MarkDeclarationReferenced(VD->getLocation(), Constructor);
   VD->setInit(Context, Temp);
 }