From 930dd85f1506300fa712cf1145998825c4c6ecc3 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sat, 4 Feb 2017 14:41:40 +0200 Subject: [PATCH] Ensured delegates are generated within the main name-space and reused them more. --- .../Generators/CSharp/CSharpTypePrinter.cs | 40 ++++----------- src/Generator/Passes/DelegatesPass.cs | 49 ++++++++++--------- 2 files changed, 36 insertions(+), 53 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index ef36f5c9..4682f2e8 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -34,10 +34,7 @@ namespace CppSharp.Generators.CSharp return new CSharpTypePrinterResult {Type = type}; } - public override string ToString() - { - return Type; - } + public override string ToString() => Type; } public class CSharpTypePrinter : ITypePrinter, @@ -45,32 +42,21 @@ namespace CppSharp.Generators.CSharp { public const string IntPtrType = "global::System.IntPtr"; - public static bool AppendGlobal { get; set; } - - static CSharpTypePrinter() - { - AppendGlobal = true; - } + public bool FullyQualify { get; set; } = true; private readonly Stack contexts; private readonly Stack marshalKinds; - public CSharpTypePrinterContextKind ContextKind - { - get { return contexts.Peek(); } - } + public CSharpTypePrinterContextKind ContextKind => contexts.Peek(); - public CSharpMarshalKind MarshalKind - { - get { return marshalKinds.Peek(); } - } + public CSharpMarshalKind MarshalKind => marshalKinds.Peek(); public CSharpTypePrinterContext TypePrinterContext; public BindingContext Context { get; set; } - public DriverOptions Options { get { return Context.Options; } } - public TypeMapDatabase TypeMapDatabase { get { return Context.TypeMaps; } } + public DriverOptions Options => Context.Options; + public TypeMapDatabase TypeMapDatabase => Context.TypeMaps; public CSharpTypePrinter(BindingContext context) { @@ -88,20 +74,14 @@ namespace CppSharp.Generators.CSharp contexts.Push(contextKind); } - public CSharpTypePrinterContextKind PopContext() - { - return contexts.Pop(); - } + public CSharpTypePrinterContextKind PopContext() => contexts.Pop(); public void PushMarshalKind(CSharpMarshalKind marshalKind) { marshalKinds.Push(marshalKind); } - public CSharpMarshalKind PopMarshalKind() - { - return marshalKinds.Pop(); - } + public CSharpMarshalKind PopMarshalKind() => marshalKinds.Pop(); public CSharpTypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals) { @@ -726,10 +706,10 @@ namespace CppSharp.Generators.CSharp names.Reverse(); var isInCurrentOutputNamespace = names[0] == Generator.CurrentOutputNamespace; - if (isInCurrentOutputNamespace) + if (isInCurrentOutputNamespace || !FullyQualify) names.RemoveAt(0); - return (isInCurrentOutputNamespace || !AppendGlobal ? + return (isInCurrentOutputNamespace || !FullyQualify ? string.Empty : "global::") + string.Join(".", names); } diff --git a/src/Generator/Passes/DelegatesPass.cs b/src/Generator/Passes/DelegatesPass.cs index 86f3c1a8..c30d2755 100644 --- a/src/Generator/Passes/DelegatesPass.cs +++ b/src/Generator/Passes/DelegatesPass.cs @@ -20,9 +20,9 @@ namespace CppSharp.Passes Signature = signature; } - public string Namespace { get; private set; } + public string Namespace { get; } - public string Signature { get; private set; } + public string Signature { get; } } public DelegatesPass() @@ -37,8 +37,8 @@ namespace CppSharp.Passes public override bool VisitASTContext(ASTContext context) { - foreach (var library in Options.Modules.SelectMany(m => m.Libraries)) - libsDelegates[library] = new Dictionary(); + foreach (var module in Options.Modules) + libsDelegates[module] = new Dictionary(); var unit = context.TranslationUnits.GetGenerated().LastOrDefault(); @@ -48,7 +48,7 @@ namespace CppSharp.Passes var result = base.VisitASTContext(context); foreach (var module in Options.Modules.Where(m => namespacesDelegates.ContainsKey(m))) - module.Units.Last(u => u.HasDeclarations).Declarations.Add(namespacesDelegates[module]); + namespacesDelegates[module].Namespace.Declarations.Add(namespacesDelegates[module]); return result; } @@ -100,10 +100,20 @@ namespace CppSharp.Passes } else { + Namespace parent = null; + if (string.IsNullOrEmpty(module.OutputNamespace)) + { + var group = module.Units.SelectMany(u => u.Declarations).OfType( + ).GroupBy(d => d.Name).Where(g => g.Any(d => d.HasDeclarations)).ToList(); + if (group.Count == 1) + parent = group.Last().Last(); + } + if (parent == null) + parent = module.Units.Last(); namespaceDelegates = new Namespace { Name = DelegatesNamespace, - Namespace = module.Units.Last() + Namespace = parent }; namespacesDelegates.Add(module, namespaceDelegates); } @@ -126,10 +136,9 @@ namespace CppSharp.Passes Access = AccessSpecifier.Private }; - Generator.CurrentOutputNamespace = module.OutputNamespace; var delegateString = @delegate.Visit(TypePrinter).Type; var existingDelegate = GetExistingDelegate( - method.TranslationUnit.Module.Libraries, delegateString); + method.TranslationUnit.Module, delegateString); if (existingDelegate != null) { @@ -140,24 +149,22 @@ namespace CppSharp.Passes existingDelegate = new DelegateDefinition(module.OutputNamespace, delegateString); Context.Delegates.Add(method, existingDelegate); - foreach (var library in module.Libraries) - libsDelegates[library].Add(delegateString, existingDelegate); + libsDelegates[module].Add(delegateString, existingDelegate); namespaceDelegates.Declarations.Add(@delegate); return true; } - private DelegateDefinition GetExistingDelegate(IList libraries, string delegateString) + private DelegateDefinition GetExistingDelegate(Module module, string delegateString) { - if (libraries.Count == 0) + if (module.Libraries.Count == 0) return Context.Delegates.Values.FirstOrDefault(t => t.Signature == delegateString); DelegateDefinition @delegate = null; - if (libraries.Union( - Context.Symbols.Libraries.Where(l => libraries.Contains(l.FileName)).SelectMany( - l => l.Dependencies)).Any(l => libsDelegates.ContainsKey(l) && - libsDelegates[l].TryGetValue(delegateString, out @delegate))) + if (new[] { module }.Union(module.Dependencies).Any( + m => libsDelegates.ContainsKey(m) && libsDelegates[m].TryGetValue( + delegateString, out @delegate))) return @delegate; return null; @@ -165,8 +172,6 @@ namespace CppSharp.Passes private string GenerateDelegateSignature(IEnumerable @params, QualifiedType returnType) { - TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); - CSharpTypePrinter.AppendGlobal = false; var typesBuilder = new StringBuilder(); if (!returnType.Type.IsPrimitiveType(PrimitiveType.Void)) { @@ -186,8 +191,6 @@ namespace CppSharp.Passes else delegateName = "Func_" + delegateName; - TypePrinter.PopContext(); - CSharpTypePrinter.AppendGlobal = true; return delegateName; } @@ -197,7 +200,7 @@ namespace CppSharp.Passes { if (typePrinter == null) { - typePrinter = new CSharpTypePrinter(Context); + typePrinter = new CSharpTypePrinter(Context) { FullyQualify = false }; typePrinter.PushContext(CSharpTypePrinterContextKind.Native); typePrinter.PushMarshalKind(CSharpMarshalKind.GenericDelegate); } @@ -207,7 +210,7 @@ namespace CppSharp.Passes private Dictionary namespacesDelegates = new Dictionary(); private CSharpTypePrinter typePrinter; - private static readonly Dictionary> libsDelegates = - new Dictionary>(); + private static readonly Dictionary> libsDelegates = + new Dictionary>(); } }