From 2593f4727f23d4974fec5c65a68a90efca31adee Mon Sep 17 00:00:00 2001 From: triton Date: Wed, 2 Sep 2015 17:14:15 +0100 Subject: [PATCH] Added support for getting the array element size to the AST. --- src/AST/Type.cs | 3 +++ src/Core/Parser/ASTConverter.cs | 3 ++- src/CppParser/AST.h | 1 + src/CppParser/Bindings/CLI/AST.cpp | 10 ++++++++ src/CppParser/Bindings/CLI/AST.h | 6 +++++ .../CSharp/i686-apple-darwin12.4.0/AST.cs | 24 +++++++++++++++---- .../Bindings/CSharp/i686-pc-win32-msvc/AST.cs | 24 +++++++++++++++---- .../CSharp/x86_64-apple-darwin12.4.0/AST.cs | 24 +++++++++++++++---- .../Bindings/CSharp/x86_64-linux-gnu/AST.cs | 24 +++++++++++++++---- src/CppParser/Parser.cpp | 5 ++-- 10 files changed, 105 insertions(+), 19 deletions(-) diff --git a/src/AST/Type.cs b/src/AST/Type.cs index bd5deb96..35b27f03 100644 --- a/src/AST/Type.cs +++ b/src/AST/Type.cs @@ -166,6 +166,9 @@ namespace CppSharp.AST // In case of a constant size array. public long Size; + // Size of the element type of the array. + public long ElementSize; + public ArrayType() { } diff --git a/src/Core/Parser/ASTConverter.cs b/src/Core/Parser/ASTConverter.cs index 0cd50239..3c491929 100644 --- a/src/Core/Parser/ASTConverter.cs +++ b/src/Core/Parser/ASTConverter.cs @@ -419,7 +419,8 @@ namespace CppSharp { Size = type.Size, SizeType = VisitArraySizeType(type.SizeType), - QualifiedType = VisitQualified(type.QualifiedType) + QualifiedType = VisitQualified(type.QualifiedType), + ElementSize = type.ElementSize }; VisitType(type, _type); return _type; diff --git a/src/CppParser/AST.h b/src/CppParser/AST.h index bf4f4f0e..ceb4597e 100644 --- a/src/CppParser/AST.h +++ b/src/CppParser/AST.h @@ -84,6 +84,7 @@ public: CppSharp::CppParser::AST::QualifiedType QualifiedType; ArraySize SizeType; long Size; + long ElementSize; }; class Parameter; diff --git a/src/CppParser/Bindings/CLI/AST.cpp b/src/CppParser/Bindings/CLI/AST.cpp index 342c2818..499a8d21 100644 --- a/src/CppParser/Bindings/CLI/AST.cpp +++ b/src/CppParser/Bindings/CLI/AST.cpp @@ -327,6 +327,16 @@ void CppSharp::Parser::AST::ArrayType::Size::set(long value) ((::CppSharp::CppParser::AST::ArrayType*)NativePtr)->Size = value; } +long CppSharp::Parser::AST::ArrayType::ElementSize::get() +{ + return ((::CppSharp::CppParser::AST::ArrayType*)NativePtr)->ElementSize; +} + +void CppSharp::Parser::AST::ArrayType::ElementSize::set(long value) +{ + ((::CppSharp::CppParser::AST::ArrayType*)NativePtr)->ElementSize = value; +} + CppSharp::Parser::AST::FunctionType::FunctionType(::CppSharp::CppParser::AST::FunctionType* native) : CppSharp::Parser::AST::Type((::CppSharp::CppParser::AST::Type*)native) { diff --git a/src/CppParser/Bindings/CLI/AST.h b/src/CppParser/Bindings/CLI/AST.h index 996ab859..9489a6a9 100644 --- a/src/CppParser/Bindings/CLI/AST.h +++ b/src/CppParser/Bindings/CLI/AST.h @@ -508,6 +508,12 @@ namespace CppSharp long get(); void set(long); } + + property long ElementSize + { + long get(); + void set(long); + } }; public ref class FunctionType : CppSharp::Parser::AST::Type diff --git a/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/AST.cs b/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/AST.cs index ed576d6d..ab3e4196 100644 --- a/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/AST.cs +++ b/src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/AST.cs @@ -740,7 +740,7 @@ namespace CppSharp public unsafe partial class ArrayType : CppSharp.Parser.AST.Type, IDisposable { - [StructLayout(LayoutKind.Explicit, Size = 24)] + [StructLayout(LayoutKind.Explicit, Size = 28)] public new partial struct Internal { [FieldOffset(0)] @@ -758,6 +758,9 @@ namespace CppSharp [FieldOffset(20)] public int Size; + [FieldOffset(24)] + public int ElementSize; + [SuppressUnmanagedCodeSecurity] [DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl, EntryPoint="_ZN8CppSharp9CppParser3AST9ArrayTypeC2Ev")] @@ -791,7 +794,7 @@ namespace CppSharp private static ArrayType.Internal* __CopyValue(ArrayType.Internal native) { - var ret = Marshal.AllocHGlobal(24); + var ret = Marshal.AllocHGlobal(28); CppSharp.Parser.AST.ArrayType.Internal.cctor_2(ret, new global::System.IntPtr(&native)); return (ArrayType.Internal*) ret; } @@ -811,7 +814,7 @@ namespace CppSharp public ArrayType() : this((Internal*) null) { - __Instance = Marshal.AllocHGlobal(24); + __Instance = Marshal.AllocHGlobal(28); __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; Internal.ctor_0(__Instance); @@ -820,7 +823,7 @@ namespace CppSharp public ArrayType(CppSharp.Parser.AST.ArrayType _0) : this((Internal*) null) { - __Instance = Marshal.AllocHGlobal(24); + __Instance = Marshal.AllocHGlobal(28); __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; var arg0 = ReferenceEquals(_0, null) ? global::System.IntPtr.Zero : _0.__Instance; @@ -884,6 +887,19 @@ namespace CppSharp ((Internal*) __Instance)->Size = value; } } + + public int ElementSize + { + get + { + return ((Internal*) __Instance)->ElementSize; + } + + set + { + ((Internal*) __Instance)->ElementSize = value; + } + } } public unsafe partial class FunctionType : CppSharp.Parser.AST.Type, IDisposable diff --git a/src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/AST.cs b/src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/AST.cs index 33899fa4..4fa21f32 100644 --- a/src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/AST.cs +++ b/src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/AST.cs @@ -740,7 +740,7 @@ namespace CppSharp public unsafe partial class ArrayType : CppSharp.Parser.AST.Type, IDisposable { - [StructLayout(LayoutKind.Explicit, Size = 24)] + [StructLayout(LayoutKind.Explicit, Size = 28)] public new partial struct Internal { [FieldOffset(0)] @@ -758,6 +758,9 @@ namespace CppSharp [FieldOffset(20)] public int Size; + [FieldOffset(24)] + public int ElementSize; + [SuppressUnmanagedCodeSecurity] [DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall, EntryPoint="??0ArrayType@AST@CppParser@CppSharp@@QAE@XZ")] @@ -791,7 +794,7 @@ namespace CppSharp private static ArrayType.Internal* __CopyValue(ArrayType.Internal native) { - var ret = Marshal.AllocHGlobal(24); + var ret = Marshal.AllocHGlobal(28); CppSharp.Parser.AST.ArrayType.Internal.cctor_2(ret, new global::System.IntPtr(&native)); return (ArrayType.Internal*) ret; } @@ -811,7 +814,7 @@ namespace CppSharp public ArrayType() : this((Internal*) null) { - __Instance = Marshal.AllocHGlobal(24); + __Instance = Marshal.AllocHGlobal(28); __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; Internal.ctor_0(__Instance); @@ -820,7 +823,7 @@ namespace CppSharp public ArrayType(CppSharp.Parser.AST.ArrayType _0) : this((Internal*) null) { - __Instance = Marshal.AllocHGlobal(24); + __Instance = Marshal.AllocHGlobal(28); __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; var arg0 = ReferenceEquals(_0, null) ? global::System.IntPtr.Zero : _0.__Instance; @@ -884,6 +887,19 @@ namespace CppSharp ((Internal*) __Instance)->Size = value; } } + + public int ElementSize + { + get + { + return ((Internal*) __Instance)->ElementSize; + } + + set + { + ((Internal*) __Instance)->ElementSize = value; + } + } } public unsafe partial class FunctionType : CppSharp.Parser.AST.Type, IDisposable diff --git a/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/AST.cs b/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/AST.cs index efe6982c..71e7b35a 100644 --- a/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/AST.cs +++ b/src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/AST.cs @@ -740,7 +740,7 @@ namespace CppSharp public unsafe partial class ArrayType : CppSharp.Parser.AST.Type, IDisposable { - [StructLayout(LayoutKind.Explicit, Size = 40)] + [StructLayout(LayoutKind.Explicit, Size = 48)] public new partial struct Internal { [FieldOffset(0)] @@ -758,6 +758,9 @@ namespace CppSharp [FieldOffset(32)] public long Size; + [FieldOffset(40)] + public long ElementSize; + [SuppressUnmanagedCodeSecurity] [DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl, EntryPoint="_ZN8CppSharp9CppParser3AST9ArrayTypeC2Ev")] @@ -791,7 +794,7 @@ namespace CppSharp private static ArrayType.Internal* __CopyValue(ArrayType.Internal native) { - var ret = Marshal.AllocHGlobal(40); + var ret = Marshal.AllocHGlobal(48); CppSharp.Parser.AST.ArrayType.Internal.cctor_2(ret, new global::System.IntPtr(&native)); return (ArrayType.Internal*) ret; } @@ -811,7 +814,7 @@ namespace CppSharp public ArrayType() : this((Internal*) null) { - __Instance = Marshal.AllocHGlobal(40); + __Instance = Marshal.AllocHGlobal(48); __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; Internal.ctor_0(__Instance); @@ -820,7 +823,7 @@ namespace CppSharp public ArrayType(CppSharp.Parser.AST.ArrayType _0) : this((Internal*) null) { - __Instance = Marshal.AllocHGlobal(40); + __Instance = Marshal.AllocHGlobal(48); __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; var arg0 = ReferenceEquals(_0, null) ? global::System.IntPtr.Zero : _0.__Instance; @@ -884,6 +887,19 @@ namespace CppSharp ((Internal*) __Instance)->Size = value; } } + + public long ElementSize + { + get + { + return ((Internal*) __Instance)->ElementSize; + } + + set + { + ((Internal*) __Instance)->ElementSize = value; + } + } } public unsafe partial class FunctionType : CppSharp.Parser.AST.Type, IDisposable diff --git a/src/CppParser/Bindings/CSharp/x86_64-linux-gnu/AST.cs b/src/CppParser/Bindings/CSharp/x86_64-linux-gnu/AST.cs index 4e4b858e..20d5e1e6 100644 --- a/src/CppParser/Bindings/CSharp/x86_64-linux-gnu/AST.cs +++ b/src/CppParser/Bindings/CSharp/x86_64-linux-gnu/AST.cs @@ -740,7 +740,7 @@ namespace CppSharp public unsafe partial class ArrayType : CppSharp.Parser.AST.Type, IDisposable { - [StructLayout(LayoutKind.Explicit, Size = 40)] + [StructLayout(LayoutKind.Explicit, Size = 48)] public new partial struct Internal { [FieldOffset(0)] @@ -758,6 +758,9 @@ namespace CppSharp [FieldOffset(32)] public long Size; + [FieldOffset(40)] + public long ElementSize; + [SuppressUnmanagedCodeSecurity] [DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl, EntryPoint="_ZN8CppSharp9CppParser3AST9ArrayTypeC2Ev")] @@ -791,7 +794,7 @@ namespace CppSharp private static ArrayType.Internal* __CopyValue(ArrayType.Internal native) { - var ret = Marshal.AllocHGlobal(40); + var ret = Marshal.AllocHGlobal(48); CppSharp.Parser.AST.ArrayType.Internal.cctor_2(ret, new global::System.IntPtr(&native)); return (ArrayType.Internal*) ret; } @@ -811,7 +814,7 @@ namespace CppSharp public ArrayType() : this((Internal*) null) { - __Instance = Marshal.AllocHGlobal(40); + __Instance = Marshal.AllocHGlobal(48); __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; Internal.ctor_0(__Instance); @@ -820,7 +823,7 @@ namespace CppSharp public ArrayType(CppSharp.Parser.AST.ArrayType _0) : this((Internal*) null) { - __Instance = Marshal.AllocHGlobal(40); + __Instance = Marshal.AllocHGlobal(48); __ownsNativeInstance = true; NativeToManagedMap[__Instance] = this; var arg0 = ReferenceEquals(_0, null) ? global::System.IntPtr.Zero : _0.__Instance; @@ -884,6 +887,19 @@ namespace CppSharp ((Internal*) __Instance)->Size = value; } } + + public long ElementSize + { + get + { + return ((Internal*) __Instance)->ElementSize; + } + + set + { + ((Internal*) __Instance)->ElementSize = value; + } + } } public unsafe partial class FunctionType : CppSharp.Parser.AST.Type, IDisposable diff --git a/src/CppParser/Parser.cpp b/src/CppParser/Parser.cpp index c8db1808..418a11ab 100644 --- a/src/CppParser/Parser.cpp +++ b/src/CppParser/Parser.cpp @@ -1750,10 +1750,11 @@ Type* Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL, if (TL && !TL->isNull()) Next = TL->getNextTypeLoc(); auto A = new ArrayType(); - A->QualifiedType = GetQualifiedType(AT->getElementType(), - WalkType(AT->getElementType(), &Next)); + auto ElemTy = AT->getElementType(); + A->QualifiedType = GetQualifiedType(ElemTy, WalkType(ElemTy, &Next)); A->SizeType = ArrayType::ArraySize::Constant; A->Size = AST->getConstantArrayElementCount(AT); + A->ElementSize = (long)AST->getTypeSize(ElemTy); Ty = A; break;