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 9 years ago committed by Dimitar Dobrev
parent
commit
def7d44195
  1. 24
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 28
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs

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

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

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

@ -197,6 +197,9 @@ namespace CppSharp.Generators.CSharp
public override TypePrinterResult VisitPointerType(PointerType pointer, public override TypePrinterResult VisitPointerType(PointerType pointer,
TypeQualifiers quals) TypeQualifiers quals)
{ {
if (MarshalKind == MarshalKind.NativeField)
return IntPtrType;
var pointee = pointer.Pointee; var pointee = pointer.Pointee;
if (pointee is FunctionType) if (pointee is FunctionType)
@ -457,7 +460,7 @@ namespace CppSharp.Generators.CSharp
} }
} }
static string GetIntString(PrimitiveType primitive, ParserTargetInfo targetInfo) static string GetIntString(PrimitiveType primitive, ParserTargetInfo targetInfo)
{ {
uint width; uint width;
bool signed; bool signed;
@ -686,6 +689,29 @@ namespace CppSharp.Generators.CSharp
} }
private CSharpExpressionPrinter expressionPrinter => new CSharpExpressionPrinter(this); 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 public static class CSharpTypePrinterExtensions

Loading…
Cancel
Save