Browse Source

Changed the C# generator to always fully qualify types.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/761/head
Dimitar Dobrev 9 years ago
parent
commit
8cb1af92b7
  1. 4
      src/Generator/Generator.cs
  2. 27
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 31
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 3
      src/Generator/Passes/CheckDuplicatedNamesPass.cs
  5. 1
      src/Generator/Passes/DelegatesPass.cs
  6. 1
      src/Generator/Passes/HandleDefaultParamValuesPass.cs

4
src/Generator/Generator.cs

@ -39,8 +39,6 @@ namespace CppSharp.Generators
/// </summary> /// </summary>
public abstract class Generator : IDisposable public abstract class Generator : IDisposable
{ {
public static string CurrentOutputNamespace = string.Empty;
public BindingContext Context { get; private set; } public BindingContext Context { get; private set; }
protected Generator(BindingContext context) protected Generator(BindingContext context)
@ -92,7 +90,6 @@ namespace CppSharp.Generators
if (templates.Count == 0) if (templates.Count == 0)
return; return;
CurrentOutputNamespace = unit.Module.OutputNamespace;
foreach (var template in templates) foreach (var template in templates)
{ {
template.Process(); template.Process();
@ -114,7 +111,6 @@ namespace CppSharp.Generators
{ {
foreach (var module in Context.Options.Modules) foreach (var module in Context.Options.Modules)
{ {
CurrentOutputNamespace = module.OutputNamespace;
var output = new GeneratorOutput var output = new GeneratorOutput
{ {
TranslationUnit = new TranslationUnit TranslationUnit = new TranslationUnit

27
src/Generator/Generators/CSharp/CSharpSources.cs

@ -489,10 +489,8 @@ namespace CppSharp.Generators.CSharp
// use interfaces if any - derived types with a secondary base this class must be compatible with the map // use interfaces if any - derived types with a secondary base this class must be compatible with the map
var @interface = @class.Namespace.Classes.Find(c => c.OriginalClass == @class); var @interface = @class.Namespace.Classes.Find(c => c.OriginalClass == @class);
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
var dict = string.Format("global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, {0}>", var dict = string.Format("global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, {0}>",
(@interface ?? @class).Visit(TypePrinter)); (@interface ?? @class).Visit(TypePrinter));
TypePrinter.PopPrintScopeKind();
WriteLine("internal static readonly {0} NativeToManagedMap = new {0}();", dict); WriteLine("internal static readonly {0} NativeToManagedMap = new {0}();", dict);
WriteLine("protected void*[] __OriginalVTables;"); WriteLine("protected void*[] __OriginalVTables;");
} }
@ -896,7 +894,6 @@ namespace CppSharp.Generators.CSharp
var arrayType = field.Type as ArrayType; var arrayType = field.Type as ArrayType;
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
if (arrayType != null && @class.IsValueType) if (arrayType != null && @class.IsValueType)
{ {
ctx.ReturnVarName = HandleValueArray(arrayType, field); ctx.ReturnVarName = HandleValueArray(arrayType, field);
@ -910,7 +907,6 @@ namespace CppSharp.Generators.CSharp
(@class.IsValueType ? "." : "->")}{Helpers.SafeIdentifier(name)}"; (@class.IsValueType ? "." : "->")}{Helpers.SafeIdentifier(name)}";
TypePrinter.PopContext(); TypePrinter.PopContext();
} }
TypePrinter.PopPrintScopeKind();
param.Visit(marshal); param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
@ -1105,7 +1101,6 @@ namespace CppSharp.Generators.CSharp
var name = @class.Layout.Fields.First(f => f.FieldPtr == field.OriginalPtr).Name; var name = @class.Layout.Fields.First(f => f.FieldPtr == field.OriginalPtr).Name;
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
var ctx = new CSharpMarshalContext(Context) var ctx = new CSharpMarshalContext(Context)
{ {
Kind = MarshalKind.NativeField, Kind = MarshalKind.NativeField,
@ -1116,7 +1111,6 @@ namespace CppSharp.Generators.CSharp
(@class.IsValueType ? "." : "->")}{Helpers.SafeIdentifier(name)}", (@class.IsValueType ? "." : "->")}{Helpers.SafeIdentifier(name)}",
ReturnType = decl.QualifiedType ReturnType = decl.QualifiedType
}; };
TypePrinter.PopPrintScopeKind();
TypePrinter.PopContext(); TypePrinter.PopContext();
var arrayType = field.Type as ArrayType; var arrayType = field.Type as ArrayType;
@ -1743,7 +1737,7 @@ namespace CppSharp.Generators.CSharp
var vTableMethodDelegateName = GetVTableMethodDelegateName(method); var vTableMethodDelegateName = GetVTableMethodDelegateName(method);
WriteLine("private static {0} {1}Instance;", WriteLine("private static {0} {1}Instance;",
GetDelegateName(method, @class.TranslationUnit.Module.OutputNamespace), Context.Delegates[method].Signature,
vTableMethodDelegateName); vTableMethodDelegateName);
NewLine(); NewLine();
@ -2039,7 +2033,6 @@ namespace CppSharp.Generators.CSharp
var className = @class.IsAbstractImpl ? @class.BaseClass.Name : @class.Name; var className = @class.IsAbstractImpl ? @class.BaseClass.Name : @class.Name;
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
var ctorCall = string.Format("{0}{1}", @class.Name, @class.IsAbstract ? "Internal" : ""); var ctorCall = string.Format("{0}{1}", @class.Name, @class.IsAbstract ? "Internal" : "");
if (!@class.IsAbstractImpl) if (!@class.IsAbstractImpl)
{ {
@ -2105,7 +2098,6 @@ namespace CppSharp.Generators.CSharp
WriteLine($"{Helpers.InstanceField} = *({@class.Visit(TypePrinter)}*) native;"); WriteLine($"{Helpers.InstanceField} = *({@class.Visit(TypePrinter)}*) native;");
TypePrinter.PopContext(); TypePrinter.PopContext();
} }
TypePrinter.PopPrintScopeKind();
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -2113,7 +2105,6 @@ namespace CppSharp.Generators.CSharp
private void GenerateNativeConstructorByValue(Class @class, string ctorCall) private void GenerateNativeConstructorByValue(Class @class, string ctorCall)
{ {
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
var @internal = (@class.IsAbstractImpl ? @class.BaseClass : @class).Visit(TypePrinter); var @internal = (@class.IsAbstractImpl ? @class.BaseClass : @class).Visit(TypePrinter);
TypePrinter.PopContext(); TypePrinter.PopContext();
@ -2176,7 +2167,6 @@ namespace CppSharp.Generators.CSharp
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
TypePrinter.PopPrintScopeKind();
} }
private void GenerateClassConstructorBase(Class @class, Method method) private void GenerateClassConstructorBase(Class @class, Method method)
@ -2535,21 +2525,10 @@ namespace CppSharp.Generators.CSharp
delegateId = Generator.GeneratedIdentifier(@delegate); delegateId = Generator.GeneratedIdentifier(@delegate);
WriteLine("var {0} = ({1}) Marshal.GetDelegateForFunctionPointer(new IntPtr({2}), typeof({1}));", WriteLine("var {0} = ({1}) Marshal.GetDelegateForFunctionPointer(new IntPtr({2}), typeof({1}));",
delegateId, GetDelegateName(method, method.TranslationUnit.Module.OutputNamespace), delegateId, Context.Delegates[method].Signature,
Helpers.SlotIdentifier); Helpers.SlotIdentifier);
} }
private string GetDelegateName(Function function, string outputNamespace)
{
var @delegate = Context.Delegates[function];
if (string.IsNullOrWhiteSpace(@delegate.Namespace) ||
outputNamespace == @delegate.Namespace)
{
return @delegate.Signature;
}
return string.Format("global::{0}.{1}", @delegate.Namespace, @delegate.Signature);
}
private void GenerateOperator(Method method) private void GenerateOperator(Method method)
{ {
if (method.SynthKind == FunctionSynthKind.ComplementOperator) if (method.SynthKind == FunctionSynthKind.ComplementOperator)
@ -2615,10 +2594,8 @@ namespace CppSharp.Generators.CSharp
else else
{ {
TypePrinter.PushContext(TypePrinterContextKind.Native); TypePrinter.PushContext(TypePrinterContextKind.Native);
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
var classInternal = @class.Visit(TypePrinter); var classInternal = @class.Visit(TypePrinter);
TypePrinter.PopContext(); TypePrinter.PopContext();
TypePrinter.PopPrintScopeKind();
WriteLine($@"*(({classInternal}*) {Helpers.InstanceIdentifier}) = *(({ WriteLine($@"*(({classInternal}*) {Helpers.InstanceIdentifier}) = *(({
classInternal}*) {method.Parameters[0].Name}.{Helpers.InstanceIdentifier});"); classInternal}*) {method.Parameters[0].Name}.{Helpers.InstanceIdentifier});");
} }

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

@ -23,14 +23,11 @@ namespace CppSharp.Generators.CSharp
private readonly Stack<TypePrinterContextKind> contexts; private readonly Stack<TypePrinterContextKind> contexts;
private readonly Stack<MarshalKind> marshalKinds; private readonly Stack<MarshalKind> marshalKinds;
private readonly Stack<TypePrintScopeKind> printScopeKinds;
public TypePrinterContextKind ContextKind => contexts.Peek(); public TypePrinterContextKind ContextKind => contexts.Peek();
public MarshalKind MarshalKind => marshalKinds.Peek(); public MarshalKind MarshalKind => marshalKinds.Peek();
public TypePrintScopeKind PrintScopeKind => printScopeKinds.Peek();
public CSharpTypePrinterContext TypePrinterContext; public CSharpTypePrinterContext TypePrinterContext;
public BindingContext Context { get; set; } public BindingContext Context { get; set; }
@ -43,10 +40,8 @@ namespace CppSharp.Generators.CSharp
Context = context; Context = context;
contexts = new Stack<TypePrinterContextKind>(); contexts = new Stack<TypePrinterContextKind>();
marshalKinds = new Stack<MarshalKind>(); marshalKinds = new Stack<MarshalKind>();
printScopeKinds = new Stack<TypePrintScopeKind>();
PushContext(TypePrinterContextKind.Managed); PushContext(TypePrinterContextKind.Managed);
PushMarshalKind(MarshalKind.Unknown); PushMarshalKind(MarshalKind.Unknown);
PushPrintScopeKind(TypePrintScopeKind.GlobalQualified);
TypePrinterContext = new CSharpTypePrinterContext(); TypePrinterContext = new CSharpTypePrinterContext();
} }
@ -65,13 +60,6 @@ namespace CppSharp.Generators.CSharp
public MarshalKind PopMarshalKind() => marshalKinds.Pop(); public MarshalKind PopMarshalKind() => marshalKinds.Pop();
public void PushPrintScopeKind(TypePrintScopeKind printScopeKind)
{
printScopeKinds.Push(printScopeKind);
}
public TypePrintScopeKind PopPrintScopeKind() => printScopeKinds.Pop();
public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals) public override TypePrinterResult VisitTagType(TagType tag, TypeQualifiers quals)
{ {
if (tag.Declaration == null) if (tag.Declaration == null)
@ -589,12 +577,8 @@ namespace CppSharp.Generators.CSharp
public override TypePrinterResult VisitClassDecl(Class @class) public override TypePrinterResult VisitClassDecl(Class @class)
{ {
if (ContextKind == TypePrinterContextKind.Native) if (ContextKind == TypePrinterContextKind.Native)
{
if (PrintScopeKind == TypePrintScopeKind.Local)
return Helpers.InternalStruct;
return $"{GetName(@class.OriginalClass ?? @class)}.{Helpers.InternalStruct}"; return $"{GetName(@class.OriginalClass ?? @class)}.{Helpers.InternalStruct}";
}
return GetName(@class); return GetName(@class);
} }
@ -658,9 +642,6 @@ namespace CppSharp.Generators.CSharp
public string GetName(Declaration decl) public string GetName(Declaration decl)
{ {
if (PrintScopeKind == TypePrintScopeKind.Local)
return decl.Name;
var names = new Stack<string>(); var names = new Stack<string>();
Declaration ctx; Declaration ctx;
@ -700,15 +681,7 @@ namespace CppSharp.Generators.CSharp
!string.IsNullOrWhiteSpace(ctx.TranslationUnit.Module.OutputNamespace)) !string.IsNullOrWhiteSpace(ctx.TranslationUnit.Module.OutputNamespace))
names.Push(ctx.TranslationUnit.Module.OutputNamespace); names.Push(ctx.TranslationUnit.Module.OutputNamespace);
names.Reverse(); return $"global::{string.Join(".", names)}";
var isInCurrentOutputNamespace = names.Peek() == Generator.CurrentOutputNamespace;
if (isInCurrentOutputNamespace ||
PrintScopeKind != TypePrintScopeKind.GlobalQualified)
names.Pop();
return (isInCurrentOutputNamespace ||
PrintScopeKind != TypePrintScopeKind.GlobalQualified ?
string.Empty : "global::") + string.Join(".", names);
} }
public override TypePrinterResult VisitVariableDecl(Variable variable) public override TypePrinterResult VisitVariableDecl(Variable variable)

3
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -41,9 +41,6 @@ namespace CppSharp.Passes
private bool UpdateName(Function function) private bool UpdateName(Function function)
{ {
if (function.TranslationUnit.Module != null)
Generator.CurrentOutputNamespace = function.TranslationUnit.Module.OutputNamespace;
var @params = function.Parameters.Where(p => p.Kind != ParameterKind.IndirectReturnType) var @params = function.Parameters.Where(p => p.Kind != ParameterKind.IndirectReturnType)
.Select(p => p.QualifiedType.ToString()); .Select(p => p.QualifiedType.ToString());
// Include the conversion type in case of conversion operators // Include the conversion type in case of conversion operators

1
src/Generator/Passes/DelegatesPass.cs

@ -171,7 +171,6 @@ namespace CppSharp.Passes
if (typePrinter == null) if (typePrinter == null)
{ {
typePrinter = new CSharpTypePrinter(Context); typePrinter = new CSharpTypePrinter(Context);
typePrinter.PushPrintScopeKind(TypePrintScopeKind.Qualified);
typePrinter.PushContext(TypePrinterContextKind.Native); typePrinter.PushContext(TypePrinterContextKind.Native);
typePrinter.PushMarshalKind(MarshalKind.GenericDelegate); typePrinter.PushMarshalKind(MarshalKind.GenericDelegate);
} }

1
src/Generator/Passes/HandleDefaultParamValuesPass.cs

@ -40,7 +40,6 @@ namespace CppSharp.Passes
if (!base.VisitFunctionDecl(function) || function.Ignore) if (!base.VisitFunctionDecl(function) || function.Ignore)
return false; return false;
Generator.CurrentOutputNamespace = function.TranslationUnit.Module.OutputNamespace;
var overloadIndices = new List<int>(function.Parameters.Count); var overloadIndices = new List<int>(function.Parameters.Count);
foreach (var parameter in function.Parameters.Where(p => p.DefaultArgument != null)) foreach (var parameter in function.Parameters.Where(p => p.DefaultArgument != null))
{ {

Loading…
Cancel
Save