From 749e5a399e1a8d6e3a4919c74d6780c6f1634a88 Mon Sep 17 00:00:00 2001 From: Elias Holzer Date: Tue, 6 May 2014 14:03:59 +0200 Subject: [PATCH] Added new property Parameter.Index. Also added test which checks whether both parsers assign the AST parameter properties properly. --- src/AST/Function.cs | 1 + src/Core/Parser/ASTConverter.cs | 1 + src/CppParser/AST.h | 1 + src/CppParser/Bindings/CLI/AST.cpp | 10 +++++ src/CppParser/Bindings/CLI/AST.h | 6 +++ src/CppParser/Bindings/CSharp/AST.cs | 22 +++++++++- src/CppParser/Parser.cpp | 1 + src/Generator.Tests/AST/TestAST.cs | 61 ++++++++++++++++++++++++++++ src/Parser/Parser.cpp | 1 + tests/Native/AST.h | 2 + 10 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 src/Generator.Tests/AST/TestAST.cs create mode 100644 tests/Native/AST.h diff --git a/src/AST/Function.cs b/src/AST/Function.cs index a3c795f8..6209e0a7 100644 --- a/src/AST/Function.cs +++ b/src/AST/Function.cs @@ -40,6 +40,7 @@ namespace CppSharp.AST public Type Type { get { return QualifiedType.Type; } } public QualifiedType QualifiedType { get; set; } public bool IsIndirect { get; set; } + public uint Index { get; set; } public ParameterKind Kind { get; set; } public ParameterUsage Usage { get; set; } diff --git a/src/Core/Parser/ASTConverter.cs b/src/Core/Parser/ASTConverter.cs index 73b0c136..30259c37 100644 --- a/src/Core/Parser/ASTConverter.cs +++ b/src/Core/Parser/ASTConverter.cs @@ -803,6 +803,7 @@ namespace CppSharp _param.QualifiedType = typeConverter.VisitQualified(decl.QualifiedType); _param.IsIndirect = decl.IsIndirect; _param.HasDefaultValue = decl.HasDefaultValue; + _param.Index = decl.Index; return _param; } diff --git a/src/CppParser/AST.h b/src/CppParser/AST.h index a6963afa..ad124955 100644 --- a/src/CppParser/AST.h +++ b/src/CppParser/AST.h @@ -433,6 +433,7 @@ struct CS_API Parameter : public Declaration CppSharp::CppParser::AST::QualifiedType QualifiedType; bool IsIndirect; bool HasDefaultValue; + unsigned int Index; }; enum struct CXXMethodKind diff --git a/src/CppParser/Bindings/CLI/AST.cpp b/src/CppParser/Bindings/CLI/AST.cpp index 52512be4..635b1d41 100644 --- a/src/CppParser/Bindings/CLI/AST.cpp +++ b/src/CppParser/Bindings/CLI/AST.cpp @@ -1459,6 +1459,16 @@ void CppSharp::Parser::AST::Parameter::HasDefaultValue::set(bool value) ((::CppSharp::CppParser::AST::Parameter*)NativePtr)->HasDefaultValue = value; } +unsigned int CppSharp::Parser::AST::Parameter::Index::get() +{ + return ((::CppSharp::CppParser::AST::Parameter*)NativePtr)->Index; +} + +void CppSharp::Parser::AST::Parameter::Index::set(unsigned int value) +{ + ((::CppSharp::CppParser::AST::Parameter*)NativePtr)->Index = value; +} + CppSharp::Parser::AST::Function::Function(::CppSharp::CppParser::AST::Function* native) : CppSharp::Parser::AST::Declaration((::CppSharp::CppParser::AST::Declaration*)native) { diff --git a/src/CppParser/Bindings/CLI/AST.h b/src/CppParser/Bindings/CLI/AST.h index 12b55362..b5471ce2 100644 --- a/src/CppParser/Bindings/CLI/AST.h +++ b/src/CppParser/Bindings/CLI/AST.h @@ -1120,6 +1120,12 @@ namespace CppSharp bool get(); void set(bool); } + + property unsigned int Index + { + unsigned int get(); + void set(unsigned int); + } }; public ref class Function : CppSharp::Parser::AST::Declaration diff --git a/src/CppParser/Bindings/CSharp/AST.cs b/src/CppParser/Bindings/CSharp/AST.cs index 8da40492..47ec94c4 100644 --- a/src/CppParser/Bindings/CSharp/AST.cs +++ b/src/CppParser/Bindings/CSharp/AST.cs @@ -3389,7 +3389,7 @@ namespace CppSharp public unsafe partial class Parameter : CppSharp.Parser.AST.Declaration, IDisposable { - [StructLayout(LayoutKind.Explicit, Size = 104)] + [StructLayout(LayoutKind.Explicit, Size = 108)] public new struct Internal { [FieldOffset(0)] @@ -3437,6 +3437,9 @@ namespace CppSharp [FieldOffset(101)] internal bool HasDefaultValue; + [FieldOffset(104)] + internal uint Index; + [SuppressUnmanagedCodeSecurity] [DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall, EntryPoint="??0Parameter@AST@CppParser@CppSharp@@QAE@XZ")] @@ -3471,7 +3474,7 @@ namespace CppSharp public Parameter() : this(IntPtr.Zero) { - __Instance = Marshal.AllocHGlobal(104); + __Instance = Marshal.AllocHGlobal(108); Internal.ctor_0(__Instance); } @@ -3526,6 +3529,21 @@ namespace CppSharp __ptr->HasDefaultValue = value; } } + + public uint Index + { + get + { + var __ptr = (Internal*)__Instance.ToPointer(); + return __ptr->Index; + } + + set + { + var __ptr = (Internal*)__Instance.ToPointer(); + __ptr->Index = value; + } + } } public unsafe partial class Function : CppSharp.Parser.AST.Declaration, IDisposable diff --git a/src/CppParser/Parser.cpp b/src/CppParser/Parser.cpp index 66fafabf..4bc41dcc 100644 --- a/src/CppParser/Parser.cpp +++ b/src/CppParser/Parser.cpp @@ -1885,6 +1885,7 @@ void Parser::WalkFunction(clang::FunctionDecl* FD, Function* F, P->QualifiedType = GetQualifiedType(VD->getType(), WalkType(VD->getType(), &PTL)); P->HasDefaultValue = VD->hasDefaultArg(); P->_Namespace = NS; + P->Index = VD->getFunctionScopeIndex(); HandleDeclaration(VD, P); F->Parameters.push_back(P); diff --git a/src/Generator.Tests/AST/TestAST.cs b/src/Generator.Tests/AST/TestAST.cs new file mode 100644 index 00000000..872a7081 --- /dev/null +++ b/src/Generator.Tests/AST/TestAST.cs @@ -0,0 +1,61 @@ +using System.Linq; +using CppSharp.Passes; +using CppSharp.AST; +using CppSharp.AST.Extensions; +using NUnit.Framework; + +namespace CppSharp.Generator.Tests.AST +{ + [TestFixture] + public class TestAST : ASTTestFixture + { + private PassBuilder passBuilder; + + [TestFixtureSetUp] + public void Init() + { + } + + [SetUp] + public void Setup() + { + ParseLibrary("AST.h"); + passBuilder = new PassBuilder(Driver); + } + + [Test] + public void TestASTParameter() + { + var func = AstContext.FindFunction("TestParameterProperties").FirstOrDefault(); + Assert.IsNotNull(func); + + var paramNames = new [] { "a", "b", "c" }; + var paramTypes = new [] + { + new QualifiedType(new BuiltinType(PrimitiveType.Bool)), + new QualifiedType( + new PointerType() + { + Modifier = PointerType.TypeModifier.LVReference, + QualifiedPointee = new QualifiedType( + new BuiltinType(PrimitiveType.Int16), + new TypeQualifiers() { IsConst = true }) + }), + new QualifiedType( + new PointerType() + { + Modifier = PointerType.TypeModifier.Pointer, + QualifiedPointee = new QualifiedType(new BuiltinType(PrimitiveType.Int32)) + }) + }; + for (int i = 0; i < func.Parameters.Count; i++) + { + var param = func.Parameters[i]; + Assert.AreEqual(paramNames[i], param.Name, "Parameter.Name"); + Assert.AreEqual(paramTypes[i], param.QualifiedType, "Parameter.QualifiedType"); + Assert.AreEqual(i, param.Index, "Parameter.Index"); + } + Assert.IsTrue(func.Parameters[2].HasDefaultValue, "Parameter.HasDefaultValue"); + } + } +} diff --git a/src/Parser/Parser.cpp b/src/Parser/Parser.cpp index 747be19c..d2ca2fb5 100644 --- a/src/Parser/Parser.cpp +++ b/src/Parser/Parser.cpp @@ -1900,6 +1900,7 @@ void Parser::WalkFunction(clang::FunctionDecl* FD, CppSharp::AST::Function^ F, P->QualifiedType = GetQualifiedType(VD->getType(), WalkType(VD->getType(), &PTL)); P->HasDefaultValue = VD->hasDefaultArg(); P->Namespace = NS; + P->Index = VD->getFunctionScopeIndex(); HandleDeclaration(VD, P); F->Parameters->Add(P); diff --git a/tests/Native/AST.h b/tests/Native/AST.h new file mode 100644 index 00000000..3e917755 --- /dev/null +++ b/tests/Native/AST.h @@ -0,0 +1,2 @@ +// Tests assignment of AST.Parameter properties +void TestParameterProperties(bool a, const short& b, int* c = nullptr) {};