MLIR  16.0.0git
DialectImplementation.h
Go to the documentation of this file.
1 //===- DialectImplementation.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 // This file contains utilities classes for implementing dialect attributes and
10 // types.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_IR_DIALECTIMPLEMENTATION_H
15 #define MLIR_IR_DIALECTIMPLEMENTATION_H
16 
18 
19 namespace mlir {
20 
21 //===----------------------------------------------------------------------===//
22 // DialectAsmPrinter
23 //===----------------------------------------------------------------------===//
24 
25 /// This is a pure-virtual base class that exposes the asmprinter hooks
26 /// necessary to implement a custom printAttribute/printType() method on a
27 /// dialect.
28 class DialectAsmPrinter : public AsmPrinter {
29 public:
31  ~DialectAsmPrinter() override;
32 };
33 
34 //===----------------------------------------------------------------------===//
35 // DialectAsmParser
36 //===----------------------------------------------------------------------===//
37 
38 /// The DialectAsmParser has methods for interacting with the asm parser when
39 /// parsing attributes and types.
40 class DialectAsmParser : public AsmParser {
41 public:
43  ~DialectAsmParser() override;
44 
45  /// Returns the full specification of the symbol being parsed. This allows for
46  /// using a separate parser if necessary.
47  virtual StringRef getFullSymbolSpec() const = 0;
48 };
49 
50 //===----------------------------------------------------------------------===//
51 // Parse Fields
52 //===----------------------------------------------------------------------===//
53 
54 /// Provide a template class that can be specialized by users to dispatch to
55 /// parsers. Auto-generated parsers generate calls to `FieldParser<T>::parse`,
56 /// where `T` is the parameter storage type, to parse custom types.
57 template <typename T, typename = T>
58 struct FieldParser;
59 
60 /// Parse an attribute.
61 template <typename AttributeT>
62 struct FieldParser<
63  AttributeT, std::enable_if_t<std::is_base_of<Attribute, AttributeT>::value,
64  AttributeT>> {
66  AttributeT value;
67  if (parser.parseCustomAttributeWithFallback(value))
68  return failure();
69  return value;
70  }
71 };
72 
73 /// Parse an attribute.
74 template <typename TypeT>
75 struct FieldParser<
76  TypeT, std::enable_if_t<std::is_base_of<Type, TypeT>::value, TypeT>> {
78  TypeT value;
79  if (parser.parseCustomTypeWithFallback(value))
80  return failure();
81  return value;
82  }
83 };
84 
85 /// Parse any integer.
86 template <typename IntT>
87 struct FieldParser<IntT,
88  std::enable_if_t<std::is_integral<IntT>::value, IntT>> {
90  IntT value;
91  if (parser.parseInteger(value))
92  return failure();
93  return value;
94  }
95 };
96 
97 /// Parse a string.
98 template <>
99 struct FieldParser<std::string> {
101  std::string value;
102  if (parser.parseString(&value))
103  return failure();
104  return value;
105  }
106 };
107 
108 /// Parse any container that supports back insertion as a list.
109 template <typename ContainerT>
110 struct FieldParser<
111  ContainerT, std::enable_if_t<std::is_member_function_pointer<
112  decltype(&ContainerT::push_back)>::value,
113  ContainerT>> {
114  using ElementT = typename ContainerT::value_type;
116  ContainerT elements;
117  auto elementParser = [&]() {
118  auto element = FieldParser<ElementT>::parse(parser);
119  if (failed(element))
120  return failure();
121  elements.push_back(*element);
122  return success();
123  };
124  if (parser.parseCommaSeparatedList(elementParser))
125  return failure();
126  return elements;
127  }
128 };
129 
130 /// Parse an affine map.
131 template <>
134  AffineMap map;
135  if (failed(parser.parseAffineMap(map)))
136  return failure();
137  return map;
138  }
139 };
140 
141 } // namespace mlir
142 
143 #endif // MLIR_IR_DIALECTIMPLEMENTATION_H
Include the generated interface declarations.
~DialectAsmPrinter() override
virtual ParseResult parseAffineMap(AffineMap &map)=0
Parse an affine map instance into &#39;map&#39;.
virtual ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())=0
Parse a list of comma-separated items with an optional delimiter.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
Definition: LogicalResult.h:72
virtual ParseResult parseCustomAttributeWithFallback(Attribute &result, Type type, function_ref< ParseResult(Attribute &result, Type type)> parseAttribute)=0
Parse a custom attribute with the provided callback, unless the next token is #, in which case the ge...
static constexpr const bool value
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
This class provides support for representing a failure result, or a valid value of type T...
Definition: LogicalResult.h:78
static FailureOr< AffineMap > parse(AsmParser &parser)
A multi-dimensional affine map Affine map&#39;s are immutable like Type&#39;s, and they are uniqued...
Definition: AffineMap.h:42
ParseResult parseString(std::string *string)
Parse a quoted string token.
This base class exposes generic asm parser hooks, usable across the various derived parsers...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
Provide a template class that can be specialized by users to dispatch to parsers. ...
AsmPrinter()
Initialize the printer with no internal implementation.
This base class exposes generic asm printer hooks, usable across the various derived printers...
virtual ParseResult parseCustomTypeWithFallback(Type &result, function_ref< ParseResult(Type &result)> parseType)=0
Parse a custom type with the provided callback, unless the next token is #, in which case the generic...
AsmParser()=default
The DialectAsmParser has methods for interacting with the asm parser when parsing attributes and type...
static FailureOr< std::string > parse(AsmParser &parser)