@@ -105,7 +105,14 @@ static bool emitAttributeBuilder(const EnumAttr &enumAttr, raw_ostream &os) {
105
105
return true ;
106
106
}
107
107
108
- os << llvm::formatv (" @register_attribute_builder(\" {0}\" )\n " ,
108
+ llvm::SmallVector<StringRef> namespaces;
109
+ enumAttr.getStorageType ().ltrim (" ::" ).split (namespaces, " ::" );
110
+ namespaces = llvm::SmallVector<StringRef>{llvm::drop_end (namespaces)};
111
+ std::string namespace_ = getAttributeNameSpace (namespaces);
112
+ if (!namespace_.empty ())
113
+ namespace_ += " _" ;
114
+
115
+ os << llvm::formatv (" @register_attribute_builder(\" {0}{1}\" )\n " , namespace_,
109
116
enumAttr.getAttrDefName ());
110
117
os << llvm::formatv (" def _{0}(x, context):\n " ,
111
118
enumAttr.getAttrDefName ().lower ());
@@ -120,11 +127,33 @@ static bool emitAttributeBuilder(const EnumAttr &enumAttr, raw_ostream &os) {
120
127
// / Emits an attribute builder for the given dialect enum attribute to support
121
128
// / automatic conversion between enum values and attributes in Python. Returns
122
129
// / `false` on success, `true` on failure.
123
- static bool emitDialectEnumAttributeBuilder (StringRef attrDefName,
124
- StringRef formatString,
130
+ static bool emitDialectEnumAttributeBuilder (const AttrOrTypeDef &attr,
125
131
raw_ostream &os) {
126
- os << llvm::formatv (" @register_attribute_builder(\" {0}\" )\n " , attrDefName);
127
- os << llvm::formatv (" def _{0}(x, context):\n " , attrDefName.lower ());
132
+ StringRef mnemonic = attr.getMnemonic ().value ();
133
+ std::optional<StringRef> assemblyFormat = attr.getAssemblyFormat ();
134
+ StringRef dialect = attr.getDialect ().getName ();
135
+ std::string formatString;
136
+ if (assemblyFormat == " `<` $value `>`" )
137
+ formatString =
138
+ llvm::formatv (" #{0}.{1}<{{str(x)}>" , dialect, mnemonic).str ();
139
+ else if (assemblyFormat == " $value" )
140
+ formatString =
141
+ llvm::formatv (" #{0}<{1} {{str(x)}>" , dialect, mnemonic).str ();
142
+ else {
143
+ llvm::errs ()
144
+ << " unsupported assembly format for python enum bindings generation" ;
145
+ return true ;
146
+ }
147
+
148
+ llvm::SmallVector<StringRef> namespaces;
149
+ attr.getStorageNamespace ().ltrim (" ::" ).split (namespaces, " ::" );
150
+ std::string namespace_ = getAttributeNameSpace (namespaces);
151
+ if (!namespace_.empty ())
152
+ namespace_ += " _" ;
153
+
154
+ os << llvm::formatv (" @register_attribute_builder(\" {0}{1}\" )\n " , namespace_,
155
+ attr.getName ());
156
+ os << llvm::formatv (" def _{0}(x, context):\n " , attr.getName ().lower ());
128
157
os << llvm::formatv (" return "
129
158
" _ods_ir.Attribute.parse(f'{0}', context=context)\n\n " ,
130
159
formatString);
@@ -142,29 +171,10 @@ static bool emitPythonEnums(const llvm::RecordKeeper &recordKeeper,
142
171
emitEnumClass (enumAttr, os);
143
172
emitAttributeBuilder (enumAttr, os);
144
173
}
145
- for (auto &it : recordKeeper.getAllDerivedDefinitionsIfDefined (" EnumAttr" )) {
146
- AttrOrTypeDef attr (&*it);
147
- if (!attr.getMnemonic ()) {
148
- llvm::errs () << " enum case " << attr
149
- << " needs mnemonic for python enum bindings generation" ;
150
- return true ;
151
- }
152
- StringRef mnemonic = attr.getMnemonic ().value ();
153
- std::optional<StringRef> assemblyFormat = attr.getAssemblyFormat ();
154
- StringRef dialect = attr.getDialect ().getName ();
155
- if (assemblyFormat == " `<` $value `>`" ) {
156
- emitDialectEnumAttributeBuilder (
157
- attr.getName (),
158
- llvm::formatv (" #{0}.{1}<{{str(x)}>" , dialect, mnemonic).str (), os);
159
- } else if (assemblyFormat == " $value" ) {
160
- emitDialectEnumAttributeBuilder (
161
- attr.getName (),
162
- llvm::formatv (" #{0}<{1} {{str(x)}>" , dialect, mnemonic).str (), os);
163
- } else {
164
- llvm::errs ()
165
- << " unsupported assembly format for python enum bindings generation" ;
166
- return true ;
167
- }
174
+ for (const auto &it :
175
+ recordKeeper.getAllDerivedDefinitionsIfDefined (" EnumAttr" )) {
176
+ const AttrOrTypeDef attr (&*it);
177
+ return emitDialectEnumAttributeBuilder (attr, os);
168
178
}
169
179
170
180
return false ;
0 commit comments