mirror of
https://github.com/llvm/llvm-project.git
synced 2025-05-03 00:06:06 +00:00
Patch to improve ir-gen for constructors with default argument
expressions and a test case. llvm-svn: 78213
This commit is contained in:
parent
be47ccffef
commit
aa890bf2f3
@ -1672,6 +1672,14 @@ public:
|
||||
CXXConstructorDecl *Constructor,
|
||||
QualType DeclInitType,
|
||||
Expr **Exprs, unsigned NumExprs);
|
||||
|
||||
/// BuildCXXConstructExpr - Creates a complete call to a constructor,
|
||||
/// including handling of its default argument expressions.
|
||||
Expr * BuildCXXConstructExpr(ASTContext &C,
|
||||
QualType DeclInitType,
|
||||
CXXConstructorDecl *Constructor,
|
||||
bool Elidable,
|
||||
Expr **Exprs, unsigned NumExprs);
|
||||
|
||||
/// FinalizeVarWithDestructor - Prepare for calling destructor on the
|
||||
/// constructed variable.
|
||||
|
@ -2356,13 +2356,16 @@ void Sema::DefineImplicitCopyConstructor(SourceLocation CurrentLocation,
|
||||
CopyConstructor->setUsed();
|
||||
}
|
||||
|
||||
void Sema::InitializeVarWithConstructor(VarDecl *VD,
|
||||
CXXConstructorDecl *Constructor,
|
||||
QualType DeclInitType,
|
||||
Expr **Exprs, unsigned NumExprs) {
|
||||
CXXConstructExpr *Temp = CXXConstructExpr::Create(Context, DeclInitType,
|
||||
/// BuildCXXConstructExpr - Creates a complete call to a constructor,
|
||||
/// including handling of its default argument expressions.
|
||||
Expr *Sema::BuildCXXConstructExpr(ASTContext &C,
|
||||
QualType DeclInitType,
|
||||
CXXConstructorDecl *Constructor,
|
||||
bool Elidable,
|
||||
Expr **Exprs, unsigned NumExprs) {
|
||||
CXXConstructExpr *Temp = CXXConstructExpr::Create(C, DeclInitType,
|
||||
Constructor,
|
||||
false, Exprs, NumExprs);
|
||||
Elidable, Exprs, NumExprs);
|
||||
// default arguments must be added to constructor call expression.
|
||||
FunctionDecl *FDecl = cast<FunctionDecl>(Constructor);
|
||||
unsigned NumArgsInProto = FDecl->param_size();
|
||||
@ -2379,10 +2382,19 @@ void Sema::InitializeVarWithConstructor(VarDecl *VD,
|
||||
for (unsigned I = 0, N = E->getNumTemporaries(); I != N; ++I)
|
||||
ExprTemporaries.push_back(E->getTemporary(I));
|
||||
}
|
||||
Expr *Arg = new (Context) CXXDefaultArgExpr(FDecl->getParamDecl(j));
|
||||
Expr *Arg = new (C) CXXDefaultArgExpr(FDecl->getParamDecl(j));
|
||||
Temp->setArg(j, Arg);
|
||||
}
|
||||
|
||||
return Temp;
|
||||
}
|
||||
|
||||
void Sema::InitializeVarWithConstructor(VarDecl *VD,
|
||||
CXXConstructorDecl *Constructor,
|
||||
QualType DeclInitType,
|
||||
Expr **Exprs, unsigned NumExprs) {
|
||||
Expr *Temp = BuildCXXConstructExpr(Context,
|
||||
DeclInitType, Constructor,
|
||||
false, Exprs, NumExprs);
|
||||
MarkDeclarationReferenced(VD->getLocation(), Constructor);
|
||||
VD->setInit(Context, Temp);
|
||||
}
|
||||
|
@ -911,8 +911,8 @@ Sema::PerformImplicitConversion(Expr *&From, QualType ToType,
|
||||
assert(!ToType->isReferenceType());
|
||||
|
||||
// FIXME: Keep track of whether the copy constructor is elidable or not.
|
||||
From = CXXConstructExpr::Create(Context, ToType,
|
||||
SCS.CopyConstructor, false, &From, 1);
|
||||
From = BuildCXXConstructExpr(Context,
|
||||
ToType, SCS.CopyConstructor, false, &From, 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -177,8 +177,8 @@ bool Sema::CheckInitializerTypes(Expr *&Init, QualType &DeclType,
|
||||
if (!Constructor)
|
||||
return true;
|
||||
|
||||
Init = CXXConstructExpr::Create(Context, DeclType, Constructor, false,
|
||||
&Init, 1);
|
||||
Init = BuildCXXConstructExpr(Context,
|
||||
DeclType, Constructor, false, &Init, 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1073,11 +1073,11 @@ TemplateExprInstantiator::VisitCXXConstructExpr(CXXConstructExpr *E) {
|
||||
Args.push_back(ArgInst.takeAs<Expr>());
|
||||
}
|
||||
|
||||
return SemaRef.Owned(CXXConstructExpr::Create(SemaRef.Context, T,
|
||||
E->getConstructor(),
|
||||
E->isElidable(),
|
||||
Args.takeAs<Expr>(),
|
||||
Args.size()));
|
||||
return SemaRef.Owned(SemaRef.BuildCXXConstructExpr(SemaRef.Context, T,
|
||||
E->getConstructor(),
|
||||
E->isElidable(),
|
||||
Args.takeAs<Expr>(),
|
||||
Args.size()));
|
||||
}
|
||||
|
||||
Sema::OwningExprResult
|
||||
|
30
clang/test/CodeGenCXX/constructor-default-arg.cpp
Normal file
30
clang/test/CodeGenCXX/constructor-default-arg.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
// RUN: clang-cc %s -emit-llvm -o %t &&
|
||||
// RUN: grep 'call void @_ZN1XC1ERK1Xiii' %t | count 3
|
||||
|
||||
extern "C" int printf(...);
|
||||
|
||||
|
||||
struct C {
|
||||
C() : iC(6) {}
|
||||
int iC;
|
||||
};
|
||||
|
||||
int foo() {
|
||||
return 6;
|
||||
};
|
||||
|
||||
class X { // ...
|
||||
public:
|
||||
X(int) {}
|
||||
X(const X&, int i = 1, int j = 2, int k = foo()) {
|
||||
printf("X(const X&, %d, %d, %d)\n", i, j, k);
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
X a(1);
|
||||
X b(a, 2);
|
||||
X c = b;
|
||||
X d(a, 5, 6);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user