Skip to content

Commit

Permalink
Merge pull request #666 from andreasfertig/cfrontFixes
Browse files Browse the repository at this point in the history
Suppress unused assignment operators in Cfront mode.
  • Loading branch information
andreasfertig authored Sep 18, 2024
2 parents 751c3d9 + d29fc31 commit cba412f
Show file tree
Hide file tree
Showing 13 changed files with 4 additions and 412 deletions.
9 changes: 4 additions & 5 deletions CfrontCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,10 @@ static void InsertVtblPtr(const CXXMethodDecl* stmt, const CXXRecordDecl* cur, S

void CfrontCodeGenerator::InsertCXXMethodDecl(const CXXMethodDecl* stmt, SkipBody)
{
// Skip if he method is unused like assignment operators by default.
RETURN_IF(not stmt->isUsed() and
(IsCopyOrMoveAssign(stmt) or (not stmt->hasBody() and not isa<CXXConstructorDecl>(stmt))));

OutputFormatHelper initOutputFormatHelper{};
initOutputFormatHelper.SetIndent(mOutputFormatHelper, OutputFormatHelper::SkipIndenting::Yes);

Expand Down Expand Up @@ -503,8 +507,6 @@ void CfrontCodeGenerator::InsertCXXMethodDecl(const CXXMethodDecl* stmt, SkipBod
for(const auto& base : parent->bases()) {
const auto rd = base.getType()->getAsRecordDecl();

insertFields(rd);

auto* lhsCast = StaticCast(GetRecordDeclType(rd), thisOfParent, true);
auto* rhsDeclRef = mkVarDeclRefExpr("__rhs"sv, parentType);
auto* rhsCast = StaticCast(GetRecordDeclType(rd), rhsDeclRef, true);
Expand All @@ -520,7 +522,6 @@ void CfrontCodeGenerator::InsertCXXMethodDecl(const CXXMethodDecl* stmt, SkipBod

// Skip ctor for trivial types
if(const auto* ctorDecl = dyn_cast_or_null<CXXConstructorDecl>(stmt)) {

const auto* parent = stmt->getParent();

if(not stmt->doesThisDeclarationHaveABody()) {
Expand Down Expand Up @@ -582,8 +583,6 @@ void CfrontCodeGenerator::InsertCXXMethodDecl(const CXXMethodDecl* stmt, SkipBod
baseRd and baseRd->hasNonTrivialDefaultConstructor()) {
bodyStmts.AddBodyStmts(CallConstructor(baseType, Ptr(baseType), nullptr, {}, DoCast::Yes));
}

insertFields(baseType->getAsRecordDecl());
}

// insert our vtable pointer
Expand Down
9 changes: 0 additions & 9 deletions tests/EduCfrontTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ inline Other * Constructor_Other(Other * __this, int xy)
return __this;
}

inline Other * operatorEqual(Other * __this, Other * __rhs)
{
operatorEqual(&__this->mA, &__rhs->mA);
__this->y = __rhs->y;
return __this;
}

inline void Destructor_Other(Other * __this);


int __main(void)
{
Expand Down
5 changes: 0 additions & 5 deletions tests/EduCfrontTest14.expect
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ inline B * MoveConstructor_B(B * __this, B * __rhs)
return __this;
}

inline B * operatorEqual(B * __this, const B * __rhs)
{
return __this;
}


int __main(void)
{
Expand Down
40 changes: 0 additions & 40 deletions tests/EduCfrontTest5.expect
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ inline WithDefaultCtor * Constructor_WithDefaultCtor(WithDefaultCtor * __this)
return __this;
}

inline WithDefaultCtor * operatorEqual(WithDefaultCtor * __this, const WithDefaultCtor * __rhs)
{
return __this;
}

inline WithDefaultCtor * operatorEqual(WithDefaultCtor * __this, WithDefaultCtor * __rhs)
{
return __this;
}


typedef struct Type
{
Expand All @@ -51,11 +41,6 @@ inline void Destructor_Type(Type * __this)
{
}

inline Type * operatorEqual(Type * __this, const Type * __rhs)
{
return __this;
}


typedef struct Base
{
Expand All @@ -67,12 +52,6 @@ inline void Destructor_Base(Base * __this)
{
}

inline Base * operatorEqual(Base * __this, const Base * __rhs)
{
operatorEqual(&__this->mY, &__rhs->mY);
return __this;
}

inline Base * Constructor_Base(Base * __this)
{
__this->__vptrBase = __vtbl_array[0];
Expand All @@ -91,12 +70,6 @@ inline void Destructor_BaseSecond(BaseSecond * __this)
{
}

inline BaseSecond * operatorEqual(BaseSecond * __this, const BaseSecond * __rhs)
{
operatorEqual(&__this->mX, &__rhs->mX);
return __this;
}

inline BaseSecond * Constructor_BaseSecond(BaseSecond * __this)
{
__this->__vptrBaseSecond = __vtbl_array[1];
Expand All @@ -122,23 +95,10 @@ inline void Destructor_Derived(Derived * __this)
Destructor_Base((Base *)__this);
}

inline Derived * operatorEqual(Derived * __this, const Derived * __rhs)
{
operatorEqual(&__this->mY, &__rhs->mY);
operatorEqual((Base *)__this, (Base *)__rhs);
operatorEqual(&__this->mX, &__rhs->mX);
operatorEqual((BaseSecond *)((char*)__this+16), (BaseSecond *)((char*)__rhs+16));
__this->mD = __rhs->mD;
operatorEqual(&__this->mWd, &__rhs->mWd);
return __this;
}

inline Derived * Constructor_Derived(Derived * __this)
{
Constructor_Base((Base *)__this);
Constructor_Type(&__this->mY, 5);
Constructor_BaseSecond((BaseSecond *)__this);
Constructor_Type(&__this->mX, 5);
__this->__vptrBase = __vtbl_array[2];
__this->__vptrBaseSecond = __vtbl_array[3];
Constructor_WithDefaultCtor(&__this->mWd);
Expand Down
52 changes: 0 additions & 52 deletions tests/EduCfrontTest7.expect
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,12 @@ inline void Destructor_Type(Type * __this)
{
}

inline Type * operatorEqual(Type * __this, const Type * __rhs)
{
__this->tint = __rhs->tint;
__this->oint = __rhs->oint;
return __this;
}


typedef struct DefaultCtorType
{
char __dummy;
} DefaultCtorType;

inline DefaultCtorType * operatorEqual(DefaultCtorType * __this, const DefaultCtorType * __rhs)
{
return __this;
}

inline DefaultCtorType * operatorEqual(DefaultCtorType * __this, DefaultCtorType * __rhs)
{
return __this;
}


typedef struct Base
{
Expand All @@ -67,12 +50,6 @@ inline void Destructor_Base(Base * __this)
{
}

inline Base * operatorEqual(Base * __this, const Base * __rhs)
{
operatorEqual(&__this->mX, &__rhs->mX);
return __this;
}

inline Base * Constructor_Base(Base * __this)
{
__this->__vptrBase = __vtbl_array[0];
Expand All @@ -93,18 +70,9 @@ inline void Destructor_BaseSecond(BaseSecond * __this)
Destructor_Base((Base *)__this);
}

inline BaseSecond * operatorEqual(BaseSecond * __this, const BaseSecond * __rhs)
{
operatorEqual(&__this->mX, &__rhs->mX);
operatorEqual((Base *)__this, (Base *)__rhs);
operatorEqual(&__this->mY, &__rhs->mY);
return __this;
}

inline BaseSecond * Constructor_BaseSecond(BaseSecond * __this)
{
Constructor_Base((Base *)__this);
Constructor_Type(&__this->mX, 5);
__this->__vptrBase = __vtbl_array[1];
Constructor_Type(&__this->mY, 5);
return __this;
Expand All @@ -121,12 +89,6 @@ inline void Destructor_BaseThird(BaseThird * __this)
{
}

inline BaseThird * operatorEqual(BaseThird * __this, const BaseThird * __rhs)
{
operatorEqual(&__this->mZ, &__rhs->mZ);
return __this;
}

inline BaseThird * Constructor_BaseThird(BaseThird * __this)
{
__this->__vptrBaseThird = __vtbl_array[2];
Expand Down Expand Up @@ -154,24 +116,10 @@ inline void Destructor_Derived(Derived * __this)
Destructor_BaseSecond((BaseSecond *)__this);
}

inline Derived * operatorEqual(Derived * __this, const Derived * __rhs)
{
operatorEqual(&__this->mY, &__rhs->mY);
operatorEqual((BaseSecond *)__this, (BaseSecond *)__rhs);
operatorEqual(&__this->mZ, &__rhs->mZ);
operatorEqual((BaseThird *)((char*)__this+16), (BaseThird *)((char*)__rhs+16));
__this->mD = __rhs->mD;
operatorEqual(&__this->dt, &__rhs->dt);
__this->g = __rhs->g;
return __this;
}

inline Derived * Constructor_Derived(Derived * __this)
{
Constructor_BaseSecond((BaseSecond *)__this);
Constructor_Type(&__this->mY, 5);
Constructor_BaseThird((BaseThird *)__this);
Constructor_Type(&__this->mZ, 5);
__this->__vptrBase = __vtbl_array[3];
__this->__vptrBaseThird = __vtbl_array[4];
/* dt // trivial type, maybe uninitialized */
Expand Down
64 changes: 0 additions & 64 deletions tests/EduCfrontVtable2Test.expect
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@ inline void vA(A * __this)
puts("A->v");
}

inline A * operatorEqual(A * __this, const A * __rhs)
{
__this->a = __rhs->a;
return __this;
}

inline A * operatorEqual(A * __this, A * __rhs)
{
__this->a = __rhs->a;
return __this;
}

inline A * Constructor_A(A * __this)
{
__this->__vptrA = __vtbl_array[0];
Expand All @@ -59,18 +47,6 @@ inline void wB(B * __this)
puts("B->w");
}

inline B * operatorEqual(B * __this, const B * __rhs)
{
__this->b = __rhs->b;
return __this;
}

inline B * operatorEqual(B * __this, B * __rhs)
{
__this->b = __rhs->b;
return __this;
}

inline B * Constructor_B(B * __this)
{
__this->__vptrB = __vtbl_array[1];
Expand All @@ -92,26 +68,6 @@ inline void wC(C * __this)
puts("C->w");
}

inline C * operatorEqual(C * __this, const C * __rhs)
{
__this->a = __rhs->a;
operatorEqual((A *)__this, (A *)__rhs);
__this->b = __rhs->b;
operatorEqual((B *)((char*)__this+16), (B *)((char*)__rhs+16));
__this->c = __rhs->c;
return __this;
}

inline C * operatorEqual(C * __this, C * __rhs)
{
__this->a = __rhs->a;
operatorEqual((A *)__this, (A *)__rhs);
__this->b = __rhs->b;
operatorEqual((B *)((char*)__this+16), (B *)((char*)__rhs+16));
__this->c = __rhs->c;
return __this;
}

inline C * Constructor_C(C * __this)
{
Constructor_A((A *)__this);
Expand All @@ -131,26 +87,6 @@ typedef struct D
int d;
} D;

inline D * operatorEqual(D * __this, const D * __rhs)
{
__this->a = __rhs->a;
operatorEqual((A *)__this, (A *)__rhs);
__this->b = __rhs->b;
operatorEqual((B *)((char*)__this+16), (B *)((char*)__rhs+16));
__this->d = __rhs->d;
return __this;
}

inline D * operatorEqual(D * __this, D * __rhs)
{
__this->a = __rhs->a;
operatorEqual((A *)__this, (A *)__rhs);
__this->b = __rhs->b;
operatorEqual((B *)((char*)__this+16), (B *)((char*)__rhs+16));
__this->d = __rhs->d;
return __this;
}

inline D * Constructor_D(D * __this)
{
Constructor_A((A *)__this);
Expand Down
Loading

0 comments on commit cba412f

Please sign in to comment.