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