MLIR  18.0.0git
Parser.h
Go to the documentation of this file.
1 //===- Parser.h - MLIR Base Parser Class ------------------------*- 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 #ifndef MLIR_LIB_ASMPARSER_PARSER_H
10 #define MLIR_LIB_ASMPARSER_PARSER_H
11 
12 #include "ParserState.h"
13 #include "mlir/IR/Builders.h"
15 #include <optional>
16 
17 namespace mlir {
18 namespace detail {
19 //===----------------------------------------------------------------------===//
20 // Parser
21 //===----------------------------------------------------------------------===//
22 
23 /// This class implement support for parsing global entities like attributes and
24 /// types. It is intended to be subclassed by specialized subparsers that
25 /// include state.
26 class Parser {
27 public:
29 
31 
33  : builder(state.config.getContext()), state(state) {}
34 
35  // Helper methods to get stuff from the parser-global state.
36  ParserState &getState() const { return state; }
37  MLIRContext *getContext() const { return state.config.getContext(); }
38  const llvm::SourceMgr &getSourceMgr() { return state.lex.getSourceMgr(); }
39 
40  /// Parse a comma-separated list of elements up until the specified end token.
43  function_ref<ParseResult()> parseElement,
44  bool allowEmptyList = true);
45 
46  /// Parse a list of comma-separated items with an optional delimiter. If a
47  /// delimiter is provided, then an empty list is allowed. If not, then at
48  /// least one element will be parsed.
51  function_ref<ParseResult()> parseElementFn,
52  StringRef contextMessage = StringRef());
53 
54  /// Parse a comma separated list of elements that must have at least one entry
55  /// in it.
58  return parseCommaSeparatedList(Delimiter::None, parseElementFn);
59  }
60 
61  /// Parse the body of a dialect symbol, which starts and ends with <>'s, and
62  /// may be recursive. Return with the 'body' StringRef encompassing the entire
63  /// body. `isCodeCompletion` is set to true if the body contained a code
64  /// completion location, in which case the body is only populated up to the
65  /// completion.
66  ParseResult parseDialectSymbolBody(StringRef &body, bool &isCodeCompletion);
68  bool isCodeCompletion = false;
69  return parseDialectSymbolBody(body, isCodeCompletion);
70  }
71 
72  // We have two forms of parsing methods - those that return a non-null
73  // pointer on success, and those that return a ParseResult to indicate whether
74  // they returned a failure. The second class fills in by-reference arguments
75  // as the results of their action.
76 
77  //===--------------------------------------------------------------------===//
78  // Error Handling
79  //===--------------------------------------------------------------------===//
80 
81  /// Emit an error and return failure.
82  InFlightDiagnostic emitError(const Twine &message = {});
83  InFlightDiagnostic emitError(SMLoc loc, const Twine &message = {});
84 
85  /// Emit an error about a "wrong token". If the current token is at the
86  /// start of a source line, this will apply heuristics to back up and report
87  /// the error at the end of the previous line, which is where the expected
88  /// token is supposed to be.
89  InFlightDiagnostic emitWrongTokenError(const Twine &message = {});
90 
91  /// Encode the specified source location information into an attribute for
92  /// attachment to the IR.
95  }
96 
97  //===--------------------------------------------------------------------===//
98  // Token Parsing
99  //===--------------------------------------------------------------------===//
100 
101  /// Return the current token the parser is inspecting.
102  const Token &getToken() const { return state.curToken; }
103  StringRef getTokenSpelling() const { return state.curToken.getSpelling(); }
104 
105  /// If the current token has the specified kind, consume it and return true.
106  /// If not, return false.
107  bool consumeIf(Token::Kind kind) {
108  if (state.curToken.isNot(kind))
109  return false;
110  consumeToken(kind);
111  return true;
112  }
113 
114  /// Advance the current lexer onto the next token.
115  void consumeToken() {
116  assert(state.curToken.isNot(Token::eof, Token::error) &&
117  "shouldn't advance past EOF or errors");
119  }
120 
121  /// Advance the current lexer onto the next token, asserting what the expected
122  /// current token is. This is preferred to the above method because it leads
123  /// to more self-documenting code with better checking.
125  assert(state.curToken.is(kind) && "consumed an unexpected token");
126  consumeToken();
127  }
128 
129  /// Reset the parser to the given lexer position.
130  void resetToken(const char *tokPos) {
131  state.lex.resetPointer(tokPos);
133  }
134 
135  /// Consume the specified token if present and return success. On failure,
136  /// output a diagnostic and return failure.
137  ParseResult parseToken(Token::Kind expectedToken, const Twine &message);
138 
139  /// Parse an optional integer value from the stream.
141 
142  /// Parse a floating point value from an integer literal token.
143  ParseResult parseFloatFromIntegerLiteral(std::optional<APFloat> &result,
144  const Token &tok, bool isNegative,
145  const llvm::fltSemantics &semantics,
146  size_t typeSizeInBits);
147 
148  /// Returns true if the current token corresponds to a keyword.
149  bool isCurrentTokenAKeyword() const {
150  return getToken().isAny(Token::bare_identifier, Token::inttype) ||
151  getToken().isKeyword();
152  }
153 
154  /// Parse a keyword, if present, into 'keyword'.
155  ParseResult parseOptionalKeyword(StringRef *keyword);
156 
157  //===--------------------------------------------------------------------===//
158  // Resource Parsing
159  //===--------------------------------------------------------------------===//
160 
161  /// Parse a handle to a dialect resource within the assembly format.
163  parseResourceHandle(const OpAsmDialectInterface *dialect, StringRef &name);
165 
166  //===--------------------------------------------------------------------===//
167  // Type Parsing
168  //===--------------------------------------------------------------------===//
169 
170  /// Invoke the `getChecked` method of the given Attribute or Type class, using
171  /// the provided location to emit errors in the case of failure. Note that
172  /// unlike `OpBuilder::getType`, this method does not implicitly insert a
173  /// context parameter.
174  template <typename T, typename... ParamsT>
175  T getChecked(SMLoc loc, ParamsT &&...params) {
176  return T::getChecked([&] { return emitError(loc); },
177  std::forward<ParamsT>(params)...);
178  }
179 
183 
184  /// Optionally parse a type.
186 
187  /// Parse an arbitrary type.
188  Type parseType();
189 
190  /// Parse a complex type.
192 
193  /// Parse an extended type.
195 
196  /// Parse a function type.
198 
199  /// Parse a memref type.
201 
202  /// Parse a non function type.
204 
205  /// Parse a tensor type.
207 
208  /// Parse a tuple type.
210 
211  /// Parse a vector type.
212  VectorType parseVectorType();
214  SmallVectorImpl<bool> &scalableDims);
216  bool allowDynamic = true,
217  bool withTrailingX = true);
220 
221  //===--------------------------------------------------------------------===//
222  // Attribute Parsing
223  //===--------------------------------------------------------------------===//
224 
225  /// Parse an arbitrary attribute with an optional type.
226  Attribute parseAttribute(Type type = {});
227 
228  /// Parse an optional attribute with the provided type.
230  Type type = {});
231  OptionalParseResult parseOptionalAttribute(ArrayAttr &attribute, Type type);
232  OptionalParseResult parseOptionalAttribute(StringAttr &attribute, Type type);
233  OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result, Type type);
234 
235  /// Parse an optional attribute that is demarcated by a specific token.
236  template <typename AttributeT>
238  AttributeT &attr,
239  Type type = {}) {
240  if (getToken().isNot(kind))
241  return std::nullopt;
242 
243  if (Attribute parsedAttr = parseAttribute(type)) {
244  attr = cast<AttributeT>(parsedAttr);
245  return success();
246  }
247  return failure();
248  }
249 
250  /// Parse an attribute dictionary.
251  ParseResult parseAttributeDict(NamedAttrList &attributes);
252 
253  /// Parse a distinct attribute.
254  Attribute parseDistinctAttr(Type type);
255 
256  /// Parse an extended attribute.
257  Attribute parseExtendedAttr(Type type);
258 
259  /// Parse a float attribute.
260  Attribute parseFloatAttr(Type type, bool isNegative);
261 
262  /// Parse a decimal or a hexadecimal literal, which can be either an integer
263  /// or a float attribute.
264  Attribute parseDecOrHexAttr(Type type, bool isNegative);
265 
266  /// Parse a dense elements attribute.
267  Attribute parseDenseElementsAttr(Type attrType);
268  ShapedType parseElementsLiteralType(Type type);
269 
270  /// Parse a dense resource elements attribute.
271  Attribute parseDenseResourceElementsAttr(Type attrType);
272 
273  /// Parse a DenseArrayAttr.
274  Attribute parseDenseArrayAttr(Type type);
275 
276  /// Parse a sparse elements attribute.
277  Attribute parseSparseElementsAttr(Type attrType);
278 
279  /// Parse a strided layout attribute.
280  Attribute parseStridedLayoutAttr();
281 
282  //===--------------------------------------------------------------------===//
283  // Location Parsing
284  //===--------------------------------------------------------------------===//
285 
286  /// Parse a raw location instance.
287  ParseResult parseLocationInstance(LocationAttr &loc);
288 
289  /// Parse a callsite location instance.
290  ParseResult parseCallSiteLocation(LocationAttr &loc);
291 
292  /// Parse a fused location instance.
293  ParseResult parseFusedLocation(LocationAttr &loc);
294 
295  /// Parse a name or FileLineCol location instance.
296  ParseResult parseNameOrFileLineColLocation(LocationAttr &loc);
297 
298  //===--------------------------------------------------------------------===//
299  // Affine Parsing
300  //===--------------------------------------------------------------------===//
301 
302  /// Parse a reference to either an affine map, expr, or an integer set.
303  ParseResult parseAffineMapOrIntegerSetReference(AffineMap &map,
304  IntegerSet &set);
305  ParseResult parseAffineMapReference(AffineMap &map);
306  ParseResult
307  parseAffineExprReference(ArrayRef<std::pair<StringRef, AffineExpr>> symbolSet,
308  AffineExpr &expr);
309  ParseResult parseIntegerSetReference(IntegerSet &set);
310 
311  /// Parse an AffineMap where the dim and symbol identifiers are SSA ids.
312  ParseResult
313  parseAffineMapOfSSAIds(AffineMap &map,
314  function_ref<ParseResult(bool)> parseElement,
315  Delimiter delimiter);
316 
317  /// Parse an AffineExpr where dim and symbol identifiers are SSA ids.
318  ParseResult
319  parseAffineExprOfSSAIds(AffineExpr &expr,
320  function_ref<ParseResult(bool)> parseElement);
321 
322  //===--------------------------------------------------------------------===//
323  // Code Completion
324  //===--------------------------------------------------------------------===//
325 
326  /// The set of various code completion methods. Every completion method
327  /// returns `failure` to signal that parsing should abort after any desired
328  /// completions have been enqueued. Note that `failure` is does not mean
329  /// completion failed, it's just a signal to the parser to stop.
330 
331  ParseResult codeCompleteDialectName();
332  ParseResult codeCompleteOperationName(StringRef dialectName);
333  ParseResult codeCompleteDialectOrElidedOpName(SMLoc loc);
334  ParseResult codeCompleteStringDialectOrOperationName(StringRef name);
335  ParseResult codeCompleteExpectedTokens(ArrayRef<StringRef> tokens);
336  ParseResult codeCompleteOptionalTokens(ArrayRef<StringRef> tokens);
337 
338  Attribute codeCompleteAttribute();
339  Type codeCompleteType();
340  Attribute
341  codeCompleteDialectSymbol(const llvm::StringMap<Attribute> &aliases);
342  Type codeCompleteDialectSymbol(const llvm::StringMap<Type> &aliases);
343 
344 protected:
345  /// The Parser is subclassed and reinstantiated. Do not add additional
346  /// non-trivial state here, add it to the ParserState class.
348 };
349 } // namespace detail
350 } // namespace mlir
351 
352 #endif // MLIR_LIB_ASMPARSER_PARSER_H
Delimiter
These are the supported delimiters around operand lists and region argument lists,...
@ None
Zero or more operands with no delimiters.
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:50
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:41
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:308
Token lexToken()
Definition: Lexer.cpp:73
const llvm::SourceMgr & getSourceMgr()
Definition: Lexer.h:28
Location getEncodedSourceLocation(SMLoc loc)
Encode the specified source location information into a Location object for attachment to the IR or e...
Definition: Lexer.cpp:50
void resetPointer(const char *newPointer)
Change the position of the lexer cursor.
Definition: Lexer.h:38
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class implements Optional functionality for ParseResult.
Definition: OpDefinition.h:39
This class represents success/failure for parsing-like operations that find it important to chain tog...
MLIRContext * getContext() const
Return the MLIRContext to be used when parsing.
Definition: AsmState.h:474
This represents a token in the MLIR syntax.
Definition: Token.h:20
bool isKeyword() const
Return true if this is one of the keyword token kinds (e.g. kw_if).
Definition: Token.cpp:192
bool is(Kind k) const
Definition: Token.h:38
bool isAny(Kind k1, Kind k2) const
Definition: Token.h:40
bool isNot(Kind k) const
Definition: Token.h:50
StringRef getSpelling() const
Definition: Token.h:34
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class implement support for parsing global entities like attributes and types.
Definition: Parser.h:26
ParseResult parseOptionalKeyword(StringRef *keyword)
Parse a keyword, if present, into 'keyword'.
Definition: Parser.cpp:346
Attribute parseDenseArrayAttr(Type type)
Parse a DenseArrayAttr.
Attribute parseStridedLayoutAttr()
Parse a strided layout attribute.
Attribute parseDecOrHexAttr(Type type, bool isNegative)
Parse a decimal or a hexadecimal literal, which can be either an integer or a float attribute.
OpAsmParser::Delimiter Delimiter
Definition: Parser.h:28
ParseResult parseXInDimensionList()
Parse an 'x' token in a dimension list, handling the case where the x is juxtaposed with an element t...
Definition: TypeParser.cpp:592
T getChecked(SMLoc loc, ParamsT &&...params)
Invoke the getChecked method of the given Attribute or Type class, using the provided location to emi...
Definition: Parser.h:175
OptionalParseResult parseOptionalType(Type &type)
Optionally parse a type.
Definition: TypeParser.cpp:33
ParseResult parseToken(Token::Kind expectedToken, const Twine &message)
Consume the specified token if present and return success.
Definition: Parser.cpp:267
ParseResult parseCommaSeparatedListUntil(Token::Kind rightToken, function_ref< ParseResult()> parseElement, bool allowEmptyList=true)
Parse a comma-separated list of elements up until the specified end token.
Definition: Parser.cpp:173
ParseResult codeCompleteOperationName(StringRef dialectName)
Definition: Parser.cpp:407
Builder builder
Definition: Parser.h:30
Type parseType()
Parse an arbitrary type.
Definition: TypeParser.cpp:70
Attribute parseDenseElementsAttr(Type attrType)
Parse a dense elements attribute.
void consumeToken(Token::Kind kind)
Advance the current lexer onto the next token, asserting what the expected current token is.
Definition: Parser.h:124
ParseResult parseTypeListParens(SmallVectorImpl< Type > &elements)
Parse a parenthesized list of types.
Definition: TypeParser.cpp:112
ParserState & getState() const
Definition: Parser.h:36
Attribute parseDenseResourceElementsAttr(Type attrType)
Parse a dense resource elements attribute.
ParseResult parseVectorDimensionList(SmallVectorImpl< int64_t > &dimensions, SmallVectorImpl< bool > &scalableDims)
Parse a dimension list in a vector type.
Definition: TypeParser.cpp:491
Type parseMemRefType()
Parse a memref type.
Definition: TypeParser.cpp:179
Type parseNonFunctionType()
Parse a non function type.
Definition: TypeParser.cpp:271
Parser(ParserState &state)
Definition: Parser.h:32
Type codeCompleteType()
Definition: Parser.cpp:467
ParseResult parseAffineMapReference(AffineMap &map)
Location getEncodedSourceLocation(SMLoc loc)
Encode the specified source location information into an attribute for attachment to the IR.
Definition: Parser.h:93
Type parseExtendedType()
Parse an extended type.
Type parseTupleType()
Parse a tuple type.
Definition: TypeParser.cpp:427
InFlightDiagnostic emitError(const Twine &message={})
Emit an error and return failure.
Definition: Parser.cpp:192
ParserState & state
The Parser is subclassed and reinstantiated.
Definition: Parser.h:347
ParseResult codeCompleteDialectName()
The set of various code completion methods.
Definition: Parser.cpp:402
ParseResult parseAffineExprReference(ArrayRef< std::pair< StringRef, AffineExpr >> symbolSet, AffineExpr &expr)
Attribute parseAttribute(Type type={})
Parse an arbitrary attribute with an optional type.
const llvm::SourceMgr & getSourceMgr()
Definition: Parser.h:38
StringRef getTokenSpelling() const
Definition: Parser.h:103
ParseResult parseLocationInstance(LocationAttr &loc)
Parse a raw location instance.
void consumeToken()
Advance the current lexer onto the next token.
Definition: Parser.h:115
ParseResult codeCompleteExpectedTokens(ArrayRef< StringRef > tokens)
Definition: Parser.cpp:453
Attribute codeCompleteAttribute()
Definition: Parser.cpp:462
ParseResult parseIntegerInDimensionList(int64_t &value)
Definition: TypeParser.cpp:564
ParseResult parseAttributeDict(NamedAttrList &attributes)
Parse an attribute dictionary.
ParseResult parseDialectSymbolBody(StringRef &body, bool &isCodeCompletion)
Parse the body of a dialect symbol, which starts and ends with <>'s, and may be recursive.
ShapedType parseElementsLiteralType(Type type)
Shaped type for elements attribute.
Type parseComplexType()
Parse a complex type.
Definition: TypeParser.cpp:130
ParseResult parseDimensionListRanked(SmallVectorImpl< int64_t > &dimensions, bool allowDynamic=true, bool withTrailingX=true)
Parse a dimension list of a tensor or memref type.
Definition: TypeParser.cpp:527
ParseResult parseFunctionResultTypes(SmallVectorImpl< Type > &elements)
Parse a function result type.
Definition: TypeParser.cpp:81
ParseResult codeCompleteDialectOrElidedOpName(SMLoc loc)
Definition: Parser.cpp:417
ParseResult parseDialectSymbolBody(StringRef &body)
Definition: Parser.h:67
MLIRContext * getContext() const
Definition: Parser.h:37
Attribute parseDistinctAttr(Type type)
Parse a distinct attribute.
InFlightDiagnostic emitWrongTokenError(const Twine &message={})
Emit an error about a "wrong token".
Definition: Parser.cpp:215
ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())
Parse a list of comma-separated items with an optional delimiter.
Definition: Parser.cpp:84
VectorType parseVectorType()
Parse a vector type.
Definition: TypeParser.cpp:453
OptionalParseResult parseOptionalInteger(APInt &result)
Parse an optional integer value from the stream.
Definition: Parser.cpp:275
ParseResult parseFusedLocation(LocationAttr &loc)
Parse a fused location instance.
Type parseFunctionType()
Parse a function type.
Definition: TypeParser.cpp:153
Attribute parseSparseElementsAttr(Type attrType)
Parse a sparse elements attribute.
ParseResult parseAffineMapOrIntegerSetReference(AffineMap &map, IntegerSet &set)
Parse a reference to either an affine map, expr, or an integer set.
OptionalParseResult parseOptionalAttribute(Attribute &attribute, Type type={})
Parse an optional attribute with the provided type.
bool isCurrentTokenAKeyword() const
Returns true if the current token corresponds to a keyword.
Definition: Parser.h:149
ParseResult codeCompleteStringDialectOrOperationName(StringRef name)
Definition: Parser.cpp:440
ParseResult parseFloatFromIntegerLiteral(std::optional< APFloat > &result, const Token &tok, bool isNegative, const llvm::fltSemantics &semantics, size_t typeSizeInBits)
Parse a floating point value from an integer literal token.
Definition: Parser.cpp:312
ParseResult codeCompleteOptionalTokens(ArrayRef< StringRef > tokens)
Definition: Parser.cpp:457
ParseResult parseAffineMapOfSSAIds(AffineMap &map, function_ref< ParseResult(bool)> parseElement, Delimiter delimiter)
Parse an AffineMap where the dim and symbol identifiers are SSA ids.
Attribute parseFloatAttr(Type type, bool isNegative)
Parse a float attribute.
ParseResult parseCallSiteLocation(LocationAttr &loc)
Parse a callsite location instance.
ParseResult parseIntegerSetReference(IntegerSet &set)
ParseResult parseNameOrFileLineColLocation(LocationAttr &loc)
Parse a name or FileLineCol location instance.
ParseResult parseTypeListNoParens(SmallVectorImpl< Type > &elements)
Parse a list of types without an enclosing parenthesis.
Definition: TypeParser.cpp:97
void resetToken(const char *tokPos)
Reset the parser to the given lexer position.
Definition: Parser.h:130
ParseResult parseAffineExprOfSSAIds(AffineExpr &expr, function_ref< ParseResult(bool)> parseElement)
Parse an AffineExpr where dim and symbol identifiers are SSA ids.
Attribute parseExtendedAttr(Type type)
Parse an extended attribute.
const Token & getToken() const
Return the current token the parser is inspecting.
Definition: Parser.h:102
ParseResult parseCommaSeparatedList(function_ref< ParseResult()> parseElementFn)
Parse a comma separated list of elements that must have at least one entry in it.
Definition: Parser.h:57
FailureOr< AsmDialectResourceHandle > parseResourceHandle(const OpAsmDialectInterface *dialect, StringRef &name)
Parse a handle to a dialect resource within the assembly format.
Definition: Parser.cpp:360
Type parseTensorType()
Parse a tensor type.
Definition: TypeParser.cpp:369
bool consumeIf(Token::Kind kind)
If the current token has the specified kind, consume it and return true.
Definition: Parser.h:107
OptionalParseResult parseOptionalAttributeWithToken(Token::Kind kind, AttributeT &attr, Type type={})
Parse an optional attribute that is demarcated by a specific token.
Definition: Parser.h:237
Attribute codeCompleteDialectSymbol(const llvm::StringMap< Attribute > &aliases)
Definition: Parser.cpp:473
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
llvm::function_ref< Fn > function_ref
Definition: LLVM.h:147
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class refers to all of the state maintained globally by the parser, such as the current lexer po...
Definition: ParserState.h:51
const ParserConfig & config
The configuration used to setup the parser.
Definition: ParserState.h:63
Lexer lex
The lexer for the source file we're parsing.
Definition: ParserState.h:66
Token curToken
This is the next token that hasn't been consumed yet.
Definition: ParserState.h:69