Browse Source

Fixed the generated C# with 4+ modules and repetitive delegates.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/955/head
Dimitar Dobrev 8 years ago
parent
commit
3ce995ede0
  1. 31
      src/Generator/Passes/DelegatesPass.cs

31
src/Generator/Passes/DelegatesPass.cs

@ -38,6 +38,14 @@ namespace CppSharp.Passes
return result; return result;
} }
public override bool VisitClassDecl(Class @class)
{
if (@class.Ignore)
return false;
return base.VisitClassDecl(@class);
}
public override bool VisitMethodDecl(Method method) public override bool VisitMethodDecl(Method method)
{ {
if (!base.VisitMethodDecl(method) || !method.IsVirtual || method.Ignore) if (!base.VisitMethodDecl(method) || !method.IsVirtual || method.Ignore)
@ -59,7 +67,7 @@ namespace CppSharp.Passes
public override bool VisitParameterDecl(Parameter parameter) public override bool VisitParameterDecl(Parameter parameter)
{ {
if (!base.VisitParameterDecl(parameter) || parameter.Namespace == null || if (!base.VisitDeclaration(parameter) || parameter.Namespace == null ||
parameter.Namespace.Ignore) parameter.Namespace.Ignore)
return false; return false;
@ -89,13 +97,15 @@ namespace CppSharp.Passes
return new QualifiedType(new TypedefType { Declaration = @delegate }); return new QualifiedType(new TypedefType { Declaration = @delegate });
} }
private TypedefDecl GetDelegate(QualifiedType type, ITypedDecl decl) private TypedefDecl GetDelegate(QualifiedType type, ITypedDecl typedDecl)
{ {
FunctionType newFunctionType = GetNewFunctionType(decl, type); FunctionType newFunctionType = GetNewFunctionType(typedDecl, type);
var delegateName = GetDelegateName(newFunctionType); var delegateName = GetDelegateName(newFunctionType);
var access = decl is Method ? AccessSpecifier.Private : AccessSpecifier.Public; var access = typedDecl is Method ? AccessSpecifier.Private : AccessSpecifier.Public;
var existingDelegate = delegates.SingleOrDefault(t => t.Name == delegateName); var decl = (Declaration) typedDecl;
Module module = decl.TranslationUnit.Module;
var existingDelegate = delegates.Find(t => Match(t, delegateName, module));
if (existingDelegate != null) if (existingDelegate != null)
{ {
// Ensure a delegate used for a virtual method and a type is public // Ensure a delegate used for a virtual method and a type is public
@ -110,12 +120,12 @@ namespace CppSharp.Passes
// Add a new delegate with the calling convention appended to its name // Add a new delegate with the calling convention appended to its name
delegateName += '_' + newFunctionType.CallingConvention.ToString(); delegateName += '_' + newFunctionType.CallingConvention.ToString();
existingDelegate = delegates.SingleOrDefault(t => t.Name == delegateName); existingDelegate = delegates.Find(t => Match(t, delegateName, module));
if (existingDelegate != null) if (existingDelegate != null)
return existingDelegate; return existingDelegate;
} }
var namespaceDelegates = GetDeclContextForDelegates(((Declaration) decl).Namespace); var namespaceDelegates = GetDeclContextForDelegates(decl.Namespace);
var delegateType = new QualifiedType(new PointerType(new QualifiedType(newFunctionType))); var delegateType = new QualifiedType(new PointerType(new QualifiedType(newFunctionType)));
existingDelegate = new TypedefDecl existingDelegate = new TypedefDecl
{ {
@ -154,6 +164,13 @@ namespace CppSharp.Passes
return functionType; return functionType;
} }
private static bool Match(TypedefDecl t, string delegateName, Module module)
{
return t.Name == delegateName &&
(module == t.TranslationUnit.Module ||
module.Dependencies.Contains(t.TranslationUnit.Module));
}
private DeclarationContext GetDeclContextForDelegates(DeclarationContext @namespace) private DeclarationContext GetDeclContextForDelegates(DeclarationContext @namespace)
{ {
if (Options.IsCLIGenerator) if (Options.IsCLIGenerator)

Loading…
Cancel
Save