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