xref: /llvm-project/mlir/include/mlir/TableGen/Property.h (revision d8399d5dd6a5a7025621eddd97fc0fa1f494bad8)
1 //===- Property.h - Property 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 // Property wrapper to simplify using TableGen Record defining a MLIR
10 // Property.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TABLEGEN_PROPERTY_H_
15 #define MLIR_TABLEGEN_PROPERTY_H_
16 
17 #include "mlir/Support/LLVM.h"
18 #include "mlir/TableGen/Constraint.h"
19 #include "llvm/ADT/StringRef.h"
20 
21 namespace llvm {
22 class DefInit;
23 class Record;
24 } // namespace llvm
25 
26 namespace mlir {
27 namespace tblgen {
28 class Dialect;
29 class Type;
30 class Pred;
31 
32 // Wrapper class providing helper methods for accessing MLIR Property defined
33 // in TableGen. This class should closely reflect what is defined as class
34 // `Property` in TableGen.
35 class Property {
36 public:
37   explicit Property(const llvm::Record *record);
38   explicit Property(const llvm::DefInit *init);
39   Property(StringRef summary, StringRef description, StringRef storageType,
40            StringRef interfaceType, StringRef convertFromStorageCall,
41            StringRef assignToStorageCall, StringRef convertToAttributeCall,
42            StringRef convertFromAttributeCall, StringRef parserCall,
43            StringRef optionalParserCall, StringRef printerCall,
44            StringRef readFromMlirBytecodeCall,
45            StringRef writeToMlirBytecodeCall, StringRef hashPropertyCall,
46            StringRef defaultValue, StringRef storageTypeValueOverride);
47 
48   // Returns the summary (for error messages) of this property's type.
49   StringRef getSummary() const { return summary; }
50 
51   // Returns the description of this property.
52   StringRef getDescription() const { return description; }
53 
54   // Returns the storage type.
55   StringRef getStorageType() const { return storageType; }
56 
57   // Returns the interface type for this property.
58   StringRef getInterfaceType() const { return interfaceType; }
59 
60   // Returns the template getter method call which reads this property's
61   // storage and returns the value as of the desired return type.
62   StringRef getConvertFromStorageCall() const { return convertFromStorageCall; }
63 
64   // Returns the template setter method call which reads this property's
65   // in the provided interface type and assign it to the storage.
66   StringRef getAssignToStorageCall() const { return assignToStorageCall; }
67 
68   // Returns the conversion method call which reads this property's
69   // in the storage type and builds an attribute.
70   StringRef getConvertToAttributeCall() const { return convertToAttributeCall; }
71 
72   // Returns the setter method call which reads this property's
73   // in the provided interface type and assign it to the storage.
74   StringRef getConvertFromAttributeCall() const {
75     return convertFromAttributeCall;
76   }
77 
78   // Return the property's predicate. Properties that didn't come from
79   // tablegen (the hardcoded ones) have the null predicate.
80   Pred getPredicate() const;
81 
82   // Returns the method call which parses this property from textual MLIR.
83   StringRef getParserCall() const { return parserCall; }
84 
85   // Returns true if this property has defined an optional parser.
86   bool hasOptionalParser() const { return !optionalParserCall.empty(); }
87 
88   // Returns the method call which optionally parses this property from textual
89   // MLIR.
90   StringRef getOptionalParserCall() const { return optionalParserCall; }
91 
92   // Returns the method call which prints this property to textual MLIR.
93   StringRef getPrinterCall() const { return printerCall; }
94 
95   // Returns the method call which reads this property from
96   // bytecode and assign it to the storage.
97   StringRef getReadFromMlirBytecodeCall() const {
98     return readFromMlirBytecodeCall;
99   }
100 
101   // Returns the method call which write this property's
102   // to the the bytecode.
103   StringRef getWriteToMlirBytecodeCall() const {
104     return writeToMlirBytecodeCall;
105   }
106 
107   // Returns the code to compute the hash for this property.
108   StringRef getHashPropertyCall() const { return hashPropertyCall; }
109 
110   // Returns whether this Property has a default value.
111   bool hasDefaultValue() const { return !defaultValue.empty(); }
112 
113   // Returns the default value for this Property.
114   StringRef getDefaultValue() const { return defaultValue; }
115 
116   // Returns whether this Property has a default storage-type value that is
117   // distinct from its default interface-type value.
118   bool hasStorageTypeValueOverride() const {
119     return !storageTypeValueOverride.empty();
120   }
121 
122   StringRef getStorageTypeValueOverride() const {
123     return storageTypeValueOverride;
124   }
125 
126   // Returns this property's TableGen def-name.
127   StringRef getPropertyDefName() const;
128 
129   // Returns the base-level property that this Property constraint is based on
130   // or the Property itself otherwise. (Note: there are currently no
131   // property constraints, this function is added for future-proofing)
132   Property getBaseProperty() const;
133 
134   // Returns the TableGen definition this Property was constructed from.
135   const llvm::Record &getDef() const { return *def; }
136 
137 private:
138   // The TableGen definition of this constraint.
139   const llvm::Record *def;
140 
141   // Elements describing a Property, in general fetched from the record.
142   StringRef summary;
143   StringRef description;
144   StringRef storageType;
145   StringRef interfaceType;
146   StringRef convertFromStorageCall;
147   StringRef assignToStorageCall;
148   StringRef convertToAttributeCall;
149   StringRef convertFromAttributeCall;
150   StringRef parserCall;
151   StringRef optionalParserCall;
152   StringRef printerCall;
153   StringRef readFromMlirBytecodeCall;
154   StringRef writeToMlirBytecodeCall;
155   StringRef hashPropertyCall;
156   StringRef defaultValue;
157   StringRef storageTypeValueOverride;
158 };
159 
160 // A struct wrapping an op property and its name together
161 struct NamedProperty {
162   llvm::StringRef name;
163   Property prop;
164 };
165 
166 } // namespace tblgen
167 } // namespace mlir
168 
169 #endif // MLIR_TABLEGEN_PROPERTY_H_
170