2525using namespace llvm ;
2626using namespace llvm_dialects ;
2727
28- // Default destructor instantiated explicitly to avoid having to add more
29- // includes in the header.
30- Operation::~Operation () = default ;
31-
3228static std::optional<std::vector<NamedValue>>
3329parseArguments (raw_ostream &errs, GenDialectsContext &context, Record *rec) {
3430 Record *superClassRec = rec->getValueAsDef (" superclass" );
@@ -54,38 +50,78 @@ parseArguments(raw_ostream &errs, GenDialectsContext &context, Record *rec) {
5450 NamedValue::Parser::OperationArguments);
5551}
5652
57- std::unique_ptr<OpClass>
58- OpClass::parse (raw_ostream &errs, GenDialectsContext &context, Record *record) {
59- auto opClass = std::make_unique<OpClass>();
60- opClass->dialect = context.getDialect (record->getValueAsDef (" dialect" ));
61- opClass->name = record->getName ();
62- opClass->superclass = context.getOpClass (record->getValueAsDef (" superclass" ));
53+ bool OperationBase::init (raw_ostream &errs, GenDialectsContext &context,
54+ Record *record) {
55+ m_dialect = context.getDialect (record->getValueAsDef (" dialect" ));
56+ m_superclass = context.getOpClass (record->getValueAsDef (" superclass" ));
6357
6458 auto arguments = parseArguments (errs, context, record);
6559 if (!arguments.has_value ())
66- return {};
67- opClass->arguments = std::move (*arguments);
68-
69- OpClass *ptr = opClass.get ();
70- opClass->dialect ->opClasses .push_back (opClass.get ());
71- if (opClass->superclass )
72- opClass->superclass ->subclasses .push_back (ptr);
60+ return false ;
61+ m_arguments = std::move (*arguments);
7362
74- return opClass ;
63+ return true ;
7564}
7665
77- SmallVector<NamedValue> OpClass ::getFullArguments () const {
66+ SmallVector<NamedValue> OperationBase ::getFullArguments () const {
7867 SmallVector<NamedValue> args;
79- if (superclass )
80- args = superclass ->getFullArguments ();
81- args.insert (args.end (), arguments .begin (), arguments .end ());
68+ if (m_superclass )
69+ args = m_superclass ->getFullArguments ();
70+ args.insert (args.end (), m_arguments .begin (), m_arguments .end ());
8271 return args;
8372}
8473
85- unsigned OpClass::getNumFullArguments () const {
86- if (superclass)
87- return superclass->getNumFullArguments () + arguments.size ();
88- return arguments.size ();
74+ unsigned OperationBase::getNumFullArguments () const {
75+ if (m_superclass)
76+ return m_superclass->getNumFullArguments () + m_arguments.size ();
77+ return m_arguments.size ();
78+ }
79+
80+ void OperationBase::emitArgumentAccessorDeclarations (llvm::raw_ostream &out,
81+ FmtContext &fmt) const {
82+ for (const auto &arg : m_arguments) {
83+ out << tgfmt (" $0 get$1();\n " , &fmt, arg.type ->getBuilderCppType (),
84+ convertToCamelFromSnakeCase (arg.name , true ));
85+ }
86+ }
87+
88+ void OperationBase::emitArgumentAccessorDefinitions (llvm::raw_ostream &out,
89+ FmtContext &fmt) const {
90+ unsigned numSuperclassArgs = 0 ;
91+ if (m_superclass)
92+ numSuperclassArgs = m_superclass->getNumFullArguments ();
93+ for (auto indexedArg : llvm::enumerate (m_arguments)) {
94+ const NamedValue &arg = indexedArg.value ();
95+ std::string value = llvm::formatv (" getArgOperand({0})" ,
96+ numSuperclassArgs + indexedArg.index ());
97+ if (auto *attr = dyn_cast<Attr>(arg.type ))
98+ value = tgfmt (attr->getFromLlvmValue (), &fmt, value);
99+ else if (arg.type ->isTypeArg ())
100+ value += " ->getType()" ;
101+ out << tgfmt (R"(
102+ $0 $_op::get$1() {
103+ return $2;
104+ }
105+ )" ,
106+ &fmt, arg.type ->getBuilderCppType (),
107+ convertToCamelFromSnakeCase (arg.name , true ), value);
108+ }
109+ }
110+
111+ std::unique_ptr<OpClass>
112+ OpClass::parse (raw_ostream &errs, GenDialectsContext &context, Record *record) {
113+ auto opClass = std::make_unique<OpClass>();
114+ opClass->name = record->getName ();
115+
116+ if (!opClass->init (errs, context, record))
117+ return {};
118+
119+ OpClass *ptr = opClass.get ();
120+ opClass->dialect ()->opClasses .push_back (opClass.get ());
121+ if (opClass->superclass ())
122+ opClass->superclass ()->subclasses .push_back (ptr);
123+
124+ return opClass;
89125}
90126
91127static std::string evaluateAttrLlvmType (raw_ostream &errs, raw_ostream &out,
@@ -113,22 +149,27 @@ static std::string evaluateAttrLlvmType(raw_ostream &errs, raw_ostream &out,
113149 return attrType;
114150}
115151
152+ // Default destructor instantiated explicitly to avoid having to add more
153+ // includes in the header.
154+ Operation::~Operation () = default ;
155+
116156bool Operation::parse (raw_ostream &errs, GenDialectsContext *context,
117157 GenDialect *dialect, Record *record) {
118158 auto op = std::make_unique<Operation>(*context);
119- op->superclass = context->getOpClass (record->getValueAsDef (" superclass" ));
120- if (op->superclass )
121- op->superclass ->operations .push_back (op.get ());
159+
160+ if (!op->init (errs, *context, record))
161+ return false ;
162+
163+ assert (op->dialect () == dialect);
164+
165+ if (op->superclass ())
166+ op->superclass ()->operations .push_back (op.get ());
167+
122168 op->name = record->getName ();
123169 op->mnemonic = record->getValueAsString (" mnemonic" );
124170 for (Record *traitRec : record->getValueAsListOfDefs (" traits" ))
125171 op->traits .push_back (context->getTrait (traitRec));
126172
127- auto arguments = parseArguments (errs, *context, record);
128- if (!arguments.has_value ())
129- return false ;
130- op->arguments = std::move (*arguments);
131-
132173 EvaluationPlanner evaluation (op->m_system );
133174
134175 for (const auto &arg : op->getFullArguments ()) {
@@ -265,20 +306,6 @@ bool Operation::parse(raw_ostream &errs, GenDialectsContext *context,
265306 return true ;
266307}
267308
268- SmallVector<NamedValue> Operation::getFullArguments () const {
269- SmallVector<NamedValue> args;
270- if (superclass)
271- args = superclass->getFullArguments ();
272- args.insert (args.end (), arguments.begin (), arguments.end ());
273- return args;
274- }
275-
276- unsigned Operation::getNumFullArguments () const {
277- if (superclass)
278- return superclass->getNumFullArguments () + arguments.size ();
279- return arguments.size ();
280- }
281-
282309void Operation::emitVerifierMethod (llvm::raw_ostream &out,
283310 FmtContext &fmt) const {
284311 SymbolTable symbols;
0 commit comments