Skip to content

Commit

Permalink
Copy ctor fixes and cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syniurge committed Oct 29, 2019
1 parent 361006f commit 4f1c66b
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 47 deletions.
4 changes: 2 additions & 2 deletions dmd/aggregate.d
Original file line number Diff line number Diff line change
Expand Up @@ -1104,9 +1104,9 @@ extern (C++) abstract class AggregateDeclaration : ScopeDsymbol
return null;
}

CtorDeclaration hasCopyCtor(Scope* sc)
bool hasCopyCtor()
{
return null;
return false;
}

Expression buildVarInitializer(Scope* sc, VarDeclaration vd, Expression exp)
Expand Down
4 changes: 2 additions & 2 deletions dmd/aggregate.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class AggregateDeclaration : public ScopeDsymbol
Expression *defaultInitLiteral(Loc loc);
size_t literalElemDim(); // returns the total number of fields of an aggregate literal (TODO: better name?)
CtorDeclaration* hasImplicitCtor(Expression* farg);
virtual CtorDeclaration* hasCopyCtor(Scope* sc) { return nullptr; }
virtual bool hasCopyCtor() { return false; }
virtual Expression* buildVarInitializer(Scope* sc, VarDeclaration* vd, Expression* exp) { return NULL; }

AggregateDeclaration *isAggregateDeclaration() { return this; }
Expand All @@ -183,7 +183,7 @@ class StructDeclaration : public AggregateDeclaration
FuncDeclarations postblits; // Array of postblit functions
FuncDeclaration *postblit; // aggregate postblit

bool hasCopyCtor; // copy constructor
bool hasCopyCtor_; // copy constructor

FuncDeclaration *xeq; // TypeInfo_Struct.xopEquals
FuncDeclaration *xcmp; // TypeInfo_Struct.xopCmp
Expand Down
31 changes: 14 additions & 17 deletions dmd/cpp/cppaggregate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,42 +474,39 @@ bool ClassDeclaration::isBaseOf(::ClassDeclaration *cd, int *poffset)
}

template <typename AggTy>
::CtorDeclaration* ad_hasCopyCtor(AggTy* ad, Scope* sc)
bool ad_hasCopyCtor(AggTy* ad)
{
auto CRD = dyn_cast<clang::CXXRecordDecl>(ad->Definition());
if (!CRD->isCompleteDefinition())
return nullptr;
return false;

auto& S = calypso.getSema();
auto _CRD = const_cast<clang::CXXRecordDecl *>(CRD);

for (int i = 0; i < 2; i++)
if (auto Ctor = S.LookupCopyingConstructor(_CRD, i ? 0 : clang::Qualifiers::Const))
if (auto sym = dsymForDecl(ad, Ctor))
{
assert(sym->isCtorDeclaration());
return static_cast<::CtorDeclaration*>(sym);
}
if (dsymForDecl(ad, Ctor))
return true;

return nullptr;
return false;
}

::CtorDeclaration* StructDeclaration::hasCopyCtor(Scope* sc)
bool StructDeclaration::hasCopyCtor()
{
return ad_hasCopyCtor(this, sc);
return ad_hasCopyCtor(this);
}

::CtorDeclaration* ClassDeclaration::hasCopyCtor(Scope* sc)
bool ClassDeclaration::hasCopyCtor()
{
return ad_hasCopyCtor(this, sc);
return ad_hasCopyCtor(this);
}

inline ::CtorDeclaration* hasCopyCtor(AggregateDeclaration* sym, Scope* sc)
inline bool hasCopyCtor(AggregateDeclaration* sym)
{
assert(isCPP(sym));
return sym->isClassDeclaration() ?
static_cast<cpp::ClassDeclaration*>(sym)->hasCopyCtor(sc) :
static_cast<cpp::StructDeclaration*>(sym)->hasCopyCtor(sc);
static_cast<cpp::ClassDeclaration*>(sym)->hasCopyCtor() :
static_cast<cpp::StructDeclaration*>(sym)->hasCopyCtor();
}

template <typename AggTy>
Expand Down Expand Up @@ -731,9 +728,9 @@ Expression *LangPlugin::callCpCtor(Scope *sc, Expression *e)
{
Type* tv = e->type->baseElemOf();
assert(isAggregateValue(tv));
auto sym = getAggregateSym(tv);
auto sym = isAggregate(tv);

if (!hasCopyCtor(sym, sc))
if (!hasCopyCtor(sym))
return nullptr;

auto arguments = new Expressions;
Expand Down
4 changes: 2 additions & 2 deletions dmd/cpp/cppaggregate.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class StructDeclaration : public ::StructDeclaration
Expression *defaultInit(Loc loc) override;
bool isBaseOf(::ClassDeclaration* cd, int* poffset) override;
bool disableDefaultCtor() override { return false; }
::CtorDeclaration* hasCopyCtor(Scope* sc) override;
bool hasCopyCtor() override;
Expression* buildVarInitializer(Scope* sc, ::VarDeclaration* vd, Expression* exp) override;

const clang::RecordDecl *Definition();
Expand Down Expand Up @@ -86,7 +86,7 @@ class ClassDeclaration : public ::ClassDeclaration
bool byRef() const override { return false; }
Expression *defaultInit(Loc loc) override;
bool needsInterfaceSemantic() const override { return false; }
::CtorDeclaration* hasCopyCtor(Scope* sc) override;
bool hasCopyCtor() override;
Expression* buildVarInitializer(Scope* sc, ::VarDeclaration* vd, Expression* exp) override;

const clang::CXXRecordDecl *Definition();
Expand Down
2 changes: 1 addition & 1 deletion dmd/cpp/cppmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Dsymbols* DeclMapper::CreateTemplateInstanceFor(const SpecTy* D, Dsymbols* decld
ti->minst = minst;

auto cpptdtypes = c_td->tdtypesFromInst(minst ? minst->_scope : nullptr, ti->Inst);
// NOTE: minst may be null for speculative instances, e.g from hasCopyCtor()
// NOTE: minst may be null for speculative instances, e.g from clone.d
ti->tdtypes.setDim(cpptdtypes->dim);
memcpy(ti->tdtypes.tdata(), cpptdtypes->tdata(), cpptdtypes->dim * sizeof(void*));
delete cpptdtypes;
Expand Down
7 changes: 6 additions & 1 deletion dmd/dstruct.d
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ extern (C++) class StructDeclaration : AggregateDeclaration
FuncDeclarations postblits; // Array of postblit functions
FuncDeclaration postblit; // aggregate postblit

bool hasCopyCtor; // copy constructor
bool hasCopyCtor_; // copy constructor // CALYPSO

FuncDeclaration xeq; // TypeInfo_Struct.xopEquals
FuncDeclaration xcmp; // TypeInfo_Struct.xopCmp
Expand Down Expand Up @@ -476,6 +476,11 @@ extern (C++) class StructDeclaration : AggregateDeclaration
return (ispod == StructPOD.yes);
}

override bool hasCopyCtor()
{
return hasCopyCtor_;
}

bool disableDefaultCtor() // CALYPSO
{
return true;
Expand Down
8 changes: 4 additions & 4 deletions dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,10 @@ LcheckFields:
if (v.overlapped)
continue;

auto ts = v.type.baseElemOf().isTypeStruct();
if (!ts)
auto tb = v.type.baseElemOf(); // CALYPSO
if (!tb.isAggregateValue)
continue;
if (ts.sym.hasCopyCtor)
if (tb.isAggregate.hasCopyCtor)
{
fieldWithCpCtor = v;
break;
Expand Down Expand Up @@ -4788,7 +4788,7 @@ version (IN_LLVM)
sd.dtor = buildDtor(sd, sc2);
sd.tidtor = buildExternDDtor(sd, sc2);
sd.postblit = buildPostBlit(sd, sc2);
sd.hasCopyCtor = buildCopyCtor(sd, sc2);
sd.hasCopyCtor_ = buildCopyCtor(sd, sc2); // CALYPSO

buildOpAssign(sd, sc2);
buildOpEquals(sd, sc2);
Expand Down
21 changes: 10 additions & 11 deletions dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,7 @@ private bool functionParameters(const ref Loc loc, Scope* sc,
? p.type.substWildTo(wildmatch)
: p.type;

const hasCopyCtor = (arg.type.ty == Tstruct) && (cast(TypeStruct)arg.type).sym.hasCopyCtor;
const hasCopyCtor = arg.type.isAggregateValue && arg.type.isAggregate.hasCopyCtor; // CALYPSO
const typesMatch = arg.type.mutableOf().unSharedOf().equals(tprm.mutableOf().unSharedOf());
if (!((hasCopyCtor && typesMatch) || tprm.equals(arg.type)))
{
Expand Down Expand Up @@ -8499,8 +8499,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
result = e;
return;
}
if ((sd.isStructDeclaration() && (cast(StructDeclaration)sd).postblit)
|| sd.hasCopyCtor(sc)) // CALYPSO (not pretty)
if ((sd.isStructDeclaration() && (cast(StructDeclaration)sd).postblit) || sd.hasCopyCtor) // CALYPSO (not pretty)
{
/* We have a copy constructor for this
*/
Expand All @@ -8519,16 +8518,16 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor

if (e2x.isLvalue())
{
if (sd.langPlugin() && sd.hasCopyCtor(sc)) // CALYPSO HACK LDC 1.17 FIXME this needs to get merged with DMD's revived copy ctor stuff
if (sd.hasCopyCtor)
{
Expression e = callCpCtor(sc, e2x, null); // destinationType FIXME
e = new ConstructExp(exp.loc, e1x, e);
result = e.expressionSemantic(sc);
return;
}
if (auto lp = sd.langPlugin())
{
Expression e = lp.callCpCtor(sc, e2x);
e = new ConstructExp(exp.loc, e1x, e);
result = e.expressionSemantic(sc);
return;
}

if ((cast(StructDeclaration)sd).hasCopyCtor) // CALYPSO FIXME
{
/* Rewrite as:
* e1 = init, e1.copyCtor(e2);
*/
Expand Down
10 changes: 5 additions & 5 deletions dmd/mtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -4785,18 +4785,18 @@ extern (C++) final class TypeFunction : TypeNext
{
const isRef = (p.storageClass & (STC.ref_ | STC.out_)) != 0;

StructDeclaration argStruct, prmStruct;
AggregateDeclaration argStruct, prmStruct;

// first look for a copy constructor
if (arg.isLvalue() && !isRef && targ.ty == Tstruct && tprm.ty == Tstruct)
if (arg.isLvalue() && !isRef && targ.isAggregateValue && tprm.isAggregateValue) // CALYPSO
{
// if the argument and the parameter are of the same unqualified struct type
argStruct = (cast(TypeStruct)targ).sym;
prmStruct = (cast(TypeStruct)tprm).sym;
argStruct = targ.isAggregate();
prmStruct = tprm.isAggregate();
}

// check if the copy constructor may be called to copy the argument
if (argStruct && argStruct == prmStruct && argStruct.hasCopyCtor)
if (argStruct && argStruct == prmStruct && argStruct.hasCopyCtor && !argStruct.langPlugin) // CALYPSO HACK disable for now (TODO create a hook to skip the temporary creation?)
{
/* this is done by seeing if a call to the copy constructor can be made:
*
Expand Down
2 changes: 1 addition & 1 deletion dmd/semantic3.d
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ version (IN_LLVM)
}
}

const hasCopyCtor = exp.type.ty == Tstruct && (cast(TypeStruct)exp.type).sym.hasCopyCtor;
const hasCopyCtor = exp.type.isAggregateValue && exp.type.isAggregate.hasCopyCtor; // CALYPSO
// if a copy constructor is present, the return type conversion will be handled by it
if (!hasCopyCtor)
exp = exp.implicitCastTo(sc2, tret);
Expand Down
2 changes: 1 addition & 1 deletion gen/abi-win64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct Win64TargetABI : TargetABI {
if (isMSVCpp && t->ty == Tstruct) {
StructDeclaration *sd = static_cast<TypeStruct *>(t)->sym;
assert(sd);
if (sd->postblit || sd->hasCopyCtor)
if (sd->postblit || sd->hasCopyCtor()) // CALYPSO
return true;
}
}
Expand Down

0 comments on commit 4f1c66b

Please sign in to comment.