MLIR  21.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 #include "llvm/Support/InterleavedRange.h"
12 
13 using namespace mlir;
14 
15 ParseResult mlir::parseSemiFunctionType(OpAsmParser &parser, Type &argumentType,
16  Type &resultType, bool resultOptional) {
17  argumentType = resultType = nullptr;
18 
19  bool hasLParen = resultOptional ? parser.parseOptionalLParen().succeeded()
20  : parser.parseLParen().succeeded();
21  if (!resultOptional && !hasLParen)
22  return failure();
23  if (parser.parseType(argumentType).failed())
24  return failure();
25  if (!hasLParen)
26  return success();
27 
28  return failure(parser.parseRParen().failed() ||
29  parser.parseArrow().failed() ||
30  parser.parseType(resultType).failed());
31 }
32 
33 ParseResult mlir::parseSemiFunctionType(OpAsmParser &parser, Type &argumentType,
34  SmallVectorImpl<Type> &resultTypes) {
35  argumentType = nullptr;
36  bool hasLParen = parser.parseOptionalLParen().succeeded();
37  if (parser.parseType(argumentType).failed())
38  return failure();
39  if (!hasLParen)
40  return success();
41 
42  if (parser.parseRParen().failed() || parser.parseArrow().failed())
43  return failure();
44 
45  if (parser.parseOptionalLParen().failed()) {
46  Type type;
47  if (parser.parseType(type).failed())
48  return failure();
49  resultTypes.push_back(type);
50  return success();
51  }
52  if (parser.parseTypeList(resultTypes).failed() ||
53  parser.parseRParen().failed()) {
54  resultTypes.clear();
55  return failure();
56  }
57  return success();
58 }
59 
61  Type argumentType, TypeRange resultType) {
62  if (!resultType.empty())
63  printer << "(";
64  printer << argumentType;
65  if (resultType.empty())
66  return;
67  printer << ") -> ";
68 
69  if (resultType.size() > 1)
70  printer << "(";
71  printer << llvm::interleaved(resultType);
72  if (resultType.size() > 1)
73  printer << ")";
74 }
75 
77  Type argumentType, Type resultType,
78  bool resultOptional) {
79  assert(resultOptional || resultType != nullptr);
80  return printSemiFunctionType(printer, op, argumentType,
81  resultType ? TypeRange(resultType)
82  : TypeRange());
83 }
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual ParseResult parseArrow()=0
Parse a '->' token.
virtual ParseResult parseLParen()=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:78
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 provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:37
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.
ParseResult parseSemiFunctionType(OpAsmParser &parser, Type &argumentType, Type &resultType, bool resultOptional=true)
Parses a single non-function type or a function type with at least one argument.
Definition: Syntax.cpp:15
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:60