Browse Source

Changed the C# generator to always fully qualify types.

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

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

@ -489,10 +489,8 @@ namespace CppSharp.Generators.CSharp @@ -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
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}>",
(@interface ?? @class).Visit(TypePrinter));
TypePrinter.PopPrintScopeKind();
WriteLine("internal static readonly {0} NativeToManagedMap = new {0}();", dict);
WriteLine("protected void*[] __OriginalVTables;");
}
@ -896,7 +894,6 @@ namespace CppSharp.Generators.CSharp @@ -896,7 +894,6 @@ namespace CppSharp.Generators.CSharp
var arrayType = field.Type as ArrayType;
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
if (arrayType != null && @class.IsValueType)
{
ctx.ReturnVarName = HandleValueArray(arrayType, field);
@ -910,7 +907,6 @@ namespace CppSharp.Generators.CSharp @@ -910,7 +907,6 @@ namespace CppSharp.Generators.CSharp
(@class.IsValueType ? "." : "->")}{Helpers.SafeIdentifier(name)}";
TypePrinter.PopContext();
}
TypePrinter.PopPrintScopeKind();
param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
@ -1105,7 +1101,6 @@ namespace CppSharp.Generators.CSharp @@ -1105,7 +1101,6 @@ namespace CppSharp.Generators.CSharp
var name = @class.Layout.Fields.First(f => f.FieldPtr == field.OriginalPtr).Name;
TypePrinter.PushContext(TypePrinterContextKind.Native);
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
var ctx = new CSharpMarshalContext(Context)
{
Kind = MarshalKind.NativeField,
@ -1116,7 +1111,6 @@ namespace CppSharp.Generators.CSharp @@ -1116,7 +1111,6 @@ namespace CppSharp.Generators.CSharp
(@class.IsValueType ? "." : "->")}{Helpers.SafeIdentifier(name)}",
ReturnType = decl.QualifiedType
};
TypePrinter.PopPrintScopeKind();
TypePrinter.PopContext();
var arrayType = field.Type as ArrayType;
@ -1743,7 +1737,7 @@ namespace CppSharp.Generators.CSharp @@ -1743,7 +1737,7 @@ namespace CppSharp.Generators.CSharp
var vTableMethodDelegateName = GetVTableMethodDelegateName(method);
WriteLine("private static {0} {1}Instance;",
GetDelegateName(method, @class.TranslationUnit.Module.OutputNamespace),
Context.Delegates[method].Signature,
vTableMethodDelegateName);
NewLine();
@ -2039,7 +2033,6 @@ namespace CppSharp.Generators.CSharp @@ -2039,7 +2033,6 @@ namespace CppSharp.Generators.CSharp
var className = @class.IsAbstractImpl ? @class.BaseClass.Name : @class.Name;
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
var ctorCall = string.Format("{0}{1}", @class.Name, @class.IsAbstract ? "Internal" : "");
if (!@class.IsAbstractImpl)
{
@ -2105,7 +2098,6 @@ namespace CppSharp.Generators.CSharp @@ -2105,7 +2098,6 @@ namespace CppSharp.Generators.CSharp
WriteLine($"{Helpers.InstanceField} = *({@class.Visit(TypePrinter)}*) native;");
TypePrinter.PopContext();
}
TypePrinter.PopPrintScopeKind();
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
@ -2113,7 +2105,6 @@ namespace CppSharp.Generators.CSharp @@ -2113,7 +2105,6 @@ namespace CppSharp.Generators.CSharp
private void GenerateNativeConstructorByValue(Class @class, string ctorCall)
{
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
TypePrinter.PushContext(TypePrinterContextKind.Native);
var @internal = (@class.IsAbstractImpl ? @class.BaseClass : @class).Visit(TypePrinter);
TypePrinter.PopContext();
@ -2176,7 +2167,6 @@ namespace CppSharp.Generators.CSharp @@ -2176,7 +2167,6 @@ namespace CppSharp.Generators.CSharp
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
TypePrinter.PopPrintScopeKind();
}
private void GenerateClassConstructorBase(Class @class, Method method)
@ -2535,21 +2525,10 @@ namespace CppSharp.Generators.CSharp @@ -2535,21 +2525,10 @@ namespace CppSharp.Generators.CSharp
delegateId = Generator.GeneratedIdentifier(@delegate);
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);
}
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)
{
if (method.SynthKind == FunctionSynthKind.ComplementOperator)
@ -2615,10 +2594,8 @@ namespace CppSharp.Generators.CSharp @@ -2615,10 +2594,8 @@ namespace CppSharp.Generators.CSharp
else
{
TypePrinter.PushContext(TypePrinterContextKind.Native);
TypePrinter.PushPrintScopeKind(TypePrintScopeKind.Local);
var classInternal = @class.Visit(TypePrinter);
TypePrinter.PopContext();
TypePrinter.PopPrintScopeKind();
WriteLine($@"*(({classInternal}*) {Helpers.InstanceIdentifier}) = *(({
classInternal}*) {method.Parameters[0].Name}.{Helpers.InstanceIdentifier});");
}

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

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

3
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -41,9 +41,6 @@ namespace CppSharp.Passes @@ -41,9 +41,6 @@ namespace CppSharp.Passes
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)
.Select(p => p.QualifiedType.ToString());
// Include the conversion type in case of conversion operators

1
src/Generator/Passes/DelegatesPass.cs

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

1
src/Generator/Passes/HandleDefaultParamValuesPass.cs

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

Loading…
Cancel
Save