diff --git a/src/Generator/Driver.cs b/src/Generator/Driver.cs index c1e17a6f..5a40a350 100644 --- a/src/Generator/Driver.cs +++ b/src/Generator/Driver.cs @@ -311,6 +311,7 @@ namespace CppSharp TranslationUnitPasses.AddPass(new CheckIgnoredDeclsPass()); TranslationUnitPasses.AddPass(new CheckFlagEnumsPass()); TranslationUnitPasses.AddPass(new CheckDuplicatedNamesPass()); + if (Options.IsCSharpGenerator) { TranslationUnitPasses.AddPass(new GenerateAbstractImplementationsPass()); @@ -319,16 +320,11 @@ namespace CppSharp TranslationUnitPasses.AddPass(new FixDefaultParamValuesOfOverridesPass()); TranslationUnitPasses.AddPass(new HandleDefaultParamValuesPass()); } - } - - if (Options.IsCSharpGenerator) - { TranslationUnitPasses.AddPass(new MultipleInheritancePass()); TranslationUnitPasses.AddPass(new ParamTypeToInterfacePass()); - } - - if (Options.IsCSharpGenerator) TranslationUnitPasses.AddPass(new DelegatesPass()); + TranslationUnitPasses.AddPass(new EqualiseAccessOfOverrideAndBasePass()); + } TranslationUnitPasses.AddPass(new GetterSetterToPropertyPass()); TranslationUnitPasses.AddPass(new StripUnusedSystemTypesPass()); diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index ba97de18..41cffb24 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -1238,7 +1238,7 @@ namespace CppSharp.Generators.CSharp var printedType = prop.Type.Visit(TypePrinter); if (prop.ExplicitInterfaceImpl == null) { - Write(Helpers.GetAccess(GetValidPropertyAccess(prop))); + Write(Helpers.GetAccess(prop.Access)); if (prop.IsStatic) Write("static "); @@ -2227,7 +2227,7 @@ namespace CppSharp.Generators.CSharp if (method.ExplicitInterfaceImpl == null) { - Write(Helpers.GetAccess(GetValidMethodAccess(method))); + Write(Helpers.GetAccess(method.Access)); } GenerateMethodSpecifier(method, @class); @@ -2431,25 +2431,6 @@ namespace CppSharp.Generators.CSharp } } - private static AccessSpecifier GetValidMethodAccess(Method method) - { - if (!method.IsOverride) - return method.Access; - var baseMethod = ((Class) method.Namespace).GetBaseMethod(method); - return baseMethod.IsGenerated ? baseMethod.Access : method.Access; - } - - private static AccessSpecifier GetValidPropertyAccess(Property property) - { - if (property.Access == AccessSpecifier.Public) - return AccessSpecifier.Public; - if (!property.IsOverride) - return property.Access; - var baseProperty = ((Class) property.Namespace).GetBaseProperty(property); - // access can be changed from private to other while overriding in C++ - return baseProperty != null ? baseProperty.Access : property.Access; - } - private void GenerateVirtualPropertyCall(Method method, Class @class, Property property, List parameters = null) { diff --git a/src/Generator/Passes/EqualiseAccessOfOverrideAndBasePass.cs b/src/Generator/Passes/EqualiseAccessOfOverrideAndBasePass.cs new file mode 100644 index 00000000..513b9ce5 --- /dev/null +++ b/src/Generator/Passes/EqualiseAccessOfOverrideAndBasePass.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.Linq; +using CppSharp.AST; + +namespace CppSharp.Passes +{ + public class EqualiseAccessOfOverrideAndBasePass : TranslationUnitPass + { + public override bool VisitASTContext(ASTContext context) + { + var result = base.VisitASTContext(context); + + foreach (var baseOverride in basesOverrides) + { + var access = baseOverride.Value.Max(o => o.Access); + foreach (var @override in baseOverride.Value) + @override.Access = access; + } + + return result; + } + + public override bool VisitMethodDecl(Method method) + { + if (!base.VisitMethodDecl(method) || !method.IsOverride) + return false; + + var baseMethod = ((Class) method.Namespace).GetBaseMethod(method); + if (!baseMethod.IsGenerated) + return false; + + HashSet overrides; + if (basesOverrides.ContainsKey(baseMethod)) + overrides = basesOverrides[baseMethod]; + else + overrides = basesOverrides[baseMethod] = new HashSet { baseMethod }; + overrides.Add(method); + + return true; + } + + private Dictionary> basesOverrides = new Dictionary>(); + } +} diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index 13256001..b5dd96ac 100644 --- a/tests/Common/Common.cpp +++ b/tests/Common/Common.cpp @@ -592,6 +592,15 @@ void HasVirtualProperty::setProperty(int target) { } +int HasVirtualProperty::getProtectedProperty() +{ + return 2; +} + +void HasVirtualProperty::setProtectedProperty(int value) +{ +} + ChangedAccessOfInheritedProperty::ChangedAccessOfInheritedProperty() { } @@ -605,6 +614,15 @@ void ChangedAccessOfInheritedProperty::setProperty(int value) { } +int ChangedAccessOfInheritedProperty::getProtectedProperty() +{ + return 3; +} + +void ChangedAccessOfInheritedProperty::setProtectedProperty(int value) +{ +} + Empty ReturnsEmpty::getEmpty() { return Empty(); diff --git a/tests/Common/Common.h b/tests/Common/Common.h index beb7216a..c6b3336a 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -901,12 +901,17 @@ class DLL_API HasVirtualProperty public: virtual int getProperty(); virtual void setProperty(int target); +protected: + virtual int getProtectedProperty(); + virtual void setProtectedProperty(int value); }; class DLL_API ChangedAccessOfInheritedProperty : public HasVirtualProperty { public: ChangedAccessOfInheritedProperty(); + int getProtectedProperty(); + void setProtectedProperty(int value); protected: int getProperty(); void setProperty(int value);