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