1 //===- Parser.cpp - MLIR Parser Implementation ----------------------------===//
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 // This file implements the parser for the MLIR textual form.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Parser.h"
14 #include "Lexer.h"
15 #include "mlir/Analysis/Verifier.h"
16 #include "mlir/IR/AffineExpr.h"
17 #include "mlir/IR/AffineMap.h"
18 #include "mlir/IR/Attributes.h"
19 #include "mlir/IR/Builders.h"
20 #include "mlir/IR/Dialect.h"
21 #include "mlir/IR/DialectImplementation.h"
22 #include "mlir/IR/IntegerSet.h"
23 #include "mlir/IR/Location.h"
24 #include "mlir/IR/MLIRContext.h"
25 #include "mlir/IR/Module.h"
26 #include "mlir/IR/OpImplementation.h"
27 #include "mlir/IR/StandardTypes.h"
28 #include "mlir/Support/STLExtras.h"
29 #include "llvm/ADT/APInt.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/StringSet.h"
32 #include "llvm/ADT/bit.h"
33 #include "llvm/Support/PrettyStackTrace.h"
34 #include "llvm/Support/SMLoc.h"
35 #include "llvm/Support/SourceMgr.h"
36 #include <algorithm>
37 using namespace mlir;
38 using llvm::MemoryBuffer;
39 using llvm::SMLoc;
40 using llvm::SourceMgr;
41 
42 namespace {
43 class Parser;
44 
45 //===----------------------------------------------------------------------===//
46 // SymbolState
47 //===----------------------------------------------------------------------===//
48 
49 /// This class contains record of any parsed top-level symbols.
50 struct SymbolState {
51   // A map from attribute alias identifier to Attribute.
52   llvm::StringMap<Attribute> attributeAliasDefinitions;
53 
54   // A map from type alias identifier to Type.
55   llvm::StringMap<Type> typeAliasDefinitions;
56 
57   /// A set of locations into the main parser memory buffer for each of the
58   /// active nested parsers. Given that some nested parsers, i.e. custom dialect
59   /// parsers, operate on a temporary memory buffer, this provides an anchor
60   /// point for emitting diagnostics.
61   SmallVector<llvm::SMLoc, 1> nestedParserLocs;
62 
63   /// The top-level lexer that contains the original memory buffer provided by
64   /// the user. This is used by nested parsers to get a properly encoded source
65   /// location.
66   Lexer *topLevelLexer = nullptr;
67 };
68 
69 //===----------------------------------------------------------------------===//
70 // ParserState
71 //===----------------------------------------------------------------------===//
72 
73 /// This class refers to all of the state maintained globally by the parser,
74 /// such as the current lexer position etc.
75 struct ParserState {
76   ParserState(const llvm::SourceMgr &sourceMgr, MLIRContext *ctx,
77               SymbolState &symbols)
78       : context(ctx), lex(sourceMgr, ctx), curToken(lex.lexToken()),
79         symbols(symbols), parserDepth(symbols.nestedParserLocs.size()) {
80     // Set the top level lexer for the symbol state if one doesn't exist.
81     if (!symbols.topLevelLexer)
82       symbols.topLevelLexer = &lex;
83   }
84   ~ParserState() {
85     // Reset the top level lexer if it refers the lexer in our state.
86     if (symbols.topLevelLexer == &lex)
87       symbols.topLevelLexer = nullptr;
88   }
89   ParserState(const ParserState &) = delete;
90   void operator=(const ParserState &) = delete;
91 
92   /// The context we're parsing into.
93   MLIRContext *const context;
94 
95   /// The lexer for the source file we're parsing.
96   Lexer lex;
97 
98   /// This is the next token that hasn't been consumed yet.
99   Token curToken;
100 
101   /// The current state for symbol parsing.
102   SymbolState &symbols;
103 
104   /// The depth of this parser in the nested parsing stack.
105   size_t parserDepth;
106 };
107 
108 //===----------------------------------------------------------------------===//
109 // Parser
110 //===----------------------------------------------------------------------===//
111 
112 /// This class implement support for parsing global entities like types and
113 /// shared entities like SSA names.  It is intended to be subclassed by
114 /// specialized subparsers that include state, e.g. when a local symbol table.
115 class Parser {
116 public:
117   Builder builder;
118 
119   Parser(ParserState &state) : builder(state.context), state(state) {}
120 
121   // Helper methods to get stuff from the parser-global state.
122   ParserState &getState() const { return state; }
123   MLIRContext *getContext() const { return state.context; }
124   const llvm::SourceMgr &getSourceMgr() { return state.lex.getSourceMgr(); }
125 
126   /// Parse a comma-separated list of elements up until the specified end token.
127   ParseResult
128   parseCommaSeparatedListUntil(Token::Kind rightToken,
129                                const std::function<ParseResult()> &parseElement,
130                                bool allowEmptyList = true);
131 
132   /// Parse a comma separated list of elements that must have at least one entry
133   /// in it.
134   ParseResult
135   parseCommaSeparatedList(const std::function<ParseResult()> &parseElement);
136 
137   ParseResult parsePrettyDialectSymbolName(StringRef &prettyName);
138 
139   // We have two forms of parsing methods - those that return a non-null
140   // pointer on success, and those that return a ParseResult to indicate whether
141   // they returned a failure.  The second class fills in by-reference arguments
142   // as the results of their action.
143 
144   //===--------------------------------------------------------------------===//
145   // Error Handling
146   //===--------------------------------------------------------------------===//
147 
148   /// Emit an error and return failure.
149   InFlightDiagnostic emitError(const Twine &message = {}) {
150     return emitError(state.curToken.getLoc(), message);
151   }
152   InFlightDiagnostic emitError(SMLoc loc, const Twine &message = {});
153 
154   /// Encode the specified source location information into an attribute for
155   /// attachment to the IR.
156   Location getEncodedSourceLocation(llvm::SMLoc loc) {
157     // If there are no active nested parsers, we can get the encoded source
158     // location directly.
159     if (state.parserDepth == 0)
160       return state.lex.getEncodedSourceLocation(loc);
161     // Otherwise, we need to re-encode it to point to the top level buffer.
162     return state.symbols.topLevelLexer->getEncodedSourceLocation(
163         remapLocationToTopLevelBuffer(loc));
164   }
165 
166   /// Remaps the given SMLoc to the top level lexer of the parser. This is used
167   /// to adjust locations of potentially nested parsers to ensure that they can
168   /// be emitted properly as diagnostics.
169   llvm::SMLoc remapLocationToTopLevelBuffer(llvm::SMLoc loc) {
170     // If there are no active nested parsers, we can return location directly.
171     SymbolState &symbols = state.symbols;
172     if (state.parserDepth == 0)
173       return loc;
174     assert(symbols.topLevelLexer && "expected valid top-level lexer");
175 
176     // Otherwise, we need to remap the location to the main parser. This is
177     // simply offseting the location onto the location of the last nested
178     // parser.
179     size_t offset = loc.getPointer() - state.lex.getBufferBegin();
180     auto *rawLoc =
181         symbols.nestedParserLocs[state.parserDepth - 1].getPointer() + offset;
182     return llvm::SMLoc::getFromPointer(rawLoc);
183   }
184 
185   //===--------------------------------------------------------------------===//
186   // Token Parsing
187   //===--------------------------------------------------------------------===//
188 
189   /// Return the current token the parser is inspecting.
190   const Token &getToken() const { return state.curToken; }
191   StringRef getTokenSpelling() const { return state.curToken.getSpelling(); }
192 
193   /// If the current token has the specified kind, consume it and return true.
194   /// If not, return false.
195   bool consumeIf(Token::Kind kind) {
196     if (state.curToken.isNot(kind))
197       return false;
198     consumeToken(kind);
199     return true;
200   }
201 
202   /// Advance the current lexer onto the next token.
203   void consumeToken() {
204     assert(state.curToken.isNot(Token::eof, Token::error) &&
205            "shouldn't advance past EOF or errors");
206     state.curToken = state.lex.lexToken();
207   }
208 
209   /// Advance the current lexer onto the next token, asserting what the expected
210   /// current token is.  This is preferred to the above method because it leads
211   /// to more self-documenting code with better checking.
212   void consumeToken(Token::Kind kind) {
213     assert(state.curToken.is(kind) && "consumed an unexpected token");
214     consumeToken();
215   }
216 
217   /// Consume the specified token if present and return success.  On failure,
218   /// output a diagnostic and return failure.
219   ParseResult parseToken(Token::Kind expectedToken, const Twine &message);
220 
221   //===--------------------------------------------------------------------===//
222   // Type Parsing
223   //===--------------------------------------------------------------------===//
224 
225   ParseResult parseFunctionResultTypes(SmallVectorImpl<Type> &elements);
226   ParseResult parseTypeListNoParens(SmallVectorImpl<Type> &elements);
227   ParseResult parseTypeListParens(SmallVectorImpl<Type> &elements);
228 
229   /// Parse an arbitrary type.
230   Type parseType();
231 
232   /// Parse a complex type.
233   Type parseComplexType();
234 
235   /// Parse an extended type.
236   Type parseExtendedType();
237 
238   /// Parse a function type.
239   Type parseFunctionType();
240 
241   /// Parse a memref type.
242   Type parseMemRefType();
243 
244   /// Parse a non function type.
245   Type parseNonFunctionType();
246 
247   /// Parse a tensor type.
248   Type parseTensorType();
249 
250   /// Parse a tuple type.
251   Type parseTupleType();
252 
253   /// Parse a vector type.
254   VectorType parseVectorType();
255   ParseResult parseDimensionListRanked(SmallVectorImpl<int64_t> &dimensions,
256                                        bool allowDynamic = true);
257   ParseResult parseXInDimensionList();
258 
259   /// Parse strided layout specification.
260   ParseResult parseStridedLayout(int64_t &offset,
261                                  SmallVectorImpl<int64_t> &strides);
262 
263   // Parse a brace-delimiter list of comma-separated integers with `?` as an
264   // unknown marker.
265   ParseResult parseStrideList(SmallVectorImpl<int64_t> &dimensions);
266 
267   //===--------------------------------------------------------------------===//
268   // Attribute Parsing
269   //===--------------------------------------------------------------------===//
270 
271   /// Parse an arbitrary attribute with an optional type.
272   Attribute parseAttribute(Type type = {});
273 
274   /// Parse an attribute dictionary.
275   ParseResult parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes);
276 
277   /// Parse an extended attribute.
278   Attribute parseExtendedAttr(Type type);
279 
280   /// Parse a float attribute.
281   Attribute parseFloatAttr(Type type, bool isNegative);
282 
283   /// Parse a decimal or a hexadecimal literal, which can be either an integer
284   /// or a float attribute.
285   Attribute parseDecOrHexAttr(Type type, bool isNegative);
286 
287   /// Parse an opaque elements attribute.
288   Attribute parseOpaqueElementsAttr(Type attrType);
289 
290   /// Parse a dense elements attribute.
291   Attribute parseDenseElementsAttr(Type attrType);
292   ShapedType parseElementsLiteralType(Type type);
293 
294   /// Parse a sparse elements attribute.
295   Attribute parseSparseElementsAttr(Type attrType);
296 
297   //===--------------------------------------------------------------------===//
298   // Location Parsing
299   //===--------------------------------------------------------------------===//
300 
301   /// Parse an inline location.
302   ParseResult parseLocation(LocationAttr &loc);
303 
304   /// Parse a raw location instance.
305   ParseResult parseLocationInstance(LocationAttr &loc);
306 
307   /// Parse a callsite location instance.
308   ParseResult parseCallSiteLocation(LocationAttr &loc);
309 
310   /// Parse a fused location instance.
311   ParseResult parseFusedLocation(LocationAttr &loc);
312 
313   /// Parse a name or FileLineCol location instance.
314   ParseResult parseNameOrFileLineColLocation(LocationAttr &loc);
315 
316   /// Parse an optional trailing location.
317   ///
318   ///   trailing-location     ::= (`loc` `(` location `)`)?
319   ///
320   ParseResult parseOptionalTrailingLocation(Location &loc) {
321     // If there is a 'loc' we parse a trailing location.
322     if (!getToken().is(Token::kw_loc))
323       return success();
324 
325     // Parse the location.
326     LocationAttr directLoc;
327     if (parseLocation(directLoc))
328       return failure();
329     loc = directLoc;
330     return success();
331   }
332 
333   //===--------------------------------------------------------------------===//
334   // Affine Parsing
335   //===--------------------------------------------------------------------===//
336 
337   /// Parse a reference to either an affine map, or an integer set.
338   ParseResult parseAffineMapOrIntegerSetReference(AffineMap &map,
339                                                   IntegerSet &set);
340   ParseResult parseAffineMapReference(AffineMap &map);
341   ParseResult parseIntegerSetReference(IntegerSet &set);
342 
343   /// Parse an AffineMap where the dim and symbol identifiers are SSA ids.
344   ParseResult
345   parseAffineMapOfSSAIds(AffineMap &map,
346                          function_ref<ParseResult(bool)> parseElement,
347                          OpAsmParser::Delimiter delimiter);
348 
349 private:
350   /// The Parser is subclassed and reinstantiated.  Do not add additional
351   /// non-trivial state here, add it to the ParserState class.
352   ParserState &state;
353 };
354 } // end anonymous namespace
355 
356 //===----------------------------------------------------------------------===//
357 // Helper methods.
358 //===----------------------------------------------------------------------===//
359 
360 /// Parse a comma separated list of elements that must have at least one entry
361 /// in it.
362 ParseResult Parser::parseCommaSeparatedList(
363     const std::function<ParseResult()> &parseElement) {
364   // Non-empty case starts with an element.
365   if (parseElement())
366     return failure();
367 
368   // Otherwise we have a list of comma separated elements.
369   while (consumeIf(Token::comma)) {
370     if (parseElement())
371       return failure();
372   }
373   return success();
374 }
375 
376 /// Parse a comma-separated list of elements, terminated with an arbitrary
377 /// token.  This allows empty lists if allowEmptyList is true.
378 ///
379 ///   abstract-list ::= rightToken                  // if allowEmptyList == true
380 ///   abstract-list ::= element (',' element)* rightToken
381 ///
382 ParseResult Parser::parseCommaSeparatedListUntil(
383     Token::Kind rightToken, const std::function<ParseResult()> &parseElement,
384     bool allowEmptyList) {
385   // Handle the empty case.
386   if (getToken().is(rightToken)) {
387     if (!allowEmptyList)
388       return emitError("expected list element");
389     consumeToken(rightToken);
390     return success();
391   }
392 
393   if (parseCommaSeparatedList(parseElement) ||
394       parseToken(rightToken, "expected ',' or '" +
395                                  Token::getTokenSpelling(rightToken) + "'"))
396     return failure();
397 
398   return success();
399 }
400 
401 //===----------------------------------------------------------------------===//
402 // DialectAsmParser
403 //===----------------------------------------------------------------------===//
404 
405 namespace {
406 /// This class provides the main implementation of the DialectAsmParser that
407 /// allows for dialects to parse attributes and types. This allows for dialect
408 /// hooking into the main MLIR parsing logic.
409 class CustomDialectAsmParser : public DialectAsmParser {
410 public:
411   CustomDialectAsmParser(StringRef fullSpec, Parser &parser)
412       : fullSpec(fullSpec), nameLoc(parser.getToken().getLoc()),
413         parser(parser) {}
414   ~CustomDialectAsmParser() override {}
415 
416   /// Emit a diagnostic at the specified location and return failure.
417   InFlightDiagnostic emitError(llvm::SMLoc loc, const Twine &message) override {
418     return parser.emitError(loc, message);
419   }
420 
421   /// Return a builder which provides useful access to MLIRContext, global
422   /// objects like types and attributes.
423   Builder &getBuilder() const override { return parser.builder; }
424 
425   /// Get the location of the next token and store it into the argument.  This
426   /// always succeeds.
427   llvm::SMLoc getCurrentLocation() override {
428     return parser.getToken().getLoc();
429   }
430 
431   /// Return the location of the original name token.
432   llvm::SMLoc getNameLoc() const override { return nameLoc; }
433 
434   /// Re-encode the given source location as an MLIR location and return it.
435   Location getEncodedSourceLoc(llvm::SMLoc loc) override {
436     return parser.getEncodedSourceLocation(loc);
437   }
438 
439   /// Returns the full specification of the symbol being parsed. This allows
440   /// for using a separate parser if necessary.
441   StringRef getFullSymbolSpec() const override { return fullSpec; }
442 
443   /// Parse a floating point value from the stream.
444   ParseResult parseFloat(double &result) override {
445     bool negative = parser.consumeIf(Token::minus);
446     Token curTok = parser.getToken();
447 
448     // Check for a floating point value.
449     if (curTok.is(Token::floatliteral)) {
450       auto val = curTok.getFloatingPointValue();
451       if (!val.hasValue())
452         return emitError(curTok.getLoc(), "floating point value too large");
453       parser.consumeToken(Token::floatliteral);
454       result = negative ? -*val : *val;
455       return success();
456     }
457 
458     // TODO(riverriddle) support hex floating point values.
459     return emitError(getCurrentLocation(), "expected floating point literal");
460   }
461 
462   /// Parse an optional integer value from the stream.
463   OptionalParseResult parseOptionalInteger(uint64_t &result) override {
464     Token curToken = parser.getToken();
465     if (curToken.isNot(Token::integer, Token::minus))
466       return llvm::None;
467 
468     bool negative = parser.consumeIf(Token::minus);
469     Token curTok = parser.getToken();
470     if (parser.parseToken(Token::integer, "expected integer value"))
471       return failure();
472 
473     auto val = curTok.getUInt64IntegerValue();
474     if (!val)
475       return emitError(curTok.getLoc(), "integer value too large");
476     result = negative ? -*val : *val;
477     return success();
478   }
479 
480   //===--------------------------------------------------------------------===//
481   // Token Parsing
482   //===--------------------------------------------------------------------===//
483 
484   /// Parse a `->` token.
485   ParseResult parseArrow() override {
486     return parser.parseToken(Token::arrow, "expected '->'");
487   }
488 
489   /// Parses a `->` if present.
490   ParseResult parseOptionalArrow() override {
491     return success(parser.consumeIf(Token::arrow));
492   }
493 
494   /// Parse a '{' token.
495   ParseResult parseLBrace() override {
496     return parser.parseToken(Token::l_brace, "expected '{'");
497   }
498 
499   /// Parse a '{' token if present
500   ParseResult parseOptionalLBrace() override {
501     return success(parser.consumeIf(Token::l_brace));
502   }
503 
504   /// Parse a `}` token.
505   ParseResult parseRBrace() override {
506     return parser.parseToken(Token::r_brace, "expected '}'");
507   }
508 
509   /// Parse a `}` token if present
510   ParseResult parseOptionalRBrace() override {
511     return success(parser.consumeIf(Token::r_brace));
512   }
513 
514   /// Parse a `:` token.
515   ParseResult parseColon() override {
516     return parser.parseToken(Token::colon, "expected ':'");
517   }
518 
519   /// Parse a `:` token if present.
520   ParseResult parseOptionalColon() override {
521     return success(parser.consumeIf(Token::colon));
522   }
523 
524   /// Parse a `,` token.
525   ParseResult parseComma() override {
526     return parser.parseToken(Token::comma, "expected ','");
527   }
528 
529   /// Parse a `,` token if present.
530   ParseResult parseOptionalComma() override {
531     return success(parser.consumeIf(Token::comma));
532   }
533 
534   /// Parses a `...` if present.
535   ParseResult parseOptionalEllipsis() override {
536     return success(parser.consumeIf(Token::ellipsis));
537   }
538 
539   /// Parse a `=` token.
540   ParseResult parseEqual() override {
541     return parser.parseToken(Token::equal, "expected '='");
542   }
543 
544   /// Parse a '<' token.
545   ParseResult parseLess() override {
546     return parser.parseToken(Token::less, "expected '<'");
547   }
548 
549   /// Parse a `<` token if present.
550   ParseResult parseOptionalLess() override {
551     return success(parser.consumeIf(Token::less));
552   }
553 
554   /// Parse a '>' token.
555   ParseResult parseGreater() override {
556     return parser.parseToken(Token::greater, "expected '>'");
557   }
558 
559   /// Parse a `>` token if present.
560   ParseResult parseOptionalGreater() override {
561     return success(parser.consumeIf(Token::greater));
562   }
563 
564   /// Parse a `(` token.
565   ParseResult parseLParen() override {
566     return parser.parseToken(Token::l_paren, "expected '('");
567   }
568 
569   /// Parses a '(' if present.
570   ParseResult parseOptionalLParen() override {
571     return success(parser.consumeIf(Token::l_paren));
572   }
573 
574   /// Parse a `)` token.
575   ParseResult parseRParen() override {
576     return parser.parseToken(Token::r_paren, "expected ')'");
577   }
578 
579   /// Parses a ')' if present.
580   ParseResult parseOptionalRParen() override {
581     return success(parser.consumeIf(Token::r_paren));
582   }
583 
584   /// Parse a `[` token.
585   ParseResult parseLSquare() override {
586     return parser.parseToken(Token::l_square, "expected '['");
587   }
588 
589   /// Parses a '[' if present.
590   ParseResult parseOptionalLSquare() override {
591     return success(parser.consumeIf(Token::l_square));
592   }
593 
594   /// Parse a `]` token.
595   ParseResult parseRSquare() override {
596     return parser.parseToken(Token::r_square, "expected ']'");
597   }
598 
599   /// Parses a ']' if present.
600   ParseResult parseOptionalRSquare() override {
601     return success(parser.consumeIf(Token::r_square));
602   }
603 
604   /// Parses a '?' if present.
605   ParseResult parseOptionalQuestion() override {
606     return success(parser.consumeIf(Token::question));
607   }
608 
609   /// Parses a '*' if present.
610   ParseResult parseOptionalStar() override {
611     return success(parser.consumeIf(Token::star));
612   }
613 
614   /// Returns if the current token corresponds to a keyword.
615   bool isCurrentTokenAKeyword() const {
616     return parser.getToken().is(Token::bare_identifier) ||
617            parser.getToken().isKeyword();
618   }
619 
620   /// Parse the given keyword if present.
621   ParseResult parseOptionalKeyword(StringRef keyword) override {
622     // Check that the current token has the same spelling.
623     if (!isCurrentTokenAKeyword() || parser.getTokenSpelling() != keyword)
624       return failure();
625     parser.consumeToken();
626     return success();
627   }
628 
629   /// Parse a keyword, if present, into 'keyword'.
630   ParseResult parseOptionalKeyword(StringRef *keyword) override {
631     // Check that the current token is a keyword.
632     if (!isCurrentTokenAKeyword())
633       return failure();
634 
635     *keyword = parser.getTokenSpelling();
636     parser.consumeToken();
637     return success();
638   }
639 
640   //===--------------------------------------------------------------------===//
641   // Attribute Parsing
642   //===--------------------------------------------------------------------===//
643 
644   /// Parse an arbitrary attribute and return it in result.
645   ParseResult parseAttribute(Attribute &result, Type type) override {
646     result = parser.parseAttribute(type);
647     return success(static_cast<bool>(result));
648   }
649 
650   /// Parse an affine map instance into 'map'.
651   ParseResult parseAffineMap(AffineMap &map) override {
652     return parser.parseAffineMapReference(map);
653   }
654 
655   /// Parse an integer set instance into 'set'.
656   ParseResult printIntegerSet(IntegerSet &set) override {
657     return parser.parseIntegerSetReference(set);
658   }
659 
660   //===--------------------------------------------------------------------===//
661   // Type Parsing
662   //===--------------------------------------------------------------------===//
663 
664   ParseResult parseType(Type &result) override {
665     result = parser.parseType();
666     return success(static_cast<bool>(result));
667   }
668 
669   ParseResult parseDimensionList(SmallVectorImpl<int64_t> &dimensions,
670                                  bool allowDynamic) override {
671     return parser.parseDimensionListRanked(dimensions, allowDynamic);
672   }
673 
674 private:
675   /// The full symbol specification.
676   StringRef fullSpec;
677 
678   /// The source location of the dialect symbol.
679   SMLoc nameLoc;
680 
681   /// The main parser.
682   Parser &parser;
683 };
684 } // namespace
685 
686 /// Parse the body of a pretty dialect symbol, which starts and ends with <>'s,
687 /// and may be recursive.  Return with the 'prettyName' StringRef encompassing
688 /// the entire pretty name.
689 ///
690 ///   pretty-dialect-sym-body ::= '<' pretty-dialect-sym-contents+ '>'
691 ///   pretty-dialect-sym-contents ::= pretty-dialect-sym-body
692 ///                                  | '(' pretty-dialect-sym-contents+ ')'
693 ///                                  | '[' pretty-dialect-sym-contents+ ']'
694 ///                                  | '{' pretty-dialect-sym-contents+ '}'
695 ///                                  | '[^[<({>\])}\0]+'
696 ///
697 ParseResult Parser::parsePrettyDialectSymbolName(StringRef &prettyName) {
698   // Pretty symbol names are a relatively unstructured format that contains a
699   // series of properly nested punctuation, with anything else in the middle.
700   // Scan ahead to find it and consume it if successful, otherwise emit an
701   // error.
702   auto *curPtr = getTokenSpelling().data();
703 
704   SmallVector<char, 8> nestedPunctuation;
705 
706   // Scan over the nested punctuation, bailing out on error and consuming until
707   // we find the end.  We know that we're currently looking at the '<', so we
708   // can go until we find the matching '>' character.
709   assert(*curPtr == '<');
710   do {
711     char c = *curPtr++;
712     switch (c) {
713     case '\0':
714       // This also handles the EOF case.
715       return emitError("unexpected nul or EOF in pretty dialect name");
716     case '<':
717     case '[':
718     case '(':
719     case '{':
720       nestedPunctuation.push_back(c);
721       continue;
722 
723     case '-':
724       // The sequence `->` is treated as special token.
725       if (*curPtr == '>')
726         ++curPtr;
727       continue;
728 
729     case '>':
730       if (nestedPunctuation.pop_back_val() != '<')
731         return emitError("unbalanced '>' character in pretty dialect name");
732       break;
733     case ']':
734       if (nestedPunctuation.pop_back_val() != '[')
735         return emitError("unbalanced ']' character in pretty dialect name");
736       break;
737     case ')':
738       if (nestedPunctuation.pop_back_val() != '(')
739         return emitError("unbalanced ')' character in pretty dialect name");
740       break;
741     case '}':
742       if (nestedPunctuation.pop_back_val() != '{')
743         return emitError("unbalanced '}' character in pretty dialect name");
744       break;
745 
746     default:
747       continue;
748     }
749   } while (!nestedPunctuation.empty());
750 
751   // Ok, we succeeded, remember where we stopped, reset the lexer to know it is
752   // consuming all this stuff, and return.
753   state.lex.resetPointer(curPtr);
754 
755   unsigned length = curPtr - prettyName.begin();
756   prettyName = StringRef(prettyName.begin(), length);
757   consumeToken();
758   return success();
759 }
760 
761 /// Parse an extended dialect symbol.
762 template <typename Symbol, typename SymbolAliasMap, typename CreateFn>
763 static Symbol parseExtendedSymbol(Parser &p, Token::Kind identifierTok,
764                                   SymbolAliasMap &aliases,
765                                   CreateFn &&createSymbol) {
766   // Parse the dialect namespace.
767   StringRef identifier = p.getTokenSpelling().drop_front();
768   auto loc = p.getToken().getLoc();
769   p.consumeToken(identifierTok);
770 
771   // If there is no '<' token following this, and if the typename contains no
772   // dot, then we are parsing a symbol alias.
773   if (p.getToken().isNot(Token::less) && !identifier.contains('.')) {
774     // Check for an alias for this type.
775     auto aliasIt = aliases.find(identifier);
776     if (aliasIt == aliases.end())
777       return (p.emitError("undefined symbol alias id '" + identifier + "'"),
778               nullptr);
779     return aliasIt->second;
780   }
781 
782   // Otherwise, we are parsing a dialect-specific symbol.  If the name contains
783   // a dot, then this is the "pretty" form.  If not, it is the verbose form that
784   // looks like <"...">.
785   std::string symbolData;
786   auto dialectName = identifier;
787 
788   // Handle the verbose form, where "identifier" is a simple dialect name.
789   if (!identifier.contains('.')) {
790     // Consume the '<'.
791     if (p.parseToken(Token::less, "expected '<' in dialect type"))
792       return nullptr;
793 
794     // Parse the symbol specific data.
795     if (p.getToken().isNot(Token::string))
796       return (p.emitError("expected string literal data in dialect symbol"),
797               nullptr);
798     symbolData = p.getToken().getStringValue();
799     loc = llvm::SMLoc::getFromPointer(p.getToken().getLoc().getPointer() + 1);
800     p.consumeToken(Token::string);
801 
802     // Consume the '>'.
803     if (p.parseToken(Token::greater, "expected '>' in dialect symbol"))
804       return nullptr;
805   } else {
806     // Ok, the dialect name is the part of the identifier before the dot, the
807     // part after the dot is the dialect's symbol, or the start thereof.
808     auto dotHalves = identifier.split('.');
809     dialectName = dotHalves.first;
810     auto prettyName = dotHalves.second;
811     loc = llvm::SMLoc::getFromPointer(prettyName.data());
812 
813     // If the dialect's symbol is followed immediately by a <, then lex the body
814     // of it into prettyName.
815     if (p.getToken().is(Token::less) &&
816         prettyName.bytes_end() == p.getTokenSpelling().bytes_begin()) {
817       if (p.parsePrettyDialectSymbolName(prettyName))
818         return nullptr;
819     }
820 
821     symbolData = prettyName.str();
822   }
823 
824   // Record the name location of the type remapped to the top level buffer.
825   llvm::SMLoc locInTopLevelBuffer = p.remapLocationToTopLevelBuffer(loc);
826   p.getState().symbols.nestedParserLocs.push_back(locInTopLevelBuffer);
827 
828   // Call into the provided symbol construction function.
829   Symbol sym = createSymbol(dialectName, symbolData, loc);
830 
831   // Pop the last parser location.
832   p.getState().symbols.nestedParserLocs.pop_back();
833   return sym;
834 }
835 
836 /// Parses a symbol, of type 'T', and returns it if parsing was successful. If
837 /// parsing failed, nullptr is returned. The number of bytes read from the input
838 /// string is returned in 'numRead'.
839 template <typename T, typename ParserFn>
840 static T parseSymbol(StringRef inputStr, MLIRContext *context,
841                      SymbolState &symbolState, ParserFn &&parserFn,
842                      size_t *numRead = nullptr) {
843   SourceMgr sourceMgr;
844   auto memBuffer = MemoryBuffer::getMemBuffer(
845       inputStr, /*BufferName=*/"<mlir_parser_buffer>",
846       /*RequiresNullTerminator=*/false);
847   sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
848   ParserState state(sourceMgr, context, symbolState);
849   Parser parser(state);
850 
851   Token startTok = parser.getToken();
852   T symbol = parserFn(parser);
853   if (!symbol)
854     return T();
855 
856   // If 'numRead' is valid, then provide the number of bytes that were read.
857   Token endTok = parser.getToken();
858   if (numRead) {
859     *numRead = static_cast<size_t>(endTok.getLoc().getPointer() -
860                                    startTok.getLoc().getPointer());
861 
862     // Otherwise, ensure that all of the tokens were parsed.
863   } else if (startTok.getLoc() != endTok.getLoc() && endTok.isNot(Token::eof)) {
864     parser.emitError(endTok.getLoc(), "encountered unexpected token");
865     return T();
866   }
867   return symbol;
868 }
869 
870 //===----------------------------------------------------------------------===//
871 // Error Handling
872 //===----------------------------------------------------------------------===//
873 
874 InFlightDiagnostic Parser::emitError(SMLoc loc, const Twine &message) {
875   auto diag = mlir::emitError(getEncodedSourceLocation(loc), message);
876 
877   // If we hit a parse error in response to a lexer error, then the lexer
878   // already reported the error.
879   if (getToken().is(Token::error))
880     diag.abandon();
881   return diag;
882 }
883 
884 //===----------------------------------------------------------------------===//
885 // Token Parsing
886 //===----------------------------------------------------------------------===//
887 
888 /// Consume the specified token if present and return success.  On failure,
889 /// output a diagnostic and return failure.
890 ParseResult Parser::parseToken(Token::Kind expectedToken,
891                                const Twine &message) {
892   if (consumeIf(expectedToken))
893     return success();
894   return emitError(message);
895 }
896 
897 //===----------------------------------------------------------------------===//
898 // Type Parsing
899 //===----------------------------------------------------------------------===//
900 
901 /// Parse an arbitrary type.
902 ///
903 ///   type ::= function-type
904 ///          | non-function-type
905 ///
906 Type Parser::parseType() {
907   if (getToken().is(Token::l_paren))
908     return parseFunctionType();
909   return parseNonFunctionType();
910 }
911 
912 /// Parse a function result type.
913 ///
914 ///   function-result-type ::= type-list-parens
915 ///                          | non-function-type
916 ///
917 ParseResult Parser::parseFunctionResultTypes(SmallVectorImpl<Type> &elements) {
918   if (getToken().is(Token::l_paren))
919     return parseTypeListParens(elements);
920 
921   Type t = parseNonFunctionType();
922   if (!t)
923     return failure();
924   elements.push_back(t);
925   return success();
926 }
927 
928 /// Parse a list of types without an enclosing parenthesis.  The list must have
929 /// at least one member.
930 ///
931 ///   type-list-no-parens ::=  type (`,` type)*
932 ///
933 ParseResult Parser::parseTypeListNoParens(SmallVectorImpl<Type> &elements) {
934   auto parseElt = [&]() -> ParseResult {
935     auto elt = parseType();
936     elements.push_back(elt);
937     return elt ? success() : failure();
938   };
939 
940   return parseCommaSeparatedList(parseElt);
941 }
942 
943 /// Parse a parenthesized list of types.
944 ///
945 ///   type-list-parens ::= `(` `)`
946 ///                      | `(` type-list-no-parens `)`
947 ///
948 ParseResult Parser::parseTypeListParens(SmallVectorImpl<Type> &elements) {
949   if (parseToken(Token::l_paren, "expected '('"))
950     return failure();
951 
952   // Handle empty lists.
953   if (getToken().is(Token::r_paren))
954     return consumeToken(), success();
955 
956   if (parseTypeListNoParens(elements) ||
957       parseToken(Token::r_paren, "expected ')'"))
958     return failure();
959   return success();
960 }
961 
962 /// Parse a complex type.
963 ///
964 ///   complex-type ::= `complex` `<` type `>`
965 ///
966 Type Parser::parseComplexType() {
967   consumeToken(Token::kw_complex);
968 
969   // Parse the '<'.
970   if (parseToken(Token::less, "expected '<' in complex type"))
971     return nullptr;
972 
973   llvm::SMLoc elementTypeLoc = getToken().getLoc();
974   auto elementType = parseType();
975   if (!elementType ||
976       parseToken(Token::greater, "expected '>' in complex type"))
977     return nullptr;
978   if (!elementType.isa<FloatType>() && !elementType.isa<IntegerType>())
979     return emitError(elementTypeLoc, "invalid element type for complex"),
980            nullptr;
981 
982   return ComplexType::get(elementType);
983 }
984 
985 /// Parse an extended type.
986 ///
987 ///   extended-type ::= (dialect-type | type-alias)
988 ///   dialect-type  ::= `!` dialect-namespace `<` `"` type-data `"` `>`
989 ///   dialect-type  ::= `!` alias-name pretty-dialect-attribute-body?
990 ///   type-alias    ::= `!` alias-name
991 ///
992 Type Parser::parseExtendedType() {
993   return parseExtendedSymbol<Type>(
994       *this, Token::exclamation_identifier, state.symbols.typeAliasDefinitions,
995       [&](StringRef dialectName, StringRef symbolData,
996           llvm::SMLoc loc) -> Type {
997         // If we found a registered dialect, then ask it to parse the type.
998         if (auto *dialect = state.context->getRegisteredDialect(dialectName)) {
999           return parseSymbol<Type>(
1000               symbolData, state.context, state.symbols, [&](Parser &parser) {
1001                 CustomDialectAsmParser customParser(symbolData, parser);
1002                 return dialect->parseType(customParser);
1003               });
1004         }
1005 
1006         // Otherwise, form a new opaque type.
1007         return OpaqueType::getChecked(
1008             Identifier::get(dialectName, state.context), symbolData,
1009             state.context, getEncodedSourceLocation(loc));
1010       });
1011 }
1012 
1013 /// Parse a function type.
1014 ///
1015 ///   function-type ::= type-list-parens `->` function-result-type
1016 ///
1017 Type Parser::parseFunctionType() {
1018   assert(getToken().is(Token::l_paren));
1019 
1020   SmallVector<Type, 4> arguments, results;
1021   if (parseTypeListParens(arguments) ||
1022       parseToken(Token::arrow, "expected '->' in function type") ||
1023       parseFunctionResultTypes(results))
1024     return nullptr;
1025 
1026   return builder.getFunctionType(arguments, results);
1027 }
1028 
1029 /// Parse the offset and strides from a strided layout specification.
1030 ///
1031 ///   strided-layout ::= `offset:` dimension `,` `strides: ` stride-list
1032 ///
1033 ParseResult Parser::parseStridedLayout(int64_t &offset,
1034                                        SmallVectorImpl<int64_t> &strides) {
1035   // Parse offset.
1036   consumeToken(Token::kw_offset);
1037   if (!consumeIf(Token::colon))
1038     return emitError("expected colon after `offset` keyword");
1039   auto maybeOffset = getToken().getUnsignedIntegerValue();
1040   bool question = getToken().is(Token::question);
1041   if (!maybeOffset && !question)
1042     return emitError("invalid offset");
1043   offset = maybeOffset ? static_cast<int64_t>(maybeOffset.getValue())
1044                        : MemRefType::getDynamicStrideOrOffset();
1045   consumeToken();
1046 
1047   if (!consumeIf(Token::comma))
1048     return emitError("expected comma after offset value");
1049 
1050   // Parse stride list.
1051   if (!consumeIf(Token::kw_strides))
1052     return emitError("expected `strides` keyword after offset specification");
1053   if (!consumeIf(Token::colon))
1054     return emitError("expected colon after `strides` keyword");
1055   if (failed(parseStrideList(strides)))
1056     return emitError("invalid braces-enclosed stride list");
1057   if (llvm::any_of(strides, [](int64_t st) { return st == 0; }))
1058     return emitError("invalid memref stride");
1059 
1060   return success();
1061 }
1062 
1063 /// Parse a memref type.
1064 ///
1065 ///   memref-type ::= ranked-memref-type | unranked-memref-type
1066 ///
1067 ///   ranked-memref-type ::= `memref` `<` dimension-list-ranked type
1068 ///                          (`,` semi-affine-map-composition)? (`,`
1069 ///                          memory-space)? `>`
1070 ///
1071 ///   unranked-memref-type ::= `memref` `<*x` type (`,` memory-space)? `>`
1072 ///
1073 ///   semi-affine-map-composition ::= (semi-affine-map `,` )* semi-affine-map
1074 ///   memory-space ::= integer-literal /* | TODO: address-space-id */
1075 ///
1076 Type Parser::parseMemRefType() {
1077   consumeToken(Token::kw_memref);
1078 
1079   if (parseToken(Token::less, "expected '<' in memref type"))
1080     return nullptr;
1081 
1082   bool isUnranked;
1083   SmallVector<int64_t, 4> dimensions;
1084 
1085   if (consumeIf(Token::star)) {
1086     // This is an unranked memref type.
1087     isUnranked = true;
1088     if (parseXInDimensionList())
1089       return nullptr;
1090 
1091   } else {
1092     isUnranked = false;
1093     if (parseDimensionListRanked(dimensions))
1094       return nullptr;
1095   }
1096 
1097   // Parse the element type.
1098   auto typeLoc = getToken().getLoc();
1099   auto elementType = parseType();
1100   if (!elementType)
1101     return nullptr;
1102 
1103   // Check that memref is formed from allowed types.
1104   if (!elementType.isIntOrFloat() && !elementType.isa<VectorType>() &&
1105       !elementType.isa<ComplexType>())
1106     return emitError(typeLoc, "invalid memref element type"), nullptr;
1107 
1108   // Parse semi-affine-map-composition.
1109   SmallVector<AffineMap, 2> affineMapComposition;
1110   Optional<unsigned> memorySpace;
1111   unsigned numDims = dimensions.size();
1112 
1113   auto parseElt = [&]() -> ParseResult {
1114     // Check for the memory space.
1115     if (getToken().is(Token::integer)) {
1116       if (memorySpace)
1117         return emitError("multiple memory spaces specified in memref type");
1118       memorySpace = getToken().getUnsignedIntegerValue();
1119       if (!memorySpace.hasValue())
1120         return emitError("invalid memory space in memref type");
1121       consumeToken(Token::integer);
1122       return success();
1123     }
1124     if (isUnranked)
1125       return emitError("cannot have affine map for unranked memref type");
1126     if (memorySpace)
1127       return emitError("expected memory space to be last in memref type");
1128 
1129     AffineMap map;
1130     llvm::SMLoc mapLoc = getToken().getLoc();
1131     if (getToken().is(Token::kw_offset)) {
1132       int64_t offset;
1133       SmallVector<int64_t, 4> strides;
1134       if (failed(parseStridedLayout(offset, strides)))
1135         return failure();
1136       // Construct strided affine map.
1137       map = makeStridedLinearLayoutMap(strides, offset, state.context);
1138     } else {
1139       // Parse an affine map attribute.
1140       auto affineMap = parseAttribute();
1141       if (!affineMap)
1142         return failure();
1143       auto affineMapAttr = affineMap.dyn_cast<AffineMapAttr>();
1144       if (!affineMapAttr)
1145         return emitError("expected affine map in memref type");
1146       map = affineMapAttr.getValue();
1147     }
1148 
1149     if (map.getNumDims() != numDims) {
1150       size_t i = affineMapComposition.size();
1151       return emitError(mapLoc, "memref affine map dimension mismatch between ")
1152              << (i == 0 ? Twine("memref rank") : "affine map " + Twine(i))
1153              << " and affine map" << i + 1 << ": " << numDims
1154              << " != " << map.getNumDims();
1155     }
1156     numDims = map.getNumResults();
1157     affineMapComposition.push_back(map);
1158     return success();
1159   };
1160 
1161   // Parse a list of mappings and address space if present.
1162   if (!consumeIf(Token::greater)) {
1163     // Parse comma separated list of affine maps, followed by memory space.
1164     if (parseToken(Token::comma, "expected ',' or '>' in memref type") ||
1165         parseCommaSeparatedListUntil(Token::greater, parseElt,
1166                                      /*allowEmptyList=*/false)) {
1167       return nullptr;
1168     }
1169   }
1170 
1171   if (isUnranked)
1172     return UnrankedMemRefType::get(elementType, memorySpace.getValueOr(0));
1173 
1174   return MemRefType::get(dimensions, elementType, affineMapComposition,
1175                          memorySpace.getValueOr(0));
1176 }
1177 
1178 /// Parse any type except the function type.
1179 ///
1180 ///   non-function-type ::= integer-type
1181 ///                       | index-type
1182 ///                       | float-type
1183 ///                       | extended-type
1184 ///                       | vector-type
1185 ///                       | tensor-type
1186 ///                       | memref-type
1187 ///                       | complex-type
1188 ///                       | tuple-type
1189 ///                       | none-type
1190 ///
1191 ///   index-type ::= `index`
1192 ///   float-type ::= `f16` | `bf16` | `f32` | `f64`
1193 ///   none-type ::= `none`
1194 ///
1195 Type Parser::parseNonFunctionType() {
1196   switch (getToken().getKind()) {
1197   default:
1198     return (emitError("expected non-function type"), nullptr);
1199   case Token::kw_memref:
1200     return parseMemRefType();
1201   case Token::kw_tensor:
1202     return parseTensorType();
1203   case Token::kw_complex:
1204     return parseComplexType();
1205   case Token::kw_tuple:
1206     return parseTupleType();
1207   case Token::kw_vector:
1208     return parseVectorType();
1209   // integer-type
1210   case Token::inttype: {
1211     auto width = getToken().getIntTypeBitwidth();
1212     if (!width.hasValue())
1213       return (emitError("invalid integer width"), nullptr);
1214     if (width.getValue() > IntegerType::kMaxWidth) {
1215       emitError(getToken().getLoc(), "integer bitwidth is limited to ")
1216           << IntegerType::kMaxWidth << " bits";
1217       return nullptr;
1218     }
1219 
1220     consumeToken(Token::inttype);
1221     return IntegerType::get(width.getValue(), builder.getContext());
1222   }
1223 
1224   // float-type
1225   case Token::kw_bf16:
1226     consumeToken(Token::kw_bf16);
1227     return builder.getBF16Type();
1228   case Token::kw_f16:
1229     consumeToken(Token::kw_f16);
1230     return builder.getF16Type();
1231   case Token::kw_f32:
1232     consumeToken(Token::kw_f32);
1233     return builder.getF32Type();
1234   case Token::kw_f64:
1235     consumeToken(Token::kw_f64);
1236     return builder.getF64Type();
1237 
1238   // index-type
1239   case Token::kw_index:
1240     consumeToken(Token::kw_index);
1241     return builder.getIndexType();
1242 
1243   // none-type
1244   case Token::kw_none:
1245     consumeToken(Token::kw_none);
1246     return builder.getNoneType();
1247 
1248   // extended type
1249   case Token::exclamation_identifier:
1250     return parseExtendedType();
1251   }
1252 }
1253 
1254 /// Parse a tensor type.
1255 ///
1256 ///   tensor-type ::= `tensor` `<` dimension-list type `>`
1257 ///   dimension-list ::= dimension-list-ranked | `*x`
1258 ///
1259 Type Parser::parseTensorType() {
1260   consumeToken(Token::kw_tensor);
1261 
1262   if (parseToken(Token::less, "expected '<' in tensor type"))
1263     return nullptr;
1264 
1265   bool isUnranked;
1266   SmallVector<int64_t, 4> dimensions;
1267 
1268   if (consumeIf(Token::star)) {
1269     // This is an unranked tensor type.
1270     isUnranked = true;
1271 
1272     if (parseXInDimensionList())
1273       return nullptr;
1274 
1275   } else {
1276     isUnranked = false;
1277     if (parseDimensionListRanked(dimensions))
1278       return nullptr;
1279   }
1280 
1281   // Parse the element type.
1282   auto elementTypeLoc = getToken().getLoc();
1283   auto elementType = parseType();
1284   if (!elementType || parseToken(Token::greater, "expected '>' in tensor type"))
1285     return nullptr;
1286   if (!TensorType::isValidElementType(elementType))
1287     return emitError(elementTypeLoc, "invalid tensor element type"), nullptr;
1288 
1289   if (isUnranked)
1290     return UnrankedTensorType::get(elementType);
1291   return RankedTensorType::get(dimensions, elementType);
1292 }
1293 
1294 /// Parse a tuple type.
1295 ///
1296 ///   tuple-type ::= `tuple` `<` (type (`,` type)*)? `>`
1297 ///
1298 Type Parser::parseTupleType() {
1299   consumeToken(Token::kw_tuple);
1300 
1301   // Parse the '<'.
1302   if (parseToken(Token::less, "expected '<' in tuple type"))
1303     return nullptr;
1304 
1305   // Check for an empty tuple by directly parsing '>'.
1306   if (consumeIf(Token::greater))
1307     return TupleType::get(getContext());
1308 
1309   // Parse the element types and the '>'.
1310   SmallVector<Type, 4> types;
1311   if (parseTypeListNoParens(types) ||
1312       parseToken(Token::greater, "expected '>' in tuple type"))
1313     return nullptr;
1314 
1315   return TupleType::get(types, getContext());
1316 }
1317 
1318 /// Parse a vector type.
1319 ///
1320 ///   vector-type ::= `vector` `<` non-empty-static-dimension-list type `>`
1321 ///   non-empty-static-dimension-list ::= decimal-literal `x`
1322 ///                                       static-dimension-list
1323 ///   static-dimension-list ::= (decimal-literal `x`)*
1324 ///
1325 VectorType Parser::parseVectorType() {
1326   consumeToken(Token::kw_vector);
1327 
1328   if (parseToken(Token::less, "expected '<' in vector type"))
1329     return nullptr;
1330 
1331   SmallVector<int64_t, 4> dimensions;
1332   if (parseDimensionListRanked(dimensions, /*allowDynamic=*/false))
1333     return nullptr;
1334   if (dimensions.empty())
1335     return (emitError("expected dimension size in vector type"), nullptr);
1336   if (any_of(dimensions, [](int64_t i) { return i <= 0; }))
1337     return emitError(getToken().getLoc(),
1338                      "vector types must have positive constant sizes"),
1339            nullptr;
1340 
1341   // Parse the element type.
1342   auto typeLoc = getToken().getLoc();
1343   auto elementType = parseType();
1344   if (!elementType || parseToken(Token::greater, "expected '>' in vector type"))
1345     return nullptr;
1346   if (!VectorType::isValidElementType(elementType))
1347     return emitError(typeLoc, "vector elements must be int or float type"),
1348            nullptr;
1349 
1350   return VectorType::get(dimensions, elementType);
1351 }
1352 
1353 /// Parse a dimension list of a tensor or memref type.  This populates the
1354 /// dimension list, using -1 for the `?` dimensions if `allowDynamic` is set and
1355 /// errors out on `?` otherwise.
1356 ///
1357 ///   dimension-list-ranked ::= (dimension `x`)*
1358 ///   dimension ::= `?` | decimal-literal
1359 ///
1360 /// When `allowDynamic` is not set, this is used to parse:
1361 ///
1362 ///   static-dimension-list ::= (decimal-literal `x`)*
1363 ParseResult
1364 Parser::parseDimensionListRanked(SmallVectorImpl<int64_t> &dimensions,
1365                                  bool allowDynamic) {
1366   while (getToken().isAny(Token::integer, Token::question)) {
1367     if (consumeIf(Token::question)) {
1368       if (!allowDynamic)
1369         return emitError("expected static shape");
1370       dimensions.push_back(-1);
1371     } else {
1372       // Hexadecimal integer literals (starting with `0x`) are not allowed in
1373       // aggregate type declarations.  Therefore, `0xf32` should be processed as
1374       // a sequence of separate elements `0`, `x`, `f32`.
1375       if (getTokenSpelling().size() > 1 && getTokenSpelling()[1] == 'x') {
1376         // We can get here only if the token is an integer literal.  Hexadecimal
1377         // integer literals can only start with `0x` (`1x` wouldn't lex as a
1378         // literal, just `1` would, at which point we don't get into this
1379         // branch).
1380         assert(getTokenSpelling()[0] == '0' && "invalid integer literal");
1381         dimensions.push_back(0);
1382         state.lex.resetPointer(getTokenSpelling().data() + 1);
1383         consumeToken();
1384       } else {
1385         // Make sure this integer value is in bound and valid.
1386         auto dimension = getToken().getUnsignedIntegerValue();
1387         if (!dimension.hasValue())
1388           return emitError("invalid dimension");
1389         dimensions.push_back((int64_t)dimension.getValue());
1390         consumeToken(Token::integer);
1391       }
1392     }
1393 
1394     // Make sure we have an 'x' or something like 'xbf32'.
1395     if (parseXInDimensionList())
1396       return failure();
1397   }
1398 
1399   return success();
1400 }
1401 
1402 /// Parse an 'x' token in a dimension list, handling the case where the x is
1403 /// juxtaposed with an element type, as in "xf32", leaving the "f32" as the next
1404 /// token.
1405 ParseResult Parser::parseXInDimensionList() {
1406   if (getToken().isNot(Token::bare_identifier) || getTokenSpelling()[0] != 'x')
1407     return emitError("expected 'x' in dimension list");
1408 
1409   // If we had a prefix of 'x', lex the next token immediately after the 'x'.
1410   if (getTokenSpelling().size() != 1)
1411     state.lex.resetPointer(getTokenSpelling().data() + 1);
1412 
1413   // Consume the 'x'.
1414   consumeToken(Token::bare_identifier);
1415 
1416   return success();
1417 }
1418 
1419 // Parse a comma-separated list of dimensions, possibly empty:
1420 //   stride-list ::= `[` (dimension (`,` dimension)*)? `]`
1421 ParseResult Parser::parseStrideList(SmallVectorImpl<int64_t> &dimensions) {
1422   if (!consumeIf(Token::l_square))
1423     return failure();
1424   // Empty list early exit.
1425   if (consumeIf(Token::r_square))
1426     return success();
1427   while (true) {
1428     if (consumeIf(Token::question)) {
1429       dimensions.push_back(MemRefType::getDynamicStrideOrOffset());
1430     } else {
1431       // This must be an integer value.
1432       int64_t val;
1433       if (getToken().getSpelling().getAsInteger(10, val))
1434         return emitError("invalid integer value: ") << getToken().getSpelling();
1435       // Make sure it is not the one value for `?`.
1436       if (ShapedType::isDynamic(val))
1437         return emitError("invalid integer value: ")
1438                << getToken().getSpelling()
1439                << ", use `?` to specify a dynamic dimension";
1440       dimensions.push_back(val);
1441       consumeToken(Token::integer);
1442     }
1443     if (!consumeIf(Token::comma))
1444       break;
1445   }
1446   if (!consumeIf(Token::r_square))
1447     return failure();
1448   return success();
1449 }
1450 
1451 //===----------------------------------------------------------------------===//
1452 // Attribute parsing.
1453 //===----------------------------------------------------------------------===//
1454 
1455 /// Return the symbol reference referred to by the given token, that is known to
1456 /// be an @-identifier.
1457 static std::string extractSymbolReference(Token tok) {
1458   assert(tok.is(Token::at_identifier) && "expected valid @-identifier");
1459   StringRef nameStr = tok.getSpelling().drop_front();
1460 
1461   // Check to see if the reference is a string literal, or a bare identifier.
1462   if (nameStr.front() == '"')
1463     return tok.getStringValue();
1464   return std::string(nameStr);
1465 }
1466 
1467 /// Parse an arbitrary attribute.
1468 ///
1469 ///  attribute-value ::= `unit`
1470 ///                    | bool-literal
1471 ///                    | integer-literal (`:` (index-type | integer-type))?
1472 ///                    | float-literal (`:` float-type)?
1473 ///                    | string-literal (`:` type)?
1474 ///                    | type
1475 ///                    | `[` (attribute-value (`,` attribute-value)*)? `]`
1476 ///                    | `{` (attribute-entry (`,` attribute-entry)*)? `}`
1477 ///                    | symbol-ref-id (`::` symbol-ref-id)*
1478 ///                    | `dense` `<` attribute-value `>` `:`
1479 ///                      (tensor-type | vector-type)
1480 ///                    | `sparse` `<` attribute-value `,` attribute-value `>`
1481 ///                      `:` (tensor-type | vector-type)
1482 ///                    | `opaque` `<` dialect-namespace  `,` hex-string-literal
1483 ///                      `>` `:` (tensor-type | vector-type)
1484 ///                    | extended-attribute
1485 ///
1486 Attribute Parser::parseAttribute(Type type) {
1487   switch (getToken().getKind()) {
1488   // Parse an AffineMap or IntegerSet attribute.
1489   case Token::kw_affine_map: {
1490     consumeToken(Token::kw_affine_map);
1491 
1492     AffineMap map;
1493     if (parseToken(Token::less, "expected '<' in affine map") ||
1494         parseAffineMapReference(map) ||
1495         parseToken(Token::greater, "expected '>' in affine map"))
1496       return Attribute();
1497     return AffineMapAttr::get(map);
1498   }
1499   case Token::kw_affine_set: {
1500     consumeToken(Token::kw_affine_set);
1501 
1502     IntegerSet set;
1503     if (parseToken(Token::less, "expected '<' in integer set") ||
1504         parseIntegerSetReference(set) ||
1505         parseToken(Token::greater, "expected '>' in integer set"))
1506       return Attribute();
1507     return IntegerSetAttr::get(set);
1508   }
1509 
1510   // Parse an array attribute.
1511   case Token::l_square: {
1512     consumeToken(Token::l_square);
1513 
1514     SmallVector<Attribute, 4> elements;
1515     auto parseElt = [&]() -> ParseResult {
1516       elements.push_back(parseAttribute());
1517       return elements.back() ? success() : failure();
1518     };
1519 
1520     if (parseCommaSeparatedListUntil(Token::r_square, parseElt))
1521       return nullptr;
1522     return builder.getArrayAttr(elements);
1523   }
1524 
1525   // Parse a boolean attribute.
1526   case Token::kw_false:
1527     consumeToken(Token::kw_false);
1528     return builder.getBoolAttr(false);
1529   case Token::kw_true:
1530     consumeToken(Token::kw_true);
1531     return builder.getBoolAttr(true);
1532 
1533   // Parse a dense elements attribute.
1534   case Token::kw_dense:
1535     return parseDenseElementsAttr(type);
1536 
1537   // Parse a dictionary attribute.
1538   case Token::l_brace: {
1539     SmallVector<NamedAttribute, 4> elements;
1540     if (parseAttributeDict(elements))
1541       return nullptr;
1542     return builder.getDictionaryAttr(elements);
1543   }
1544 
1545   // Parse an extended attribute, i.e. alias or dialect attribute.
1546   case Token::hash_identifier:
1547     return parseExtendedAttr(type);
1548 
1549   // Parse floating point and integer attributes.
1550   case Token::floatliteral:
1551     return parseFloatAttr(type, /*isNegative=*/false);
1552   case Token::integer:
1553     return parseDecOrHexAttr(type, /*isNegative=*/false);
1554   case Token::minus: {
1555     consumeToken(Token::minus);
1556     if (getToken().is(Token::integer))
1557       return parseDecOrHexAttr(type, /*isNegative=*/true);
1558     if (getToken().is(Token::floatliteral))
1559       return parseFloatAttr(type, /*isNegative=*/true);
1560 
1561     return (emitError("expected constant integer or floating point value"),
1562             nullptr);
1563   }
1564 
1565   // Parse a location attribute.
1566   case Token::kw_loc: {
1567     LocationAttr attr;
1568     return failed(parseLocation(attr)) ? Attribute() : attr;
1569   }
1570 
1571   // Parse an opaque elements attribute.
1572   case Token::kw_opaque:
1573     return parseOpaqueElementsAttr(type);
1574 
1575   // Parse a sparse elements attribute.
1576   case Token::kw_sparse:
1577     return parseSparseElementsAttr(type);
1578 
1579   // Parse a string attribute.
1580   case Token::string: {
1581     auto val = getToken().getStringValue();
1582     consumeToken(Token::string);
1583     // Parse the optional trailing colon type if one wasn't explicitly provided.
1584     if (!type && consumeIf(Token::colon) && !(type = parseType()))
1585       return Attribute();
1586 
1587     return type ? StringAttr::get(val, type)
1588                 : StringAttr::get(val, getContext());
1589   }
1590 
1591   // Parse a symbol reference attribute.
1592   case Token::at_identifier: {
1593     std::string nameStr = extractSymbolReference(getToken());
1594     consumeToken(Token::at_identifier);
1595 
1596     // Parse any nested references.
1597     std::vector<FlatSymbolRefAttr> nestedRefs;
1598     while (getToken().is(Token::colon)) {
1599       // Check for the '::' prefix.
1600       const char *curPointer = getToken().getLoc().getPointer();
1601       consumeToken(Token::colon);
1602       if (!consumeIf(Token::colon)) {
1603         state.lex.resetPointer(curPointer);
1604         consumeToken();
1605         break;
1606       }
1607       // Parse the reference itself.
1608       auto curLoc = getToken().getLoc();
1609       if (getToken().isNot(Token::at_identifier)) {
1610         emitError(curLoc, "expected nested symbol reference identifier");
1611         return Attribute();
1612       }
1613 
1614       std::string nameStr = extractSymbolReference(getToken());
1615       consumeToken(Token::at_identifier);
1616       nestedRefs.push_back(SymbolRefAttr::get(nameStr, getContext()));
1617     }
1618 
1619     return builder.getSymbolRefAttr(nameStr, nestedRefs);
1620   }
1621 
1622   // Parse a 'unit' attribute.
1623   case Token::kw_unit:
1624     consumeToken(Token::kw_unit);
1625     return builder.getUnitAttr();
1626 
1627   default:
1628     // Parse a type attribute.
1629     if (Type type = parseType())
1630       return TypeAttr::get(type);
1631     return nullptr;
1632   }
1633 }
1634 
1635 /// Attribute dictionary.
1636 ///
1637 ///   attribute-dict ::= `{` `}`
1638 ///                    | `{` attribute-entry (`,` attribute-entry)* `}`
1639 ///   attribute-entry ::= bare-id `=` attribute-value
1640 ///
1641 ParseResult
1642 Parser::parseAttributeDict(SmallVectorImpl<NamedAttribute> &attributes) {
1643   if (parseToken(Token::l_brace, "expected '{' in attribute dictionary"))
1644     return failure();
1645 
1646   auto parseElt = [&]() -> ParseResult {
1647     // We allow keywords as attribute names.
1648     if (getToken().isNot(Token::bare_identifier, Token::inttype) &&
1649         !getToken().isKeyword())
1650       return emitError("expected attribute name");
1651     Identifier nameId = builder.getIdentifier(getTokenSpelling());
1652     consumeToken();
1653 
1654     // Try to parse the '=' for the attribute value.
1655     if (!consumeIf(Token::equal)) {
1656       // If there is no '=', we treat this as a unit attribute.
1657       attributes.push_back({nameId, builder.getUnitAttr()});
1658       return success();
1659     }
1660 
1661     auto attr = parseAttribute();
1662     if (!attr)
1663       return failure();
1664 
1665     attributes.push_back({nameId, attr});
1666     return success();
1667   };
1668 
1669   if (parseCommaSeparatedListUntil(Token::r_brace, parseElt))
1670     return failure();
1671 
1672   return success();
1673 }
1674 
1675 /// Parse an extended attribute.
1676 ///
1677 ///   extended-attribute ::= (dialect-attribute | attribute-alias)
1678 ///   dialect-attribute  ::= `#` dialect-namespace `<` `"` attr-data `"` `>`
1679 ///   dialect-attribute  ::= `#` alias-name pretty-dialect-sym-body?
1680 ///   attribute-alias    ::= `#` alias-name
1681 ///
1682 Attribute Parser::parseExtendedAttr(Type type) {
1683   Attribute attr = parseExtendedSymbol<Attribute>(
1684       *this, Token::hash_identifier, state.symbols.attributeAliasDefinitions,
1685       [&](StringRef dialectName, StringRef symbolData,
1686           llvm::SMLoc loc) -> Attribute {
1687         // Parse an optional trailing colon type.
1688         Type attrType = type;
1689         if (consumeIf(Token::colon) && !(attrType = parseType()))
1690           return Attribute();
1691 
1692         // If we found a registered dialect, then ask it to parse the attribute.
1693         if (auto *dialect = state.context->getRegisteredDialect(dialectName)) {
1694           return parseSymbol<Attribute>(
1695               symbolData, state.context, state.symbols, [&](Parser &parser) {
1696                 CustomDialectAsmParser customParser(symbolData, parser);
1697                 return dialect->parseAttribute(customParser, attrType);
1698               });
1699         }
1700 
1701         // Otherwise, form a new opaque attribute.
1702         return OpaqueAttr::getChecked(
1703             Identifier::get(dialectName, state.context), symbolData,
1704             attrType ? attrType : NoneType::get(state.context),
1705             getEncodedSourceLocation(loc));
1706       });
1707 
1708   // Ensure that the attribute has the same type as requested.
1709   if (attr && type && attr.getType() != type) {
1710     emitError("attribute type different than expected: expected ")
1711         << type << ", but got " << attr.getType();
1712     return nullptr;
1713   }
1714   return attr;
1715 }
1716 
1717 /// Parse a float attribute.
1718 Attribute Parser::parseFloatAttr(Type type, bool isNegative) {
1719   auto val = getToken().getFloatingPointValue();
1720   if (!val.hasValue())
1721     return (emitError("floating point value too large for attribute"), nullptr);
1722   consumeToken(Token::floatliteral);
1723   if (!type) {
1724     // Default to F64 when no type is specified.
1725     if (!consumeIf(Token::colon))
1726       type = builder.getF64Type();
1727     else if (!(type = parseType()))
1728       return nullptr;
1729   }
1730   if (!type.isa<FloatType>())
1731     return (emitError("floating point value not valid for specified type"),
1732             nullptr);
1733   return FloatAttr::get(type, isNegative ? -val.getValue() : val.getValue());
1734 }
1735 
1736 /// Construct a float attribute bitwise equivalent to the integer literal.
1737 static Optional<APFloat> buildHexadecimalFloatLiteral(Parser *p, FloatType type,
1738                                                       uint64_t value) {
1739   // FIXME: bfloat is currently stored as a double internally because it doesn't
1740   // have valid APFloat semantics.
1741   if (type.isF64() || type.isBF16())
1742     return APFloat(type.getFloatSemantics(), APInt(/*numBits=*/64, value));
1743 
1744   APInt apInt(type.getWidth(), value);
1745   if (apInt != value) {
1746     p->emitError("hexadecimal float constant out of range for type");
1747     return llvm::None;
1748   }
1749   return APFloat(type.getFloatSemantics(), apInt);
1750 }
1751 
1752 /// Parse a decimal or a hexadecimal literal, which can be either an integer
1753 /// or a float attribute.
1754 Attribute Parser::parseDecOrHexAttr(Type type, bool isNegative) {
1755   auto val = getToken().getUInt64IntegerValue();
1756   if (!val.hasValue())
1757     return (emitError("integer constant out of range for attribute"), nullptr);
1758 
1759   // Remember if the literal is hexadecimal.
1760   StringRef spelling = getToken().getSpelling();
1761   auto loc = state.curToken.getLoc();
1762   bool isHex = spelling.size() > 1 && spelling[1] == 'x';
1763 
1764   consumeToken(Token::integer);
1765   if (!type) {
1766     // Default to i64 if not type is specified.
1767     if (!consumeIf(Token::colon))
1768       type = builder.getIntegerType(64);
1769     else if (!(type = parseType()))
1770       return nullptr;
1771   }
1772 
1773   if (auto floatType = type.dyn_cast<FloatType>()) {
1774     if (isNegative)
1775       return emitError(
1776                  loc,
1777                  "hexadecimal float literal should not have a leading minus"),
1778              nullptr;
1779     if (!isHex) {
1780       emitError(loc, "unexpected decimal integer literal for a float attribute")
1781               .attachNote()
1782           << "add a trailing dot to make the literal a float";
1783       return nullptr;
1784     }
1785 
1786     // Construct a float attribute bitwise equivalent to the integer literal.
1787     Optional<APFloat> apVal =
1788         buildHexadecimalFloatLiteral(this, floatType, *val);
1789     return apVal ? FloatAttr::get(floatType, *apVal) : Attribute();
1790   }
1791 
1792   if (!type.isIntOrIndex())
1793     return emitError(loc, "integer literal not valid for specified type"),
1794            nullptr;
1795 
1796   // Parse the integer literal.
1797   int width = type.isIndex() ? 64 : type.getIntOrFloatBitWidth();
1798   APInt apInt(width, *val, isNegative);
1799   if (apInt != *val)
1800     return emitError(loc, "integer constant out of range for attribute"),
1801            nullptr;
1802 
1803   // Otherwise construct an integer attribute.
1804   if (isNegative ? (int64_t)-val.getValue() >= 0 : (int64_t)val.getValue() < 0)
1805     return emitError(loc, "integer constant out of range for attribute"),
1806            nullptr;
1807 
1808   return builder.getIntegerAttr(type, isNegative ? -apInt : apInt);
1809 }
1810 
1811 /// Parse an opaque elements attribute.
1812 Attribute Parser::parseOpaqueElementsAttr(Type attrType) {
1813   consumeToken(Token::kw_opaque);
1814   if (parseToken(Token::less, "expected '<' after 'opaque'"))
1815     return nullptr;
1816 
1817   if (getToken().isNot(Token::string))
1818     return (emitError("expected dialect namespace"), nullptr);
1819 
1820   auto name = getToken().getStringValue();
1821   auto *dialect = builder.getContext()->getRegisteredDialect(name);
1822   // TODO(shpeisman): Allow for having an unknown dialect on an opaque
1823   // attribute. Otherwise, it can't be roundtripped without having the dialect
1824   // registered.
1825   if (!dialect)
1826     return (emitError("no registered dialect with namespace '" + name + "'"),
1827             nullptr);
1828 
1829   consumeToken(Token::string);
1830   if (parseToken(Token::comma, "expected ','"))
1831     return nullptr;
1832 
1833   if (getToken().getKind() != Token::string)
1834     return (emitError("opaque string should start with '0x'"), nullptr);
1835 
1836   auto val = getToken().getStringValue();
1837   if (val.size() < 2 || val[0] != '0' || val[1] != 'x')
1838     return (emitError("opaque string should start with '0x'"), nullptr);
1839 
1840   val = val.substr(2);
1841   if (!llvm::all_of(val, llvm::isHexDigit))
1842     return (emitError("opaque string only contains hex digits"), nullptr);
1843 
1844   consumeToken(Token::string);
1845   if (parseToken(Token::greater, "expected '>'"))
1846     return nullptr;
1847 
1848   auto type = parseElementsLiteralType(attrType);
1849   if (!type)
1850     return nullptr;
1851 
1852   return OpaqueElementsAttr::get(dialect, type, llvm::fromHex(val));
1853 }
1854 
1855 namespace {
1856 class TensorLiteralParser {
1857 public:
1858   TensorLiteralParser(Parser &p) : p(p) {}
1859 
1860   ParseResult parse() {
1861     if (p.getToken().is(Token::l_square))
1862       return parseList(shape);
1863     return parseElement();
1864   }
1865 
1866   /// Build a dense attribute instance with the parsed elements and the given
1867   /// shaped type.
1868   DenseElementsAttr getAttr(llvm::SMLoc loc, ShapedType type);
1869 
1870   ArrayRef<int64_t> getShape() const { return shape; }
1871 
1872 private:
1873   enum class ElementKind { Boolean, Integer, Float };
1874 
1875   /// Return a string to represent the given element kind.
1876   const char *getElementKindStr(ElementKind kind) {
1877     switch (kind) {
1878     case ElementKind::Boolean:
1879       return "'boolean'";
1880     case ElementKind::Integer:
1881       return "'integer'";
1882     case ElementKind::Float:
1883       return "'float'";
1884     }
1885     llvm_unreachable("unknown element kind");
1886   }
1887 
1888   /// Build a Dense Integer attribute for the given type.
1889   DenseElementsAttr getIntAttr(llvm::SMLoc loc, ShapedType type,
1890                                IntegerType eltTy);
1891 
1892   /// Build a Dense Float attribute for the given type.
1893   DenseElementsAttr getFloatAttr(llvm::SMLoc loc, ShapedType type,
1894                                  FloatType eltTy);
1895 
1896   /// Parse a single element, returning failure if it isn't a valid element
1897   /// literal. For example:
1898   /// parseElement(1) -> Success, 1
1899   /// parseElement([1]) -> Failure
1900   ParseResult parseElement();
1901 
1902   /// Parse a list of either lists or elements, returning the dimensions of the
1903   /// parsed sub-tensors in dims. For example:
1904   ///   parseList([1, 2, 3]) -> Success, [3]
1905   ///   parseList([[1, 2], [3, 4]]) -> Success, [2, 2]
1906   ///   parseList([[1, 2], 3]) -> Failure
1907   ///   parseList([[1, [2, 3]], [4, [5]]]) -> Failure
1908   ParseResult parseList(SmallVectorImpl<int64_t> &dims);
1909 
1910   Parser &p;
1911 
1912   /// The shape inferred from the parsed elements.
1913   SmallVector<int64_t, 4> shape;
1914 
1915   /// Storage used when parsing elements, this is a pair of <is_negated, token>.
1916   std::vector<std::pair<bool, Token>> storage;
1917 
1918   /// A flag that indicates the type of elements that have been parsed.
1919   Optional<ElementKind> knownEltKind;
1920 };
1921 } // namespace
1922 
1923 /// Build a dense attribute instance with the parsed elements and the given
1924 /// shaped type.
1925 DenseElementsAttr TensorLiteralParser::getAttr(llvm::SMLoc loc,
1926                                                ShapedType type) {
1927   // Check that the parsed storage size has the same number of elements to the
1928   // type, or is a known splat.
1929   if (!shape.empty() && getShape() != type.getShape()) {
1930     p.emitError(loc) << "inferred shape of elements literal ([" << getShape()
1931                      << "]) does not match type ([" << type.getShape() << "])";
1932     return nullptr;
1933   }
1934 
1935   // If the type is an integer, build a set of APInt values from the storage
1936   // with the correct bitwidth.
1937   if (auto intTy = type.getElementType().dyn_cast<IntegerType>())
1938     return getIntAttr(loc, type, intTy);
1939 
1940   // Otherwise, this must be a floating point type.
1941   auto floatTy = type.getElementType().dyn_cast<FloatType>();
1942   if (!floatTy) {
1943     p.emitError(loc) << "expected floating-point or integer element type, got "
1944                      << type.getElementType();
1945     return nullptr;
1946   }
1947   return getFloatAttr(loc, type, floatTy);
1948 }
1949 
1950 /// Build a Dense Integer attribute for the given type.
1951 DenseElementsAttr TensorLiteralParser::getIntAttr(llvm::SMLoc loc,
1952                                                   ShapedType type,
1953                                                   IntegerType eltTy) {
1954   std::vector<APInt> intElements;
1955   intElements.reserve(storage.size());
1956   for (const auto &signAndToken : storage) {
1957     bool isNegative = signAndToken.first;
1958     const Token &token = signAndToken.second;
1959 
1960     // Check to see if floating point values were parsed.
1961     if (token.is(Token::floatliteral)) {
1962       p.emitError() << "expected integer elements, but parsed floating-point";
1963       return nullptr;
1964     }
1965 
1966     assert(token.isAny(Token::integer, Token::kw_true, Token::kw_false) &&
1967            "unexpected token type");
1968     if (token.isAny(Token::kw_true, Token::kw_false)) {
1969       if (!eltTy.isInteger(1))
1970         p.emitError() << "expected i1 type for 'true' or 'false' values";
1971       APInt apInt(eltTy.getWidth(), token.is(Token::kw_true),
1972                   /*isSigned=*/false);
1973       intElements.push_back(apInt);
1974       continue;
1975     }
1976 
1977     // Create APInt values for each element with the correct bitwidth.
1978     auto val = token.getUInt64IntegerValue();
1979     if (!val.hasValue() || (isNegative ? (int64_t)-val.getValue() >= 0
1980                                        : (int64_t)val.getValue() < 0)) {
1981       p.emitError(token.getLoc(),
1982                   "integer constant out of range for attribute");
1983       return nullptr;
1984     }
1985     APInt apInt(eltTy.getWidth(), val.getValue(), isNegative);
1986     if (apInt != val.getValue())
1987       return (p.emitError("integer constant out of range for type"), nullptr);
1988     intElements.push_back(isNegative ? -apInt : apInt);
1989   }
1990 
1991   return DenseElementsAttr::get(type, intElements);
1992 }
1993 
1994 /// Build a Dense Float attribute for the given type.
1995 DenseElementsAttr TensorLiteralParser::getFloatAttr(llvm::SMLoc loc,
1996                                                     ShapedType type,
1997                                                     FloatType eltTy) {
1998   std::vector<APFloat> floatValues;
1999   floatValues.reserve(storage.size());
2000   for (const auto &signAndToken : storage) {
2001     bool isNegative = signAndToken.first;
2002     const Token &token = signAndToken.second;
2003 
2004     // Handle hexadecimal float literals.
2005     if (token.is(Token::integer) && token.getSpelling().startswith("0x")) {
2006       if (isNegative) {
2007         p.emitError(token.getLoc())
2008             << "hexadecimal float literal should not have a leading minus";
2009         return nullptr;
2010       }
2011       auto val = token.getUInt64IntegerValue();
2012       if (!val.hasValue()) {
2013         p.emitError("hexadecimal float constant out of range for attribute");
2014         return nullptr;
2015       }
2016       Optional<APFloat> apVal = buildHexadecimalFloatLiteral(&p, eltTy, *val);
2017       if (!apVal)
2018         return nullptr;
2019       floatValues.push_back(*apVal);
2020       continue;
2021     }
2022 
2023     // Check to see if any decimal integers or booleans were parsed.
2024     if (!token.is(Token::floatliteral)) {
2025       p.emitError() << "expected floating-point elements, but parsed integer";
2026       return nullptr;
2027     }
2028 
2029     // Build the float values from tokens.
2030     auto val = token.getFloatingPointValue();
2031     if (!val.hasValue()) {
2032       p.emitError("floating point value too large for attribute");
2033       return nullptr;
2034     }
2035     // Treat BF16 as double because it is not supported in LLVM's APFloat.
2036     APFloat apVal(isNegative ? -*val : *val);
2037     if (!eltTy.isBF16() && !eltTy.isF64()) {
2038       bool unused;
2039       apVal.convert(eltTy.getFloatSemantics(), APFloat::rmNearestTiesToEven,
2040                     &unused);
2041     }
2042     floatValues.push_back(apVal);
2043   }
2044 
2045   return DenseElementsAttr::get(type, floatValues);
2046 }
2047 
2048 ParseResult TensorLiteralParser::parseElement() {
2049   switch (p.getToken().getKind()) {
2050   // Parse a boolean element.
2051   case Token::kw_true:
2052   case Token::kw_false:
2053   case Token::floatliteral:
2054   case Token::integer:
2055     storage.emplace_back(/*isNegative=*/false, p.getToken());
2056     p.consumeToken();
2057     break;
2058 
2059   // Parse a signed integer or a negative floating-point element.
2060   case Token::minus:
2061     p.consumeToken(Token::minus);
2062     if (!p.getToken().isAny(Token::floatliteral, Token::integer))
2063       return p.emitError("expected integer or floating point literal");
2064     storage.emplace_back(/*isNegative=*/true, p.getToken());
2065     p.consumeToken();
2066     break;
2067 
2068   default:
2069     return p.emitError("expected element literal of primitive type");
2070   }
2071 
2072   return success();
2073 }
2074 
2075 /// Parse a list of either lists or elements, returning the dimensions of the
2076 /// parsed sub-tensors in dims. For example:
2077 ///   parseList([1, 2, 3]) -> Success, [3]
2078 ///   parseList([[1, 2], [3, 4]]) -> Success, [2, 2]
2079 ///   parseList([[1, 2], 3]) -> Failure
2080 ///   parseList([[1, [2, 3]], [4, [5]]]) -> Failure
2081 ParseResult TensorLiteralParser::parseList(SmallVectorImpl<int64_t> &dims) {
2082   p.consumeToken(Token::l_square);
2083 
2084   auto checkDims = [&](const SmallVectorImpl<int64_t> &prevDims,
2085                        const SmallVectorImpl<int64_t> &newDims) -> ParseResult {
2086     if (prevDims == newDims)
2087       return success();
2088     return p.emitError("tensor literal is invalid; ranks are not consistent "
2089                        "between elements");
2090   };
2091 
2092   bool first = true;
2093   SmallVector<int64_t, 4> newDims;
2094   unsigned size = 0;
2095   auto parseCommaSeparatedList = [&]() -> ParseResult {
2096     SmallVector<int64_t, 4> thisDims;
2097     if (p.getToken().getKind() == Token::l_square) {
2098       if (parseList(thisDims))
2099         return failure();
2100     } else if (parseElement()) {
2101       return failure();
2102     }
2103     ++size;
2104     if (!first)
2105       return checkDims(newDims, thisDims);
2106     newDims = thisDims;
2107     first = false;
2108     return success();
2109   };
2110   if (p.parseCommaSeparatedListUntil(Token::r_square, parseCommaSeparatedList))
2111     return failure();
2112 
2113   // Return the sublists' dimensions with 'size' prepended.
2114   dims.clear();
2115   dims.push_back(size);
2116   dims.append(newDims.begin(), newDims.end());
2117   return success();
2118 }
2119 
2120 /// Parse a dense elements attribute.
2121 Attribute Parser::parseDenseElementsAttr(Type attrType) {
2122   consumeToken(Token::kw_dense);
2123   if (parseToken(Token::less, "expected '<' after 'dense'"))
2124     return nullptr;
2125 
2126   // Parse the literal data.
2127   TensorLiteralParser literalParser(*this);
2128   if (literalParser.parse())
2129     return nullptr;
2130 
2131   if (parseToken(Token::greater, "expected '>'"))
2132     return nullptr;
2133 
2134   auto typeLoc = getToken().getLoc();
2135   auto type = parseElementsLiteralType(attrType);
2136   if (!type)
2137     return nullptr;
2138   return literalParser.getAttr(typeLoc, type);
2139 }
2140 
2141 /// Shaped type for elements attribute.
2142 ///
2143 ///   elements-literal-type ::= vector-type | ranked-tensor-type
2144 ///
2145 /// This method also checks the type has static shape.
2146 ShapedType Parser::parseElementsLiteralType(Type type) {
2147   // If the user didn't provide a type, parse the colon type for the literal.
2148   if (!type) {
2149     if (parseToken(Token::colon, "expected ':'"))
2150       return nullptr;
2151     if (!(type = parseType()))
2152       return nullptr;
2153   }
2154 
2155   if (!type.isa<RankedTensorType>() && !type.isa<VectorType>()) {
2156     emitError("elements literal must be a ranked tensor or vector type");
2157     return nullptr;
2158   }
2159 
2160   auto sType = type.cast<ShapedType>();
2161   if (!sType.hasStaticShape())
2162     return (emitError("elements literal type must have static shape"), nullptr);
2163 
2164   return sType;
2165 }
2166 
2167 /// Parse a sparse elements attribute.
2168 Attribute Parser::parseSparseElementsAttr(Type attrType) {
2169   consumeToken(Token::kw_sparse);
2170   if (parseToken(Token::less, "Expected '<' after 'sparse'"))
2171     return nullptr;
2172 
2173   /// Parse indices
2174   auto indicesLoc = getToken().getLoc();
2175   TensorLiteralParser indiceParser(*this);
2176   if (indiceParser.parse())
2177     return nullptr;
2178 
2179   if (parseToken(Token::comma, "expected ','"))
2180     return nullptr;
2181 
2182   /// Parse values.
2183   auto valuesLoc = getToken().getLoc();
2184   TensorLiteralParser valuesParser(*this);
2185   if (valuesParser.parse())
2186     return nullptr;
2187 
2188   if (parseToken(Token::greater, "expected '>'"))
2189     return nullptr;
2190 
2191   auto type = parseElementsLiteralType(attrType);
2192   if (!type)
2193     return nullptr;
2194 
2195   // If the indices are a splat, i.e. the literal parser parsed an element and
2196   // not a list, we set the shape explicitly. The indices are represented by a
2197   // 2-dimensional shape where the second dimension is the rank of the type.
2198   // Given that the parsed indices is a splat, we know that we only have one
2199   // indice and thus one for the first dimension.
2200   auto indiceEltType = builder.getIntegerType(64);
2201   ShapedType indicesType;
2202   if (indiceParser.getShape().empty()) {
2203     indicesType = RankedTensorType::get({1, type.getRank()}, indiceEltType);
2204   } else {
2205     // Otherwise, set the shape to the one parsed by the literal parser.
2206     indicesType = RankedTensorType::get(indiceParser.getShape(), indiceEltType);
2207   }
2208   auto indices = indiceParser.getAttr(indicesLoc, indicesType);
2209 
2210   // If the values are a splat, set the shape explicitly based on the number of
2211   // indices. The number of indices is encoded in the first dimension of the
2212   // indice shape type.
2213   auto valuesEltType = type.getElementType();
2214   ShapedType valuesType =
2215       valuesParser.getShape().empty()
2216           ? RankedTensorType::get({indicesType.getDimSize(0)}, valuesEltType)
2217           : RankedTensorType::get(valuesParser.getShape(), valuesEltType);
2218   auto values = valuesParser.getAttr(valuesLoc, valuesType);
2219 
2220   /// Sanity check.
2221   if (valuesType.getRank() != 1)
2222     return (emitError("expected 1-d tensor for values"), nullptr);
2223 
2224   auto sameShape = (indicesType.getRank() == 1) ||
2225                    (type.getRank() == indicesType.getDimSize(1));
2226   auto sameElementNum = indicesType.getDimSize(0) == valuesType.getDimSize(0);
2227   if (!sameShape || !sameElementNum) {
2228     emitError() << "expected shape ([" << type.getShape()
2229                 << "]); inferred shape of indices literal (["
2230                 << indicesType.getShape()
2231                 << "]); inferred shape of values literal (["
2232                 << valuesType.getShape() << "])";
2233     return nullptr;
2234   }
2235 
2236   // Build the sparse elements attribute by the indices and values.
2237   return SparseElementsAttr::get(type, indices, values);
2238 }
2239 
2240 //===----------------------------------------------------------------------===//
2241 // Location parsing.
2242 //===----------------------------------------------------------------------===//
2243 
2244 /// Parse a location.
2245 ///
2246 ///   location           ::= `loc` inline-location
2247 ///   inline-location    ::= '(' location-inst ')'
2248 ///
2249 ParseResult Parser::parseLocation(LocationAttr &loc) {
2250   // Check for 'loc' identifier.
2251   if (parseToken(Token::kw_loc, "expected 'loc' keyword"))
2252     return emitError();
2253 
2254   // Parse the inline-location.
2255   if (parseToken(Token::l_paren, "expected '(' in inline location") ||
2256       parseLocationInstance(loc) ||
2257       parseToken(Token::r_paren, "expected ')' in inline location"))
2258     return failure();
2259   return success();
2260 }
2261 
2262 /// Specific location instances.
2263 ///
2264 /// location-inst ::= filelinecol-location |
2265 ///                   name-location |
2266 ///                   callsite-location |
2267 ///                   fused-location |
2268 ///                   unknown-location
2269 /// filelinecol-location ::= string-literal ':' integer-literal
2270 ///                                         ':' integer-literal
2271 /// name-location ::= string-literal
2272 /// callsite-location ::= 'callsite' '(' location-inst 'at' location-inst ')'
2273 /// fused-location ::= fused ('<' attribute-value '>')?
2274 ///                    '[' location-inst (location-inst ',')* ']'
2275 /// unknown-location ::= 'unknown'
2276 ///
2277 ParseResult Parser::parseCallSiteLocation(LocationAttr &loc) {
2278   consumeToken(Token::bare_identifier);
2279 
2280   // Parse the '('.
2281   if (parseToken(Token::l_paren, "expected '(' in callsite location"))
2282     return failure();
2283 
2284   // Parse the callee location.
2285   LocationAttr calleeLoc;
2286   if (parseLocationInstance(calleeLoc))
2287     return failure();
2288 
2289   // Parse the 'at'.
2290   if (getToken().isNot(Token::bare_identifier) ||
2291       getToken().getSpelling() != "at")
2292     return emitError("expected 'at' in callsite location");
2293   consumeToken(Token::bare_identifier);
2294 
2295   // Parse the caller location.
2296   LocationAttr callerLoc;
2297   if (parseLocationInstance(callerLoc))
2298     return failure();
2299 
2300   // Parse the ')'.
2301   if (parseToken(Token::r_paren, "expected ')' in callsite location"))
2302     return failure();
2303 
2304   // Return the callsite location.
2305   loc = CallSiteLoc::get(calleeLoc, callerLoc);
2306   return success();
2307 }
2308 
2309 ParseResult Parser::parseFusedLocation(LocationAttr &loc) {
2310   consumeToken(Token::bare_identifier);
2311 
2312   // Try to parse the optional metadata.
2313   Attribute metadata;
2314   if (consumeIf(Token::less)) {
2315     metadata = parseAttribute();
2316     if (!metadata)
2317       return emitError("expected valid attribute metadata");
2318     // Parse the '>' token.
2319     if (parseToken(Token::greater,
2320                    "expected '>' after fused location metadata"))
2321       return failure();
2322   }
2323 
2324   SmallVector<Location, 4> locations;
2325   auto parseElt = [&] {
2326     LocationAttr newLoc;
2327     if (parseLocationInstance(newLoc))
2328       return failure();
2329     locations.push_back(newLoc);
2330     return success();
2331   };
2332 
2333   if (parseToken(Token::l_square, "expected '[' in fused location") ||
2334       parseCommaSeparatedList(parseElt) ||
2335       parseToken(Token::r_square, "expected ']' in fused location"))
2336     return failure();
2337 
2338   // Return the fused location.
2339   loc = FusedLoc::get(locations, metadata, getContext());
2340   return success();
2341 }
2342 
2343 ParseResult Parser::parseNameOrFileLineColLocation(LocationAttr &loc) {
2344   auto *ctx = getContext();
2345   auto str = getToken().getStringValue();
2346   consumeToken(Token::string);
2347 
2348   // If the next token is ':' this is a filelinecol location.
2349   if (consumeIf(Token::colon)) {
2350     // Parse the line number.
2351     if (getToken().isNot(Token::integer))
2352       return emitError("expected integer line number in FileLineColLoc");
2353     auto line = getToken().getUnsignedIntegerValue();
2354     if (!line.hasValue())
2355       return emitError("expected integer line number in FileLineColLoc");
2356     consumeToken(Token::integer);
2357 
2358     // Parse the ':'.
2359     if (parseToken(Token::colon, "expected ':' in FileLineColLoc"))
2360       return failure();
2361 
2362     // Parse the column number.
2363     if (getToken().isNot(Token::integer))
2364       return emitError("expected integer column number in FileLineColLoc");
2365     auto column = getToken().getUnsignedIntegerValue();
2366     if (!column.hasValue())
2367       return emitError("expected integer column number in FileLineColLoc");
2368     consumeToken(Token::integer);
2369 
2370     loc = FileLineColLoc::get(str, line.getValue(), column.getValue(), ctx);
2371     return success();
2372   }
2373 
2374   // Otherwise, this is a NameLoc.
2375 
2376   // Check for a child location.
2377   if (consumeIf(Token::l_paren)) {
2378     auto childSourceLoc = getToken().getLoc();
2379 
2380     // Parse the child location.
2381     LocationAttr childLoc;
2382     if (parseLocationInstance(childLoc))
2383       return failure();
2384 
2385     // The child must not be another NameLoc.
2386     if (childLoc.isa<NameLoc>())
2387       return emitError(childSourceLoc,
2388                        "child of NameLoc cannot be another NameLoc");
2389     loc = NameLoc::get(Identifier::get(str, ctx), childLoc);
2390 
2391     // Parse the closing ')'.
2392     if (parseToken(Token::r_paren,
2393                    "expected ')' after child location of NameLoc"))
2394       return failure();
2395   } else {
2396     loc = NameLoc::get(Identifier::get(str, ctx), ctx);
2397   }
2398 
2399   return success();
2400 }
2401 
2402 ParseResult Parser::parseLocationInstance(LocationAttr &loc) {
2403   // Handle either name or filelinecol locations.
2404   if (getToken().is(Token::string))
2405     return parseNameOrFileLineColLocation(loc);
2406 
2407   // Bare tokens required for other cases.
2408   if (!getToken().is(Token::bare_identifier))
2409     return emitError("expected location instance");
2410 
2411   // Check for the 'callsite' signifying a callsite location.
2412   if (getToken().getSpelling() == "callsite")
2413     return parseCallSiteLocation(loc);
2414 
2415   // If the token is 'fused', then this is a fused location.
2416   if (getToken().getSpelling() == "fused")
2417     return parseFusedLocation(loc);
2418 
2419   // Check for a 'unknown' for an unknown location.
2420   if (getToken().getSpelling() == "unknown") {
2421     consumeToken(Token::bare_identifier);
2422     loc = UnknownLoc::get(getContext());
2423     return success();
2424   }
2425 
2426   return emitError("expected location instance");
2427 }
2428 
2429 //===----------------------------------------------------------------------===//
2430 // Affine parsing.
2431 //===----------------------------------------------------------------------===//
2432 
2433 /// Lower precedence ops (all at the same precedence level). LNoOp is false in
2434 /// the boolean sense.
2435 enum AffineLowPrecOp {
2436   /// Null value.
2437   LNoOp,
2438   Add,
2439   Sub
2440 };
2441 
2442 /// Higher precedence ops - all at the same precedence level. HNoOp is false
2443 /// in the boolean sense.
2444 enum AffineHighPrecOp {
2445   /// Null value.
2446   HNoOp,
2447   Mul,
2448   FloorDiv,
2449   CeilDiv,
2450   Mod
2451 };
2452 
2453 namespace {
2454 /// This is a specialized parser for affine structures (affine maps, affine
2455 /// expressions, and integer sets), maintaining the state transient to their
2456 /// bodies.
2457 class AffineParser : public Parser {
2458 public:
2459   AffineParser(ParserState &state, bool allowParsingSSAIds = false,
2460                function_ref<ParseResult(bool)> parseElement = nullptr)
2461       : Parser(state), allowParsingSSAIds(allowParsingSSAIds),
2462         parseElement(parseElement), numDimOperands(0), numSymbolOperands(0) {}
2463 
2464   AffineMap parseAffineMapRange(unsigned numDims, unsigned numSymbols);
2465   ParseResult parseAffineMapOrIntegerSetInline(AffineMap &map, IntegerSet &set);
2466   IntegerSet parseIntegerSetConstraints(unsigned numDims, unsigned numSymbols);
2467   ParseResult parseAffineMapOfSSAIds(AffineMap &map,
2468                                      OpAsmParser::Delimiter delimiter);
2469   void getDimsAndSymbolSSAIds(SmallVectorImpl<StringRef> &dimAndSymbolSSAIds,
2470                               unsigned &numDims);
2471 
2472 private:
2473   // Binary affine op parsing.
2474   AffineLowPrecOp consumeIfLowPrecOp();
2475   AffineHighPrecOp consumeIfHighPrecOp();
2476 
2477   // Identifier lists for polyhedral structures.
2478   ParseResult parseDimIdList(unsigned &numDims);
2479   ParseResult parseSymbolIdList(unsigned &numSymbols);
2480   ParseResult parseDimAndOptionalSymbolIdList(unsigned &numDims,
2481                                               unsigned &numSymbols);
2482   ParseResult parseIdentifierDefinition(AffineExpr idExpr);
2483 
2484   AffineExpr parseAffineExpr();
2485   AffineExpr parseParentheticalExpr();
2486   AffineExpr parseNegateExpression(AffineExpr lhs);
2487   AffineExpr parseIntegerExpr();
2488   AffineExpr parseBareIdExpr();
2489   AffineExpr parseSSAIdExpr(bool isSymbol);
2490   AffineExpr parseSymbolSSAIdExpr();
2491 
2492   AffineExpr getAffineBinaryOpExpr(AffineHighPrecOp op, AffineExpr lhs,
2493                                    AffineExpr rhs, SMLoc opLoc);
2494   AffineExpr getAffineBinaryOpExpr(AffineLowPrecOp op, AffineExpr lhs,
2495                                    AffineExpr rhs);
2496   AffineExpr parseAffineOperandExpr(AffineExpr lhs);
2497   AffineExpr parseAffineLowPrecOpExpr(AffineExpr llhs, AffineLowPrecOp llhsOp);
2498   AffineExpr parseAffineHighPrecOpExpr(AffineExpr llhs, AffineHighPrecOp llhsOp,
2499                                        SMLoc llhsOpLoc);
2500   AffineExpr parseAffineConstraint(bool *isEq);
2501 
2502 private:
2503   bool allowParsingSSAIds;
2504   function_ref<ParseResult(bool)> parseElement;
2505   unsigned numDimOperands;
2506   unsigned numSymbolOperands;
2507   SmallVector<std::pair<StringRef, AffineExpr>, 4> dimsAndSymbols;
2508 };
2509 } // end anonymous namespace
2510 
2511 /// Create an affine binary high precedence op expression (mul's, div's, mod).
2512 /// opLoc is the location of the op token to be used to report errors
2513 /// for non-conforming expressions.
2514 AffineExpr AffineParser::getAffineBinaryOpExpr(AffineHighPrecOp op,
2515                                                AffineExpr lhs, AffineExpr rhs,
2516                                                SMLoc opLoc) {
2517   // TODO: make the error location info accurate.
2518   switch (op) {
2519   case Mul:
2520     if (!lhs.isSymbolicOrConstant() && !rhs.isSymbolicOrConstant()) {
2521       emitError(opLoc, "non-affine expression: at least one of the multiply "
2522                        "operands has to be either a constant or symbolic");
2523       return nullptr;
2524     }
2525     return lhs * rhs;
2526   case FloorDiv:
2527     if (!rhs.isSymbolicOrConstant()) {
2528       emitError(opLoc, "non-affine expression: right operand of floordiv "
2529                        "has to be either a constant or symbolic");
2530       return nullptr;
2531     }
2532     return lhs.floorDiv(rhs);
2533   case CeilDiv:
2534     if (!rhs.isSymbolicOrConstant()) {
2535       emitError(opLoc, "non-affine expression: right operand of ceildiv "
2536                        "has to be either a constant or symbolic");
2537       return nullptr;
2538     }
2539     return lhs.ceilDiv(rhs);
2540   case Mod:
2541     if (!rhs.isSymbolicOrConstant()) {
2542       emitError(opLoc, "non-affine expression: right operand of mod "
2543                        "has to be either a constant or symbolic");
2544       return nullptr;
2545     }
2546     return lhs % rhs;
2547   case HNoOp:
2548     llvm_unreachable("can't create affine expression for null high prec op");
2549     return nullptr;
2550   }
2551   llvm_unreachable("Unknown AffineHighPrecOp");
2552 }
2553 
2554 /// Create an affine binary low precedence op expression (add, sub).
2555 AffineExpr AffineParser::getAffineBinaryOpExpr(AffineLowPrecOp op,
2556                                                AffineExpr lhs, AffineExpr rhs) {
2557   switch (op) {
2558   case AffineLowPrecOp::Add:
2559     return lhs + rhs;
2560   case AffineLowPrecOp::Sub:
2561     return lhs - rhs;
2562   case AffineLowPrecOp::LNoOp:
2563     llvm_unreachable("can't create affine expression for null low prec op");
2564     return nullptr;
2565   }
2566   llvm_unreachable("Unknown AffineLowPrecOp");
2567 }
2568 
2569 /// Consume this token if it is a lower precedence affine op (there are only
2570 /// two precedence levels).
2571 AffineLowPrecOp AffineParser::consumeIfLowPrecOp() {
2572   switch (getToken().getKind()) {
2573   case Token::plus:
2574     consumeToken(Token::plus);
2575     return AffineLowPrecOp::Add;
2576   case Token::minus:
2577     consumeToken(Token::minus);
2578     return AffineLowPrecOp::Sub;
2579   default:
2580     return AffineLowPrecOp::LNoOp;
2581   }
2582 }
2583 
2584 /// Consume this token if it is a higher precedence affine op (there are only
2585 /// two precedence levels)
2586 AffineHighPrecOp AffineParser::consumeIfHighPrecOp() {
2587   switch (getToken().getKind()) {
2588   case Token::star:
2589     consumeToken(Token::star);
2590     return Mul;
2591   case Token::kw_floordiv:
2592     consumeToken(Token::kw_floordiv);
2593     return FloorDiv;
2594   case Token::kw_ceildiv:
2595     consumeToken(Token::kw_ceildiv);
2596     return CeilDiv;
2597   case Token::kw_mod:
2598     consumeToken(Token::kw_mod);
2599     return Mod;
2600   default:
2601     return HNoOp;
2602   }
2603 }
2604 
2605 /// Parse a high precedence op expression list: mul, div, and mod are high
2606 /// precedence binary ops, i.e., parse a
2607 ///   expr_1 op_1 expr_2 op_2 ... expr_n
2608 /// where op_1, op_2 are all a AffineHighPrecOp (mul, div, mod).
2609 /// All affine binary ops are left associative.
2610 /// Given llhs, returns (llhs llhsOp lhs) op rhs, or (lhs op rhs) if llhs is
2611 /// null. If no rhs can be found, returns (llhs llhsOp lhs) or lhs if llhs is
2612 /// null. llhsOpLoc is the location of the llhsOp token that will be used to
2613 /// report an error for non-conforming expressions.
2614 AffineExpr AffineParser::parseAffineHighPrecOpExpr(AffineExpr llhs,
2615                                                    AffineHighPrecOp llhsOp,
2616                                                    SMLoc llhsOpLoc) {
2617   AffineExpr lhs = parseAffineOperandExpr(llhs);
2618   if (!lhs)
2619     return nullptr;
2620 
2621   // Found an LHS. Parse the remaining expression.
2622   auto opLoc = getToken().getLoc();
2623   if (AffineHighPrecOp op = consumeIfHighPrecOp()) {
2624     if (llhs) {
2625       AffineExpr expr = getAffineBinaryOpExpr(llhsOp, llhs, lhs, opLoc);
2626       if (!expr)
2627         return nullptr;
2628       return parseAffineHighPrecOpExpr(expr, op, opLoc);
2629     }
2630     // No LLHS, get RHS
2631     return parseAffineHighPrecOpExpr(lhs, op, opLoc);
2632   }
2633 
2634   // This is the last operand in this expression.
2635   if (llhs)
2636     return getAffineBinaryOpExpr(llhsOp, llhs, lhs, llhsOpLoc);
2637 
2638   // No llhs, 'lhs' itself is the expression.
2639   return lhs;
2640 }
2641 
2642 /// Parse an affine expression inside parentheses.
2643 ///
2644 ///   affine-expr ::= `(` affine-expr `)`
2645 AffineExpr AffineParser::parseParentheticalExpr() {
2646   if (parseToken(Token::l_paren, "expected '('"))
2647     return nullptr;
2648   if (getToken().is(Token::r_paren))
2649     return (emitError("no expression inside parentheses"), nullptr);
2650 
2651   auto expr = parseAffineExpr();
2652   if (!expr)
2653     return nullptr;
2654   if (parseToken(Token::r_paren, "expected ')'"))
2655     return nullptr;
2656 
2657   return expr;
2658 }
2659 
2660 /// Parse the negation expression.
2661 ///
2662 ///   affine-expr ::= `-` affine-expr
2663 AffineExpr AffineParser::parseNegateExpression(AffineExpr lhs) {
2664   if (parseToken(Token::minus, "expected '-'"))
2665     return nullptr;
2666 
2667   AffineExpr operand = parseAffineOperandExpr(lhs);
2668   // Since negation has the highest precedence of all ops (including high
2669   // precedence ops) but lower than parentheses, we are only going to use
2670   // parseAffineOperandExpr instead of parseAffineExpr here.
2671   if (!operand)
2672     // Extra error message although parseAffineOperandExpr would have
2673     // complained. Leads to a better diagnostic.
2674     return (emitError("missing operand of negation"), nullptr);
2675   return (-1) * operand;
2676 }
2677 
2678 /// Parse a bare id that may appear in an affine expression.
2679 ///
2680 ///   affine-expr ::= bare-id
2681 AffineExpr AffineParser::parseBareIdExpr() {
2682   if (getToken().isNot(Token::bare_identifier))
2683     return (emitError("expected bare identifier"), nullptr);
2684 
2685   StringRef sRef = getTokenSpelling();
2686   for (auto entry : dimsAndSymbols) {
2687     if (entry.first == sRef) {
2688       consumeToken(Token::bare_identifier);
2689       return entry.second;
2690     }
2691   }
2692 
2693   return (emitError("use of undeclared identifier"), nullptr);
2694 }
2695 
2696 /// Parse an SSA id which may appear in an affine expression.
2697 AffineExpr AffineParser::parseSSAIdExpr(bool isSymbol) {
2698   if (!allowParsingSSAIds)
2699     return (emitError("unexpected ssa identifier"), nullptr);
2700   if (getToken().isNot(Token::percent_identifier))
2701     return (emitError("expected ssa identifier"), nullptr);
2702   auto name = getTokenSpelling();
2703   // Check if we already parsed this SSA id.
2704   for (auto entry : dimsAndSymbols) {
2705     if (entry.first == name) {
2706       consumeToken(Token::percent_identifier);
2707       return entry.second;
2708     }
2709   }
2710   // Parse the SSA id and add an AffineDim/SymbolExpr to represent it.
2711   if (parseElement(isSymbol))
2712     return (emitError("failed to parse ssa identifier"), nullptr);
2713   auto idExpr = isSymbol
2714                     ? getAffineSymbolExpr(numSymbolOperands++, getContext())
2715                     : getAffineDimExpr(numDimOperands++, getContext());
2716   dimsAndSymbols.push_back({name, idExpr});
2717   return idExpr;
2718 }
2719 
2720 AffineExpr AffineParser::parseSymbolSSAIdExpr() {
2721   if (parseToken(Token::kw_symbol, "expected symbol keyword") ||
2722       parseToken(Token::l_paren, "expected '(' at start of SSA symbol"))
2723     return nullptr;
2724   AffineExpr symbolExpr = parseSSAIdExpr(/*isSymbol=*/true);
2725   if (!symbolExpr)
2726     return nullptr;
2727   if (parseToken(Token::r_paren, "expected ')' at end of SSA symbol"))
2728     return nullptr;
2729   return symbolExpr;
2730 }
2731 
2732 /// Parse a positive integral constant appearing in an affine expression.
2733 ///
2734 ///   affine-expr ::= integer-literal
2735 AffineExpr AffineParser::parseIntegerExpr() {
2736   auto val = getToken().getUInt64IntegerValue();
2737   if (!val.hasValue() || (int64_t)val.getValue() < 0)
2738     return (emitError("constant too large for index"), nullptr);
2739 
2740   consumeToken(Token::integer);
2741   return builder.getAffineConstantExpr((int64_t)val.getValue());
2742 }
2743 
2744 /// Parses an expression that can be a valid operand of an affine expression.
2745 /// lhs: if non-null, lhs is an affine expression that is the lhs of a binary
2746 /// operator, the rhs of which is being parsed. This is used to determine
2747 /// whether an error should be emitted for a missing right operand.
2748 //  Eg: for an expression without parentheses (like i + j + k + l), each
2749 //  of the four identifiers is an operand. For i + j*k + l, j*k is not an
2750 //  operand expression, it's an op expression and will be parsed via
2751 //  parseAffineHighPrecOpExpression(). However, for i + (j*k) + -l, (j*k) and
2752 //  -l are valid operands that will be parsed by this function.
2753 AffineExpr AffineParser::parseAffineOperandExpr(AffineExpr lhs) {
2754   switch (getToken().getKind()) {
2755   case Token::bare_identifier:
2756     return parseBareIdExpr();
2757   case Token::kw_symbol:
2758     return parseSymbolSSAIdExpr();
2759   case Token::percent_identifier:
2760     return parseSSAIdExpr(/*isSymbol=*/false);
2761   case Token::integer:
2762     return parseIntegerExpr();
2763   case Token::l_paren:
2764     return parseParentheticalExpr();
2765   case Token::minus:
2766     return parseNegateExpression(lhs);
2767   case Token::kw_ceildiv:
2768   case Token::kw_floordiv:
2769   case Token::kw_mod:
2770   case Token::plus:
2771   case Token::star:
2772     if (lhs)
2773       emitError("missing right operand of binary operator");
2774     else
2775       emitError("missing left operand of binary operator");
2776     return nullptr;
2777   default:
2778     if (lhs)
2779       emitError("missing right operand of binary operator");
2780     else
2781       emitError("expected affine expression");
2782     return nullptr;
2783   }
2784 }
2785 
2786 /// Parse affine expressions that are bare-id's, integer constants,
2787 /// parenthetical affine expressions, and affine op expressions that are a
2788 /// composition of those.
2789 ///
2790 /// All binary op's associate from left to right.
2791 ///
2792 /// {add, sub} have lower precedence than {mul, div, and mod}.
2793 ///
2794 /// Add, sub'are themselves at the same precedence level. Mul, floordiv,
2795 /// ceildiv, and mod are at the same higher precedence level. Negation has
2796 /// higher precedence than any binary op.
2797 ///
2798 /// llhs: the affine expression appearing on the left of the one being parsed.
2799 /// This function will return ((llhs llhsOp lhs) op rhs) if llhs is non null,
2800 /// and lhs op rhs otherwise; if there is no rhs, llhs llhsOp lhs is returned
2801 /// if llhs is non-null; otherwise lhs is returned. This is to deal with left
2802 /// associativity.
2803 ///
2804 /// Eg: when the expression is e1 + e2*e3 + e4, with e1 as llhs, this function
2805 /// will return the affine expr equivalent of (e1 + (e2*e3)) + e4, where
2806 /// (e2*e3) will be parsed using parseAffineHighPrecOpExpr().
2807 AffineExpr AffineParser::parseAffineLowPrecOpExpr(AffineExpr llhs,
2808                                                   AffineLowPrecOp llhsOp) {
2809   AffineExpr lhs;
2810   if (!(lhs = parseAffineOperandExpr(llhs)))
2811     return nullptr;
2812 
2813   // Found an LHS. Deal with the ops.
2814   if (AffineLowPrecOp lOp = consumeIfLowPrecOp()) {
2815     if (llhs) {
2816       AffineExpr sum = getAffineBinaryOpExpr(llhsOp, llhs, lhs);
2817       return parseAffineLowPrecOpExpr(sum, lOp);
2818     }
2819     // No LLHS, get RHS and form the expression.
2820     return parseAffineLowPrecOpExpr(lhs, lOp);
2821   }
2822   auto opLoc = getToken().getLoc();
2823   if (AffineHighPrecOp hOp = consumeIfHighPrecOp()) {
2824     // We have a higher precedence op here. Get the rhs operand for the llhs
2825     // through parseAffineHighPrecOpExpr.
2826     AffineExpr highRes = parseAffineHighPrecOpExpr(lhs, hOp, opLoc);
2827     if (!highRes)
2828       return nullptr;
2829 
2830     // If llhs is null, the product forms the first operand of the yet to be
2831     // found expression. If non-null, the op to associate with llhs is llhsOp.
2832     AffineExpr expr =
2833         llhs ? getAffineBinaryOpExpr(llhsOp, llhs, highRes) : highRes;
2834 
2835     // Recurse for subsequent low prec op's after the affine high prec op
2836     // expression.
2837     if (AffineLowPrecOp nextOp = consumeIfLowPrecOp())
2838       return parseAffineLowPrecOpExpr(expr, nextOp);
2839     return expr;
2840   }
2841   // Last operand in the expression list.
2842   if (llhs)
2843     return getAffineBinaryOpExpr(llhsOp, llhs, lhs);
2844   // No llhs, 'lhs' itself is the expression.
2845   return lhs;
2846 }
2847 
2848 /// Parse an affine expression.
2849 ///  affine-expr ::= `(` affine-expr `)`
2850 ///                | `-` affine-expr
2851 ///                | affine-expr `+` affine-expr
2852 ///                | affine-expr `-` affine-expr
2853 ///                | affine-expr `*` affine-expr
2854 ///                | affine-expr `floordiv` affine-expr
2855 ///                | affine-expr `ceildiv` affine-expr
2856 ///                | affine-expr `mod` affine-expr
2857 ///                | bare-id
2858 ///                | integer-literal
2859 ///
2860 /// Additional conditions are checked depending on the production. For eg.,
2861 /// one of the operands for `*` has to be either constant/symbolic; the second
2862 /// operand for floordiv, ceildiv, and mod has to be a positive integer.
2863 AffineExpr AffineParser::parseAffineExpr() {
2864   return parseAffineLowPrecOpExpr(nullptr, AffineLowPrecOp::LNoOp);
2865 }
2866 
2867 /// Parse a dim or symbol from the lists appearing before the actual
2868 /// expressions of the affine map. Update our state to store the
2869 /// dimensional/symbolic identifier.
2870 ParseResult AffineParser::parseIdentifierDefinition(AffineExpr idExpr) {
2871   if (getToken().isNot(Token::bare_identifier))
2872     return emitError("expected bare identifier");
2873 
2874   auto name = getTokenSpelling();
2875   for (auto entry : dimsAndSymbols) {
2876     if (entry.first == name)
2877       return emitError("redefinition of identifier '" + name + "'");
2878   }
2879   consumeToken(Token::bare_identifier);
2880 
2881   dimsAndSymbols.push_back({name, idExpr});
2882   return success();
2883 }
2884 
2885 /// Parse the list of dimensional identifiers to an affine map.
2886 ParseResult AffineParser::parseDimIdList(unsigned &numDims) {
2887   if (parseToken(Token::l_paren,
2888                  "expected '(' at start of dimensional identifiers list")) {
2889     return failure();
2890   }
2891 
2892   auto parseElt = [&]() -> ParseResult {
2893     auto dimension = getAffineDimExpr(numDims++, getContext());
2894     return parseIdentifierDefinition(dimension);
2895   };
2896   return parseCommaSeparatedListUntil(Token::r_paren, parseElt);
2897 }
2898 
2899 /// Parse the list of symbolic identifiers to an affine map.
2900 ParseResult AffineParser::parseSymbolIdList(unsigned &numSymbols) {
2901   consumeToken(Token::l_square);
2902   auto parseElt = [&]() -> ParseResult {
2903     auto symbol = getAffineSymbolExpr(numSymbols++, getContext());
2904     return parseIdentifierDefinition(symbol);
2905   };
2906   return parseCommaSeparatedListUntil(Token::r_square, parseElt);
2907 }
2908 
2909 /// Parse the list of symbolic identifiers to an affine map.
2910 ParseResult
2911 AffineParser::parseDimAndOptionalSymbolIdList(unsigned &numDims,
2912                                               unsigned &numSymbols) {
2913   if (parseDimIdList(numDims)) {
2914     return failure();
2915   }
2916   if (!getToken().is(Token::l_square)) {
2917     numSymbols = 0;
2918     return success();
2919   }
2920   return parseSymbolIdList(numSymbols);
2921 }
2922 
2923 /// Parses an ambiguous affine map or integer set definition inline.
2924 ParseResult AffineParser::parseAffineMapOrIntegerSetInline(AffineMap &map,
2925                                                            IntegerSet &set) {
2926   unsigned numDims = 0, numSymbols = 0;
2927 
2928   // List of dimensional and optional symbol identifiers.
2929   if (parseDimAndOptionalSymbolIdList(numDims, numSymbols)) {
2930     return failure();
2931   }
2932 
2933   // This is needed for parsing attributes as we wouldn't know whether we would
2934   // be parsing an integer set attribute or an affine map attribute.
2935   bool isArrow = getToken().is(Token::arrow);
2936   bool isColon = getToken().is(Token::colon);
2937   if (!isArrow && !isColon) {
2938     return emitError("expected '->' or ':'");
2939   } else if (isArrow) {
2940     parseToken(Token::arrow, "expected '->' or '['");
2941     map = parseAffineMapRange(numDims, numSymbols);
2942     return map ? success() : failure();
2943   } else if (parseToken(Token::colon, "expected ':' or '['")) {
2944     return failure();
2945   }
2946 
2947   if ((set = parseIntegerSetConstraints(numDims, numSymbols)))
2948     return success();
2949 
2950   return failure();
2951 }
2952 
2953 /// Parse an AffineMap where the dim and symbol identifiers are SSA ids.
2954 ParseResult
2955 AffineParser::parseAffineMapOfSSAIds(AffineMap &map,
2956                                      OpAsmParser::Delimiter delimiter) {
2957   Token::Kind rightToken;
2958   switch (delimiter) {
2959   case OpAsmParser::Delimiter::Square:
2960     if (parseToken(Token::l_square, "expected '['"))
2961       return failure();
2962     rightToken = Token::r_square;
2963     break;
2964   case OpAsmParser::Delimiter::Paren:
2965     if (parseToken(Token::l_paren, "expected '('"))
2966       return failure();
2967     rightToken = Token::r_paren;
2968     break;
2969   default:
2970     return emitError("unexpected delimiter");
2971   }
2972 
2973   SmallVector<AffineExpr, 4> exprs;
2974   auto parseElt = [&]() -> ParseResult {
2975     auto elt = parseAffineExpr();
2976     exprs.push_back(elt);
2977     return elt ? success() : failure();
2978   };
2979 
2980   // Parse a multi-dimensional affine expression (a comma-separated list of
2981   // 1-d affine expressions); the list cannot be empty. Grammar:
2982   // multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
2983   if (parseCommaSeparatedListUntil(rightToken, parseElt,
2984                                    /*allowEmptyList=*/true))
2985     return failure();
2986   // Parsed a valid affine map.
2987   if (exprs.empty())
2988     map = AffineMap::get(getContext());
2989   else
2990     map = AffineMap::get(numDimOperands, dimsAndSymbols.size() - numDimOperands,
2991                          exprs);
2992   return success();
2993 }
2994 
2995 /// Parse the range and sizes affine map definition inline.
2996 ///
2997 ///  affine-map ::= dim-and-symbol-id-lists `->` multi-dim-affine-expr
2998 ///
2999 ///  multi-dim-affine-expr ::= `(` `)`
3000 ///  multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)`
3001 AffineMap AffineParser::parseAffineMapRange(unsigned numDims,
3002                                             unsigned numSymbols) {
3003   parseToken(Token::l_paren, "expected '(' at start of affine map range");
3004 
3005   SmallVector<AffineExpr, 4> exprs;
3006   auto parseElt = [&]() -> ParseResult {
3007     auto elt = parseAffineExpr();
3008     ParseResult res = elt ? success() : failure();
3009     exprs.push_back(elt);
3010     return res;
3011   };
3012 
3013   // Parse a multi-dimensional affine expression (a comma-separated list of
3014   // 1-d affine expressions); the list cannot be empty. Grammar:
3015   // multi-dim-affine-expr ::= `(` affine-expr (`,` affine-expr)* `)
3016   if (parseCommaSeparatedListUntil(Token::r_paren, parseElt, true))
3017     return AffineMap();
3018 
3019   if (exprs.empty())
3020     return AffineMap::get(getContext());
3021 
3022   // Parsed a valid affine map.
3023   return AffineMap::get(numDims, numSymbols, exprs);
3024 }
3025 
3026 /// Parse an affine constraint.
3027 ///  affine-constraint ::= affine-expr `>=` `0`
3028 ///                      | affine-expr `==` `0`
3029 ///
3030 /// isEq is set to true if the parsed constraint is an equality, false if it
3031 /// is an inequality (greater than or equal).
3032 ///
3033 AffineExpr AffineParser::parseAffineConstraint(bool *isEq) {
3034   AffineExpr expr = parseAffineExpr();
3035   if (!expr)
3036     return nullptr;
3037 
3038   if (consumeIf(Token::greater) && consumeIf(Token::equal) &&
3039       getToken().is(Token::integer)) {
3040     auto dim = getToken().getUnsignedIntegerValue();
3041     if (dim.hasValue() && dim.getValue() == 0) {
3042       consumeToken(Token::integer);
3043       *isEq = false;
3044       return expr;
3045     }
3046     return (emitError("expected '0' after '>='"), nullptr);
3047   }
3048 
3049   if (consumeIf(Token::equal) && consumeIf(Token::equal) &&
3050       getToken().is(Token::integer)) {
3051     auto dim = getToken().getUnsignedIntegerValue();
3052     if (dim.hasValue() && dim.getValue() == 0) {
3053       consumeToken(Token::integer);
3054       *isEq = true;
3055       return expr;
3056     }
3057     return (emitError("expected '0' after '=='"), nullptr);
3058   }
3059 
3060   return (emitError("expected '== 0' or '>= 0' at end of affine constraint"),
3061           nullptr);
3062 }
3063 
3064 /// Parse the constraints that are part of an integer set definition.
3065 ///  integer-set-inline
3066 ///                ::= dim-and-symbol-id-lists `:`
3067 ///                '(' affine-constraint-conjunction? ')'
3068 ///  affine-constraint-conjunction ::= affine-constraint (`,`
3069 ///                                       affine-constraint)*
3070 ///
3071 IntegerSet AffineParser::parseIntegerSetConstraints(unsigned numDims,
3072                                                     unsigned numSymbols) {
3073   if (parseToken(Token::l_paren,
3074                  "expected '(' at start of integer set constraint list"))
3075     return IntegerSet();
3076 
3077   SmallVector<AffineExpr, 4> constraints;
3078   SmallVector<bool, 4> isEqs;
3079   auto parseElt = [&]() -> ParseResult {
3080     bool isEq;
3081     auto elt = parseAffineConstraint(&isEq);
3082     ParseResult res = elt ? success() : failure();
3083     if (elt) {
3084       constraints.push_back(elt);
3085       isEqs.push_back(isEq);
3086     }
3087     return res;
3088   };
3089 
3090   // Parse a list of affine constraints (comma-separated).
3091   if (parseCommaSeparatedListUntil(Token::r_paren, parseElt, true))
3092     return IntegerSet();
3093 
3094   // If no constraints were parsed, then treat this as a degenerate 'true' case.
3095   if (constraints.empty()) {
3096     /* 0 == 0 */
3097     auto zero = getAffineConstantExpr(0, getContext());
3098     return IntegerSet::get(numDims, numSymbols, zero, true);
3099   }
3100 
3101   // Parsed a valid integer set.
3102   return IntegerSet::get(numDims, numSymbols, constraints, isEqs);
3103 }
3104 
3105 /// Parse an ambiguous reference to either and affine map or an integer set.
3106 ParseResult Parser::parseAffineMapOrIntegerSetReference(AffineMap &map,
3107                                                         IntegerSet &set) {
3108   return AffineParser(state).parseAffineMapOrIntegerSetInline(map, set);
3109 }
3110 ParseResult Parser::parseAffineMapReference(AffineMap &map) {
3111   llvm::SMLoc curLoc = getToken().getLoc();
3112   IntegerSet set;
3113   if (parseAffineMapOrIntegerSetReference(map, set))
3114     return failure();
3115   if (set)
3116     return emitError(curLoc, "expected AffineMap, but got IntegerSet");
3117   return success();
3118 }
3119 ParseResult Parser::parseIntegerSetReference(IntegerSet &set) {
3120   llvm::SMLoc curLoc = getToken().getLoc();
3121   AffineMap map;
3122   if (parseAffineMapOrIntegerSetReference(map, set))
3123     return failure();
3124   if (map)
3125     return emitError(curLoc, "expected IntegerSet, but got AffineMap");
3126   return success();
3127 }
3128 
3129 /// Parse an AffineMap of SSA ids. The callback 'parseElement' is used to
3130 /// parse SSA value uses encountered while parsing affine expressions.
3131 ParseResult
3132 Parser::parseAffineMapOfSSAIds(AffineMap &map,
3133                                function_ref<ParseResult(bool)> parseElement,
3134                                OpAsmParser::Delimiter delimiter) {
3135   return AffineParser(state, /*allowParsingSSAIds=*/true, parseElement)
3136       .parseAffineMapOfSSAIds(map, delimiter);
3137 }
3138 
3139 //===----------------------------------------------------------------------===//
3140 // OperationParser
3141 //===----------------------------------------------------------------------===//
3142 
3143 namespace {
3144 /// This class provides support for parsing operations and regions of
3145 /// operations.
3146 class OperationParser : public Parser {
3147 public:
3148   OperationParser(ParserState &state, ModuleOp moduleOp)
3149       : Parser(state), opBuilder(moduleOp.getBodyRegion()), moduleOp(moduleOp) {
3150   }
3151 
3152   ~OperationParser();
3153 
3154   /// After parsing is finished, this function must be called to see if there
3155   /// are any remaining issues.
3156   ParseResult finalize();
3157 
3158   //===--------------------------------------------------------------------===//
3159   // SSA Value Handling
3160   //===--------------------------------------------------------------------===//
3161 
3162   /// This represents a use of an SSA value in the program.  The first two
3163   /// entries in the tuple are the name and result number of a reference.  The
3164   /// third is the location of the reference, which is used in case this ends
3165   /// up being a use of an undefined value.
3166   struct SSAUseInfo {
3167     StringRef name;  // Value name, e.g. %42 or %abc
3168     unsigned number; // Number, specified with #12
3169     SMLoc loc;       // Location of first definition or use.
3170   };
3171 
3172   /// Push a new SSA name scope to the parser.
3173   void pushSSANameScope(bool isIsolated);
3174 
3175   /// Pop the last SSA name scope from the parser.
3176   ParseResult popSSANameScope();
3177 
3178   /// Register a definition of a value with the symbol table.
3179   ParseResult addDefinition(SSAUseInfo useInfo, Value value);
3180 
3181   /// Parse an optional list of SSA uses into 'results'.
3182   ParseResult parseOptionalSSAUseList(SmallVectorImpl<SSAUseInfo> &results);
3183 
3184   /// Parse a single SSA use into 'result'.
3185   ParseResult parseSSAUse(SSAUseInfo &result);
3186 
3187   /// Given a reference to an SSA value and its type, return a reference. This
3188   /// returns null on failure.
3189   Value resolveSSAUse(SSAUseInfo useInfo, Type type);
3190 
3191   ParseResult parseSSADefOrUseAndType(
3192       const std::function<ParseResult(SSAUseInfo, Type)> &action);
3193 
3194   ParseResult parseOptionalSSAUseAndTypeList(SmallVectorImpl<Value> &results);
3195 
3196   /// Return the location of the value identified by its name and number if it
3197   /// has been already reference.
3198   Optional<SMLoc> getReferenceLoc(StringRef name, unsigned number) {
3199     auto &values = isolatedNameScopes.back().values;
3200     if (!values.count(name) || number >= values[name].size())
3201       return {};
3202     if (values[name][number].first)
3203       return values[name][number].second;
3204     return {};
3205   }
3206 
3207   //===--------------------------------------------------------------------===//
3208   // Operation Parsing
3209   //===--------------------------------------------------------------------===//
3210 
3211   /// Parse an operation instance.
3212   ParseResult parseOperation();
3213 
3214   /// Parse a single operation successor and its operand list.
3215   ParseResult parseSuccessorAndUseList(Block *&dest,
3216                                        SmallVectorImpl<Value> &operands);
3217 
3218   /// Parse a comma-separated list of operation successors in brackets.
3219   ParseResult parseSuccessors(SmallVectorImpl<Block *> &destinations,
3220                               SmallVectorImpl<SmallVector<Value, 4>> &operands);
3221 
3222   /// Parse an operation instance that is in the generic form.
3223   Operation *parseGenericOperation();
3224 
3225   /// Parse an operation instance that is in the generic form and insert it at
3226   /// the provided insertion point.
3227   Operation *parseGenericOperation(Block *insertBlock,
3228                                    Block::iterator insertPt);
3229 
3230   /// Parse an operation instance that is in the op-defined custom form.
3231   Operation *parseCustomOperation();
3232 
3233   //===--------------------------------------------------------------------===//
3234   // Region Parsing
3235   //===--------------------------------------------------------------------===//
3236 
3237   /// Parse a region into 'region' with the provided entry block arguments.
3238   /// 'isIsolatedNameScope' indicates if the naming scope of this region is
3239   /// isolated from those above.
3240   ParseResult parseRegion(Region &region,
3241                           ArrayRef<std::pair<SSAUseInfo, Type>> entryArguments,
3242                           bool isIsolatedNameScope = false);
3243 
3244   /// Parse a region body into 'region'.
3245   ParseResult parseRegionBody(Region &region);
3246 
3247   //===--------------------------------------------------------------------===//
3248   // Block Parsing
3249   //===--------------------------------------------------------------------===//
3250 
3251   /// Parse a new block into 'block'.
3252   ParseResult parseBlock(Block *&block);
3253 
3254   /// Parse a list of operations into 'block'.
3255   ParseResult parseBlockBody(Block *block);
3256 
3257   /// Parse a (possibly empty) list of block arguments.
3258   ParseResult parseOptionalBlockArgList(SmallVectorImpl<BlockArgument> &results,
3259                                         Block *owner);
3260 
3261   /// Get the block with the specified name, creating it if it doesn't
3262   /// already exist.  The location specified is the point of use, which allows
3263   /// us to diagnose references to blocks that are not defined precisely.
3264   Block *getBlockNamed(StringRef name, SMLoc loc);
3265 
3266   /// Define the block with the specified name. Returns the Block* or nullptr in
3267   /// the case of redefinition.
3268   Block *defineBlockNamed(StringRef name, SMLoc loc, Block *existing);
3269 
3270 private:
3271   /// Returns the info for a block at the current scope for the given name.
3272   std::pair<Block *, SMLoc> &getBlockInfoByName(StringRef name) {
3273     return blocksByName.back()[name];
3274   }
3275 
3276   /// Insert a new forward reference to the given block.
3277   void insertForwardRef(Block *block, SMLoc loc) {
3278     forwardRef.back().try_emplace(block, loc);
3279   }
3280 
3281   /// Erase any forward reference to the given block.
3282   bool eraseForwardRef(Block *block) { return forwardRef.back().erase(block); }
3283 
3284   /// Record that a definition was added at the current scope.
3285   void recordDefinition(StringRef def);
3286 
3287   /// Get the value entry for the given SSA name.
3288   SmallVectorImpl<std::pair<Value, SMLoc>> &getSSAValueEntry(StringRef name);
3289 
3290   /// Create a forward reference placeholder value with the given location and
3291   /// result type.
3292   Value createForwardRefPlaceholder(SMLoc loc, Type type);
3293 
3294   /// Return true if this is a forward reference.
3295   bool isForwardRefPlaceholder(Value value) {
3296     return forwardRefPlaceholders.count(value);
3297   }
3298 
3299   /// This struct represents an isolated SSA name scope. This scope may contain
3300   /// other nested non-isolated scopes. These scopes are used for operations
3301   /// that are known to be isolated to allow for reusing names within their
3302   /// regions, even if those names are used above.
3303   struct IsolatedSSANameScope {
3304     /// Record that a definition was added at the current scope.
3305     void recordDefinition(StringRef def) {
3306       definitionsPerScope.back().insert(def);
3307     }
3308 
3309     /// Push a nested name scope.
3310     void pushSSANameScope() { definitionsPerScope.push_back({}); }
3311 
3312     /// Pop a nested name scope.
3313     void popSSANameScope() {
3314       for (auto &def : definitionsPerScope.pop_back_val())
3315         values.erase(def.getKey());
3316     }
3317 
3318     /// This keeps track of all of the SSA values we are tracking for each name
3319     /// scope, indexed by their name. This has one entry per result number.
3320     llvm::StringMap<SmallVector<std::pair<Value, SMLoc>, 1>> values;
3321 
3322     /// This keeps track of all of the values defined by a specific name scope.
3323     SmallVector<llvm::StringSet<>, 2> definitionsPerScope;
3324   };
3325 
3326   /// A list of isolated name scopes.
3327   SmallVector<IsolatedSSANameScope, 2> isolatedNameScopes;
3328 
3329   /// This keeps track of the block names as well as the location of the first
3330   /// reference for each nested name scope. This is used to diagnose invalid
3331   /// block references and memorize them.
3332   SmallVector<DenseMap<StringRef, std::pair<Block *, SMLoc>>, 2> blocksByName;
3333   SmallVector<DenseMap<Block *, SMLoc>, 2> forwardRef;
3334 
3335   /// These are all of the placeholders we've made along with the location of
3336   /// their first reference, to allow checking for use of undefined values.
3337   DenseMap<Value, SMLoc> forwardRefPlaceholders;
3338 
3339   /// The builder used when creating parsed operation instances.
3340   OpBuilder opBuilder;
3341 
3342   /// The top level module operation.
3343   ModuleOp moduleOp;
3344 };
3345 } // end anonymous namespace
3346 
3347 OperationParser::~OperationParser() {
3348   for (auto &fwd : forwardRefPlaceholders) {
3349     // Drop all uses of undefined forward declared reference and destroy
3350     // defining operation.
3351     fwd.first.dropAllUses();
3352     fwd.first.getDefiningOp()->destroy();
3353   }
3354 }
3355 
3356 /// After parsing is finished, this function must be called to see if there are
3357 /// any remaining issues.
3358 ParseResult OperationParser::finalize() {
3359   // Check for any forward references that are left.  If we find any, error
3360   // out.
3361   if (!forwardRefPlaceholders.empty()) {
3362     SmallVector<std::pair<const char *, Value>, 4> errors;
3363     // Iteration over the map isn't deterministic, so sort by source location.
3364     for (auto entry : forwardRefPlaceholders)
3365       errors.push_back({entry.second.getPointer(), entry.first});
3366     llvm::array_pod_sort(errors.begin(), errors.end());
3367 
3368     for (auto entry : errors) {
3369       auto loc = SMLoc::getFromPointer(entry.first);
3370       emitError(loc, "use of undeclared SSA value name");
3371     }
3372     return failure();
3373   }
3374 
3375   return success();
3376 }
3377 
3378 //===----------------------------------------------------------------------===//
3379 // SSA Value Handling
3380 //===----------------------------------------------------------------------===//
3381 
3382 void OperationParser::pushSSANameScope(bool isIsolated) {
3383   blocksByName.push_back(DenseMap<StringRef, std::pair<Block *, SMLoc>>());
3384   forwardRef.push_back(DenseMap<Block *, SMLoc>());
3385 
3386   // Push back a new name definition scope.
3387   if (isIsolated)
3388     isolatedNameScopes.push_back({});
3389   isolatedNameScopes.back().pushSSANameScope();
3390 }
3391 
3392 ParseResult OperationParser::popSSANameScope() {
3393   auto forwardRefInCurrentScope = forwardRef.pop_back_val();
3394 
3395   // Verify that all referenced blocks were defined.
3396   if (!forwardRefInCurrentScope.empty()) {
3397     SmallVector<std::pair<const char *, Block *>, 4> errors;
3398     // Iteration over the map isn't deterministic, so sort by source location.
3399     for (auto entry : forwardRefInCurrentScope) {
3400       errors.push_back({entry.second.getPointer(), entry.first});
3401       // Add this block to the top-level region to allow for automatic cleanup.
3402       moduleOp.getOperation()->getRegion(0).push_back(entry.first);
3403     }
3404     llvm::array_pod_sort(errors.begin(), errors.end());
3405 
3406     for (auto entry : errors) {
3407       auto loc = SMLoc::getFromPointer(entry.first);
3408       emitError(loc, "reference to an undefined block");
3409     }
3410     return failure();
3411   }
3412 
3413   // Pop the next nested namescope. If there is only one internal namescope,
3414   // just pop the isolated scope.
3415   auto &currentNameScope = isolatedNameScopes.back();
3416   if (currentNameScope.definitionsPerScope.size() == 1)
3417     isolatedNameScopes.pop_back();
3418   else
3419     currentNameScope.popSSANameScope();
3420 
3421   blocksByName.pop_back();
3422   return success();
3423 }
3424 
3425 /// Register a definition of a value with the symbol table.
3426 ParseResult OperationParser::addDefinition(SSAUseInfo useInfo, Value value) {
3427   auto &entries = getSSAValueEntry(useInfo.name);
3428 
3429   // Make sure there is a slot for this value.
3430   if (entries.size() <= useInfo.number)
3431     entries.resize(useInfo.number + 1);
3432 
3433   // If we already have an entry for this, check to see if it was a definition
3434   // or a forward reference.
3435   if (auto existing = entries[useInfo.number].first) {
3436     if (!isForwardRefPlaceholder(existing)) {
3437       return emitError(useInfo.loc)
3438           .append("redefinition of SSA value '", useInfo.name, "'")
3439           .attachNote(getEncodedSourceLocation(entries[useInfo.number].second))
3440           .append("previously defined here");
3441     }
3442 
3443     // If it was a forward reference, update everything that used it to use
3444     // the actual definition instead, delete the forward ref, and remove it
3445     // from our set of forward references we track.
3446     existing.replaceAllUsesWith(value);
3447     existing.getDefiningOp()->destroy();
3448     forwardRefPlaceholders.erase(existing);
3449   }
3450 
3451   /// Record this definition for the current scope.
3452   entries[useInfo.number] = {value, useInfo.loc};
3453   recordDefinition(useInfo.name);
3454   return success();
3455 }
3456 
3457 /// Parse a (possibly empty) list of SSA operands.
3458 ///
3459 ///   ssa-use-list ::= ssa-use (`,` ssa-use)*
3460 ///   ssa-use-list-opt ::= ssa-use-list?
3461 ///
3462 ParseResult
3463 OperationParser::parseOptionalSSAUseList(SmallVectorImpl<SSAUseInfo> &results) {
3464   if (getToken().isNot(Token::percent_identifier))
3465     return success();
3466   return parseCommaSeparatedList([&]() -> ParseResult {
3467     SSAUseInfo result;
3468     if (parseSSAUse(result))
3469       return failure();
3470     results.push_back(result);
3471     return success();
3472   });
3473 }
3474 
3475 /// Parse a SSA operand for an operation.
3476 ///
3477 ///   ssa-use ::= ssa-id
3478 ///
3479 ParseResult OperationParser::parseSSAUse(SSAUseInfo &result) {
3480   result.name = getTokenSpelling();
3481   result.number = 0;
3482   result.loc = getToken().getLoc();
3483   if (parseToken(Token::percent_identifier, "expected SSA operand"))
3484     return failure();
3485 
3486   // If we have an attribute ID, it is a result number.
3487   if (getToken().is(Token::hash_identifier)) {
3488     if (auto value = getToken().getHashIdentifierNumber())
3489       result.number = value.getValue();
3490     else
3491       return emitError("invalid SSA value result number");
3492     consumeToken(Token::hash_identifier);
3493   }
3494 
3495   return success();
3496 }
3497 
3498 /// Given an unbound reference to an SSA value and its type, return the value
3499 /// it specifies.  This returns null on failure.
3500 Value OperationParser::resolveSSAUse(SSAUseInfo useInfo, Type type) {
3501   auto &entries = getSSAValueEntry(useInfo.name);
3502 
3503   // If we have already seen a value of this name, return it.
3504   if (useInfo.number < entries.size() && entries[useInfo.number].first) {
3505     auto result = entries[useInfo.number].first;
3506     // Check that the type matches the other uses.
3507     if (result.getType() == type)
3508       return result;
3509 
3510     emitError(useInfo.loc, "use of value '")
3511         .append(useInfo.name,
3512                 "' expects different type than prior uses: ", type, " vs ",
3513                 result.getType())
3514         .attachNote(getEncodedSourceLocation(entries[useInfo.number].second))
3515         .append("prior use here");
3516     return nullptr;
3517   }
3518 
3519   // Make sure we have enough slots for this.
3520   if (entries.size() <= useInfo.number)
3521     entries.resize(useInfo.number + 1);
3522 
3523   // If the value has already been defined and this is an overly large result
3524   // number, diagnose that.
3525   if (entries[0].first && !isForwardRefPlaceholder(entries[0].first))
3526     return (emitError(useInfo.loc, "reference to invalid result number"),
3527             nullptr);
3528 
3529   // Otherwise, this is a forward reference.  Create a placeholder and remember
3530   // that we did so.
3531   auto result = createForwardRefPlaceholder(useInfo.loc, type);
3532   entries[useInfo.number].first = result;
3533   entries[useInfo.number].second = useInfo.loc;
3534   return result;
3535 }
3536 
3537 /// Parse an SSA use with an associated type.
3538 ///
3539 ///   ssa-use-and-type ::= ssa-use `:` type
3540 ParseResult OperationParser::parseSSADefOrUseAndType(
3541     const std::function<ParseResult(SSAUseInfo, Type)> &action) {
3542   SSAUseInfo useInfo;
3543   if (parseSSAUse(useInfo) ||
3544       parseToken(Token::colon, "expected ':' and type for SSA operand"))
3545     return failure();
3546 
3547   auto type = parseType();
3548   if (!type)
3549     return failure();
3550 
3551   return action(useInfo, type);
3552 }
3553 
3554 /// Parse a (possibly empty) list of SSA operands, followed by a colon, then
3555 /// followed by a type list.
3556 ///
3557 ///   ssa-use-and-type-list
3558 ///     ::= ssa-use-list ':' type-list-no-parens
3559 ///
3560 ParseResult OperationParser::parseOptionalSSAUseAndTypeList(
3561     SmallVectorImpl<Value> &results) {
3562   SmallVector<SSAUseInfo, 4> valueIDs;
3563   if (parseOptionalSSAUseList(valueIDs))
3564     return failure();
3565 
3566   // If there were no operands, then there is no colon or type lists.
3567   if (valueIDs.empty())
3568     return success();
3569 
3570   SmallVector<Type, 4> types;
3571   if (parseToken(Token::colon, "expected ':' in operand list") ||
3572       parseTypeListNoParens(types))
3573     return failure();
3574 
3575   if (valueIDs.size() != types.size())
3576     return emitError("expected ")
3577            << valueIDs.size() << " types to match operand list";
3578 
3579   results.reserve(valueIDs.size());
3580   for (unsigned i = 0, e = valueIDs.size(); i != e; ++i) {
3581     if (auto value = resolveSSAUse(valueIDs[i], types[i]))
3582       results.push_back(value);
3583     else
3584       return failure();
3585   }
3586 
3587   return success();
3588 }
3589 
3590 /// Record that a definition was added at the current scope.
3591 void OperationParser::recordDefinition(StringRef def) {
3592   isolatedNameScopes.back().recordDefinition(def);
3593 }
3594 
3595 /// Get the value entry for the given SSA name.
3596 SmallVectorImpl<std::pair<Value, SMLoc>> &
3597 OperationParser::getSSAValueEntry(StringRef name) {
3598   return isolatedNameScopes.back().values[name];
3599 }
3600 
3601 /// Create and remember a new placeholder for a forward reference.
3602 Value OperationParser::createForwardRefPlaceholder(SMLoc loc, Type type) {
3603   // Forward references are always created as operations, because we just need
3604   // something with a def/use chain.
3605   //
3606   // We create these placeholders as having an empty name, which we know
3607   // cannot be created through normal user input, allowing us to distinguish
3608   // them.
3609   auto name = OperationName("placeholder", getContext());
3610   auto *op = Operation::create(
3611       getEncodedSourceLocation(loc), name, type, /*operands=*/{},
3612       /*attributes=*/llvm::None, /*successors=*/{}, /*numRegions=*/0,
3613       /*resizableOperandList=*/false);
3614   forwardRefPlaceholders[op->getResult(0)] = loc;
3615   return op->getResult(0);
3616 }
3617 
3618 //===----------------------------------------------------------------------===//
3619 // Operation Parsing
3620 //===----------------------------------------------------------------------===//
3621 
3622 /// Parse an operation.
3623 ///
3624 ///  operation         ::= op-result-list?
3625 ///                        (generic-operation | custom-operation)
3626 ///                        trailing-location?
3627 ///  generic-operation ::= string-literal `(` ssa-use-list? `)`
3628 ///                        successor-list? (`(` region-list `)`)?
3629 ///                        attribute-dict? `:` function-type
3630 ///  custom-operation  ::= bare-id custom-operation-format
3631 ///  op-result-list    ::= op-result (`,` op-result)* `=`
3632 ///  op-result         ::= ssa-id (`:` integer-literal)
3633 ///
3634 ParseResult OperationParser::parseOperation() {
3635   auto loc = getToken().getLoc();
3636   SmallVector<std::tuple<StringRef, unsigned, SMLoc>, 1> resultIDs;
3637   size_t numExpectedResults = 0;
3638   if (getToken().is(Token::percent_identifier)) {
3639     // Parse the group of result ids.
3640     auto parseNextResult = [&]() -> ParseResult {
3641       // Parse the next result id.
3642       if (!getToken().is(Token::percent_identifier))
3643         return emitError("expected valid ssa identifier");
3644 
3645       Token nameTok = getToken();
3646       consumeToken(Token::percent_identifier);
3647 
3648       // If the next token is a ':', we parse the expected result count.
3649       size_t expectedSubResults = 1;
3650       if (consumeIf(Token::colon)) {
3651         // Check that the next token is an integer.
3652         if (!getToken().is(Token::integer))
3653           return emitError("expected integer number of results");
3654 
3655         // Check that number of results is > 0.
3656         auto val = getToken().getUInt64IntegerValue();
3657         if (!val.hasValue() || val.getValue() < 1)
3658           return emitError("expected named operation to have atleast 1 result");
3659         consumeToken(Token::integer);
3660         expectedSubResults = *val;
3661       }
3662 
3663       resultIDs.emplace_back(nameTok.getSpelling(), expectedSubResults,
3664                              nameTok.getLoc());
3665       numExpectedResults += expectedSubResults;
3666       return success();
3667     };
3668     if (parseCommaSeparatedList(parseNextResult))
3669       return failure();
3670 
3671     if (parseToken(Token::equal, "expected '=' after SSA name"))
3672       return failure();
3673   }
3674 
3675   Operation *op;
3676   if (getToken().is(Token::bare_identifier) || getToken().isKeyword())
3677     op = parseCustomOperation();
3678   else if (getToken().is(Token::string))
3679     op = parseGenericOperation();
3680   else
3681     return emitError("expected operation name in quotes");
3682 
3683   // If parsing of the basic operation failed, then this whole thing fails.
3684   if (!op)
3685     return failure();
3686 
3687   // If the operation had a name, register it.
3688   if (!resultIDs.empty()) {
3689     if (op->getNumResults() == 0)
3690       return emitError(loc, "cannot name an operation with no results");
3691     if (numExpectedResults != op->getNumResults())
3692       return emitError(loc, "operation defines ")
3693              << op->getNumResults() << " results but was provided "
3694              << numExpectedResults << " to bind";
3695 
3696     // Add definitions for each of the result groups.
3697     unsigned opResI = 0;
3698     for (std::tuple<StringRef, unsigned, SMLoc> &resIt : resultIDs) {
3699       for (unsigned subRes : llvm::seq<unsigned>(0, std::get<1>(resIt))) {
3700         if (addDefinition({std::get<0>(resIt), subRes, std::get<2>(resIt)},
3701                           op->getResult(opResI++)))
3702           return failure();
3703       }
3704     }
3705   }
3706 
3707   return success();
3708 }
3709 
3710 /// Parse a single operation successor and its operand list.
3711 ///
3712 ///   successor ::= block-id branch-use-list?
3713 ///   branch-use-list ::= `(` ssa-use-list ':' type-list-no-parens `)`
3714 ///
3715 ParseResult
3716 OperationParser::parseSuccessorAndUseList(Block *&dest,
3717                                           SmallVectorImpl<Value> &operands) {
3718   // Verify branch is identifier and get the matching block.
3719   if (!getToken().is(Token::caret_identifier))
3720     return emitError("expected block name");
3721   dest = getBlockNamed(getTokenSpelling(), getToken().getLoc());
3722   consumeToken();
3723 
3724   // Handle optional arguments.
3725   if (consumeIf(Token::l_paren) &&
3726       (parseOptionalSSAUseAndTypeList(operands) ||
3727        parseToken(Token::r_paren, "expected ')' to close argument list"))) {
3728     return failure();
3729   }
3730 
3731   return success();
3732 }
3733 
3734 /// Parse a comma-separated list of operation successors in brackets.
3735 ///
3736 ///   successor-list ::= `[` successor (`,` successor )* `]`
3737 ///
3738 ParseResult OperationParser::parseSuccessors(
3739     SmallVectorImpl<Block *> &destinations,
3740     SmallVectorImpl<SmallVector<Value, 4>> &operands) {
3741   if (parseToken(Token::l_square, "expected '['"))
3742     return failure();
3743 
3744   auto parseElt = [this, &destinations, &operands]() {
3745     Block *dest;
3746     SmallVector<Value, 4> destOperands;
3747     auto res = parseSuccessorAndUseList(dest, destOperands);
3748     destinations.push_back(dest);
3749     operands.push_back(destOperands);
3750     return res;
3751   };
3752   return parseCommaSeparatedListUntil(Token::r_square, parseElt,
3753                                       /*allowEmptyList=*/false);
3754 }
3755 
3756 namespace {
3757 // RAII-style guard for cleaning up the regions in the operation state before
3758 // deleting them.  Within the parser, regions may get deleted if parsing failed,
3759 // and other errors may be present, in particular undominated uses.  This makes
3760 // sure such uses are deleted.
3761 struct CleanupOpStateRegions {
3762   ~CleanupOpStateRegions() {
3763     SmallVector<Region *, 4> regionsToClean;
3764     regionsToClean.reserve(state.regions.size());
3765     for (auto &region : state.regions)
3766       if (region)
3767         for (auto &block : *region)
3768           block.dropAllDefinedValueUses();
3769   }
3770   OperationState &state;
3771 };
3772 } // namespace
3773 
3774 Operation *OperationParser::parseGenericOperation() {
3775   // Get location information for the operation.
3776   auto srcLocation = getEncodedSourceLocation(getToken().getLoc());
3777 
3778   auto name = getToken().getStringValue();
3779   if (name.empty())
3780     return (emitError("empty operation name is invalid"), nullptr);
3781   if (name.find('\0') != StringRef::npos)
3782     return (emitError("null character not allowed in operation name"), nullptr);
3783 
3784   consumeToken(Token::string);
3785 
3786   OperationState result(srcLocation, name);
3787 
3788   // Generic operations have a resizable operation list.
3789   result.setOperandListToResizable();
3790 
3791   // Parse the operand list.
3792   SmallVector<SSAUseInfo, 8> operandInfos;
3793 
3794   if (parseToken(Token::l_paren, "expected '(' to start operand list") ||
3795       parseOptionalSSAUseList(operandInfos) ||
3796       parseToken(Token::r_paren, "expected ')' to end operand list")) {
3797     return nullptr;
3798   }
3799 
3800   // Parse the successor list but don't add successors to the result yet to
3801   // avoid messing up with the argument order.
3802   SmallVector<Block *, 2> successors;
3803   SmallVector<SmallVector<Value, 4>, 2> successorOperands;
3804   if (getToken().is(Token::l_square)) {
3805     // Check if the operation is a known terminator.
3806     const AbstractOperation *abstractOp = result.name.getAbstractOperation();
3807     if (abstractOp && !abstractOp->hasProperty(OperationProperty::Terminator))
3808       return emitError("successors in non-terminator"), nullptr;
3809     if (parseSuccessors(successors, successorOperands))
3810       return nullptr;
3811   }
3812 
3813   // Parse the region list.
3814   CleanupOpStateRegions guard{result};
3815   if (consumeIf(Token::l_paren)) {
3816     do {
3817       // Create temporary regions with the top level region as parent.
3818       result.regions.emplace_back(new Region(moduleOp));
3819       if (parseRegion(*result.regions.back(), /*entryArguments=*/{}))
3820         return nullptr;
3821     } while (consumeIf(Token::comma));
3822     if (parseToken(Token::r_paren, "expected ')' to end region list"))
3823       return nullptr;
3824   }
3825 
3826   if (getToken().is(Token::l_brace)) {
3827     if (parseAttributeDict(result.attributes))
3828       return nullptr;
3829   }
3830 
3831   if (parseToken(Token::colon, "expected ':' followed by operation type"))
3832     return nullptr;
3833 
3834   auto typeLoc = getToken().getLoc();
3835   auto type = parseType();
3836   if (!type)
3837     return nullptr;
3838   auto fnType = type.dyn_cast<FunctionType>();
3839   if (!fnType)
3840     return (emitError(typeLoc, "expected function type"), nullptr);
3841 
3842   result.addTypes(fnType.getResults());
3843 
3844   // Check that we have the right number of types for the operands.
3845   auto operandTypes = fnType.getInputs();
3846   if (operandTypes.size() != operandInfos.size()) {
3847     auto plural = "s"[operandInfos.size() == 1];
3848     return (emitError(typeLoc, "expected ")
3849                 << operandInfos.size() << " operand type" << plural
3850                 << " but had " << operandTypes.size(),
3851             nullptr);
3852   }
3853 
3854   // Resolve all of the operands.
3855   for (unsigned i = 0, e = operandInfos.size(); i != e; ++i) {
3856     result.operands.push_back(resolveSSAUse(operandInfos[i], operandTypes[i]));
3857     if (!result.operands.back())
3858       return nullptr;
3859   }
3860 
3861   // Add the successors, and their operands after the proper operands.
3862   for (auto succ : llvm::zip(successors, successorOperands)) {
3863     Block *successor = std::get<0>(succ);
3864     const SmallVector<Value, 4> &operands = std::get<1>(succ);
3865     result.addSuccessor(successor, operands);
3866   }
3867 
3868   // Parse a location if one is present.
3869   if (parseOptionalTrailingLocation(result.location))
3870     return nullptr;
3871 
3872   return opBuilder.createOperation(result);
3873 }
3874 
3875 Operation *OperationParser::parseGenericOperation(Block *insertBlock,
3876                                                   Block::iterator insertPt) {
3877   OpBuilder::InsertionGuard restoreInsertionPoint(opBuilder);
3878   opBuilder.setInsertionPoint(insertBlock, insertPt);
3879   return parseGenericOperation();
3880 }
3881 
3882 namespace {
3883 class CustomOpAsmParser : public OpAsmParser {
3884 public:
3885   CustomOpAsmParser(SMLoc nameLoc, const AbstractOperation *opDefinition,
3886                     OperationParser &parser)
3887       : nameLoc(nameLoc), opDefinition(opDefinition), parser(parser) {}
3888 
3889   /// Parse an instance of the operation described by 'opDefinition' into the
3890   /// provided operation state.
3891   ParseResult parseOperation(OperationState &opState) {
3892     if (opDefinition->parseAssembly(*this, opState))
3893       return failure();
3894     return success();
3895   }
3896 
3897   Operation *parseGenericOperation(Block *insertBlock,
3898                                    Block::iterator insertPt) final {
3899     return parser.parseGenericOperation(insertBlock, insertPt);
3900   }
3901 
3902   //===--------------------------------------------------------------------===//
3903   // Utilities
3904   //===--------------------------------------------------------------------===//
3905 
3906   /// Return if any errors were emitted during parsing.
3907   bool didEmitError() const { return emittedError; }
3908 
3909   /// Emit a diagnostic at the specified location and return failure.
3910   InFlightDiagnostic emitError(llvm::SMLoc loc, const Twine &message) override {
3911     emittedError = true;
3912     return parser.emitError(loc, "custom op '" + opDefinition->name + "' " +
3913                                      message);
3914   }
3915 
3916   llvm::SMLoc getCurrentLocation() override {
3917     return parser.getToken().getLoc();
3918   }
3919 
3920   Builder &getBuilder() const override { return parser.builder; }
3921 
3922   llvm::SMLoc getNameLoc() const override { return nameLoc; }
3923 
3924   //===--------------------------------------------------------------------===//
3925   // Token Parsing
3926   //===--------------------------------------------------------------------===//
3927 
3928   /// Parse a `->` token.
3929   ParseResult parseArrow() override {
3930     return parser.parseToken(Token::arrow, "expected '->'");
3931   }
3932 
3933   /// Parses a `->` if present.
3934   ParseResult parseOptionalArrow() override {
3935     return success(parser.consumeIf(Token::arrow));
3936   }
3937 
3938   /// Parse a `:` token.
3939   ParseResult parseColon() override {
3940     return parser.parseToken(Token::colon, "expected ':'");
3941   }
3942 
3943   /// Parse a `:` token if present.
3944   ParseResult parseOptionalColon() override {
3945     return success(parser.consumeIf(Token::colon));
3946   }
3947 
3948   /// Parse a `,` token.
3949   ParseResult parseComma() override {
3950     return parser.parseToken(Token::comma, "expected ','");
3951   }
3952 
3953   /// Parse a `,` token if present.
3954   ParseResult parseOptionalComma() override {
3955     return success(parser.consumeIf(Token::comma));
3956   }
3957 
3958   /// Parses a `...` if present.
3959   ParseResult parseOptionalEllipsis() override {
3960     return success(parser.consumeIf(Token::ellipsis));
3961   }
3962 
3963   /// Parse a `=` token.
3964   ParseResult parseEqual() override {
3965     return parser.parseToken(Token::equal, "expected '='");
3966   }
3967 
3968   /// Parse a '<' token.
3969   ParseResult parseLess() override {
3970     return parser.parseToken(Token::less, "expected '<'");
3971   }
3972 
3973   /// Parse a '>' token.
3974   ParseResult parseGreater() override {
3975     return parser.parseToken(Token::greater, "expected '>'");
3976   }
3977 
3978   /// Parse a `(` token.
3979   ParseResult parseLParen() override {
3980     return parser.parseToken(Token::l_paren, "expected '('");
3981   }
3982 
3983   /// Parses a '(' if present.
3984   ParseResult parseOptionalLParen() override {
3985     return success(parser.consumeIf(Token::l_paren));
3986   }
3987 
3988   /// Parse a `)` token.
3989   ParseResult parseRParen() override {
3990     return parser.parseToken(Token::r_paren, "expected ')'");
3991   }
3992 
3993   /// Parses a ')' if present.
3994   ParseResult parseOptionalRParen() override {
3995     return success(parser.consumeIf(Token::r_paren));
3996   }
3997 
3998   /// Parse a `[` token.
3999   ParseResult parseLSquare() override {
4000     return parser.parseToken(Token::l_square, "expected '['");
4001   }
4002 
4003   /// Parses a '[' if present.
4004   ParseResult parseOptionalLSquare() override {
4005     return success(parser.consumeIf(Token::l_square));
4006   }
4007 
4008   /// Parse a `]` token.
4009   ParseResult parseRSquare() override {
4010     return parser.parseToken(Token::r_square, "expected ']'");
4011   }
4012 
4013   /// Parses a ']' if present.
4014   ParseResult parseOptionalRSquare() override {
4015     return success(parser.consumeIf(Token::r_square));
4016   }
4017 
4018   //===--------------------------------------------------------------------===//
4019   // Attribute Parsing
4020   //===--------------------------------------------------------------------===//
4021 
4022   /// Parse an arbitrary attribute of a given type and return it in result. This
4023   /// also adds the attribute to the specified attribute list with the specified
4024   /// name.
4025   ParseResult parseAttribute(Attribute &result, Type type, StringRef attrName,
4026                              SmallVectorImpl<NamedAttribute> &attrs) override {
4027     result = parser.parseAttribute(type);
4028     if (!result)
4029       return failure();
4030 
4031     attrs.push_back(parser.builder.getNamedAttr(attrName, result));
4032     return success();
4033   }
4034 
4035   /// Parse a named dictionary into 'result' if it is present.
4036   ParseResult
4037   parseOptionalAttrDict(SmallVectorImpl<NamedAttribute> &result) override {
4038     if (parser.getToken().isNot(Token::l_brace))
4039       return success();
4040     return parser.parseAttributeDict(result);
4041   }
4042 
4043   /// Parse a named dictionary into 'result' if the `attributes` keyword is
4044   /// present.
4045   ParseResult parseOptionalAttrDictWithKeyword(
4046       SmallVectorImpl<NamedAttribute> &result) override {
4047     if (failed(parseOptionalKeyword("attributes")))
4048       return success();
4049     return parser.parseAttributeDict(result);
4050   }
4051 
4052   /// Parse an affine map instance into 'map'.
4053   ParseResult parseAffineMap(AffineMap &map) override {
4054     return parser.parseAffineMapReference(map);
4055   }
4056 
4057   /// Parse an integer set instance into 'set'.
4058   ParseResult printIntegerSet(IntegerSet &set) override {
4059     return parser.parseIntegerSetReference(set);
4060   }
4061 
4062   //===--------------------------------------------------------------------===//
4063   // Identifier Parsing
4064   //===--------------------------------------------------------------------===//
4065 
4066   /// Returns if the current token corresponds to a keyword.
4067   bool isCurrentTokenAKeyword() const {
4068     return parser.getToken().is(Token::bare_identifier) ||
4069            parser.getToken().isKeyword();
4070   }
4071 
4072   /// Parse the given keyword if present.
4073   ParseResult parseOptionalKeyword(StringRef keyword) override {
4074     // Check that the current token has the same spelling.
4075     if (!isCurrentTokenAKeyword() || parser.getTokenSpelling() != keyword)
4076       return failure();
4077     parser.consumeToken();
4078     return success();
4079   }
4080 
4081   /// Parse a keyword, if present, into 'keyword'.
4082   ParseResult parseOptionalKeyword(StringRef *keyword) override {
4083     // Check that the current token is a keyword.
4084     if (!isCurrentTokenAKeyword())
4085       return failure();
4086 
4087     *keyword = parser.getTokenSpelling();
4088     parser.consumeToken();
4089     return success();
4090   }
4091 
4092   /// Parse an optional @-identifier and store it (without the '@' symbol) in a
4093   /// string attribute named 'attrName'.
4094   ParseResult
4095   parseOptionalSymbolName(StringAttr &result, StringRef attrName,
4096                           SmallVectorImpl<NamedAttribute> &attrs) override {
4097     Token atToken = parser.getToken();
4098     if (atToken.isNot(Token::at_identifier))
4099       return failure();
4100 
4101     result = getBuilder().getStringAttr(extractSymbolReference(atToken));
4102     attrs.push_back(getBuilder().getNamedAttr(attrName, result));
4103     parser.consumeToken();
4104     return success();
4105   }
4106 
4107   //===--------------------------------------------------------------------===//
4108   // Operand Parsing
4109   //===--------------------------------------------------------------------===//
4110 
4111   /// Parse a single operand.
4112   ParseResult parseOperand(OperandType &result) override {
4113     OperationParser::SSAUseInfo useInfo;
4114     if (parser.parseSSAUse(useInfo))
4115       return failure();
4116 
4117     result = {useInfo.loc, useInfo.name, useInfo.number};
4118     return success();
4119   }
4120 
4121   /// Parse zero or more SSA comma-separated operand references with a specified
4122   /// surrounding delimiter, and an optional required operand count.
4123   ParseResult parseOperandList(SmallVectorImpl<OperandType> &result,
4124                                int requiredOperandCount = -1,
4125                                Delimiter delimiter = Delimiter::None) override {
4126     return parseOperandOrRegionArgList(result, /*isOperandList=*/true,
4127                                        requiredOperandCount, delimiter);
4128   }
4129 
4130   /// Parse zero or more SSA comma-separated operand or region arguments with
4131   ///  optional surrounding delimiter and required operand count.
4132   ParseResult
4133   parseOperandOrRegionArgList(SmallVectorImpl<OperandType> &result,
4134                               bool isOperandList, int requiredOperandCount = -1,
4135                               Delimiter delimiter = Delimiter::None) {
4136     auto startLoc = parser.getToken().getLoc();
4137 
4138     // Handle delimiters.
4139     switch (delimiter) {
4140     case Delimiter::None:
4141       // Don't check for the absence of a delimiter if the number of operands
4142       // is unknown (and hence the operand list could be empty).
4143       if (requiredOperandCount == -1)
4144         break;
4145       // Token already matches an identifier and so can't be a delimiter.
4146       if (parser.getToken().is(Token::percent_identifier))
4147         break;
4148       // Test against known delimiters.
4149       if (parser.getToken().is(Token::l_paren) ||
4150           parser.getToken().is(Token::l_square))
4151         return emitError(startLoc, "unexpected delimiter");
4152       return emitError(startLoc, "invalid operand");
4153     case Delimiter::OptionalParen:
4154       if (parser.getToken().isNot(Token::l_paren))
4155         return success();
4156       LLVM_FALLTHROUGH;
4157     case Delimiter::Paren:
4158       if (parser.parseToken(Token::l_paren, "expected '(' in operand list"))
4159         return failure();
4160       break;
4161     case Delimiter::OptionalSquare:
4162       if (parser.getToken().isNot(Token::l_square))
4163         return success();
4164       LLVM_FALLTHROUGH;
4165     case Delimiter::Square:
4166       if (parser.parseToken(Token::l_square, "expected '[' in operand list"))
4167         return failure();
4168       break;
4169     }
4170 
4171     // Check for zero operands.
4172     if (parser.getToken().is(Token::percent_identifier)) {
4173       do {
4174         OperandType operandOrArg;
4175         if (isOperandList ? parseOperand(operandOrArg)
4176                           : parseRegionArgument(operandOrArg))
4177           return failure();
4178         result.push_back(operandOrArg);
4179       } while (parser.consumeIf(Token::comma));
4180     }
4181 
4182     // Handle delimiters.   If we reach here, the optional delimiters were
4183     // present, so we need to parse their closing one.
4184     switch (delimiter) {
4185     case Delimiter::None:
4186       break;
4187     case Delimiter::OptionalParen:
4188     case Delimiter::Paren:
4189       if (parser.parseToken(Token::r_paren, "expected ')' in operand list"))
4190         return failure();
4191       break;
4192     case Delimiter::OptionalSquare:
4193     case Delimiter::Square:
4194       if (parser.parseToken(Token::r_square, "expected ']' in operand list"))
4195         return failure();
4196       break;
4197     }
4198 
4199     if (requiredOperandCount != -1 &&
4200         result.size() != static_cast<size_t>(requiredOperandCount))
4201       return emitError(startLoc, "expected ")
4202              << requiredOperandCount << " operands";
4203     return success();
4204   }
4205 
4206   /// Parse zero or more trailing SSA comma-separated trailing operand
4207   /// references with a specified surrounding delimiter, and an optional
4208   /// required operand count. A leading comma is expected before the operands.
4209   ParseResult parseTrailingOperandList(SmallVectorImpl<OperandType> &result,
4210                                        int requiredOperandCount,
4211                                        Delimiter delimiter) override {
4212     if (parser.getToken().is(Token::comma)) {
4213       parseComma();
4214       return parseOperandList(result, requiredOperandCount, delimiter);
4215     }
4216     if (requiredOperandCount != -1)
4217       return emitError(parser.getToken().getLoc(), "expected ")
4218              << requiredOperandCount << " operands";
4219     return success();
4220   }
4221 
4222   /// Resolve an operand to an SSA value, emitting an error on failure.
4223   ParseResult resolveOperand(const OperandType &operand, Type type,
4224                              SmallVectorImpl<Value> &result) override {
4225     OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number,
4226                                                operand.location};
4227     if (auto value = parser.resolveSSAUse(operandInfo, type)) {
4228       result.push_back(value);
4229       return success();
4230     }
4231     return failure();
4232   }
4233 
4234   /// Parse an AffineMap of SSA ids.
4235   ParseResult parseAffineMapOfSSAIds(SmallVectorImpl<OperandType> &operands,
4236                                      Attribute &mapAttr, StringRef attrName,
4237                                      SmallVectorImpl<NamedAttribute> &attrs,
4238                                      Delimiter delimiter) override {
4239     SmallVector<OperandType, 2> dimOperands;
4240     SmallVector<OperandType, 1> symOperands;
4241 
4242     auto parseElement = [&](bool isSymbol) -> ParseResult {
4243       OperandType operand;
4244       if (parseOperand(operand))
4245         return failure();
4246       if (isSymbol)
4247         symOperands.push_back(operand);
4248       else
4249         dimOperands.push_back(operand);
4250       return success();
4251     };
4252 
4253     AffineMap map;
4254     if (parser.parseAffineMapOfSSAIds(map, parseElement, delimiter))
4255       return failure();
4256     // Add AffineMap attribute.
4257     if (map) {
4258       mapAttr = AffineMapAttr::get(map);
4259       attrs.push_back(parser.builder.getNamedAttr(attrName, mapAttr));
4260     }
4261 
4262     // Add dim operands before symbol operands in 'operands'.
4263     operands.assign(dimOperands.begin(), dimOperands.end());
4264     operands.append(symOperands.begin(), symOperands.end());
4265     return success();
4266   }
4267 
4268   //===--------------------------------------------------------------------===//
4269   // Region Parsing
4270   //===--------------------------------------------------------------------===//
4271 
4272   /// Parse a region that takes `arguments` of `argTypes` types.  This
4273   /// effectively defines the SSA values of `arguments` and assigns their type.
4274   ParseResult parseRegion(Region &region, ArrayRef<OperandType> arguments,
4275                           ArrayRef<Type> argTypes,
4276                           bool enableNameShadowing) override {
4277     assert(arguments.size() == argTypes.size() &&
4278            "mismatching number of arguments and types");
4279 
4280     SmallVector<std::pair<OperationParser::SSAUseInfo, Type>, 2>
4281         regionArguments;
4282     for (auto pair : llvm::zip(arguments, argTypes)) {
4283       const OperandType &operand = std::get<0>(pair);
4284       Type type = std::get<1>(pair);
4285       OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number,
4286                                                  operand.location};
4287       regionArguments.emplace_back(operandInfo, type);
4288     }
4289 
4290     // Try to parse the region.
4291     assert((!enableNameShadowing ||
4292             opDefinition->hasProperty(OperationProperty::IsolatedFromAbove)) &&
4293            "name shadowing is only allowed on isolated regions");
4294     if (parser.parseRegion(region, regionArguments, enableNameShadowing))
4295       return failure();
4296     return success();
4297   }
4298 
4299   /// Parses a region if present.
4300   ParseResult parseOptionalRegion(Region &region,
4301                                   ArrayRef<OperandType> arguments,
4302                                   ArrayRef<Type> argTypes,
4303                                   bool enableNameShadowing) override {
4304     if (parser.getToken().isNot(Token::l_brace))
4305       return success();
4306     return parseRegion(region, arguments, argTypes, enableNameShadowing);
4307   }
4308 
4309   /// Parse a region argument. The type of the argument will be resolved later
4310   /// by a call to `parseRegion`.
4311   ParseResult parseRegionArgument(OperandType &argument) override {
4312     return parseOperand(argument);
4313   }
4314 
4315   /// Parse a region argument if present.
4316   ParseResult parseOptionalRegionArgument(OperandType &argument) override {
4317     if (parser.getToken().isNot(Token::percent_identifier))
4318       return success();
4319     return parseRegionArgument(argument);
4320   }
4321 
4322   ParseResult
4323   parseRegionArgumentList(SmallVectorImpl<OperandType> &result,
4324                           int requiredOperandCount = -1,
4325                           Delimiter delimiter = Delimiter::None) override {
4326     return parseOperandOrRegionArgList(result, /*isOperandList=*/false,
4327                                        requiredOperandCount, delimiter);
4328   }
4329 
4330   //===--------------------------------------------------------------------===//
4331   // Successor Parsing
4332   //===--------------------------------------------------------------------===//
4333 
4334   /// Parse a single operation successor and its operand list.
4335   ParseResult
4336   parseSuccessorAndUseList(Block *&dest,
4337                            SmallVectorImpl<Value> &operands) override {
4338     return parser.parseSuccessorAndUseList(dest, operands);
4339   }
4340 
4341   //===--------------------------------------------------------------------===//
4342   // Type Parsing
4343   //===--------------------------------------------------------------------===//
4344 
4345   /// Parse a type.
4346   ParseResult parseType(Type &result) override {
4347     return failure(!(result = parser.parseType()));
4348   }
4349 
4350   /// Parse an optional arrow followed by a type list.
4351   ParseResult
4352   parseOptionalArrowTypeList(SmallVectorImpl<Type> &result) override {
4353     if (!parser.consumeIf(Token::arrow))
4354       return success();
4355     return parser.parseFunctionResultTypes(result);
4356   }
4357 
4358   /// Parse a colon followed by a type.
4359   ParseResult parseColonType(Type &result) override {
4360     return failure(parser.parseToken(Token::colon, "expected ':'") ||
4361                    !(result = parser.parseType()));
4362   }
4363 
4364   /// Parse a colon followed by a type list, which must have at least one type.
4365   ParseResult parseColonTypeList(SmallVectorImpl<Type> &result) override {
4366     if (parser.parseToken(Token::colon, "expected ':'"))
4367       return failure();
4368     return parser.parseTypeListNoParens(result);
4369   }
4370 
4371   /// Parse an optional colon followed by a type list, which if present must
4372   /// have at least one type.
4373   ParseResult
4374   parseOptionalColonTypeList(SmallVectorImpl<Type> &result) override {
4375     if (!parser.consumeIf(Token::colon))
4376       return success();
4377     return parser.parseTypeListNoParens(result);
4378   }
4379 
4380 private:
4381   /// The source location of the operation name.
4382   SMLoc nameLoc;
4383 
4384   /// The abstract information of the operation.
4385   const AbstractOperation *opDefinition;
4386 
4387   /// The main operation parser.
4388   OperationParser &parser;
4389 
4390   /// A flag that indicates if any errors were emitted during parsing.
4391   bool emittedError = false;
4392 };
4393 } // end anonymous namespace.
4394 
4395 Operation *OperationParser::parseCustomOperation() {
4396   auto opLoc = getToken().getLoc();
4397   auto opName = getTokenSpelling();
4398 
4399   auto *opDefinition = AbstractOperation::lookup(opName, getContext());
4400   if (!opDefinition && !opName.contains('.')) {
4401     // If the operation name has no namespace prefix we treat it as a standard
4402     // operation and prefix it with "std".
4403     // TODO: Would it be better to just build a mapping of the registered
4404     // operations in the standard dialect?
4405     opDefinition =
4406         AbstractOperation::lookup(Twine("std." + opName).str(), getContext());
4407   }
4408 
4409   if (!opDefinition) {
4410     emitError(opLoc) << "custom op '" << opName << "' is unknown";
4411     return nullptr;
4412   }
4413 
4414   consumeToken();
4415 
4416   // If the custom op parser crashes, produce some indication to help
4417   // debugging.
4418   std::string opNameStr = opName.str();
4419   llvm::PrettyStackTraceFormat fmt("MLIR Parser: custom op parser '%s'",
4420                                    opNameStr.c_str());
4421 
4422   // Get location information for the operation.
4423   auto srcLocation = getEncodedSourceLocation(opLoc);
4424 
4425   // Have the op implementation take a crack and parsing this.
4426   OperationState opState(srcLocation, opDefinition->name);
4427   CleanupOpStateRegions guard{opState};
4428   CustomOpAsmParser opAsmParser(opLoc, opDefinition, *this);
4429   if (opAsmParser.parseOperation(opState))
4430     return nullptr;
4431 
4432   // If it emitted an error, we failed.
4433   if (opAsmParser.didEmitError())
4434     return nullptr;
4435 
4436   // Parse a location if one is present.
4437   if (parseOptionalTrailingLocation(opState.location))
4438     return nullptr;
4439 
4440   // Otherwise, we succeeded.  Use the state it parsed as our op information.
4441   return opBuilder.createOperation(opState);
4442 }
4443 
4444 //===----------------------------------------------------------------------===//
4445 // Region Parsing
4446 //===----------------------------------------------------------------------===//
4447 
4448 /// Region.
4449 ///
4450 ///   region ::= '{' region-body
4451 ///
4452 ParseResult OperationParser::parseRegion(
4453     Region &region,
4454     ArrayRef<std::pair<OperationParser::SSAUseInfo, Type>> entryArguments,
4455     bool isIsolatedNameScope) {
4456   // Parse the '{'.
4457   if (parseToken(Token::l_brace, "expected '{' to begin a region"))
4458     return failure();
4459 
4460   // Check for an empty region.
4461   if (entryArguments.empty() && consumeIf(Token::r_brace))
4462     return success();
4463   auto currentPt = opBuilder.saveInsertionPoint();
4464 
4465   // Push a new named value scope.
4466   pushSSANameScope(isIsolatedNameScope);
4467 
4468   // Parse the first block directly to allow for it to be unnamed.
4469   Block *block = new Block();
4470 
4471   // Add arguments to the entry block.
4472   if (!entryArguments.empty()) {
4473     for (auto &placeholderArgPair : entryArguments) {
4474       auto &argInfo = placeholderArgPair.first;
4475       // Ensure that the argument was not already defined.
4476       if (auto defLoc = getReferenceLoc(argInfo.name, argInfo.number)) {
4477         return emitError(argInfo.loc, "region entry argument '" + argInfo.name +
4478                                           "' is already in use")
4479                    .attachNote(getEncodedSourceLocation(*defLoc))
4480                << "previously referenced here";
4481       }
4482       if (addDefinition(placeholderArgPair.first,
4483                         block->addArgument(placeholderArgPair.second))) {
4484         delete block;
4485         return failure();
4486       }
4487     }
4488 
4489     // If we had named arguments, then don't allow a block name.
4490     if (getToken().is(Token::caret_identifier))
4491       return emitError("invalid block name in region with named arguments");
4492   }
4493 
4494   if (parseBlock(block)) {
4495     delete block;
4496     return failure();
4497   }
4498 
4499   // Verify that no other arguments were parsed.
4500   if (!entryArguments.empty() &&
4501       block->getNumArguments() > entryArguments.size()) {
4502     delete block;
4503     return emitError("entry block arguments were already defined");
4504   }
4505 
4506   // Parse the rest of the region.
4507   region.push_back(block);
4508   if (parseRegionBody(region))
4509     return failure();
4510 
4511   // Pop the SSA value scope for this region.
4512   if (popSSANameScope())
4513     return failure();
4514 
4515   // Reset the original insertion point.
4516   opBuilder.restoreInsertionPoint(currentPt);
4517   return success();
4518 }
4519 
4520 /// Region.
4521 ///
4522 ///   region-body ::= block* '}'
4523 ///
4524 ParseResult OperationParser::parseRegionBody(Region &region) {
4525   // Parse the list of blocks.
4526   while (!consumeIf(Token::r_brace)) {
4527     Block *newBlock = nullptr;
4528     if (parseBlock(newBlock))
4529       return failure();
4530     region.push_back(newBlock);
4531   }
4532   return success();
4533 }
4534 
4535 //===----------------------------------------------------------------------===//
4536 // Block Parsing
4537 //===----------------------------------------------------------------------===//
4538 
4539 /// Block declaration.
4540 ///
4541 ///   block ::= block-label? operation*
4542 ///   block-label    ::= block-id block-arg-list? `:`
4543 ///   block-id       ::= caret-id
4544 ///   block-arg-list ::= `(` ssa-id-and-type-list? `)`
4545 ///
4546 ParseResult OperationParser::parseBlock(Block *&block) {
4547   // The first block of a region may already exist, if it does the caret
4548   // identifier is optional.
4549   if (block && getToken().isNot(Token::caret_identifier))
4550     return parseBlockBody(block);
4551 
4552   SMLoc nameLoc = getToken().getLoc();
4553   auto name = getTokenSpelling();
4554   if (parseToken(Token::caret_identifier, "expected block name"))
4555     return failure();
4556 
4557   block = defineBlockNamed(name, nameLoc, block);
4558 
4559   // Fail if the block was already defined.
4560   if (!block)
4561     return emitError(nameLoc, "redefinition of block '") << name << "'";
4562 
4563   // If an argument list is present, parse it.
4564   if (consumeIf(Token::l_paren)) {
4565     SmallVector<BlockArgument, 8> bbArgs;
4566     if (parseOptionalBlockArgList(bbArgs, block) ||
4567         parseToken(Token::r_paren, "expected ')' to end argument list"))
4568       return failure();
4569   }
4570 
4571   if (parseToken(Token::colon, "expected ':' after block name"))
4572     return failure();
4573 
4574   return parseBlockBody(block);
4575 }
4576 
4577 ParseResult OperationParser::parseBlockBody(Block *block) {
4578   // Set the insertion point to the end of the block to parse.
4579   opBuilder.setInsertionPointToEnd(block);
4580 
4581   // Parse the list of operations that make up the body of the block.
4582   while (getToken().isNot(Token::caret_identifier, Token::r_brace))
4583     if (parseOperation())
4584       return failure();
4585 
4586   return success();
4587 }
4588 
4589 /// Get the block with the specified name, creating it if it doesn't already
4590 /// exist.  The location specified is the point of use, which allows
4591 /// us to diagnose references to blocks that are not defined precisely.
4592 Block *OperationParser::getBlockNamed(StringRef name, SMLoc loc) {
4593   auto &blockAndLoc = getBlockInfoByName(name);
4594   if (!blockAndLoc.first) {
4595     blockAndLoc = {new Block(), loc};
4596     insertForwardRef(blockAndLoc.first, loc);
4597   }
4598 
4599   return blockAndLoc.first;
4600 }
4601 
4602 /// Define the block with the specified name. Returns the Block* or nullptr in
4603 /// the case of redefinition.
4604 Block *OperationParser::defineBlockNamed(StringRef name, SMLoc loc,
4605                                          Block *existing) {
4606   auto &blockAndLoc = getBlockInfoByName(name);
4607   if (!blockAndLoc.first) {
4608     // If the caller provided a block, use it.  Otherwise create a new one.
4609     if (!existing)
4610       existing = new Block();
4611     blockAndLoc.first = existing;
4612     blockAndLoc.second = loc;
4613     return blockAndLoc.first;
4614   }
4615 
4616   // Forward declarations are removed once defined, so if we are defining a
4617   // existing block and it is not a forward declaration, then it is a
4618   // redeclaration.
4619   if (!eraseForwardRef(blockAndLoc.first))
4620     return nullptr;
4621   return blockAndLoc.first;
4622 }
4623 
4624 /// Parse a (possibly empty) list of SSA operands with types as block arguments.
4625 ///
4626 ///   ssa-id-and-type-list ::= ssa-id-and-type (`,` ssa-id-and-type)*
4627 ///
4628 ParseResult OperationParser::parseOptionalBlockArgList(
4629     SmallVectorImpl<BlockArgument> &results, Block *owner) {
4630   if (getToken().is(Token::r_brace))
4631     return success();
4632 
4633   // If the block already has arguments, then we're handling the entry block.
4634   // Parse and register the names for the arguments, but do not add them.
4635   bool definingExistingArgs = owner->getNumArguments() != 0;
4636   unsigned nextArgument = 0;
4637 
4638   return parseCommaSeparatedList([&]() -> ParseResult {
4639     return parseSSADefOrUseAndType(
4640         [&](SSAUseInfo useInfo, Type type) -> ParseResult {
4641           // If this block did not have existing arguments, define a new one.
4642           if (!definingExistingArgs)
4643             return addDefinition(useInfo, owner->addArgument(type));
4644 
4645           // Otherwise, ensure that this argument has already been created.
4646           if (nextArgument >= owner->getNumArguments())
4647             return emitError("too many arguments specified in argument list");
4648 
4649           // Finally, make sure the existing argument has the correct type.
4650           auto arg = owner->getArgument(nextArgument++);
4651           if (arg.getType() != type)
4652             return emitError("argument and block argument type mismatch");
4653           return addDefinition(useInfo, arg);
4654         });
4655   });
4656 }
4657 
4658 //===----------------------------------------------------------------------===//
4659 // Top-level entity parsing.
4660 //===----------------------------------------------------------------------===//
4661 
4662 namespace {
4663 /// This parser handles entities that are only valid at the top level of the
4664 /// file.
4665 class ModuleParser : public Parser {
4666 public:
4667   explicit ModuleParser(ParserState &state) : Parser(state) {}
4668 
4669   ParseResult parseModule(ModuleOp module);
4670 
4671 private:
4672   /// Parse an attribute alias declaration.
4673   ParseResult parseAttributeAliasDef();
4674 
4675   /// Parse an attribute alias declaration.
4676   ParseResult parseTypeAliasDef();
4677 };
4678 } // end anonymous namespace
4679 
4680 /// Parses an attribute alias declaration.
4681 ///
4682 ///   attribute-alias-def ::= '#' alias-name `=` attribute-value
4683 ///
4684 ParseResult ModuleParser::parseAttributeAliasDef() {
4685   assert(getToken().is(Token::hash_identifier));
4686   StringRef aliasName = getTokenSpelling().drop_front();
4687 
4688   // Check for redefinitions.
4689   if (getState().symbols.attributeAliasDefinitions.count(aliasName) > 0)
4690     return emitError("redefinition of attribute alias id '" + aliasName + "'");
4691 
4692   // Make sure this isn't invading the dialect attribute namespace.
4693   if (aliasName.contains('.'))
4694     return emitError("attribute names with a '.' are reserved for "
4695                      "dialect-defined names");
4696 
4697   consumeToken(Token::hash_identifier);
4698 
4699   // Parse the '='.
4700   if (parseToken(Token::equal, "expected '=' in attribute alias definition"))
4701     return failure();
4702 
4703   // Parse the attribute value.
4704   Attribute attr = parseAttribute();
4705   if (!attr)
4706     return failure();
4707 
4708   getState().symbols.attributeAliasDefinitions[aliasName] = attr;
4709   return success();
4710 }
4711 
4712 /// Parse a type alias declaration.
4713 ///
4714 ///   type-alias-def ::= '!' alias-name `=` 'type' type
4715 ///
4716 ParseResult ModuleParser::parseTypeAliasDef() {
4717   assert(getToken().is(Token::exclamation_identifier));
4718   StringRef aliasName = getTokenSpelling().drop_front();
4719 
4720   // Check for redefinitions.
4721   if (getState().symbols.typeAliasDefinitions.count(aliasName) > 0)
4722     return emitError("redefinition of type alias id '" + aliasName + "'");
4723 
4724   // Make sure this isn't invading the dialect type namespace.
4725   if (aliasName.contains('.'))
4726     return emitError("type names with a '.' are reserved for "
4727                      "dialect-defined names");
4728 
4729   consumeToken(Token::exclamation_identifier);
4730 
4731   // Parse the '=' and 'type'.
4732   if (parseToken(Token::equal, "expected '=' in type alias definition") ||
4733       parseToken(Token::kw_type, "expected 'type' in type alias definition"))
4734     return failure();
4735 
4736   // Parse the type.
4737   Type aliasedType = parseType();
4738   if (!aliasedType)
4739     return failure();
4740 
4741   // Register this alias with the parser state.
4742   getState().symbols.typeAliasDefinitions.try_emplace(aliasName, aliasedType);
4743   return success();
4744 }
4745 
4746 /// This is the top-level module parser.
4747 ParseResult ModuleParser::parseModule(ModuleOp module) {
4748   OperationParser opParser(getState(), module);
4749 
4750   // Module itself is a name scope.
4751   opParser.pushSSANameScope(/*isIsolated=*/true);
4752 
4753   while (true) {
4754     switch (getToken().getKind()) {
4755     default:
4756       // Parse a top-level operation.
4757       if (opParser.parseOperation())
4758         return failure();
4759       break;
4760 
4761     // If we got to the end of the file, then we're done.
4762     case Token::eof: {
4763       if (opParser.finalize())
4764         return failure();
4765 
4766       // Handle the case where the top level module was explicitly defined.
4767       auto &bodyBlocks = module.getBodyRegion().getBlocks();
4768       auto &operations = bodyBlocks.front().getOperations();
4769       assert(!operations.empty() && "expected a valid module terminator");
4770 
4771       // Check that the first operation is a module, and it is the only
4772       // non-terminator operation.
4773       ModuleOp nested = dyn_cast<ModuleOp>(operations.front());
4774       if (nested && std::next(operations.begin(), 2) == operations.end()) {
4775         // Merge the data of the nested module operation into 'module'.
4776         module.setLoc(nested.getLoc());
4777         module.setAttrs(nested.getOperation()->getAttrList());
4778         bodyBlocks.splice(bodyBlocks.end(), nested.getBodyRegion().getBlocks());
4779 
4780         // Erase the original module body.
4781         bodyBlocks.pop_front();
4782       }
4783 
4784       return opParser.popSSANameScope();
4785     }
4786 
4787     // If we got an error token, then the lexer already emitted an error, just
4788     // stop.  Someday we could introduce error recovery if there was demand
4789     // for it.
4790     case Token::error:
4791       return failure();
4792 
4793     // Parse an attribute alias.
4794     case Token::hash_identifier:
4795       if (parseAttributeAliasDef())
4796         return failure();
4797       break;
4798 
4799     // Parse a type alias.
4800     case Token::exclamation_identifier:
4801       if (parseTypeAliasDef())
4802         return failure();
4803       break;
4804     }
4805   }
4806 }
4807 
4808 //===----------------------------------------------------------------------===//
4809 
4810 /// This parses the file specified by the indicated SourceMgr and returns an
4811 /// MLIR module if it was valid.  If not, it emits diagnostics and returns
4812 /// null.
4813 OwningModuleRef mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
4814                                       MLIRContext *context) {
4815   auto sourceBuf = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
4816 
4817   // This is the result module we are parsing into.
4818   OwningModuleRef module(ModuleOp::create(FileLineColLoc::get(
4819       sourceBuf->getBufferIdentifier(), /*line=*/0, /*column=*/0, context)));
4820 
4821   SymbolState aliasState;
4822   ParserState state(sourceMgr, context, aliasState);
4823   if (ModuleParser(state).parseModule(*module))
4824     return nullptr;
4825 
4826   // Make sure the parse module has no other structural problems detected by
4827   // the verifier.
4828   if (failed(verify(*module)))
4829     return nullptr;
4830 
4831   return module;
4832 }
4833 
4834 /// This parses the file specified by the indicated filename and returns an
4835 /// MLIR module if it was valid.  If not, the error message is emitted through
4836 /// the error handler registered in the context, and a null pointer is returned.
4837 OwningModuleRef mlir::parseSourceFile(StringRef filename,
4838                                       MLIRContext *context) {
4839   llvm::SourceMgr sourceMgr;
4840   return parseSourceFile(filename, sourceMgr, context);
4841 }
4842 
4843 /// This parses the file specified by the indicated filename using the provided
4844 /// SourceMgr and returns an MLIR module if it was valid.  If not, the error
4845 /// message is emitted through the error handler registered in the context, and
4846 /// a null pointer is returned.
4847 OwningModuleRef mlir::parseSourceFile(StringRef filename,
4848                                       llvm::SourceMgr &sourceMgr,
4849                                       MLIRContext *context) {
4850   if (sourceMgr.getNumBuffers() != 0) {
4851     // TODO(b/136086478): Extend to support multiple buffers.
4852     emitError(mlir::UnknownLoc::get(context),
4853               "only main buffer parsed at the moment");
4854     return nullptr;
4855   }
4856   auto file_or_err = llvm::MemoryBuffer::getFileOrSTDIN(filename);
4857   if (std::error_code error = file_or_err.getError()) {
4858     emitError(mlir::UnknownLoc::get(context),
4859               "could not open input file " + filename);
4860     return nullptr;
4861   }
4862 
4863   // Load the MLIR module.
4864   sourceMgr.AddNewSourceBuffer(std::move(*file_or_err), llvm::SMLoc());
4865   return parseSourceFile(sourceMgr, context);
4866 }
4867 
4868 /// This parses the program string to a MLIR module if it was valid. If not,
4869 /// it emits diagnostics and returns null.
4870 OwningModuleRef mlir::parseSourceString(StringRef moduleStr,
4871                                         MLIRContext *context) {
4872   auto memBuffer = MemoryBuffer::getMemBuffer(moduleStr);
4873   if (!memBuffer)
4874     return nullptr;
4875 
4876   SourceMgr sourceMgr;
4877   sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
4878   return parseSourceFile(sourceMgr, context);
4879 }
4880 
4881 /// Parses a symbol, of type 'T', and returns it if parsing was successful. If
4882 /// parsing failed, nullptr is returned. The number of bytes read from the input
4883 /// string is returned in 'numRead'.
4884 template <typename T, typename ParserFn>
4885 static T parseSymbol(StringRef inputStr, MLIRContext *context, size_t &numRead,
4886                      ParserFn &&parserFn) {
4887   SymbolState aliasState;
4888   return parseSymbol<T>(
4889       inputStr, context, aliasState,
4890       [&](Parser &parser) {
4891         SourceMgrDiagnosticHandler handler(
4892             const_cast<llvm::SourceMgr &>(parser.getSourceMgr()),
4893             parser.getContext());
4894         return parserFn(parser);
4895       },
4896       &numRead);
4897 }
4898 
4899 Attribute mlir::parseAttribute(StringRef attrStr, MLIRContext *context) {
4900   size_t numRead = 0;
4901   return parseAttribute(attrStr, context, numRead);
4902 }
4903 Attribute mlir::parseAttribute(StringRef attrStr, Type type) {
4904   size_t numRead = 0;
4905   return parseAttribute(attrStr, type, numRead);
4906 }
4907 
4908 Attribute mlir::parseAttribute(StringRef attrStr, MLIRContext *context,
4909                                size_t &numRead) {
4910   return parseSymbol<Attribute>(attrStr, context, numRead, [](Parser &parser) {
4911     return parser.parseAttribute();
4912   });
4913 }
4914 Attribute mlir::parseAttribute(StringRef attrStr, Type type, size_t &numRead) {
4915   return parseSymbol<Attribute>(
4916       attrStr, type.getContext(), numRead,
4917       [type](Parser &parser) { return parser.parseAttribute(type); });
4918 }
4919 
4920 Type mlir::parseType(StringRef typeStr, MLIRContext *context) {
4921   size_t numRead = 0;
4922   return parseType(typeStr, context, numRead);
4923 }
4924 
4925 Type mlir::parseType(StringRef typeStr, MLIRContext *context, size_t &numRead) {
4926   return parseSymbol<Type>(typeStr, context, numRead,
4927                            [](Parser &parser) { return parser.parseType(); });
4928 }
4929