Repository navigation
Expand file tree
/
Copy pathMember.cpp
More file actions
149 lines (137 loc) · 4.12 KB
/
Copy pathMember.cpp
File metadata and controls
149 lines (137 loc) · 4.12 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
#include "IR.h"
#include "TSEmitter.h"
namespace metagen {
std::string MemberDecl::toString() {
TSFile file;
MemberDecl &decl = *this;
file.write(decl, false);
return file.toString();
}
void TSFile::write(MemberDecl &decl, bool isInterface,
std::vector<std::string> *classTypeParameters) {
if (decl.kind == kMemberMethod) {
std::string line;
if (decl.isStatic) {
line += "static ";
}
line += decl.name;
bool staticReturnsThis =
decl.isStatic && decl.returnType.kind == kTypeInstanceObject;
if (staticReturnsThis) {
line += "<";
if (classTypeParameters != nullptr && !classTypeParameters->empty()) {
for (const std::string ¶m : *classTypeParameters) {
line += param + ", ";
}
}
line += "This extends abstract new (...args: any) => any>";
} else if (decl.isStatic && classTypeParameters != nullptr &&
!classTypeParameters->empty()) {
line += "<";
for (size_t i = 0; i < classTypeParameters->size(); i++) {
line += classTypeParameters->at(i);
if (i < classTypeParameters->size() - 1) {
line += ", ";
}
}
line += ">";
}
if (isInterface && decl.optional) {
line += "?";
}
line += "(";
if (staticReturnsThis) {
line += "this: This";
if (!decl.parameters.empty()) {
line += ", ";
}
}
std::unordered_set<std::string> paramNames;
for (size_t i = 0; i < decl.parameters.size(); i++) {
if (i > 0) {
line += ", ";
}
auto param = decl.parameters[i];
if (paramNames.contains(param.name)) {
param.name += "_";
}
paramNames.emplace(param.name);
line += param.name;
line += ": ";
line += typeToString(param.type, decl.isStatic, false);
}
line += "): ";
line += typeToString(decl.returnType, decl.isStatic, true);
line += ";";
if (decl.tsIgnore) {
// Due to inconsistencies between Objective-C and TypeScript type
// systems, we have to use this annotation to prevent the TypeScript
// compiler from complaining about how one type doesn't satisfy another.
code.write("// @ts-ignore MemberDecl.tsIgnore");
}
code.write(line);
} else if (decl.kind == kMemberProperty) {
auto getterType = typeToString(decl.propertyType, decl.isStatic, true);
auto setterType = typeToString(decl.propertyType, decl.isStatic, false);
if (!decl.isReadonly && getterType != setterType) {
if (decl.optional && isInterface) {
getterType += " | undefined";
setterType += " | undefined";
}
std::string line;
if (decl.isStatic) {
line += "static ";
}
line += "get ";
line += decl.name;
line += "(): ";
line += decl.propertyType.kind == kTypeInstanceObject && decl.isStatic
? decl.parentClassName
: getterType;
line += ";";
if (decl.tsIgnore) {
code.write("// @ts-ignore MemberDecl.tsIgnore");
}
code.write(line);
line = "";
if (decl.isStatic) {
line += "static ";
}
line += "set ";
line += decl.name;
line += "(value: ";
line += decl.propertyType.kind == kTypeInstanceObject && decl.isStatic
? decl.parentClassName
: setterType;
line += ");";
if (decl.tsIgnore) {
code.write("// @ts-ignore MemberDecl.tsIgnore");
}
code.write(line);
} else {
std::string line;
if (decl.isStatic) {
line += "static ";
}
if (decl.isReadonly) {
line += "readonly ";
}
line += decl.name;
if (isInterface && decl.optional) {
line += "?";
}
line += ": ";
line += decl.propertyType.kind == kTypeInstanceObject && decl.isStatic
? decl.parentClassName
: getterType;
line += ";";
if (decl.tsIgnore) {
code.write("// @ts-ignore MemberDecl.tsIgnore");
}
code.write(line);
}
} else {
assert(false && "Unknown class member type");
}
}
} // namespace metagen