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/Builder.h"
18 
19 namespace llvm {
20 class DagInit;
21 class Record;
22 class SMLoc;
23 } // namespace llvm
24 
25 namespace mlir {
26 namespace tblgen {
27 class Dialect;
28 class TypeParameter;
29 
30 //===----------------------------------------------------------------------===//
31 // TypeBuilder
32 //===----------------------------------------------------------------------===//
33 
34 /// Wrapper class that represents a Tablegen TypeBuilder.
35 class TypeBuilder : public Builder {
36 public:
37   using Builder::Builder;
38 
39   /// Return an optional code body used for the `getChecked` variant of this
40   /// builder.
41   Optional<StringRef> getCheckedBody() const;
42 
43   /// Returns true if this builder is able to infer the MLIRContext parameter.
44   bool hasInferredContextParameter() const;
45 };
46 
47 //===----------------------------------------------------------------------===//
48 // TypeDef
49 //===----------------------------------------------------------------------===//
50 
51 /// Wrapper class that contains a TableGen TypeDef's record and provides helper
52 /// methods for accessing them.
53 class TypeDef {
54 public:
55   explicit TypeDef(const llvm::Record *def);
56 
57   // Get the dialect for which this type belongs.
58   Dialect getDialect() const;
59 
60   // Returns the name of this TypeDef record.
61   StringRef getName() const;
62 
63   // Query functions for the documentation of the operator.
64   bool hasDescription() const;
65   StringRef getDescription() const;
66   bool hasSummary() const;
67   StringRef getSummary() const;
68 
69   // Returns the name of the C++ class to generate.
70   StringRef getCppClassName() const;
71 
72   // Returns the name of the C++ base class to use when generating this type.
73   StringRef getCppBaseClassName() const;
74 
75   // Returns the name of the storage class for this type.
76   StringRef getStorageClassName() const;
77 
78   // Returns the C++ namespace for this types storage class.
79   StringRef getStorageNamespace() const;
80 
81   // Returns true if we should generate the storage class.
82   bool genStorageClass() const;
83 
84   // Indicates whether or not to generate the storage class constructor.
85   bool hasStorageCustomConstructor() const;
86 
87   // Fill a list with this types parameters. See TypeDef in OpBase.td for
88   // documentation of parameter usage.
89   void getParameters(SmallVectorImpl<TypeParameter> &) const;
90   // Return the number of type parameters
91   unsigned getNumParameters() const;
92 
93   // Return the keyword/mnemonic to use in the printer/parser methods if we are
94   // supposed to auto-generate them.
95   Optional<StringRef> getMnemonic() const;
96 
97   // Returns the code to use as the types printer method. If not specified,
98   // return a non-value. Otherwise, return the contents of that code block.
99   Optional<StringRef> getPrinterCode() const;
100 
101   // Returns the code to use as the types parser method. If not specified,
102   // return a non-value. Otherwise, return the contents of that code block.
103   Optional<StringRef> getParserCode() const;
104 
105   // Returns true if the accessors based on the types parameters should be
106   // generated.
107   bool genAccessors() const;
108 
109   // Return true if we need to generate the verifyConstructionInvariants
110   // declaration and getChecked method.
111   bool genVerifyInvariantsDecl() const;
112 
113   // Returns the dialects extra class declaration code.
114   Optional<StringRef> getExtraDecls() const;
115 
116   // Get the code location (for error printing).
117   ArrayRef<llvm::SMLoc> getLoc() const;
118 
119   // Returns true if the default get/getChecked methods should be skipped during
120   // generation.
121   bool skipDefaultBuilders() const;
122 
123   // Returns the builders of this type.
getBuilders()124   ArrayRef<TypeBuilder> getBuilders() const { return builders; }
125 
126   // Returns whether two TypeDefs are equal by checking the equality of the
127   // underlying record.
128   bool operator==(const TypeDef &other) const;
129 
130   // Compares two TypeDefs by comparing the names of the dialects.
131   bool operator<(const TypeDef &other) const;
132 
133   // Returns whether the TypeDef is defined.
134   operator bool() const { return def != nullptr; }
135 
136 private:
137   const llvm::Record *def;
138 
139   // The builders of this type definition.
140   SmallVector<TypeBuilder> builders;
141 };
142 
143 //===----------------------------------------------------------------------===//
144 // TypeParameter
145 //===----------------------------------------------------------------------===//
146 
147 // A wrapper class for tblgen TypeParameter, arrays of which belong to TypeDefs
148 // to parameterize them.
149 class TypeParameter {
150 public:
TypeParameter(const llvm::DagInit * def,unsigned num)151   explicit TypeParameter(const llvm::DagInit *def, unsigned num)
152       : def(def), num(num) {}
153 
154   // Get the parameter name.
155   StringRef getName() const;
156   // If specified, get the custom allocator code for this parameter.
157   Optional<StringRef> getAllocator() const;
158   // Get the C++ type of this parameter.
159   StringRef getCppType() const;
160   // Get a description of this parameter for documentation purposes.
161   Optional<StringRef> getSummary() const;
162   // Get the assembly syntax documentation.
163   StringRef getSyntax() const;
164 
165 private:
166   const llvm::DagInit *def;
167   const unsigned num;
168 };
169 
170 } // end namespace tblgen
171 } // end namespace mlir
172 
173 #endif // MLIR_TABLEGEN_TYPEDEF_H
174