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