idlize

Форк
0
/
ConvertorsPrinter.ts 
113 строк · 4.2 Кб
1
import { DeclarationTable } from "../DeclarationTable";
2
import { LanguageWriter } from "../LanguageWriters";
3
import { PeerLibrary } from "../PeerLibrary";
4

5
class ConvertorsPrinter {
6
    constructor(
7
        private readonly library: PeerLibrary,
8
        private readonly writer: LanguageWriter,
9
    ) {}
10

11
    private get table(): DeclarationTable {
12
        return this.library.declarationTable
13
    }
14

15
    writeUnionConvertors() {
16

17
        this.writer.print('template<typename T, typename P>')
18
        this.writer.print('void AssignTo(std::optional<T>& dst, const P& src);')
19
        this.writer.print("")
20

21
        this.writer.print('template<typename T, typename P>')
22
        this.writer.print('void AssignUnionTo(std::optional<T>& dst, const P& src);')
23
        this.writer.print("")
24

25
        this.writer.print('template<typename T, typename P>')
26
        this.writer.print('void AssignOptionalTo(std::optional<T>& dst, const P& src);')
27
        this.writer.print("")
28

29
        for (const [typename, selectors] of this.table.allUnionTypes()) {
30
            this.writer.print('template<typename T>')
31
            this.writer.print(`void AssignUnionTo(std::optional<T>& dst, const ${ typename }& src) {`)
32
            this.writer.pushIndent()
33
            this.writer.print(`switch (src.selector) {`)
34
            this.writer.pushIndent()
35
            selectors.forEach(selector => {
36
                this.writer.print(`case ${ selector.id - 1 }: AssignTo(dst, src.${ selector.name }); break;`)
37
            })
38
            this.writer.print(`default: LOGE("Unexpected src->selector: %{public}d\\n", src.selector); abort(); `)
39
            this.writer.popIndent()
40
            this.writer.print("}")
41
            this.writer.popIndent()
42
            this.writer.print("}")
43
            this.writer.print("")
44
        }
45

46
    }
47

48
    writeOptionalConvertors() {
49
        this.writer.print("#define ASSIGN_OPT(name)\\")
50
        this.writer.pushIndent()
51
        this.writer.print("template<typename T> \\")
52
        this.writer.print("void AssignOptionalTo(std::optional<T>& dst, const name& src) { \\")
53
        this.writer.pushIndent()
54
        this.writer.print("if (src.tag != ARK_TAG_UNDEFINED) { \\")
55
        this.writer.pushIndent()
56
        this.writer.print("AssignUnionTo(dst, src.value); \\")
57
        this.writer.popIndent()
58
        this.writer.print("} \\")
59
        this.writer.popIndent()
60
        this.writer.print("} \\")
61
        this.writer.print("template<typename T> \\")
62
        this.writer.print("void WithOptional(const name& src, T call) { \\")
63
        this.writer.pushIndent()
64
        this.writer.print("if (src.tag != ARK_TAG_UNDEFINED) { \\")
65
        this.writer.pushIndent()
66
        this.writer.print("call(src.value); \\")
67
        this.writer.popIndent()
68
        this.writer.print("} \\")
69
        this.writer.popIndent()
70
        this.writer.print("}")
71
        this.writer.popIndent()
72
        this.table.allUnionTypes()
73
        this.writer.pushIndent()
74
        this.table.allOptionalTypes().forEach(optionalName => {
75
            this.writer.print(`ASSIGN_OPT(${optionalName})`)
76
        })
77
        this.writer.popIndent()
78
        this.writer.print("#undef ASSIGN_OPT")
79
    }
80

81
    writeLiteralConvertors() {
82

83
        this.writer.print('template<typename T, typename P>')
84
        this.writer.print('void AssignLiteralTo(std::optional<T>& dst, const P& src);')
85
        this.writer.print("")
86

87
        for (const [name, type] of this.table.allLiteralTypes()) {
88
            this.writer.print('template<typename T>')
89
            this.writer.print(`void AssignLiteralTo(std::optional<T>& dst, const ${name}& src) {`)
90
            this.writer.pushIndent()
91
            if (type === "template") {
92
                this.writer.print(`AssignTo(dst, src.template_); `)
93
            } else {
94
                this.writer.print(`AssignTo(dst, src.${type}); `)
95
            }
96
            this.writer.popIndent()
97
            this.writer.print(`}`)
98
            this.writer.print("")
99
        }
100
        this.writer.print("")
101
    }
102

103
    print() {
104
        this.writeUnionConvertors()
105
        this.writeLiteralConvertors()
106
        this.writeOptionalConvertors()
107
    }
108
}
109

110
export function writeConvertors(library: PeerLibrary, writer: LanguageWriter) {
111
    const printer = new ConvertorsPrinter(library, writer)
112
    printer.print()
113
}
114

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.