18#include <mlir/TableGen/AttrOrTypeDef.h>
25 using HeaderGenerator::HeaderGenerator;
41MLIR_CAPI_EXPORTED void {0}{2}_{3}Get{4}(Mlir{1}, MlirStringCallback callback, void *userData);
48 static constexpr char fmt[] = R
"(
50MLIR_CAPI_EXPORTED {7} {0}{2}_{3}Get{4}(Mlir{1});
53 assert(!
paramName.empty() &&
"paramName must be set");
69 static constexpr char fmt[] = R
"(
71MLIR_CAPI_EXPORTED intptr_t {0}{2}_{3}Get{4}Count(Mlir{1});
74MLIR_CAPI_EXPORTED {7} {0}{2}_{3}Get{4}At(Mlir{1}, intptr_t pos);
77 assert(!
paramName.empty() &&
"paramName must be set");
94 static constexpr char fmt[] = R
"(
96MLIR_CAPI_EXPORTED Mlir{1} {0}{2}_{3}Get(MlirContext ctx{4});
101 std::string paramListBuffer;
102 llvm::raw_string_ostream paramListStream(paramListBuffer);
103 for (
const auto ¶m : def.getParameters()) {
104 mlir::StringRef cppType = param.getCppType();
108 paramListStream <<
", intptr_t " << param.getName() <<
"Count, "
127 mlir::tblgen::Dialect defDialect = def.getDialect();
148 for (
const auto ¶m : def.getParameters()) {
150 mlir::StringRef cppType = param.getCppType();
161 std::optional<mlir::StringRef> extraDecls = def.getExtraDecls();
162 if (extraDecls.has_value()) {
178 using ImplementationGenerator::ImplementationGenerator;
190#include <mlir/IR/Diagnostics.h>
191#include <mlir/IR/BuiltinAttributes.h>
192#include <mlir/CAPI/IR.h>
193#include <mlir/CAPI/Support.h>
194#include <llvm/ADT/TypeSwitch.h>
203 static constexpr char fmt[] = R
"(
204intptr_t {0}{2}_{3}Get{4}Count(Mlir{1} inp) {{
205 auto size = llvm::cast<{3}>(unwrap(inp)).get{4}().size();
206 assert(std::in_range<intptr_t>(size) && "lossy conversion");
207 return static_cast<intptr_t>(size);
210{5} {0}{2}_{3}Get{4}At(Mlir{1} inp, intptr_t pos) {{
211 return {6}(llvm::cast<{3}>(unwrap(inp)).get{4}()[pos]);
214 assert(!className.empty() && "className must be set");
215 assert(!
paramName.empty() &&
"paramName must be set");
233void {0}{2}_{3}Get{4}(Mlir{1} inp, MlirStringCallback callback, void *userData) {{
235 llvm::raw_string_ostream(text) << llvm::cast<{3}>(unwrap(inp)).get{4}();
236 callback(wrap(llvm::StringRef(text)), userData);
243 static constexpr char fmt[] = R
"(
244{5} {0}{2}_{3}Get{4}(Mlir{1} inp) {{
245 return {6}(llvm::cast<{3}>(unwrap(inp)).get{4}());
248 assert(!className.empty() && "className must be set");
249 assert(!
paramName.empty() &&
"paramName must be set");
264 static constexpr char fmt[] = R
"(
265Mlir{1} {0}{2}_{3}Get(MlirContext ctx{4}) {{
267 return wrap({3}::{7}unwrap(ctx){5}));
270 assert(!className.empty() && "className must be set");
273 std::string paramListBuffer;
274 std::string argListBuffer;
275 std::string prefixBuffer;
276 llvm::raw_string_ostream paramListStream(paramListBuffer);
277 llvm::raw_string_ostream argListStream(argListBuffer);
278 llvm::raw_string_ostream prefixStream(prefixBuffer);
280 for (
const auto ¶m : def.getParameters()) {
281 mlir::StringRef pName = param.getName();
282 mlir::StringRef cppType = param.getCppType();
287 paramListStream <<
", intptr_t " << pName <<
"Count, " << capiElemType <<
" const *"
292 argListStream <<
", ::llvm::ArrayRef<" << cppElemType <<
">(" << pName <<
", " << pName
296 if (storageCppType.has_value()) {
297 prefixStream <<
"SmallVector<" << storageCppType.value() <<
"> storage;";
298 argListStream <<
", llvm::map_to_vector(unwrapList(" << pName <<
"Count, " << pName
299 <<
", storage), [](auto a) {return llvm::cast<" << cppElemType
302 prefixStream <<
"SmallVector<" << cppElemType <<
"> storage;";
303 argListStream <<
", unwrapList(" << pName <<
"Count, " << pName <<
", storage)";
308 paramListStream <<
", " << capiType <<
' ' << pName;
311 argListStream <<
", ";
313 prefixStream <<
"auto " << pName <<
"Integer = llzk::parseDynamicAPInt(unwrap(" << pName
317 <<
"Integer) { mlir::emitError(mlir::UnknownLoc::get(unwrap(ctx)), llvm::toString("
318 << pName <<
"Integer.takeError())); return {nullptr}; }\n";
319 argListStream <<
"*" << pName <<
"Integer";
321 argListStream << pName;
322 }
else if (capiType ==
"MlirAttribute" || capiType ==
"MlirType") {
324 argListStream <<
"::llvm::cast<" << cppType <<
">(unwrap(" << pName <<
"))";
327 argListStream <<
"unwrap(" << pName <<
')';
341 def.genVerifyDecl() && llvm::any_of(
343 [](
const auto &p) { return isDynamicAPIntType(p.getCppType()); }
345 ?
"getChecked(mlir::UnknownLoc::get(unwrap(ctx)), "
351 mlir::tblgen::Dialect defDialect = def.getDialect();
372 for (
const auto ¶m : def.getParameters()) {
374 mlir::StringRef cppType = param.getCppType();
385 std::optional<mlir::StringRef> extraDecls = def.getExtraDecls();
386 if (extraDecls.has_value()) {
llvm::cl::opt< std::string > & getFunctionPrefix()
Return the FunctionPrefix option, registering it on first access.
std::string mapCppTypeToCapiType(StringRef cppType)
llvm::cl::opt< std::string > & getDialectName()
Return the DialectName option, registering it on first access.
std::optional< std::string > mapCapiTypeToBasicCppType(StringRef capiType)
llvm::cl::opt< bool > & getGenExtraClassMethods()
Return the GenExtraClassMethods option, registering it on first access.
llvm::cl::opt< bool > & getGenTypeOrAttrParamGetters()
Return the GenTypeOrAttrParamGetters option, registering it on first access.
llvm::cl::opt< bool > & getGenIsA()
Return the GenIsA option, registering it on first access.
llvm::cl::opt< bool > & getGenTypeOrAttrGet()
Return the GenTypeOrAttrGet option, registering it on first access.
mlir::StringRef extractArrayRefElementType(mlir::StringRef cppType)
Extract element type from ArrayRef<...>.
bool isPrimitiveType(mlir::StringRef cppType)
Check if a C++ type is a known primitive type.
bool isDynamicAPIntType(mlir::StringRef cppType)
Whether a parameter represents an arbitrary-precision mathematical integer.
bool isArrayRefType(mlir::StringRef cppType)
Check if a C++ type is an ArrayRef type.
std::string toPascalCase(mlir::StringRef str)
Convert names separated by underscore or colon to PascalCase.
Generator for attribute/type C implementation files.
mlir::StringRef paramName
void genCompleteRecord(const mlir::tblgen::AttrOrTypeDef &def)
void setParamName(mlir::StringRef name)
Set the parameter name for code generation.
void genPrologue() const override
virtual void genDefaultGetBuilderImpl(const mlir::tblgen::AttrOrTypeDef &def) const
Generate default Get builder implementation.
virtual void genArrayRefParameterImpls(mlir::StringRef cppType) const
~AttrOrTypeImplementationGenerator() override=default
std::string paramNameCapitalized
virtual void genParameterGetterImpl(mlir::StringRef cppType) const
mlir::StringRef className
mlir::StringRef dialectNamespace
virtual void genExtraMethods(mlir::StringRef extraDecl) const
Generate code for extra methods from an extraClassDeclaration.
virtual void setNamespaceAndClassName(const mlir::tblgen::Dialect &d, mlir::StringRef cppClassName)
Set the dialect and class name for code generation.
std::string dialectNameCapitalized
Generator for common C implementation file elements.
virtual void genIsAImpl() const