14#include <mlir/TableGen/Interfaces.h>
15#include <mlir/TableGen/Operator.h>
17#include <llvm/ADT/StringSet.h>
18#include <llvm/Support/FormatVariadic.h>
21 llvm::SmallVector<ExtraMethod> methods =
parseExtraMethods(op.getExtraClassDeclaration());
22 llvm::StringSet<> methodNames;
24 methodNames.insert(method.methodName);
27 for (
const mlir::tblgen::Trait &traitDef : op.getTraits()) {
28 const auto *trait = llvm::dyn_cast<mlir::tblgen::InterfaceTrait>(&traitDef);
29 if (!trait || !trait->shouldDeclareMethods()) {
33 llvm::StringSet<> requestedMethods;
34 for (llvm::StringRef methodName : trait->getAlwaysDeclaredMethods()) {
35 requestedMethods.insert(methodName);
37 mlir::tblgen::Interface
interface = trait->getInterface();
38 for (
const mlir::tblgen::InterfaceMethod &interfaceMethod : interface.getMethods()) {
41 if (!requestedMethods.contains(interfaceMethod.getName()) || interfaceMethod.isStatic() ||
42 interfaceMethod.getBody()) {
46 if (!methodNames.insert(interfaceMethod.getName()).second) {
47 warnSkipped(interfaceMethod.getName(),
"C API does not support method overloading");
52 method.
returnType = interfaceMethod.getReturnType().str();
53 method.methodName = interfaceMethod.getName().str();
54 for (
const auto &argument : interfaceMethod.getArguments()) {
55 method.parameters.emplace_back(argument.type.str(), argument.name.str());
57 methods.push_back(std::move(method));
66 llvm::raw_string_ostream oss(params);
68 if (!op.allResultTypesKnown()) {
70 for (
auto [i, result] : llvm::enumerate(op.getResults())) {
71 llvm::StringRef name = result.name;
72 genResult(oss, result, name.empty() ? llvm::formatv(
"result{0}", i).str() : name.str());
78 for (
const mlir::tblgen::NamedTypeConstraint &operand : op.getOperands()) {
82 auto attrs = op.getAttributes();
85 for (
const mlir::tblgen::NamedAttribute &namedAttr : attrs) {
92 auto regions = op.getRegions();
93 if (!regions.empty()) {
95 for (
const mlir::tblgen::NamedRegion ®ion : regions) {
SmallVector< ExtraMethod > parseExtraMethods(StringRef extraDecl)
Parse method declarations from extraClassDeclaration using Clang's Lexer.
llvm::SmallVector< ExtraMethod > getCAPIExposedOpMethods(const mlir::tblgen::Operator &op)
Return public operation methods explicitly requested by DeclareOpInterfaceMethods traits and declared...
virtual void genRegionsSuffix(llvm::raw_ostream &os, const mlir::tblgen::Operator &op)
Generate region section suffix code to os. Default does nothing.
virtual void genOperand(llvm::raw_ostream &os, const mlir::tblgen::NamedTypeConstraint &operand)=0
Generate code for operand to os.
std::string gen(const mlir::tblgen::Operator &op)
Generate string from the operation pieces.
virtual void genAttributesSuffix(llvm::raw_ostream &os, const mlir::tblgen::Operator &op)
Generate attribute section suffix code to os. Default does nothing.
virtual void genResultInferred(llvm::raw_ostream &os)
Generate code to os when result type is inferred. Default does nothing.
virtual void genHeader(llvm::raw_ostream &os)
Generate header code to os. Default does nothing.
virtual void genRegion(llvm::raw_ostream &os, const mlir::tblgen::NamedRegion ®ion)=0
Generate code for region to os.
virtual void genAttributesPrefix(llvm::raw_ostream &os, const mlir::tblgen::Operator &op)
Generate attribute section prefix code to os. Default does nothing.
virtual void genResult(llvm::raw_ostream &os, const mlir::tblgen::NamedTypeConstraint &result, const std::string &resultName)=0
Generate code for result to os.
virtual void genAttribute(llvm::raw_ostream &os, const mlir::tblgen::NamedAttribute &attr)=0
Generate code for attr to os.
virtual void genRegionsPrefix(llvm::raw_ostream &os, const mlir::tblgen::Operator &op)
Generate region section prefix code to os. Default does nothing.