Browse Source

Simplify and optimize the printing of pointers in C++

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1284/head
Dimitar Dobrev 5 years ago
parent
commit
9337004bb6
  1. 2
      src/Generator.Tests/AST/TestAST.cs
  2. 10
      src/Generator/Generators/C/CppTypePrinter.cs
  3. 40
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 9
      src/Generator/Generators/TypePrinter.cs
  5. 6
      src/Generator/Passes/SymbolsCodeGenerator.cs

2
src/Generator.Tests/AST/TestAST.cs

@ -477,7 +477,7 @@ namespace CppSharp.Generator.Tests.AST @@ -477,7 +477,7 @@ namespace CppSharp.Generator.Tests.AST
var builtin = new BuiltinType(PrimitiveType.Char);
var pointee = new QualifiedType(builtin, new TypeQualifiers { IsConst = true });
var pointer = new QualifiedType(new PointerType(pointee), new TypeQualifiers { IsConst = true });
var type = pointer.Visit(cppTypePrinter).Type;
string type = pointer.Visit(cppTypePrinter);
Assert.That(type, Is.EqualTo("const char* const"));
}

10
src/Generator/Generators/C/CppTypePrinter.cs

@ -72,12 +72,10 @@ namespace CppSharp.Generators.C @@ -72,12 +72,10 @@ namespace CppSharp.Generators.C
var qual = GetStringQuals(quals, false);
var pointeeType = pointer.Pointee.Visit(this, pointer.QualifiedPointee.Qualifiers);
var mod = PrintTypeModifiers ? ConvertModifierToString(pointer.Modifier) : string.Empty;
if (pointeeType.Type.Contains("{0}"))
{
pointeeType.NameSuffix = pointeeType.NameSuffix + mod + qual;
return pointeeType;
}
return $"{pointeeType}{mod}{(string.IsNullOrEmpty(qual) ? string.Empty : " ")}{qual}";
pointeeType.NameSuffix.Append(mod);
if (!string.IsNullOrEmpty(qual))
pointeeType.NameSuffix.Append(' ').Append(qual);
return pointeeType;
}
public override TypePrinterResult VisitMemberPointerType(MemberPointerType member, TypeQualifiers quals)

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

@ -87,21 +87,15 @@ namespace CppSharp.Generators.CSharp @@ -87,21 +87,15 @@ namespace CppSharp.Generators.CSharp
Enumeration @enum;
if (arrayType.TryGetEnum(out @enum))
{
return new TypePrinterResult
{
Type = $"fixed {@enum.BuiltinType}",
NameSuffix = $"[{array.Size}]"
};
return new TypePrinterResult($"fixed {@enum.BuiltinType}",
$"[{array.Size}]");
}
Class @class;
if (arrayType.TryGetClass(out @class))
{
return new TypePrinterResult
{
Type = "fixed byte",
NameSuffix = $"[{array.Size * @class.Layout.GetSize()}]"
};
return new TypePrinterResult($"fixed byte",
$"[{array.Size * @class.Layout.GetSize()}]");
}
TypePrinterResult arrayElemType = array.QualifiedType.Visit(this);
@ -113,11 +107,8 @@ namespace CppSharp.Generators.CSharp @@ -113,11 +107,8 @@ namespace CppSharp.Generators.CSharp
// Do not write the fixed keyword multiple times for nested array types
var fixedKeyword = arrayType is ArrayType ? string.Empty : "fixed ";
return new TypePrinterResult
{
Type = $"{fixedKeyword}{arrayElemType.Type}",
NameSuffix = $"[{array.Size}]"
};
return new TypePrinterResult(fixedKeyword + arrayElemType.Type,
$"[{array.Size}]");
}
// const char* and const char[] are the same so we can use a string
@ -507,16 +498,16 @@ namespace CppSharp.Generators.CSharp @@ -507,16 +498,16 @@ namespace CppSharp.Generators.CSharp
case PrimitiveType.LongLong:
case PrimitiveType.ULongLong:
return GetIntString(primitive, Context.TargetInfo);
case PrimitiveType.Int128: return new TypePrinterResult { Type = "fixed byte",
NameSuffix = "[16]"}; // The type is always 128 bits wide
case PrimitiveType.UInt128: return new TypePrinterResult { Type = "fixed byte",
NameSuffix = "[16]"}; // The type is always 128 bits wide
case PrimitiveType.Half: return new TypePrinterResult { Type = "fixed byte",
NameSuffix = $"[{Context.TargetInfo.HalfWidth}]"};
case PrimitiveType.Int128: return new TypePrinterResult("fixed byte",
"[16]"); // The type is always 128 bits wide
case PrimitiveType.UInt128: return new TypePrinterResult("fixed byte",
"[16]"); // The type is always 128 bits wide
case PrimitiveType.Half: return new TypePrinterResult("fixed byte",
$"[{Context.TargetInfo.HalfWidth}]");
case PrimitiveType.Float: return "float";
case PrimitiveType.Double: return "double";
case PrimitiveType.LongDouble: return new TypePrinterResult { Type = "fixed byte",
NameSuffix = $"[{Context.TargetInfo.LongDoubleWidth}]"};
case PrimitiveType.LongDouble: return new TypePrinterResult("fixed byte",
$"[{Context.TargetInfo.LongDoubleWidth}]");
case PrimitiveType.IntPtr: return IntPtrType;
case PrimitiveType.UIntPtr: return QualifiedType("System.UIntPtr");
case PrimitiveType.Null: return "void*";
@ -734,8 +725,7 @@ namespace CppSharp.Generators.CSharp @@ -734,8 +725,7 @@ namespace CppSharp.Generators.CSharp
PopMarshalKind();
var returnTypePrinter = new TypePrinterResult();
if (!string.IsNullOrWhiteSpace(fieldTypePrinted.NameSuffix))
returnTypePrinter.NameSuffix = fieldTypePrinted.NameSuffix;
returnTypePrinter.NameSuffix.Append(fieldTypePrinted.NameSuffix);
returnTypePrinter.Type = $"{fieldTypePrinted.Type} {field.Name}";

9
src/Generator/Generators/TypePrinter.cs

@ -1,13 +1,20 @@ @@ -1,13 +1,20 @@
using CppSharp.AST;
using System;
using System.Collections.Generic;
using System.Text;
namespace CppSharp.Generators
{
public class TypePrinterResult
{
public string Type { get; set; }
public string NameSuffix { get; set; } = string.Empty;
public StringBuilder NameSuffix { get; set; } = new StringBuilder();
public TypePrinterResult(string type = "", string nameSuffix = "")
{
Type = type;
NameSuffix.Append(nameSuffix);
}
public static implicit operator TypePrinterResult(string type) =>
new TypePrinterResult { Type = type };

6
src/Generator/Passes/SymbolsCodeGenerator.cs

@ -218,10 +218,8 @@ namespace CppSharp.Passes @@ -218,10 +218,8 @@ namespace CppSharp.Passes
string variable = $@"({(method?.IsStatic == false ?
(@namespace + "::") : string.Empty)}*{wrapper}){signature}";
if (!string.IsNullOrEmpty(returnType.NameSuffix))
Write(returnType.Type, $"{returnType.NameSuffix} {variable}");
else
Write($"{returnType} {variable}");
returnType.NameSuffix.Append(' ').Append(variable);
Write(returnType);
if (function.Access == AccessSpecifier.Protected)
{

Loading…
Cancel
Save