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