1 //===-- TypeDef.h - Record wrapper for type definitions ---------*- 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 // TypeDef wrapper to simplify using TableGen Record defining a MLIR type. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef MLIR_TABLEGEN_TYPEDEF_H 14 #define MLIR_TABLEGEN_TYPEDEF_H 15 16 #include "mlir/Support/LLVM.h" 17 #include "mlir/TableGen/Dialect.h" 18 19 namespace llvm { 20 class Record; 21 class DagInit; 22 class SMLoc; 23 } // namespace llvm 24 25 namespace mlir { 26 namespace tblgen { 27 28 class TypeParameter; 29 30 /// Wrapper class that contains a TableGen TypeDef's record and provides helper 31 /// methods for accessing them. 32 class TypeDef { 33 public: TypeDef(const llvm::Record * def)34 explicit TypeDef(const llvm::Record *def) : def(def) {} 35 36 // Get the dialect for which this type belongs. 37 Dialect getDialect() const; 38 39 // Returns the name of this TypeDef record. 40 StringRef getName() const; 41 42 // Query functions for the documentation of the operator. 43 bool hasDescription() const; 44 StringRef getDescription() const; 45 bool hasSummary() const; 46 StringRef getSummary() const; 47 48 // Returns the name of the C++ class to generate. 49 StringRef getCppClassName() const; 50 51 // Returns the name of the storage class for this type. 52 StringRef getStorageClassName() const; 53 54 // Returns the C++ namespace for this types storage class. 55 StringRef getStorageNamespace() const; 56 57 // Returns true if we should generate the storage class. 58 bool genStorageClass() const; 59 60 // Indicates whether or not to generate the storage class constructor. 61 bool hasStorageCustomConstructor() const; 62 63 // Fill a list with this types parameters. See TypeDef in OpBase.td for 64 // documentation of parameter usage. 65 void getParameters(SmallVectorImpl<TypeParameter> &) const; 66 // Return the number of type parameters 67 unsigned getNumParameters() const; 68 69 // Return the keyword/mnemonic to use in the printer/parser methods if we are 70 // supposed to auto-generate them. 71 Optional<StringRef> getMnemonic() const; 72 73 // Returns the code to use as the types printer method. If not specified, 74 // return a non-value. Otherwise, return the contents of that code block. 75 Optional<StringRef> getPrinterCode() const; 76 77 // Returns the code to use as the types parser method. If not specified, 78 // return a non-value. Otherwise, return the contents of that code block. 79 Optional<StringRef> getParserCode() const; 80 81 // Returns true if the accessors based on the types parameters should be 82 // generated. 83 bool genAccessors() const; 84 85 // Return true if we need to generate the verifyConstructionInvariants 86 // declaration and getChecked method. 87 bool genVerifyInvariantsDecl() const; 88 89 // Returns the dialects extra class declaration code. 90 Optional<StringRef> getExtraDecls() const; 91 92 // Get the code location (for error printing). 93 ArrayRef<llvm::SMLoc> getLoc() const; 94 95 // Returns whether two TypeDefs are equal by checking the equality of the 96 // underlying record. 97 bool operator==(const TypeDef &other) const; 98 99 // Compares two TypeDefs by comparing the names of the dialects. 100 bool operator<(const TypeDef &other) const; 101 102 // Returns whether the TypeDef is defined. 103 operator bool() const { return def != nullptr; } 104 105 private: 106 const llvm::Record *def; 107 }; 108 109 // A wrapper class for tblgen TypeParameter, arrays of which belong to TypeDefs 110 // to parameterize them. 111 class TypeParameter { 112 public: TypeParameter(const llvm::DagInit * def,unsigned num)113 explicit TypeParameter(const llvm::DagInit *def, unsigned num) 114 : def(def), num(num) {} 115 116 // Get the parameter name. 117 StringRef getName() const; 118 // If specified, get the custom allocator code for this parameter. 119 llvm::Optional<StringRef> getAllocator() const; 120 // Get the C++ type of this parameter. 121 StringRef getCppType() const; 122 // Get a description of this parameter for documentation purposes. 123 llvm::Optional<StringRef> getDescription() const; 124 // Get the assembly syntax documentation. 125 StringRef getSyntax() const; 126 127 private: 128 const llvm::DagInit *def; 129 const unsigned num; 130 }; 131 132 } // end namespace tblgen 133 } // end namespace mlir 134 135 #endif // MLIR_TABLEGEN_TYPEDEF_H 136