Browse Source

Refactor type printing placeholder and name prefix/suffix code.

pull/1305/head
João Matos 6 years ago committed by João Matos
parent
commit
d3935ec45c
  1. 5
      src/Generator/Generators/C/CCodeGenerator.cs
  2. 34
      src/Generator/Generators/C/CppTypePrinter.cs
  3. 9
      src/Generator/Generators/CLI/CLIHeaders.cs
  4. 15
      src/Generator/Generators/TypePrinter.cs

5
src/Generator/Generators/C/CCodeGenerator.cs

@ -92,8 +92,9 @@ namespace CppSharp.Generators.C @@ -92,8 +92,9 @@ namespace CppSharp.Generators.C
PushBlock();
var typeName = typedef.Type.Visit(CTypePrinter);
WriteLine($"typedef {typeName} {typedef};");
var result = typedef.Type.Visit(CTypePrinter);
result.Name = typedef.Name;
WriteLine($"typedef {result};");
var newlineKind = NewLineKind.BeforeNextBlock;

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

@ -71,11 +71,11 @@ namespace CppSharp.Generators.C @@ -71,11 +71,11 @@ namespace CppSharp.Generators.C
{
var pointeeType = pointer.Pointee.Visit(this, pointer.QualifiedPointee.Qualifiers);
var mod = PrintTypeModifiers ? ConvertModifierToString(pointer.Modifier) : string.Empty;
pointeeType.NameSuffix.Append(mod);
pointeeType.NamePrefix.Append(mod);
var qual = GetStringQuals(quals, false);
if (!string.IsNullOrEmpty(qual))
pointeeType.NameSuffix.Append(' ').Append(qual);
pointeeType.NamePrefix.Append(' ').Append(qual);
return pointeeType;
}
@ -166,7 +166,7 @@ namespace CppSharp.Generators.C @@ -166,7 +166,7 @@ namespace CppSharp.Generators.C
if (ResolveTypedefs && !typedef.Declaration.Type.IsPointerTo(out func))
{
TypePrinterResult type = typedef.Declaration.QualifiedType.Visit(this);
return new TypePrinterResult { Type = $"{qual}{type.Type}", NameSuffix = type.NameSuffix };
return new TypePrinterResult { Type = $"{qual}{type.Type}", NamePrefix = type.NamePrefix, NameSuffix = type.NameSuffix };
}
return $"{qual}{typedef.Declaration.Visit(this)}";
}
@ -320,21 +320,27 @@ namespace CppSharp.Generators.C @@ -320,21 +320,27 @@ namespace CppSharp.Generators.C
return string.Join(", ", args);
}
public override TypePrinterResult VisitParameter(Parameter arg, bool hasName = true)
public override TypePrinterResult VisitParameter(Parameter param, bool hasName = true)
{
string type = arg.Type.Visit(this, arg.QualifiedType.Qualifiers);
string name = arg.Name;
Parameter oldParam = Parameter;
Parameter = param;
var result = param.Type.Visit(this, param.QualifiedType.Qualifiers);
Parameter = oldParam;
string name = param.Name;
bool printName = hasName && !string.IsNullOrEmpty(name);
if (PrintFlavorKind == CppTypePrintFlavorKind.ObjC)
return printName ? $":({type}){name}" : $":({type})";
CppSharp.AST.Type desugared = arg.Type.Desugar();
desugared = (desugared.GetFinalPointee() ?? desugared).Desugar();
return printName ?
((!(arg.Type is TypedefType) || ResolveTypedefs) &&
desugared is FunctionType ?
type.Replace("(*)", $"(*{name})") : $"{type} {name}") : type;
return printName ? $":({result.Type}){name}" : $":({result.Type})";
if (!printName)
return result;
string typeName;
result.Name = param.Name;
typeName = result.ToString();
return typeName;
}
public override TypePrinterResult VisitDelegate(FunctionType function)

9
src/Generator/Generators/CLI/CLIHeaders.cs

@ -759,10 +759,11 @@ namespace CppSharp.Generators.CLI @@ -759,10 +759,11 @@ namespace CppSharp.Generators.CLI
"(System::Runtime::InteropServices::CallingConvention::{0})] ",
interopCallConv);
WriteLine("{0}{1};",
!insideClass ? "public " : "",
string.Format(CTypePrinter.VisitDelegate(functionType).ToString(),
typedef.Name));
var visibility = !insideClass ? "public " : string.Empty;
var result = CTypePrinter.VisitDelegate(functionType);
result.Name = typedef.Name;
WriteLine($"{visibility}{result};");
PopBlock(NewLineKind.BeforeNextBlock);
return true;

15
src/Generator/Generators/TypePrinter.cs

@ -8,8 +8,12 @@ namespace CppSharp.Generators @@ -8,8 +8,12 @@ namespace CppSharp.Generators
public class TypePrinterResult
{
public string Type { get; set; }
public string Name { get; set; } = string.Empty;
public StringBuilder NamePrefix { get; set; } = new StringBuilder();
public StringBuilder NameSuffix { get; set; } = new StringBuilder();
public bool HasNamePlaceholder => Type.Contains("{0}");
public TypePrinterResult(string type = "", string nameSuffix = "")
{
Type = type;
@ -22,9 +26,14 @@ namespace CppSharp.Generators @@ -22,9 +26,14 @@ namespace CppSharp.Generators
public static implicit operator string(TypePrinterResult result) =>
result.ToString();
public override string ToString() =>
NameSuffix.Length > 0 ? Type.Contains("{0}") ?
string.Format(Type, NameSuffix) : Type + NameSuffix : Type;
public override string ToString()
{
if (HasNamePlaceholder)
return string.Format(Type, $"{NamePrefix}{Name}{NameSuffix}");
var namePrefix = (Name.Length > 0) ? $"{NamePrefix} " : NamePrefix.ToString();
return $"{Type}{namePrefix}{Name}{NameSuffix}";
}
}
public class TypePrinter : ITypePrinter<TypePrinterResult>,

Loading…
Cancel
Save