MLIR  20.0.0git
AsmParserImpl.h
Go to the documentation of this file.
1 //===- AsmParserImpl.h - MLIR AsmParserImpl 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_ASMPARSERIMPL_H
10 #define MLIR_LIB_ASMPARSER_ASMPARSERIMPL_H
11 
12 #include "Parser.h"
14 #include "mlir/IR/Builders.h"
16 #include "llvm/Support/Base64.h"
17 #include <optional>
18 
19 namespace mlir {
20 namespace detail {
21 //===----------------------------------------------------------------------===//
22 // AsmParserImpl
23 //===----------------------------------------------------------------------===//
24 
25 /// This class provides the implementation of the generic parser methods within
26 /// AsmParser.
27 template <typename BaseT>
28 class AsmParserImpl : public BaseT {
29 public:
32  ~AsmParserImpl() override = default;
33 
34  /// Return the location of the original name token.
35  SMLoc getNameLoc() const override { return nameLoc; }
36 
37  //===--------------------------------------------------------------------===//
38  // Utilities
39  //===--------------------------------------------------------------------===//
40 
41  /// Return if any errors were emitted during parsing.
42  bool didEmitError() const { return emittedError; }
43 
44  /// Emit a diagnostic at the specified location and return failure.
45  InFlightDiagnostic emitError(SMLoc loc, const Twine &message) override {
46  emittedError = true;
47  return parser.emitError(loc, message);
48  }
49 
50  /// Return a builder which provides useful access to MLIRContext, global
51  /// objects like types and attributes.
52  Builder &getBuilder() const override { return parser.builder; }
53 
54  /// Get the location of the next token and store it into the argument. This
55  /// always succeeds.
56  SMLoc getCurrentLocation() override { return parser.getToken().getLoc(); }
57 
58  /// Re-encode the given source location as an MLIR location and return it.
59  Location getEncodedSourceLoc(SMLoc loc) override {
60  return parser.getEncodedSourceLocation(loc);
61  }
62 
63  //===--------------------------------------------------------------------===//
64  // Token Parsing
65  //===--------------------------------------------------------------------===//
66 
68 
69  /// Parse a `->` token.
70  ParseResult parseArrow() override {
71  return parser.parseToken(Token::arrow, "expected '->'");
72  }
73 
74  /// Parses a `->` if present.
75  ParseResult parseOptionalArrow() override {
76  return success(parser.consumeIf(Token::arrow));
77  }
78 
79  /// Parse a '{' token.
80  ParseResult parseLBrace() override {
81  return parser.parseToken(Token::l_brace, "expected '{'");
82  }
83 
84  /// Parse a '{' token if present
85  ParseResult parseOptionalLBrace() override {
86  return success(parser.consumeIf(Token::l_brace));
87  }
88 
89  /// Parse a `}` token.
90  ParseResult parseRBrace() override {
91  return parser.parseToken(Token::r_brace, "expected '}'");
92  }
93 
94  /// Parse a `}` token if present
95  ParseResult parseOptionalRBrace() override {
96  return success(parser.consumeIf(Token::r_brace));
97  }
98 
99  /// Parse a `:` token.
100  ParseResult parseColon() override {
101  return parser.parseToken(Token::colon, "expected ':'");
102  }
103 
104  /// Parse a `:` token if present.
105  ParseResult parseOptionalColon() override {
106  return success(parser.consumeIf(Token::colon));
107  }
108 
109  /// Parse a `,` token.
110  ParseResult parseComma() override {
111  return parser.parseToken(Token::comma, "expected ','");
112  }
113 
114  /// Parse a `,` token if present.
115  ParseResult parseOptionalComma() override {
116  return success(parser.consumeIf(Token::comma));
117  }
118 
119  /// Parses a `...`.
120  ParseResult parseEllipsis() override {
121  return parser.parseToken(Token::ellipsis, "expected '...'");
122  }
123 
124  /// Parses a `...` if present.
125  ParseResult parseOptionalEllipsis() override {
126  return success(parser.consumeIf(Token::ellipsis));
127  }
128 
129  /// Parse a `=` token.
130  ParseResult parseEqual() override {
131  return parser.parseToken(Token::equal, "expected '='");
132  }
133 
134  /// Parse a `=` token if present.
135  ParseResult parseOptionalEqual() override {
136  return success(parser.consumeIf(Token::equal));
137  }
138 
139  /// Parse a '<' token.
140  ParseResult parseLess() override {
141  return parser.parseToken(Token::less, "expected '<'");
142  }
143 
144  /// Parse a `<` token if present.
145  ParseResult parseOptionalLess() override {
146  return success(parser.consumeIf(Token::less));
147  }
148 
149  /// Parse a '>' token.
150  ParseResult parseGreater() override {
151  return parser.parseToken(Token::greater, "expected '>'");
152  }
153 
154  /// Parse a `>` token if present.
155  ParseResult parseOptionalGreater() override {
156  return success(parser.consumeIf(Token::greater));
157  }
158 
159  /// Parse a `(` token.
160  ParseResult parseLParen() override {
161  return parser.parseToken(Token::l_paren, "expected '('");
162  }
163 
164  /// Parses a '(' if present.
165  ParseResult parseOptionalLParen() override {
166  return success(parser.consumeIf(Token::l_paren));
167  }
168 
169  /// Parse a `)` token.
170  ParseResult parseRParen() override {
171  return parser.parseToken(Token::r_paren, "expected ')'");
172  }
173 
174  /// Parses a ')' if present.
175  ParseResult parseOptionalRParen() override {
176  return success(parser.consumeIf(Token::r_paren));
177  }
178 
179  /// Parse a `[` token.
180  ParseResult parseLSquare() override {
181  return parser.parseToken(Token::l_square, "expected '['");
182  }
183 
184  /// Parses a '[' if present.
185  ParseResult parseOptionalLSquare() override {
186  return success(parser.consumeIf(Token::l_square));
187  }
188 
189  /// Parse a `]` token.
190  ParseResult parseRSquare() override {
191  return parser.parseToken(Token::r_square, "expected ']'");
192  }
193 
194  /// Parses a ']' if present.
195  ParseResult parseOptionalRSquare() override {
196  return success(parser.consumeIf(Token::r_square));
197  }
198 
199  /// Parses a '?' token.
200  ParseResult parseQuestion() override {
201  return parser.parseToken(Token::question, "expected '?'");
202  }
203 
204  /// Parses a '?' if present.
205  ParseResult parseOptionalQuestion() override {
206  return success(parser.consumeIf(Token::question));
207  }
208 
209  /// Parses a '*' token.
210  ParseResult parseStar() override {
211  return parser.parseToken(Token::star, "expected '*'");
212  }
213 
214  /// Parses a '*' if present.
215  ParseResult parseOptionalStar() override {
216  return success(parser.consumeIf(Token::star));
217  }
218 
219  /// Parses a '+' token.
220  ParseResult parsePlus() override {
221  return parser.parseToken(Token::plus, "expected '+'");
222  }
223 
224  /// Parses a '+' token if present.
225  ParseResult parseOptionalPlus() override {
226  return success(parser.consumeIf(Token::plus));
227  }
228 
229  /// Parses a '-' token.
230  ParseResult parseMinus() override {
231  return parser.parseToken(Token::minus, "expected '-'");
232  }
233 
234  /// Parses a '-' token if present.
235  ParseResult parseOptionalMinus() override {
236  return success(parser.consumeIf(Token::minus));
237  }
238 
239  /// Parse a '|' token.
240  ParseResult parseVerticalBar() override {
241  return parser.parseToken(Token::vertical_bar, "expected '|'");
242  }
243 
244  /// Parse a '|' token if present.
245  ParseResult parseOptionalVerticalBar() override {
246  return success(parser.consumeIf(Token::vertical_bar));
247  }
248 
249  /// Parses a quoted string token if present.
250  ParseResult parseOptionalString(std::string *string) override {
251  if (!parser.getToken().is(Token::string))
252  return failure();
253 
254  if (string)
255  *string = parser.getToken().getStringValue();
257  return success();
258  }
259 
260  /// Parses a Base64 encoded string of bytes.
261  ParseResult parseBase64Bytes(std::vector<char> *bytes) override {
262  auto loc = getCurrentLocation();
263  if (!parser.getToken().is(Token::string))
264  return emitError(loc, "expected string");
265 
266  if (bytes) {
267  // decodeBase64 doesn't modify its input so we can use the token spelling
268  // and just slice off the quotes/whitespaces if there are any. Whitespace
269  // and quotes cannot appear as part of a (standard) base64 encoded string,
270  // so this is safe to do.
271  StringRef b64QuotedString = parser.getTokenSpelling();
272  StringRef b64String =
273  b64QuotedString.ltrim("\" \t\n\v\f\r").rtrim("\" \t\n\v\f\r");
274  if (auto err = llvm::decodeBase64(b64String, *bytes))
275  return emitError(loc, toString(std::move(err)));
276  }
277 
279  return success();
280  }
281 
282  /// Parse a floating point value with given semantics from the stream. Since
283  /// this implementation parses the string as double precision and only
284  /// afterwards converts the value to the requested semantic, precision may be
285  /// lost.
286  ParseResult parseFloat(const llvm::fltSemantics &semantics,
287  APFloat &result) override {
288  bool isNegative = parser.consumeIf(Token::minus);
289  Token curTok = parser.getToken();
290  SMLoc loc = curTok.getLoc();
291 
292  // Check for a floating point value.
293  if (curTok.is(Token::floatliteral)) {
294  auto val = curTok.getFloatingPointValue();
295  if (!val)
296  return emitError(loc, "floating point value too large");
297  parser.consumeToken(Token::floatliteral);
298  result = APFloat(isNegative ? -*val : *val);
299  bool losesInfo;
300  result.convert(semantics, APFloat::rmNearestTiesToEven, &losesInfo);
301  return success();
302  }
303 
304  // Check for a hexadecimal float value.
305  if (curTok.is(Token::integer)) {
306  std::optional<APFloat> apResult;
308  apResult, curTok, isNegative, semantics,
309  APFloat::semanticsSizeInBits(semantics))))
310  return failure();
311 
312  result = *apResult;
313  parser.consumeToken(Token::integer);
314  return success();
315  }
316 
317  return emitError(loc, "expected floating point literal");
318  }
319 
320  /// Parse a floating point value from the stream.
321  ParseResult parseFloat(double &result) override {
322  llvm::APFloat apResult(0.0);
323  if (parseFloat(APFloat::IEEEdouble(), apResult))
324  return failure();
325 
326  result = apResult.convertToDouble();
327  return success();
328  }
329 
330  /// Parse an optional integer value from the stream.
331  OptionalParseResult parseOptionalInteger(APInt &result) override {
332  return parser.parseOptionalInteger(result);
333  }
334 
335  /// Parse an optional integer value from the stream.
337  return parser.parseOptionalDecimalInteger(result);
338  }
339 
340  /// Parse a list of comma-separated items with an optional delimiter. If a
341  /// delimiter is provided, then an empty list is allowed. If not, then at
342  /// least one element will be parsed.
343  ParseResult parseCommaSeparatedList(Delimiter delimiter,
344  function_ref<ParseResult()> parseElt,
345  StringRef contextMessage) override {
346  return parser.parseCommaSeparatedList(delimiter, parseElt, contextMessage);
347  }
348 
349  //===--------------------------------------------------------------------===//
350  // Keyword Parsing
351  //===--------------------------------------------------------------------===//
352 
353  ParseResult parseKeyword(StringRef keyword, const Twine &msg) override {
355  return parser.codeCompleteExpectedTokens(keyword);
356 
357  auto loc = getCurrentLocation();
358  if (parseOptionalKeyword(keyword))
359  return emitError(loc, "expected '") << keyword << "'" << msg;
360  return success();
361  }
363 
364  /// Parse the given keyword if present.
365  ParseResult parseOptionalKeyword(StringRef keyword) override {
367  return parser.codeCompleteOptionalTokens(keyword);
368 
369  // Check that the current token has the same spelling.
371  parser.getTokenSpelling() != keyword)
372  return failure();
374  return success();
375  }
376 
377  /// Parse a keyword, if present, into 'keyword'.
378  ParseResult parseOptionalKeyword(StringRef *keyword) override {
379  // Check that the current token is a keyword.
381  return failure();
382 
383  *keyword = parser.getTokenSpelling();
385  return success();
386  }
387 
388  /// Parse a keyword if it is one of the 'allowedKeywords'.
389  ParseResult
390  parseOptionalKeyword(StringRef *keyword,
391  ArrayRef<StringRef> allowedKeywords) override {
393  return parser.codeCompleteOptionalTokens(allowedKeywords);
394 
395  // Check that the current token is a keyword.
397  return failure();
398 
399  StringRef currentKeyword = parser.getTokenSpelling();
400  if (llvm::is_contained(allowedKeywords, currentKeyword)) {
401  *keyword = currentKeyword;
403  return success();
404  }
405 
406  return failure();
407  }
408 
409  /// Parse an optional keyword or string and set instance into 'result'.`
410  ParseResult parseOptionalKeywordOrString(std::string *result) override {
411  StringRef keyword;
412  if (succeeded(parseOptionalKeyword(&keyword))) {
413  *result = keyword.str();
414  return success();
415  }
416 
417  return parseOptionalString(result);
418  }
419 
420  //===--------------------------------------------------------------------===//
421  // Attribute Parsing
422  //===--------------------------------------------------------------------===//
423 
424  /// Parse an arbitrary attribute and return it in result.
425  ParseResult parseAttribute(Attribute &result, Type type) override {
426  result = parser.parseAttribute(type);
427  return success(static_cast<bool>(result));
428  }
429 
430  /// Parse a custom attribute with the provided callback, unless the next
431  /// token is `#`, in which case the generic parser is invoked.
433  Attribute &result, Type type,
434  function_ref<ParseResult(Attribute &result, Type type)> parseAttribute)
435  override {
436  if (parser.getToken().isNot(Token::hash_identifier))
437  return parseAttribute(result, type);
438  result = parser.parseAttribute(type);
439  return success(static_cast<bool>(result));
440  }
441 
442  /// Parse a custom attribute with the provided callback, unless the next
443  /// token is `#`, in which case the generic parser is invoked.
445  Type &result,
446  function_ref<ParseResult(Type &result)> parseType) override {
447  if (parser.getToken().isNot(Token::exclamation_identifier))
448  return parseType(result);
449  result = parser.parseType();
450  return success(static_cast<bool>(result));
451  }
452 
454  Type type) override {
455  return parser.parseOptionalAttribute(result, type);
456  }
458  Type type) override {
459  return parser.parseOptionalAttribute(result, type);
460  }
462  Type type) override {
463  return parser.parseOptionalAttribute(result, type);
464  }
466  Type type) override {
467  return parser.parseOptionalAttribute(result, type);
468  }
469 
470  /// Parse a named dictionary into 'result' if it is present.
471  ParseResult parseOptionalAttrDict(NamedAttrList &result) override {
472  if (parser.getToken().isNot(Token::l_brace))
473  return success();
474  return parser.parseAttributeDict(result);
475  }
476 
477  /// Parse a named dictionary into 'result' if the `attributes` keyword is
478  /// present.
479  ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result) override {
480  if (failed(parseOptionalKeyword("attributes")))
481  return success();
482  return parser.parseAttributeDict(result);
483  }
484 
485  /// Parse an affine map instance into 'map'.
486  ParseResult parseAffineMap(AffineMap &map) override {
487  return parser.parseAffineMapReference(map);
488  }
489 
490  /// Parse an affine expr instance into 'expr' using the already computed
491  /// mapping from symbols to affine expressions in 'symbolSet'.
492  ParseResult
493  parseAffineExpr(ArrayRef<std::pair<StringRef, AffineExpr>> symbolSet,
494  AffineExpr &expr) override {
495  return parser.parseAffineExprReference(symbolSet, expr);
496  }
497 
498  /// Parse an integer set instance into 'set'.
499  ParseResult parseIntegerSet(IntegerSet &set) override {
500  return parser.parseIntegerSetReference(set);
501  }
502 
503  //===--------------------------------------------------------------------===//
504  // Identifier Parsing
505  //===--------------------------------------------------------------------===//
506 
507  /// Parse an optional @-identifier and store it (without the '@' symbol) in a
508  /// string attribute named 'attrName'.
509  ParseResult parseOptionalSymbolName(StringAttr &result) override {
510  Token atToken = parser.getToken();
511  if (atToken.isNot(Token::at_identifier))
512  return failure();
513 
514  result = getBuilder().getStringAttr(atToken.getSymbolReference());
516 
517  // If we are populating the assembly parser state, record this as a symbol
518  // reference.
519  if (parser.getState().asmState) {
521  atToken.getLocRange());
522  }
523  return success();
524  }
525 
526  //===--------------------------------------------------------------------===//
527  // Resource Parsing
528  //===--------------------------------------------------------------------===//
529 
530  /// Parse a handle to a resource within the assembly format.
531  FailureOr<AsmDialectResourceHandle>
532  parseResourceHandle(Dialect *dialect) override {
533  const auto *interface = dyn_cast<OpAsmDialectInterface>(dialect);
534  if (!interface) {
535  return parser.emitError() << "dialect '" << dialect->getNamespace()
536  << "' does not expect resource handles";
537  }
538  StringRef resourceName;
539  return parser.parseResourceHandle(interface, resourceName);
540  }
541 
542  //===--------------------------------------------------------------------===//
543  // Type Parsing
544  //===--------------------------------------------------------------------===//
545 
546  /// Parse a type.
547  ParseResult parseType(Type &result) override {
548  return failure(!(result = parser.parseType()));
549  }
550 
551  /// Parse an optional type.
553  return parser.parseOptionalType(result);
554  }
555 
556  /// Parse an arrow followed by a type list.
557  ParseResult parseArrowTypeList(SmallVectorImpl<Type> &result) override {
558  if (parseArrow() || parser.parseFunctionResultTypes(result))
559  return failure();
560  return success();
561  }
562 
563  /// Parse an optional arrow followed by a type list.
564  ParseResult
566  if (!parser.consumeIf(Token::arrow))
567  return success();
568  return parser.parseFunctionResultTypes(result);
569  }
570 
571  /// Parse a colon followed by a type.
572  ParseResult parseColonType(Type &result) override {
573  return failure(parser.parseToken(Token::colon, "expected ':'") ||
574  !(result = parser.parseType()));
575  }
576 
577  /// Parse a colon followed by a type list, which must have at least one type.
578  ParseResult parseColonTypeList(SmallVectorImpl<Type> &result) override {
579  if (parser.parseToken(Token::colon, "expected ':'"))
580  return failure();
581  return parser.parseTypeListNoParens(result);
582  }
583 
584  /// Parse an optional colon followed by a type list, which if present must
585  /// have at least one type.
586  ParseResult
588  if (!parser.consumeIf(Token::colon))
589  return success();
590  return parser.parseTypeListNoParens(result);
591  }
592 
594  bool allowDynamic,
595  bool withTrailingX) override {
596  return parser.parseDimensionListRanked(dimensions, allowDynamic,
597  withTrailingX);
598  }
599 
600  ParseResult parseXInDimensionList() override {
601  return parser.parseXInDimensionList();
602  }
603 
604  LogicalResult pushCyclicParsing(const void *opaquePointer) override {
605  return success(parser.getState().cyclicParsingStack.insert(opaquePointer));
606  }
607 
608  void popCyclicParsing() override {
609  parser.getState().cyclicParsingStack.pop_back();
610  }
611 
612  //===--------------------------------------------------------------------===//
613  // Code Completion
614  //===--------------------------------------------------------------------===//
615 
616  /// Parse a keyword, or an empty string if the current location signals a code
617  /// completion.
618  ParseResult parseKeywordOrCompletion(StringRef *keyword) override {
619  Token tok = parser.getToken();
620  if (tok.isCodeCompletion() && tok.getSpelling().empty()) {
621  *keyword = "";
622  return success();
623  }
624  return parseKeyword(keyword);
625  }
626 
627  /// Signal the code completion of a set of expected tokens.
629  Token tok = parser.getToken();
630  if (tok.isCodeCompletion() && tok.getSpelling().empty())
631  (void)parser.codeCompleteExpectedTokens(tokens);
632  }
633 
634 protected:
635  /// The source location of the dialect symbol.
636  SMLoc nameLoc;
637 
638  /// The main parser.
640 
641  /// A flag that indicates if any errors were emitted during parsing.
642  bool emittedError = false;
643 };
644 } // namespace detail
645 } // namespace mlir
646 
647 #endif // MLIR_LIB_ASMPARSER_ASMPARSERIMPL_H
Base type for affine expression.
Definition: AffineExpr.h:68
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition: AffineMap.h:46
void addUses(Value value, ArrayRef< SMLoc > locations)
Add a source uses of the given value.
Delimiter
These are the supported delimiters around operand lists and region argument lists,...
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
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
StringAttr getStringAttr(const Twine &bytes)
Definition: Builders.cpp:293
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
StringRef getNamespace() const
Definition: Dialect.h:54
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:313
An integer set representing a conjunction of one or more affine equalities and inequalities.
Definition: IntegerSet.h:44
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
This class implements Optional functionality for ParseResult.
Definition: OpDefinition.h:39
This represents a token in the MLIR syntax.
Definition: Token.h:20
SMRange getLocRange() const
Definition: Token.cpp:30
SMLoc getLoc() const
Definition: Token.cpp:24
bool is(Kind k) const
Definition: Token.h:38
std::string getStringValue() const
Given a token containing a string literal, return its value, including removing the quote characters ...
Definition: Token.cpp:86
std::string getSymbolReference() const
Given a token containing a symbol reference, return the unescaped string value.
Definition: Token.cpp:153
std::optional< double > getFloatingPointValue() const
For a floatliteral token, return its value as a double.
Definition: Token.cpp:56
bool isNot(Kind k) const
Definition: Token.h:50
bool isCodeCompletion() const
Returns true if the current token represents a code completion.
Definition: Token.h:62
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 provides the implementation of the generic parser methods within AsmParser.
Definition: AsmParserImpl.h:28
ParseResult parseOptionalPlus() override
Parses a '+' token if present.
ParseResult parseOptionalLBrace() override
Parse a '{' token if present.
Definition: AsmParserImpl.h:85
ParseResult parseOptionalArrowTypeList(SmallVectorImpl< Type > &result) override
Parse an optional arrow followed by a type list.
ParseResult parseOptionalStar() override
Parses a '*' if present.
ParseResult parseOptionalMinus() override
Parses a '-' token if present.
ParseResult parseColon() override
Parse a : token.
ParseResult parseFloat(double &result) override
Parse a floating point value from the stream.
InFlightDiagnostic emitError(SMLoc loc, const Twine &message) override
Emit a diagnostic at the specified location and return failure.
Definition: AsmParserImpl.h:45
ParseResult parseMinus() override
Parses a '-' token.
ParseResult parseOptionalGreater() override
Parse a > token if present.
ParseResult parseOptionalEllipsis() override
Parses a ... if present.
ParseResult parseColonType(Type &result) override
Parse a colon followed by a type.
ParseResult parseArrow() override
Parse a -> token.
Definition: AsmParserImpl.h:70
void codeCompleteExpectedTokens(ArrayRef< StringRef > tokens) override
Signal the code completion of a set of expected tokens.
ParseResult parseEllipsis() override
Parses a ....
ParseResult parseLParen() override
Parse a ( token.
ParseResult parseOptionalKeywordOrString(std::string *result) override
Parse an optional keyword or string and set instance into 'result'.`.
Location getEncodedSourceLoc(SMLoc loc) override
Re-encode the given source location as an MLIR location and return it.
Definition: AsmParserImpl.h:59
ParseResult parseOptionalAttrDict(NamedAttrList &result) override
Parse a named dictionary into 'result' if it is present.
SMLoc nameLoc
The source location of the dialect symbol.
ParseResult parseRParen() override
Parse a ) token.
ParseResult parseQuestion() override
Parses a '?' token.
OptionalParseResult parseOptionalAttribute(ArrayAttr &result, Type type) override
ParseResult parseCustomAttributeWithFallback(Attribute &result, Type type, function_ref< ParseResult(Attribute &result, Type type)> parseAttribute) override
Parse a custom attribute with the provided callback, unless the next token is #, in which case the ge...
OptionalParseResult parseOptionalType(Type &result) override
Parse an optional type.
ParseResult parseIntegerSet(IntegerSet &set) override
Parse an integer set instance into 'set'.
ParseResult parseFloat(const llvm::fltSemantics &semantics, APFloat &result) override
Parse a floating point value with given semantics from the stream.
SMLoc getNameLoc() const override
Return the location of the original name token.
Definition: AsmParserImpl.h:35
ParseResult parseOptionalQuestion() override
Parses a '?' if present.
ParseResult parseLBrace() override
Parse a '{' token.
Definition: AsmParserImpl.h:80
ParseResult parseAffineExpr(ArrayRef< std::pair< StringRef, AffineExpr >> symbolSet, AffineExpr &expr) override
Parse an affine expr instance into 'expr' using the already computed mapping from symbols to affine e...
ParseResult parseOptionalComma() override
Parse a , token if present.
ParseResult parsePlus() override
Parses a '+' token.
ParseResult parseLess() override
Parse a '<' token.
ParseResult parseOptionalKeyword(StringRef *keyword, ArrayRef< StringRef > allowedKeywords) override
Parse a keyword if it is one of the 'allowedKeywords'.
OptionalParseResult parseOptionalDecimalInteger(APInt &result) override
Parse an optional integer value from the stream.
ParseResult parseRBrace() override
Parse a } token.
Definition: AsmParserImpl.h:90
ParseResult parseOptionalRBrace() override
Parse a } token if present.
Definition: AsmParserImpl.h:95
ParseResult parseGreater() override
Parse a '>' token.
FailureOr< AsmDialectResourceHandle > parseResourceHandle(Dialect *dialect) override
Parse a handle to a resource within the assembly format.
ParseResult parseColonTypeList(SmallVectorImpl< Type > &result) override
Parse a colon followed by a type list, which must have at least one type.
ParseResult parseOptionalLess() override
Parse a < token if present.
OptionalParseResult parseOptionalAttribute(StringAttr &result, Type type) override
OptionalParseResult parseOptionalAttribute(Attribute &result, Type type) override
ParseResult parseAttribute(Attribute &result, Type type) override
Parse an arbitrary attribute and return it in result.
Parser & parser
The main parser.
LogicalResult pushCyclicParsing(const void *opaquePointer) override
ParseResult parseKeywordOrCompletion(StringRef *keyword) override
Parse a keyword, or an empty string if the current location signals a code completion.
AsmParserImpl(SMLoc nameLoc, Parser &parser)
Definition: AsmParserImpl.h:30
ParseResult parseOptionalKeyword(StringRef keyword) override
Parse the given keyword if present.
bool didEmitError() const
Return if any errors were emitted during parsing.
Definition: AsmParserImpl.h:42
ParseResult parseComma() override
Parse a , token.
OptionalParseResult parseOptionalInteger(APInt &result) override
Parse an optional integer value from the stream.
ParseResult parseRSquare() override
Parse a ] token.
Builder & getBuilder() const override
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
Definition: AsmParserImpl.h:52
ParseResult parseLSquare() override
Parse a [ token.
ParseResult parseBase64Bytes(std::vector< char > *bytes) override
Parses a Base64 encoded string of bytes.
ParseResult parseOptionalArrow() override
Parses a -> if present.
Definition: AsmParserImpl.h:75
~AsmParserImpl() override=default
ParseResult parseDimensionList(SmallVectorImpl< int64_t > &dimensions, bool allowDynamic, bool withTrailingX) override
ParseResult parseEqual() override
Parse a = token.
ParseResult parseCustomTypeWithFallback(Type &result, function_ref< ParseResult(Type &result)> parseType) override
Parse a custom attribute with the provided callback, unless the next token is #, in which case the ge...
ParseResult parseOptionalColonTypeList(SmallVectorImpl< Type > &result) override
Parse an optional colon followed by a type list, which if present must have at least one type.
ParseResult parseOptionalVerticalBar() override
Parse a '|' token if present.
ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result) override
Parse a named dictionary into 'result' if the attributes keyword is present.
ParseResult parseStar() override
Parses a '*' token.
ParseResult parseOptionalLParen() override
Parses a '(' if present.
ParseResult parseOptionalEqual() override
Parse a = token if present.
void popCyclicParsing() override
ParseResult parseOptionalLSquare() override
Parses a '[' if present.
ParseResult parseOptionalKeyword(StringRef *keyword) override
Parse a keyword, if present, into 'keyword'.
ParseResult parseAffineMap(AffineMap &map) override
Parse an affine map instance into 'map'.
ParseResult parseOptionalSymbolName(StringAttr &result) override
Parse an optional -identifier and store it (without the '@' symbol) in a string attribute named 'attr...
ParseResult parseVerticalBar() override
Parse a '|' token.
OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result, Type type) override
SMLoc getCurrentLocation() override
Get the location of the next token and store it into the argument.
Definition: AsmParserImpl.h:56
bool emittedError
A flag that indicates if any errors were emitted during parsing.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
ParseResult parseOptionalColon() override
Parse a : token if present.
ParseResult parseOptionalString(std::string *string) override
Parses a quoted string token if present.
ParseResult parseOptionalRParen() override
Parses a ')' if present.
ParseResult parseXInDimensionList() override
ParseResult parseKeyword(StringRef keyword, const Twine &msg) override
ParseResult parseType(Type &result) override
Parse a type.
ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElt, StringRef contextMessage) override
Parse a list of comma-separated items with an optional delimiter.
ParseResult parseOptionalRSquare() override
Parses a ']' if present.
ParseResult parseArrowTypeList(SmallVectorImpl< Type > &result) override
Parse an arrow followed by a type list.
This class implement support for parsing global entities like attributes and types.
Definition: Parser.h:26
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:608
OptionalParseResult parseOptionalType(Type &type)
Optionally parse a type.
Definition: TypeParser.cpp:32
ParseResult parseToken(Token::Kind expectedToken, const Twine &message)
Consume the specified token if present and return success.
Definition: Parser.cpp:267
OptionalParseResult parseOptionalDecimalInteger(APInt &result)
Parse an optional integer value only in decimal format from the stream.
Definition: Parser.cpp:312
Builder builder
Definition: Parser.h:30
Type parseType()
Parse an arbitrary type.
Definition: TypeParser.cpp:75
ParserState & getState() const
Definition: Parser.h:36
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
InFlightDiagnostic emitError(const Twine &message={})
Emit an error and return failure.
Definition: Parser.cpp:192
ParseResult parseAffineExprReference(ArrayRef< std::pair< StringRef, AffineExpr >> symbolSet, AffineExpr &expr)
Attribute parseAttribute(Type type={})
Parse an arbitrary attribute with an optional type.
StringRef getTokenSpelling() const
Definition: Parser.h:103
void consumeToken()
Advance the current lexer onto the next token.
Definition: Parser.h:118
ParseResult codeCompleteExpectedTokens(ArrayRef< StringRef > tokens)
Definition: Parser.cpp:488
ParseResult parseAttributeDict(NamedAttrList &attributes)
Parse an attribute dictionary.
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:543
ParseResult parseFunctionResultTypes(SmallVectorImpl< Type > &elements)
Parse a function result type.
Definition: TypeParser.cpp:86
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
OptionalParseResult parseOptionalInteger(APInt &result)
Parse an optional integer value from the stream.
Definition: Parser.cpp:275
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:161
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:351
ParseResult codeCompleteOptionalTokens(ArrayRef< StringRef > tokens)
Definition: Parser.cpp:492
ParseResult parseIntegerSetReference(IntegerSet &set)
ParseResult parseTypeListNoParens(SmallVectorImpl< Type > &elements)
Parse a list of types without an enclosing parenthesis.
Definition: TypeParser.cpp:102
const Token & getToken() const
Return the current token the parser is inspecting.
Definition: Parser.h:102
FailureOr< AsmDialectResourceHandle > parseResourceHandle(const OpAsmDialectInterface *dialect, StringRef &name)
Parse a handle to a dialect resource within the assembly format.
Definition: Parser.cpp:395
bool consumeIf(Token::Kind kind)
If the current token has the specified kind, consume it and return true.
Definition: Parser.h:110
Include the generated interface declarations.
StringRef toString(AsmResourceEntryKind kind)
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
SetVector< const void * > cyclicParsingStack
Stack of potentially cyclic mutable attributes or type currently being parsed.
Definition: ParserState.h:79
AsmParserState * asmState
An optional pointer to a struct containing high level parser state to be populated during parsing.
Definition: ParserState.h:83