idlize
113 строк · 4.2 Кб
1import { DeclarationTable } from "../DeclarationTable";2import { LanguageWriter } from "../LanguageWriters";3import { PeerLibrary } from "../PeerLibrary";4
5class ConvertorsPrinter {6constructor(7private readonly library: PeerLibrary,8private readonly writer: LanguageWriter,9) {}10
11private get table(): DeclarationTable {12return this.library.declarationTable13}14
15writeUnionConvertors() {16
17this.writer.print('template<typename T, typename P>')18this.writer.print('void AssignTo(std::optional<T>& dst, const P& src);')19this.writer.print("")20
21this.writer.print('template<typename T, typename P>')22this.writer.print('void AssignUnionTo(std::optional<T>& dst, const P& src);')23this.writer.print("")24
25this.writer.print('template<typename T, typename P>')26this.writer.print('void AssignOptionalTo(std::optional<T>& dst, const P& src);')27this.writer.print("")28
29for (const [typename, selectors] of this.table.allUnionTypes()) {30this.writer.print('template<typename T>')31this.writer.print(`void AssignUnionTo(std::optional<T>& dst, const ${ typename }& src) {`)32this.writer.pushIndent()33this.writer.print(`switch (src.selector) {`)34this.writer.pushIndent()35selectors.forEach(selector => {36this.writer.print(`case ${ selector.id - 1 }: AssignTo(dst, src.${ selector.name }); break;`)37})38this.writer.print(`default: LOGE("Unexpected src->selector: %{public}d\\n", src.selector); abort(); `)39this.writer.popIndent()40this.writer.print("}")41this.writer.popIndent()42this.writer.print("}")43this.writer.print("")44}45
46}47
48writeOptionalConvertors() {49this.writer.print("#define ASSIGN_OPT(name)\\")50this.writer.pushIndent()51this.writer.print("template<typename T> \\")52this.writer.print("void AssignOptionalTo(std::optional<T>& dst, const name& src) { \\")53this.writer.pushIndent()54this.writer.print("if (src.tag != ARK_TAG_UNDEFINED) { \\")55this.writer.pushIndent()56this.writer.print("AssignUnionTo(dst, src.value); \\")57this.writer.popIndent()58this.writer.print("} \\")59this.writer.popIndent()60this.writer.print("} \\")61this.writer.print("template<typename T> \\")62this.writer.print("void WithOptional(const name& src, T call) { \\")63this.writer.pushIndent()64this.writer.print("if (src.tag != ARK_TAG_UNDEFINED) { \\")65this.writer.pushIndent()66this.writer.print("call(src.value); \\")67this.writer.popIndent()68this.writer.print("} \\")69this.writer.popIndent()70this.writer.print("}")71this.writer.popIndent()72this.table.allUnionTypes()73this.writer.pushIndent()74this.table.allOptionalTypes().forEach(optionalName => {75this.writer.print(`ASSIGN_OPT(${optionalName})`)76})77this.writer.popIndent()78this.writer.print("#undef ASSIGN_OPT")79}80
81writeLiteralConvertors() {82
83this.writer.print('template<typename T, typename P>')84this.writer.print('void AssignLiteralTo(std::optional<T>& dst, const P& src);')85this.writer.print("")86
87for (const [name, type] of this.table.allLiteralTypes()) {88this.writer.print('template<typename T>')89this.writer.print(`void AssignLiteralTo(std::optional<T>& dst, const ${name}& src) {`)90this.writer.pushIndent()91if (type === "template") {92this.writer.print(`AssignTo(dst, src.template_); `)93} else {94this.writer.print(`AssignTo(dst, src.${type}); `)95}96this.writer.popIndent()97this.writer.print(`}`)98this.writer.print("")99}100this.writer.print("")101}102
103print() {104this.writeUnionConvertors()105this.writeLiteralConvertors()106this.writeOptionalConvertors()107}108}
109
110export function writeConvertors(library: PeerLibrary, writer: LanguageWriter) {111const printer = new ConvertorsPrinter(library, writer)112printer.print()113}
114