MLIR  19.0.0git
Syntax.cpp
Go to the documentation of this file.
1 //===- Syntax.cpp - Custom syntax for Linalg transform ops ----------------===//
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 
12 using namespace mlir;
13 
15  Type &resultType) {
16  argumentType = resultType = nullptr;
17  bool hasLParen = parser.parseOptionalLParen().succeeded();
18  if (parser.parseType(argumentType).failed())
19  return failure();
20  if (!hasLParen)
21  return success();
22 
23  return failure(parser.parseRParen().failed() ||
24  parser.parseArrow().failed() ||
25  parser.parseType(resultType).failed());
26 }
27 
29  SmallVectorImpl<Type> &resultTypes) {
30  argumentType = nullptr;
31  bool hasLParen = parser.parseOptionalLParen().succeeded();
32  if (parser.parseType(argumentType).failed())
33  return failure();
34  if (!hasLParen)
35  return success();
36 
37  if (parser.parseRParen().failed() || parser.parseArrow().failed())
38  return failure();
39 
40  if (parser.parseOptionalLParen().failed()) {
41  Type type;
42  if (parser.parseType(type).failed())
43  return failure();
44  resultTypes.push_back(type);
45  return success();
46  }
47  if (parser.parseTypeList(resultTypes).failed() ||
48  parser.parseRParen().failed()) {
49  resultTypes.clear();
50  return failure();
51  }
52  return success();
53 }
54 
56  Type argumentType, TypeRange resultType) {
57  if (!resultType.empty())
58  printer << "(";
59  printer << argumentType;
60  if (resultType.empty())
61  return;
62  printer << ") -> ";
63 
64  if (resultType.size() > 1)
65  printer << "(";
66  llvm::interleaveComma(resultType, printer.getStream());
67  if (resultType.size() > 1)
68  printer << ")";
69 }
70 
72  Type argumentType, Type resultType) {
73  return printSemiFunctionType(printer, op, argumentType,
74  resultType ? TypeRange(resultType)
75  : TypeRange());
76 }
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual ParseResult parseArrow()=0
Parse a '->' token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
virtual ParseResult parseOptionalLParen()=0
Parse a ( token if present.
ParseResult parseTypeList(SmallVectorImpl< Type > &result)
Parse a type list.
Definition: AsmPrinter.cpp:76
virtual raw_ostream & getStream() const
Return the raw output stream used by this printer.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class represents success/failure for parsing-like operations that find it important to chain tog...
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
ParseResult parseSemiFunctionType(OpAsmParser &parser, Type &argumentType, Type &resultType)
Parses a single non-function type or a function type with at least one argument.
Definition: Syntax.cpp:14
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
void printSemiFunctionType(OpAsmPrinter &printer, Operation *op, Type argumentType, TypeRange resultType)
Prints argument and result types in a syntax similar to that of FunctionType but allowing and requiri...
Definition: Syntax.cpp:55
bool succeeded() const
Returns true if the provided LogicalResult corresponds to a success value.
Definition: LogicalResult.h:41
bool failed() const
Returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:44