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