1 | //===- TypeDetail.h ---------------------------------------------*- 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 | #ifndef LIB_MLIR_TOOLS_PDLL_AST_TYPEDETAIL_H_ |
10 | #define LIB_MLIR_TOOLS_PDLL_AST_TYPEDETAIL_H_ |
11 | |
12 | #include "mlir/Tools/PDLL/AST/Types.h" |
13 | |
14 | namespace mlir { |
15 | namespace pdll { |
16 | namespace ast { |
17 | //===----------------------------------------------------------------------===// |
18 | // Type |
19 | //===----------------------------------------------------------------------===// |
20 | |
21 | struct Type::Storage : public StorageUniquer::BaseStorage { |
22 | Storage(TypeID typeID) : typeID(typeID) {} |
23 | |
24 | /// The type identifier for the derived type class. |
25 | TypeID typeID; |
26 | }; |
27 | |
28 | namespace detail { |
29 | |
30 | /// A utility CRTP base class that defines many of the necessary utilities for |
31 | /// defining a PDLL AST Type. |
32 | template <typename ConcreteT, typename KeyT = void> |
33 | struct TypeStorageBase : public Type::Storage { |
34 | using KeyTy = KeyT; |
35 | using Base = TypeStorageBase<ConcreteT, KeyT>; |
36 | TypeStorageBase(KeyTy key) |
37 | : Type::Storage(TypeID::get<ConcreteT>()), key(key) {} |
38 | |
39 | /// Construct an instance with the given storage allocator. |
40 | static ConcreteT *construct(StorageUniquer::StorageAllocator &alloc, |
41 | const KeyTy &key) { |
42 | return new (alloc.allocate<ConcreteT>()) ConcreteT(key); |
43 | } |
44 | |
45 | /// Utility methods required by the storage allocator. |
46 | bool operator==(const KeyTy &key) const { return this->key == key; } |
47 | |
48 | /// Return the key value of this storage class. |
49 | const KeyTy &getValue() const { return key; } |
50 | |
51 | protected: |
52 | KeyTy key; |
53 | }; |
54 | /// A specialization of the storage base for singleton types. |
55 | template <typename ConcreteT> |
56 | struct TypeStorageBase<ConcreteT, void> : public Type::Storage { |
57 | using Base = TypeStorageBase<ConcreteT, void>; |
58 | TypeStorageBase() : Type::Storage(TypeID::get<ConcreteT>()) {} |
59 | }; |
60 | |
61 | //===----------------------------------------------------------------------===// |
62 | // AttributeType |
63 | //===----------------------------------------------------------------------===// |
64 | |
65 | struct AttributeTypeStorage : public TypeStorageBase<AttributeTypeStorage> {}; |
66 | |
67 | //===----------------------------------------------------------------------===// |
68 | // ConstraintType |
69 | //===----------------------------------------------------------------------===// |
70 | |
71 | struct ConstraintTypeStorage : public TypeStorageBase<ConstraintTypeStorage> {}; |
72 | |
73 | //===----------------------------------------------------------------------===// |
74 | // OperationType |
75 | //===----------------------------------------------------------------------===// |
76 | |
77 | struct OperationTypeStorage |
78 | : public TypeStorageBase<OperationTypeStorage, |
79 | std::pair<StringRef, const ods::Operation *>> { |
80 | using Base::Base; |
81 | |
82 | static OperationTypeStorage * |
83 | construct(StorageUniquer::StorageAllocator &alloc, |
84 | const std::pair<StringRef, const ods::Operation *> &key) { |
85 | return new (alloc.allocate<OperationTypeStorage>()) OperationTypeStorage( |
86 | std::make_pair(x: alloc.copyInto(str: key.first), y: key.second)); |
87 | } |
88 | }; |
89 | |
90 | //===----------------------------------------------------------------------===// |
91 | // RangeType |
92 | //===----------------------------------------------------------------------===// |
93 | |
94 | struct RangeTypeStorage : public TypeStorageBase<RangeTypeStorage, Type> { |
95 | using Base::Base; |
96 | }; |
97 | |
98 | //===----------------------------------------------------------------------===// |
99 | // RewriteType |
100 | //===----------------------------------------------------------------------===// |
101 | |
102 | struct RewriteTypeStorage : public TypeStorageBase<RewriteTypeStorage> {}; |
103 | |
104 | //===----------------------------------------------------------------------===// |
105 | // TupleType |
106 | //===----------------------------------------------------------------------===// |
107 | |
108 | struct TupleTypeStorage |
109 | : public TypeStorageBase<TupleTypeStorage, |
110 | std::pair<ArrayRef<Type>, ArrayRef<StringRef>>> { |
111 | using Base::Base; |
112 | |
113 | static TupleTypeStorage * |
114 | construct(StorageUniquer::StorageAllocator &alloc, |
115 | std::pair<ArrayRef<Type>, ArrayRef<StringRef>> key) { |
116 | SmallVector<StringRef> names = llvm::to_vector(Range: llvm::map_range( |
117 | C&: key.second, F: [&](StringRef name) { return alloc.copyInto(str: name); })); |
118 | return new (alloc.allocate<TupleTypeStorage>()) |
119 | TupleTypeStorage(std::make_pair(x: alloc.copyInto(elements: key.first), |
120 | y: alloc.copyInto(elements: llvm::ArrayRef(names)))); |
121 | } |
122 | }; |
123 | |
124 | //===----------------------------------------------------------------------===// |
125 | // TypeType |
126 | //===----------------------------------------------------------------------===// |
127 | |
128 | struct TypeTypeStorage : public TypeStorageBase<TypeTypeStorage> {}; |
129 | |
130 | //===----------------------------------------------------------------------===// |
131 | // ValueType |
132 | //===----------------------------------------------------------------------===// |
133 | |
134 | struct ValueTypeStorage : public TypeStorageBase<ValueTypeStorage> {}; |
135 | |
136 | } // namespace detail |
137 | } // namespace ast |
138 | } // namespace pdll |
139 | } // namespace mlir |
140 | |
141 | #endif // LIB_MLIR_TOOLS_PDLL_AST_TYPEDETAIL_H_ |
142 | |