From 240d2db53db3026d78c132a735e6973e79996238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Krog?= Date: Tue, 11 Feb 2014 22:29:31 +0100 Subject: [PATCH 1/4] Add new IExpressionPrinter interface, with CSharpExpressionPrinter implementation --- src/AST/Expression.cs | 49 +++++++++++++++++++ .../CSharp/CSharpExpressionPrinter.cs | 41 ++++++++++++++++ src/Generator/Types/IExpressionPrinter.cs | 13 +++++ 3 files changed, 103 insertions(+) create mode 100644 src/AST/Expression.cs create mode 100644 src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs create mode 100644 src/Generator/Types/IExpressionPrinter.cs diff --git a/src/AST/Expression.cs b/src/AST/Expression.cs new file mode 100644 index 00000000..24e89cd9 --- /dev/null +++ b/src/AST/Expression.cs @@ -0,0 +1,49 @@ +using System; + +namespace CppSharp.AST +{ + public abstract class Expression + { + public string DebugText; + + public abstract TV Visit(IExpressionVisitor visitor); + } + + public class BuiltinTypeExpression : Expression + { + public long Value { get; set; } + + public BuiltinType Type { get; set; } + + public bool IsHexadecimal + { + get + { + if (DebugText == null) + { + return false; + } + return DebugText.Contains("0x") || DebugText.Contains("0X"); + } + } + + public override string ToString() + { + var printAsHex = IsHexadecimal && Type.IsUnsigned; + var format = printAsHex ? "x" : string.Empty; + var value = Type.IsUnsigned ? Value.ToString(format) : + ((long)Value).ToString(format); + return printAsHex ? "0x" + value : value; + } + + public override T Visit(IExpressionVisitor visitor) + { + return visitor.VisitBuiltinExpression(this); + } + } + + public interface IExpressionVisitor + { + T VisitBuiltinExpression(BuiltinTypeExpression builtinType); + } +} \ No newline at end of file diff --git a/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs b/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs new file mode 100644 index 00000000..9b450e60 --- /dev/null +++ b/src/Generator/Generators/CSharp/CSharpExpressionPrinter.cs @@ -0,0 +1,41 @@ +using CppSharp.AST; +using CppSharp.Types; + +namespace CppSharp.Generators.CSharp +{ + public class CSharpExpressionPrinterResult + { + public string Value; + + public override string ToString() + { + return Value; + } + } + + public static class CSharpExpressionPrinterExtensions + { + public static CSharpExpressionPrinterResult CSharpValue(this Expression value, + CSharpExpressionPrinter printer) + { + return value.Visit(printer); + } + + } + public class CSharpExpressionPrinter : IExpressionPrinter, + IExpressionVisitor + { + public CSharpExpressionPrinterResult VisitBuiltinExpression(BuiltinTypeExpression builtinType) + { + return new CSharpExpressionPrinterResult() + { + Value = builtinType.ToString(), + }; + } + + public string ToString(Type type) + { + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/Generator/Types/IExpressionPrinter.cs b/src/Generator/Types/IExpressionPrinter.cs new file mode 100644 index 00000000..19edd17a --- /dev/null +++ b/src/Generator/Types/IExpressionPrinter.cs @@ -0,0 +1,13 @@ +using CppSharp.AST; + +namespace CppSharp.Types +{ + public interface IExpressionPrinter + { + string ToString(Type type); + } + + public interface IExpressionPrinter : IExpressionPrinter, IExpressionVisitor + { + } +} \ No newline at end of file From c8b69fd601f34aa399c6f10533cad7b1f8285ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Krog?= Date: Tue, 11 Feb 2014 22:31:53 +0100 Subject: [PATCH 2/4] Add Expression property in Field, allows a field to have a "value" --- src/AST/Field.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/AST/Field.cs b/src/AST/Field.cs index 82bd5634..e84d83cd 100644 --- a/src/AST/Field.cs +++ b/src/AST/Field.cs @@ -16,6 +16,8 @@ namespace CppSharp.AST get { return Offset / (sizeof (byte) * 8); } } + public Expression Expression { get; set; } + public Field() { Offset = 0; From df7c1371a679ab9bc268645919c5ad21262a9f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Krog?= Date: Tue, 11 Feb 2014 22:33:41 +0100 Subject: [PATCH 3/4] Pass in an CSharpExpressionPrinter instance to CSharpTextTemplate --- src/Generator/Generators/CSharp/CSharpGenerator.cs | 4 +++- src/Generator/Generators/CSharp/CSharpTextTemplate.cs | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpGenerator.cs b/src/Generator/Generators/CSharp/CSharpGenerator.cs index 40ce6b86..949568c0 100644 --- a/src/Generator/Generators/CSharp/CSharpGenerator.cs +++ b/src/Generator/Generators/CSharp/CSharpGenerator.cs @@ -7,10 +7,12 @@ namespace CppSharp.Generators.CSharp public class CSharpGenerator : Generator { private readonly CSharpTypePrinter typePrinter; + private readonly CSharpExpressionPrinter expressionPrinter; public CSharpGenerator(Driver driver) : base(driver) { typePrinter = new CSharpTypePrinter(driver.TypeDatabase, driver.ASTContext); + expressionPrinter = new CSharpExpressionPrinter(); CppSharp.AST.Type.TypePrinterDelegate += type => type.Visit(typePrinter).Type; } @@ -18,7 +20,7 @@ namespace CppSharp.Generators.CSharp { var outputs = new List