Patch to force synthesis of copy assignment operator

function in the order according to c++03. ir-gen 
for copy assignment in the trivial case and the first
test case.

llvm-svn: 78938
This commit is contained in:
Fariborz Jahanian 2009-08-13 21:09:41 +00:00
parent df743ae603
commit 4985b33fd7
5 changed files with 84 additions and 5 deletions

View File

@ -228,6 +228,18 @@ CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
assert(MD->isInstance() &&
"Trying to emit a member call expr on a static method!");
if (MD->isCopyAssignment()) {
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
if (ClassDecl->hasTrivialCopyAssignment()) {
assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
"EmitCXXOperatorMemberCallExpr - user declared copy assignment");
llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
QualType Ty = E->getType();
EmitAggregateCopy(This, Src, Ty);
return RValue::get(This);
}
}
const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
const llvm::Type *Ty =

View File

@ -653,11 +653,9 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName,
else if (!ClassDecl->hasUserDeclaredConstructor())
DeferredDeclsToEmit.push_back(D);
}
else
if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
if (MD->isCopyAssignment()) {
DeferredDeclsToEmit.push_back(D);
}
else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
if (MD->isCopyAssignment())
DeferredCopyAssignmentToEmit(D);
}
// This function doesn't have a complete type (for example, the return
@ -718,7 +716,46 @@ void CodeGenModule::DeferredCopyConstructorToEmit(GlobalDecl CopyCtorDecl) {
}
}
DeferredDeclsToEmit.push_back(CopyCtorDecl);
}
/// Defer definition of copy assignments which need be implicitly defined.
void CodeGenModule::DeferredCopyAssignmentToEmit(GlobalDecl CopyAssignDecl) {
const CXXMethodDecl *CD = cast<CXXMethodDecl>(CopyAssignDecl.getDecl());
const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
if (ClassDecl->hasTrivialCopyAssignment() ||
ClassDecl->hasUserDeclaredCopyAssignment())
return;
// First make sure all direct base classes and virtual bases and non-static
// data mebers which need to have their copy assignments implicitly defined
// are defined. 12.8.p12
for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
Base != ClassDecl->bases_end(); ++Base) {
CXXRecordDecl *BaseClassDecl
= cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
const CXXMethodDecl *MD = 0;
if (BaseClassDecl->hasConstCopyAssignment(getContext(), MD))
GetAddrOfFunction(GlobalDecl(MD), 0);
}
for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
FieldEnd = ClassDecl->field_end();
Field != FieldEnd; ++Field) {
QualType FieldType = Context.getCanonicalType((*Field)->getType());
if (const ArrayType *Array = Context.getAsArrayType(FieldType))
FieldType = Array->getElementType();
if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
if ((*Field)->isAnonymousStructOrUnion())
continue;
CXXRecordDecl *FieldClassDecl
= cast<CXXRecordDecl>(FieldClassType->getDecl());
const CXXMethodDecl *MD = 0;
if (FieldClassDecl->hasConstCopyAssignment(getContext(), MD))
GetAddrOfFunction(GlobalDecl(MD), 0);
}
}
DeferredDeclsToEmit.push_back(CopyAssignDecl);
}
/// GetAddrOfFunction - Return the address of the given function. If Ty is

View File

@ -398,6 +398,7 @@ private:
const llvm::PointerType *PTy,
const VarDecl *D);
void DeferredCopyConstructorToEmit(GlobalDecl D);
void DeferredCopyAssignmentToEmit(GlobalDecl D);
/// SetCommonAttributes - Set attributes which are common to any
/// form of a global definition (alias, Objective-C method,

View File

@ -3128,6 +3128,7 @@ bool Sema::CheckOverloadedOperatorDeclaration(FunctionDecl *FnDecl) {
assert(isa<CXXMethodDecl>(FnDecl) &&
"Overloaded = not member, but not filtered.");
CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl);
Method->setCopyAssignment(true);
Method->getParent()->addedAssignmentOperator(Context, Method);
}

View File

@ -0,0 +1,28 @@
// RUN: clang-cc -emit-llvm -o %t %s &&
// RUN: grep "_ZN1XaSERK1X" %t | count 0
extern "C" int printf(...);
struct X {
X() : d(0.0), d1(1.1), d2(1.2), d3(1.3) {}
double d;
double d1;
double d2;
double d3;
void pr() {
printf("d = %f d1 = %f d2 = %f d3 = %f\n", d, d1,d2,d3);
}
};
X srcX;
X dstX;
X dstY;
int main() {
dstY = dstX = srcX;
srcX.pr();
dstX.pr();
dstY.pr();
}