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; }
82 std::string defaultValue;
93 : parameters(parameters) {}
95 : parameters(std::move(parameters)) {}
121 template <
typename RetTypeT,
typename NameT>
124 : returnType(
stringify(std::forward<RetTypeT>(retType))),
125 methodName(
stringify(std::forward<NameT>(name))),
126 parameters(std::move(parameters)) {}
129 template <
typename RetTypeT,
typename NameT>
133 std::forward<NameT>(name),
137 template <
typename RetTypeT,
typename NameT,
typename... Parameters>
140 std::forward<NameT>(name),
142 {std::forward<Parameters>(parameters)...})) {}
154 StringRef
getName()
const {
return methodName; }
174 template <
typename ParamT>
176 templateParams.push_back(
stringify(param));
180 template <
typename ContainerT>
182 templateParams.insert(std::begin(container), std::end(container));
187 std::string returnType;
189 std::string methodName;
205 : declOnly(other.declOnly), body(std::move(other.body)), stringOs(body),
216 template <
typename ValueT>
219 os << std::forward<ValueT>(value);
242 scope(StringRef open =
"", StringRef close =
"",
bool indent =
false) {
255 llvm::raw_string_ostream stringOs;
286 StringRef namePrefix)
const {}
294 template <ClassDeclaration::Kind DeclKind>
301 return other->
getKind() == DeclKind;
329 template <
typename RetTypeT,
typename NameT,
typename... Args>
334 std::forward<NameT>(name), std::forward<Args>(args)...),
339 std::initializer_list<MethodParameter> params)
386 StringRef namePrefix)
const override;
389 template <
typename ParamT>
393 template <
typename ContainerT>
419 template <
typename NameT,
typename... Args>
422 std::forward<Args>(args)...) {}
425 template <
typename NameT,
typename ValueT>
427 initializers.emplace_back(
stringify(std::forward<NameT>(name)),
436 StringRef namePrefix)
const override;
440 return isa<Method>(other) && cast<Method>(other)->isConstructor();
449 : name(std::move(name)), value(std::move(value)) {}
475 static_cast<unsigned>(rhs));
482 static_cast<unsigned>(rhs));
488 template <
typename ParamT>
495 template <
typename ContainerT>
506 template <
typename NameT>
508 : name(
stringify(std::forward<NameT>(name))), visibility(visibility) {}
511 template <
typename ParamT>
516 template <
typename ContainerT>
518 templateParams.insert(std::begin(container), std::end(container));
543 template <
typename NameT,
typename ValueT = std::
string>
545 : name(
stringify(std::forward<NameT>(name))),
546 value(
stringify(std::forward<ValueT>(value))) {}
552 template <
typename ParamT>
558 template <
typename ContainerT>
560 templateParams.insert(std::begin(container), std::end(container));
578 template <
typename TypeT,
typename NameT>
580 : type(
stringify(std::forward<TypeT>(type))),
581 name(
stringify(std::forward<NameT>(name))) {}
619 std::string extraClassDefinition =
"")
621 std::move(extraClassDefinition)) {}
624 std::string extraClassDefinition =
"")
625 : extraClassDeclaration(extraClassDeclaration),
626 extraClassDefinition(extraClassDefinition) {}
633 StringRef namePrefix)
const override;
638 std::string extraClassDeclaration;
641 std::string extraClassDefinition;
657 template <
typename NameT,
658 typename = std::enable_if_t<!std::is_same<NameT, Class>::value>>
673 Properties | defaultProperties,
674 std::forward<Args>(args)...));
689 std::forward<NameT>(name),
690 Properties | properties, parameters));
698 return addMethod(std::forward<RetTypeT>(retType), std::forward<NameT>(name),
699 Properties, parameters);
703 typename NameT,
typename... Args>
706 return addMethod(std::forward<RetTypeT>(retType), std::forward<NameT>(name),
707 properties | Properties, {std::forward<Args>(args)...});
712 typename NameT,
typename... Args>
714 return addMethod(std::forward<RetTypeT>(retType), std::forward<NameT>(name),
715 Properties, std::forward<Args>(args)...);
720 typename NameT,
typename... Args>
722 return addMethod<Properties | Method::Static>(
723 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
724 std::forward<Args>(args)...);
729 typename NameT,
typename... Args>
732 return addMethod<Properties | Method::StaticInline>(
733 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
734 std::forward<Args>(args)...);
739 typename NameT,
typename... Args>
741 return addMethod<Properties | Method::Inline>(
742 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
743 std::forward<Args>(args)...);
748 typename NameT,
typename... Args>
750 return addMethod<Properties | Method::Const>(
751 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
752 std::forward<Args>(args)...);
757 typename NameT,
typename... Args>
759 return addMethod<Properties | Method::Declaration>(
760 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
761 std::forward<Args>(args)...);
766 typename NameT,
typename... Args>
769 return addMethod<Properties | Method::StaticDeclaration>(
770 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
771 std::forward<Args>(args)...);
776 template <
typename TypeT,
typename NameT>
778 fields.emplace_back(std::forward<TypeT>(type), std::forward<NameT>(name));
785 template <
typename ParamT>
791 template <
typename ContainerT>
793 templateParams.insert(std::begin(container), std::end(container));
821 template <
typename DeclT,
typename... Args>
823 auto decl = std::make_unique<DeclT>(std::forward<Args>(args)...);
824 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)
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.
StringRef getName() const
Get the C++ parameter name.
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.
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.