MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / emit_header

Method emit_header

imperative/tablegen/targets/cpp_class.cpp:50–165  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48};
49
50void OpDefEmitter::emit_header() {
51 os << formatv(
52 "class {0} : public OpDefImplBase<{0}> {{\n"
53 " MGB_DYN_TYPE_OBJ_FINAL_DECL;\n\n"
54 "public:\n",
55 op.getCppClassName());
56 // handle enum alias
57 for (auto&& i : op.getMgbAttributes()) {
58 if (auto attr = llvm::dyn_cast<MgbEnumAttr>(&i.attr)) {
59 os << formatv(
60 " using {0} = {1};\n", attr->getEnumName(),
61 attr->getUnderlyingType());
62 }
63 }
64 for (auto&& i : op.getMgbAttributes()) {
65 auto defaultValue = i.attr.getDefaultValue().str();
66 if (!defaultValue.empty()) {
67 defaultValue = formatv(" = {0}", defaultValue);
68 }
69 os << formatv(" {0} {1}{2};\n", attr_to_ctype(i.attr), i.name, defaultValue);
70 }
71
72 auto gen_ctor = [&](auto&& paramList, auto&& memInitList, auto&& body) {
73 os << formatv(
74 " {0}({1}){2}{3}\n", op.getCppClassName(), paramList, memInitList,
75 body);
76 };
77
78 gen_ctor("", "", " = default;");
79
80 if (!op.getMgbAttributes().empty()) {
81 std::string strategy_val = "";
82 std::vector<std::string> paramList, initList;
83 for (auto&& i : op.getMgbAttributes()) {
84 if (attr_to_ctype(i.attr).compare("Strategy") == 0) {
85 strategy_val = i.name;
86 }
87 paramList.push_back(formatv("{0} {1}_", attr_to_ctype(i.attr), i.name));
88 initList.push_back(formatv("{0}({0}_)", i.name));
89 }
90 paramList.push_back("std::string scope_ = {}");
91 if (!strategy_val.empty()) {
92 gen_ctor(
93 llvm::join(paramList, ", "), ": " + llvm::join(initList, ", "),
94 formatv(" {"
95 "\n set_scope(scope_);"
96 "\n mgb_assert(static_cast<uint32_t>({0}) <= "
97 "uint32_t(8));"
98 "\n }",
99 strategy_val));
100 } else {
101 gen_ctor(
102 llvm::join(paramList, ", "), ": " + llvm::join(initList, ", "),
103 " { set_scope(scope_); }");
104 }
105 }
106
107 auto packedParams = op.getPackedParams();

Callers 1

gen_op_def_c_headerFunction · 0.80

Calls 14

attr_to_ctypeFunction · 0.85
getMgbAttributesMethod · 0.80
getEnumNameMethod · 0.80
getUnderlyingTypeMethod · 0.80
getPackedParamsMethod · 0.80
getFieldsMethod · 0.80
getFullNameMethod · 0.80
getExtraArgumentsMethod · 0.80
getAccessorMethod · 0.80
getExtraOpdefDeclMethod · 0.80
strMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected