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