| 1 | //===- Types.cpp ----------------------------------------------------------===// |
| 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 | #include "mlir/Tools/PDLL/AST/Types.h" |
| 10 | #include "mlir/Tools/PDLL/AST/Context.h" |
| 11 | #include <optional> |
| 12 | |
| 13 | using namespace mlir; |
| 14 | using namespace mlir::pdll; |
| 15 | using namespace mlir::pdll::ast; |
| 16 | |
| 17 | MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::AttributeTypeStorage) |
| 18 | MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::ConstraintTypeStorage) |
| 19 | MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::OperationTypeStorage) |
| 20 | MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::RangeTypeStorage) |
| 21 | MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::RewriteTypeStorage) |
| 22 | MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::TupleTypeStorage) |
| 23 | MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::TypeTypeStorage) |
| 24 | MLIR_DEFINE_EXPLICIT_TYPE_ID(mlir::pdll::ast::detail::ValueTypeStorage) |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // Type |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | TypeID Type::getTypeID() const { return impl->typeID; } |
| 31 | |
| 32 | Type Type::refineWith(Type other) const { |
| 33 | if (*this == other) |
| 34 | return *this; |
| 35 | |
| 36 | // Operation types are compatible if the operation names don't conflict. |
| 37 | if (auto opTy = mlir::dyn_cast<OperationType>(Val: *this)) { |
| 38 | auto otherOpTy = mlir::dyn_cast<ast::OperationType>(Val&: other); |
| 39 | if (!otherOpTy) |
| 40 | return nullptr; |
| 41 | if (!otherOpTy.getName()) |
| 42 | return *this; |
| 43 | if (!opTy.getName()) |
| 44 | return other; |
| 45 | |
| 46 | return nullptr; |
| 47 | } |
| 48 | |
| 49 | return nullptr; |
| 50 | } |
| 51 | |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | // AttributeType |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | AttributeType AttributeType::get(Context &context) { |
| 57 | return context.getTypeUniquer().get<ImplTy>(); |
| 58 | } |
| 59 | |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | // ConstraintType |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | |
| 64 | ConstraintType ConstraintType::get(Context &context) { |
| 65 | return context.getTypeUniquer().get<ImplTy>(); |
| 66 | } |
| 67 | |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | // OperationType |
| 70 | //===----------------------------------------------------------------------===// |
| 71 | |
| 72 | OperationType OperationType::get(Context &context, |
| 73 | std::optional<StringRef> name, |
| 74 | const ods::Operation *odsOp) { |
| 75 | return context.getTypeUniquer().get<ImplTy>( |
| 76 | /*initFn=*/function_ref<void(ImplTy *)>(), |
| 77 | args: std::make_pair(x: name.value_or(u: "" ), y&: odsOp)); |
| 78 | } |
| 79 | |
| 80 | std::optional<StringRef> OperationType::getName() const { |
| 81 | StringRef name = getImplAs<ImplTy>()->getValue().first; |
| 82 | return name.empty() ? std::optional<StringRef>() |
| 83 | : std::optional<StringRef>(name); |
| 84 | } |
| 85 | |
| 86 | const ods::Operation *OperationType::getODSOperation() const { |
| 87 | return getImplAs<ImplTy>()->getValue().second; |
| 88 | } |
| 89 | |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | // RangeType |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | |
| 94 | RangeType RangeType::get(Context &context, Type elementType) { |
| 95 | return context.getTypeUniquer().get<ImplTy>( |
| 96 | /*initFn=*/function_ref<void(ImplTy *)>(), args&: elementType); |
| 97 | } |
| 98 | |
| 99 | Type RangeType::getElementType() const { |
| 100 | return getImplAs<ImplTy>()->getValue(); |
| 101 | } |
| 102 | |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | // TypeRangeType |
| 105 | //===----------------------------------------------------------------------===// |
| 106 | |
| 107 | bool TypeRangeType::classof(Type type) { |
| 108 | RangeType range = mlir::dyn_cast<RangeType>(Val&: type); |
| 109 | return range && mlir::isa<TypeType>(Val: range.getElementType()); |
| 110 | } |
| 111 | |
| 112 | TypeRangeType TypeRangeType::get(Context &context) { |
| 113 | return mlir::cast<TypeRangeType>( |
| 114 | Val: RangeType::get(context, elementType: TypeType::get(context))); |
| 115 | } |
| 116 | |
| 117 | //===----------------------------------------------------------------------===// |
| 118 | // ValueRangeType |
| 119 | //===----------------------------------------------------------------------===// |
| 120 | |
| 121 | bool ValueRangeType::classof(Type type) { |
| 122 | RangeType range = mlir::dyn_cast<RangeType>(Val&: type); |
| 123 | return range && mlir::isa<ValueType>(Val: range.getElementType()); |
| 124 | } |
| 125 | |
| 126 | ValueRangeType ValueRangeType::get(Context &context) { |
| 127 | return mlir::cast<ValueRangeType>( |
| 128 | Val: RangeType::get(context, elementType: ValueType::get(context))); |
| 129 | } |
| 130 | |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | // RewriteType |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | |
| 135 | RewriteType RewriteType::get(Context &context) { |
| 136 | return context.getTypeUniquer().get<ImplTy>(); |
| 137 | } |
| 138 | |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | // TupleType |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | |
| 143 | TupleType TupleType::get(Context &context, ArrayRef<Type> elementTypes, |
| 144 | ArrayRef<StringRef> elementNames) { |
| 145 | assert(elementTypes.size() == elementNames.size()); |
| 146 | return context.getTypeUniquer().get<ImplTy>( |
| 147 | /*initFn=*/function_ref<void(ImplTy *)>(), args&: elementTypes, args&: elementNames); |
| 148 | } |
| 149 | TupleType TupleType::get(Context &context, ArrayRef<Type> elementTypes) { |
| 150 | SmallVector<StringRef> elementNames(elementTypes.size()); |
| 151 | return get(context, elementTypes, elementNames); |
| 152 | } |
| 153 | |
| 154 | ArrayRef<Type> TupleType::getElementTypes() const { |
| 155 | return getImplAs<ImplTy>()->getValue().first; |
| 156 | } |
| 157 | |
| 158 | ArrayRef<StringRef> TupleType::getElementNames() const { |
| 159 | return getImplAs<ImplTy>()->getValue().second; |
| 160 | } |
| 161 | |
| 162 | //===----------------------------------------------------------------------===// |
| 163 | // TypeType |
| 164 | //===----------------------------------------------------------------------===// |
| 165 | |
| 166 | TypeType TypeType::get(Context &context) { |
| 167 | return context.getTypeUniquer().get<ImplTy>(); |
| 168 | } |
| 169 | |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | // ValueType |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
| 174 | ValueType ValueType::get(Context &context) { |
| 175 | return context.getTypeUniquer().get<ImplTy>(); |
| 176 | } |
| 177 | |