-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp.py
332 lines (250 loc) · 9.62 KB
/
cpp.py
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
from tree_sitter import Node
from setup import PATH_BY_UNIT
from visitor import visitor
from utils import only
_PRELUDE: str = """
module;
#include <type_traits>
#include <{0}>
export module {1};
#define REGULAR_ENUM(ty) \\
constexpr bool operator ==(std::underlying_type_t<ty> a, ty b) noexcept \\
{{ \\
return a == static_cast<std::underlying_type_t<ty>>(b); \\
}} \\
constexpr bool operator ==(ty a, std::underlying_type_t<ty> b) noexcept \\
{{ \\
return static_cast<std::underlying_type_t<ty>>(a) == b; \\
}}
#define BITFLAG_ENUM(ty) \\
constexpr ty operator|(ty a, ty b) noexcept \\
{{ \\
return static_cast<ty>(static_cast<std::underlying_type_t<ty>>(a) | static_cast<std::underlying_type_t<ty>>(b)); \\
}} \\
constexpr ty operator&(ty a, ty b) noexcept \\
{{ \\
return static_cast<ty>(static_cast<std::underlying_type_t<ty>>(a) & static_cast<std::underlying_type_t<ty>>(b)); \\
}} \\
constexpr ty operator^(ty a, ty b) noexcept \\
{{ \\
return static_cast<ty>(static_cast<std::underlying_type_t<ty>>(a) ^ static_cast<std::underlying_type_t<ty>>(b)); \\
}} \\
constexpr ty operator~(ty a) noexcept \\
{{ \\
return static_cast<ty>(~static_cast<std::underlying_type_t<ty>>(a)); \\
}} \\
constexpr ty& operator|=(ty& a, ty b) noexcept \\
{{ \\
return a = a | b; \\
}} \\
constexpr ty& operator&=(ty& a, ty b) noexcept \\
{{ \\
return a = a & b; \\
}} \\
constexpr ty& operator^=(ty& a, ty b) noexcept \\
{{ \\
return a = a ^ b; \\
}}
export namespace {2}
{{
"""
def _cut_similarity(model: str, target: str) -> str:
mi, ti = 0, 0
f = len(model)
last = 0
while mi < f:
if target[ti] == "_":
ti += 1
last = ti
continue
elif model[mi].upper() == target[ti]:
ti += 1
else:
break
mi += 1
if target[ti] == "_":
ti += 1
else:
ti = last
if target[ti].isnumeric():
ti -= 1
# elif mi == f:
# ti = last
return target[ti:]
@visitor
class CppVisitor:
def __init__(
self,
unit: str,
*,
module: str = "sdl.{ext}",
namespace: str = "sdl::{ext}",
) -> None:
"""
Generate a C++ module from the parsed SDL header file.
Args:
unit (str): The SDL unit to generate the module for.
module (str, optional): The module name to use. Defaults to "sdl.{ext}".
namespace (str, optional): The namespace to use. Defaults to "sdl::{ext}".
"""
self._enum = set()
mod = module.format(ext=unit)
ns = "sdl" if unit == "SDL" else namespace.format(ext=unit)
if unit != "SDL":
unit = f"SDL_{unit}"
header = PATH_BY_UNIT[unit].split("/")[-1][:-2] # remove ".h"
self._file = open(f"out/cpp/{header}.g.cppm", "w")
self._file.write(_PRELUDE.format(PATH_BY_UNIT[unit], mod, ns))
def __del__(self) -> None:
self._file.write("}\n")
self._file.close()
def start_platform_code(self, platforms: list[str]):
self._file.write(
f"#if {' || '.join(map(lambda p: f'defined({p})', platforms))}\n"
)
def end_platform_code(self):
self._file.write("#endif\n\n")
def visit_function(self, rules: dict[str, Node | list[Node]]):
name = rules["function.name"]
ret = rules["function.return"].text.decode()
params = rules["function.params"]
if "function.return_ptr" in rules:
ret += "*"
if any(only("type_qualifier", rules["function.decl"])):
ret = "const " + ret
body_ret = "" if ret == "void" else "return "
def extract_type(node: Node) -> str:
if node.text == b"void":
return "void"
ty = node.child_by_field_name("type").text.decode()
decl = node.child_by_field_name("declarator")
if ty[4:] in self._enum:
ty = ty[4:]
if node.named_children[0].type == "type_qualifier":
ty = node.named_children[0].text.decode() + " " + ty
while decl is not None and decl.type == "pointer_declarator":
decl = decl.child_by_field_name("declarator")
ty += "*"
return ty
def extract_name(node: Node) -> str:
decl = node.child_by_field_name("declarator")
if decl is None:
return ""
while not decl.type.endswith("identifier"):
match decl.type:
case "pointer_declarator":
decl = decl.child_by_field_name("declarator")
case "type_qualifier":
decl = decl.child_by_field_name("declarator")
case "function_declarator":
# function_declarator > parenthesized_declarator > pointer_declarator > identifier
decl = decl.child_by_field_name("declarator")
decl = decl.named_children[0]
decl = decl.child_by_field_name("declarator")
case "array_declarator":
decl = decl.child_by_field_name("declarator")
case _:
break
return decl.text.decode()
def cast_if_enum(ty: str, name: str) -> str:
if ty.startswith("const "):
ty = ty[6:]
# TODO: take care of `const Enum *` case
if ty[-1] == "*":
ty_tmp = ty[:-1]
if ty_tmp in self._enum:
return f"(SDL_{ty_tmp}*)({name})"
if ty in self._enum:
return f"(SDL_{ty})({name})"
return name
ps = list(only("parameter_declaration", params))
ps_types = [extract_type(p) for p in ps]
ps_name = [extract_name(p) for p in ps]
for i, n in enumerate(ps_name):
if n == "" and ps_types[i] != "void":
print(f"Note: Skipping {name.text.decode()} due to unnamed parameter")
return
self._file.write(f"\n {ret} {name.text[4:].decode()}(")
self._file.write(", ".join(f"{ty} {nm}" for ty, nm in zip(ps_types, ps_name)))
self._file.write(f""")
{{
{body_ret}{name.text.decode()}({", ".join(cast_if_enum(t, n) for t, n in zip(ps_types, ps_name))});
}}
""")
def visit_enum(self, rules: dict[str, Node | list[Node]]):
name = rules["enum.name"]
entries = rules["enum.entries"]
self._file.write(f"""
enum class {name.text[4:].decode()}
{{
""")
for entry in only("enumerator", entries):
entry_name = entry.child_by_field_name("name")
clean_name = _cut_similarity(
name.text[4:].decode(),
entry_name.text[4:].decode(),
)
self._file.write(f" {clean_name} = {entry_name.text.decode()},\n")
self._file.write(f" }};\n REGULAR_ENUM({name.text[4:].decode()});\n")
self._enum.add(name.text[4:].decode())
pass
def visit_opaque(self, rules: dict[str, Node | list[Node]]):
name = rules["opaque.name"]
al = name.text[4:].decode() if name.text[4:] == b"_" else name.text.decode()
self._file.write(f"\n using {al} = {name.text.decode()};\n")
pass
def visit_struct(self, rules: dict[str, Node | list[Node]]):
name = rules["struct.name"]
self._file.write(
f"\n using {name.text[4:].decode()} = {name.text.decode()};\n"
)
pass
def visit_union(self, rules: dict[str, Node | list[Node]]):
name = rules["union.name"]
self._file.write(
f"\n using {name.text[4:].decode()} = {name.text.decode()};\n"
)
pass
def visit_bitflag(self, rules: dict[str, Node | list[Node]]):
name = rules["bitflag.name"]
ty = rules["bitflag.type"]
flags = rules["flag"]
self._enum.add(name.text[4:].decode())
name = name.text[4:].decode()
self._file.write(f"""
enum class {name} : {ty.text.decode()}
{{
""")
for entry in filter(lambda x: x.type == "preproc_def", flags):
entry_name = entry.child_by_field_name("name")
clean_name = _cut_similarity(
name,
entry_name.text[4:].decode(),
)
self._file.write(f" {clean_name} = {entry_name.text.decode()},\n")
self._file.write(f" }};\n BITFLAG_ENUM({name});\n")
pass
def visit_alias(self, rules: dict[str, Node | list[Node]]):
name = rules["alias.name"]
ty = rules["alias.type"]
self._file.write(
f"\n using {name.text[4:].decode()} = {ty.text.decode()};\n"
)
def visit_callback(self, rules: dict[str, Node | list[Node]]):
pass
def visit_fn_macro(self, rules: dict[str, Node | list[Node]]):
pass
def visit_const(self, rules: dict[str, Node | list[Node]]):
name = rules["const.name"].text
value = rules["const.value"].text
# these are macros that alias to other functions, we don't need them
# so just skip them
if any(c for c in name if c >= 97 and c <= 122):
return
# these values are supposed to be private or C-specific (eg. __FILE__ and __LINE__)
if value.find(b"__") != -1:
return
self._file.write(
f"\n constexpr auto {name[4:].decode()}() {{ return {value.decode()}; }}\n"
)
pass