1 //===- Parser.cpp - MLIR Parser Implementation ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the parser for the MLIR textual form.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "Parser.h"
14 #include "mlir/IR/AffineMap.h"
15 #include "mlir/IR/Module.h"
16 #include "mlir/IR/Verifier.h"
17 #include "mlir/Parser.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/StringSet.h"
20 #include "llvm/ADT/bit.h"
21 #include "llvm/Support/PrettyStackTrace.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include <algorithm>
24 
25 using namespace mlir;
26 using namespace mlir::detail;
27 using llvm::MemoryBuffer;
28 using llvm::SMLoc;
29 using llvm::SourceMgr;
30 
31 //===----------------------------------------------------------------------===//
32 // Parser
33 //===----------------------------------------------------------------------===//
34 
35 /// Parse a comma separated list of elements that must have at least one entry
36 /// in it.
37 ParseResult Parser::parseCommaSeparatedList(
38     const std::function<ParseResult()> &parseElement) {
39   // Non-empty case starts with an element.
40   if (parseElement())
41     return failure();
42 
43   // Otherwise we have a list of comma separated elements.
44   while (consumeIf(Token::comma)) {
45     if (parseElement())
46       return failure();
47   }
48   return success();
49 }
50 
51 /// Parse a comma-separated list of elements, terminated with an arbitrary
52 /// token.  This allows empty lists if allowEmptyList is true.
53 ///
54 ///   abstract-list ::= rightToken                  // if allowEmptyList == true
55 ///   abstract-list ::= element (',' element)* rightToken
56 ///
57 ParseResult Parser::parseCommaSeparatedListUntil(
58     Token::Kind rightToken, const std::function<ParseResult()> &parseElement,
59     bool allowEmptyList) {
60   // Handle the empty case.
61   if (getToken().is(rightToken)) {
62     if (!allowEmptyList)
63       return emitError("expected list element");
64     consumeToken(rightToken);
65     return success();
66   }
67 
68   if (parseCommaSeparatedList(parseElement) ||
69       parseToken(rightToken, "expected ',' or '" +
70                                  Token::getTokenSpelling(rightToken) + "'"))
71     return failure();
72 
73   return success();
74 }
75 
76 InFlightDiagnostic Parser::emitError(SMLoc loc, const Twine &message) {
77   auto diag = mlir::emitError(getEncodedSourceLocation(loc), message);
78 
79   // If we hit a parse error in response to a lexer error, then the lexer
80   // already reported the error.
81   if (getToken().is(Token::error))
82     diag.abandon();
83   return diag;
84 }
85 
86 /// Consume the specified token if present and return success.  On failure,
87 /// output a diagnostic and return failure.
88 ParseResult Parser::parseToken(Token::Kind expectedToken,
89                                const Twine &message) {
90   if (consumeIf(expectedToken))
91     return success();
92   return emitError(message);
93 }
94 
95 //===----------------------------------------------------------------------===//
96 // OperationParser
97 //===----------------------------------------------------------------------===//
98 
99 namespace {
100 /// This class provides support for parsing operations and regions of
101 /// operations.
102 class OperationParser : public Parser {
103 public:
104   OperationParser(ParserState &state, ModuleOp moduleOp)
105       : Parser(state), opBuilder(moduleOp.getBodyRegion()), moduleOp(moduleOp) {
106   }
107 
108   ~OperationParser();
109 
110   /// After parsing is finished, this function must be called to see if there
111   /// are any remaining issues.
112   ParseResult finalize();
113 
114   //===--------------------------------------------------------------------===//
115   // SSA Value Handling
116   //===--------------------------------------------------------------------===//
117 
118   /// This represents a use of an SSA value in the program.  The first two
119   /// entries in the tuple are the name and result number of a reference.  The
120   /// third is the location of the reference, which is used in case this ends
121   /// up being a use of an undefined value.
122   struct SSAUseInfo {
123     StringRef name;  // Value name, e.g. %42 or %abc
124     unsigned number; // Number, specified with #12
125     SMLoc loc;       // Location of first definition or use.
126   };
127 
128   /// Push a new SSA name scope to the parser.
129   void pushSSANameScope(bool isIsolated);
130 
131   /// Pop the last SSA name scope from the parser.
132   ParseResult popSSANameScope();
133 
134   /// Register a definition of a value with the symbol table.
135   ParseResult addDefinition(SSAUseInfo useInfo, Value value);
136 
137   /// Parse an optional list of SSA uses into 'results'.
138   ParseResult parseOptionalSSAUseList(SmallVectorImpl<SSAUseInfo> &results);
139 
140   /// Parse a single SSA use into 'result'.
141   ParseResult parseSSAUse(SSAUseInfo &result);
142 
143   /// Given a reference to an SSA value and its type, return a reference. This
144   /// returns null on failure.
145   Value resolveSSAUse(SSAUseInfo useInfo, Type type);
146 
147   ParseResult parseSSADefOrUseAndType(
148       const std::function<ParseResult(SSAUseInfo, Type)> &action);
149 
150   ParseResult parseOptionalSSAUseAndTypeList(SmallVectorImpl<Value> &results);
151 
152   /// Return the location of the value identified by its name and number if it
153   /// has been already reference.
154   Optional<SMLoc> getReferenceLoc(StringRef name, unsigned number) {
155     auto &values = isolatedNameScopes.back().values;
156     if (!values.count(name) || number >= values[name].size())
157       return {};
158     if (values[name][number].first)
159       return values[name][number].second;
160     return {};
161   }
162 
163   //===--------------------------------------------------------------------===//
164   // Operation Parsing
165   //===--------------------------------------------------------------------===//
166 
167   /// Parse an operation instance.
168   ParseResult parseOperation();
169 
170   /// Parse a single operation successor.
171   ParseResult parseSuccessor(Block *&dest);
172 
173   /// Parse a comma-separated list of operation successors in brackets.
174   ParseResult parseSuccessors(SmallVectorImpl<Block *> &destinations);
175 
176   /// Parse an operation instance that is in the generic form.
177   Operation *parseGenericOperation();
178 
179   /// Parse an operation instance that is in the generic form and insert it at
180   /// the provided insertion point.
181   Operation *parseGenericOperation(Block *insertBlock,
182                                    Block::iterator insertPt);
183 
184   /// This is the structure of a result specifier in the assembly syntax,
185   /// including the name, number of results, and location.
186   typedef std::tuple<StringRef, unsigned, SMLoc> ResultRecord;
187 
188   /// Parse an operation instance that is in the op-defined custom form.
189   /// resultInfo specifies information about the "%name =" specifiers.
190   Operation *parseCustomOperation(ArrayRef<ResultRecord> resultIDs);
191 
192   //===--------------------------------------------------------------------===//
193   // Region Parsing
194   //===--------------------------------------------------------------------===//
195 
196   /// Parse a region into 'region' with the provided entry block arguments.
197   /// 'isIsolatedNameScope' indicates if the naming scope of this region is
198   /// isolated from those above.
199   ParseResult parseRegion(Region &region,
200                           ArrayRef<std::pair<SSAUseInfo, Type>> entryArguments,
201                           bool isIsolatedNameScope = false);
202 
203   /// Parse a region body into 'region'.
204   ParseResult parseRegionBody(Region &region);
205 
206   //===--------------------------------------------------------------------===//
207   // Block Parsing
208   //===--------------------------------------------------------------------===//
209 
210   /// Parse a new block into 'block'.
211   ParseResult parseBlock(Block *&block);
212 
213   /// Parse a list of operations into 'block'.
214   ParseResult parseBlockBody(Block *block);
215 
216   /// Parse a (possibly empty) list of block arguments.
217   ParseResult parseOptionalBlockArgList(SmallVectorImpl<BlockArgument> &results,
218                                         Block *owner);
219 
220   /// Get the block with the specified name, creating it if it doesn't
221   /// already exist.  The location specified is the point of use, which allows
222   /// us to diagnose references to blocks that are not defined precisely.
223   Block *getBlockNamed(StringRef name, SMLoc loc);
224 
225   /// Define the block with the specified name. Returns the Block* or nullptr in
226   /// the case of redefinition.
227   Block *defineBlockNamed(StringRef name, SMLoc loc, Block *existing);
228 
229 private:
230   /// Returns the info for a block at the current scope for the given name.
231   std::pair<Block *, SMLoc> &getBlockInfoByName(StringRef name) {
232     return blocksByName.back()[name];
233   }
234 
235   /// Insert a new forward reference to the given block.
236   void insertForwardRef(Block *block, SMLoc loc) {
237     forwardRef.back().try_emplace(block, loc);
238   }
239 
240   /// Erase any forward reference to the given block.
241   bool eraseForwardRef(Block *block) { return forwardRef.back().erase(block); }
242 
243   /// Record that a definition was added at the current scope.
244   void recordDefinition(StringRef def);
245 
246   /// Get the value entry for the given SSA name.
247   SmallVectorImpl<std::pair<Value, SMLoc>> &getSSAValueEntry(StringRef name);
248 
249   /// Create a forward reference placeholder value with the given location and
250   /// result type.
251   Value createForwardRefPlaceholder(SMLoc loc, Type type);
252 
253   /// Return true if this is a forward reference.
254   bool isForwardRefPlaceholder(Value value) {
255     return forwardRefPlaceholders.count(value);
256   }
257 
258   /// This struct represents an isolated SSA name scope. This scope may contain
259   /// other nested non-isolated scopes. These scopes are used for operations
260   /// that are known to be isolated to allow for reusing names within their
261   /// regions, even if those names are used above.
262   struct IsolatedSSANameScope {
263     /// Record that a definition was added at the current scope.
264     void recordDefinition(StringRef def) {
265       definitionsPerScope.back().insert(def);
266     }
267 
268     /// Push a nested name scope.
269     void pushSSANameScope() { definitionsPerScope.push_back({}); }
270 
271     /// Pop a nested name scope.
272     void popSSANameScope() {
273       for (auto &def : definitionsPerScope.pop_back_val())
274         values.erase(def.getKey());
275     }
276 
277     /// This keeps track of all of the SSA values we are tracking for each name
278     /// scope, indexed by their name. This has one entry per result number.
279     llvm::StringMap<SmallVector<std::pair<Value, SMLoc>, 1>> values;
280 
281     /// This keeps track of all of the values defined by a specific name scope.
282     SmallVector<llvm::StringSet<>, 2> definitionsPerScope;
283   };
284 
285   /// A list of isolated name scopes.
286   SmallVector<IsolatedSSANameScope, 2> isolatedNameScopes;
287 
288   /// This keeps track of the block names as well as the location of the first
289   /// reference for each nested name scope. This is used to diagnose invalid
290   /// block references and memorize them.
291   SmallVector<DenseMap<StringRef, std::pair<Block *, SMLoc>>, 2> blocksByName;
292   SmallVector<DenseMap<Block *, SMLoc>, 2> forwardRef;
293 
294   /// These are all of the placeholders we've made along with the location of
295   /// their first reference, to allow checking for use of undefined values.
296   DenseMap<Value, SMLoc> forwardRefPlaceholders;
297 
298   /// The builder used when creating parsed operation instances.
299   OpBuilder opBuilder;
300 
301   /// The top level module operation.
302   ModuleOp moduleOp;
303 };
304 } // end anonymous namespace
305 
306 OperationParser::~OperationParser() {
307   for (auto &fwd : forwardRefPlaceholders) {
308     // Drop all uses of undefined forward declared reference and destroy
309     // defining operation.
310     fwd.first.dropAllUses();
311     fwd.first.getDefiningOp()->destroy();
312   }
313 }
314 
315 /// After parsing is finished, this function must be called to see if there are
316 /// any remaining issues.
317 ParseResult OperationParser::finalize() {
318   // Check for any forward references that are left.  If we find any, error
319   // out.
320   if (!forwardRefPlaceholders.empty()) {
321     SmallVector<const char *, 4> errors;
322     // Iteration over the map isn't deterministic, so sort by source location.
323     for (auto entry : forwardRefPlaceholders)
324       errors.push_back(entry.second.getPointer());
325     llvm::array_pod_sort(errors.begin(), errors.end());
326 
327     for (auto entry : errors) {
328       auto loc = SMLoc::getFromPointer(entry);
329       emitError(loc, "use of undeclared SSA value name");
330     }
331     return failure();
332   }
333 
334   return success();
335 }
336 
337 //===----------------------------------------------------------------------===//
338 // SSA Value Handling
339 //===----------------------------------------------------------------------===//
340 
341 void OperationParser::pushSSANameScope(bool isIsolated) {
342   blocksByName.push_back(DenseMap<StringRef, std::pair<Block *, SMLoc>>());
343   forwardRef.push_back(DenseMap<Block *, SMLoc>());
344 
345   // Push back a new name definition scope.
346   if (isIsolated)
347     isolatedNameScopes.push_back({});
348   isolatedNameScopes.back().pushSSANameScope();
349 }
350 
351 ParseResult OperationParser::popSSANameScope() {
352   auto forwardRefInCurrentScope = forwardRef.pop_back_val();
353 
354   // Verify that all referenced blocks were defined.
355   if (!forwardRefInCurrentScope.empty()) {
356     SmallVector<std::pair<const char *, Block *>, 4> errors;
357     // Iteration over the map isn't deterministic, so sort by source location.
358     for (auto entry : forwardRefInCurrentScope) {
359       errors.push_back({entry.second.getPointer(), entry.first});
360       // Add this block to the top-level region to allow for automatic cleanup.
361       moduleOp.getOperation()->getRegion(0).push_back(entry.first);
362     }
363     llvm::array_pod_sort(errors.begin(), errors.end());
364 
365     for (auto entry : errors) {
366       auto loc = SMLoc::getFromPointer(entry.first);
367       emitError(loc, "reference to an undefined block");
368     }
369     return failure();
370   }
371 
372   // Pop the next nested namescope. If there is only one internal namescope,
373   // just pop the isolated scope.
374   auto &currentNameScope = isolatedNameScopes.back();
375   if (currentNameScope.definitionsPerScope.size() == 1)
376     isolatedNameScopes.pop_back();
377   else
378     currentNameScope.popSSANameScope();
379 
380   blocksByName.pop_back();
381   return success();
382 }
383 
384 /// Register a definition of a value with the symbol table.
385 ParseResult OperationParser::addDefinition(SSAUseInfo useInfo, Value value) {
386   auto &entries = getSSAValueEntry(useInfo.name);
387 
388   // Make sure there is a slot for this value.
389   if (entries.size() <= useInfo.number)
390     entries.resize(useInfo.number + 1);
391 
392   // If we already have an entry for this, check to see if it was a definition
393   // or a forward reference.
394   if (auto existing = entries[useInfo.number].first) {
395     if (!isForwardRefPlaceholder(existing)) {
396       return emitError(useInfo.loc)
397           .append("redefinition of SSA value '", useInfo.name, "'")
398           .attachNote(getEncodedSourceLocation(entries[useInfo.number].second))
399           .append("previously defined here");
400     }
401 
402     if (existing.getType() != value.getType()) {
403       return emitError(useInfo.loc)
404           .append("definition of SSA value '", useInfo.name, "#",
405                   useInfo.number, "' has type ", value.getType())
406           .attachNote(getEncodedSourceLocation(entries[useInfo.number].second))
407           .append("previously used here with type ", existing.getType());
408     }
409 
410     // If it was a forward reference, update everything that used it to use
411     // the actual definition instead, delete the forward ref, and remove it
412     // from our set of forward references we track.
413     existing.replaceAllUsesWith(value);
414     existing.getDefiningOp()->destroy();
415     forwardRefPlaceholders.erase(existing);
416   }
417 
418   /// Record this definition for the current scope.
419   entries[useInfo.number] = {value, useInfo.loc};
420   recordDefinition(useInfo.name);
421   return success();
422 }
423 
424 /// Parse a (possibly empty) list of SSA operands.
425 ///
426 ///   ssa-use-list ::= ssa-use (`,` ssa-use)*
427 ///   ssa-use-list-opt ::= ssa-use-list?
428 ///
429 ParseResult
430 OperationParser::parseOptionalSSAUseList(SmallVectorImpl<SSAUseInfo> &results) {
431   if (getToken().isNot(Token::percent_identifier))
432     return success();
433   return parseCommaSeparatedList([&]() -> ParseResult {
434     SSAUseInfo result;
435     if (parseSSAUse(result))
436       return failure();
437     results.push_back(result);
438     return success();
439   });
440 }
441 
442 /// Parse a SSA operand for an operation.
443 ///
444 ///   ssa-use ::= ssa-id
445 ///
446 ParseResult OperationParser::parseSSAUse(SSAUseInfo &result) {
447   result.name = getTokenSpelling();
448   result.number = 0;
449   result.loc = getToken().getLoc();
450   if (parseToken(Token::percent_identifier, "expected SSA operand"))
451     return failure();
452 
453   // If we have an attribute ID, it is a result number.
454   if (getToken().is(Token::hash_identifier)) {
455     if (auto value = getToken().getHashIdentifierNumber())
456       result.number = value.getValue();
457     else
458       return emitError("invalid SSA value result number");
459     consumeToken(Token::hash_identifier);
460   }
461 
462   return success();
463 }
464 
465 /// Given an unbound reference to an SSA value and its type, return the value
466 /// it specifies.  This returns null on failure.
467 Value OperationParser::resolveSSAUse(SSAUseInfo useInfo, Type type) {
468   auto &entries = getSSAValueEntry(useInfo.name);
469 
470   // If we have already seen a value of this name, return it.
471   if (useInfo.number < entries.size() && entries[useInfo.number].first) {
472     auto result = entries[useInfo.number].first;
473     // Check that the type matches the other uses.
474     if (result.getType() == type)
475       return result;
476 
477     emitError(useInfo.loc, "use of value '")
478         .append(useInfo.name,
479                 "' expects different type than prior uses: ", type, " vs ",
480                 result.getType())
481         .attachNote(getEncodedSourceLocation(entries[useInfo.number].second))
482         .append("prior use here");
483     return nullptr;
484   }
485 
486   // Make sure we have enough slots for this.
487   if (entries.size() <= useInfo.number)
488     entries.resize(useInfo.number + 1);
489 
490   // If the value has already been defined and this is an overly large result
491   // number, diagnose that.
492   if (entries[0].first && !isForwardRefPlaceholder(entries[0].first))
493     return (emitError(useInfo.loc, "reference to invalid result number"),
494             nullptr);
495 
496   // Otherwise, this is a forward reference.  Create a placeholder and remember
497   // that we did so.
498   auto result = createForwardRefPlaceholder(useInfo.loc, type);
499   entries[useInfo.number].first = result;
500   entries[useInfo.number].second = useInfo.loc;
501   return result;
502 }
503 
504 /// Parse an SSA use with an associated type.
505 ///
506 ///   ssa-use-and-type ::= ssa-use `:` type
507 ParseResult OperationParser::parseSSADefOrUseAndType(
508     const std::function<ParseResult(SSAUseInfo, Type)> &action) {
509   SSAUseInfo useInfo;
510   if (parseSSAUse(useInfo) ||
511       parseToken(Token::colon, "expected ':' and type for SSA operand"))
512     return failure();
513 
514   auto type = parseType();
515   if (!type)
516     return failure();
517 
518   return action(useInfo, type);
519 }
520 
521 /// Parse a (possibly empty) list of SSA operands, followed by a colon, then
522 /// followed by a type list.
523 ///
524 ///   ssa-use-and-type-list
525 ///     ::= ssa-use-list ':' type-list-no-parens
526 ///
527 ParseResult OperationParser::parseOptionalSSAUseAndTypeList(
528     SmallVectorImpl<Value> &results) {
529   SmallVector<SSAUseInfo, 4> valueIDs;
530   if (parseOptionalSSAUseList(valueIDs))
531     return failure();
532 
533   // If there were no operands, then there is no colon or type lists.
534   if (valueIDs.empty())
535     return success();
536 
537   SmallVector<Type, 4> types;
538   if (parseToken(Token::colon, "expected ':' in operand list") ||
539       parseTypeListNoParens(types))
540     return failure();
541 
542   if (valueIDs.size() != types.size())
543     return emitError("expected ")
544            << valueIDs.size() << " types to match operand list";
545 
546   results.reserve(valueIDs.size());
547   for (unsigned i = 0, e = valueIDs.size(); i != e; ++i) {
548     if (auto value = resolveSSAUse(valueIDs[i], types[i]))
549       results.push_back(value);
550     else
551       return failure();
552   }
553 
554   return success();
555 }
556 
557 /// Record that a definition was added at the current scope.
558 void OperationParser::recordDefinition(StringRef def) {
559   isolatedNameScopes.back().recordDefinition(def);
560 }
561 
562 /// Get the value entry for the given SSA name.
563 SmallVectorImpl<std::pair<Value, SMLoc>> &
564 OperationParser::getSSAValueEntry(StringRef name) {
565   return isolatedNameScopes.back().values[name];
566 }
567 
568 /// Create and remember a new placeholder for a forward reference.
569 Value OperationParser::createForwardRefPlaceholder(SMLoc loc, Type type) {
570   // Forward references are always created as operations, because we just need
571   // something with a def/use chain.
572   //
573   // We create these placeholders as having an empty name, which we know
574   // cannot be created through normal user input, allowing us to distinguish
575   // them.
576   auto name = OperationName("placeholder", getContext());
577   auto *op = Operation::create(
578       getEncodedSourceLocation(loc), name, type, /*operands=*/{},
579       /*attributes=*/llvm::None, /*successors=*/{}, /*numRegions=*/0);
580   forwardRefPlaceholders[op->getResult(0)] = loc;
581   return op->getResult(0);
582 }
583 
584 //===----------------------------------------------------------------------===//
585 // Operation Parsing
586 //===----------------------------------------------------------------------===//
587 
588 /// Parse an operation.
589 ///
590 ///  operation         ::= op-result-list?
591 ///                        (generic-operation | custom-operation)
592 ///                        trailing-location?
593 ///  generic-operation ::= string-literal `(` ssa-use-list? `)`
594 ///                        successor-list? (`(` region-list `)`)?
595 ///                        attribute-dict? `:` function-type
596 ///  custom-operation  ::= bare-id custom-operation-format
597 ///  op-result-list    ::= op-result (`,` op-result)* `=`
598 ///  op-result         ::= ssa-id (`:` integer-literal)
599 ///
600 ParseResult OperationParser::parseOperation() {
601   auto loc = getToken().getLoc();
602   SmallVector<ResultRecord, 1> resultIDs;
603   size_t numExpectedResults = 0;
604   if (getToken().is(Token::percent_identifier)) {
605     // Parse the group of result ids.
606     auto parseNextResult = [&]() -> ParseResult {
607       // Parse the next result id.
608       if (!getToken().is(Token::percent_identifier))
609         return emitError("expected valid ssa identifier");
610 
611       Token nameTok = getToken();
612       consumeToken(Token::percent_identifier);
613 
614       // If the next token is a ':', we parse the expected result count.
615       size_t expectedSubResults = 1;
616       if (consumeIf(Token::colon)) {
617         // Check that the next token is an integer.
618         if (!getToken().is(Token::integer))
619           return emitError("expected integer number of results");
620 
621         // Check that number of results is > 0.
622         auto val = getToken().getUInt64IntegerValue();
623         if (!val.hasValue() || val.getValue() < 1)
624           return emitError("expected named operation to have atleast 1 result");
625         consumeToken(Token::integer);
626         expectedSubResults = *val;
627       }
628 
629       resultIDs.emplace_back(nameTok.getSpelling(), expectedSubResults,
630                              nameTok.getLoc());
631       numExpectedResults += expectedSubResults;
632       return success();
633     };
634     if (parseCommaSeparatedList(parseNextResult))
635       return failure();
636 
637     if (parseToken(Token::equal, "expected '=' after SSA name"))
638       return failure();
639   }
640 
641   Operation *op;
642   if (getToken().is(Token::bare_identifier) || getToken().isKeyword())
643     op = parseCustomOperation(resultIDs);
644   else if (getToken().is(Token::string))
645     op = parseGenericOperation();
646   else
647     return emitError("expected operation name in quotes");
648 
649   // If parsing of the basic operation failed, then this whole thing fails.
650   if (!op)
651     return failure();
652 
653   // If the operation had a name, register it.
654   if (!resultIDs.empty()) {
655     if (op->getNumResults() == 0)
656       return emitError(loc, "cannot name an operation with no results");
657     if (numExpectedResults != op->getNumResults())
658       return emitError(loc, "operation defines ")
659              << op->getNumResults() << " results but was provided "
660              << numExpectedResults << " to bind";
661 
662     // Add definitions for each of the result groups.
663     unsigned opResI = 0;
664     for (ResultRecord &resIt : resultIDs) {
665       for (unsigned subRes : llvm::seq<unsigned>(0, std::get<1>(resIt))) {
666         if (addDefinition({std::get<0>(resIt), subRes, std::get<2>(resIt)},
667                           op->getResult(opResI++)))
668           return failure();
669       }
670     }
671   }
672 
673   return success();
674 }
675 
676 /// Parse a single operation successor.
677 ///
678 ///   successor ::= block-id
679 ///
680 ParseResult OperationParser::parseSuccessor(Block *&dest) {
681   // Verify branch is identifier and get the matching block.
682   if (!getToken().is(Token::caret_identifier))
683     return emitError("expected block name");
684   dest = getBlockNamed(getTokenSpelling(), getToken().getLoc());
685   consumeToken();
686   return success();
687 }
688 
689 /// Parse a comma-separated list of operation successors in brackets.
690 ///
691 ///   successor-list ::= `[` successor (`,` successor )* `]`
692 ///
693 ParseResult
694 OperationParser::parseSuccessors(SmallVectorImpl<Block *> &destinations) {
695   if (parseToken(Token::l_square, "expected '['"))
696     return failure();
697 
698   auto parseElt = [this, &destinations] {
699     Block *dest;
700     ParseResult res = parseSuccessor(dest);
701     destinations.push_back(dest);
702     return res;
703   };
704   return parseCommaSeparatedListUntil(Token::r_square, parseElt,
705                                       /*allowEmptyList=*/false);
706 }
707 
708 namespace {
709 // RAII-style guard for cleaning up the regions in the operation state before
710 // deleting them.  Within the parser, regions may get deleted if parsing failed,
711 // and other errors may be present, in particular undominated uses.  This makes
712 // sure such uses are deleted.
713 struct CleanupOpStateRegions {
714   ~CleanupOpStateRegions() {
715     SmallVector<Region *, 4> regionsToClean;
716     regionsToClean.reserve(state.regions.size());
717     for (auto &region : state.regions)
718       if (region)
719         for (auto &block : *region)
720           block.dropAllDefinedValueUses();
721   }
722   OperationState &state;
723 };
724 } // namespace
725 
726 Operation *OperationParser::parseGenericOperation() {
727   // Get location information for the operation.
728   auto srcLocation = getEncodedSourceLocation(getToken().getLoc());
729 
730   auto name = getToken().getStringValue();
731   if (name.empty())
732     return (emitError("empty operation name is invalid"), nullptr);
733   if (name.find('\0') != StringRef::npos)
734     return (emitError("null character not allowed in operation name"), nullptr);
735 
736   consumeToken(Token::string);
737 
738   OperationState result(srcLocation, name);
739 
740   // Parse the operand list.
741   SmallVector<SSAUseInfo, 8> operandInfos;
742   if (parseToken(Token::l_paren, "expected '(' to start operand list") ||
743       parseOptionalSSAUseList(operandInfos) ||
744       parseToken(Token::r_paren, "expected ')' to end operand list")) {
745     return nullptr;
746   }
747 
748   // Parse the successor list.
749   if (getToken().is(Token::l_square)) {
750     // Check if the operation is a known terminator.
751     const AbstractOperation *abstractOp = result.name.getAbstractOperation();
752     if (abstractOp && !abstractOp->hasProperty(OperationProperty::Terminator))
753       return emitError("successors in non-terminator"), nullptr;
754 
755     SmallVector<Block *, 2> successors;
756     if (parseSuccessors(successors))
757       return nullptr;
758     result.addSuccessors(successors);
759   }
760 
761   // Parse the region list.
762   CleanupOpStateRegions guard{result};
763   if (consumeIf(Token::l_paren)) {
764     do {
765       // Create temporary regions with the top level region as parent.
766       result.regions.emplace_back(new Region(moduleOp));
767       if (parseRegion(*result.regions.back(), /*entryArguments=*/{}))
768         return nullptr;
769     } while (consumeIf(Token::comma));
770     if (parseToken(Token::r_paren, "expected ')' to end region list"))
771       return nullptr;
772   }
773 
774   if (getToken().is(Token::l_brace)) {
775     if (parseAttributeDict(result.attributes))
776       return nullptr;
777   }
778 
779   if (parseToken(Token::colon, "expected ':' followed by operation type"))
780     return nullptr;
781 
782   auto typeLoc = getToken().getLoc();
783   auto type = parseType();
784   if (!type)
785     return nullptr;
786   auto fnType = type.dyn_cast<FunctionType>();
787   if (!fnType)
788     return (emitError(typeLoc, "expected function type"), nullptr);
789 
790   result.addTypes(fnType.getResults());
791 
792   // Check that we have the right number of types for the operands.
793   auto operandTypes = fnType.getInputs();
794   if (operandTypes.size() != operandInfos.size()) {
795     auto plural = "s"[operandInfos.size() == 1];
796     return (emitError(typeLoc, "expected ")
797                 << operandInfos.size() << " operand type" << plural
798                 << " but had " << operandTypes.size(),
799             nullptr);
800   }
801 
802   // Resolve all of the operands.
803   for (unsigned i = 0, e = operandInfos.size(); i != e; ++i) {
804     result.operands.push_back(resolveSSAUse(operandInfos[i], operandTypes[i]));
805     if (!result.operands.back())
806       return nullptr;
807   }
808 
809   // Parse a location if one is present.
810   if (parseOptionalTrailingLocation(result.location))
811     return nullptr;
812 
813   return opBuilder.createOperation(result);
814 }
815 
816 Operation *OperationParser::parseGenericOperation(Block *insertBlock,
817                                                   Block::iterator insertPt) {
818   OpBuilder::InsertionGuard restoreInsertionPoint(opBuilder);
819   opBuilder.setInsertionPoint(insertBlock, insertPt);
820   return parseGenericOperation();
821 }
822 
823 namespace {
824 class CustomOpAsmParser : public OpAsmParser {
825 public:
826   CustomOpAsmParser(SMLoc nameLoc,
827                     ArrayRef<OperationParser::ResultRecord> resultIDs,
828                     const AbstractOperation *opDefinition,
829                     OperationParser &parser)
830       : nameLoc(nameLoc), resultIDs(resultIDs), opDefinition(opDefinition),
831         parser(parser) {}
832 
833   /// Parse an instance of the operation described by 'opDefinition' into the
834   /// provided operation state.
835   ParseResult parseOperation(OperationState &opState) {
836     if (opDefinition->parseAssembly(*this, opState))
837       return failure();
838     return success();
839   }
840 
841   Operation *parseGenericOperation(Block *insertBlock,
842                                    Block::iterator insertPt) final {
843     return parser.parseGenericOperation(insertBlock, insertPt);
844   }
845 
846   //===--------------------------------------------------------------------===//
847   // Utilities
848   //===--------------------------------------------------------------------===//
849 
850   /// Return if any errors were emitted during parsing.
851   bool didEmitError() const { return emittedError; }
852 
853   /// Emit a diagnostic at the specified location and return failure.
854   InFlightDiagnostic emitError(llvm::SMLoc loc, const Twine &message) override {
855     emittedError = true;
856     return parser.emitError(loc, "custom op '" + opDefinition->name + "' " +
857                                      message);
858   }
859 
860   llvm::SMLoc getCurrentLocation() override {
861     return parser.getToken().getLoc();
862   }
863 
864   Builder &getBuilder() const override { return parser.builder; }
865 
866   /// Return the name of the specified result in the specified syntax, as well
867   /// as the subelement in the name.  For example, in this operation:
868   ///
869   ///  %x, %y:2, %z = foo.op
870   ///
871   ///    getResultName(0) == {"x", 0 }
872   ///    getResultName(1) == {"y", 0 }
873   ///    getResultName(2) == {"y", 1 }
874   ///    getResultName(3) == {"z", 0 }
875   std::pair<StringRef, unsigned>
876   getResultName(unsigned resultNo) const override {
877     // Scan for the resultID that contains this result number.
878     for (unsigned nameID = 0, e = resultIDs.size(); nameID != e; ++nameID) {
879       const auto &entry = resultIDs[nameID];
880       if (resultNo < std::get<1>(entry)) {
881         // Don't pass on the leading %.
882         StringRef name = std::get<0>(entry).drop_front();
883         return {name, resultNo};
884       }
885       resultNo -= std::get<1>(entry);
886     }
887 
888     // Invalid result number.
889     return {"", ~0U};
890   }
891 
892   /// Return the number of declared SSA results.  This returns 4 for the foo.op
893   /// example in the comment for getResultName.
894   size_t getNumResults() const override {
895     size_t count = 0;
896     for (auto &entry : resultIDs)
897       count += std::get<1>(entry);
898     return count;
899   }
900 
901   llvm::SMLoc getNameLoc() const override { return nameLoc; }
902 
903   //===--------------------------------------------------------------------===//
904   // Token Parsing
905   //===--------------------------------------------------------------------===//
906 
907   /// Parse a `->` token.
908   ParseResult parseArrow() override {
909     return parser.parseToken(Token::arrow, "expected '->'");
910   }
911 
912   /// Parses a `->` if present.
913   ParseResult parseOptionalArrow() override {
914     return success(parser.consumeIf(Token::arrow));
915   }
916 
917   /// Parse a `:` token.
918   ParseResult parseColon() override {
919     return parser.parseToken(Token::colon, "expected ':'");
920   }
921 
922   /// Parse a `:` token if present.
923   ParseResult parseOptionalColon() override {
924     return success(parser.consumeIf(Token::colon));
925   }
926 
927   /// Parse a `,` token.
928   ParseResult parseComma() override {
929     return parser.parseToken(Token::comma, "expected ','");
930   }
931 
932   /// Parse a `,` token if present.
933   ParseResult parseOptionalComma() override {
934     return success(parser.consumeIf(Token::comma));
935   }
936 
937   /// Parses a `...` if present.
938   ParseResult parseOptionalEllipsis() override {
939     return success(parser.consumeIf(Token::ellipsis));
940   }
941 
942   /// Parse a `=` token.
943   ParseResult parseEqual() override {
944     return parser.parseToken(Token::equal, "expected '='");
945   }
946 
947   /// Parse a '<' token.
948   ParseResult parseLess() override {
949     return parser.parseToken(Token::less, "expected '<'");
950   }
951 
952   /// Parse a '>' token.
953   ParseResult parseGreater() override {
954     return parser.parseToken(Token::greater, "expected '>'");
955   }
956 
957   /// Parse a `(` token.
958   ParseResult parseLParen() override {
959     return parser.parseToken(Token::l_paren, "expected '('");
960   }
961 
962   /// Parses a '(' if present.
963   ParseResult parseOptionalLParen() override {
964     return success(parser.consumeIf(Token::l_paren));
965   }
966 
967   /// Parse a `)` token.
968   ParseResult parseRParen() override {
969     return parser.parseToken(Token::r_paren, "expected ')'");
970   }
971 
972   /// Parses a ')' if present.
973   ParseResult parseOptionalRParen() override {
974     return success(parser.consumeIf(Token::r_paren));
975   }
976 
977   /// Parse a `[` token.
978   ParseResult parseLSquare() override {
979     return parser.parseToken(Token::l_square, "expected '['");
980   }
981 
982   /// Parses a '[' if present.
983   ParseResult parseOptionalLSquare() override {
984     return success(parser.consumeIf(Token::l_square));
985   }
986 
987   /// Parse a `]` token.
988   ParseResult parseRSquare() override {
989     return parser.parseToken(Token::r_square, "expected ']'");
990   }
991 
992   /// Parses a ']' if present.
993   ParseResult parseOptionalRSquare() override {
994     return success(parser.consumeIf(Token::r_square));
995   }
996 
997   //===--------------------------------------------------------------------===//
998   // Attribute Parsing
999   //===--------------------------------------------------------------------===//
1000 
1001   /// Parse an arbitrary attribute of a given type and return it in result. This
1002   /// also adds the attribute to the specified attribute list with the specified
1003   /// name.
1004   ParseResult parseAttribute(Attribute &result, Type type, StringRef attrName,
1005                              NamedAttrList &attrs) override {
1006     result = parser.parseAttribute(type);
1007     if (!result)
1008       return failure();
1009 
1010     attrs.push_back(parser.builder.getNamedAttr(attrName, result));
1011     return success();
1012   }
1013 
1014   /// Parse a named dictionary into 'result' if it is present.
1015   ParseResult parseOptionalAttrDict(NamedAttrList &result) override {
1016     if (parser.getToken().isNot(Token::l_brace))
1017       return success();
1018     return parser.parseAttributeDict(result);
1019   }
1020 
1021   /// Parse a named dictionary into 'result' if the `attributes` keyword is
1022   /// present.
1023   ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result) override {
1024     if (failed(parseOptionalKeyword("attributes")))
1025       return success();
1026     return parser.parseAttributeDict(result);
1027   }
1028 
1029   /// Parse an affine map instance into 'map'.
1030   ParseResult parseAffineMap(AffineMap &map) override {
1031     return parser.parseAffineMapReference(map);
1032   }
1033 
1034   /// Parse an integer set instance into 'set'.
1035   ParseResult printIntegerSet(IntegerSet &set) override {
1036     return parser.parseIntegerSetReference(set);
1037   }
1038 
1039   //===--------------------------------------------------------------------===//
1040   // Identifier Parsing
1041   //===--------------------------------------------------------------------===//
1042 
1043   /// Returns if the current token corresponds to a keyword.
1044   bool isCurrentTokenAKeyword() const {
1045     return parser.getToken().is(Token::bare_identifier) ||
1046            parser.getToken().isKeyword();
1047   }
1048 
1049   /// Parse the given keyword if present.
1050   ParseResult parseOptionalKeyword(StringRef keyword) override {
1051     // Check that the current token has the same spelling.
1052     if (!isCurrentTokenAKeyword() || parser.getTokenSpelling() != keyword)
1053       return failure();
1054     parser.consumeToken();
1055     return success();
1056   }
1057 
1058   /// Parse a keyword, if present, into 'keyword'.
1059   ParseResult parseOptionalKeyword(StringRef *keyword) override {
1060     // Check that the current token is a keyword.
1061     if (!isCurrentTokenAKeyword())
1062       return failure();
1063 
1064     *keyword = parser.getTokenSpelling();
1065     parser.consumeToken();
1066     return success();
1067   }
1068 
1069   /// Parse an optional @-identifier and store it (without the '@' symbol) in a
1070   /// string attribute named 'attrName'.
1071   ParseResult parseOptionalSymbolName(StringAttr &result, StringRef attrName,
1072                                       NamedAttrList &attrs) override {
1073     Token atToken = parser.getToken();
1074     if (atToken.isNot(Token::at_identifier))
1075       return failure();
1076 
1077     result = getBuilder().getStringAttr(atToken.getSymbolReference());
1078     attrs.push_back(getBuilder().getNamedAttr(attrName, result));
1079     parser.consumeToken();
1080     return success();
1081   }
1082 
1083   //===--------------------------------------------------------------------===//
1084   // Operand Parsing
1085   //===--------------------------------------------------------------------===//
1086 
1087   /// Parse a single operand.
1088   ParseResult parseOperand(OperandType &result) override {
1089     OperationParser::SSAUseInfo useInfo;
1090     if (parser.parseSSAUse(useInfo))
1091       return failure();
1092 
1093     result = {useInfo.loc, useInfo.name, useInfo.number};
1094     return success();
1095   }
1096 
1097   /// Parse a single operand if present.
1098   OptionalParseResult parseOptionalOperand(OperandType &result) override {
1099     if (parser.getToken().is(Token::percent_identifier))
1100       return parseOperand(result);
1101     return llvm::None;
1102   }
1103 
1104   /// Parse zero or more SSA comma-separated operand references with a specified
1105   /// surrounding delimiter, and an optional required operand count.
1106   ParseResult parseOperandList(SmallVectorImpl<OperandType> &result,
1107                                int requiredOperandCount = -1,
1108                                Delimiter delimiter = Delimiter::None) override {
1109     return parseOperandOrRegionArgList(result, /*isOperandList=*/true,
1110                                        requiredOperandCount, delimiter);
1111   }
1112 
1113   /// Parse zero or more SSA comma-separated operand or region arguments with
1114   ///  optional surrounding delimiter and required operand count.
1115   ParseResult
1116   parseOperandOrRegionArgList(SmallVectorImpl<OperandType> &result,
1117                               bool isOperandList, int requiredOperandCount = -1,
1118                               Delimiter delimiter = Delimiter::None) {
1119     auto startLoc = parser.getToken().getLoc();
1120 
1121     // Handle delimiters.
1122     switch (delimiter) {
1123     case Delimiter::None:
1124       // Don't check for the absence of a delimiter if the number of operands
1125       // is unknown (and hence the operand list could be empty).
1126       if (requiredOperandCount == -1)
1127         break;
1128       // Token already matches an identifier and so can't be a delimiter.
1129       if (parser.getToken().is(Token::percent_identifier))
1130         break;
1131       // Test against known delimiters.
1132       if (parser.getToken().is(Token::l_paren) ||
1133           parser.getToken().is(Token::l_square))
1134         return emitError(startLoc, "unexpected delimiter");
1135       return emitError(startLoc, "invalid operand");
1136     case Delimiter::OptionalParen:
1137       if (parser.getToken().isNot(Token::l_paren))
1138         return success();
1139       LLVM_FALLTHROUGH;
1140     case Delimiter::Paren:
1141       if (parser.parseToken(Token::l_paren, "expected '(' in operand list"))
1142         return failure();
1143       break;
1144     case Delimiter::OptionalSquare:
1145       if (parser.getToken().isNot(Token::l_square))
1146         return success();
1147       LLVM_FALLTHROUGH;
1148     case Delimiter::Square:
1149       if (parser.parseToken(Token::l_square, "expected '[' in operand list"))
1150         return failure();
1151       break;
1152     }
1153 
1154     // Check for zero operands.
1155     if (parser.getToken().is(Token::percent_identifier)) {
1156       do {
1157         OperandType operandOrArg;
1158         if (isOperandList ? parseOperand(operandOrArg)
1159                           : parseRegionArgument(operandOrArg))
1160           return failure();
1161         result.push_back(operandOrArg);
1162       } while (parser.consumeIf(Token::comma));
1163     }
1164 
1165     // Handle delimiters.   If we reach here, the optional delimiters were
1166     // present, so we need to parse their closing one.
1167     switch (delimiter) {
1168     case Delimiter::None:
1169       break;
1170     case Delimiter::OptionalParen:
1171     case Delimiter::Paren:
1172       if (parser.parseToken(Token::r_paren, "expected ')' in operand list"))
1173         return failure();
1174       break;
1175     case Delimiter::OptionalSquare:
1176     case Delimiter::Square:
1177       if (parser.parseToken(Token::r_square, "expected ']' in operand list"))
1178         return failure();
1179       break;
1180     }
1181 
1182     if (requiredOperandCount != -1 &&
1183         result.size() != static_cast<size_t>(requiredOperandCount))
1184       return emitError(startLoc, "expected ")
1185              << requiredOperandCount << " operands";
1186     return success();
1187   }
1188 
1189   /// Parse zero or more trailing SSA comma-separated trailing operand
1190   /// references with a specified surrounding delimiter, and an optional
1191   /// required operand count. A leading comma is expected before the operands.
1192   ParseResult parseTrailingOperandList(SmallVectorImpl<OperandType> &result,
1193                                        int requiredOperandCount,
1194                                        Delimiter delimiter) override {
1195     if (parser.getToken().is(Token::comma)) {
1196       parseComma();
1197       return parseOperandList(result, requiredOperandCount, delimiter);
1198     }
1199     if (requiredOperandCount != -1)
1200       return emitError(parser.getToken().getLoc(), "expected ")
1201              << requiredOperandCount << " operands";
1202     return success();
1203   }
1204 
1205   /// Resolve an operand to an SSA value, emitting an error on failure.
1206   ParseResult resolveOperand(const OperandType &operand, Type type,
1207                              SmallVectorImpl<Value> &result) override {
1208     OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number,
1209                                                operand.location};
1210     if (auto value = parser.resolveSSAUse(operandInfo, type)) {
1211       result.push_back(value);
1212       return success();
1213     }
1214     return failure();
1215   }
1216 
1217   /// Parse an AffineMap of SSA ids.
1218   ParseResult parseAffineMapOfSSAIds(SmallVectorImpl<OperandType> &operands,
1219                                      Attribute &mapAttr, StringRef attrName,
1220                                      NamedAttrList &attrs,
1221                                      Delimiter delimiter) override {
1222     SmallVector<OperandType, 2> dimOperands;
1223     SmallVector<OperandType, 1> symOperands;
1224 
1225     auto parseElement = [&](bool isSymbol) -> ParseResult {
1226       OperandType operand;
1227       if (parseOperand(operand))
1228         return failure();
1229       if (isSymbol)
1230         symOperands.push_back(operand);
1231       else
1232         dimOperands.push_back(operand);
1233       return success();
1234     };
1235 
1236     AffineMap map;
1237     if (parser.parseAffineMapOfSSAIds(map, parseElement, delimiter))
1238       return failure();
1239     // Add AffineMap attribute.
1240     if (map) {
1241       mapAttr = AffineMapAttr::get(map);
1242       attrs.push_back(parser.builder.getNamedAttr(attrName, mapAttr));
1243     }
1244 
1245     // Add dim operands before symbol operands in 'operands'.
1246     operands.assign(dimOperands.begin(), dimOperands.end());
1247     operands.append(symOperands.begin(), symOperands.end());
1248     return success();
1249   }
1250 
1251   //===--------------------------------------------------------------------===//
1252   // Region Parsing
1253   //===--------------------------------------------------------------------===//
1254 
1255   /// Parse a region that takes `arguments` of `argTypes` types.  This
1256   /// effectively defines the SSA values of `arguments` and assigns their type.
1257   ParseResult parseRegion(Region &region, ArrayRef<OperandType> arguments,
1258                           ArrayRef<Type> argTypes,
1259                           bool enableNameShadowing) override {
1260     assert(arguments.size() == argTypes.size() &&
1261            "mismatching number of arguments and types");
1262 
1263     SmallVector<std::pair<OperationParser::SSAUseInfo, Type>, 2>
1264         regionArguments;
1265     for (auto pair : llvm::zip(arguments, argTypes)) {
1266       const OperandType &operand = std::get<0>(pair);
1267       Type type = std::get<1>(pair);
1268       OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number,
1269                                                  operand.location};
1270       regionArguments.emplace_back(operandInfo, type);
1271     }
1272 
1273     // Try to parse the region.
1274     assert((!enableNameShadowing ||
1275             opDefinition->hasProperty(OperationProperty::IsolatedFromAbove)) &&
1276            "name shadowing is only allowed on isolated regions");
1277     if (parser.parseRegion(region, regionArguments, enableNameShadowing))
1278       return failure();
1279     return success();
1280   }
1281 
1282   /// Parses a region if present.
1283   ParseResult parseOptionalRegion(Region &region,
1284                                   ArrayRef<OperandType> arguments,
1285                                   ArrayRef<Type> argTypes,
1286                                   bool enableNameShadowing) override {
1287     if (parser.getToken().isNot(Token::l_brace))
1288       return success();
1289     return parseRegion(region, arguments, argTypes, enableNameShadowing);
1290   }
1291 
1292   /// Parse a region argument. The type of the argument will be resolved later
1293   /// by a call to `parseRegion`.
1294   ParseResult parseRegionArgument(OperandType &argument) override {
1295     return parseOperand(argument);
1296   }
1297 
1298   /// Parse a region argument if present.
1299   ParseResult parseOptionalRegionArgument(OperandType &argument) override {
1300     if (parser.getToken().isNot(Token::percent_identifier))
1301       return success();
1302     return parseRegionArgument(argument);
1303   }
1304 
1305   ParseResult
1306   parseRegionArgumentList(SmallVectorImpl<OperandType> &result,
1307                           int requiredOperandCount = -1,
1308                           Delimiter delimiter = Delimiter::None) override {
1309     return parseOperandOrRegionArgList(result, /*isOperandList=*/false,
1310                                        requiredOperandCount, delimiter);
1311   }
1312 
1313   //===--------------------------------------------------------------------===//
1314   // Successor Parsing
1315   //===--------------------------------------------------------------------===//
1316 
1317   /// Parse a single operation successor.
1318   ParseResult parseSuccessor(Block *&dest) override {
1319     return parser.parseSuccessor(dest);
1320   }
1321 
1322   /// Parse an optional operation successor and its operand list.
1323   OptionalParseResult parseOptionalSuccessor(Block *&dest) override {
1324     if (parser.getToken().isNot(Token::caret_identifier))
1325       return llvm::None;
1326     return parseSuccessor(dest);
1327   }
1328 
1329   /// Parse a single operation successor and its operand list.
1330   ParseResult
1331   parseSuccessorAndUseList(Block *&dest,
1332                            SmallVectorImpl<Value> &operands) override {
1333     if (parseSuccessor(dest))
1334       return failure();
1335 
1336     // Handle optional arguments.
1337     if (succeeded(parseOptionalLParen()) &&
1338         (parser.parseOptionalSSAUseAndTypeList(operands) || parseRParen())) {
1339       return failure();
1340     }
1341     return success();
1342   }
1343 
1344   //===--------------------------------------------------------------------===//
1345   // Type Parsing
1346   //===--------------------------------------------------------------------===//
1347 
1348   /// Parse a type.
1349   ParseResult parseType(Type &result) override {
1350     return failure(!(result = parser.parseType()));
1351   }
1352 
1353   /// Parse an optional type.
1354   OptionalParseResult parseOptionalType(Type &result) override {
1355     return parser.parseOptionalType(result);
1356   }
1357 
1358   /// Parse an arrow followed by a type list.
1359   ParseResult parseArrowTypeList(SmallVectorImpl<Type> &result) override {
1360     if (parseArrow() || parser.parseFunctionResultTypes(result))
1361       return failure();
1362     return success();
1363   }
1364 
1365   /// Parse an optional arrow followed by a type list.
1366   ParseResult
1367   parseOptionalArrowTypeList(SmallVectorImpl<Type> &result) override {
1368     if (!parser.consumeIf(Token::arrow))
1369       return success();
1370     return parser.parseFunctionResultTypes(result);
1371   }
1372 
1373   /// Parse a colon followed by a type.
1374   ParseResult parseColonType(Type &result) override {
1375     return failure(parser.parseToken(Token::colon, "expected ':'") ||
1376                    !(result = parser.parseType()));
1377   }
1378 
1379   /// Parse a colon followed by a type list, which must have at least one type.
1380   ParseResult parseColonTypeList(SmallVectorImpl<Type> &result) override {
1381     if (parser.parseToken(Token::colon, "expected ':'"))
1382       return failure();
1383     return parser.parseTypeListNoParens(result);
1384   }
1385 
1386   /// Parse an optional colon followed by a type list, which if present must
1387   /// have at least one type.
1388   ParseResult
1389   parseOptionalColonTypeList(SmallVectorImpl<Type> &result) override {
1390     if (!parser.consumeIf(Token::colon))
1391       return success();
1392     return parser.parseTypeListNoParens(result);
1393   }
1394 
1395   /// Parse a list of assignments of the form
1396   /// (%x1 = %y1 : type1, %x2 = %y2 : type2, ...).
1397   /// The list must contain at least one entry
1398   ParseResult parseAssignmentList(SmallVectorImpl<OperandType> &lhs,
1399                                   SmallVectorImpl<OperandType> &rhs) override {
1400     auto parseElt = [&]() -> ParseResult {
1401       OperandType regionArg, operand;
1402       if (parseRegionArgument(regionArg) || parseEqual() ||
1403           parseOperand(operand))
1404         return failure();
1405       lhs.push_back(regionArg);
1406       rhs.push_back(operand);
1407       return success();
1408     };
1409     if (parseLParen())
1410       return failure();
1411     return parser.parseCommaSeparatedListUntil(Token::r_paren, parseElt);
1412   }
1413 
1414 private:
1415   /// The source location of the operation name.
1416   SMLoc nameLoc;
1417 
1418   /// Information about the result name specifiers.
1419   ArrayRef<OperationParser::ResultRecord> resultIDs;
1420 
1421   /// The abstract information of the operation.
1422   const AbstractOperation *opDefinition;
1423 
1424   /// The main operation parser.
1425   OperationParser &parser;
1426 
1427   /// A flag that indicates if any errors were emitted during parsing.
1428   bool emittedError = false;
1429 };
1430 } // end anonymous namespace.
1431 
1432 Operation *
1433 OperationParser::parseCustomOperation(ArrayRef<ResultRecord> resultIDs) {
1434   auto opLoc = getToken().getLoc();
1435   auto opName = getTokenSpelling();
1436 
1437   auto *opDefinition = AbstractOperation::lookup(opName, getContext());
1438   if (!opDefinition && !opName.contains('.')) {
1439     // If the operation name has no namespace prefix we treat it as a standard
1440     // operation and prefix it with "std".
1441     // TODO: Would it be better to just build a mapping of the registered
1442     // operations in the standard dialect?
1443     opDefinition =
1444         AbstractOperation::lookup(Twine("std." + opName).str(), getContext());
1445   }
1446 
1447   if (!opDefinition) {
1448     emitError(opLoc) << "custom op '" << opName << "' is unknown";
1449     return nullptr;
1450   }
1451 
1452   consumeToken();
1453 
1454   // If the custom op parser crashes, produce some indication to help
1455   // debugging.
1456   std::string opNameStr = opName.str();
1457   llvm::PrettyStackTraceFormat fmt("MLIR Parser: custom op parser '%s'",
1458                                    opNameStr.c_str());
1459 
1460   // Get location information for the operation.
1461   auto srcLocation = getEncodedSourceLocation(opLoc);
1462 
1463   // Have the op implementation take a crack and parsing this.
1464   OperationState opState(srcLocation, opDefinition->name);
1465   CleanupOpStateRegions guard{opState};
1466   CustomOpAsmParser opAsmParser(opLoc, resultIDs, opDefinition, *this);
1467   if (opAsmParser.parseOperation(opState))
1468     return nullptr;
1469 
1470   // If it emitted an error, we failed.
1471   if (opAsmParser.didEmitError())
1472     return nullptr;
1473 
1474   // Parse a location if one is present.
1475   if (parseOptionalTrailingLocation(opState.location))
1476     return nullptr;
1477 
1478   // Otherwise, we succeeded.  Use the state it parsed as our op information.
1479   return opBuilder.createOperation(opState);
1480 }
1481 
1482 //===----------------------------------------------------------------------===//
1483 // Region Parsing
1484 //===----------------------------------------------------------------------===//
1485 
1486 /// Region.
1487 ///
1488 ///   region ::= '{' region-body
1489 ///
1490 ParseResult OperationParser::parseRegion(
1491     Region &region,
1492     ArrayRef<std::pair<OperationParser::SSAUseInfo, Type>> entryArguments,
1493     bool isIsolatedNameScope) {
1494   // Parse the '{'.
1495   if (parseToken(Token::l_brace, "expected '{' to begin a region"))
1496     return failure();
1497 
1498   // Check for an empty region.
1499   if (entryArguments.empty() && consumeIf(Token::r_brace))
1500     return success();
1501   auto currentPt = opBuilder.saveInsertionPoint();
1502 
1503   // Push a new named value scope.
1504   pushSSANameScope(isIsolatedNameScope);
1505 
1506   // Parse the first block directly to allow for it to be unnamed.
1507   Block *block = new Block();
1508 
1509   // Add arguments to the entry block.
1510   if (!entryArguments.empty()) {
1511     for (auto &placeholderArgPair : entryArguments) {
1512       auto &argInfo = placeholderArgPair.first;
1513       // Ensure that the argument was not already defined.
1514       if (auto defLoc = getReferenceLoc(argInfo.name, argInfo.number)) {
1515         return emitError(argInfo.loc, "region entry argument '" + argInfo.name +
1516                                           "' is already in use")
1517                    .attachNote(getEncodedSourceLocation(*defLoc))
1518                << "previously referenced here";
1519       }
1520       if (addDefinition(placeholderArgPair.first,
1521                         block->addArgument(placeholderArgPair.second))) {
1522         delete block;
1523         return failure();
1524       }
1525     }
1526 
1527     // If we had named arguments, then don't allow a block name.
1528     if (getToken().is(Token::caret_identifier))
1529       return emitError("invalid block name in region with named arguments");
1530   }
1531 
1532   if (parseBlock(block)) {
1533     delete block;
1534     return failure();
1535   }
1536 
1537   // Verify that no other arguments were parsed.
1538   if (!entryArguments.empty() &&
1539       block->getNumArguments() > entryArguments.size()) {
1540     delete block;
1541     return emitError("entry block arguments were already defined");
1542   }
1543 
1544   // Parse the rest of the region.
1545   region.push_back(block);
1546   if (parseRegionBody(region))
1547     return failure();
1548 
1549   // Pop the SSA value scope for this region.
1550   if (popSSANameScope())
1551     return failure();
1552 
1553   // Reset the original insertion point.
1554   opBuilder.restoreInsertionPoint(currentPt);
1555   return success();
1556 }
1557 
1558 /// Region.
1559 ///
1560 ///   region-body ::= block* '}'
1561 ///
1562 ParseResult OperationParser::parseRegionBody(Region &region) {
1563   // Parse the list of blocks.
1564   while (!consumeIf(Token::r_brace)) {
1565     Block *newBlock = nullptr;
1566     if (parseBlock(newBlock))
1567       return failure();
1568     region.push_back(newBlock);
1569   }
1570   return success();
1571 }
1572 
1573 //===----------------------------------------------------------------------===//
1574 // Block Parsing
1575 //===----------------------------------------------------------------------===//
1576 
1577 /// Block declaration.
1578 ///
1579 ///   block ::= block-label? operation*
1580 ///   block-label    ::= block-id block-arg-list? `:`
1581 ///   block-id       ::= caret-id
1582 ///   block-arg-list ::= `(` ssa-id-and-type-list? `)`
1583 ///
1584 ParseResult OperationParser::parseBlock(Block *&block) {
1585   // The first block of a region may already exist, if it does the caret
1586   // identifier is optional.
1587   if (block && getToken().isNot(Token::caret_identifier))
1588     return parseBlockBody(block);
1589 
1590   SMLoc nameLoc = getToken().getLoc();
1591   auto name = getTokenSpelling();
1592   if (parseToken(Token::caret_identifier, "expected block name"))
1593     return failure();
1594 
1595   block = defineBlockNamed(name, nameLoc, block);
1596 
1597   // Fail if the block was already defined.
1598   if (!block)
1599     return emitError(nameLoc, "redefinition of block '") << name << "'";
1600 
1601   // If an argument list is present, parse it.
1602   if (consumeIf(Token::l_paren)) {
1603     SmallVector<BlockArgument, 8> bbArgs;
1604     if (parseOptionalBlockArgList(bbArgs, block) ||
1605         parseToken(Token::r_paren, "expected ')' to end argument list"))
1606       return failure();
1607   }
1608 
1609   if (parseToken(Token::colon, "expected ':' after block name"))
1610     return failure();
1611 
1612   return parseBlockBody(block);
1613 }
1614 
1615 ParseResult OperationParser::parseBlockBody(Block *block) {
1616   // Set the insertion point to the end of the block to parse.
1617   opBuilder.setInsertionPointToEnd(block);
1618 
1619   // Parse the list of operations that make up the body of the block.
1620   while (getToken().isNot(Token::caret_identifier, Token::r_brace))
1621     if (parseOperation())
1622       return failure();
1623 
1624   return success();
1625 }
1626 
1627 /// Get the block with the specified name, creating it if it doesn't already
1628 /// exist.  The location specified is the point of use, which allows
1629 /// us to diagnose references to blocks that are not defined precisely.
1630 Block *OperationParser::getBlockNamed(StringRef name, SMLoc loc) {
1631   auto &blockAndLoc = getBlockInfoByName(name);
1632   if (!blockAndLoc.first) {
1633     blockAndLoc = {new Block(), loc};
1634     insertForwardRef(blockAndLoc.first, loc);
1635   }
1636 
1637   return blockAndLoc.first;
1638 }
1639 
1640 /// Define the block with the specified name. Returns the Block* or nullptr in
1641 /// the case of redefinition.
1642 Block *OperationParser::defineBlockNamed(StringRef name, SMLoc loc,
1643                                          Block *existing) {
1644   auto &blockAndLoc = getBlockInfoByName(name);
1645   if (!blockAndLoc.first) {
1646     // If the caller provided a block, use it.  Otherwise create a new one.
1647     if (!existing)
1648       existing = new Block();
1649     blockAndLoc.first = existing;
1650     blockAndLoc.second = loc;
1651     return blockAndLoc.first;
1652   }
1653 
1654   // Forward declarations are removed once defined, so if we are defining a
1655   // existing block and it is not a forward declaration, then it is a
1656   // redeclaration.
1657   if (!eraseForwardRef(blockAndLoc.first))
1658     return nullptr;
1659   return blockAndLoc.first;
1660 }
1661 
1662 /// Parse a (possibly empty) list of SSA operands with types as block arguments.
1663 ///
1664 ///   ssa-id-and-type-list ::= ssa-id-and-type (`,` ssa-id-and-type)*
1665 ///
1666 ParseResult OperationParser::parseOptionalBlockArgList(
1667     SmallVectorImpl<BlockArgument> &results, Block *owner) {
1668   if (getToken().is(Token::r_brace))
1669     return success();
1670 
1671   // If the block already has arguments, then we're handling the entry block.
1672   // Parse and register the names for the arguments, but do not add them.
1673   bool definingExistingArgs = owner->getNumArguments() != 0;
1674   unsigned nextArgument = 0;
1675 
1676   return parseCommaSeparatedList([&]() -> ParseResult {
1677     return parseSSADefOrUseAndType(
1678         [&](SSAUseInfo useInfo, Type type) -> ParseResult {
1679           // If this block did not have existing arguments, define a new one.
1680           if (!definingExistingArgs)
1681             return addDefinition(useInfo, owner->addArgument(type));
1682 
1683           // Otherwise, ensure that this argument has already been created.
1684           if (nextArgument >= owner->getNumArguments())
1685             return emitError("too many arguments specified in argument list");
1686 
1687           // Finally, make sure the existing argument has the correct type.
1688           auto arg = owner->getArgument(nextArgument++);
1689           if (arg.getType() != type)
1690             return emitError("argument and block argument type mismatch");
1691           return addDefinition(useInfo, arg);
1692         });
1693   });
1694 }
1695 
1696 //===----------------------------------------------------------------------===//
1697 // Top-level entity parsing.
1698 //===----------------------------------------------------------------------===//
1699 
1700 namespace {
1701 /// This parser handles entities that are only valid at the top level of the
1702 /// file.
1703 class ModuleParser : public Parser {
1704 public:
1705   explicit ModuleParser(ParserState &state) : Parser(state) {}
1706 
1707   ParseResult parseModule(ModuleOp module);
1708 
1709 private:
1710   /// Parse an attribute alias declaration.
1711   ParseResult parseAttributeAliasDef();
1712 
1713   /// Parse an attribute alias declaration.
1714   ParseResult parseTypeAliasDef();
1715 };
1716 } // end anonymous namespace
1717 
1718 /// Parses an attribute alias declaration.
1719 ///
1720 ///   attribute-alias-def ::= '#' alias-name `=` attribute-value
1721 ///
1722 ParseResult ModuleParser::parseAttributeAliasDef() {
1723   assert(getToken().is(Token::hash_identifier));
1724   StringRef aliasName = getTokenSpelling().drop_front();
1725 
1726   // Check for redefinitions.
1727   if (getState().symbols.attributeAliasDefinitions.count(aliasName) > 0)
1728     return emitError("redefinition of attribute alias id '" + aliasName + "'");
1729 
1730   // Make sure this isn't invading the dialect attribute namespace.
1731   if (aliasName.contains('.'))
1732     return emitError("attribute names with a '.' are reserved for "
1733                      "dialect-defined names");
1734 
1735   consumeToken(Token::hash_identifier);
1736 
1737   // Parse the '='.
1738   if (parseToken(Token::equal, "expected '=' in attribute alias definition"))
1739     return failure();
1740 
1741   // Parse the attribute value.
1742   Attribute attr = parseAttribute();
1743   if (!attr)
1744     return failure();
1745 
1746   getState().symbols.attributeAliasDefinitions[aliasName] = attr;
1747   return success();
1748 }
1749 
1750 /// Parse a type alias declaration.
1751 ///
1752 ///   type-alias-def ::= '!' alias-name `=` 'type' type
1753 ///
1754 ParseResult ModuleParser::parseTypeAliasDef() {
1755   assert(getToken().is(Token::exclamation_identifier));
1756   StringRef aliasName = getTokenSpelling().drop_front();
1757 
1758   // Check for redefinitions.
1759   if (getState().symbols.typeAliasDefinitions.count(aliasName) > 0)
1760     return emitError("redefinition of type alias id '" + aliasName + "'");
1761 
1762   // Make sure this isn't invading the dialect type namespace.
1763   if (aliasName.contains('.'))
1764     return emitError("type names with a '.' are reserved for "
1765                      "dialect-defined names");
1766 
1767   consumeToken(Token::exclamation_identifier);
1768 
1769   // Parse the '=' and 'type'.
1770   if (parseToken(Token::equal, "expected '=' in type alias definition") ||
1771       parseToken(Token::kw_type, "expected 'type' in type alias definition"))
1772     return failure();
1773 
1774   // Parse the type.
1775   Type aliasedType = parseType();
1776   if (!aliasedType)
1777     return failure();
1778 
1779   // Register this alias with the parser state.
1780   getState().symbols.typeAliasDefinitions.try_emplace(aliasName, aliasedType);
1781   return success();
1782 }
1783 
1784 /// This is the top-level module parser.
1785 ParseResult ModuleParser::parseModule(ModuleOp module) {
1786   OperationParser opParser(getState(), module);
1787 
1788   // Module itself is a name scope.
1789   opParser.pushSSANameScope(/*isIsolated=*/true);
1790 
1791   while (true) {
1792     switch (getToken().getKind()) {
1793     default:
1794       // Parse a top-level operation.
1795       if (opParser.parseOperation())
1796         return failure();
1797       break;
1798 
1799     // If we got to the end of the file, then we're done.
1800     case Token::eof: {
1801       if (opParser.finalize())
1802         return failure();
1803 
1804       // Handle the case where the top level module was explicitly defined.
1805       auto &bodyBlocks = module.getBodyRegion().getBlocks();
1806       auto &operations = bodyBlocks.front().getOperations();
1807       assert(!operations.empty() && "expected a valid module terminator");
1808 
1809       // Check that the first operation is a module, and it is the only
1810       // non-terminator operation.
1811       ModuleOp nested = dyn_cast<ModuleOp>(operations.front());
1812       if (nested && std::next(operations.begin(), 2) == operations.end()) {
1813         // Merge the data of the nested module operation into 'module'.
1814         module.setLoc(nested.getLoc());
1815         module.setAttrs(nested.getOperation()->getMutableAttrDict());
1816         bodyBlocks.splice(bodyBlocks.end(), nested.getBodyRegion().getBlocks());
1817 
1818         // Erase the original module body.
1819         bodyBlocks.pop_front();
1820       }
1821 
1822       return opParser.popSSANameScope();
1823     }
1824 
1825     // If we got an error token, then the lexer already emitted an error, just
1826     // stop.  Someday we could introduce error recovery if there was demand
1827     // for it.
1828     case Token::error:
1829       return failure();
1830 
1831     // Parse an attribute alias.
1832     case Token::hash_identifier:
1833       if (parseAttributeAliasDef())
1834         return failure();
1835       break;
1836 
1837     // Parse a type alias.
1838     case Token::exclamation_identifier:
1839       if (parseTypeAliasDef())
1840         return failure();
1841       break;
1842     }
1843   }
1844 }
1845 
1846 //===----------------------------------------------------------------------===//
1847 
1848 /// This parses the file specified by the indicated SourceMgr and returns an
1849 /// MLIR module if it was valid.  If not, it emits diagnostics and returns
1850 /// null.
1851 OwningModuleRef mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
1852                                       MLIRContext *context) {
1853   auto sourceBuf = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
1854 
1855   // This is the result module we are parsing into.
1856   OwningModuleRef module(ModuleOp::create(FileLineColLoc::get(
1857       sourceBuf->getBufferIdentifier(), /*line=*/0, /*column=*/0, context)));
1858 
1859   SymbolState aliasState;
1860   ParserState state(sourceMgr, context, aliasState);
1861   if (ModuleParser(state).parseModule(*module))
1862     return nullptr;
1863 
1864   // Make sure the parse module has no other structural problems detected by
1865   // the verifier.
1866   if (failed(verify(*module)))
1867     return nullptr;
1868 
1869   return module;
1870 }
1871 
1872 /// This parses the file specified by the indicated filename and returns an
1873 /// MLIR module if it was valid.  If not, the error message is emitted through
1874 /// the error handler registered in the context, and a null pointer is returned.
1875 OwningModuleRef mlir::parseSourceFile(StringRef filename,
1876                                       MLIRContext *context) {
1877   llvm::SourceMgr sourceMgr;
1878   return parseSourceFile(filename, sourceMgr, context);
1879 }
1880 
1881 /// This parses the file specified by the indicated filename using the provided
1882 /// SourceMgr and returns an MLIR module if it was valid.  If not, the error
1883 /// message is emitted through the error handler registered in the context, and
1884 /// a null pointer is returned.
1885 OwningModuleRef mlir::parseSourceFile(StringRef filename,
1886                                       llvm::SourceMgr &sourceMgr,
1887                                       MLIRContext *context) {
1888   if (sourceMgr.getNumBuffers() != 0) {
1889     // TODO(b/136086478): Extend to support multiple buffers.
1890     emitError(mlir::UnknownLoc::get(context),
1891               "only main buffer parsed at the moment");
1892     return nullptr;
1893   }
1894   auto file_or_err = llvm::MemoryBuffer::getFileOrSTDIN(filename);
1895   if (std::error_code error = file_or_err.getError()) {
1896     emitError(mlir::UnknownLoc::get(context),
1897               "could not open input file " + filename);
1898     return nullptr;
1899   }
1900 
1901   // Load the MLIR module.
1902   sourceMgr.AddNewSourceBuffer(std::move(*file_or_err), llvm::SMLoc());
1903   return parseSourceFile(sourceMgr, context);
1904 }
1905 
1906 /// This parses the program string to a MLIR module if it was valid. If not,
1907 /// it emits diagnostics and returns null.
1908 OwningModuleRef mlir::parseSourceString(StringRef moduleStr,
1909                                         MLIRContext *context) {
1910   auto memBuffer = MemoryBuffer::getMemBuffer(moduleStr);
1911   if (!memBuffer)
1912     return nullptr;
1913 
1914   SourceMgr sourceMgr;
1915   sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
1916   return parseSourceFile(sourceMgr, context);
1917 }
1918