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
PushBlock(); PushBlock();
var typeName = typedef.Type.Visit(CTypePrinter); var result = typedef.Type.Visit(CTypePrinter);
WriteLine($"typedef {typeName} {typedef};"); result.Name = typedef.Name;
WriteLine($"typedef {result};");
var newlineKind = NewLineKind.BeforeNextBlock; var newlineKind = NewLineKind.BeforeNextBlock;

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

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

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

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

15
src/Generator/Generators/TypePrinter.cs

@ -8,8 +8,12 @@ namespace CppSharp.Generators
public class TypePrinterResult public class TypePrinterResult
{ {
public string Type { get; set; } 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 StringBuilder NameSuffix { get; set; } = new StringBuilder();
public bool HasNamePlaceholder => Type.Contains("{0}");
public TypePrinterResult(string type = "", string nameSuffix = "") public TypePrinterResult(string type = "", string nameSuffix = "")
{ {
Type = type; Type = type;
@ -22,9 +26,14 @@ namespace CppSharp.Generators
public static implicit operator string(TypePrinterResult result) => public static implicit operator string(TypePrinterResult result) =>
result.ToString(); result.ToString();
public override string ToString() => public override string ToString()
NameSuffix.Length > 0 ? Type.Contains("{0}") ? {
string.Format(Type, NameSuffix) : Type + NameSuffix : Type; 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>, public class TypePrinter : ITypePrinter<TypePrinterResult>,

Loading…
Cancel
Save