Browse Source

The FindTypedef method supports full type names now.

pull/237/head
Elias Holzer 12 years ago
parent
commit
c670148493
  1. 25
      src/AST/Namespace.cs
  2. 2
      src/Generator.Tests/AST/TestAST.cs
  3. 6
      tests/Native/AST.h

25
src/AST/Namespace.cs

@ -294,15 +294,30 @@ namespace CppSharp.AST
public TypedefDecl FindTypedef(string name, bool createDecl = false) public TypedefDecl FindTypedef(string name, bool createDecl = false)
{ {
var typedef = Typedefs.Find(e => e.Name.Equals(name)); var entries = name.Split(new string[] { "::" },
StringSplitOptions.RemoveEmptyEntries).ToList();
if (typedef == null && createDecl) if (entries.Count <= 1)
{ {
typedef = new TypedefDecl { Name = name, Namespace = this }; var typeDef = Typedefs.Find(e => e.Name.Equals(name));
Typedefs.Add(typedef);
if (typeDef == null && createDecl)
{
typeDef = new TypedefDecl() { Name = name, Namespace = this };
Typedefs.Add(typeDef);
}
return typeDef;
} }
return typedef; var typeDefName = entries[entries.Count - 1];
var namespaces = entries.Take(entries.Count - 1);
var @namespace = FindNamespace(namespaces);
if (@namespace == null)
return null;
return @namespace.FindTypedef(typeDefName, createDecl);
} }
public T FindType<T>(string name) where T : Declaration public T FindType<T>(string name) where T : Declaration

2
src/Generator.Tests/AST/TestAST.cs

@ -65,6 +65,8 @@ namespace CppSharp.Generator.Tests.AST
Assert.IsNotNull(@class, "Couldn't find Math::Complex class."); Assert.IsNotNull(@class, "Couldn't find Math::Complex class.");
var plusOperator = @class.FindOperator(CXXOperatorKind.Plus).FirstOrDefault(); var plusOperator = @class.FindOperator(CXXOperatorKind.Plus).FirstOrDefault();
Assert.IsNotNull(plusOperator, "Couldn't find operator+ in Math::Complex class."); Assert.IsNotNull(plusOperator, "Couldn't find operator+ in Math::Complex class.");
var typedef = AstContext.FindTypedef("Math::Single").FirstOrDefault();
Assert.IsNotNull(typedef);
} }
} }
} }

6
tests/Native/AST.h

@ -4,6 +4,7 @@ void TestParameterProperties(bool a, const short& b, int* c = nullptr) {};
// Tests various AST helper methods (like FindClass, FindOperator etc.) // Tests various AST helper methods (like FindClass, FindOperator etc.)
namespace Math namespace Math
{ {
// Tests FindClass("Math::Complex")
struct Complex { struct Complex {
Complex(double r, double i) : re(r), im(i) {} Complex(double r, double i) : re(r), im(i) {}
Complex operator+(Complex &other); Complex operator+(Complex &other);
@ -11,7 +12,10 @@ namespace Math
double re, im; double re, im;
}; };
// Operator overloaded using a member function // Tests FindTypedef("Math::Single")
typedef float Single;
// Tests FindOperator method
Complex Complex::operator+(Complex &other) { Complex Complex::operator+(Complex &other) {
return Complex(re + other.re, im + other.im); return Complex(re + other.re, im + other.im);
} }

Loading…
Cancel
Save