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(SMLoc nameLoc,
929                     ArrayRef<OperationParser::ResultRecord> resultIDs,
930                     const AbstractOperation *opDefinition,
931                     OperationParser &parser)
932       : nameLoc(nameLoc), resultIDs(resultIDs), opDefinition(opDefinition),
933         parser(parser) {}
934 
935   /// Parse an instance of the operation described by 'opDefinition' into the
936   /// provided operation state.
937   ParseResult parseOperation(OperationState &opState) {
938     if (opDefinition->parseAssembly(*this, opState))
939       return failure();
940     // Verify that the parsed attributes does not have duplicate attributes.
941     // This can happen if an attribute set during parsing is also specified in
942     // the attribute dictionary in the assembly, or the attribute is set
943     // multiple during parsing.
944     Optional<NamedAttribute> duplicate = opState.attributes.findDuplicate();
945     if (duplicate)
946       return emitError(getNameLoc(), "attribute '")
947              << duplicate->first
948              << "' occurs more than once in the attribute list";
949     return success();
950   }
951 
952   Operation *parseGenericOperation(Block *insertBlock,
953                                    Block::iterator insertPt) final {
954     return parser.parseGenericOperation(insertBlock, insertPt);
955   }
956 
957   //===--------------------------------------------------------------------===//
958   // Utilities
959   //===--------------------------------------------------------------------===//
960 
961   /// Return if any errors were emitted during parsing.
962   bool didEmitError() const { return emittedError; }
963 
964   /// Emit a diagnostic at the specified location and return failure.
965   InFlightDiagnostic emitError(llvm::SMLoc loc, const Twine &message) override {
966     emittedError = true;
967     return parser.emitError(loc, "custom op '" + opDefinition->name.strref() +
968                                      "' " + 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 ||
1494             opDefinition->hasTrait<OpTrait::IsIsolatedFromAbove>()) &&
1495            "name shadowing is only allowed on isolated regions");
1496     if (parser.parseRegion(region, regionArguments, enableNameShadowing))
1497       return failure();
1498     return success();
1499   }
1500 
1501   /// Parses a region if present.
1502   OptionalParseResult parseOptionalRegion(Region &region,
1503                                           ArrayRef<OperandType> arguments,
1504                                           ArrayRef<Type> argTypes,
1505                                           bool enableNameShadowing) override {
1506     if (parser.getToken().isNot(Token::l_brace))
1507       return llvm::None;
1508     return parseRegion(region, arguments, argTypes, enableNameShadowing);
1509   }
1510 
1511   /// Parses a region if present. If the region is present, a new region is
1512   /// allocated and placed in `region`. If no region is present, `region`
1513   /// remains untouched.
1514   OptionalParseResult
1515   parseOptionalRegion(std::unique_ptr<Region> &region,
1516                       ArrayRef<OperandType> arguments, ArrayRef<Type> argTypes,
1517                       bool enableNameShadowing = false) override {
1518     if (parser.getToken().isNot(Token::l_brace))
1519       return llvm::None;
1520     std::unique_ptr<Region> newRegion = std::make_unique<Region>();
1521     if (parseRegion(*newRegion, arguments, argTypes, enableNameShadowing))
1522       return failure();
1523 
1524     region = std::move(newRegion);
1525     return success();
1526   }
1527 
1528   /// Parse a region argument. The type of the argument will be resolved later
1529   /// by a call to `parseRegion`.
1530   ParseResult parseRegionArgument(OperandType &argument) override {
1531     return parseOperand(argument);
1532   }
1533 
1534   /// Parse a region argument if present.
1535   ParseResult parseOptionalRegionArgument(OperandType &argument) override {
1536     if (parser.getToken().isNot(Token::percent_identifier))
1537       return success();
1538     return parseRegionArgument(argument);
1539   }
1540 
1541   ParseResult
1542   parseRegionArgumentList(SmallVectorImpl<OperandType> &result,
1543                           int requiredOperandCount = -1,
1544                           Delimiter delimiter = Delimiter::None) override {
1545     return parseOperandOrRegionArgList(result, /*isOperandList=*/false,
1546                                        requiredOperandCount, delimiter);
1547   }
1548 
1549   //===--------------------------------------------------------------------===//
1550   // Successor Parsing
1551   //===--------------------------------------------------------------------===//
1552 
1553   /// Parse a single operation successor.
1554   ParseResult parseSuccessor(Block *&dest) override {
1555     return parser.parseSuccessor(dest);
1556   }
1557 
1558   /// Parse an optional operation successor and its operand list.
1559   OptionalParseResult parseOptionalSuccessor(Block *&dest) override {
1560     if (parser.getToken().isNot(Token::caret_identifier))
1561       return llvm::None;
1562     return parseSuccessor(dest);
1563   }
1564 
1565   /// Parse a single operation successor and its operand list.
1566   ParseResult
1567   parseSuccessorAndUseList(Block *&dest,
1568                            SmallVectorImpl<Value> &operands) override {
1569     if (parseSuccessor(dest))
1570       return failure();
1571 
1572     // Handle optional arguments.
1573     if (succeeded(parseOptionalLParen()) &&
1574         (parser.parseOptionalSSAUseAndTypeList(operands) || parseRParen())) {
1575       return failure();
1576     }
1577     return success();
1578   }
1579 
1580   //===--------------------------------------------------------------------===//
1581   // Type Parsing
1582   //===--------------------------------------------------------------------===//
1583 
1584   /// Parse a type.
1585   ParseResult parseType(Type &result) override {
1586     return failure(!(result = parser.parseType()));
1587   }
1588 
1589   /// Parse an optional type.
1590   OptionalParseResult parseOptionalType(Type &result) override {
1591     return parser.parseOptionalType(result);
1592   }
1593 
1594   /// Parse an arrow followed by a type list.
1595   ParseResult parseArrowTypeList(SmallVectorImpl<Type> &result) override {
1596     if (parseArrow() || parser.parseFunctionResultTypes(result))
1597       return failure();
1598     return success();
1599   }
1600 
1601   /// Parse an optional arrow followed by a type list.
1602   ParseResult
1603   parseOptionalArrowTypeList(SmallVectorImpl<Type> &result) override {
1604     if (!parser.consumeIf(Token::arrow))
1605       return success();
1606     return parser.parseFunctionResultTypes(result);
1607   }
1608 
1609   /// Parse a colon followed by a type.
1610   ParseResult parseColonType(Type &result) override {
1611     return failure(parser.parseToken(Token::colon, "expected ':'") ||
1612                    !(result = parser.parseType()));
1613   }
1614 
1615   /// Parse a colon followed by a type list, which must have at least one type.
1616   ParseResult parseColonTypeList(SmallVectorImpl<Type> &result) override {
1617     if (parser.parseToken(Token::colon, "expected ':'"))
1618       return failure();
1619     return parser.parseTypeListNoParens(result);
1620   }
1621 
1622   /// Parse an optional colon followed by a type list, which if present must
1623   /// have at least one type.
1624   ParseResult
1625   parseOptionalColonTypeList(SmallVectorImpl<Type> &result) override {
1626     if (!parser.consumeIf(Token::colon))
1627       return success();
1628     return parser.parseTypeListNoParens(result);
1629   }
1630 
1631   /// Parse a list of assignments of the form
1632   ///   (%x1 = %y1, %x2 = %y2, ...).
1633   OptionalParseResult
1634   parseOptionalAssignmentList(SmallVectorImpl<OperandType> &lhs,
1635                               SmallVectorImpl<OperandType> &rhs) override {
1636     if (failed(parseOptionalLParen()))
1637       return llvm::None;
1638 
1639     auto parseElt = [&]() -> ParseResult {
1640       OperandType regionArg, operand;
1641       if (parseRegionArgument(regionArg) || parseEqual() ||
1642           parseOperand(operand))
1643         return failure();
1644       lhs.push_back(regionArg);
1645       rhs.push_back(operand);
1646       return success();
1647     };
1648     return parser.parseCommaSeparatedListUntil(Token::r_paren, parseElt);
1649   }
1650 
1651 private:
1652   /// The source location of the operation name.
1653   SMLoc nameLoc;
1654 
1655   /// Information about the result name specifiers.
1656   ArrayRef<OperationParser::ResultRecord> resultIDs;
1657 
1658   /// The abstract information of the operation.
1659   const AbstractOperation *opDefinition;
1660 
1661   /// The main operation parser.
1662   OperationParser &parser;
1663 
1664   /// A flag that indicates if any errors were emitted during parsing.
1665   bool emittedError = false;
1666 };
1667 } // end anonymous namespace.
1668 
1669 Operation *
1670 OperationParser::parseCustomOperation(ArrayRef<ResultRecord> resultIDs) {
1671   llvm::SMLoc opLoc = getToken().getLoc();
1672   StringRef opName = getTokenSpelling();
1673 
1674   auto *opDefinition = AbstractOperation::lookup(opName, getContext());
1675   if (!opDefinition) {
1676     if (opName.contains('.')) {
1677       // This op has a dialect, we try to check if we can register it in the
1678       // context on the fly.
1679       StringRef dialectName = opName.split('.').first;
1680       if (!getContext()->getLoadedDialect(dialectName) &&
1681           getContext()->getOrLoadDialect(dialectName)) {
1682         opDefinition = AbstractOperation::lookup(opName, getContext());
1683       }
1684     } else {
1685       // If the operation name has no namespace prefix we treat it as a standard
1686       // operation and prefix it with "std".
1687       // TODO: Would it be better to just build a mapping of the registered
1688       // operations in the standard dialect?
1689       if (getContext()->getOrLoadDialect("std"))
1690         opDefinition = AbstractOperation::lookup(Twine("std." + opName).str(),
1691                                                  getContext());
1692     }
1693   }
1694 
1695   if (!opDefinition) {
1696     emitError(opLoc) << "custom op '" << opName << "' is unknown";
1697     return nullptr;
1698   }
1699 
1700   consumeToken();
1701 
1702   // If the custom op parser crashes, produce some indication to help
1703   // debugging.
1704   std::string opNameStr = opName.str();
1705   llvm::PrettyStackTraceFormat fmt("MLIR Parser: custom op parser '%s'",
1706                                    opNameStr.c_str());
1707 
1708   // Get location information for the operation.
1709   auto srcLocation = getEncodedSourceLocation(opLoc);
1710 
1711   // Have the op implementation take a crack and parsing this.
1712   OperationState opState(srcLocation, opDefinition->name);
1713   CleanupOpStateRegions guard{opState};
1714   CustomOpAsmParser opAsmParser(opLoc, resultIDs, opDefinition, *this);
1715   if (opAsmParser.parseOperation(opState))
1716     return nullptr;
1717 
1718   // If it emitted an error, we failed.
1719   if (opAsmParser.didEmitError())
1720     return nullptr;
1721 
1722   // Otherwise, create the operation and try to parse a location for it.
1723   Operation *op = opBuilder.createOperation(opState);
1724   if (parseTrailingOperationLocation(op))
1725     return nullptr;
1726   return op;
1727 }
1728 
1729 ParseResult OperationParser::parseTrailingOperationLocation(Operation *op) {
1730   // If there is a 'loc' we parse a trailing location.
1731   if (!consumeIf(Token::kw_loc))
1732     return success();
1733   if (parseToken(Token::l_paren, "expected '(' in location"))
1734     return failure();
1735   Token tok = getToken();
1736 
1737   // Check to see if we are parsing a location alias.
1738   LocationAttr directLoc;
1739   if (tok.is(Token::hash_identifier)) {
1740     consumeToken();
1741 
1742     StringRef identifier = tok.getSpelling().drop_front();
1743     if (identifier.contains('.')) {
1744       return emitError(tok.getLoc())
1745              << "expected location, but found dialect attribute: '#"
1746              << identifier << "'";
1747     }
1748 
1749     // If this alias can be resolved, do it now.
1750     Attribute attr =
1751         getState().symbols.attributeAliasDefinitions.lookup(identifier);
1752     if (attr) {
1753       if (!(directLoc = attr.dyn_cast<LocationAttr>()))
1754         return emitError(tok.getLoc())
1755                << "expected location, but found '" << attr << "'";
1756     } else {
1757       // Otherwise, remember this operation and resolve its location later.
1758       opsWithDeferredLocs.emplace_back(op, tok);
1759     }
1760 
1761     // Otherwise, we parse the location directly.
1762   } else if (parseLocationInstance(directLoc)) {
1763     return failure();
1764   }
1765 
1766   if (parseToken(Token::r_paren, "expected ')' in location"))
1767     return failure();
1768 
1769   if (directLoc)
1770     op->setLoc(directLoc);
1771   return success();
1772 }
1773 
1774 //===----------------------------------------------------------------------===//
1775 // Region Parsing
1776 //===----------------------------------------------------------------------===//
1777 
1778 /// Region.
1779 ///
1780 ///   region ::= '{' region-body
1781 ///
1782 ParseResult OperationParser::parseRegion(
1783     Region &region,
1784     ArrayRef<std::pair<OperationParser::SSAUseInfo, Type>> entryArguments,
1785     bool isIsolatedNameScope) {
1786   // Parse the '{'.
1787   if (parseToken(Token::l_brace, "expected '{' to begin a region"))
1788     return failure();
1789 
1790   // Check for an empty region.
1791   if (entryArguments.empty() && consumeIf(Token::r_brace))
1792     return success();
1793   auto currentPt = opBuilder.saveInsertionPoint();
1794 
1795   // Push a new named value scope.
1796   pushSSANameScope(isIsolatedNameScope);
1797 
1798   // Parse the first block directly to allow for it to be unnamed.
1799   auto owning_block = std::make_unique<Block>();
1800   Block *block = owning_block.get();
1801 
1802   // Add arguments to the entry block.
1803   if (!entryArguments.empty()) {
1804     for (auto &placeholderArgPair : entryArguments) {
1805       auto &argInfo = placeholderArgPair.first;
1806       // Ensure that the argument was not already defined.
1807       if (auto defLoc = getReferenceLoc(argInfo.name, argInfo.number)) {
1808         return emitError(argInfo.loc, "region entry argument '" + argInfo.name +
1809                                           "' is already in use")
1810                    .attachNote(getEncodedSourceLocation(*defLoc))
1811                << "previously referenced here";
1812       }
1813       if (addDefinition(placeholderArgPair.first,
1814                         block->addArgument(placeholderArgPair.second))) {
1815         return failure();
1816       }
1817     }
1818 
1819     // If we had named arguments, then don't allow a block name.
1820     if (getToken().is(Token::caret_identifier))
1821       return emitError("invalid block name in region with named arguments");
1822   }
1823 
1824   if (parseBlock(block)) {
1825     return failure();
1826   }
1827 
1828   // Verify that no other arguments were parsed.
1829   if (!entryArguments.empty() &&
1830       block->getNumArguments() > entryArguments.size()) {
1831     return emitError("entry block arguments were already defined");
1832   }
1833 
1834   // Parse the rest of the region.
1835   region.push_back(owning_block.release());
1836   if (parseRegionBody(region))
1837     return failure();
1838 
1839   // Pop the SSA value scope for this region.
1840   if (popSSANameScope())
1841     return failure();
1842 
1843   // Reset the original insertion point.
1844   opBuilder.restoreInsertionPoint(currentPt);
1845   return success();
1846 }
1847 
1848 /// Region.
1849 ///
1850 ///   region-body ::= block* '}'
1851 ///
1852 ParseResult OperationParser::parseRegionBody(Region &region) {
1853   // Parse the list of blocks.
1854   while (!consumeIf(Token::r_brace)) {
1855     Block *newBlock = nullptr;
1856     if (parseBlock(newBlock))
1857       return failure();
1858     region.push_back(newBlock);
1859   }
1860   return success();
1861 }
1862 
1863 //===----------------------------------------------------------------------===//
1864 // Block Parsing
1865 //===----------------------------------------------------------------------===//
1866 
1867 /// Block declaration.
1868 ///
1869 ///   block ::= block-label? operation*
1870 ///   block-label    ::= block-id block-arg-list? `:`
1871 ///   block-id       ::= caret-id
1872 ///   block-arg-list ::= `(` ssa-id-and-type-list? `)`
1873 ///
1874 ParseResult OperationParser::parseBlock(Block *&block) {
1875   // The first block of a region may already exist, if it does the caret
1876   // identifier is optional.
1877   if (block && getToken().isNot(Token::caret_identifier))
1878     return parseBlockBody(block);
1879 
1880   SMLoc nameLoc = getToken().getLoc();
1881   auto name = getTokenSpelling();
1882   if (parseToken(Token::caret_identifier, "expected block name"))
1883     return failure();
1884 
1885   block = defineBlockNamed(name, nameLoc, block);
1886 
1887   // Fail if the block was already defined.
1888   if (!block)
1889     return emitError(nameLoc, "redefinition of block '") << name << "'";
1890 
1891   // If an argument list is present, parse it.
1892   if (consumeIf(Token::l_paren)) {
1893     SmallVector<BlockArgument, 8> bbArgs;
1894     if (parseOptionalBlockArgList(bbArgs, block) ||
1895         parseToken(Token::r_paren, "expected ')' to end argument list"))
1896       return failure();
1897   }
1898 
1899   if (parseToken(Token::colon, "expected ':' after block name"))
1900     return failure();
1901 
1902   return parseBlockBody(block);
1903 }
1904 
1905 ParseResult OperationParser::parseBlockBody(Block *block) {
1906   // Set the insertion point to the end of the block to parse.
1907   opBuilder.setInsertionPointToEnd(block);
1908 
1909   // Parse the list of operations that make up the body of the block.
1910   while (getToken().isNot(Token::caret_identifier, Token::r_brace))
1911     if (parseOperation())
1912       return failure();
1913 
1914   return success();
1915 }
1916 
1917 /// Get the block with the specified name, creating it if it doesn't already
1918 /// exist.  The location specified is the point of use, which allows
1919 /// us to diagnose references to blocks that are not defined precisely.
1920 Block *OperationParser::getBlockNamed(StringRef name, SMLoc loc) {
1921   auto &blockAndLoc = getBlockInfoByName(name);
1922   if (!blockAndLoc.first) {
1923     blockAndLoc = {new Block(), loc};
1924     insertForwardRef(blockAndLoc.first, loc);
1925   }
1926 
1927   return blockAndLoc.first;
1928 }
1929 
1930 /// Define the block with the specified name. Returns the Block* or nullptr in
1931 /// the case of redefinition.
1932 Block *OperationParser::defineBlockNamed(StringRef name, SMLoc loc,
1933                                          Block *existing) {
1934   auto &blockAndLoc = getBlockInfoByName(name);
1935   if (!blockAndLoc.first) {
1936     // If the caller provided a block, use it.  Otherwise create a new one.
1937     if (!existing)
1938       existing = new Block();
1939     blockAndLoc.first = existing;
1940     blockAndLoc.second = loc;
1941     return blockAndLoc.first;
1942   }
1943 
1944   // Forward declarations are removed once defined, so if we are defining a
1945   // existing block and it is not a forward declaration, then it is a
1946   // redeclaration.
1947   if (!eraseForwardRef(blockAndLoc.first))
1948     return nullptr;
1949   return blockAndLoc.first;
1950 }
1951 
1952 /// Parse a (possibly empty) list of SSA operands with types as block arguments.
1953 ///
1954 ///   ssa-id-and-type-list ::= ssa-id-and-type (`,` ssa-id-and-type)*
1955 ///
1956 ParseResult OperationParser::parseOptionalBlockArgList(
1957     SmallVectorImpl<BlockArgument> &results, Block *owner) {
1958   if (getToken().is(Token::r_brace))
1959     return success();
1960 
1961   // If the block already has arguments, then we're handling the entry block.
1962   // Parse and register the names for the arguments, but do not add them.
1963   bool definingExistingArgs = owner->getNumArguments() != 0;
1964   unsigned nextArgument = 0;
1965 
1966   return parseCommaSeparatedList([&]() -> ParseResult {
1967     return parseSSADefOrUseAndType(
1968         [&](SSAUseInfo useInfo, Type type) -> ParseResult {
1969           // If this block did not have existing arguments, define a new one.
1970           if (!definingExistingArgs)
1971             return addDefinition(useInfo, owner->addArgument(type));
1972 
1973           // Otherwise, ensure that this argument has already been created.
1974           if (nextArgument >= owner->getNumArguments())
1975             return emitError("too many arguments specified in argument list");
1976 
1977           // Finally, make sure the existing argument has the correct type.
1978           auto arg = owner->getArgument(nextArgument++);
1979           if (arg.getType() != type)
1980             return emitError("argument and block argument type mismatch");
1981           return addDefinition(useInfo, arg);
1982         });
1983   });
1984 }
1985 
1986 //===----------------------------------------------------------------------===//
1987 // Top-level entity parsing.
1988 //===----------------------------------------------------------------------===//
1989 
1990 namespace {
1991 /// This parser handles entities that are only valid at the top level of the
1992 /// file.
1993 class TopLevelOperationParser : public Parser {
1994 public:
1995   explicit TopLevelOperationParser(ParserState &state) : Parser(state) {}
1996 
1997   /// Parse a set of operations into the end of the given Block.
1998   ParseResult parse(Block *topLevelBlock, Location parserLoc);
1999 
2000 private:
2001   /// Parse an attribute alias declaration.
2002   ParseResult parseAttributeAliasDef();
2003 
2004   /// Parse an attribute alias declaration.
2005   ParseResult parseTypeAliasDef();
2006 };
2007 } // end anonymous namespace
2008 
2009 /// Parses an attribute alias declaration.
2010 ///
2011 ///   attribute-alias-def ::= '#' alias-name `=` attribute-value
2012 ///
2013 ParseResult TopLevelOperationParser::parseAttributeAliasDef() {
2014   assert(getToken().is(Token::hash_identifier));
2015   StringRef aliasName = getTokenSpelling().drop_front();
2016 
2017   // Check for redefinitions.
2018   if (getState().symbols.attributeAliasDefinitions.count(aliasName) > 0)
2019     return emitError("redefinition of attribute alias id '" + aliasName + "'");
2020 
2021   // Make sure this isn't invading the dialect attribute namespace.
2022   if (aliasName.contains('.'))
2023     return emitError("attribute names with a '.' are reserved for "
2024                      "dialect-defined names");
2025 
2026   consumeToken(Token::hash_identifier);
2027 
2028   // Parse the '='.
2029   if (parseToken(Token::equal, "expected '=' in attribute alias definition"))
2030     return failure();
2031 
2032   // Parse the attribute value.
2033   Attribute attr = parseAttribute();
2034   if (!attr)
2035     return failure();
2036 
2037   getState().symbols.attributeAliasDefinitions[aliasName] = attr;
2038   return success();
2039 }
2040 
2041 /// Parse a type alias declaration.
2042 ///
2043 ///   type-alias-def ::= '!' alias-name `=` 'type' type
2044 ///
2045 ParseResult TopLevelOperationParser::parseTypeAliasDef() {
2046   assert(getToken().is(Token::exclamation_identifier));
2047   StringRef aliasName = getTokenSpelling().drop_front();
2048 
2049   // Check for redefinitions.
2050   if (getState().symbols.typeAliasDefinitions.count(aliasName) > 0)
2051     return emitError("redefinition of type alias id '" + aliasName + "'");
2052 
2053   // Make sure this isn't invading the dialect type namespace.
2054   if (aliasName.contains('.'))
2055     return emitError("type names with a '.' are reserved for "
2056                      "dialect-defined names");
2057 
2058   consumeToken(Token::exclamation_identifier);
2059 
2060   // Parse the '=' and 'type'.
2061   if (parseToken(Token::equal, "expected '=' in type alias definition") ||
2062       parseToken(Token::kw_type, "expected 'type' in type alias definition"))
2063     return failure();
2064 
2065   // Parse the type.
2066   Type aliasedType = parseType();
2067   if (!aliasedType)
2068     return failure();
2069 
2070   // Register this alias with the parser state.
2071   getState().symbols.typeAliasDefinitions.try_emplace(aliasName, aliasedType);
2072   return success();
2073 }
2074 
2075 ParseResult TopLevelOperationParser::parse(Block *topLevelBlock,
2076                                            Location parserLoc) {
2077   // Create a top-level operation to contain the parsed state.
2078   OwningOpRef<Operation *> topLevelOp(ModuleOp::create(parserLoc));
2079   OperationParser opParser(getState(), topLevelOp.get());
2080   while (true) {
2081     switch (getToken().getKind()) {
2082     default:
2083       // Parse a top-level operation.
2084       if (opParser.parseOperation())
2085         return failure();
2086       break;
2087 
2088     // If we got to the end of the file, then we're done.
2089     case Token::eof: {
2090       if (opParser.finalize())
2091         return failure();
2092 
2093       // Verify that the parsed operations are valid.
2094       if (failed(verify(topLevelOp.get())))
2095         return failure();
2096 
2097       // Splice the blocks of the parsed operation over to the provided
2098       // top-level block.
2099       auto &parsedOps = (*topLevelOp)->getRegion(0).front().getOperations();
2100       auto &destOps = topLevelBlock->getOperations();
2101       destOps.splice(destOps.empty() ? destOps.end() : std::prev(destOps.end()),
2102                      parsedOps, parsedOps.begin(), std::prev(parsedOps.end()));
2103       return success();
2104     }
2105 
2106     // If we got an error token, then the lexer already emitted an error, just
2107     // stop.  Someday we could introduce error recovery if there was demand
2108     // for it.
2109     case Token::error:
2110       return failure();
2111 
2112     // Parse an attribute alias.
2113     case Token::hash_identifier:
2114       if (parseAttributeAliasDef())
2115         return failure();
2116       break;
2117 
2118     // Parse a type alias.
2119     case Token::exclamation_identifier:
2120       if (parseTypeAliasDef())
2121         return failure();
2122       break;
2123     }
2124   }
2125 }
2126 
2127 //===----------------------------------------------------------------------===//
2128 
2129 LogicalResult mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
2130                                     Block *block, MLIRContext *context,
2131                                     LocationAttr *sourceFileLoc) {
2132   const auto *sourceBuf = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
2133 
2134   Location parserLoc = FileLineColLoc::get(
2135       context, sourceBuf->getBufferIdentifier(), /*line=*/0, /*column=*/0);
2136   if (sourceFileLoc)
2137     *sourceFileLoc = parserLoc;
2138 
2139   SymbolState aliasState;
2140   ParserState state(sourceMgr, context, aliasState);
2141   return TopLevelOperationParser(state).parse(block, parserLoc);
2142 }
2143 
2144 LogicalResult mlir::parseSourceFile(llvm::StringRef filename, Block *block,
2145                                     MLIRContext *context,
2146                                     LocationAttr *sourceFileLoc) {
2147   llvm::SourceMgr sourceMgr;
2148   return parseSourceFile(filename, sourceMgr, block, context, sourceFileLoc);
2149 }
2150 
2151 LogicalResult mlir::parseSourceFile(llvm::StringRef filename,
2152                                     llvm::SourceMgr &sourceMgr, Block *block,
2153                                     MLIRContext *context,
2154                                     LocationAttr *sourceFileLoc) {
2155   if (sourceMgr.getNumBuffers() != 0) {
2156     // TODO: Extend to support multiple buffers.
2157     return emitError(mlir::UnknownLoc::get(context),
2158                      "only main buffer parsed at the moment");
2159   }
2160   auto file_or_err = llvm::MemoryBuffer::getFileOrSTDIN(filename);
2161   if (std::error_code error = file_or_err.getError())
2162     return emitError(mlir::UnknownLoc::get(context),
2163                      "could not open input file " + filename);
2164 
2165   // Load the MLIR source file.
2166   sourceMgr.AddNewSourceBuffer(std::move(*file_or_err), llvm::SMLoc());
2167   return parseSourceFile(sourceMgr, block, context, sourceFileLoc);
2168 }
2169 
2170 LogicalResult mlir::parseSourceString(llvm::StringRef sourceStr, Block *block,
2171                                       MLIRContext *context,
2172                                       LocationAttr *sourceFileLoc) {
2173   auto memBuffer = MemoryBuffer::getMemBuffer(sourceStr);
2174   if (!memBuffer)
2175     return failure();
2176 
2177   SourceMgr sourceMgr;
2178   sourceMgr.AddNewSourceBuffer(std::move(memBuffer), SMLoc());
2179   return parseSourceFile(sourceMgr, block, context, sourceFileLoc);
2180 }
2181