1//===- PDLTypes.cpp - Pattern Descriptor Language Types -------------------===//
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/Dialect/PDL/IR/PDLTypes.h"
10#include "mlir/Dialect/PDL/IR/PDL.h"
11
12using namespace mlir;
13using namespace mlir::pdl;
14
15//===----------------------------------------------------------------------===//
16// TableGen'd type method definitions
17//===----------------------------------------------------------------------===//
18
19#define GET_TYPEDEF_CLASSES
20#include "mlir/Dialect/PDL/IR/PDLOpsTypes.cpp.inc"
21
22//===----------------------------------------------------------------------===//
23// PDLDialect
24//===----------------------------------------------------------------------===//
25
26void PDLDialect::registerTypes() {
27 addTypes<
28#define GET_TYPEDEF_LIST
29#include "mlir/Dialect/PDL/IR/PDLOpsTypes.cpp.inc"
30 >();
31}
32
33static Type parsePDLType(AsmParser &parser) {
34 StringRef typeTag;
35 {
36 Type genType;
37 auto parseResult = generatedTypeParser(parser, mnemonic: &typeTag, value&: genType);
38 if (parseResult.has_value())
39 return genType;
40 }
41
42 // FIXME: This ends up with a double error being emitted if `RangeType` also
43 // emits an error. We should rework the `generatedTypeParser` to better
44 // support when the keyword is valid but the individual type parser itself
45 // emits an error.
46 parser.emitError(loc: parser.getNameLoc(), message: "invalid 'pdl' type: `")
47 << typeTag << "'";
48 return Type();
49}
50
51//===----------------------------------------------------------------------===//
52// PDL Types
53//===----------------------------------------------------------------------===//
54
55bool PDLType::classof(Type type) {
56 return llvm::isa<PDLDialect>(Val: type.getDialect());
57}
58
59Type pdl::getRangeElementTypeOrSelf(Type type) {
60 if (auto rangeType = llvm::dyn_cast<RangeType>(Val&: type))
61 return rangeType.getElementType();
62 return type;
63}
64
65//===----------------------------------------------------------------------===//
66// RangeType
67//===----------------------------------------------------------------------===//
68
69Type RangeType::parse(AsmParser &parser) {
70 if (parser.parseLess())
71 return Type();
72
73 SMLoc elementLoc = parser.getCurrentLocation();
74 Type elementType = parsePDLType(parser);
75 if (!elementType || parser.parseGreater())
76 return Type();
77
78 if (llvm::isa<RangeType>(Val: elementType)) {
79 parser.emitError(loc: elementLoc)
80 << "element of pdl.range cannot be another range, but got"
81 << elementType;
82 return Type();
83 }
84 return RangeType::get(elementType);
85}
86
87void RangeType::print(AsmPrinter &printer) const {
88 printer << "<";
89 (void)generatedTypePrinter(def: getElementType(), printer);
90 printer << ">";
91}
92
93LogicalResult RangeType::verify(function_ref<InFlightDiagnostic()> emitError,
94 Type elementType) {
95 if (!llvm::isa<PDLType>(Val: elementType) || llvm::isa<RangeType>(Val: elementType)) {
96 return emitError()
97 << "expected element of pdl.range to be one of [!pdl.attribute, "
98 "!pdl.operation, !pdl.type, !pdl.value], but got "
99 << elementType;
100 }
101 return success();
102}
103