This repository was archived by the owner on Jul 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTypeDefVisitor.cs
124 lines (103 loc) · 5.02 KB
/
TypeDefVisitor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
namespace ClangSharpPInvokeGenerator
{
using System;
using System.IO;
using System.Collections.Generic;
using ClangSharp;
internal sealed class TypeDefVisitor : ICXCursorVisitor
{
private readonly TextWriter tw;
private readonly HashSet<string> visitedTypeDefs = new HashSet<string>();
public TypeDefVisitor(TextWriter tw)
{
this.tw = tw;
}
public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data)
{
if (cursor.IsInSystemHeader())
{
return CXChildVisitResult.CXChildVisit_Continue;
}
CXCursorKind curKind = clang.getCursorKind(cursor);
if (curKind == CXCursorKind.CXCursor_TypedefDecl)
{
var spelling = clang.getCursorSpelling(cursor).ToString();
if (this.visitedTypeDefs.Contains(spelling))
{
return CXChildVisitResult.CXChildVisit_Continue;
}
this.visitedTypeDefs.Add(spelling);
CXType type = clang.getCanonicalType(clang.getTypedefDeclUnderlyingType(cursor));
// we handle enums and records in struct and enum visitors with forward declarations also
if (type.kind == CXTypeKind.CXType_Record || type.kind == CXTypeKind.CXType_Enum)
{
return CXChildVisitResult.CXChildVisit_Continue;
}
// no idea what this is? -- template stuff?
if (type.kind == CXTypeKind.CXType_Unexposed)
{
var canonical = clang.getCanonicalType(type);
if (canonical.kind == CXTypeKind.CXType_Unexposed)
{
return CXChildVisitResult.CXChildVisit_Continue;
}
}
if (type.kind == CXTypeKind.CXType_Pointer)
{
var pointee = clang.getPointeeType(type);
if (pointee.kind == CXTypeKind.CXType_Record || pointee.kind == CXTypeKind.CXType_Void)
{
this.tw.WriteLine(" public partial struct " + spelling);
this.tw.WriteLine(" {");
this.tw.WriteLine(" public " + spelling + "(IntPtr pointer)");
this.tw.WriteLine(" {");
this.tw.WriteLine(" this.Pointer = pointer;");
this.tw.WriteLine(" }");
this.tw.WriteLine();
this.tw.WriteLine(" public IntPtr Pointer;");
this.tw.WriteLine(" }");
this.tw.WriteLine();
return CXChildVisitResult.CXChildVisit_Continue;
}
if (pointee.kind == CXTypeKind.CXType_FunctionProto)
{
this.tw.WriteLine(" [UnmanagedFunctionPointer(" + pointee.CallingConventionSpelling() + ")]");
this.tw.Write(" public delegate ");
Extensions.ReturnTypeHelper(clang.getResultType(pointee), tw);
this.tw.Write(" ");
this.tw.Write(spelling);
this.tw.Write("(");
uint argumentCounter = 0;
clang.visitChildren(cursor, delegate(CXCursor cxCursor, CXCursor parent1, IntPtr ptr)
{
if (cxCursor.kind == CXCursorKind.CXCursor_ParmDecl)
{
Extensions.ArgumentHelper(pointee, cxCursor, tw, argumentCounter++);
}
return CXChildVisitResult.CXChildVisit_Continue;
}, new CXClientData(IntPtr.Zero));
this.tw.WriteLine(");");
this.tw.WriteLine();
return CXChildVisitResult.CXChildVisit_Continue;
}
}
if (clang.isPODType(type) != 0)
{
var podType = type.ToPlainTypeString();
this.tw.WriteLine(" public partial struct " + spelling);
this.tw.WriteLine(" {");
this.tw.WriteLine(" public " + spelling + "(" + podType + " value)");
this.tw.WriteLine(" {");
this.tw.WriteLine(" this.Value = value;");
this.tw.WriteLine(" }");
this.tw.WriteLine();
this.tw.WriteLine(" public " + type.ToPlainTypeString() + " Value;");
this.tw.WriteLine(" }");
this.tw.WriteLine();
}
return CXChildVisitResult.CXChildVisit_Continue;
}
return CXChildVisitResult.CXChildVisit_Recurse;
}
}
}