This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_header_codegen.cc
More file actions
342 lines (301 loc) · 10.1 KB
/
Copy pathcpp_header_codegen.cc
File metadata and controls
342 lines (301 loc) · 10.1 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
#include <vector>
#include <string>
#include <cassert>
#include "ecsact/runtime/meta.hh"
#include "ecsact/codegen/plugin.h"
#include "ecsact/codegen/plugin.hh"
#include "ecsact/lang-support/lang-cc.hh"
#include "ecsact/cpp_codegen_plugin_util.hh"
constexpr auto GENERATED_FILE_DISCLAIMER = R"(// GENERATED FILE - DO NOT EDIT
)";
template<typename T>
static void write_constexpr_id(
ecsact::codegen_plugin_context& ctx,
const char* id_type_name,
T id,
std::string_view indentation
) {
ctx.writef(
"{}static constexpr auto id = static_cast<{}>({});\n",
indentation,
id_type_name,
static_cast<int32_t>(id)
);
}
template<typename T>
static std::vector<ecsact_field_id> get_field_ids(T id) {
ecsact_composite_id compo_id;
if constexpr(std::is_same_v<ecsact_composite_id, T>) {
compo_id = id;
} else {
compo_id = ecsact_id_cast<ecsact_composite_id>(id);
}
std::vector<ecsact_field_id> field_ids;
field_ids.resize(ecsact_meta_count_fields(compo_id));
ecsact_meta_get_field_ids(
compo_id,
static_cast<int32_t>(field_ids.size()),
field_ids.data(),
nullptr
);
return field_ids;
}
static bool has_parent_system(ecsact_system_id id) {
auto parent_id = ecsact_meta_get_parent_system_id(id);
return parent_id != (ecsact_system_like_id)-1;
}
static auto cpp_field_type_name(ecsact_field_type field_type) -> std::string {
using ecsact::cc_lang_support::cpp_type_str;
switch(field_type.kind) {
case ECSACT_TYPE_KIND_BUILTIN:
return cpp_type_str(field_type.type.builtin);
case ECSACT_TYPE_KIND_ENUM:
return ecsact_meta_enum_name(field_type.type.enum_id);
case ECSACT_TYPE_KIND_FIELD_INDEX: {
auto field_index_field_type = ecsact_meta_field_type(
field_type.type.field_index.composite_id,
field_type.type.field_index.field_id
);
return cpp_field_type_name(field_index_field_type);
}
}
}
static void write_fields(
ecsact::codegen_plugin_context& ctx,
ecsact_composite_id compo_id,
std::predicate<ecsact_field_type, const char*> auto&& filter,
std::string_view indentation
) {
using ecsact::cc_lang_support::cpp_identifier;
using ecsact::cc_lang_support::cpp_type_str;
using namespace std::string_literals;
for(auto field_id : get_field_ids(compo_id)) {
auto field_type = ecsact_meta_field_type(compo_id, field_id);
auto field_name = ecsact_meta_field_name(compo_id, field_id);
if(!filter(field_type, field_name)) {
continue;
}
ctx.writef(
"{}{} {}",
indentation,
cpp_field_type_name(field_type),
field_name
);
if(field_type.length > 1) {
ctx.writef("[{}]", field_type.length);
}
ctx.writef(";\n");
}
}
static void for_each_field(
ecsact_composite_id compo_id,
std::invocable<ecsact_field_type, const char*> auto&& fn
) {
using ecsact::cc_lang_support::cpp_identifier;
using ecsact::cc_lang_support::cpp_type_str;
using namespace std::string_literals;
for(auto field_id : get_field_ids(compo_id)) {
auto field_type = ecsact_meta_field_type(compo_id, field_id);
auto field_name = ecsact_meta_field_name(compo_id, field_id);
fn(field_type, field_name);
}
}
static auto write_operators(
ecsact::codegen_plugin_context& ctx,
ecsact_composite_id compo_id,
std::string_view indentation
) -> void {
using ecsact::cc_lang_support::cpp_identifier;
using ecsact::cc_lang_support::cpp_type_str;
using namespace std::string_literals;
auto full_name =
ecsact_meta_decl_full_name(ecsact_id_cast<ecsact_decl_id>(compo_id));
ctx.writef(
"{}auto operator<=>(const {}&) const = default;\n",
indentation,
cpp_identifier(full_name)
);
}
static void write_system_impl_decl(
ecsact::codegen_plugin_context& ctx,
std::string_view indentation
) {
ctx.writef("{}struct context;\n", indentation);
ctx.writef("{}static void impl(context&);\n", indentation);
}
static void write_system_struct(
ecsact::codegen_plugin_context& ctx,
ecsact_system_id sys_id,
std::string indentation
) {
using namespace std::string_literals;
using ecsact::cc_lang_support::anonymous_system_name;
using ecsact::meta::get_child_system_ids;
std::string sys_name = ecsact_meta_system_name(sys_id);
if(!sys_name.empty()) {
ctx.writef("{}struct {} {{\n", indentation, sys_name);
write_constexpr_id(ctx, "ecsact_system_id", sys_id, indentation + "\t");
for(auto child_system_id : get_child_system_ids(sys_id)) {
write_system_struct(ctx, child_system_id, indentation + "\t");
}
write_system_impl_decl(ctx, indentation + "\t");
ctx.writef("{}}};\n", indentation);
} else {
ctx.writef("{}struct {} {{\n", indentation, anonymous_system_name(sys_id));
write_constexpr_id(ctx, "ecsact_system_id", sys_id, indentation + "\t");
ctx.writef("{}\tstruct context;\n", indentation);
ctx.writef("{}}};\n", indentation);
for(auto child_system_id : get_child_system_ids(sys_id)) {
write_system_struct(ctx, child_system_id, indentation);
}
}
}
template<typename CompositeID>
static auto has_assoc_fields(CompositeID compo_id) -> bool {
for(auto field_id : ecsact::meta::get_field_ids(compo_id)) {
auto field_type = ecsact::meta::get_field_type(compo_id, field_id);
if(field_type.kind == ECSACT_TYPE_KIND_BUILTIN) {
if(field_type.type.builtin == ECSACT_ENTITY_TYPE) {
return true;
}
}
if(field_type.kind == ECSACT_TYPE_KIND_FIELD_INDEX) {
return true;
}
}
return false;
}
template<typename CompositeID>
static auto write_indexed_fields_struct(
ecsact::codegen_plugin_context& ctx,
CompositeID compo_id
) -> void {
using ecsact::cpp_codegen_plugin_util::block;
using ecsact::cpp_codegen_plugin_util::method_printer;
ctx.writef("\n");
block(ctx, "struct IndexedFields", [&] {
write_fields(
ctx,
ecsact_id_cast<ecsact_composite_id>(compo_id),
[](ecsact_field_type type, const char*) -> bool {
return type.kind == ECSACT_TYPE_KIND_FIELD_INDEX;
},
"\t"
);
{
ctx.writef("constexpr static ");
auto _ = method_printer(ctx, "from_composite")
.parameter("const auto&", "composite")
.return_type("IndexedFields");
block(ctx, "return", [&] {
for_each_field(
ecsact_id_cast<ecsact_composite_id>(compo_id),
[&](ecsact_field_type type, const char* field_name) {
if(type.kind != ECSACT_TYPE_KIND_FIELD_INDEX) {
return;
}
ctx.writef("\t.{0} = composite.{0},\n", field_name);
}
);
});
ctx.writef(";\n");
}
});
ctx.writef(";\n");
}
void ecsact_codegen_plugin(
ecsact_package_id package_id,
ecsact_codegen_write_fn_t write_fn,
ecsact_codegen_report_fn_t report_fn
) {
using ecsact::cc_lang_support::cpp_identifier;
using namespace std::string_literals;
using ecsact::meta::get_action_ids;
using ecsact::meta::get_child_system_ids;
using ecsact::meta::get_component_ids;
using ecsact::meta::get_enum_ids;
using ecsact::meta::get_enum_values;
using ecsact::meta::get_system_ids;
using ecsact::meta::get_transient_ids;
ecsact::codegen_plugin_context ctx{package_id, 0, write_fn, report_fn};
ctx.writef(GENERATED_FILE_DISCLAIMER);
ctx.writef("#pragma once\n\n");
ctx.writef("#include <cstdint>\n");
ctx.writef("#include <compare>\n");
ctx.writef("#include \"ecsact/runtime/common.h\"\n");
ctx.writef("\n");
const auto namespace_str =
cpp_identifier(ecsact_meta_package_name(ctx.package_id));
ctx.writef("namespace {} {{\n\n", namespace_str);
for(auto enum_id : get_enum_ids(ctx.package_id)) {
ctx.writef("enum class {} {{", ecsact_meta_enum_name(enum_id));
++ctx.indentation;
ctx.writef("\n");
for(auto& enum_value : get_enum_values(enum_id)) {
ctx.writef("{} = {},\n", enum_value.name, enum_value.value);
}
ctx.writef("}};");
--ctx.indentation;
}
for(auto comp_id : get_component_ids(ctx.package_id)) {
auto compo_id = ecsact_id_cast<ecsact_composite_id>(comp_id);
ctx.writef("struct {} {{\n", ecsact_meta_component_name(comp_id));
ctx.writef("\tstatic constexpr bool transient = false;\n");
ctx.writef(
"\tstatic constexpr bool has_assoc_fields = {};\n",
has_assoc_fields(comp_id) ? "true" : "false"
);
write_constexpr_id(ctx, "ecsact_component_id", comp_id, "\t");
ctx.indentation += 1;
write_indexed_fields_struct(ctx, comp_id);
ctx.indentation -= 1;
ctx.writef("\n");
write_fields(ctx, compo_id, [](auto, auto) { return true; }, "\t"s);
write_operators(ctx, compo_id, "\t"s);
ctx.writef("}};\n");
}
for(auto comp_id : get_transient_ids(ctx.package_id)) {
auto compo_id = ecsact_id_cast<ecsact_composite_id>(comp_id);
ctx.writef("struct {} {{\n", ecsact_meta_transient_name(comp_id));
ctx.writef("\tstatic constexpr bool transient = true;\n");
ctx.writef(
"\tstatic constexpr bool has_assoc_fields = {};\n",
has_assoc_fields(comp_id) ? "true" : "false"
);
write_constexpr_id(ctx, "ecsact_transient_id", comp_id, "\t");
ctx.indentation += 1;
write_indexed_fields_struct(ctx, comp_id);
ctx.indentation -= 1;
ctx.writef("\n");
write_fields(ctx, compo_id, [](auto, auto) { return true; }, "\t"s);
write_operators(ctx, compo_id, "\t"s);
ctx.writef("}};\n");
}
for(auto action_id : get_action_ids(ctx.package_id)) {
auto compo_id = ecsact_id_cast<ecsact_composite_id>(action_id);
ctx.writef("struct {} {{\n", ecsact_meta_action_name(action_id));
ctx.writef(
"\tstatic constexpr bool has_assoc_fields = {};\n",
has_assoc_fields(action_id) ? "true" : "false"
);
write_constexpr_id(ctx, "ecsact_action_id", compo_id, "\t");
for(auto child_system_id : get_child_system_ids(action_id)) {
write_system_struct(ctx, child_system_id, "\t");
}
write_system_impl_decl(ctx, "\t");
ctx.indentation += 1;
write_indexed_fields_struct(ctx, compo_id);
ctx.indentation -= 1;
ctx.writef("\n");
write_fields(ctx, compo_id, [](auto, auto) { return true; }, "\t");
write_operators(ctx, compo_id, "\t"s);
ctx.writef("}};\n");
}
for(auto sys_id : get_system_ids(ctx.package_id)) {
if(has_parent_system(sys_id)) {
continue;
}
write_system_struct(ctx, sys_id, "");
}
ctx.writef("\n}}// namespace {}\n", namespace_str);
}