23 #ifndef MLIR_TABLEGEN_CLASS_H_
24 #define MLIR_TABLEGEN_CLASS_H_
29 #include "llvm/ADT/SetVector.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/ADT/StringSet.h"
33 #include "llvm/ADT/Twine.h"
48 template <
typename TypeT,
typename NameT,
typename DefaultT>
50 bool optional =
false)
51 : type(
stringify(std::forward<TypeT>(type))),
52 name(
stringify(std::forward<NameT>(name))),
53 defaultValue(
stringify(std::forward<DefaultT>(defaultValue))),
58 template <
typename TypeT,
typename NameT>
69 StringRef
getType()
const {
return type; }
71 StringRef
getName()
const {
return name; }
86 std::string defaultValue;
97 : parameters(parameters) {}
99 : parameters(std::move(parameters)) {}
125 template <
typename RetTypeT,
typename NameT>
128 : returnType(
stringify(std::forward<RetTypeT>(retType))),
129 methodName(
stringify(std::forward<NameT>(name))),
130 parameters(std::move(parameters)) {}
133 template <
typename RetTypeT,
typename NameT>
137 std::forward<NameT>(name),
141 template <
typename RetTypeT,
typename NameT,
typename... Parameters>
144 std::forward<NameT>(name),
146 {std::forward<Parameters>(parameters)...})) {}
158 StringRef
getName()
const {
return methodName; }
178 template <
typename ParamT>
180 templateParams.push_back(
stringify(param));
184 template <
typename ContainerT>
186 templateParams.insert(std::begin(container), std::end(container));
191 std::string returnType;
193 std::string methodName;
209 : declOnly(other.declOnly), body(std::move(other.body)), stringOs(body),
220 template <
typename ValueT>
223 os << std::forward<ValueT>(value);
246 scope(StringRef open =
"", StringRef close =
"",
bool indent =
false) {
259 llvm::raw_string_ostream stringOs;
290 StringRef namePrefix)
const {}
298 template <ClassDeclaration::Kind DeclKind>
305 return other->
getKind() == DeclKind;
333 template <
typename RetTypeT,
typename NameT,
typename... Args>
338 std::forward<NameT>(name), std::forward<Args>(args)...),
341 llvm::report_fatal_error(
342 "Invalid combination of method properties specified");
348 std::initializer_list<MethodParameter> params)
352 llvm::report_fatal_error(
353 "Invalid combination of method properties specified");
400 StringRef namePrefix)
const override;
403 template <
typename ParamT>
407 template <
typename ContainerT>
421 [[maybe_unused]]
static bool
437 template <
typename NameT,
typename... Args>
440 std::forward<Args>(args)...) {}
443 template <
typename NameT,
typename ValueT>
445 initializers.emplace_back(
stringify(std::forward<NameT>(name)),
454 StringRef namePrefix)
const override;
458 return isa<Method>(other) && cast<Method>(other)->isConstructor();
467 : name(std::move(name)), value(std::move(value)) {}
493 static_cast<unsigned>(rhs));
500 static_cast<unsigned>(rhs));
506 template <
typename ParamT>
513 template <
typename ContainerT>
524 template <
typename NameT>
526 : name(
stringify(std::forward<NameT>(name))), visibility(visibility) {}
529 template <
typename ParamT>
534 template <
typename ContainerT>
536 templateParams.insert(std::begin(container), std::end(container));
561 template <
typename NameT,
typename ValueT = std::
string>
563 : name(
stringify(std::forward<NameT>(name))),
564 value(
stringify(std::forward<ValueT>(value))) {}
570 template <
typename ParamT>
576 template <
typename ContainerT>
578 templateParams.insert(std::begin(container), std::end(container));
596 template <
typename TypeT,
typename NameT>
598 : type(
stringify(std::forward<TypeT>(type))),
599 name(
stringify(std::forward<NameT>(name))) {}
637 std::string extraClassDefinition =
"")
639 std::move(extraClassDefinition)) {}
642 std::string extraClassDefinition =
"")
643 : extraClassDeclaration(extraClassDeclaration),
644 extraClassDefinition(extraClassDefinition) {}
651 StringRef namePrefix)
const override;
656 std::string extraClassDeclaration;
659 std::string extraClassDefinition;
675 template <
typename NameT,
676 typename = std::enable_if_t<!std::is_same<NameT, Class>::value>>
691 Properties | defaultProperties,
692 std::forward<Args>(args)...));
707 std::forward<NameT>(name),
708 Properties | properties, parameters));
716 return addMethod(std::forward<RetTypeT>(retType), std::forward<NameT>(name),
717 Properties, parameters);
721 typename NameT,
typename... Args>
724 return addMethod(std::forward<RetTypeT>(retType), std::forward<NameT>(name),
725 properties | Properties, {std::forward<Args>(args)...});
730 typename NameT,
typename... Args>
732 return addMethod(std::forward<RetTypeT>(retType), std::forward<NameT>(name),
733 Properties, std::forward<Args>(args)...);
738 typename NameT,
typename... Args>
740 return addMethod<Properties | Method::Static>(
741 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
742 std::forward<Args>(args)...);
747 typename NameT,
typename... Args>
750 return addMethod<Properties | Method::StaticInline>(
751 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
752 std::forward<Args>(args)...);
757 typename NameT,
typename... Args>
759 return addMethod<Properties | Method::Inline>(
760 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
761 std::forward<Args>(args)...);
766 typename NameT,
typename... Args>
768 return addMethod<Properties | Method::Const>(
769 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
770 std::forward<Args>(args)...);
775 typename NameT,
typename... Args>
777 return addMethod<Properties | Method::Declaration>(
778 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
779 std::forward<Args>(args)...);
784 typename NameT,
typename... Args>
787 return addMethod<Properties | Method::StaticDeclaration>(
788 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
789 std::forward<Args>(args)...);
794 template <
typename TypeT,
typename NameT>
796 fields.emplace_back(std::forward<TypeT>(type), std::forward<NameT>(name));
803 template <
typename ParamT>
809 template <
typename ContainerT>
811 templateParams.insert(std::begin(container), std::end(container));
839 template <
typename DeclT,
typename... Args>
841 auto decl = std::make_unique<DeclT>(std::forward<Args>(args)...);
842 auto *ret = decl.get();
constexpr mlir::tblgen::Method::Properties operator|(mlir::tblgen::Method::Properties lhs, mlir::tblgen::Method::Properties rhs)
The OR of two method properties should return method properties.
constexpr mlir::tblgen::Method::Properties & operator|=(mlir::tblgen::Method::Properties &lhs, mlir::tblgen::Method::Properties rhs)
union mlir::linalg::@1224::ArityGroupAndKind::Kind kind
raw_ostream subclass that simplifies indention a sequence of code.
raw_indented_ostream & unindent()
Decreases the indent and returning this raw_indented_ostream.
DelimitedScope scope(StringRef open="", StringRef close="", bool indent=true)
Returns DelimitedScope.
raw_indented_ostream & indent()
Increases the indent and returning this raw_indented_ostream.
Base class for class declarations.
static bool classof(const ClassDeclaration *other)
A class declaration is a class element that appears as part of its declaration.
virtual ~ClassDeclaration()=default
ClassDeclaration(Kind kind)
Create a class declaration with a given kind.
virtual void writeDeclTo(raw_indented_ostream &os) const =0
Write the declaration.
Kind getKind() const
Get the class declaration kind.
Kind
Kinds for LLVM-style RTTI.
virtual void writeDefTo(raw_indented_ostream &os, StringRef namePrefix) const
Write the definition, if any.
A class used to emit C++ classes from Tablegen.
Method * addInlineMethod(RetTypeT &&retType, NameT &&name, Args &&...args)
Add an inline method.
Class(const Class &)=delete
Explicitly delete the copy constructor.
Method * declareStaticMethod(RetTypeT &&retType, NameT &&name, Args &&...args)
Add a declaration for a static method.
SetVector< std::string, SmallVector< std::string >, StringSet<> > templateParams
An optional list of class template parameters.
Method * addMethod(RetTypeT &&retType, NameT &&name, Args &&...args)
Add a method with statically-known properties.
std::vector< std::unique_ptr< ClassDeclaration > > declarations
A list of declarations in the class, emitted in order.
DeclT * declare(Args &&...args)
Add a declaration.
Method * declareMethod(RetTypeT &&retType, NameT &&name, Args &&...args)
Add a declaration for a method.
ParentClass & addParent(ParentClass parent)
Add a parent class.
std::vector< std::unique_ptr< Method > > methods
The pending list of methods and constructors.
void writeDeclTo(raw_indented_ostream &os) const
Write the declaration of this class, all declarations, and definitions of inline functions.
SmallVector< Field > fields
The pending list of private class fields.
Method * addStaticInlineMethod(RetTypeT &&retType, NameT &&name, Args &&...args)
Add an inline static method.
virtual void finalize()
The declaration of a class needs to be "finalized".
void writeDefTo(raw_ostream &rawOs) const
Write the definitions of thiss class's out-of-line constructors and methods.
Constructor * addConstructorAndPrune(Constructor &&newCtor)
Add a new constructor if it is not made redundant by any existing constructors and prune and existing...
Visibility getLastVisibilityDecl() const
Get the last visibility declaration.
void addTemplateParam(ParamT param)
Add a template parameter.
void addField(TypeT &&type, NameT &&name)
Add a new field to the class.
Method * addMethod(RetTypeT &&retType, NameT &&name, Method::Properties properties, ArrayRef< MethodParameter > parameters)
Add a new method to this class and prune any methods made redundant by it.
SmallVector< ParentClass > parents
The list of parent classes.
Class(NameT &&name, bool isStruct=false)
Create a class with a name, and whether it should be declared as a class or struct.
Method * addConstMethod(RetTypeT &&retType, NameT &&name, Args &&...args)
Add a const method.
std::string className
The C++ class name.
Constructor * addConstructor(Args &&...args)
Add a new constructor to this class and prune and constructors made redundant by it.
bool isStruct
Whether this is a class or a struct.
Method * addMethod(RetTypeT &&retType, NameT &&name, Method::Properties properties, Args &&...args)
void writeDeclTo(raw_ostream &rawOs) const
Write the declaration of this class, all declarations, and definitions of inline functions.
Method * addMethod(RetTypeT &&retType, NameT &&name, ArrayRef< MethodParameter > parameters)
Add a method with statically-known properties.
Method * addStaticMethod(RetTypeT &&retType, NameT &&name, Args &&...args)
Add a static method.
void writeDefTo(raw_indented_ostream &os) const
Write the definitions of thiss class's out-of-line constructors and methods.
Method * addMethodAndPrune(Method &&newMethod)
Add a new method if it is not made redundant by any existing methods and prune and existing methods m...
void addTemplateParams(ContainerT &&container)
Add a list of template parameters.
StringRef getClassName() const
Return the C++ name of the class.
Initialization of a class field in a constructor.
void writeTo(raw_indented_ostream &os) const
Write the member initializer.
MemberInitializer(std::string name, std::string value)
Create a member initializer in a constructor that initializes the class field name with value.
void addMemberInitializer(NameT &&name, ValueT &&value)
Add member initializer to constructor initializing name with value.
void writeDefTo(raw_indented_ostream &os, StringRef namePrefix) const override
Write the definition of the constructor if it is not inline.
void writeDeclTo(raw_indented_ostream &os) const override
Write the declaration of the constructor, and its definition if inline.
Constructor(NameT &&className, Properties properties, Args &&...args)
Create a constructor for a given class, with method properties, and parameters specified either as a ...
static bool classof(const ClassDeclaration *other)
Return true if a method is a constructor.
This class describes a class field.
void writeDeclTo(raw_indented_ostream &os) const override
Write the declaration of the field.
Field(TypeT &&type, NameT &&name)
Create a class field with a type and variable name.
This class contains the body of a C++ method.
void writeTo(raw_indented_ostream &os) const
Write the method body to the output stream.
MethodBody & unindent()
Unindent the output stream.
MethodBody(MethodBody &&other)
Define a move constructor to correctly initialize the streams.
MethodBody & operator<<(ValueT &&value)
Write a value to the method body.
MethodBody(bool declOnly)
Create a method body, indicating whether it should be elided for methods that are declaration-only.
MethodBody & indent()
Indent the output stream.
MethodBody & operator=(MethodBody &&body)
Define a move assignment operator.
raw_indented_ostream & getStream()
Get the underlying indented output stream.
raw_indented_ostream::DelimitedScope scope(StringRef open="", StringRef close="", bool indent=false)
Create a delimited scope: immediately print open, indent if indent is true, and print close on object...
This class contains a single method parameter for a C++ function.
bool isOptional() const
Returns true if the parameter is optional.
StringRef getName() const
Get the C++ parameter name.
StringRef getDefaultValue() const
Get the default value.
void writeDefTo(raw_indented_ostream &os) const
Write the parameter as part of a method definition.
bool hasDefaultValue() const
Returns true if the parameter has a default value.
MethodParameter(TypeT &&type, NameT &&name, DefaultT &&defaultValue, bool optional=false)
Create a method parameter with a C++ type, parameter name, and an optional default value.
void writeDeclTo(raw_indented_ostream &os) const
Write the parameter as part of a method declaration.
MethodParameter(TypeT &&type, NameT &&name, bool optional=false)
Create a method parameter with a C++ type, parameter name, and no default value.
StringRef getType() const
Get the C++ type.
This class contains a list of method parameters for constructor, class methods, and method signatures...
void writeDefTo(raw_indented_ostream &os) const
Write the parameters as part of a method definition.
MethodParameters(SmallVector< MethodParameter > parameters)
MethodParameters(std::initializer_list< MethodParameter > parameters)
Create a list of method parameters.
bool subsumes(const MethodParameters &other) const
Determine whether this list of parameters "subsumes" another, which occurs when this parameter list i...
unsigned getNumParameters() const
Return the number of parameters.
void writeDeclTo(raw_indented_ostream &os) const
Write the parameters as part of a method declaration.
This class contains the signature of a C++ method, including the return type.
MethodSignature(RetTypeT &&retType, NameT &&name, SmallVector< MethodParameter > &¶meters)
Create a method signature with a return type, a method name, and a list of parameters.
bool makesRedundant(const MethodSignature &other) const
Determine whether a method with this signature makes a method with other signature redundant.
void writeDeclTo(raw_indented_ostream &os) const
Write the signature as part of a method declaration.
MethodSignature(RetTypeT &&retType, NameT &&name, ArrayRef< MethodParameter > parameters)
Create a method signature with a return type, a method name, and a list of parameters.
unsigned getNumParameters() const
Get the number of parameters.
StringRef getName() const
Get the name of the method.
void writeTemplateParamsTo(raw_indented_ostream &os) const
Write the template parameters of the signature.
void addTemplateParam(ParamT param)
Add a template parameter.
MethodSignature(RetTypeT &&retType, NameT &&name, Parameters &&...parameters)
Create a method signature with a return type, a method name, and a variadic list of parameters.
void addTemplateParams(ContainerT &&container)
Add a list of template parameters.
StringRef getReturnType() const
Get the return type of the method.
void writeDefTo(raw_indented_ostream &os, StringRef namePrefix) const
Write the signature as part of a method definition.
Class for holding an op's method for C++ code emission.
void addTemplateParam(ParamT param)
Add a template parameter.
Method(RetTypeT &&retType, NameT &&name, Properties properties, Args &&...args)
Create a method with a return type, a name, method properties, and a some parameters.
bool isStatic() const
Returns true if this is a static method.
MethodBody methodBody
The body of the method, if it has one.
static bool methodPropertiesAreCompatible(Properties properties)
Utility method to verify method properties correctness.
MethodSignature methodSignature
The signature of the method.
bool makesRedundant(const Method &other) const
Returns if this method makes the other method redundant.
Properties
Properties (qualifiers) of class methods.
void writeDefTo(raw_indented_ostream &os, StringRef namePrefix) const override
Write the method definition. This is a no-op for inline methods.
void writeDeclTo(raw_indented_ostream &os) const override
Write the method declaration, including the definition if inline.
Properties properties
A collection of method properties.
std::optional< std::string > deprecationMessage
Deprecation message if the method is deprecated.
MethodBody & body()
Get the method body.
bool isPrivate() const
Returns true if this is a private method.
StringRef getReturnType() const
Returns the return type of this method.
void setDeprecated(std::optional< StringRef > message)
Sets or removes the deprecation message of the method.
Method(Method &&)=default
bool isInline() const
Returns true if this is an inline method.
StringRef getName() const
Returns the name of this method.
Method(StringRef retType, StringRef name, Properties properties, std::initializer_list< MethodParameter > params)
Create a method with a return type, a name, method properties, and a list of parameters.
bool isConstructor() const
Returns true if this is a constructor.
void addTemplateParams(ContainerT &&container)
Add a list of template parameters.
Method & operator=(Method &&)=default
bool isConst() const
Returns true if this class method is const.
This class describes a C++ parent class declaration.
void writeTo(raw_indented_ostream &os) const
Write the parent class declaration.
ParentClass(NameT &&name, Visibility visibility=Visibility::Public)
Create a parent class with a class name and visibility.
void addTemplateParams(ContainerT &&container)
Add a list of template parameters.
void addTemplateParam(ParamT param)
Add a template parameter.
This class describes a using-declaration for a class.
void writeDeclTo(raw_indented_ostream &os) const override
Write the using declaration.
void addTemplateParams(ContainerT &&container)
Add a list of template parameters.
void addTemplateParam(ParamT param)
Add a template parameter.
UsingDeclaration(NameT &&name, ValueT &&value="")
Create a using declaration that either aliases name to value or inherits the parent methods `name.
A declaration for the visibility of subsequent declarations.
VisibilityDeclaration(Visibility visibility)
Create a declaration for the given visibility.
Visibility getVisibility() const
Get the visibility.
void writeDeclTo(raw_indented_ostream &os) const override
Write the visibility declaration.
Visibility
This enum describes C++ inheritance visibility.
llvm::raw_ostream & operator<<(llvm::raw_ostream &os, mlir::tblgen::Visibility visibility)
Write "public", "protected", or "private".
std::string stringify(T &&t)
Generically convert a value to a std::string.
Include the generated interface declarations.
Simple RAII struct to use to indentation around entering/exiting region.