Browse Source

Fixed the fully qualified names to include "global::" in the C# end.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/658/head
Dimitar Dobrev 9 years ago
parent
commit
bbb8aeb4af
  1. 1
      src/Generator/Driver.cs
  2. 4
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 23
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 9
      src/Generator/Passes/CheckDuplicatedNamesPass.cs
  5. 3
      src/Generator/Passes/DelegatesPass.cs
  6. 34
      src/Generator/Passes/RenameRootNamespaces.cs
  7. 9
      tests/NamespacesDerived/NamespacesDerived.h

1
src/Generator/Driver.cs

@ -327,7 +327,6 @@ namespace CppSharp @@ -327,7 +327,6 @@ namespace CppSharp
TranslationUnitPasses.AddPass(new CheckIgnoredDeclsPass());
TranslationUnitPasses.AddPass(new CheckFlagEnumsPass());
TranslationUnitPasses.AddPass(new CheckDuplicatedNamesPass());
TranslationUnitPasses.AddPass(new RenameRootNamespacesPass());
if (Options.IsCSharpGenerator)
{
TranslationUnitPasses.AddPass(new GenerateAbstractImplementationsPass());

4
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -90,6 +90,7 @@ namespace CppSharp.Generators.CSharp @@ -90,6 +90,7 @@ namespace CppSharp.Generators.CSharp
typePrinter.PushContext(CSharpTypePrinterContextKind.Managed);
typePrinter.PushMarshalKind(CSharpMarshalKind.Unknown);
}
CSharpTypePrinter.AppendGlobal = false;
var suffix = new StringBuilder();
foreach (var argType in from argType in specialization.Arguments
where argType.Type.Type != null
@ -123,6 +124,7 @@ namespace CppSharp.Generators.CSharp @@ -123,6 +124,7 @@ namespace CppSharp.Generators.CSharp
typePrinter.PopContext();
typePrinter.PopMarshalKind();
}
CSharpTypePrinter.AppendGlobal = true;
FormatTypesStringForIdentifier(suffix);
return suffix.ToString();
}
@ -2535,7 +2537,7 @@ namespace CppSharp.Generators.CSharp @@ -2535,7 +2537,7 @@ namespace CppSharp.Generators.CSharp
{
return @delegate.Signature;
}
return string.Format("{0}.{1}", @delegate.Namespace, @delegate.Signature);
return string.Format("global::{0}.{1}", @delegate.Namespace, @delegate.Signature);
}
private void GenerateOperator(Method method)

