MLIR 24.0.0git
EnumInfo.h
Go to the documentation of this file.
1//===- EnumInfo.h - EnumInfo wrapper class --------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// EnumInfo wrapper to simplify using a TableGen Record defining an Enum
10// via EnumInfo and its `EnumCase`s.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef MLIR_TABLEGEN_ENUMINFO_H_
15#define MLIR_TABLEGEN_ENUMINFO_H_
16
17#include "mlir/Support/LLVM.h"
19#include "llvm/ADT/StringRef.h"
20
21namespace llvm {
22class DefInit;
23class Record;
24} // namespace llvm
25
26namespace mlir::tblgen {
27
28// Wrapper class providing around enum cases defined in TableGen.
29class EnumCase {
30public:
31 explicit EnumCase(const llvm::Record *record);
32 explicit EnumCase(const llvm::DefInit *init);
33
34 // Returns the symbol of this enum attribute case.
35 StringRef getSymbol() const;
36
37 // Returns the textual representation of this enum attribute case.
38 StringRef getStr() const;
39
40 // Returns the value of this enum attribute case.
41 int64_t getValue() const;
42
43 // Returns the TableGen definition this EnumAttrCase was constructed from.
44 const llvm::Record &getDef() const;
45
46protected:
47 // The TableGen definition of this constraint.
48 const llvm::Record *def;
49};
50
51// Wrapper class providing helper methods for accessing enums defined
52// in TableGen using EnumInfo. Some methods are only applicable when
53// the enum is also an attribute, or only when it is a bit enum.
54class EnumInfo {
55public:
56 explicit EnumInfo(const llvm::Record *record);
57 explicit EnumInfo(const llvm::Record &record);
58 explicit EnumInfo(const llvm::DefInit *init);
59
60 // Returns true if the given EnumInfo is a subclass of the named TableGen
61 // class.
62 bool isSubClassOf(StringRef className) const;
63
64 // Returns true if this enum is an EnumAttrInfo, thus making it define an
65 // attribute.
66 bool isEnumAttr() const;
67
68 // Create the `Attribute` wrapper around this EnumInfo if it is defining an
69 // attribute.
70 std::optional<Attribute> asEnumAttr() const;
71
72 // Returns true if this is a bit enum.
73 bool isBitEnum() const;
74
75 // Returns true if this is a bit enum whose cases are parsed and printed as
76 // separate, unquoted keywords.
77 bool isNewStyleBitEnum() const;
78
79 // Returns true if a 'FieldParser' and a printer are generated for the enum.
80 bool hasParserPrinter() const;
81
82 // Returns the enum class name.
83 StringRef getEnumClassName() const;
84
85 // Returns the C++ namespaces this enum class should be placed in.
86 StringRef getCppNamespace() const;
87
88 // Returns the fully qualified C++ name of the enum class.
89 std::string getQualifiedName() const;
90
91 // Returns the summary of the enum.
92 StringRef getSummary() const;
93
94 // Returns the description of the enum.
95 StringRef getDescription() const;
96
97 // Returns the bitwidth of the enum.
98 int64_t getBitwidth() const;
99
100 // Returns the underlying type.
101 StringRef getUnderlyingType() const;
102
103 // Returns the name of the utility function that converts a value of the
104 // underlying type to the corresponding symbol.
105 StringRef getUnderlyingToSymbolFnName() const;
106
107 // Returns the name of the utility function that converts a string to the
108 // corresponding symbol.
109 StringRef getStringToSymbolFnName() const;
110
111 // Returns the name of the utility function that converts a symbol to the
112 // corresponding string.
113 StringRef getSymbolToStringFnName() const;
114
115 // Returns the return type of the utility function that converts a symbol to
116 // the corresponding string.
117 StringRef getSymbolToStringFnRetType() const;
118
119 // Returns the name of the utilit function that returns the max enum value
120 // used within the enum class.
121 StringRef getMaxEnumValFnName() const;
122
123 // Returns all allowed cases for this enum attribute.
124 std::vector<EnumCase> getAllCases() const;
125
126 // Only applicable for enum attributes.
127
128 bool genSpecializedAttr() const;
129 const llvm::Record *getBaseAttrClass() const;
130 StringRef getSpecializedAttrClassName() const;
131
132 // Only applicable for bit enums.
133
134 bool printBitEnumPrimaryGroups() const;
135 bool printBitEnumQuoted() const;
136
137 // Returns the string printed between the cases of a bit enum.
138 StringRef getBitEnumSeparator() const;
139
140 // The token that separates the cases of a bit enum, ignoring the spaces
141 // around it. `Space` means the separator is a space " ".
144
145 // Returns the name of the `AsmParser` method that parses the separator
146 // between the cases of a bit enum. Must not be called for a space
147 // separator.
148 StringRef getBitEnumSeparatorParser() const;
149
150 // Returns the TableGen definition this EnumAttrCase was constructed from.
151 const llvm::Record &getDef() const;
152
153protected:
154 // The TableGen definition of this constraint.
155 const llvm::Record *def;
156};
157
158} // namespace mlir::tblgen
159
160#endif
int64_t getValue() const
Definition EnumInfo.cpp:34
const llvm::Record * def
Definition EnumInfo.h:48
StringRef getSymbol() const
Definition EnumInfo.cpp:28
const llvm::Record & getDef() const
Definition EnumInfo.cpp:36
StringRef getStr() const
Definition EnumInfo.cpp:32
EnumCase(const llvm::Record *record)
EnumCase(const llvm::DefInit *init)
bool isNewStyleBitEnum() const
Definition EnumInfo.cpp:61
EnumInfo(const llvm::Record &record)
StringRef getSymbolToStringFnName() const
Definition EnumInfo.cpp:105
bool genSpecializedAttr() const
Definition EnumInfo.cpp:130
const llvm::Record * getBaseAttrClass() const
Definition EnumInfo.cpp:135
BitEnumSeparatorKind getBitEnumSeparatorKind() const
Definition EnumInfo.cpp:155
std::vector< EnumCase > getAllCases() const
Definition EnumInfo.cpp:117
EnumInfo(const llvm::Record *record)
bool printBitEnumPrimaryGroups() const
Definition EnumInfo.cpp:143
int64_t getBitwidth() const
Definition EnumInfo.cpp:91
bool isSubClassOf(StringRef className) const
Definition EnumInfo.cpp:47
StringRef getMaxEnumValFnName() const
Definition EnumInfo.cpp:113
const llvm::Record * def
Definition EnumInfo.h:155
bool hasParserPrinter() const
Definition EnumInfo.cpp:65
EnumInfo(const llvm::DefInit *init)
const llvm::Record & getDef() const
Definition EnumInfo.cpp:182
StringRef getUnderlyingToSymbolFnName() const
Definition EnumInfo.cpp:97
StringRef getUnderlyingType() const
Definition EnumInfo.cpp:93
bool isBitEnum() const
Definition EnumInfo.cpp:59
bool printBitEnumQuoted() const
Definition EnumInfo.cpp:147
StringRef getStringToSymbolFnName() const
Definition EnumInfo.cpp:101
bool isEnumAttr() const
Definition EnumInfo.cpp:51
StringRef getCppNamespace() const
Definition EnumInfo.cpp:83
StringRef getSummary() const
Definition EnumInfo.cpp:75
StringRef getSpecializedAttrClassName() const
Definition EnumInfo.cpp:139
StringRef getEnumClassName() const
Definition EnumInfo.cpp:71
std::string getQualifiedName() const
Definition EnumInfo.cpp:87
StringRef getBitEnumSeparatorParser() const
Definition EnumInfo.cpp:167
StringRef getDescription() const
Definition EnumInfo.cpp:79
StringRef getSymbolToStringFnRetType() const
Definition EnumInfo.cpp:109
StringRef getBitEnumSeparator() const
Definition EnumInfo.cpp:151
std::optional< Attribute > asEnumAttr() const
Definition EnumInfo.cpp:53
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229