-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIR.h
More file actions
393 lines (291 loc) · 8.21 KB
/
Copy pathIR.h
File metadata and controls
393 lines (291 loc) · 8.21 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "Metadata.h"
#include "Util.h"
#include "clang-c/Index.h"
namespace metagen {
enum TypeSpecKind {
kTypeUnknown,
kTypeVoid,
kTypeU8,
kTypeU16,
kTypeU32,
kTypeU64,
kTypeI8,
kTypeI16,
kTypeI32,
kTypeI64,
kTypeF32,
kTypeF64,
kTypeU128,
kTypeI128,
kTypeBool,
kTypeString,
kTypeSelector,
kTypePointer,
kTypeCallback,
kTypeObject,
kTypeInstanceObject,
kTypeAnyObject,
kTypeEnum,
kTypeRecord,
kTypeConstArray,
kTypeIncompleteArray,
kTypeVector,
kTypeExtVector,
kTypeComplex,
kTypeFunctionPointer,
kTypeF16,
};
class TypeSpec {
public:
TypeSpec() {}
TypeSpec(CXType type,
std::vector<std::string>* classTypeParameters = nullptr);
TypeSpecKind kind;
// kPointerType
std::shared_ptr<TypeSpec> pointee;
// kTypeCallback, kTypeFunctionPointer
std::vector<TypeSpec> callbackArgs;
std::shared_ptr<TypeSpec> callbackReturn;
bool isVariadic = false;
// kTypeObject, kTypeInstanceObject, kTypeAnyObject
std::string className;
std::string protocolName;
bool isNullable = false;
std::string typeParameterName;
std::vector<TypeSpec> typeParameters;
// kTypeEnum
std::string enumName;
// kTypeRecord
std::string recordName; // struct or union
// kTypeConstArray, kTypeIncompleteArray
std::shared_ptr<TypeSpec> arrayElement;
// kTypeConstArray
size_t constArraySize;
// kTypeUnknown
std::string unknownInfo;
};
enum VariableConstEvalKind {
kEvalNone,
kEvalI64,
kEvalF64,
kEvalString,
};
struct EnumConstDecl {
std::string name;
int64_t value;
};
class VariableDecl {
public:
VariableDecl() {}
VariableDecl(CXCursor cursor);
VariableDecl(std::string& framework, const EnumConstDecl& decl);
std::string framework;
std::string name;
TypeSpec type;
VariableConstEvalKind constEvalKind = kEvalNone;
int64_t constEvalI64;
double constEvalF64;
std::string constEvalString;
};
class EnumDecl {
public:
EnumDecl() {}
EnumDecl(CXCursor cursor);
std::string framework;
std::string name;
std::vector<EnumConstDecl> constants;
MDSectionOffset mdOffset = MD_SECTION_OFFSET_NULL;
};
struct StructFieldDecl {
std::string name;
uint16_t offset;
TypeSpec type;
};
class StructDecl {
public:
StructDecl() {}
StructDecl(CXCursor cursor);
std::string framework;
std::string name;
uint16_t size;
std::vector<StructFieldDecl> fields;
MDSectionOffset mdOffset = MD_SECTION_OFFSET_NULL;
};
struct UnionFieldDecl {
std::string name;
TypeSpec type;
};
class UnionDecl {
public:
UnionDecl() {}
UnionDecl(CXCursor cursor);
std::string framework;
std::string name;
uint16_t size;
std::vector<UnionFieldDecl> fields;
MDSectionOffset mdOffset = MD_SECTION_OFFSET_NULL;
};
struct ParameterDecl {
std::string name;
TypeSpec type;
};
class FunctionDecl {
public:
FunctionDecl() {}
FunctionDecl(CXCursor cursor);
std::string framework;
std::string name;
TypeSpec returnType;
std::vector<ParameterDecl> parameters;
bool isVariadic;
};
enum MemberKind {
kMemberProperty,
kMemberMethod,
};
class MemberDecl {
public:
MemberDecl() {}
MemberDecl(CXCursor cursor,
std::vector<std::string>* classTypeParameters = nullptr);
std::string toString();
// Common
MemberKind kind;
std::string name;
std::string parentClassName;
std::string parentProtocolName;
bool isStatic;
// Method
TypeSpec returnType;
std::vector<ParameterDecl> parameters;
std::string methodSelector;
bool isVariadic;
bool isInit = false;
bool returnOwned = false;
// Property
TypeSpec propertyType;
bool isReadonly;
std::string setterName;
std::string getterSelector;
std::string setterSelector;
// For protocols
bool optional;
bool tsIgnore = false;
// Method overloads for TS emission (used to preserve superclass signatures).
std::vector<MemberDecl> overloads;
std::vector<std::string> overloadSignatureKeys;
void addOverloadFrom(const MemberDecl& member);
};
void removeDuplicateMethods(std::vector<MemberDecl>& members);
class ProtocolDecl;
class ClassDecl {
public:
ClassDecl() {}
ClassDecl(CXCursor cursor);
MemberDecl* getMemberNamed(const std::string& name);
void postProcessMembers();
std::string framework;
std::string name;
std::string runtimeName;
std::string superClassName;
std::vector<std::string> protocolNames;
std::vector<std::string> typeParameters;
std::vector<MemberDecl> members;
ClassDecl* superClassRef = nullptr;
std::vector<ClassDecl*> derivedClassRefs;
std::vector<ProtocolDecl*> protocolRefs;
std::unordered_set<std::string> implementedProtocolNames;
bool tsIgnore = false;
MDSectionOffset mdOffset = MD_SECTION_OFFSET_NULL;
};
class ProtocolDecl {
public:
ProtocolDecl() {}
ProtocolDecl(CXCursor cursor);
MemberDecl* getMemberNamed(const std::string& name);
void postProcessMembers();
std::string framework;
std::string name;
std::vector<std::string> protocolNames;
std::vector<MemberDecl> members;
std::vector<ProtocolDecl*> protocolRefs;
std::vector<ProtocolDecl*> derivedProtocolRefs;
std::vector<ClassDecl*> implementerRefs;
bool tsIgnore = false;
MDSectionOffset mdOffset = MD_SECTION_OFFSET_NULL;
};
void convertMethodToPropertyIfNeeded(MemberDecl& member,
MemberDecl* memberInSuperclass,
bool isSuperclass);
class CategoryDecl {
public:
CategoryDecl() {}
CategoryDecl(CXCursor cursor);
void processMembers(std::vector<std::string>* classTypeParameters = nullptr);
CXCursor cursor;
std::string framework;
std::string name;
std::string className;
std::vector<MemberDecl> members;
std::vector<std::string>* _classTypeParameters;
};
class MetadataFactory {
public:
MetadataFactory() {}
void process(CXCursor cursor, bool checkAvailability = false);
void resolveRefs();
void postProcess();
void implementClassProtocols(ClassDecl& decl,
std::vector<std::string>& protocols);
bool shouldProcess(CXCursor cursor, bool required = false);
void processVariable(CXCursor cursor);
void processEnum(CXCursor cursor, bool required = false);
void processStruct(CXCursor cursor, bool required = false);
void processUnion(CXCursor cursor, bool required = false);
void processFunction(CXCursor cursor);
void processClass(CXCursor cursor, bool required = false);
void processProtocol(CXCursor cursor, bool required = false);
void processCategory(CXCursor cursor);
void processType(TypeSpec& type);
void postProcessStruct(StructDecl& decl);
void postProcessUnion(UnionDecl& decl);
void postProcessFunction(FunctionDecl& decl);
void postProcessMember(MemberDecl& decl);
void postProcessClass(ClassDecl& decl);
void postProcessProtocol(ProtocolDecl& decl);
void postProcessCategory(CategoryDecl& decl);
void processEnumRefs();
void processRecordRefs();
void processClassRefs();
void processProtocolRefs();
std::unordered_set<std::string> includePaths;
std::unordered_map<std::string, VariableDecl> variables;
std::unordered_map<std::string, EnumDecl> enums;
std::unordered_map<std::string, StructDecl> structs;
std::unordered_map<std::string, UnionDecl> unions;
std::vector<FunctionDecl> functions;
std::unordered_map<std::string, ClassDecl> classes;
std::unordered_map<std::string, ProtocolDecl> protocols;
std::vector<CategoryDecl> categories;
std::unordered_set<std::string> referencedEnums;
std::unordered_set<std::string> referencedRecords;
std::unordered_set<std::string> referencedClasses;
std::unordered_set<std::string> referencedProtocols;
std::unordered_set<std::string> renamedProtocols;
std::unordered_set<std::string> missingClasses;
private:
bool _checkAvailability = false;
std::unordered_map<std::string, bool> shouldProcessCache;
std::unordered_map<std::string, EnumDecl> skippedEnums;
std::unordered_map<std::string, StructDecl> skippedStructs;
std::unordered_map<std::string, UnionDecl> skippedUnions;
std::unordered_map<std::string, ClassDecl> skippedClasses;
std::unordered_map<std::string, ProtocolDecl> skippedProtocols;
};
} // namespace metagen