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