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