Browse Source

Simplified the printing of templates.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/890/head
Dimitar Dobrev 8 years ago
parent
commit
225cd5c49b
  1. 24
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 5
      src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs
  3. 14
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs

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

@ -394,7 +394,7 @@ namespace CppSharp.Generators.CSharp
c => c.IsInterface && c.OriginalClass == @class); c => c.IsInterface && c.OriginalClass == @class);
var printedClass = (@interface ?? @class).Visit(TypePrinter); var printedClass = (@interface ?? @class).Visit(TypePrinter);
var dict = $@"global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, { var dict = $@"global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, {
printedClass}{printedClass.NameSuffix}>"; printedClass}>";
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;");
} }
@ -1247,11 +1247,11 @@ namespace CppSharp.Generators.CSharp
if (prop.IsVirtual && !isOverride && !prop.IsPure) if (prop.IsVirtual && !isOverride && !prop.IsPure)
Write("virtual "); Write("virtual ");
WriteLine($"{printedType}{printedType.NameSuffix} {GetPropertyName(prop)}"); WriteLine($"{printedType} {GetPropertyName(prop)}");
} }
else else
{ {
WriteLine($@"{printedType}{printedType.NameSuffix} { WriteLine($@"{printedType} {
prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}"); prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}");
} }
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -1682,8 +1682,7 @@ namespace CppSharp.Generators.CSharp
NewLine(); NewLine();
var printedClass = @class.Visit(TypePrinter); var printedClass = @class.Visit(TypePrinter);
WriteLine($@"var {Helpers.TargetIdentifier} = ({printedClass}{ WriteLine($"var {Helpers.TargetIdentifier} = ({printedClass}) NativeToManagedMap[instance];");
printedClass.NameSuffix}) NativeToManagedMap[instance];");
WriteLine("if ({0}.{1})", Helpers.TargetIdentifier, Helpers.OwnsNativeInstanceIdentifier); WriteLine("if ({0}.{1})", Helpers.TargetIdentifier, Helpers.OwnsNativeInstanceIdentifier);
WriteLineIndent("{0}.SetupVTables();", Helpers.TargetIdentifier); WriteLineIndent("{0}.SetupVTables();", Helpers.TargetIdentifier);
GenerateVTableManagedCall(method); GenerateVTableManagedCall(method);
@ -1907,7 +1906,7 @@ namespace CppSharp.Generators.CSharp
// The local var must be of the exact type in the object map because of TryRemove // The local var must be of the exact type in the object map because of TryRemove
var printedClass = (@interface ?? ( var printedClass = (@interface ?? (
@base.IsAbstractImpl ? @base.BaseClass : @base)).Visit(TypePrinter); @base.IsAbstractImpl ? @base.BaseClass : @base)).Visit(TypePrinter);
WriteLine($"{printedClass}{printedClass.NameSuffix} {Helpers.DummyIdentifier};"); WriteLine($"{printedClass} {Helpers.DummyIdentifier};");
WriteLine("NativeToManagedMap.TryRemove({0}, out {1});", WriteLine("NativeToManagedMap.TryRemove({0}, out {1});",
Helpers.InstanceIdentifier, Helpers.DummyIdentifier); Helpers.InstanceIdentifier, Helpers.DummyIdentifier);
var classInternal = TypePrinter.PrintNative(@class); var classInternal = TypePrinter.PrintNative(@class);
@ -1987,12 +1986,12 @@ namespace CppSharp.Generators.CSharp
{ {
PushBlock(BlockKind.Method); PushBlock(BlockKind.Method);
var printedClass = @class.Visit(TypePrinter); var printedClass = @class.Visit(TypePrinter);
WriteLine("internal static {0}{1}{2} {3}(global::System.IntPtr native, bool skipVTables = false)", WriteLine("internal static {0}{1} {2}(global::System.IntPtr native, bool skipVTables = false)",
@class.NeedsBase && !@class.BaseClass.IsInterface ? "new " : string.Empty, @class.NeedsBase && !@class.BaseClass.IsInterface ? "new " : string.Empty,
printedClass, printedClass.NameSuffix, Helpers.CreateInstanceIdentifier); printedClass, Helpers.CreateInstanceIdentifier);
WriteStartBraceIndent(); WriteStartBraceIndent();
var suffix = @class.IsAbstract ? "Internal" : string.Empty; var suffix = @class.IsAbstract ? "Internal" : string.Empty;
var ctorCall = $"{printedClass}{printedClass.NameSuffix}{suffix}"; var ctorCall = $"{printedClass}{suffix}";
WriteLine("return new {0}(native.ToPointer(), skipVTables);", ctorCall); WriteLine("return new {0}(native.ToPointer(), skipVTables);", ctorCall);
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -2189,7 +2188,7 @@ namespace CppSharp.Generators.CSharp
method.OperatorKind == CXXOperatorKind.ExplicitConversion) method.OperatorKind == CXXOperatorKind.ExplicitConversion)
{ {
var printedType = method.OriginalReturnType.Visit(TypePrinter); var printedType = method.OriginalReturnType.Visit(TypePrinter);
Write($"{functionName} {printedType}{printedType.NameSuffix}("); Write($"{functionName} {printedType}(");
} }
else else
Write("{0} {1}(", method.OriginalReturnType, functionName); Write("{0} {1}(", method.OriginalReturnType, functionName);
@ -2484,11 +2483,10 @@ namespace CppSharp.Generators.CSharp
if (@interface != null) if (@interface != null)
{ {
var printedInterface = @interface.Visit(TypePrinter); var printedInterface = @interface.Visit(TypePrinter);
WriteLine($@"return new {printedType}{printedType.NameSuffix}(({ WriteLine($@"return new {printedType}(({printedInterface}) {paramName});");
printedInterface}{printedInterface.NameSuffix}) {paramName});");
} }
else else
WriteLine($"return new {printedType}{printedType.NameSuffix}({paramName});"); WriteLine($"return new {printedType}({paramName});");
} }
else else
{ {

5
src/Generator/Generators/CSharp/CSharpSourcesExtensions.cs

@ -22,12 +22,11 @@ namespace CppSharp.Generators.CSharp
this CSharpSources gen, Class @class) this CSharpSources gen, Class @class)
{ {
var printedClass = @class.Visit(gen.TypePrinter); var printedClass = @class.Visit(gen.TypePrinter);
var returnType = $"{printedClass}{printedClass.NameSuffix}";
if (@class.IsDependent) if (@class.IsDependent)
foreach (var specialization in @class.GetSpecializationsToGenerate().Where(s => !s.Ignore)) foreach (var specialization in @class.GetSpecializationsToGenerate().Where(s => !s.Ignore))
gen.GenerateNativeConstructorByValue(specialization, returnType); gen.GenerateNativeConstructorByValue(specialization, printedClass.Type);
else else
gen.GenerateNativeConstructorByValue(@class, returnType); gen.GenerateNativeConstructorByValue(@class, printedClass.Type);
} }
public static void GenerateField(this CSharpSources gen, Class @class, public static void GenerateField(this CSharpSources gen, Class @class,

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

@ -343,7 +343,7 @@ namespace CppSharp.Generators.CSharp
{ {
if (ContextKind == TypePrinterContextKind.Managed && if (ContextKind == TypePrinterContextKind.Managed &&
decl == template.Template.TemplatedDecl) decl == template.Template.TemplatedDecl)
return $@"{decl.Visit(this)}<{string.Join(", ", return $@"{VisitDeclaration(decl)}<{string.Join(", ",
template.Arguments.Select(VisitTemplateArgument))}>"; template.Arguments.Select(VisitTemplateArgument))}>";
return decl.Visit(this); return decl.Visit(this);
} }
@ -549,12 +549,11 @@ namespace CppSharp.Generators.CSharp
if (ContextKind == TypePrinterContextKind.Native) if (ContextKind == TypePrinterContextKind.Native)
return $"{VisitDeclaration(@class.OriginalClass ?? @class)}.{Helpers.InternalStruct}"; return $"{VisitDeclaration(@class.OriginalClass ?? @class)}.{Helpers.InternalStruct}";
var typePrinterResult = new TypePrinterResult(); var printed = VisitDeclaration(@class).Type;
typePrinterResult.Type = VisitDeclaration(@class).Type; if (!@class.IsDependent)
if (@class.IsDependent) return printed;
typePrinterResult.NameSuffix = $@"<{ return $@"{printed}<{string.Join(", ",
string.Join(", ", @class.TemplateParameters.Select(p => p.Name))}>"; @class.TemplateParameters.Select(p => p.Name))}>";
return typePrinterResult;
} }
public override TypePrinterResult VisitClassTemplateSpecializationDecl( public override TypePrinterResult VisitClassTemplateSpecializationDecl(
@ -652,7 +651,6 @@ namespace CppSharp.Generators.CSharp
typeBuilder.Append("[MarshalAs(UnmanagedType.I1)] "); typeBuilder.Append("[MarshalAs(UnmanagedType.I1)] ");
var printedType = param.Type.Visit(this, param.QualifiedType.Qualifiers); var printedType = param.Type.Visit(this, param.QualifiedType.Qualifiers);
typeBuilder.Append(printedType); typeBuilder.Append(printedType);
typeBuilder.Append(printedType.NameSuffix);
var type = typeBuilder.ToString(); var type = typeBuilder.ToString();
if (Context.Delegates.ContainsKey(param)) if (Context.Delegates.ContainsKey(param))

Loading…
Cancel
Save