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