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 } // 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 (const auto &entry : resultIDs) {
1237       if (resultNo < std::get<1>(entry)) {
1238         // Don't pass on the leading %.
1239         StringRef name = std::get<0>(entry).drop_front();
1240         return {name, resultNo};
1241       }
1242       resultNo -= std::get<1>(entry);
1243     }
1244 
1245     // Invalid result number.
1246     return {"", ~0U};
1247   }
1248 
1249   /// Return the number of declared SSA results.  This returns 4 for the foo.op
1250   /// example in the comment for getResultName.
1251   size_t getNumResults() const override {
1252     size_t count = 0;
1253     for (auto &entry : resultIDs)
1254       count += std::get<1>(entry);
1255     return count;
1256   }
1257 
1258   /// Emit a diagnostic at the specified location and return failure.
1259   InFlightDiagnostic emitError(llvm::SMLoc loc, const Twine &message) override {
1260     return AsmParserImpl<OpAsmParser>::emitError(loc, "custom op '" + opName +
1261                                                           "' " + message);
1262   }
1263 
1264   //===--------------------------------------------------------------------===//
1265   // Operand Parsing
1266   //===--------------------------------------------------------------------===//
1267 
1268   /// Parse a single operand.
1269   ParseResult parseOperand(OperandType &result) override {
1270     OperationParser::SSAUseInfo useInfo;
1271     if (parser.parseSSAUse(useInfo))
1272       return failure();
1273 
1274     result = {useInfo.loc, useInfo.name, useInfo.number};
1275     return success();
1276   }
1277 
1278   /// Parse a single operand if present.
1279   OptionalParseResult parseOptionalOperand(OperandType &result) override {
1280     if (parser.getToken().is(Token::percent_identifier))
1281       return parseOperand(result);
1282     return llvm::None;
1283   }
1284 
1285   /// Parse zero or more SSA comma-separated operand references with a specified
1286   /// surrounding delimiter, and an optional required operand count.
1287   ParseResult parseOperandList(SmallVectorImpl<OperandType> &result,
1288                                int requiredOperandCount = -1,
1289                                Delimiter delimiter = Delimiter::None) override {
1290     return parseOperandOrRegionArgList(result, /*isOperandList=*/true,
1291                                        requiredOperandCount, delimiter);
1292   }
1293 
1294   /// Parse zero or more SSA comma-separated operand or region arguments with
1295   ///  optional surrounding delimiter and required operand count.
1296   ParseResult
1297   parseOperandOrRegionArgList(SmallVectorImpl<OperandType> &result,
1298                               bool isOperandList, int requiredOperandCount = -1,
1299                               Delimiter delimiter = Delimiter::None) {
1300     auto startLoc = parser.getToken().getLoc();
1301 
1302     // The no-delimiter case has some special handling for better diagnostics.
1303     if (delimiter == Delimiter::None) {
1304       // parseCommaSeparatedList doesn't handle the missing case for "none",
1305       // so we handle it custom here.
1306       if (parser.getToken().isNot(Token::percent_identifier)) {
1307         // If we didn't require any operands or required exactly zero (weird)
1308         // then this is success.
1309         if (requiredOperandCount == -1 || requiredOperandCount == 0)
1310           return success();
1311 
1312         // Otherwise, try to produce a nice error message.
1313         if (parser.getToken().is(Token::l_paren) ||
1314             parser.getToken().is(Token::l_square))
1315           return emitError(startLoc, "unexpected delimiter");
1316         return emitError(startLoc, "invalid operand");
1317       }
1318     }
1319 
1320     auto parseOneOperand = [&]() -> ParseResult {
1321       OperandType operandOrArg;
1322       if (isOperandList ? parseOperand(operandOrArg)
1323                         : parseRegionArgument(operandOrArg))
1324         return failure();
1325       result.push_back(operandOrArg);
1326       return success();
1327     };
1328 
1329     if (parseCommaSeparatedList(delimiter, parseOneOperand, " in operand list"))
1330       return failure();
1331 
1332     // Check that we got the expected # of elements.
1333     if (requiredOperandCount != -1 &&
1334         result.size() != static_cast<size_t>(requiredOperandCount))
1335       return emitError(startLoc, "expected ")
1336              << requiredOperandCount << " operands";
1337     return success();
1338   }
1339 
1340   /// Parse zero or more trailing SSA comma-separated trailing operand
1341   /// references with a specified surrounding delimiter, and an optional
1342   /// required operand count. A leading comma is expected before the operands.
1343   ParseResult parseTrailingOperandList(SmallVectorImpl<OperandType> &result,
1344                                        int requiredOperandCount,
1345                                        Delimiter delimiter) override {
1346     if (parser.getToken().is(Token::comma)) {
1347       parseComma();
1348       return parseOperandList(result, requiredOperandCount, delimiter);
1349     }
1350     if (requiredOperandCount != -1)
1351       return emitError(parser.getToken().getLoc(), "expected ")
1352              << requiredOperandCount << " operands";
1353     return success();
1354   }
1355 
1356   /// Resolve an operand to an SSA value, emitting an error on failure.
1357   ParseResult resolveOperand(const OperandType &operand, Type type,
1358                              SmallVectorImpl<Value> &result) override {
1359     OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number,
1360                                                operand.location};
1361     if (auto value = parser.resolveSSAUse(operandInfo, type)) {
1362       result.push_back(value);
1363       return success();
1364     }
1365     return failure();
1366   }
1367 
1368   /// Parse an AffineMap of SSA ids.
1369   ParseResult parseAffineMapOfSSAIds(SmallVectorImpl<OperandType> &operands,
1370                                      Attribute &mapAttr, StringRef attrName,
1371                                      NamedAttrList &attrs,
1372                                      Delimiter delimiter) override {
1373     SmallVector<OperandType, 2> dimOperands;
1374     SmallVector<OperandType, 1> symOperands;
1375 
1376     auto parseElement = [&](bool isSymbol) -> ParseResult {
1377       OperandType operand;
1378       if (parseOperand(operand))
1379         return failure();
1380       if (isSymbol)
1381         symOperands.push_back(operand);
1382       else
1383         dimOperands.push_back(operand);
1384       return success();
1385     };
1386 
1387     AffineMap map;
1388     if (parser.parseAffineMapOfSSAIds(map, parseElement, delimiter))
1389       return failure();
1390     // Add AffineMap attribute.
1391     if (map) {
1392       mapAttr = AffineMapAttr::get(map);
1393       attrs.push_back(parser.builder.getNamedAttr(attrName, mapAttr));
1394     }
1395 
1396     // Add dim operands before symbol operands in 'operands'.
1397     operands.assign(dimOperands.begin(), dimOperands.end());
1398     operands.append(symOperands.begin(), symOperands.end());
1399     return success();
1400   }
1401 
1402   /// Parse an AffineExpr of SSA ids.
1403   ParseResult
1404   parseAffineExprOfSSAIds(SmallVectorImpl<OperandType> &dimOperands,
1405                           SmallVectorImpl<OperandType> &symbOperands,
1406                           AffineExpr &expr) override {
1407     auto parseElement = [&](bool isSymbol) -> ParseResult {
1408       OperandType operand;
1409       if (parseOperand(operand))
1410         return failure();
1411       if (isSymbol)
1412         symbOperands.push_back(operand);
1413       else
1414         dimOperands.push_back(operand);
1415       return success();
1416     };
1417 
1418     return parser.parseAffineExprOfSSAIds(expr, parseElement);
1419   }
1420 
1421   //===--------------------------------------------------------------------===//
1422   // Region Parsing
1423   //===--------------------------------------------------------------------===//
1424 
1425   /// Parse a region that takes `arguments` of `argTypes` types.  This
1426   /// effectively defines the SSA values of `arguments` and assigns their type.
1427   ParseResult parseRegion(Region &region, ArrayRef<OperandType> arguments,
1428                           ArrayRef<Type> argTypes,
1429                           bool enableNameShadowing) override {
1430     assert(arguments.size() == argTypes.size() &&
1431            "mismatching number of arguments and types");
1432 
1433     SmallVector<std::pair<OperationParser::SSAUseInfo, Type>, 2>
1434         regionArguments;
1435     for (auto pair : llvm::zip(arguments, argTypes)) {
1436       const OperandType &operand = std::get<0>(pair);
1437       Type type = std::get<1>(pair);
1438       OperationParser::SSAUseInfo operandInfo = {operand.name, operand.number,
1439                                                  operand.location};
1440       regionArguments.emplace_back(operandInfo, type);
1441     }
1442 
1443     // Try to parse the region.
1444     (void)isIsolatedFromAbove;
1445     assert((!enableNameShadowing || isIsolatedFromAbove) &&
1446            "name shadowing is only allowed on isolated regions");
1447     if (parser.parseRegion(region, regionArguments, enableNameShadowing))
1448       return failure();
1449     return success();
1450   }
1451 
1452   /// Parses a region if present.
1453   OptionalParseResult parseOptionalRegion(Region &region,
1454                                           ArrayRef<OperandType> arguments,
1455                                           ArrayRef<Type> argTypes,
1456                                           bool enableNameShadowing) override {
1457     if (parser.getToken().isNot(Token::l_brace))
1458       return llvm::None;
1459     return parseRegion(region, arguments, argTypes, enableNameShadowing);
1460   }
1461 
1462   /// Parses a region if present. If the region is present, a new region is
1463   /// allocated and placed in `region`. If no region is present, `region`
1464   /// remains untouched.
1465   OptionalParseResult
1466   parseOptionalRegion(std::unique_ptr<Region> &region,
1467                       ArrayRef<OperandType> arguments, ArrayRef<Type> argTypes,
1468                       bool enableNameShadowing = false) override {
1469     if (parser.getToken().isNot(Token::l_brace))
1470       return llvm::None;
1471     std::unique_ptr<Region> newRegion = std::make_unique<Region>();
1472     if (parseRegion(*newRegion, arguments, argTypes, enableNameShadowing))
1473       return failure();
1474 
1475     region = std::move(newRegion);
1476     return success();
1477   }
1478 
1479   /// Parse a region argument. The type of the argument will be resolved later
1480   /// by a call to `parseRegion`.
1481   ParseResult parseRegionArgument(OperandType &argument) override {
1482     return parseOperand(argument);
1483   }
1484 
1485   /// Parse a region argument if present.
1486   ParseResult parseOptionalRegionArgument(OperandType &argument) override {
1487     if (parser.getToken().isNot(Token::percent_identifier))
1488       return success();
1489     return parseRegionArgument(argument);
1490   }
1491 
1492   ParseResult
1493   parseRegionArgumentList(SmallVectorImpl<OperandType> &result,
1494                           int requiredOperandCount = -1,
1495                           Delimiter delimiter = Delimiter::None) override {
1496     return parseOperandOrRegionArgList(result, /*isOperandList=*/false,
1497                                        requiredOperandCount, delimiter);
1498   }
1499 
1500   //===--------------------------------------------------------------------===//
1501   // Successor Parsing
1502   //===--------------------------------------------------------------------===//
1503 
1504   /// Parse a single operation successor.
1505   ParseResult parseSuccessor(Block *&dest) override {
1506     return parser.parseSuccessor(dest);
1507   }
1508 
1509   /// Parse an optional operation successor and its operand list.
1510   OptionalParseResult parseOptionalSuccessor(Block *&dest) override {
1511     if (parser.getToken().isNot(Token::caret_identifier))
1512       return llvm::None;
1513     return parseSuccessor(dest);
1514   }
1515 
1516   /// Parse a single operation successor and its operand list.
1517   ParseResult
1518   parseSuccessorAndUseList(Block *&dest,
1519                            SmallVectorImpl<Value> &operands) override {
1520     if (parseSuccessor(dest))
1521       return failure();
1522 
1523     // Handle optional arguments.
1524     if (succeeded(parseOptionalLParen()) &&
1525         (parser.parseOptionalSSAUseAndTypeList(operands) || parseRParen())) {
1526       return failure();
1527     }
1528     return success();
1529   }
1530 
1531   //===--------------------------------------------------------------------===//
1532   // Type Parsing
1533   //===--------------------------------------------------------------------===//
1534 
1535   /// Parse a list of assignments of the form
1536   ///   (%x1 = %y1, %x2 = %y2, ...).
1537   OptionalParseResult
1538   parseOptionalAssignmentList(SmallVectorImpl<OperandType> &lhs,
1539                               SmallVectorImpl<OperandType> &rhs) override {
1540     if (failed(parseOptionalLParen()))
1541       return llvm::None;
1542 
1543     auto parseElt = [&]() -> ParseResult {
1544       OperandType regionArg, operand;
1545       if (parseRegionArgument(regionArg) || parseEqual() ||
1546           parseOperand(operand))
1547         return failure();
1548       lhs.push_back(regionArg);
1549       rhs.push_back(operand);
1550       return success();
1551     };
1552     return parser.parseCommaSeparatedListUntil(Token::r_paren, parseElt);
1553   }
1554 
1555   /// Parse a list of assignments of the form
1556   ///   (%x1 = %y1 : type1, %x2 = %y2 : type2, ...).
1557   OptionalParseResult
1558   parseOptionalAssignmentListWithTypes(SmallVectorImpl<OperandType> &lhs,
1559                                        SmallVectorImpl<OperandType> &rhs,
1560                                        SmallVectorImpl<Type> &types) override {
1561     if (failed(parseOptionalLParen()))
1562       return llvm::None;
1563 
1564     auto parseElt = [&]() -> ParseResult {
1565       OperandType regionArg, operand;
1566       Type type;
1567       if (parseRegionArgument(regionArg) || parseEqual() ||
1568           parseOperand(operand) || parseColon() || parseType(type))
1569         return failure();
1570       lhs.push_back(regionArg);
1571       rhs.push_back(operand);
1572       types.push_back(type);
1573       return success();
1574     };
1575     return parser.parseCommaSeparatedListUntil(Token::r_paren, parseElt);
1576   }
1577 
1578 private:
1579   /// Information about the result name specifiers.
1580   ArrayRef<OperationParser::ResultRecord> resultIDs;
1581 
1582   /// The abstract information of the operation.
1583   function_ref<ParseResult(OpAsmParser &, OperationState &)> parseAssembly;
1584   bool isIsolatedFromAbove;
1585   StringRef opName;
1586 
1587   /// The backing operation parser.
1588   OperationParser &parser;
1589 };
1590 } // namespace
1591 
1592 FailureOr<OperationName> OperationParser::parseCustomOperationName() {
1593   std::string opName = getTokenSpelling().str();
1594   if (opName.empty())
1595     return (emitError("empty operation name is invalid"), failure());
1596 
1597   consumeToken();
1598 
1599   Optional<RegisteredOperationName> opInfo =
1600       RegisteredOperationName::lookup(opName, getContext());
1601   StringRef defaultDialect = getState().defaultDialectStack.back();
1602   Dialect *dialect = nullptr;
1603   if (opInfo) {
1604     dialect = &opInfo->getDialect();
1605   } else {
1606     if (StringRef(opName).contains('.')) {
1607       // This op has a dialect, we try to check if we can register it in the
1608       // context on the fly.
1609       StringRef dialectName = StringRef(opName).split('.').first;
1610       dialect = getContext()->getLoadedDialect(dialectName);
1611       if (!dialect && (dialect = getContext()->getOrLoadDialect(dialectName)))
1612         opInfo = RegisteredOperationName::lookup(opName, getContext());
1613     } else {
1614       // If the operation name has no namespace prefix we lookup the current
1615       // default dialect (set through OpAsmOpInterface).
1616       opInfo = RegisteredOperationName::lookup(
1617           Twine(defaultDialect + "." + opName).str(), getContext());
1618       if (!opInfo && getContext()->getOrLoadDialect("std")) {
1619         opInfo = RegisteredOperationName::lookup(Twine("std." + opName).str(),
1620                                                  getContext());
1621       }
1622       if (opInfo) {
1623         dialect = &opInfo->getDialect();
1624         opName = opInfo->getStringRef().str();
1625       } else if (!defaultDialect.empty()) {
1626         dialect = getContext()->getOrLoadDialect(defaultDialect);
1627         opName = (defaultDialect + "." + opName).str();
1628       }
1629     }
1630   }
1631 
1632   return OperationName(opName, getContext());
1633 }
1634 
1635 Operation *
1636 OperationParser::parseCustomOperation(ArrayRef<ResultRecord> resultIDs) {
1637   llvm::SMLoc opLoc = getToken().getLoc();
1638 
1639   FailureOr<OperationName> opNameInfo = parseCustomOperationName();
1640   if (failed(opNameInfo))
1641     return nullptr;
1642 
1643   StringRef opName = opNameInfo->getStringRef();
1644   Dialect *dialect = opNameInfo->getDialect();
1645   Optional<RegisteredOperationName> opInfo = opNameInfo->getRegisteredInfo();
1646 
1647   // This is the actual hook for the custom op parsing, usually implemented by
1648   // the op itself (`Op::parse()`). We retrieve it either from the
1649   // RegisteredOperationName or from the Dialect.
1650   function_ref<ParseResult(OpAsmParser &, OperationState &)> parseAssemblyFn;
1651   bool isIsolatedFromAbove = false;
1652 
1653   StringRef defaultDialect = "";
1654   if (opInfo) {
1655     parseAssemblyFn = opInfo->getParseAssemblyFn();
1656     isIsolatedFromAbove = opInfo->hasTrait<OpTrait::IsIsolatedFromAbove>();
1657     auto *iface = opInfo->getInterface<OpAsmOpInterface>();
1658     if (iface && !iface->getDefaultDialect().empty())
1659       defaultDialect = iface->getDefaultDialect();
1660   } else {
1661     Optional<Dialect::ParseOpHook> dialectHook;
1662     if (dialect)
1663       dialectHook = dialect->getParseOperationHook(opName);
1664     if (!dialectHook.hasValue()) {
1665       emitError(opLoc) << "custom op '" << opName << "' is unknown";
1666       return nullptr;
1667     }
1668     parseAssemblyFn = *dialectHook;
1669   }
1670   getState().defaultDialectStack.push_back(defaultDialect);
1671   auto restoreDefaultDialect = llvm::make_scope_exit(
1672       [&]() { getState().defaultDialectStack.pop_back(); });
1673 
1674   // If the custom op parser crashes, produce some indication to help
1675   // debugging.
1676   llvm::PrettyStackTraceFormat fmt("MLIR Parser: custom op parser '%s'",
1677                                    opNameInfo->getIdentifier().data());
1678 
1679   // Get location information for the operation.
1680   auto srcLocation = getEncodedSourceLocation(opLoc);
1681   OperationState opState(srcLocation, *opNameInfo);
1682 
1683   // If we are populating the parser state, start a new operation definition.
1684   if (state.asmState)
1685     state.asmState->startOperationDefinition(opState.name);
1686 
1687   // Have the op implementation take a crack and parsing this.
1688   CleanupOpStateRegions guard{opState};
1689   CustomOpAsmParser opAsmParser(opLoc, resultIDs, parseAssemblyFn,
1690                                 isIsolatedFromAbove, opName, *this);
1691   if (opAsmParser.parseOperation(opState))
1692     return nullptr;
1693 
1694   // If it emitted an error, we failed.
1695   if (opAsmParser.didEmitError())
1696     return nullptr;
1697 
1698   // Otherwise, create the operation and try to parse a location for it.
1699   Operation *op = opBuilder.createOperation(opState);
1700   if (parseTrailingLocationSpecifier(op))
1701     return nullptr;
1702   return op;
1703 }
1704 
1705 ParseResult
1706 OperationParser::parseTrailingLocationSpecifier(OpOrArgument opOrArgument) {
1707   // If there is a 'loc' we parse a trailing location.
1708   if (!consumeIf(Token::kw_loc))
1709     return success();
1710   if (parseToken(Token::l_paren, "expected '(' in location"))
1711     return failure();
1712   Token tok = getToken();
1713 
1714   // Check to see if we are parsing a location alias.
1715   LocationAttr directLoc;
1716   if (tok.is(Token::hash_identifier)) {
1717     consumeToken();
1718 
1719     StringRef identifier = tok.getSpelling().drop_front();
1720     if (identifier.contains('.')) {
1721       return emitError(tok.getLoc())
1722              << "expected location, but found dialect attribute: '#"
1723              << identifier << "'";
1724     }
1725 
1726     // If this alias can be resolved, do it now.
1727     Attribute attr = state.symbols.attributeAliasDefinitions.lookup(identifier);
1728     if (attr) {
1729       if (!(directLoc = attr.dyn_cast<LocationAttr>()))
1730         return emitError(tok.getLoc())
1731                << "expected location, but found '" << attr << "'";
1732     } else {
1733       // Otherwise, remember this operation and resolve its location later.
1734       opsAndArgumentsWithDeferredLocs.emplace_back(opOrArgument, tok);
1735     }
1736 
1737     // Otherwise, we parse the location directly.
1738   } else if (parseLocationInstance(directLoc)) {
1739     return failure();
1740   }
1741 
1742   if (parseToken(Token::r_paren, "expected ')' in location"))
1743     return failure();
1744 
1745   if (directLoc) {
1746     if (auto *op = opOrArgument.dyn_cast<Operation *>())
1747       op->setLoc(directLoc);
1748     else
1749       opOrArgument.get<BlockArgument>().setLoc(directLoc);
1750   }
1751   return success();
1752 }
1753 
1754 //===----------------------------------------------------------------------===//
1755 // Region Parsing
1756 //===----------------------------------------------------------------------===//
1757 
1758 ParseResult OperationParser::parseRegion(
1759     Region &region,
1760     ArrayRef<std::pair<OperationParser::SSAUseInfo, Type>> entryArguments,
1761     bool isIsolatedNameScope) {
1762   // Parse the '{'.
1763   Token lBraceTok = getToken();
1764   if (parseToken(Token::l_brace, "expected '{' to begin a region"))
1765     return failure();
1766 
1767   // If we are populating the parser state, start a new region definition.
1768   if (state.asmState)
1769     state.asmState->startRegionDefinition();
1770 
1771   // Parse the region body.
1772   if ((!entryArguments.empty() || getToken().isNot(Token::r_brace)) &&
1773       parseRegionBody(region, lBraceTok.getLoc(), entryArguments,
1774                       isIsolatedNameScope)) {
1775     return failure();
1776   }
1777   consumeToken(Token::r_brace);
1778 
1779   // If we are populating the parser state, finalize this region.
1780   if (state.asmState)
1781     state.asmState->finalizeRegionDefinition();
1782 
1783   return success();
1784 }
1785 
1786 ParseResult OperationParser::parseRegionBody(
1787     Region &region, llvm::SMLoc startLoc,
1788     ArrayRef<std::pair<OperationParser::SSAUseInfo, Type>> entryArguments,
1789     bool isIsolatedNameScope) {
1790   auto currentPt = opBuilder.saveInsertionPoint();
1791 
1792   // Push a new named value scope.
1793   pushSSANameScope(isIsolatedNameScope);
1794 
1795   // Parse the first block directly to allow for it to be unnamed.
1796   auto owningBlock = std::make_unique<Block>();
1797   Block *block = owningBlock.get();
1798 
1799   // If this block is not defined in the source file, add a definition for it
1800   // now in the assembly state. Blocks with a name will be defined when the name
1801   // is parsed.
1802   if (state.asmState && getToken().isNot(Token::caret_identifier))
1803     state.asmState->addDefinition(block, startLoc);
1804 
1805   // Add arguments to the entry block.
1806   if (!entryArguments.empty()) {
1807     // If we had named arguments, then don't allow a block name.
1808     if (getToken().is(Token::caret_identifier))
1809       return emitError("invalid block name in region with named arguments");
1810 
1811     for (auto &placeholderArgPair : entryArguments) {
1812       auto &argInfo = placeholderArgPair.first;
1813 
1814       // Ensure that the argument was not already defined.
1815       if (auto defLoc = getReferenceLoc(argInfo.name, argInfo.number)) {
1816         return emitError(argInfo.loc, "region entry argument '" + argInfo.name +
1817                                           "' is already in use")
1818                    .attachNote(getEncodedSourceLocation(*defLoc))
1819                << "previously referenced here";
1820       }
1821       auto loc = getEncodedSourceLocation(placeholderArgPair.first.loc);
1822       BlockArgument arg = block->addArgument(placeholderArgPair.second, loc);
1823 
1824       // Add a definition of this arg to the assembly state if provided.
1825       if (state.asmState)
1826         state.asmState->addDefinition(arg, argInfo.loc);
1827 
1828       // Record the definition for this argument.
1829       if (addDefinition(argInfo, arg))
1830         return failure();
1831     }
1832   }
1833 
1834   if (parseBlock(block))
1835     return failure();
1836 
1837   // Verify that no other arguments were parsed.
1838   if (!entryArguments.empty() &&
1839       block->getNumArguments() > entryArguments.size()) {
1840     return emitError("entry block arguments were already defined");
1841   }
1842 
1843   // Parse the rest of the region.
1844   region.push_back(owningBlock.release());
1845   while (getToken().isNot(Token::r_brace)) {
1846     Block *newBlock = nullptr;
1847     if (parseBlock(newBlock))
1848       return failure();
1849     region.push_back(newBlock);
1850   }
1851 
1852   // Pop the SSA value scope for this region.
1853   if (popSSANameScope())
1854     return failure();
1855 
1856   // Reset the original insertion point.
1857   opBuilder.restoreInsertionPoint(currentPt);
1858   return success();
1859 }
1860 
1861 //===----------------------------------------------------------------------===//
1862 // Block Parsing
1863 //===----------------------------------------------------------------------===//
1864 
1865 /// Block declaration.
1866 ///
1867 ///   block ::= block-label? operation*
1868 ///   block-label    ::= block-id block-arg-list? `:`
1869 ///   block-id       ::= caret-id
1870 ///   block-arg-list ::= `(` ssa-id-and-type-list? `)`
1871 ///
1872 ParseResult OperationParser::parseBlock(Block *&block) {
1873   // The first block of a region may already exist, if it does the caret
1874   // identifier is optional.
1875   if (block && getToken().isNot(Token::caret_identifier))
1876     return parseBlockBody(block);
1877 
1878   SMLoc nameLoc = getToken().getLoc();
1879   auto name = getTokenSpelling();
1880   if (parseToken(Token::caret_identifier, "expected block name"))
1881     return failure();
1882 
1883   block = defineBlockNamed(name, nameLoc, block);
1884 
1885   // Fail if the block was already defined.
1886   if (!block)
1887     return emitError(nameLoc, "redefinition of block '") << name << "'";
1888 
1889   // If an argument list is present, parse it.
1890   if (consumeIf(Token::l_paren)) {
1891     if (parseOptionalBlockArgList(block) ||
1892         parseToken(Token::r_paren, "expected ')' to end argument list"))
1893       return failure();
1894   }
1895 
1896   if (parseToken(Token::colon, "expected ':' after block name"))
1897     return failure();
1898 
1899   return parseBlockBody(block);
1900 }
1901 
1902 ParseResult OperationParser::parseBlockBody(Block *block) {
1903   // Set the insertion point to the end of the block to parse.
1904   opBuilder.setInsertionPointToEnd(block);
1905 
1906   // Parse the list of operations that make up the body of the block.
1907   while (getToken().isNot(Token::caret_identifier, Token::r_brace))
1908     if (parseOperation())
1909       return failure();
1910 
1911   return success();
1912 }
1913 
1914 /// Get the block with the specified name, creating it if it doesn't already
1915 /// exist.  The location specified is the point of use, which allows
1916 /// us to diagnose references to blocks that are not defined precisely.
1917 Block *OperationParser::getBlockNamed(StringRef name, SMLoc loc) {
1918   BlockDefinition &blockDef = getBlockInfoByName(name);
1919   if (!blockDef.block) {
1920     blockDef = {new Block(), loc};
1921     insertForwardRef(blockDef.block, blockDef.loc);
1922   }
1923 
1924   // Populate the high level assembly state if necessary.
1925   if (state.asmState)
1926     state.asmState->addUses(blockDef.block, loc);
1927 
1928   return blockDef.block;
1929 }
1930 
1931 /// Define the block with the specified name. Returns the Block* or nullptr in
1932 /// the case of redefinition.
1933 Block *OperationParser::defineBlockNamed(StringRef name, SMLoc loc,
1934                                          Block *existing) {
1935   auto &blockAndLoc = getBlockInfoByName(name);
1936   blockAndLoc.loc = loc;
1937 
1938   // If a block has yet to be set, this is a new definition. If the caller
1939   // provided a block, use it. Otherwise create a new one.
1940   if (!blockAndLoc.block) {
1941     blockAndLoc.block = existing ? existing : new Block();
1942 
1943     // Otherwise, the block has a forward declaration. Forward declarations are
1944     // removed once defined, so if we are defining a existing block and it is
1945     // not a forward declaration, then it is a redeclaration.
1946   } else if (!eraseForwardRef(blockAndLoc.block)) {
1947     return nullptr;
1948   }
1949 
1950   // Populate the high level assembly state if necessary.
1951   if (state.asmState)
1952     state.asmState->addDefinition(blockAndLoc.block, loc);
1953 
1954   return blockAndLoc.block;
1955 }
1956 
1957 /// Parse a (possibly empty) list of SSA operands with types as block arguments.
1958 ///
1959 ///   ssa-id-and-type-list ::= ssa-id-and-type (`,` ssa-id-and-type)*
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([&]() -> ParseResult {
1971     return parseSSADefOrUseAndType(
1972         [&](SSAUseInfo 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.loc);
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.loc);
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), llvm::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