23
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -5,6 +5,7 @@ using CppSharp.AST.Extensions; @@ -5,6 +5,7 @@ using CppSharp.AST.Extensions;
using CppSharp.Types;
using Type = CppSharp.AST.Type;
using ParserTargetInfo = CppSharp.Parser.ParserTargetInfo;
using System.Linq;
namespace CppSharp.Generators.CSharp
{
@ -41,7 +42,14 @@ namespace CppSharp.Generators.CSharp @@ -41,7 +42,14 @@ namespace CppSharp.Generators.CSharp
public class CSharpTypePrinter : ITypePrinter<CSharpTypePrinterResult>,
IDeclVisitor<CSharpTypePrinterResult>
{
public static string IntPtrType = "global::System.IntPtr";
public const string IntPtrType = "global::System.IntPtr";
public static bool AppendGlobal { get; set; }
static CSharpTypePrinter()
{
AppendGlobal = true;
}
private readonly Driver driver;
@ -623,7 +631,7 @@ namespace CppSharp.Generators.CSharp @@ -623,7 +631,7 @@ namespace CppSharp.Generators.CSharp
if (specialization.OriginalNamespace is Class)
{
names.Add(string.Format("{0}_{1}", decl.OriginalNamespace.Name, decl.Name));
ctx = ctx.Namespace;
ctx = ctx.Namespace ?? ctx;
}
else
{
@ -635,18 +643,23 @@ namespace CppSharp.Generators.CSharp @@ -635,18 +643,23 @@ namespace CppSharp.Generators.CSharp
names.Add(decl.Name);
ctx = decl.Namespace;
}
while (ctx != null)
while (!(ctx is TranslationUnit))
{
if (!string.IsNullOrWhiteSpace(ctx.Name))
names.Add(ctx.Name);
ctx = ctx.Namespace;
}
if (!string.IsNullOrWhiteSpace(ctx.TranslationUnit.Module.OutputNamespace))
names.Add(ctx.TranslationUnit.Module.OutputNamespace);
names.Reverse();
if (names[0] == Generator.CurrentOutputNamespace)
var isInCurrentOutputNamespace = names[0] == Generator.CurrentOutputNamespace;
if (isInCurrentOutputNamespace)
names.RemoveAt(0);
return string.Join(".", names);
return (isInCurrentOutputNamespace || !AppendGlobal ?
string.Empty : "global::") + string.Join(".", names);
}
public CSharpTypePrinterResult VisitVariableDecl(Variable variable)

9
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -3,6 +3,7 @@ using System.Collections.Generic; @@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using CppSharp.AST;
using CppSharp.Generators;
namespace CppSharp.Passes
{
@ -43,6 +44,7 @@ namespace CppSharp.Passes @@ -43,6 +44,7 @@ namespace CppSharp.Passes
private bool UpdateName(Function function)
{
Generator.CurrentOutputNamespace = function.TranslationUnit.Module.OutputNamespace;
var @params = function.Parameters.Where(p => p.Kind != ParameterKind.IndirectReturnType)
.Select(p => p.QualifiedType.ToString());
// Include the conversion type in case of conversion operators
@ -186,6 +188,13 @@ namespace CppSharp.Passes @@ -186,6 +188,13 @@ namespace CppSharp.Passes
return false;
}
public override bool VisitDeclaration(Declaration decl)
{
if (decl.Namespace != null && decl.TranslationUnit.IsSystemHeader)
return false;
return base.VisitDeclaration(decl);
}
private static IEnumerable<Field> GetAllFields(Class @class, List<Field> fields = null)
{
fields = fields ?? new List<Field>();

3
src/Generator/Passes/DelegatesPass.cs

@ -166,7 +166,7 @@ namespace CppSharp.Passes @@ -166,7 +166,7 @@ namespace CppSharp.Passes
private string GenerateDelegateSignature(IEnumerable<Parameter> @params, QualifiedType returnType)
{
TypePrinter.PushContext(CSharpTypePrinterContextKind.Native);
CSharpTypePrinter.AppendGlobal = false;
var typesBuilder = new StringBuilder();
if (!returnType.Type.IsPrimitiveType(PrimitiveType.Void))
{
@ -187,6 +187,7 @@ namespace CppSharp.Passes @@ -187,6 +187,7 @@ namespace CppSharp.Passes
delegateName = "Func_" + delegateName;
TypePrinter.PopContext();
CSharpTypePrinter.AppendGlobal = true;
return delegateName;
}

34
src/Generator/Passes/RenameRootNamespaces.cs

@ -1,34 +0,0 @@ @@ -1,34 +0,0 @@
using System.Collections.Generic;
using CppSharp.AST;
namespace CppSharp.Passes
{
// This pass visits all the translation units and checks if they originate from library being processed or
// from some other library that is being depended upon. It will rename the root namespaces of all the "foreign"
// libraries so that there wouldn't be clashes and so that the code generation phase would be able to generate
// names with fully qualified namespace prefixes.
public class RenameRootNamespacesPass : TranslationUnitPass
{
public override bool VisitTranslationUnit(TranslationUnit unit)
{
if (!base.VisitTranslationUnit(unit) || !unit.IsValid || unit.IsSystemHeader)
return false;
var filePath = unit.TranslationUnit.FilePath;
if (rootNamespaceRenames.ContainsKey(filePath))
{
var rootNamespace = rootNamespaceRenames[filePath];
unit.Name = rootNamespace;
}
else if (unit.GenerationKind == GenerationKind.Generate)
{
if (Driver.Options.IsCSharpGenerator)
unit.Name = unit.Module.OutputNamespace;
rootNamespaceRenames.Add(filePath, unit.Module.OutputNamespace);
}
return true;
}
private static readonly Dictionary<string, string> rootNamespaceRenames = new Dictionary<string, string>();
}
}

9
tests/NamespacesDerived/NamespacesDerived.h

@ -70,3 +70,12 @@ public: @@ -70,3 +70,12 @@ public:
HasVirtualInDependency* managedObject;
int callManagedOverride();
};
namespace NamespacesBase
{
class DLL_API ClassInNamespaceNamedAfterDependency
{
private:
Base base;
};
}

Loading…
Cancel
Save