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