diff --git a/CfrontCodeGenerator.cpp b/CfrontCodeGenerator.cpp index 92c11c4..4217c94 100644 --- a/CfrontCodeGenerator.cpp +++ b/CfrontCodeGenerator.cpp @@ -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(stmt)))); + OutputFormatHelper initOutputFormatHelper{}; initOutputFormatHelper.SetIndent(mOutputFormatHelper, OutputFormatHelper::SkipIndenting::Yes); @@ -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); @@ -520,7 +522,6 @@ void CfrontCodeGenerator::InsertCXXMethodDecl(const CXXMethodDecl* stmt, SkipBod // Skip ctor for trivial types if(const auto* ctorDecl = dyn_cast_or_null(stmt)) { - const auto* parent = stmt->getParent(); if(not stmt->doesThisDeclarationHaveABody()) { @@ -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 diff --git a/tests/EduCfrontTest.expect b/tests/EduCfrontTest.expect index 08b35d4..effddc6 100644 --- a/tests/EduCfrontTest.expect +++ b/tests/EduCfrontTest.expect @@ -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) { diff --git a/tests/EduCfrontTest14.expect b/tests/EduCfrontTest14.expect index 47afab0..41732c0 100644 --- a/tests/EduCfrontTest14.expect +++ b/tests/EduCfrontTest14.expect @@ -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) { diff --git a/tests/EduCfrontTest5.expect b/tests/EduCfrontTest5.expect index d8343fa..4c2b125 100644 --- a/tests/EduCfrontTest5.expect +++ b/tests/EduCfrontTest5.expect @@ -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 { @@ -51,11 +41,6 @@ inline void Destructor_Type(Type * __this) { } -inline Type * operatorEqual(Type * __this, const Type * __rhs) -{ - return __this; -} - typedef struct Base { @@ -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]; @@ -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]; @@ -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); diff --git a/tests/EduCfrontTest7.expect b/tests/EduCfrontTest7.expect index 795c73b..54199ca 100644 --- a/tests/EduCfrontTest7.expect +++ b/tests/EduCfrontTest7.expect @@ -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 { @@ -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]; @@ -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; @@ -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]; @@ -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 */ diff --git a/tests/EduCfrontVtable2Test.expect b/tests/EduCfrontVtable2Test.expect index ec8e613..ab5c98e 100644 --- a/tests/EduCfrontVtable2Test.expect +++ b/tests/EduCfrontVtable2Test.expect @@ -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]; @@ -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]; @@ -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); @@ -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); diff --git a/tests/EduCfrontVtable3Test.expect b/tests/EduCfrontVtable3Test.expect index 544c61b..1b15c6f 100644 --- a/tests/EduCfrontVtable3Test.expect +++ b/tests/EduCfrontVtable3Test.expect @@ -29,18 +29,6 @@ inline void FunA(A * __this) puts("fun a"); } -inline A * operatorEqual(A * __this, const A * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - -inline A * operatorEqual(A * __this, A * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - inline A * Constructor_A(A * __this) { __this->__vptrA = __vtbl_array[0]; @@ -64,22 +52,6 @@ inline void OtherB(B * __this) { } -inline B * operatorEqual(B * __this, const B * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - -inline B * operatorEqual(B * __this, B * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - inline B * Constructor_B(B * __this) { Constructor_A((A *)__this); @@ -106,26 +78,9 @@ inline void OtherC(C * __this) { } -inline C * operatorEqual(C * __this, const C * __rhs) -{ - __this->mX = __rhs->mX; - operatorEqual((B *)__this, (B *)__rhs); - __this->mB = __rhs->mB; - return __this; -} - -inline C * operatorEqual(C * __this, C * __rhs) -{ - __this->mX = __rhs->mX; - operatorEqual((B *)__this, (B *)__rhs); - __this->mB = __rhs->mB; - return __this; -} - inline C * Constructor_C(C * __this) { Constructor_B((B *)__this); - __this->mX = 5; __this->__vptrA = __vtbl_array[2]; __this->mB = 8; return __this; diff --git a/tests/EduCfrontVtable4Test.expect b/tests/EduCfrontVtable4Test.expect index 8b49e8f..aee0b01 100644 --- a/tests/EduCfrontVtable4Test.expect +++ b/tests/EduCfrontVtable4Test.expect @@ -34,12 +34,6 @@ inline void FunA(A * __this) puts("fun a"); } -inline A * operatorEqual(A * __this, const A * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - inline A * Constructor_A(A * __this) { __this->__vptrA = __vtbl_array[0]; @@ -63,22 +57,6 @@ inline void OtherB(B * __this) { } -inline B * operatorEqual(B * __this, const B * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - -inline B * operatorEqual(B * __this, B * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - inline void Destructor_B(B * __this) { Destructor_A((A *)__this); @@ -110,22 +88,6 @@ inline void OtherC(C * __this) { } -inline C * operatorEqual(C * __this, const C * __rhs) -{ - __this->mX = __rhs->mX; - operatorEqual((B *)__this, (B *)__rhs); - __this->mB = __rhs->mB; - return __this; -} - -inline C * operatorEqual(C * __this, C * __rhs) -{ - __this->mX = __rhs->mX; - operatorEqual((B *)__this, (B *)__rhs); - __this->mB = __rhs->mB; - return __this; -} - inline void Destructor_C(C * __this) { Destructor_B((B *)__this); @@ -134,7 +96,6 @@ inline void Destructor_C(C * __this) inline C * Constructor_C(C * __this) { Constructor_B((B *)__this); - __this->mX = 5; __this->__vptrA = __vtbl_array[2]; __this->mB = 8; return __this; diff --git a/tests/EduCfrontVtable5Test.expect b/tests/EduCfrontVtable5Test.expect index 5a03447..f27e6ec 100644 --- a/tests/EduCfrontVtable5Test.expect +++ b/tests/EduCfrontVtable5Test.expect @@ -28,11 +28,6 @@ inline void Destructor_Base(Base * __this) puts("~Base"); } -inline Base * operatorEqual(Base * __this, const Base * __rhs) -{ - return __this; -} - inline Base * Constructor_Base(Base * __this) { __this->__vptrBase = __vtbl_array[0]; @@ -51,12 +46,6 @@ inline void Destructor_Derived(Derived * __this) Destructor_Base((Base *)__this); } -inline Derived * operatorEqual(Derived * __this, const Derived * __rhs) -{ - operatorEqual((Base *)__this, (Base *)__rhs); - return __this; -} - inline Derived * Constructor_Derived(Derived * __this) { Constructor_Base((Base *)__this); diff --git a/tests/EduCfrontVtable6Test.expect b/tests/EduCfrontVtable6Test.expect index 181622f..40f893d 100644 --- a/tests/EduCfrontVtable6Test.expect +++ b/tests/EduCfrontVtable6Test.expect @@ -23,18 +23,6 @@ typedef struct A double md; } A; -inline A * operatorEqual(A * __this, const A * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - -inline A * operatorEqual(A * __this, A * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - typedef struct B { @@ -47,18 +35,6 @@ inline void FunB(B * __this) puts("B::Fun"); } -inline B * operatorEqual(B * __this, const B * __rhs) -{ - __this->mX = __rhs->mX; - return __this; -} - -inline B * operatorEqual(B * __this, B * __rhs) -{ - __this->mX = __rhs->mX; - return __this; -} - inline B * Constructor_B(B * __this) { __this->__vptrB = __vtbl_array[0]; @@ -80,30 +56,9 @@ inline void FunC(C * __this) puts("C::Fun"); } -inline C * operatorEqual(C * __this, const C * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - operatorEqual((B *)__this, (B *)__rhs); - __this->mB = __rhs->mB; - return __this; -} - -inline C * operatorEqual(C * __this, C * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - operatorEqual((B *)__this, (B *)__rhs); - __this->mB = __rhs->mB; - return __this; -} - inline C * Constructor_C(C * __this) { Constructor_B((B *)__this); - __this->mX = 5; __this->__vptrB = __vtbl_array[1]; __this->mB = 8; return __this; diff --git a/tests/EduCfrontVtable7Test.expect b/tests/EduCfrontVtable7Test.expect index 4d62a2b..995b45e 100644 --- a/tests/EduCfrontVtable7Test.expect +++ b/tests/EduCfrontVtable7Test.expect @@ -34,12 +34,6 @@ inline void FunFruit(Fruit * __this) puts("Fruit's Fun"); } -inline Fruit * operatorEqual(Fruit * __this, const Fruit * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - inline Fruit * Constructor_Fruit(Fruit * __this) { __this->__vptrFruit = __vtbl_array[0]; @@ -59,22 +53,6 @@ inline void FunApple(Apple * __this) printf("Apple's Fun: %d\n", __this->mX); } -inline Apple * operatorEqual(Apple * __this, const Apple * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((Fruit *)__this, (Fruit *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - -inline Apple * operatorEqual(Apple * __this, Apple * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((Fruit *)__this, (Fruit *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - inline void Destructor_Apple(Apple * __this) { Destructor_Fruit((Fruit *)__this); @@ -102,22 +80,6 @@ inline void FunPinkLady(PinkLady * __this) printf("Pink Ladies Fun: %d\n", __this->mApple); } -inline PinkLady * operatorEqual(PinkLady * __this, const PinkLady * __rhs) -{ - __this->mX = __rhs->mX; - operatorEqual((Apple *)__this, (Apple *)__rhs); - __this->mApple = __rhs->mApple; - return __this; -} - -inline PinkLady * operatorEqual(PinkLady * __this, PinkLady * __rhs) -{ - __this->mX = __rhs->mX; - operatorEqual((Apple *)__this, (Apple *)__rhs); - __this->mApple = __rhs->mApple; - return __this; -} - inline void Destructor_PinkLady(PinkLady * __this) { Destructor_Apple((Apple *)__this); @@ -126,7 +88,6 @@ inline void Destructor_PinkLady(PinkLady * __this) inline PinkLady * Constructor_PinkLady(PinkLady * __this) { Constructor_Apple((Apple *)__this); - __this->mX = 5; __this->__vptrFruit = __vtbl_array[2]; __this->mApple = 8; return __this; diff --git a/tests/EduCfrontVtablePureFuncTest.expect b/tests/EduCfrontVtablePureFuncTest.expect index 3c250a5..5741b92 100644 --- a/tests/EduCfrontVtablePureFuncTest.expect +++ b/tests/EduCfrontVtablePureFuncTest.expect @@ -26,8 +26,6 @@ typedef struct A double md; } A; -void FunA(A * __this); - inline A * Constructor_A(A * __this) { __this->__vptrA = __vtbl_array[0]; @@ -35,18 +33,6 @@ inline A * Constructor_A(A * __this) return __this; } -inline A * operatorEqual(A * __this, const A * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - -inline A * operatorEqual(A * __this, A * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - typedef struct B { @@ -64,22 +50,6 @@ inline void OtherB(B * __this) { } -inline B * operatorEqual(B * __this, const B * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - -inline B * operatorEqual(B * __this, B * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - inline B * Constructor_B(B * __this) { Constructor_A((A *)__this); diff --git a/tests/EduCfrontVtableTest.expect b/tests/EduCfrontVtableTest.expect index be65779..9875b77 100644 --- a/tests/EduCfrontVtableTest.expect +++ b/tests/EduCfrontVtableTest.expect @@ -29,18 +29,6 @@ inline void FunA(A * __this) puts("fun a"); } -inline A * operatorEqual(A * __this, const A * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - -inline A * operatorEqual(A * __this, A * __rhs) -{ - __this->md = __rhs->md; - return __this; -} - inline A * Constructor_A(A * __this) { __this->__vptrA = __vtbl_array[0]; @@ -64,22 +52,6 @@ inline void OtherB(B * __this) { } -inline B * operatorEqual(B * __this, const B * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - -inline B * operatorEqual(B * __this, B * __rhs) -{ - __this->md = __rhs->md; - operatorEqual((A *)__this, (A *)__rhs); - __this->mX = __rhs->mX; - return __this; -} - inline B * Constructor_B(B * __this) { Constructor_A((A *)__this);