Browse Source

Moved the printing of fields from the C# generator to the C# printer. (#840)

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/841/head
Kimon Topouzidis 8 years ago committed by Dimitar Dobrev
parent
commit
def7d44195
  1. 26
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 32
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs

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

@ -643,38 +643,22 @@ namespace CppSharp.Generators.CSharp @@ -643,38 +643,22 @@ namespace CppSharp.Generators.CSharp
coreType.IsPrimitiveType(PrimitiveType.Half))
return;
var safeIdentifier = SafeIdentifier(field.Name);
if(safeIdentifier.All(c => c.Equals('_')))
{
safeIdentifier = SafeIdentifier(field.Name);
}
TypePrinterResult retType = TypePrinter.VisitFieldDecl(
new Field { Name = field.Name, QualifiedType = field.QualifiedType });
PushBlock(BlockKind.Field);
if (!Options.GenerateSequentialLayout || @class.IsUnion)
WriteLine($"[FieldOffset({field.Offset})]");
TypePrinter.PushMarshalKind(MarshalKind.NativeField);
var fieldTypePrinted = field.QualifiedType.CSharpType(TypePrinter);
TypePrinter.PopMarshalKind();
var fieldType = field.QualifiedType.Type.Desugar().IsAddress() ?
CSharpTypePrinter.IntPtrType : fieldTypePrinted.Type;
var fieldName = safeIdentifier;
if (!string.IsNullOrWhiteSpace(fieldTypePrinted.NameSuffix))
fieldName += fieldTypePrinted.NameSuffix;
var access = decl != null && !decl.IsGenerated ? "internal" : "public";
if (field.Expression != null)
{
var fieldValuePrinted = field.Expression.CSharpValue(ExpressionPrinter);
Write($"{access} {fieldType} {fieldName} = {fieldValuePrinted};");
Write($"{access} {retType}{retType.NameSuffix} = {fieldValuePrinted};");
}
else
{
Write($"{access} {fieldType} {fieldName};");
Write($"{access} {retType}{retType.NameSuffix};");
}
PopBlock(NewLineKind.BeforeNextBlock);

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

@ -64,7 +64,7 @@ namespace CppSharp.Generators.CSharp @@ -64,7 +64,7 @@ namespace CppSharp.Generators.CSharp
{
PrimitiveType primitiveType;
if ((arrayType.IsPointerToPrimitiveType(out primitiveType) &&
!(arrayType is FunctionType)) ||
!(arrayType is FunctionType)) ||
(arrayType.IsPrimitiveType() && MarshalKind != MarshalKind.NativeField))
{
if (primitiveType == PrimitiveType.Void)
@ -197,6 +197,9 @@ namespace CppSharp.Generators.CSharp @@ -197,6 +197,9 @@ namespace CppSharp.Generators.CSharp
public override TypePrinterResult VisitPointerType(PointerType pointer,
TypeQualifiers quals)
{
if (MarshalKind == MarshalKind.NativeField)
return IntPtrType;
var pointee = pointer.Pointee;
if (pointee is FunctionType)
@ -347,7 +350,7 @@ namespace CppSharp.Generators.CSharp @@ -347,7 +350,7 @@ namespace CppSharp.Generators.CSharp
Kind = ContextKind,
MarshalKind = MarshalKind
};
var type = typeMap.CSharpSignature(typePrinterContext);
if (!string.IsNullOrEmpty(type))
{
@ -457,7 +460,7 @@ namespace CppSharp.Generators.CSharp @@ -457,7 +460,7 @@ namespace CppSharp.Generators.CSharp
}
}
static string GetIntString(PrimitiveType primitive, ParserTargetInfo targetInfo)
static string GetIntString(PrimitiveType primitive, ParserTargetInfo targetInfo)
{
uint width;
bool signed;
@ -686,6 +689,29 @@ namespace CppSharp.Generators.CSharp @@ -686,6 +689,29 @@ namespace CppSharp.Generators.CSharp
}
private CSharpExpressionPrinter expressionPrinter => new CSharpExpressionPrinter(this);
public override TypePrinterResult VisitFieldDecl(Field field)
{
var cSharpSourcesDummy = new CSharpSources(Context, new List<TranslationUnit>());
var safeIdentifier = cSharpSourcesDummy.SafeIdentifier(field.Name);
if (safeIdentifier.All(c => c.Equals('_')))
{
safeIdentifier = cSharpSourcesDummy.SafeIdentifier(field.Name);
}
PushMarshalKind(MarshalKind.NativeField);
var fieldTypePrinted = field.QualifiedType.Visit(this);
PopMarshalKind();
var returnTypePrinter = new TypePrinterResult();
if (!string.IsNullOrWhiteSpace(fieldTypePrinted.NameSuffix))
returnTypePrinter.NameSuffix = fieldTypePrinted.NameSuffix;
returnTypePrinter.Type = $"{fieldTypePrinted.Type} {safeIdentifier}";
return returnTypePrinter;
}
}
public static class CSharpTypePrinterExtensions

Loading…
Cancel
Save