MLIR  19.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  /// Return the last parsed token.
106  const Token &getLastToken() const { return state.lastToken; }
107 
108  /// If the current token has the specified kind, consume it and return true.
109  /// If not, return false.
110  bool consumeIf(Token::Kind kind) {
111  if (state.curToken.isNot(kind))
112  return false;
113  consumeToken(kind);
114  return true;
115  }
116 
117  /// Advance the current lexer onto the next token.
118  void consumeToken() {
119  assert(state.curToken.isNot(Token::eof, Token::error) &&
120  "shouldn't advance past EOF or errors");
123  }
124 
125  /// Advance the current lexer onto the next token, asserting what the expected
126  /// current token is. This is preferred to the above method because it leads
127  /// to more self-documenting code with better checking.
129  assert(state.curToken.is(kind) && "consumed an unexpected token");
130  consumeToken();
131  }
132 
133  /// Reset the parser to the given lexer position.
134  void resetToken(const char *tokPos) {
135  state.lex.resetPointer(tokPos);
138  }
139 
140  /// Consume the specified token if present and return success. On failure,
141  /// output a diagnostic and return failure.
142  ParseResult parseToken(Token::Kind expectedToken, const Twine &message);
143 
144  /// Parse an optional integer value from the stream.
146 
147  /// Parse a floating point value from an integer literal token.
148  ParseResult parseFloatFromIntegerLiteral(std::optional<APFloat> &result,
149  const Token &tok, bool isNegative,
150  const llvm::fltSemantics &semantics,
151  size_t typeSizeInBits);
152 
153  /// Returns true if the current token corresponds to a keyword.
154  bool isCurrentTokenAKeyword() const {
155  return getToken().isAny(Token::bare_identifier, Token::inttype) ||
156  getToken().isKeyword();
157  }
158 
159  /// Parse a keyword, if present, into 'keyword'.
160  ParseResult parseOptionalKeyword(StringRef *keyword);
161 
162  //===--------------------------------------------------------------------===//
163  // Resource Parsing
164  //===--------------------------------------------------------------------===//
165 
166  /// Parse a handle to a dialect resource within the assembly format.
168  parseResourceHandle(const OpAsmDialectInterface *dialect, StringRef &name);
170 
171  //===--------------------------------------------------------------------===//
172  // Type Parsing
173  //===--------------------------------------------------------------------===//
174 
175  /// Invoke the `getChecked` method of the given Attribute or Type class, using
176  /// the provided location to emit errors in the case of failure. Note that
177  /// unlike `OpBuilder::getType`, this method does not implicitly insert a
178  /// context parameter.
179  template <typename T, typename... ParamsT>
180  T getChecked(SMLoc loc, ParamsT &&...params) {
181  return T::getChecked([&] { return emitError(loc); },
182  std::forward<ParamsT>(params)...);
183  }
184 
188 
189  /// Optionally parse a type.
191 
192  /// Parse an arbitrary type.
193  Type parseType();
194 
195  /// Parse a complex type.
197 
198  /// Parse an extended type.
200 
201  /// Parse a function type.
203 
204  /// Parse a memref type.
206 
207  /// Parse a non function type.
209 
210  /// Parse a tensor type.
212 
213  /// Parse a tuple type.
215 
216  /// Parse a vector type.
217  VectorType parseVectorType();
219  SmallVectorImpl<bool> &scalableDims);
221  bool allowDynamic = true,
222  bool withTrailingX = true);
225 
226  //===--------------------------------------------------------------------===//
227  // Attribute Parsing
228  //===--------------------------------------------------------------------===//
229 
230  /// Parse an arbitrary attribute with an optional type.
231  Attribute parseAttribute(Type type = {});
232 
233  /// Parse an optional attribute with the provided type.
235  Type type = {});
236  OptionalParseResult parseOptionalAttribute(ArrayAttr &attribute, Type type);
237  OptionalParseResult parseOptionalAttribute(StringAttr &attribute, Type type);
238  OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result, Type type);
239 
240  /// Parse an optional attribute that is demarcated by a specific token.
241  template <typename AttributeT>
243  AttributeT &attr,
244  Type type = {}) {
245  if (getToken().isNot(kind))
246  return std::nullopt;
247 
248  if (Attribute parsedAttr = parseAttribute(type)) {
249  attr = cast<AttributeT>(parsedAttr);
250  return success();
251  }
252  return failure();
253  }
254 
255  /// Parse an attribute dictionary.
256  ParseResult parseAttributeDict(NamedAttrList &attributes);
257 
258  /// Parse a distinct attribute.
259  Attribute parseDistinctAttr(Type type);
260 
261  /// Parse an extended attribute.
262  Attribute parseExtendedAttr(Type type);
263 
264  /// Parse a float attribute.
265  Attribute parseFloatAttr(Type type, bool isNegative);
266 
267  /// Parse a decimal or a hexadecimal literal, which can be either an integer
268  /// or a float attribute.
269  Attribute parseDecOrHexAttr(Type type, bool isNegative);
270 
271  /// Parse a dense elements attribute.
272  Attribute parseDenseElementsAttr(Type attrType);
273  ShapedType parseElementsLiteralType(Type type);
274 
275  /// Parse a dense resource elements attribute.
276  Attribute parseDenseResourceElementsAttr(Type attrType);
277 
278  /// Parse a DenseArrayAttr.
279  Attribute parseDenseArrayAttr(Type type);
280 
281  /// Parse a sparse elements attribute.
282  Attribute parseSparseElementsAttr(Type attrType);
283 
284  /// Parse a strided layout attribute.
285  Attribute parseStridedLayoutAttr();
286 
287  //===--------------------------------------------------------------------===//
288  // Location Parsing
289  //===--------------------------------------------------------------------===//
290 
291  /// Parse a raw location instance.
292  ParseResult parseLocationInstance(LocationAttr &loc);
293 
294  /// Parse a callsite location instance.
295  ParseResult parseCallSiteLocation(LocationAttr &loc);
296 
297  /// Parse a fused location instance.
298  ParseResult parseFusedLocation(LocationAttr &loc);
299 
300  /// Parse a name or FileLineCol location instance.
301  ParseResult parseNameOrFileLineColLocation(LocationAttr &loc);
302 
303  //===--------------------------------------------------------------------===//
304  // Affine Parsing
305  //===--------------------------------------------------------------------===//
306 
307  /// Parse a reference to either an affine map, expr, or an integer set.
308  ParseResult parseAffineMapOrIntegerSetReference(AffineMap &map,
309  IntegerSet &set);
310  ParseResult parseAffineMapReference(AffineMap &map);
311  ParseResult
312  parseAffineExprReference(ArrayRef<std::pair<StringRef, AffineExpr>> symbolSet,
313  AffineExpr &expr);
314  ParseResult parseIntegerSetReference(IntegerSet &set);
315 
316  /// Parse an AffineMap where the dim and symbol identifiers are SSA ids.
317  ParseResult
318  parseAffineMapOfSSAIds(AffineMap &map,
319  function_ref<ParseResult(bool)> parseElement,
320  Delimiter delimiter);
321 
322  /// Parse an AffineExpr where dim and symbol identifiers are SSA ids.
323  ParseResult
324  parseAffineExprOfSSAIds(AffineExpr &expr,
325  function_ref<ParseResult(bool)> parseElement);
326 
327  //===--------------------------------------------------------------------===//
328  // Code Completion
329  //===--------------------------------------------------------------------===//
330 
331  /// The set of various code completion methods. Every completion method
332  /// returns `failure` to signal that parsing should abort after any desired
333  /// completions have been enqueued. Note that `failure` is does not mean
334  /// completion failed, it's just a signal to the parser to stop.
335 
336  ParseResult codeCompleteDialectName();
337  ParseResult codeCompleteOperationName(StringRef dialectName);
338  ParseResult codeCompleteDialectOrElidedOpName(SMLoc loc);
339  ParseResult codeCompleteStringDialectOrOperationName(StringRef name);
340  ParseResult codeCompleteExpectedTokens(ArrayRef<StringRef> tokens);
341  ParseResult codeCompleteOptionalTokens(ArrayRef<StringRef> tokens);
342 
343  Attribute codeCompleteAttribute();
344  Type codeCompleteType();
345  Attribute
346  codeCompleteDialectSymbol(const llvm::StringMap<Attribute> &aliases);
347  Type codeCompleteDialectSymbol(const llvm::StringMap<Type> &aliases);
348 
349 protected:
350  /// The Parser is subclassed and reinstantiated. Do not add additional
351  /// non-trivial state here, add it to the ParserState class.
353 };
354 } // namespace detail
355 } // namespace mlir
356 
357 #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
const Token & getLastToken() const
Return the last parsed token.
Definition: Parser.h:106
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:180
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:128
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:352
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:118
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:154
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:134
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:110
OptionalParseResult parseOptionalAttributeWithToken(Token::Kind kind, AttributeT &attr, Type type={})
Parse an optional attribute that is demarcated by a specific token.
Definition: Parser.h:242
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
Token lastToken
This is the last token that has been consumed.
Definition: ParserState.h:72