MLIR  16.0.0git
PDLTypes.cpp
Go to the documentation of this file.
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 
11 #include "mlir/IR/Builders.h"
13 #include "llvm/ADT/TypeSwitch.h"
14 
15 using namespace mlir;
16 using namespace mlir::pdl;
17 
18 //===----------------------------------------------------------------------===//
19 // TableGen'd type method definitions
20 //===----------------------------------------------------------------------===//
21 
22 #define GET_TYPEDEF_CLASSES
23 #include "mlir/Dialect/PDL/IR/PDLOpsTypes.cpp.inc"
24 
25 //===----------------------------------------------------------------------===//
26 // PDLDialect
27 //===----------------------------------------------------------------------===//
28 
29 void PDLDialect::registerTypes() {
30  addTypes<
31 #define GET_TYPEDEF_LIST
32 #include "mlir/Dialect/PDL/IR/PDLOpsTypes.cpp.inc"
33  >();
34 }
35 
37  StringRef typeTag;
38  {
39  Type genType;
40  auto parseResult = generatedTypeParser(parser, &typeTag, genType);
41  if (parseResult.has_value())
42  return genType;
43  }
44 
45  // FIXME: This ends up with a double error being emitted if `RangeType` also
46  // emits an error. We should rework the `generatedTypeParser` to better
47  // support when the keyword is valid but the individual type parser itself
48  // emits an error.
49  parser.emitError(parser.getNameLoc(), "invalid 'pdl' type: `")
50  << typeTag << "'";
51  return Type();
52 }
53 
54 //===----------------------------------------------------------------------===//
55 // PDL Types
56 //===----------------------------------------------------------------------===//
57 
58 bool PDLType::classof(Type type) {
59  return llvm::isa<PDLDialect>(type.getDialect());
60 }
61 
62 //===----------------------------------------------------------------------===//
63 // RangeType
64 //===----------------------------------------------------------------------===//
65 
66 Type RangeType::parse(AsmParser &parser) {
67  if (parser.parseLess())
68  return Type();
69 
70  SMLoc elementLoc = parser.getCurrentLocation();
71  Type elementType = parsePDLType(parser);
72  if (!elementType || parser.parseGreater())
73  return Type();
74 
75  if (elementType.isa<RangeType>()) {
76  parser.emitError(elementLoc)
77  << "element of pdl.range cannot be another range, but got"
78  << elementType;
79  return Type();
80  }
81  return RangeType::get(elementType);
82 }
83 
84 void RangeType::print(AsmPrinter &printer) const {
85  printer << "<";
86  (void)generatedTypePrinter(getElementType(), printer);
87  printer << ">";
88 }
89 
91  Type elementType) {
92  if (!elementType.isa<PDLType>() || elementType.isa<RangeType>()) {
93  return emitError()
94  << "expected element of pdl.range to be one of [!pdl.attribute, "
95  "!pdl.operation, !pdl.type, !pdl.value], but got "
96  << elementType;
97  }
98  return success();
99 }
Include the generated interface declarations.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
Dialect & getDialect() const
Get the dialect this type is registered to.
Definition: Types.h:121
constexpr Type()
Definition: Types.h:84
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:310
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity...
Definition: SPIRVOps.cpp:685
This class represents the base class of all PDL types.
Definition: PDLTypes.h:25
static bool classof(Type type)
Definition: PDLTypes.cpp:58
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseGreater()=0
Parse a &#39;>&#39; token.
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
virtual ParseResult parseLess()=0
Parse a &#39;<&#39; token.
This base class exposes generic asm parser hooks, usable across the various derived parsers...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:72
static Type parsePDLType(AsmParser &parser)
Definition: PDLTypes.cpp:36
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs, on this operation and any nested operations.
Definition: Verifier.cpp:372
This base class exposes generic asm printer hooks, usable across the various derived printers...
bool isa() const
Definition: Types.h:254