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 an optional attribute.
1015   OptionalParseResult parseOptionalAttribute(Attribute &result, Type type,
1016                                              StringRef attrName,
1017                                              NamedAttrList &attrs) override {
1018     OptionalParseResult parseResult =
1019         parser.parseOptionalAttribute(result, type);
1020     if (parseResult.hasValue() && succeeded(*parseResult))
1021       attrs.push_back(parser.builder.getNamedAttr(attrName, result));
1022     return parseResult;
1023   }
1024 
1025   /// Parse a named dictionary into 'result' if it is present.
1026   ParseResult parseOptionalAttrDict(NamedAttrList &result) override {
1027     if (parser.getToken().isNot(Token::l_brace))
1028       return success();
1029     return parser.parseAttributeDict(result);
1030   }
1031 
1032   /// Parse a named dictionary into 'result' if the `attributes` keyword is
1033   /// present.
1034   ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result) override {
1035     if (failed(parseOptionalKeyword("attributes")))
1036       return success();
1037     return parser.parseAttributeDict(result);
1038   }
1039 
1040   /// Parse an affine map instance into 'map'.
1041   ParseResult parseAffineMap(AffineMap &map) override {
1042     return parser.parseAffineMapReference(map);
1043   }
1044 
1045   /// Parse an integer set instance into 'set'.
1046   ParseResult printIntegerSet(IntegerSet &set) override {
1047     return parser.parseIntegerSetReference(set);
1048   }
1049 
1050   //===--------------------------------------------------------------------===//
1051   // Identifier Parsing
1052   //===--------------------------------------------------------------------===//
1053 
1054   /// Returns if the current token corresponds to a keyword.
1055   bool isCurrentTokenAKeyword() const {
1056     return parser.getToken().is(Token::bare_identifier) ||
1057            parser.getToken().isKeyword();
1058   }
1059 
1060   /// Parse the given keyword if present.
1061   ParseResult parseOptionalKeyword(StringRef keyword) override {
1062     // Check that the current token has the same spelling.
1063     if (!isCurrentTokenAKeyword() || parser.getTokenSpelling() != keyword)
1064       return failure();
1065     parser.consumeToken();
1066     return success();
1067   }
1068 
1069   /// Parse a keyword, if present, into 'keyword'.
1070   ParseResult parseOptionalKeyword(StringRef *keyword) override {
1071     // Check that the current token is a keyword.
1072     if (!isCurrentTokenAKeyword())
1073       return failure();
1074 
1075     *keyword = parser.getTokenSpelling();
1076     parser.consumeToken();
1077     return success();
1078   }
1079 
1080   /// Parse an optional @-identifier and store it (without the '@' symbol) in a
1081   /// string attribute named 'attrName'.
1082   ParseResult parseOptionalSymbolName(StringAttr &result, StringRef attrName,
1083                                       NamedAttrList &attrs) override {
1084     Token atToken = parser.getToken();
1085     if (atToken.isNot(Token::at_identifier))
1086       return failure();
1087 
1088     result = getBuilder().getStringAttr(atToken.getSymbolReference());
1089     attrs.push_back(getBuilder().getNamedAttr(attrName, result));
1090     parser.consumeToken();
1091     return success();
1092   }
1093 
1094   //===--------------------------------------------------------------------===//
1095   // Operand Parsing
1096   //===--------------------------------------------------------------------===//
1097 
1098   /// Parse a single operand.
1099   ParseResult parseOperand(OperandType &result) override {
1100     OperationParser::SSAUseInfo useInfo;
1101     if (parser.parseSSAUse(useInfo))
1102       return failure();
1103 
1104     result = {useInfo.loc, useInfo.name, useInfo.number};
1105     return success();
1106   }
1107 
1108   /// Parse a single operand if present.
1109   OptionalParseResult parseOptionalOperand(OperandType &result) override {
1110     if (parser.getToken().is(Token::percent_identifier))
1111       return parseOperand(result);
1112     return llvm::None;
1113   }
1114 
1115   /// Parse zero or more SSA comma-separated operand references with a specified
1116   /// surrounding delimiter, and an optional required operand count.
1117   ParseResult parseOperandList(SmallVectorImpl<OperandType> &result,
1118                                int requiredOperandCount = -1,
1119                                Delimiter delimiter = Delimiter::None) override {
1120     return parseOperandOrRegionArgList(result, /*isOperandList=*/true,
1121                                        requiredOperandCount, delimiter);
1122   }
1123 
1124   /// Parse zero or more SSA comma-separated operand or region arguments with
1125   ///  optional surrounding delimiter and required operand count.
1126   ParseResult
1127   parseOperandOrRegionArgList(SmallVectorImpl<OperandType> &result,
1128                               bool isOperandList, int requiredOperandCount = -1,
1129                               Delimiter delimiter = Delimiter::None) {
1130     auto startLoc = parser.getToken().getLoc();
1131 
1132     // Handle delimiters.
1133     switch (delimiter) {
1134     case Delimiter::None:
1135       // Don't check for the absence of a delimiter if the number of operands
1136       // is unknown (and hence the operand list could be empty).
1137       if (requiredOperandCount == -1)
1138         break;
1139       // Token already matches an identifier and so can't be a delimiter.
1140       if (parser.getToken().is(Token::percent_identifier))
1141         break;
1142       // Test against known delimiters.
1143       if (parser.getToken().is(Token::l_paren) ||
1144           parser.getToken().is(Token::l_square))
1145         return emitError(startLoc, "unexpected delimiter");
1146       return emitError(startLoc, "invalid operand");
1147     case Delimiter::OptionalParen:
1148       if (parser.getToken().isNot(Token::l_paren))
1149         return success();
1150       LLVM_FALLTHROUGH;
1151     case Delimiter::Paren:
1152       if (parser.parseToken(Token::l_paren, "expected '(' in operand list"))
1153         return failure();
1154       break;
1155     case Delimiter::OptionalSquare:
1156       if (parser.getToken().isNot(Token::l_square))
1157         return success();
1158       LLVM_FALLTHROUGH;
1159     case Delimiter::Square:
1160       if (parser.parseToken(Token::l_square, "expected '[' in operand list"))
1161         return failure();
1162       break;
1163     }
1164 
1165     // Check for zero operands.
1166     if (parser.getToken().is(Token::percent_identifier)) {
1167       do {
1168         OperandType operandOrArg;
1169         if (isOperandList ? parseOperand(operandOrArg)
1170                           : parseRegionArgument(operandOrArg))
1171           return failure();
1172         result.push_back(operandOrArg);
1173       } while (parser.consumeIf(Token::comma));
1174     }
1175 
1176     // Handle delimiters.   If we reach here, the optional delimiters were
1177     // present, so we need to parse their closing one.
1178     switch (delimiter) {
1179     case Delimiter::None:
1180       break;
1181     case Delimiter::OptionalParen:
1182     case Delimiter::Paren:
1183       if (parser.parseToken(Token::r_paren, "expected ')' in operand list"))
1184         return failure();
1185       break;
1186     case Delimiter::OptionalSquare:
1187     case Delimiter::Square:
1188       if (parser.parseToken(Token::r_square, "expected ']' in operand list"))
1189         return failure();
1190       break;
1191     }
1192 
1193     if (requiredOperandCount != -1 &&
1194         result.size() != static_cast<size_t>(requiredOperandCount))
1195       return emitError(startLoc, "expected ")
1196              << requiredOperandCount << " operands";
1197     return success();
1198   }
1199 
1200   /// Parse zero or more trailing SSA comma-separated trailing operand
1201   /// references with a specified surrounding delimiter, and an optional
1202   /// required operand count. A leading comma is expected before the operands.
1203   ParseResult parseTrailingOperandList(SmallVectorImpl<OperandType> &result,
1204                                        int requiredOperandCount,
1205                                        Delimiter delimiter) override {
1206     if (parser.getToken().is(Token::comma)) {
1207       parseComma();
1208       return parseOperandList(result, requiredOperandCount, delimiter);
1209     }
1210     if (requiredOperandCount != -1)
1211       return emitError(parser.getToken().getLoc(), "expected ")
1212              << requiredOperandCount << " operands";
1213     return success();
1214   }
1215 
1216   /// Resolve an operand to an SSA value, emitting an error on failure.
1217   ParseResult resolveOperand(const OperandType &operand, Type type,
1218                              SmallVectorImpl<Value> &result) override {
1219     OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number,
1220                                                operand.location};
1221     if (auto value = parser.resolveSSAUse(operandInfo, type)) {
1222       result.push_back(value);
1223       return success();
1224     }
1225     return failure();
1226   }
1227 
1228   /// Parse an AffineMap of SSA ids.
1229   ParseResult parseAffineMapOfSSAIds(SmallVectorImpl<OperandType> &operands,
1230                                      Attribute &mapAttr, StringRef attrName,
1231                                      NamedAttrList &attrs,
1232                                      Delimiter delimiter) override {
1233     SmallVector<OperandType, 2> dimOperands;
1234     SmallVector<OperandType, 1> symOperands;
1235 
1236     auto parseElement = [&](bool isSymbol) -> ParseResult {
1237       OperandType operand;
1238       if (parseOperand(operand))
1239         return failure();
1240       if (isSymbol)
1241         symOperands.push_back(operand);
1242       else
1243         dimOperands.push_back(operand);
1244       return success();
1245     };
1246 
1247     AffineMap map;
1248     if (parser.parseAffineMapOfSSAIds(map, parseElement, delimiter))
1249       return failure();
1250     // Add AffineMap attribute.
1251     if (map) {
1252       mapAttr = AffineMapAttr::get(map);
1253       attrs.push_back(parser.builder.getNamedAttr(attrName, mapAttr));
1254     }
1255 
1256     // Add dim operands before symbol operands in 'operands'.
1257     operands.assign(dimOperands.begin(), dimOperands.end());
1258     operands.append(symOperands.begin(), symOperands.end());
1259     return success();
1260   }
1261 
1262   //===--------------------------------------------------------------------===//
1263   // Region Parsing
1264   //===--------------------------------------------------------------------===//
1265 
1266   /// Parse a region that takes `arguments` of `argTypes` types.  This
1267   /// effectively defines the SSA values of `arguments` and assigns their type.
1268   ParseResult parseRegion(Region &region, ArrayRef<OperandType> arguments,
1269                           ArrayRef<Type> argTypes,
1270                           bool enableNameShadowing) override {
1271     assert(arguments.size() == argTypes.size() &&
1272            "mismatching number of arguments and types");
1273 
1274     SmallVector<std::pair<OperationParser::SSAUseInfo, Type>, 2>
1275         regionArguments;
1276     for (auto pair : llvm::zip(arguments, argTypes)) {
1277       const OperandType &operand = std::get<0>(pair);
1278       Type type = std::get<1>(pair);
1279       OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number,
1280                                                  operand.location};
1281       regionArguments.emplace_back(operandInfo, type);
1282     }
1283 
1284     // Try to parse the region.
1285     assert((!enableNameShadowing ||
1286             opDefinition->hasProperty(OperationProperty::IsolatedFromAbove)) &&
1287            "name shadowing is only allowed on isolated regions");
1288     if (parser.parseRegion(region, regionArguments, enableNameShadowing))
1289       return failure();
1290     return success();
1291   }
1292 
1293   /// Parses a region if present.
1294   ParseResult parseOptionalRegion(Region &region,
1295                                   ArrayRef<OperandType> arguments,
1296                                   ArrayRef<Type> argTypes,
1297                                   bool enableNameShadowing) override {
1298     if (parser.getToken().isNot(Token::l_brace))
1299       return success();
1300     return parseRegion(region, arguments, argTypes, enableNameShadowing);
1301   }
1302 
1303   /// Parse a region argument. The type of the argument will be resolved later
1304   /// by a call to `parseRegion`.
1305   ParseResult parseRegionArgument(OperandType &argument) override {
1306     return parseOperand(argument);
1307   }
1308 
1309   /// Parse a region argument if present.
1310   ParseResult parseOptionalRegionArgument(OperandType &argument) override {
1311     if (parser.getToken().isNot(Token::percent_identifier))
1312       return success();
1313     return parseRegionArgument(argument);
1314   }
1315 
1316   ParseResult
1317   parseRegionArgumentList(SmallVectorImpl<OperandType> &result,
1318                           int requiredOperandCount = -1,
1319                           Delimiter delimiter = Delimiter::None) override {
1320     return parseOperandOrRegionArgList(result, /*isOperandList=*/false,
1321                                        requiredOperandCount, delimiter);
1322   }
1323 
1324   //===--------------------------------------------------------------------===//
1325   // Successor Parsing
1326   //===--------------------------------------------------------------------===//
1327 
1328   /// Parse a single operation successor.
1329   ParseResult parseSuccessor(Block *&dest) override {
1330     return parser.parseSuccessor(dest);
1331   }
1332 
1333   /// Parse an optional operation successor and its operand list.
1334   OptionalParseResult parseOptionalSuccessor(Block *&dest) override {
1335     if (parser.getToken().isNot(Token::caret_identifier))
1336       return llvm::None;
1337     return parseSuccessor(dest);
1338   }
1339 
1340   /// Parse a single operation successor and its operand list.
1341   ParseResult
1342   parseSuccessorAndUseList(Block *&dest,
1343                            SmallVectorImpl<Value> &operands) override {
1344     if (parseSuccessor(dest))
1345       return failure();
1346 
1347     // Handle optional arguments.
1348     if (succeeded(parseOptionalLParen()) &&
1349         (parser.parseOptionalSSAUseAndTypeList(operands) || parseRParen())) {
1350       return failure();
1351     }
1352     return success();
1353   }
1354 
1355   //===--------------------------------------------------------------------===//
1356   // Type Parsing
1357   //===--------------------------------------------------------------------===//
1358 
1359   /// Parse a type.
1360   ParseResult parseType(Type &result) override {
1361     return failure(!(result = parser.parseType()));
1362   }
1363 
1364   /// Parse an optional type.
1365   OptionalParseResult parseOptionalType(Type &result) override {
1366     return parser.parseOptionalType(result);
1367   }
1368 
1369   /// Parse an arrow followed by a type list.
1370   ParseResult parseArrowTypeList(SmallVectorImpl<Type> &result) override {
1371     if (parseArrow() || parser.parseFunctionResultTypes(result))
1372       return failure();
1373     return success();
1374   }
1375 
1376   /// Parse an optional arrow followed by a type list.
1377   ParseResult
1378   parseOptionalArrowTypeList(SmallVectorImpl<Type> &result) override {
1379     if (!parser.consumeIf(Token::arrow))
1380       return success();
1381     return parser.parseFunctionResultTypes(result);
1382   }
1383 
1384   /// Parse a colon followed by a type.
1385   ParseResult parseColonType(Type &result) override {
1386     return failure(parser.parseToken(Token::colon, "expected ':'") ||
1387                    !(result = parser.parseType()));
1388   }
1389 
1390   /// Parse a colon followed by a type list, which must have at least one type.
1391   ParseResult parseColonTypeList(SmallVectorImpl<Type> &result) override {
1392     if (parser.parseToken(Token::colon, "expected ':'"))
1393       return failure();
1394     return parser.parseTypeListNoParens(result);
1395   }
1396 
1397   /// Parse an optional colon followed by a type list, which if present must
1398   /// have at least one type.
1399   ParseResult
1400   parseOptionalColonTypeList(SmallVectorImpl<Type> &result) override {
1401     if (!parser.consumeIf(Token::colon))
1402       return success();
1403     return parser.parseTypeListNoParens(result);
1404   }
1405 
1406   /// Parse a list of assignments of the form
1407   /// (%x1 = %y1 : type1, %x2 = %y2 : type2, ...).
1408   /// The list must contain at least one entry
1409   ParseResult parseAssignmentList(SmallVectorImpl<OperandType> &lhs,
1410                                   SmallVectorImpl<OperandType> &rhs) override {
1411     auto parseElt = [&]() -> ParseResult {
1412       OperandType regionArg, operand;
1413       if (parseRegionArgument(regionArg) || parseEqual() ||
1414           parseOperand(operand))
1415         return failure();
1416       lhs.push_back(regionArg);
1417       rhs.push_back(operand);
1418       return success();
1419     };
1420     if (parseLParen())
1421       return failure();
1422     return parser.parseCommaSeparatedListUntil(Token::r_paren, parseElt);
1423   }
1424 
1425 private:
1426   /// The source location of the operation name.
1427   SMLoc nameLoc;
1428 
1429   /// Information about the result name specifiers.
1430   ArrayRef<OperationParser::ResultRecord> resultIDs;
1431 
1432   /// The abstract information of the operation.
1433   const AbstractOperation *opDefinition;
1434 
1435   /// The main operation parser.
1436   OperationParser &parser;
1437 
1438   /// A flag that indicates if any errors were emitted during parsing.
1439   bool emittedError = false;
1440 };
1441 } // end anonymous namespace.
1442 
1443 Operation *
1444 OperationParser::parseCustomOperation(ArrayRef<ResultRecord> resultIDs) {
1445   auto opLoc = getToken().getLoc();
1446   auto opName = getTokenSpelling();
1447 
1448   auto *opDefinition = AbstractOperation::lookup(opName, getContext());
1449   if (!opDefinition && !opName.contains('.')) {
1450     // If the operation name has no namespace prefix we treat it as a standard
1451     // operation and prefix it with "std".
1452     // TODO: Would it be better to just build a mapping of the registered
1453     // operations in the standard dialect?
1454     opDefinition =
1455         AbstractOperation::lookup(Twine("std." + opName).str(), getContext());
1456   }
1457 
1458   if (!opDefinition) {
1459     emitError(opLoc) << "custom op '" << opName << "' is unknown";
1460     return nullptr;
1461   }
1462 
1463   consumeToken();
1464 
1465   // If the custom op parser crashes, produce some indication to help
1466   // debugging.
1467   std::string opNameStr = opName.str();
1468   llvm::PrettyStackTraceFormat fmt("MLIR Parser: custom op parser '%s'",
1469                                    opNameStr.c_str());
1470 
1471   // Get location information for the operation.
1472   auto srcLocation = getEncodedSourceLocation(opLoc);
1473 
1474   // Have the op implementation take a crack and parsing this.
1475   OperationState opState(srcLocation, opDefinition->name);
1476   CleanupOpStateRegions guard{opState};
1477   CustomOpAsmParser opAsmParser(opLoc, resultIDs, opDefinition, *this);
1478   if (opAsmParser.parseOperation(opState))
1479     return nullptr;
1480 
1481   // If it emitted an error, we failed.
1482   if (opAsmParser.didEmitError())
1483     return nullptr;
1484 
1485   // Parse a location if one is present.
1486   if (parseOptionalTrailingLocation(opState.location))
1487     return nullptr;
1488 
1489   // Otherwise, we succeeded.  Use the state it parsed as our op information.
1490   return opBuilder.createOperation(opState);
1491 }
1492 
1493 //===----------------------------------------------------------------------===//
1494 // Region Parsing
1495 //===----------------------------------------------------------------------===//
1496 
1497 /// Region.
1498 ///
1499 ///   region ::= '{' region-body
1500 ///
1501 ParseResult OperationParser::parseRegion(
1502     Region &region,
1503     ArrayRef<std::pair<OperationParser::SSAUseInfo, Type>> entryArguments,
1504     bool isIsolatedNameScope) {
1505   // Parse the '{'.
1506   if (parseToken(Token::l_brace, "expected '{' to begin a region"))
1507     return failure();
1508 
1509   // Check for an empty region.
1510   if (entryArguments.empty() && consumeIf(Token::r_brace))
1511     return success();
1512   auto currentPt = opBuilder.saveInsertionPoint();
1513 
1514   // Push a new named value scope.
1515   pushSSANameScope(isIsolatedNameScope);
1516 
1517   // Parse the first block directly to allow for it to be unnamed.
1518   auto owning_block = std::make_unique<Block>();
1519   Block *block = owning_block.get();
1520 
1521   // Add arguments to the entry block.
1522   if (!entryArguments.empty()) {
1523     for (auto &placeholderArgPair : entryArguments) {
1524       auto &argInfo = placeholderArgPair.first;
1525       // Ensure that the argument was not already defined.
1526       if (auto defLoc = getReferenceLoc(argInfo.name, argInfo.number)) {
1527         return emitError(argInfo.loc, "region entry argument '" + argInfo.name +
1528                                           "' is already in use")
1529                    .attachNote(getEncodedSourceLocation(*defLoc))
1530                << "previously referenced here";
1531       }
1532       if (addDefinition(placeholderArgPair.first,
1533                         block->addArgument(placeholderArgPair.second))) {
1534         return failure();
1535       }
1536     }
1537 
1538     // If we had named arguments, then don't allow a block name.
1539     if (getToken().is(Token::caret_identifier))
1540       return emitError("invalid block name in region with named arguments");
1541   }
1542 
1543   if (parseBlock(block)) {
1544     return failure();
1545   }
1546 
1547   // Verify that no other arguments were parsed.
1548   if (!entryArguments.empty() &&
1549       block->getNumArguments() > entryArguments.size()) {
1550     return emitError("entry block arguments were already defined");
1551   }
1552 
1553   // Parse the rest of the region.
1554   region.push_back(owning_block.release());
1555   if (parseRegionBody(region))
1556     return failure();
1557 
1558   // Pop the SSA value scope for this region.
1559   if (popSSANameScope())
1560     return failure();
1561 
1562   // Reset the original insertion point.
1563   opBuilder.restoreInsertionPoint(currentPt);
1564   return success();
1565 }
1566 
1567 /// Region.
1568 ///
1569 ///   region-body ::= block* '}'
1570 ///
1571 ParseResult OperationParser::parseRegionBody(Region &region) {
1572   // Parse the list of blocks.
1573   while (!consumeIf(Token::r_brace)) {
1574     Block *newBlock = nullptr;
1575     if (parseBlock(newBlock))
1576       return failure();
1577     region.push_back(newBlock);
1578   }
1579   return success();
1580 }
1581 
1582 //===----------------------------------------------------------------------===//
1583 // Block Parsing
1584 //===----------------------------------------------------------------------===//
1585 
1586 /// Block declaration.
1587 ///
1588 ///   block ::= block-label? operation*
1589 ///   block-label    ::= block-id block-arg-list? `:`
1590 ///   block-id       ::= caret-id
1591 ///   block-arg-list ::= `(` ssa-id-and-type-list? `)`
1592 ///
1593 ParseResult OperationParser::parseBlock(Block *&block) {
1594   // The first block of a region may already exist, if it does the caret
1595   // identifier is optional.
1596   if (block && getToken().isNot(Token::caret_identifier))
1597     return parseBlockBody(block);
1598 
1599   SMLoc nameLoc = getToken().getLoc();
1600   auto name = getTokenSpelling();
1601   if (parseToken(Token::caret_identifier, "expected block name"))
1602     return failure();
1603 
1604   block = defineBlockNamed(name, nameLoc, block);
1605 
1606   // Fail if the block was already defined.
1607   if (!block)
1608     return emitError(nameLoc, "redefinition of block '") << name << "'";
1609 
1610   // If an argument list is present, parse it.
1611   if (consumeIf(Token::l_paren)) {
1612     SmallVector<BlockArgument, 8> bbArgs;
1613     if (parseOptionalBlockArgList(bbArgs, block) ||
1614         parseToken(Token::r_paren, "expected ')' to end argument list"))
1615       return failure();
1616   }
1617 
1618   if (parseToken(Token::colon, "expected ':' after block name"))
1619     return failure();
1620 
1621   return parseBlockBody(block);
1622 }
1623 
1624 ParseResult OperationParser::parseBlockBody(Block *block) {
1625   // Set the insertion point to the end of the block to parse.
1626   opBuilder.setInsertionPointToEnd(block);
1627 
1628   // Parse the list of operations that make up the body of the block.
1629   while (getToken().isNot(Token::caret_identifier, Token::r_brace))
1630     if (parseOperation())
1631       return failure();
1632 
1633   return success();
1634 }
1635 
1636 /// Get the block with the specified name, creating it if it doesn't already
1637 /// exist.  The location specified is the point of use, which allows
1638 /// us to diagnose references to blocks that are not defined precisely.
1639 Block *OperationParser::getBlockNamed(StringRef name, SMLoc loc) {
1640   auto &blockAndLoc = getBlockInfoByName(name);
1641   if (!blockAndLoc.first) {
1642     blockAndLoc = {new Block(), loc};
1643     insertForwardRef(blockAndLoc.first, loc);
1644   }
1645 
1646   return blockAndLoc.first;
1647 }
1648 
1649 /// Define the block with the specified name. Returns the Block* or nullptr in
1650 /// the case of redefinition.
1651 Block *OperationParser::defineBlockNamed(StringRef name, SMLoc loc,
1652                                          Block *existing) {
1653   auto &blockAndLoc = getBlockInfoByName(name);
1654   if (!blockAndLoc.first) {
1655     // If the caller provided a block, use it.  Otherwise create a new one.
1656     if (!existing)
1657       existing = new Block();
1658     blockAndLoc.first = existing;
1659     blockAndLoc.second = loc;
1660     return blockAndLoc.first;
1661   }
1662 
1663   // Forward declarations are removed once defined, so if we are defining a
1664   // existing block and it is not a forward declaration, then it is a
1665   // redeclaration.
1666   if (!eraseForwardRef(blockAndLoc.first))
1667     return nullptr;
1668   return blockAndLoc.first;
1669 }
1670 
1671 /// Parse a (possibly empty) list of SSA operands with types as block arguments.
1672 ///
1673 ///   ssa-id-and-type-list ::= ssa-id-and-type (`,` ssa-id-and-type)*
1674 ///
1675 ParseResult OperationParser::parseOptionalBlockArgList(
1676     SmallVectorImpl<BlockArgument> &results, Block *owner) {
1677   if (getToken().is(Token::r_brace))
1678     return success();
1679 
1680   // If the block already has arguments, then we're handling the entry block.
1681   // Parse and register the names for the arguments, but do not add them.
1682   bool definingExistingArgs = owner->getNumArguments() != 0;
1683   unsigned nextArgument = 0;
1684 
1685   return parseCommaSeparatedList([&]() -> ParseResult {
1686     return parseSSADefOrUseAndType(
1687         [&](SSAUseInfo useInfo, Type type) -> ParseResult {
1688           // If this block did not have existing arguments, define a new one.
1689           if (!definingExistingArgs)
1690             return addDefinition(useInfo, owner->addArgument(type));
1691 
1692           // Otherwise, ensure that this argument has already been created.
1693           if (nextArgument >= owner->getNumArguments())
1694             return emitError("too many arguments specified in argument list");
1695 
1696           // Finally, make sure the existing argument has the correct type.
1697           auto arg = owner->getArgument(nextArgument++);
1698           if (arg.getType() != type)
1699             return emitError("argument and block argument type mismatch");
1700           return addDefinition(useInfo, arg);
1701         });
1702   });
1703 }
1704 
1705 //===----------------------------------------------------------------------===//
1706 // Top-level entity parsing.
1707 //===----------------------------------------------------------------------===//
1708 
1709 namespace {
1710 /// This parser handles entities that are only valid at the top level of the
1711 /// file.
1712 class ModuleParser : public Parser {
1713 public:
1714   explicit ModuleParser(ParserState &state) : Parser(state) {}
1715 
1716   ParseResult parseModule(ModuleOp module);
1717 
1718 private:
1719   /// Parse an attribute alias declaration.
1720   ParseResult parseAttributeAliasDef();
1721 
1722   /// Parse an attribute alias declaration.
1723   ParseResult parseTypeAliasDef();
1724 };
1725 } // end anonymous namespace
1726 
1727 /// Parses an attribute alias declaration.
1728 ///
1729 ///   attribute-alias-def ::= '#' alias-name `=` attribute-value
1730 ///
1731 ParseResult ModuleParser::parseAttributeAliasDef() {
1732   assert(getToken().is(Token::hash_identifier));
1733   StringRef aliasName = getTokenSpelling().drop_front();
1734 
1735   // Check for redefinitions.
1736   if (getState().symbols.attributeAliasDefinitions.count(aliasName) > 0)
1737     return emitError("redefinition of attribute alias id '" + aliasName + "'");
1738 
1739   // Make sure this isn't invading the dialect attribute namespace.
1740   if (aliasName.contains('.'))
1741     return emitError("attribute names with a '.' are reserved for "
1742                      "dialect-defined names");
1743 
1744   consumeToken(Token::hash_identifier);
1745 
1746   // Parse the '='.
1747   if (parseToken(Token::equal, "expected '=' in attribute alias definition"))
1748     return failure();
1749 
1750   // Parse the attribute value.
1751   Attribute attr = parseAttribute();
1752   if (!attr)
1753     return failure();
1754 
1755   getState().symbols.attributeAliasDefinitions[aliasName] = attr;
1756   return success();
1757 }
1758 
1759 /// Parse a type alias declaration.
1760 ///
1761 ///   type-alias-def ::= '!' alias-name `=` 'type' type
1762 ///
1763 ParseResult ModuleParser::parseTypeAliasDef() {
1764   assert(getToken().is(Token::exclamation_identifier));
1765   StringRef aliasName = getTokenSpelling().drop_front();
1766 
1767   // Check for redefinitions.
1768   if (getState().symbols.typeAliasDefinitions.count(aliasName) > 0)
1769     return emitError("redefinition of type alias id '" + aliasName + "'");
1770 
1771   // Make sure this isn't invading the dialect type namespace.
1772   if (aliasName.contains('.'))
1773     return emitError("type names with a '.' are reserved for "
1774                      "dialect-defined names");
1775 
1776   consumeToken(Token::exclamation_identifier);
1777 
1778   // Parse the '=' and 'type'.
1779   if (parseToken(Token::equal, "expected '=' in type alias definition") ||
1780       parseToken(Token::kw_type, "expected 'type' in type alias definition"))
1781     return failure();
1782 
1783   // Parse the type.
1784   Type aliasedType = parseType();
1785   if (!aliasedType)
1786     return failure();
1787 
1788   // Register this alias with the parser state.
1789   getState().symbols.typeAliasDefinitions.try_emplace(aliasName, aliasedType);
1790   return success();
1791 }
1792 
1793 /// This is the top-level module parser.
1794 ParseResult ModuleParser::parseModule(ModuleOp module) {
1795   OperationParser opParser(getState(), module);
1796 
1797   // Module itself is a name scope.
1798   opParser.pushSSANameScope(/*isIsolated=*/true);
1799 
1800   while (true) {
1801     switch (getToken().getKind()) {
1802     default:
1803       // Parse a top-level operation.
1804       if (opParser.parseOperation())
1805         return failure();
1806       break;
1807 
1808     // If we got to the end of the file, then we're done.
1809     case Token::eof: {
1810       if (opParser.finalize())
1811         return failure();
1812 
1813       // Handle the case where the top level module was explicitly defined.
1814       auto &bodyBlocks = module.getBodyRegion().getBlocks();
1815       auto &operations = bodyBlocks.front().getOperations();
1816       assert(!operations.empty() && "expected a valid module terminator");
1817 
1818       // Check that the first operation is a module, and it is the only
1819       // non-terminator operation.
1820       ModuleOp nested = dyn_cast<ModuleOp>(operations.front());
1821       if (nested && std::next(operations.begin(), 2) == operations.end()) {
1822         // Merge the data of the nested module operation into 'module'.
1823         module.setLoc(nested.getLoc());
1824         module.setAttrs(nested.getOperation()->getMutableAttrDict());
1825         bodyBlocks.splice(bodyBlocks.end(), nested.getBodyRegion().getBlocks());
1826 
1827         // Erase the original module body.
1828         bodyBlocks.pop_front();
1829       }
1830 
1831       return opParser.popSSANameScope();
1832     }
1833 
1834     // If we got an error token, then the lexer already emitted an error, just
1835     // stop.  Someday we could introduce error recovery if there was demand
1836     // for it.
1837     case Token::error:
1838       return failure();
1839 
1840     // Parse an attribute alias.
1841     case Token::hash_identifier:
1842       if (parseAttributeAliasDef())
1843         return failure();
1844       break;
1845 
1846     // Parse a type alias.
1847     case Token::exclamation_identifier:
1848       if (parseTypeAliasDef())
1849         return failure();
1850       break;
1851     }
1852   }
1853 }
1854 
1855 //===----------------------------------------------------------------------===//
1856 
1857 /// This parses the file specified by the indicated SourceMgr and returns an
1858 /// MLIR module if it was valid.  If not, it emits diagnostics and returns
1859 /// null.
1860 OwningModuleRef mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
1861                                       MLIRContext *context) {
1862   auto sourceBuf = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
1863 
1864   // This is the result module we are parsing into.
1865   OwningModuleRef module(ModuleOp::create(FileLineColLoc::get(
1866       sourceBuf->getBufferIdentifier(), /*line=*/0, /*column=*/0, context)));
1867 
1868   SymbolState aliasState;
1869   ParserState state(sourceMgr, context, aliasState);
1870   if (ModuleParser(state).parseModule(*module))
1871     return nullptr;
1872 
1873   // Make sure the parse module has no other structural problems detected by
1874   // the verifier.
1875   if (failed(verify(*module)))
1876     return nullptr;
1877 
1878   return module;
1879 }
1880 
1881 /// This parses the file specified by the indicated filename and returns an
1882 /// MLIR module if it was valid.  If not, the error message is emitted through
1883 /// the error handler registered in the context, and a null pointer is returned.
1884 OwningModuleRef mlir::parseSourceFile(StringRef filename,
1885                                       MLIRContext *context) {
1886   llvm::SourceMgr sourceMgr;
1887   return parseSourceFile(filename, sourceMgr, context);
1888 }
1889 
1890 /// This parses the file specified by the indicated filename using the provided
1891 /// SourceMgr and returns an MLIR module if it was valid.  If not, the error
1892 /// message is emitted through the error handler registered in the context, and
1893 /// a null pointer is returned.
1894 OwningModuleRef mlir::parseSourceFile(StringRef filename,
1895                                       llvm::SourceMgr &sourceMgr,
1896                                       MLIRContext *context) {
1897   if (sourceMgr.getNumBuffers() != 0) {
1898     // TODO: Extend to support multiple buffers.
1899     emitError(mlir::UnknownLoc::get(context),
1900               "only main buffer parsed at the moment");
1901     return nullptr;
1902   }
1903   auto file_or_err = llvm::MemoryBuffer::getFileOrSTDIN(filename);
1904   if (std::error_code error = file_or_err.getError()) {
1905     emitError(mlir::UnknownLoc::get(context),
1906               "could not open input file " + filename);
1907     return nullptr;
1908   }
1909 
1910   // Load the MLIR module.
1911   sourceMgr.AddNewSourceBuffer(std::move(*file_or_err), llvm::SMLoc());
1912   return parseSourceFile(sourceMgr, context);
1913 }
1914 
1915 /// This parses the program string to a MLIR module if it was valid. If not,
1916 /// it emits diagnostics and returns null.
1917 OwningModuleRef mlir::parseSourceString(StringRef moduleStr,
1918                                         MLIRContext *context) {
1919   auto memBuffer = MemoryBuffer::getMemBuffer(moduleStr);
1920   if (!memBuffer)
1921     return nullptr;
1922 
1923   SourceMgr sourceMgr;
1924   sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
1925   return parseSourceFile(sourceMgr, context);
1926 }
1927