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