|
| 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" |
| 18 | +#include "mlir/TableGen/Attribute.h" |
| 19 | +#include "llvm/ADT/StringRef.h" |
| 20 | + |
| 21 | +namespace llvm { |
| 22 | +class DefInit; |
| 23 | +class Record; |
| 24 | +} // namespace llvm |
| 25 | + |
| 26 | +namespace mlir::tblgen { |
| 27 | + |
| 28 | +// Wrapper class providing around enum cases defined in TableGen. |
| 29 | +class EnumCase { |
| 30 | +public: |
| 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 | + |
| 46 | +protected: |
| 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. |
| 54 | +class EnumInfo { |
| 55 | +public: |
| 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 underlying type. |
| 88 | + StringRef getUnderlyingType() const; |
| 89 | + |
| 90 | + // Returns the name of the utility function that converts a value of the |
| 91 | + // underlying type to the corresponding symbol. |
| 92 | + StringRef getUnderlyingToSymbolFnName() const; |
| 93 | + |
| 94 | + // Returns the name of the utility function that converts a string to the |
| 95 | + // corresponding symbol. |
| 96 | + StringRef getStringToSymbolFnName() const; |
| 97 | + |
| 98 | + // Returns the name of the utility function that converts a symbol to the |
| 99 | + // corresponding string. |
| 100 | + StringRef getSymbolToStringFnName() const; |
| 101 | + |
| 102 | + // Returns the return type of the utility function that converts a symbol to |
| 103 | + // the corresponding string. |
| 104 | + StringRef getSymbolToStringFnRetType() const; |
| 105 | + |
| 106 | + // Returns the name of the utilit function that returns the max enum value |
| 107 | + // used within the enum class. |
| 108 | + StringRef getMaxEnumValFnName() const; |
| 109 | + |
| 110 | + // Returns all allowed cases for this enum attribute. |
| 111 | + std::vector<EnumCase> getAllCases() const; |
| 112 | + |
| 113 | + // Only applicable for enum attributes. |
| 114 | + |
| 115 | + bool genSpecializedAttr() const; |
| 116 | + const llvm::Record *getBaseAttrClass() const; |
| 117 | + StringRef getSpecializedAttrClassName() const; |
| 118 | + |
| 119 | + // Only applicable for bit enums. |
| 120 | + |
| 121 | + bool printBitEnumPrimaryGroups() const; |
| 122 | + |
| 123 | + // Returns the TableGen definition this EnumAttrCase was constructed from. |
| 124 | + const llvm::Record &getDef() const; |
| 125 | + |
| 126 | +protected: |
| 127 | + // The TableGen definition of this constraint. |
| 128 | + const llvm::Record *def; |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace mlir::tblgen |
| 132 | + |
| 133 | +#endif |
0 commit comments