MLIR 22.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 the enum class name.
76 StringRef getEnumClassName() const;
77
78 // Returns the C++ namespaces this enum class should be placed in.
79 StringRef getCppNamespace() const;
80
81 // Returns the summary of the enum.
82 StringRef getSummary() const;
83
84 // Returns the description of the enum.
85 StringRef getDescription() const;
86
87 // Returns the bitwidth of the enum.
88 int64_t getBitwidth() const;
89
90 // Returns the underlying type.
91 StringRef getUnderlyingType() const;
92
93 // Returns the name of the utility function that converts a value of the
94 // underlying type to the corresponding symbol.
95 StringRef getUnderlyingToSymbolFnName() const;
96
97 // Returns the name of the utility function that converts a string to the
98 // corresponding symbol.
99 StringRef getStringToSymbolFnName() const;
100
101 // Returns the name of the utility function that converts a symbol to the
102 // corresponding string.
103 StringRef getSymbolToStringFnName() const;
104
105 // Returns the return type of the utility function that converts a symbol to
106 // the corresponding string.
107 StringRef getSymbolToStringFnRetType() const;
108
109 // Returns the name of the utilit function that returns the max enum value
110 // used within the enum class.
111 StringRef getMaxEnumValFnName() const;
112
113 // Returns all allowed cases for this enum attribute.
114 std::vector<EnumCase> getAllCases() const;
115
116 // Only applicable for enum attributes.
117
118 bool genSpecializedAttr() const;
119 const llvm::Record *getBaseAttrClass() const;
120 StringRef getSpecializedAttrClassName() const;
121
122 // Only applicable for bit enums.
123
124 bool printBitEnumPrimaryGroups() const;
125 bool printBitEnumQuoted() const;
126
127 // Returns the TableGen definition this EnumAttrCase was constructed from.
128 const llvm::Record &getDef() const;
129
130protected:
131 // The TableGen definition of this constraint.
132 const llvm::Record *def;
133};
134
135} // namespace mlir::tblgen
136
137#endif
int64_t getValue() const
Definition EnumInfo.cpp:33
const llvm::Record * def
Definition EnumInfo.h:48
StringRef getSymbol() const
Definition EnumInfo.cpp:27
const llvm::Record & getDef() const
Definition EnumInfo.cpp:35
StringRef getStr() const
Definition EnumInfo.cpp:31
EnumCase(const llvm::Record *record)
EnumCase(const llvm::DefInit *init)
EnumInfo(const llvm::Record &record)
StringRef getSymbolToStringFnName() const
Definition EnumInfo.cpp:90
bool genSpecializedAttr() const
Definition EnumInfo.cpp:115
const llvm::Record * getBaseAttrClass() const
Definition EnumInfo.cpp:120
std::vector< EnumCase > getAllCases() const
Definition EnumInfo.cpp:102
EnumInfo(const llvm::Record *record)
bool printBitEnumPrimaryGroups() const
Definition EnumInfo.cpp:128
int64_t getBitwidth() const
Definition EnumInfo.cpp:76
bool isSubClassOf(StringRef className) const
Definition EnumInfo.cpp:46
StringRef getMaxEnumValFnName() const
Definition EnumInfo.cpp:98
const llvm::Record * def
Definition EnumInfo.h:132
EnumInfo(const llvm::DefInit *init)
const llvm::Record & getDef() const
Definition EnumInfo.cpp:136
StringRef getUnderlyingToSymbolFnName() const
Definition EnumInfo.cpp:82
StringRef getUnderlyingType() const
Definition EnumInfo.cpp:78
bool isBitEnum() const
Definition EnumInfo.cpp:58
bool printBitEnumQuoted() const
Definition EnumInfo.cpp:132
StringRef getStringToSymbolFnName() const
Definition EnumInfo.cpp:86
bool isEnumAttr() const
Definition EnumInfo.cpp:50
StringRef getCppNamespace() const
Definition EnumInfo.cpp:72
StringRef getSummary() const
Definition EnumInfo.cpp:64
StringRef getSpecializedAttrClassName() const
Definition EnumInfo.cpp:124
StringRef getEnumClassName() const
Definition EnumInfo.cpp:60
StringRef getDescription() const
Definition EnumInfo.cpp:68
StringRef getSymbolToStringFnRetType() const
Definition EnumInfo.cpp:94
std::optional< Attribute > asEnumAttr() const
Definition EnumInfo.cpp:52
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229