Browse Source

Added support for C++ attributed types to the parser, AST ,generators and tests.

pull/131/head
triton 12 years ago
parent
commit
6402c3ca55
  1. 8
      src/AST/ASTVisitor.cs
  2. 37
      src/AST/Type.cs
  3. 6
      src/CppParser/AST.h
  4. 37
      src/CppParser/Bindings/CLI/AST.cpp
  5. 21
      src/CppParser/Bindings/CLI/AST.h
  6. 93
      src/CppParser/Bindings/CSharp/AST.cs
  7. 19
      src/CppParser/Parser.cpp
  8. 5
      src/Generator/Generators/CLI/CLITypePrinter.cs
  9. 5
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  10. 5
      src/Generator/Passes/CheckVirtualOverrideReturnCovariance.cs
  11. 5
      src/Generator/Types/CppTypePrinter.cs
  12. 19
      src/Parser/Parser.cpp
  13. 8
      tests/Basic/Basic.Tests.cs
  14. 6
      tests/Basic/Basic.cpp
  15. 15
      tests/Basic/Basic.h

8
src/AST/ASTVisitor.cs

@ -137,6 +137,14 @@ namespace CppSharp.AST @@ -137,6 +137,14 @@ namespace CppSharp.AST
return typedef.Declaration.Visit(this);
}
public bool VisitAttributedType(AttributedType attributed, TypeQualifiers quals)
{
if (!VisitType(attributed, quals))
return false;
return attributed.Modified.Visit(this);
}
public virtual bool VisitDecayedType(DecayedType decayed, TypeQualifiers quals)
{
if (!VisitType(decayed, quals))

37
src/AST/Type.cs

@ -461,6 +461,42 @@ namespace CppSharp.AST @@ -461,6 +461,42 @@ namespace CppSharp.AST
}
}
/// <summary>
/// An attributed type is a type to which a type attribute has been
/// applied.
///
/// For example, in the following attributed type:
/// int32_t __attribute__((vector_size(16)))
///
/// The modified type is the TypedefType for int32_t
/// The equivalent type is VectorType(16, int32_t)
/// </summary>
public class AttributedType : Type
{
public QualifiedType Modified;
public QualifiedType Equivalent;
public override T Visit<T>(ITypeVisitor<T> visitor, TypeQualifiers quals)
{
return visitor.VisitAttributedType(this, quals);
}
public override bool Equals(object obj)
{
var attributed = obj as AttributedType;
if (attributed == null) return false;
return Modified.Equals(attributed.Modified)
&& Equivalent.Equals(attributed.Equivalent);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
/// <summary>
/// Represents a pointer type decayed from an array or function type.
/// </summary>
@ -824,6 +860,7 @@ namespace CppSharp.AST @@ -824,6 +860,7 @@ namespace CppSharp.AST
T VisitMemberPointerType(MemberPointerType member, TypeQualifiers quals);
T VisitBuiltinType(BuiltinType builtin, TypeQualifiers quals);
T VisitTypedefType(TypedefType typedef, TypeQualifiers quals);
T VisitAttributedType(AttributedType attributed, TypeQualifiers quals);
T VisitDecayedType(DecayedType decayed, TypeQualifiers quals);
T VisitTemplateSpecializationType(TemplateSpecializationType template,
TypeQualifiers quals);

6
src/CppParser/AST.h

@ -114,6 +114,12 @@ struct CS_API TypedefType : public Type @@ -114,6 +114,12 @@ struct CS_API TypedefType : public Type
TypedefDecl* Declaration;
};
struct CS_API AttributedType : public Type
{
QualifiedType Modified;
QualifiedType Equivalent;
};
struct CS_API DecayedType : public Type
{
QualifiedType Decayed;

37
src/CppParser/Bindings/CLI/AST.cpp

@ -378,6 +378,43 @@ void CppSharp::Parser::AST::TypedefType::Declaration::set(CppSharp::Parser::AST: @@ -378,6 +378,43 @@ void CppSharp::Parser::AST::TypedefType::Declaration::set(CppSharp::Parser::AST:
((::CppSharp::CppParser::AST::TypedefType*)NativePtr)->Declaration = (::CppSharp::CppParser::AST::TypedefDecl*)value->NativePtr;
}
CppSharp::Parser::AST::AttributedType::AttributedType(::CppSharp::CppParser::AST::AttributedType* native)
: CppSharp::Parser::AST::Type((::CppSharp::CppParser::AST::Type*)native)
{
}
CppSharp::Parser::AST::AttributedType::AttributedType(System::IntPtr native)
: CppSharp::Parser::AST::Type(native)
{
auto __native = (::CppSharp::CppParser::AST::AttributedType*)native.ToPointer();
}
CppSharp::Parser::AST::AttributedType::AttributedType()
: CppSharp::Parser::AST::Type((::CppSharp::CppParser::AST::Type*)nullptr)
{
NativePtr = new ::CppSharp::CppParser::AST::AttributedType();
}
CppSharp::Parser::AST::QualifiedType^ CppSharp::Parser::AST::AttributedType::Modified::get()
{
return gcnew CppSharp::Parser::AST::QualifiedType((::CppSharp::CppParser::AST::QualifiedType*)&((::CppSharp::CppParser::AST::AttributedType*)NativePtr)->Modified);
}
void CppSharp::Parser::AST::AttributedType::Modified::set(CppSharp::Parser::AST::QualifiedType^ value)
{
((::CppSharp::CppParser::AST::AttributedType*)NativePtr)->Modified = *(::CppSharp::CppParser::AST::QualifiedType*)value->NativePtr;
}
CppSharp::Parser::AST::QualifiedType^ CppSharp::Parser::AST::AttributedType::Equivalent::get()
{
return gcnew CppSharp::Parser::AST::QualifiedType((::CppSharp::CppParser::AST::QualifiedType*)&((::CppSharp::CppParser::AST::AttributedType*)NativePtr)->Equivalent);
}
void CppSharp::Parser::AST::AttributedType::Equivalent::set(CppSharp::Parser::AST::QualifiedType^ value)
{
((::CppSharp::CppParser::AST::AttributedType*)NativePtr)->Equivalent = *(::CppSharp::CppParser::AST::QualifiedType*)value->NativePtr;
}
CppSharp::Parser::AST::DecayedType::DecayedType(::CppSharp::CppParser::AST::DecayedType* native)
: CppSharp::Parser::AST::Type((::CppSharp::CppParser::AST::Type*)native)
{

21
src/CppParser/Bindings/CLI/AST.h

@ -49,6 +49,7 @@ namespace CppSharp @@ -49,6 +49,7 @@ namespace CppSharp
ref class PointerType;
ref class MemberPointerType;
ref class TypedefType;
ref class AttributedType;
ref class DecayedType;
ref class TemplateArgument;
ref class TemplateSpecializationType;
@ -428,6 +429,26 @@ namespace CppSharp @@ -428,6 +429,26 @@ namespace CppSharp
}
};
public ref class AttributedType : CppSharp::Parser::AST::Type
{
public:
AttributedType(::CppSharp::CppParser::AST::AttributedType* native);
AttributedType(System::IntPtr native);
AttributedType();
property CppSharp::Parser::AST::QualifiedType^ Modified
{
CppSharp::Parser::AST::QualifiedType^ get();
void set(CppSharp::Parser::AST::QualifiedType^);
}
property CppSharp::Parser::AST::QualifiedType^ Equivalent
{
CppSharp::Parser::AST::QualifiedType^ get();
void set(CppSharp::Parser::AST::QualifiedType^);
}
};
public ref class DecayedType : CppSharp::Parser::AST::Type
{
public:

93
src/CppParser/Bindings/CSharp/AST.cs

@ -998,6 +998,99 @@ namespace CppSharp @@ -998,6 +998,99 @@ namespace CppSharp
}
}
public unsafe partial class AttributedType : CppSharp.Parser.AST.Type, IDisposable, CppSharp.Runtime.ICppMarshal
{
[StructLayout(LayoutKind.Explicit, Size = 20)]
public new struct Internal
{
[FieldOffset(4)]
public CppSharp.Parser.AST.QualifiedType.Internal Modified;
[FieldOffset(12)]
public CppSharp.Parser.AST.QualifiedType.Internal Equivalent;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall,
EntryPoint="??0AttributedType@AST@CppParser@CppSharp@@QAE@ABU0123@@Z")]
internal static extern global::System.IntPtr AttributedType_1(global::System.IntPtr instance, global::System.IntPtr _0);
}
int CppSharp.Runtime.ICppMarshal.NativeDataSize
{
get { return 20; }
}
void CppSharp.Runtime.ICppMarshal.MarshalManagedToNative(global::System.IntPtr instance)
{
}
void CppSharp.Runtime.ICppMarshal.MarshalNativeToManaged(global::System.IntPtr instance)
{
}
internal AttributedType(AttributedType.Internal* native)
: this(new global::System.IntPtr(native))
{
}
internal AttributedType(AttributedType.Internal native)
: this(&native)
{
}
internal AttributedType(global::System.IntPtr native)
: base(native)
{
}
public AttributedType()
: this(IntPtr.Zero)
{
__Instance = Marshal.AllocHGlobal(20);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
public CppSharp.Parser.AST.QualifiedType Modified
{
get
{
var __ptr = (Internal*)__Instance.ToPointer();
var __copy = new global::System.IntPtr(&__ptr->Modified);
var __instance = Marshal.AllocHGlobal(8);
CppSharp.Runtime.Helpers.memcpy(__instance, new IntPtr(&__copy), new UIntPtr(8));
return new CppSharp.Parser.AST.QualifiedType(__instance);
}
set
{
var __ptr = (Internal*)__Instance.ToPointer();
__ptr->Modified = value == (CppSharp.Parser.AST.QualifiedType) null ? new CppSharp.Parser.AST.QualifiedType.Internal() : *(CppSharp.Parser.AST.QualifiedType.Internal*) (value.__Instance);
}
}
public CppSharp.Parser.AST.QualifiedType Equivalent
{
get
{
var __ptr = (Internal*)__Instance.ToPointer();
var __copy = new global::System.IntPtr(&__ptr->Equivalent);
var __instance = Marshal.AllocHGlobal(8);
CppSharp.Runtime.Helpers.memcpy(__instance, new IntPtr(&__copy), new UIntPtr(8));
return new CppSharp.Parser.AST.QualifiedType(__instance);
}
set
{
var __ptr = (Internal*)__Instance.ToPointer();
__ptr->Equivalent = value == (CppSharp.Parser.AST.QualifiedType) null ? new CppSharp.Parser.AST.QualifiedType.Internal() : *(CppSharp.Parser.AST.QualifiedType.Internal*) (value.__Instance);
}
}
}
public unsafe partial class DecayedType : CppSharp.Parser.AST.Type, IDisposable, CppSharp.Runtime.ICppMarshal
{
[StructLayout(LayoutKind.Explicit, Size = 28)]

19
src/CppParser/Parser.cpp

@ -1100,6 +1100,25 @@ Type* Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL, @@ -1100,6 +1100,25 @@ Type* Parser::WalkType(clang::QualType QualType, clang::TypeLoc* TL,
assert(Type && "Expected a valid type");
switch(Type->getTypeClass())
{
case clang::Type::Attributed:
{
auto Attributed = Type->getAs<clang::AttributedType>();
assert(Attributed && "Expected an attributed type");
TypeLoc Next;
if (TL && !TL->isNull()) Next = TL->getNextTypeLoc();
auto AT = new AttributedType();
auto Modified = Attributed->getModifiedType();
AT->Modified = GetQualifiedType(Modified, WalkType(Modified, &Next));
auto Equivalent = Attributed->getEquivalentType();
AT->Equivalent = GetQualifiedType(Equivalent, WalkType(Equivalent, &Next));
Ty = AT;
break;
}
case clang::Type::Builtin:
{
auto Builtin = Type->getAs<clang::BuiltinType>();

5
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -202,6 +202,11 @@ namespace CppSharp.Generators.CLI @@ -202,6 +202,11 @@ namespace CppSharp.Generators.CLI
return decl.Type.Visit(this);
}
public string VisitAttributedType(AttributedType attributed, TypeQualifiers quals)
{
return attributed.Modified.Visit(this);
}
public string VisitDecayedType(DecayedType decayed, TypeQualifiers quals)
{
return decayed.Decayed.Visit(this);

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

@ -263,6 +263,11 @@ namespace CppSharp.Generators.CSharp @@ -263,6 +263,11 @@ namespace CppSharp.Generators.CSharp
return decl.Type.Visit(this);
}
public CSharpTypePrinterResult VisitAttributedType(AttributedType attributed, TypeQualifiers quals)
{
return attributed.Modified.Visit(this);
}
public CSharpTypePrinterResult VisitDecayedType(DecayedType decayed, TypeQualifiers quals)
{
return decayed.Decayed.Visit(this);

5
src/Generator/Passes/CheckVirtualOverrideReturnCovariance.cs

@ -106,6 +106,11 @@ namespace CppSharp.Passes @@ -106,6 +106,11 @@ namespace CppSharp.Passes
return false;
}
public bool VisitAttributedType(AttributedType attributed, TypeQualifiers quals)
{
return false;
}
public bool VisitDecayedType(DecayedType decayed, TypeQualifiers quals)
{
return false;

5
src/Generator/Types/CppTypePrinter.cs

@ -120,6 +120,11 @@ namespace CppSharp.Types @@ -120,6 +120,11 @@ namespace CppSharp.Types
return GetDeclName(typedef.Declaration);
}
public string VisitAttributedType(AttributedType attributed, TypeQualifiers quals)
{
return attributed.Modified.Visit(this);
}
public string GetDeclName(Declaration declaration)
{
switch (PrintKind)

19
src/Parser/Parser.cpp

@ -1222,6 +1222,25 @@ CppSharp::AST::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc* @@ -1222,6 +1222,25 @@ CppSharp::AST::Type^ Parser::WalkType(clang::QualType QualType, clang::TypeLoc*
assert(Type && "Expected a valid type");
switch(Type->getTypeClass())
{
case Type::Attributed:
{
auto Attributed = Type->getAs<clang::AttributedType>();
assert(Attributed && "Expected an attributed type");
TypeLoc Next;
if (TL && !TL->isNull()) Next = TL->getNextTypeLoc();
auto AT = gcnew CppSharp::AST::AttributedType();
auto Modified = Attributed->getModifiedType();
AT->Modified = GetQualifiedType(Modified, WalkType(Modified, &Next));
auto Equivalent = Attributed->getEquivalentType();
AT->Equivalent = GetQualifiedType(Equivalent, WalkType(Equivalent, &Next));
Ty = AT;
break;
}
case Type::Builtin:
{
auto Builtin = Type->getAs<clang::BuiltinType>();

8
tests/Basic/Basic.Tests.cs

@ -141,5 +141,13 @@ public class BasicTests @@ -141,5 +141,13 @@ public class BasicTests
Assert.AreEqual(300, new Bar2.Nested());
Assert.AreEqual(500, new Bar2());
}
[Test]
public void TestTypes()
{
// Attributed types
var sum = Types.AttributedSum(3, 4);
Assert.That(sum, Is.EqualTo(7));
}
}

6
tests/Basic/Basic.cpp

@ -208,3 +208,9 @@ int test(basic& s) @@ -208,3 +208,9 @@ int test(basic& s)
{
return 5;
}
int Types::AttributedSum(int A, int B)
{
return A + B;
}

15
tests/Basic/Basic.h

@ -185,3 +185,18 @@ class DLL_API basic @@ -185,3 +185,18 @@ class DLL_API basic
};
DLL_API int test(basic& s);
// Tests for C++ types
struct DLL_API Types
{
// AttributedType
#ifdef __clang__
#define ATTR __attribute__((stdcall))
#else
#define ATTR
#endif
typedef int AttributedFuncType(int, int) ATTR;
static AttributedFuncType AttributedSum;
};

Loading…
Cancel
Save