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) {
247 return os.scope(open, close,
indent);
259 llvm::raw_string_ostream stringOs;
290 StringRef namePrefix)
const {}
298template <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));
506template <
typename ParamT>
513template <
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>
571 void addTemplateParam(ParamT param) {
576 template <
typename ContainerT>
577 void addTemplateParams(ContainerT &&container) {
578 templateParams.insert(std::begin(container), std::end(container));
596 template <
typename TypeT,
typename NameT>
597 Field(TypeT &&type, NameT &&name)
598 : type(
stringify(std::forward<TypeT>(type))),
599 name(
stringify(std::forward<NameT>(name))) {}
612class VisibilityDeclaration
613 :
public ClassDeclarationBase<ClassDeclaration::VisibilityDeclaration> {
616 VisibilityDeclaration(Visibility visibility) : visibility(visibility) {}
619 Visibility getVisibility()
const {
return visibility; }
622 void writeDeclTo(raw_indented_ostream &os)
const override;
632class ExtraClassDeclaration
636 ExtraClassDeclaration(StringRef extraClassDeclaration,
637 std::string extraClassDefinition =
"")
638 : ExtraClassDeclaration(extraClassDeclaration.str(),
639 std::move(extraClassDefinition)) {}
641 ExtraClassDeclaration(std::string extraClassDeclaration,
642 std::string extraClassDefinition =
"")
643 : extraClassDeclaration(extraClassDeclaration),
644 extraClassDefinition(extraClassDefinition) {}
647 void writeDeclTo(raw_indented_ostream &os)
const override;
650 void writeDefTo(raw_indented_ostream &os,
651 StringRef namePrefix)
const override;
656 std::string extraClassDeclaration;
659 std::string extraClassDefinition;
666 virtual ~Class() =
default;
670 Class(
const Class &) =
delete;
675 template <
typename NameT,
676 typename = std::enable_if_t<!std::is_same<NameT, Class>::value>>
677 Class(NameT &&name,
bool isStruct =
false)
678 : className(
stringify(std::forward<NameT>(name))), isStruct(isStruct) {}
683 template <Method::Properties Properties = Method::None,
typename... Args>
684 Constructor *addConstructor(Args &&...args) {
685 Method::Properties defaultProperties = Method::Constructor;
688 if (!templateParams.empty())
689 defaultProperties |= Method::Inline;
690 return addConstructorAndPrune(Constructor(getClassName(),
691 Properties | defaultProperties,
692 std::forward<Args>(args)...));
698 template <Method::Properties Properties = Method::None,
typename RetTypeT,
700 Method *addMethod(RetTypeT &&retType, NameT &&name,
701 Method::Properties properties,
702 ArrayRef<MethodParameter> parameters) {
704 if (!templateParams.empty())
705 properties |= Method::Inline;
706 return addMethodAndPrune(Method(std::forward<RetTypeT>(retType),
707 std::forward<NameT>(name),
708 Properties | properties, parameters));
712 template <Method::Properties Properties = Method::None,
typename RetTypeT,
714 Method *addMethod(RetTypeT &&retType, NameT &&name,
715 ArrayRef<MethodParameter> parameters) {
716 return addMethod(std::forward<RetTypeT>(retType), std::forward<NameT>(name),
717 Properties, parameters);
720 template <Method::Properties Properties = Method::None,
typename RetTypeT,
721 typename NameT,
typename... Args>
722 Method *addMethod(RetTypeT &&retType, NameT &&name,
723 Method::Properties properties, Args &&...args) {
724 return addMethod(std::forward<RetTypeT>(retType), std::forward<NameT>(name),
725 properties | Properties, {std::forward<Args>(args)...});
729 template <Method::Properties Properties = Method::None,
typename RetTypeT,
730 typename NameT,
typename... Args>
731 Method *addMethod(RetTypeT &&retType, NameT &&name, Args &&...args) {
732 return addMethod(std::forward<RetTypeT>(retType), std::forward<NameT>(name),
733 Properties, std::forward<Args>(args)...);
737 template <Method::Properties Properties = Method::None,
typename RetTypeT,
738 typename NameT,
typename... Args>
739 Method *addStaticMethod(RetTypeT &&retType, NameT &&name, Args &&...args) {
740 return addMethod<Properties | Method::Static>(
741 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
742 std::forward<Args>(args)...);
746 template <Method::Properties Properties = Method::None,
typename RetTypeT,
747 typename NameT,
typename... Args>
748 Method *addStaticInlineMethod(RetTypeT &&retType, NameT &&name,
750 return addMethod<Properties | Method::StaticInline>(
751 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
752 std::forward<Args>(args)...);
756 template <Method::Properties Properties = Method::None,
typename RetTypeT,
757 typename NameT,
typename... Args>
758 Method *addInlineMethod(RetTypeT &&retType, NameT &&name, Args &&...args) {
759 return addMethod<Properties | Method::Inline>(
760 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
761 std::forward<Args>(args)...);
765 template <Method::Properties Properties = Method::None,
typename RetTypeT,
766 typename NameT,
typename... Args>
767 Method *addConstMethod(RetTypeT &&retType, NameT &&name, Args &&...args) {
768 return addMethod<Properties | Method::Const>(
769 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
770 std::forward<Args>(args)...);
774 template <Method::Properties Properties = Method::None,
typename RetTypeT,
775 typename NameT,
typename... Args>
776 Method *declareMethod(RetTypeT &&retType, NameT &&name, Args &&...args) {
777 return addMethod<Properties | Method::Declaration>(
778 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
779 std::forward<Args>(args)...);
783 template <Method::Properties Properties = Method::None,
typename RetTypeT,
784 typename NameT,
typename... Args>
785 Method *declareStaticMethod(RetTypeT &&retType, NameT &&name,
787 return addMethod<Properties | Method::StaticDeclaration>(
788 std::forward<RetTypeT>(retType), std::forward<NameT>(name),
789 std::forward<Args>(args)...);
792 const std::vector<std::unique_ptr<Method>> &getMethods()
const {
798 template <
typename TypeT,
typename NameT>
799 void addField(TypeT &&type, NameT &&name) {
800 fields.emplace_back(std::forward<TypeT>(type), std::forward<NameT>(name));
804 ParentClass &addParent(ParentClass parent);
807 template <
typename ParamT>
808 void addTemplateParam(ParamT param) {
813 template <
typename ContainerT>
814 void addTemplateParams(ContainerT &&container) {
815 templateParams.insert(std::begin(container), std::end(container));
819 StringRef getClassName()
const {
return className; }
823 void writeDeclTo(raw_ostream &rawOs)
const {
824 raw_indented_ostream os(rawOs);
829 void writeDefTo(raw_ostream &rawOs)
const {
830 raw_indented_ostream os(rawOs);
836 void writeDeclTo(raw_indented_ostream &os)
const;
839 void writeDefTo(raw_indented_ostream &os)
const;
843 template <
typename DeclT,
typename... Args>
844 DeclT *declare(Args &&...args) {
845 auto decl = std::make_unique<DeclT>(std::forward<Args>(args)...);
846 auto *ret = decl.get();
847 declarations.push_back(std::move(decl));
869 virtual void finalize();
874 Constructor *addConstructorAndPrune(Constructor &&newCtor);
877 Method *addMethodAndPrune(Method &&newMethod);
883 std::string className;
885 SmallVector<ParentClass> parents;
887 std::vector<std::unique_ptr<Method>> methods;
889 SmallVector<Field> fields;
constexpr mlir::tblgen::Method::Properties & operator|=(mlir::tblgen::Method::Properties &lhs, mlir::tblgen::Method::Properties rhs)
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.
raw_ostream subclass that simplifies indention a sequence of code.
Base class for class declarations.
static bool classof(const ClassDeclaration *other)
ClassDeclarationBase< DeclKind > Base
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.
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 writeDeclTo(raw_indented_ostream &os) const override
Write the declaration of the constructor, and its definition if inline.
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.
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 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.
raw_indented_ostream & getStream()
Get the underlying indented output stream.
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::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...
MethodBody & operator<<(ValueT &&value)
Write a value to the method body.
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.
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.
Method & operator=(Method &&)=default
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.
MethodBody & body()
Get the method body.
Properties
Properties (qualifiers) of class 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.
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.
void writeDefTo(raw_indented_ostream &os, StringRef namePrefix) const override
Write the method definition. This is a no-op for inline methods.
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.
static bool methodPropertiesAreCompatible(Properties properties)
Utility method to verify method properties correctness.
bool isConst() const
Returns true if this class method is const.
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 writeTo(raw_indented_ostream &os) const
Write the parent class declaration.
void addTemplateParam(ParamT param)
Add a template parameter.
This class describes a using-declaration for a class.
bool isStruct
Create a using declaration that either aliases name to value or inherits the parent methods `name.
SetVector< std::string, SmallVector< std::string >, StringSet<> > templateParams
An optional list of class template parameters.
std::vector< std::unique_ptr< ClassDeclaration > > declarations
A list of declarations in the class, emitted in order.
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.
llvm::SetVector< T, Vector, Set, N > SetVector
llvm::StringSet< AllocatorTy > StringSet
Simple RAII struct to use to indentation around entering/exiting region.