-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtype_writers.h
698 lines (618 loc) · 22.2 KB
/
type_writers.h
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#pragma once
#include "type_helpers.h"
#include "settings.h"
#include "metadata_filter.h"
namespace swiftwinrt
{
using namespace std::filesystem;
using namespace winmd::reader;
using namespace std::literals;
inline std::string_view remove_tick(std::string_view const& name)
{
return name.substr(0, name.rfind('`'));
}
struct writer;
std::string get_full_swift_type_name(writer const&, TypeDef const& type);
std::string get_full_swift_type_name(writer const&, const metadata_type* type);
std::string get_swift_module(std::string_view const& ns);
std::string get_swift_name(function_param const&);
param_category get_category(const metadata_type*, TypeDef*);
template <typename First, typename...Rest>
auto get_impl_name(First const& first, Rest const&... rest)
{
std::string result;
auto convert = [&](auto&& value)
{
for (auto&& c : value)
{
result += c == '.' ? '_' : c;
}
};
convert(first);
((result += '_', convert(rest)), ...);
return result;
}
namespace details
{
inline void append_type_prefix(std::string& result, TypeDef const& type)
{
if ((get_category(type) == category::delegate_type) && (type.TypeNamespace() != winrt_collections_namespace))
{
// All delegates except those in the 'Windows.Foundation.Collections' namespace get an 'I' appended to the
// front...
result.push_back('I');
}
}
inline void append_type_prefix(std::string& result, metadata_type const& type)
{
if (dynamic_cast<const delegate_type*>(&type) != nullptr && (type.swift_logical_namespace() != winrt_collections_namespace))
{
// All delegates except those in the 'Windows.Foundation.Collections' namespace get an 'I' appended to the
// front...
result.push_back('I');
}
}
template <bool IsGenericParam>
void append_mangled_name(std::string& result, std::string_view name)
{
result.reserve(result.length() + name.length());
for (auto ch : name)
{
if (ch == '.')
{
result += IsGenericParam ? "__C" : "_C";
}
else if (ch == '_')
{
result += IsGenericParam ? "__z" : "__";
}
else if (ch == '`')
{
result += '_';
}
else
{
result += ch;
}
}
}
inline bool is_generic(TypeDef const& type) noexcept
{
return distance(type.GenericParam()) != 0;
}
}
template <bool IsGenericParam>
inline std::string mangled_namespace(std::string_view typeNamespace)
{
if constexpr (IsGenericParam)
{
if (typeNamespace == winrt_collections_namespace)
{
return "__F";
}
}
std::string result;
details::append_mangled_name<IsGenericParam>(result, typeNamespace);
return result;
}
template <bool IsGenericParam>
inline std::string mangled_name(TypeDef const& type)
{
std::string result = "__x_ABI_C";
if (details::is_generic(type))
{
// Generic types don't have the namespace included in the mangled name
result += "__F";
}
else
{
result += mangled_namespace<IsGenericParam>(type.TypeNamespace());
result += IsGenericParam ? "__C" : "_C";
}
details::append_type_prefix(result, type);
details::append_mangled_name<IsGenericParam>(result, type.TypeName());
return result;
}
template <bool IsGenericParam>
inline std::string mangled_name(metadata_type const& type)
{
std::string result = "__x_ABI_C";
if (is_generic_def(type) || is_generic_inst(type))
{
// Generic types don't have the namespace included in the mangled name
result += "__F";
}
else
{
result += mangled_namespace<IsGenericParam>(type.swift_abi_namespace());
result += IsGenericParam ? "__C" : "_C";
}
details::append_type_prefix(result, type);
details::append_mangled_name<IsGenericParam>(result, type.swift_type_name());
return result;
}
struct writer : indented_writer_base<writer>
{
using writer_base<writer>::write;
std::string type_namespace;
std::string swift_module;
std::string support;
std::string c_mod;
bool abi_types{};
bool delegate_types{};
bool async_types{};
bool full_type_names{};
bool impl_names{};
bool mangled_names{};
bool writing_generic{};
std::set<std::string> depends;
std::set<const delegate_type*> implementableEventTypes;
std::vector<generic_param_vector> generic_param_stack;
swiftwinrt::include_only_used_filter filter;
swiftwinrt::metadata_cache const* cache;
struct generic_param_guard
{
explicit generic_param_guard(writer* arg = nullptr)
: owner(arg)
{}
~generic_param_guard()
{
if (owner)
{
owner->generic_param_stack.pop_back();
}
}
generic_param_guard(generic_param_guard&& other)
: owner(other.owner)
{
other.owner = nullptr;
}
generic_param_guard& operator=(generic_param_guard&& other)
{
owner = std::exchange(other.owner, nullptr);
return *this;
}
generic_param_guard& operator=(generic_param_guard const&) = delete;
writer* owner;
};
template<typename T>
struct member_value_guard
{
writer* const owner;
T writer::* const member;
T const previous;
explicit member_value_guard(writer* arg, T writer::* ptr, T value) :
owner(arg), member(ptr), previous(std::exchange(owner->*member, value))
{
}
~member_value_guard()
{
owner->*member = previous;
}
member_value_guard(member_value_guard const&) = delete;
member_value_guard& operator=(member_value_guard const&) = delete;
};
void add_depends(TypeDef const& type)
{
auto type_module = get_swift_module(type.TypeNamespace());
if (type_module != swift_module)
{
depends.insert(type_module);
}
}
void add_depends(metadata_type const& type)
{
auto type_module = get_swift_module(type.swift_logical_namespace());
if (type_module != swift_module && !type_module.empty())
{
depends.insert(type_module);
}
}
[[nodiscard]] auto push_generic_params(generic_inst const& signature)
{
generic_param_vector names = signature.generic_params();
generic_param_stack.push_back(std::move(names));
return generic_param_guard{ this };
}
[[nodiscard]] auto push_generic_params(interface_info const& info)
{
if (info.generic_params.empty())
{
return generic_param_guard{ nullptr };
}
generic_param_stack.push_back(info.generic_params);
return generic_param_guard{ this };
}
[[nodiscard]] auto push_abi_types(bool value)
{
return member_value_guard(this, &writer::abi_types, value);
}
[[nodiscard]] auto push_async_types(bool value)
{
return member_value_guard(this, &writer::async_types, value);
}
[[nodiscard]] auto push_delegate_types(bool value)
{
return member_value_guard(this, &writer::delegate_types, value);
}
[[nodiscard]] auto push_full_type_names(bool value)
{
return member_value_guard(this, &writer::full_type_names, value);
}
[[nodiscard]] auto push_impl_names(bool value)
{
return member_value_guard(this, &writer::impl_names, value);
}
[[nodiscard]] auto push_mangled_names(bool value)
{
return member_value_guard(this, &writer::mangled_names, value);
}
[[nodiscard]] auto push_mangled_names_if_needed(param_category forCategory)
{
if (!abi_types)
{
return push_mangled_names(false);
}
// generics, objects (interfaces, classes), structs and enums all
// need the mangled name. other fundamental types just need the cpp
// abi type
switch (forCategory) {
case param_category::generic_type:
case param_category::object_type:
case param_category::struct_type:
case param_category::enum_type:
return push_mangled_names(true);
default:
return push_mangled_names(false);
}
}
[[nodiscard]] auto push_writing_generic(bool value)
{
return member_value_guard(this, &writer::writing_generic, value);
}
private:
enum class declaration_stage
{
begin,
forward,
end,
};
// A set of already declared (or in the case of generics, defined) types
std::map<std::string_view, declaration_stage> m_declaredTypes;
public:
bool begin_declaration(std::string_view mangledName)
{
auto [itr, added] = m_declaredTypes.emplace(mangledName, declaration_stage::begin);
return added;
}
void end_declaration(std::string_view mangledName)
{
auto itr = m_declaredTypes.find(mangledName);
XLANG_ASSERT(itr != m_declaredTypes.end());
XLANG_ASSERT(itr->second != declaration_stage::end);
itr->second = declaration_stage::end;
}
bool should_forward_declare(std::string_view mangledName)
{
auto [itr, added] = m_declaredTypes.emplace(mangledName, declaration_stage::begin);
if (itr->second != declaration_stage::begin)
{
return false;
}
itr->second = declaration_stage::forward;
return true;
}
void write(indent value)
{
for (std::size_t i = 0; i < value.additional_indentation; ++i)
{
write(" ");
}
}
void write_value(int32_t value)
{
write_printf("%d", value);
}
void write_value(uint32_t value)
{
write_printf("%#0x", value);
}
void write_code(std::string_view const& value)
{
for (auto&& c : value)
{
if (c == '`')
{
return;
}
else
{
write(c);
}
}
}
void write(Constant const& value)
{
switch (value.Type())
{
case ConstantType::Int32:
write_value(value.ValueInt32());
break;
case ConstantType::UInt32:
write_value(value.ValueUInt32());
break;
default:
throw std::invalid_argument("Unexpected constant type");
}
}
void write(metadata_type const* type)
{
add_depends(*type);
if (auto gti = dynamic_cast<const generic_inst*>(type))
{
write(gti);
return;
}
if (is_generic_def(type))
{
auto name = type->swift_type_name();
auto guard{ push_writing_generic(true) };
auto typeName = mangled_names ? mangled_name<true>(*type) : std::string(remove_tick(name));
bool first = !mangled_names;
if (!generic_param_stack.empty())
{
write("%%", typeName, bind_each([&](writer& w, generic_param_type generic_param) {
if (first)
{
first = false;
}
else
{
w.write("_");
}
w.write(generic_param);
},
generic_param_stack.back()));
}
else
{
auto format = mangled_names || abi_types ? "%%" : "%<%>";
auto seperator = mangled_names || abi_types ? "_" : ",";
auto generic_type = dynamic_cast<const typedef_base*>(type);
write(format, typeName, bind_each([&](writer& w, GenericParam generic_param) {
if (first)
{
first = false;
}
else
{
w.write(seperator);
}
w.write(generic_param.Name());
},
generic_type->type().GenericParam()));
}
return;
}
if (auto mapped = dynamic_cast<mapped_type const*>(type))
{
// mapped types are defined in headers and *not* metadata files, so these don't follow the same
// naming conventions that other types do. We just grab the type name and will use that.
auto swiftTypeName = mapped->swift_type_name();
if (swiftTypeName == "HResult")
{
write("HRESULT");
}
else
{
write(swiftTypeName);
}
}
else if (mangled_names)
{
auto classType = dynamic_cast<const class_type*>(type);
if (classType && !writing_generic)
{
// not writing a generic, write the default interface instead. class names in a generic
// use the mangled name of the class type
write(classType->default_interface->mangled_name());
}
else if (writing_generic)
{
write(type->generic_param_mangled_name());
}
else
{
auto name = type->mangled_name();
if (name == "IInspectable" && !writing_generic)
{
write("C_IInspectable");
}
else
{
write(name);
}
}
}
else if (abi_types)
{
// In generics, don't use the ABI name or we get IVectorIBase instead of IVectorBase
if (!writing_generic)
{
write(type->cpp_abi_name());
}
else
{
// Otherwise, write the swift type name when writing a swift abi generic type
write(type->swift_type_name());
}
}
else if (type->swift_logical_namespace() != type_namespace || full_type_names)
{
write(get_full_swift_type_name(*this, type));
}
else
{
write(type->swift_type_name());
}
}
void write(metadata_type const& type)
{
write(&type);
}
void write_abi(ElementType type)
{
assert(mangled_names || abi_types);
if (type == ElementType::Boolean){ write("boolean"); }
else if (type == ElementType::Char) { write("WCHAR"); }
else if (type == ElementType::I1) { write("INT8"); }
else if (type == ElementType::U1) { write("UINT8"); }
else if (type == ElementType::I2) { write("INT16"); }
else if (type == ElementType::U2) { write("UINT16"); }
else if (type == ElementType::I4) { write("INT32"); }
else if (type == ElementType::U4) { write("UINT32"); }
else if (type == ElementType::I8) { write("INT64"); }
else if (type == ElementType::U8) { write("UINT64"); }
else if (type == ElementType::R4) { write("FLOAT"); }
else if (type == ElementType::R8) { write("DOUBLE"); }
else if (type == ElementType::String) { write("HSTRING"); }
else if (type == ElementType::Object) { write("C_IInspectable"); }
else
{
assert(false);
}
}
// duplicated with the names defined in types.cpp
void write_mangled(ElementType type)
{
assert(writing_generic);
if (type == ElementType::Boolean) { write("boolean"); }
else if (type == ElementType::Char) { write("wchar__zt"); }
else if (type == ElementType::U1) { write("byte"); }
else if (type == ElementType::I2) { write("short"); }
else if (type == ElementType::U2) { write("UINT16"); }
else if (type == ElementType::I4) { write("int"); }
else if (type == ElementType::U4) { write("UINT32"); }
else if (type == ElementType::I8) { write("__z__zint64"); }
else if (type == ElementType::U8) { write("UINT64"); }
else if (type == ElementType::R4) { write("float"); }
else if (type == ElementType::R8) { write("double"); }
else if (type == ElementType::String) { write("HSTRING"); }
else if (type == ElementType::Object) { write("IInspectable"); }
else
{
assert(false);
}
}
void write_swift(ElementType type)
{
if (type == ElementType::Boolean) { write("Bool"); }
else if (type == ElementType::Char) { write("Character"); }
else if (type == ElementType::I1) { write("Int8"); }
else if (type == ElementType::U1) { write("UInt8"); }
else if (type == ElementType::I2) { write("Int16"); }
else if (type == ElementType::U2) { write("UInt16"); }
else if (type == ElementType::I4) { write("Int32"); }
else if (type == ElementType::U4) { write("UInt32"); }
else if (type == ElementType::I8) { write("Int64"); }
else if (type == ElementType::U8) { write("UInt64"); }
else if (type == ElementType::R4) { write("Float"); }
else if (type == ElementType::R8) { write("Double"); }
else if (type == ElementType::String) { write("String"); }
else if (type == ElementType::Object) { write("Any"); }
else
{
assert(false);
}
}
void write(ElementType type)
{
if (mangled_names && writing_generic)
{
write_mangled(type);
}
else if (mangled_names || abi_types)
{
write_abi(type);
}
else
{
write_swift(type);
}
}
void write(generic_inst const& generic)
{
auto guard{ push_writing_generic(true) };
if (mangled_names)
{
write(generic.mangled_name());
}
else
{
if (abi_types)
{
// Consolidate with implementation in write(metadata_type)
auto guard = push_generic_params(generic);
write(generic.generic_type());
}
else
{
write("%<", remove_tick(generic.generic_type()->swift_type_name()));
bool first = true;
for (auto&& generic_arg : generic.generic_params()) {
if (!first) write(", ");
write(generic_arg);
if (is_reference_type(generic_arg))
{
write("?");
}
first = false;
}
write(">");
}
}
}
void write(const generic_inst* generic)
{
write(*generic);
}
static std::filesystem::path root_directory()
{
return settings.output_folder / "Sources";
}
std::filesystem::path project_directory()
{
if (settings.test)
{
return root_directory() / settings.support;
}
else
{
return root_directory() / swift_module;
}
}
void save_file(std::string_view const& ext = "")
{
auto filename = type_namespace;
if (!ext.empty())
{
filename += '+';
filename += ext;
}
filename += ".swift";
flush_to_file(project_directory() / filename);
}
void save_header()
{
flush_to_file(root_directory() / "CWinRT" / "include" / (type_namespace + ".h"));
}
void save_modulemap()
{
flush_to_file(root_directory() / "CWinRT" / "include" / "module.modulemap");
}
void save_cmake()
{
flush_to_file(project_directory() / "CMakeLists.txt");
}
};
}