Permalink
Browse files

Simplified the printing of templates.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information...
ddobrev committed Aug 6, 2017
1 parent 2af1af6 commit 225cd5c49b23eae1823f2600647a2b09dc3296c6
@@ -394,7 +394,7 @@ public override bool VisitClassDecl(Class @class)
c => c.IsInterface && c.OriginalClass == @class);
var printedClass = (@interface ?? @class).Visit(TypePrinter);
var dict = $@"global::System.Collections.Concurrent.ConcurrentDictionary<IntPtr, {
printedClass}{printedClass.NameSuffix}>";
printedClass}>";
WriteLine("internal static readonly {0} NativeToManagedMap = new {0}();", dict);
WriteLine("protected void*[] __OriginalVTables;");
}
@@ -1247,11 +1247,11 @@ private void GenerateProperties(Class @class)
if (prop.IsVirtual && !isOverride && !prop.IsPure)
Write("virtual ");
WriteLine($"{printedType}{printedType.NameSuffix} {GetPropertyName(prop)}");
WriteLine($"{printedType} {GetPropertyName(prop)}");
}
else
{
WriteLine($@"{printedType}{printedType.NameSuffix} {
WriteLine($@"{printedType} {
prop.ExplicitInterfaceImpl.Name}.{GetPropertyName(prop)}");
}
WriteStartBraceIndent();
@@ -1682,8 +1682,7 @@ private void GenerateVTableMethodDelegates(Class @class, Method method)
NewLine();
var printedClass = @class.Visit(TypePrinter);
WriteLine($@"var {Helpers.TargetIdentifier} = ({printedClass}{
printedClass.NameSuffix}) NativeToManagedMap[instance];");
WriteLine($"var {Helpers.TargetIdentifier} = ({printedClass}) NativeToManagedMap[instance];");
WriteLine("if ({0}.{1})", Helpers.TargetIdentifier, Helpers.OwnsNativeInstanceIdentifier);
WriteLineIndent("{0}.SetupVTables();", Helpers.TargetIdentifier);
GenerateVTableManagedCall(method);
@@ -1907,7 +1906,7 @@ private void GenerateDisposeMethods(Class @class)
// The local var must be of the exact type in the object map because of TryRemove
var printedClass = (@interface ?? (
@base.IsAbstractImpl ? @base.BaseClass : @base)).Visit(TypePrinter);
WriteLine($"{printedClass}{printedClass.NameSuffix} {Helpers.DummyIdentifier};");
WriteLine($"{printedClass} {Helpers.DummyIdentifier};");
WriteLine("NativeToManagedMap.TryRemove({0}, out {1});",
Helpers.InstanceIdentifier, Helpers.DummyIdentifier);
var classInternal = TypePrinter.PrintNative(@class);
@@ -1987,12 +1986,12 @@ private void GenerateNativeConstructor(Class @class)
{
PushBlock(BlockKind.Method);
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,
printedClass, printedClass.NameSuffix, Helpers.CreateInstanceIdentifier);
printedClass, Helpers.CreateInstanceIdentifier);
WriteStartBraceIndent();
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);
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
@@ -2189,7 +2188,7 @@ public override void GenerateMethodSpecifier(Method method, Class @class)
method.OperatorKind == CXXOperatorKind.ExplicitConversion)
{
var printedType = method.OriginalReturnType.Visit(TypePrinter);
Write($"{functionName} {printedType}{printedType.NameSuffix}(");
Write($"{functionName} {printedType}(");
}
else
Write("{0} {1}(", method.OriginalReturnType, functionName);
@@ -2484,11 +2483,10 @@ private void GenerateOperator(Method method)
if (@interface != null)
{
var printedInterface = @interface.Visit(TypePrinter);
WriteLine($@"return new {printedType}{printedType.NameSuffix}(({
printedInterface}{printedInterface.NameSuffix}) {paramName});");
WriteLine($@"return new {printedType}(({printedInterface}) {paramName});");
}
else
WriteLine($"return new {printedType}{printedType.NameSuffix}({paramName});");
WriteLine($"return new {printedType}({paramName});");
}
else
{
@@ -22,12 +22,11 @@ public static class CSharpSourcesExtensions
this CSharpSources gen, Class @class)
{
var printedClass = @class.Visit(gen.TypePrinter);
var returnType = $"{printedClass}{printedClass.NameSuffix}";
if (@class.IsDependent)
foreach (var specialization in @class.GetSpecializationsToGenerate().Where(s => !s.Ignore))
gen.GenerateNativeConstructorByValue(specialization, returnType);
gen.GenerateNativeConstructorByValue(specialization, printedClass.Type);
else
gen.GenerateNativeConstructorByValue(@class, returnType);
gen.GenerateNativeConstructorByValue(@class, printedClass.Type);
}
public static void GenerateField(this CSharpSources gen, Class @class,
@@ -343,7 +343,7 @@ public static bool IsConstCharString(QualifiedType qualType)
{
if (ContextKind == TypePrinterContextKind.Managed &&
decl == template.Template.TemplatedDecl)
return $@"{decl.Visit(this)}<{string.Join(", ",
return $@"{VisitDeclaration(decl)}<{string.Join(", ",
template.Arguments.Select(VisitTemplateArgument))}>";
return decl.Visit(this);
}
@@ -549,12 +549,11 @@ public override TypePrinterResult VisitClassDecl(Class @class)
if (ContextKind == TypePrinterContextKind.Native)
return $"{VisitDeclaration(@class.OriginalClass ?? @class)}.{Helpers.InternalStruct}";
var typePrinterResult = new TypePrinterResult();
typePrinterResult.Type = VisitDeclaration(@class).Type;
if (@class.IsDependent)
typePrinterResult.NameSuffix = $@"<{
string.Join(", ", @class.TemplateParameters.Select(p => p.Name))}>";
return typePrinterResult;
var printed = VisitDeclaration(@class).Type;
if (!@class.IsDependent)
return printed;
return $@"{printed}<{string.Join(", ",
@class.TemplateParameters.Select(p => p.Name))}>";
}
public override TypePrinterResult VisitClassTemplateSpecializationDecl(
@@ -652,7 +651,6 @@ public override TypePrinterResult VisitParameter(Parameter param, bool hasName)
typeBuilder.Append("[MarshalAs(UnmanagedType.I1)] ");
var printedType = param.Type.Visit(this, param.QualifiedType.Qualifiers);
typeBuilder.Append(printedType);
typeBuilder.Append(printedType.NameSuffix);
var type = typeBuilder.ToString();
if (Context.Delegates.ContainsKey(param))

0 comments on commit 225cd5c

Please sign in to comment.