MLIR  20.0.0git
OpImplementation.h
Go to the documentation of this file.
1 //===- OpImplementation.h - Classes for implementing Op types ---*- C++ -*-===//
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 classes used by the implementation details of Op types.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_IR_OPIMPLEMENTATION_H
14 #define MLIR_IR_OPIMPLEMENTATION_H
15 
16 #include "mlir/IR/BuiltinTypes.h"
18 #include "mlir/IR/OpDefinition.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/Support/SMLoc.h"
21 #include <optional>
22 
23 namespace mlir {
24 class AsmParsedResourceEntry;
25 class AsmResourceBuilder;
26 class Builder;
27 
28 //===----------------------------------------------------------------------===//
29 // AsmDialectResourceHandle
30 //===----------------------------------------------------------------------===//
31 
32 /// This class represents an opaque handle to a dialect resource entry.
34 public:
36  AsmDialectResourceHandle(void *resource, TypeID resourceID, Dialect *dialect)
37  : resource(resource), opaqueID(resourceID), dialect(dialect) {}
38  bool operator==(const AsmDialectResourceHandle &other) const {
39  return resource == other.resource;
40  }
41 
42  /// Return an opaque pointer to the referenced resource.
43  void *getResource() const { return resource; }
44 
45  /// Return the type ID of the resource.
46  TypeID getTypeID() const { return opaqueID; }
47 
48  /// Return the dialect that owns the resource.
49  Dialect *getDialect() const { return dialect; }
50 
51 private:
52  /// The opaque handle to the dialect resource.
53  void *resource = nullptr;
54  /// The type of the resource referenced.
55  TypeID opaqueID;
56  /// The dialect owning the given resource.
57  Dialect *dialect;
58 };
59 
60 /// This class represents a CRTP base class for dialect resource handles. It
61 /// abstracts away various utilities necessary for defined derived resource
62 /// handles.
63 template <typename DerivedT, typename ResourceT, typename DialectT>
65 public:
66  using Dialect = DialectT;
67 
68  /// Construct a handle from a pointer to the resource. The given pointer
69  /// should be guaranteed to live beyond the life of this handle.
70  AsmDialectResourceHandleBase(ResourceT *resource, DialectT *dialect)
71  : AsmDialectResourceHandle(resource, TypeID::get<DerivedT>(), dialect) {}
73  : AsmDialectResourceHandle(handle) {
74  assert(handle.getTypeID() == TypeID::get<DerivedT>());
75  }
76 
77  /// Return the resource referenced by this handle.
78  ResourceT *getResource() {
79  return static_cast<ResourceT *>(AsmDialectResourceHandle::getResource());
80  }
81  const ResourceT *getResource() const {
82  return const_cast<AsmDialectResourceHandleBase *>(this)->getResource();
83  }
84 
85  /// Return the dialect that owns the resource.
86  DialectT *getDialect() const {
87  return static_cast<DialectT *>(AsmDialectResourceHandle::getDialect());
88  }
89 
90  /// Support llvm style casting.
91  static bool classof(const AsmDialectResourceHandle *handle) {
92  return handle->getTypeID() == TypeID::get<DerivedT>();
93  }
94 };
95 
96 inline llvm::hash_code hash_value(const AsmDialectResourceHandle &param) {
97  return llvm::hash_value(param.getResource());
98 }
99 
100 //===----------------------------------------------------------------------===//
101 // AsmPrinter
102 //===----------------------------------------------------------------------===//
103 
104 /// This base class exposes generic asm printer hooks, usable across the various
105 /// derived printers.
106 class AsmPrinter {
107 public:
108  /// This class contains the internal default implementation of the base
109  /// printer methods.
110  class Impl;
111 
112  /// Initialize the printer with the given internal implementation.
114  virtual ~AsmPrinter();
115 
116  /// Return the raw output stream used by this printer.
117  virtual raw_ostream &getStream() const;
118 
119  /// Print the given floating point value in a stabilized form that can be
120  /// roundtripped through the IR. This is the companion to the 'parseFloat'
121  /// hook on the AsmParser.
122  virtual void printFloat(const APFloat &value);
123 
124  virtual void printType(Type type);
125  virtual void printAttribute(Attribute attr);
126 
127  /// Trait to check if `AttrType` provides a `print` method.
128  template <typename AttrOrType>
130  decltype(std::declval<AttrOrType>().print(std::declval<AsmPrinter &>()));
131  template <typename AttrOrType>
133  llvm::is_detected<has_print_method, AttrOrType>;
134 
135  /// Print the provided attribute in the context of an operation custom
136  /// printer/parser: this will invoke directly the print method on the
137  /// attribute class and skip the `#dialect.mnemonic` prefix in most cases.
138  template <typename AttrOrType,
139  std::enable_if_t<detect_has_print_method<AttrOrType>::value>
140  *sfinae = nullptr>
141  void printStrippedAttrOrType(AttrOrType attrOrType) {
142  if (succeeded(printAlias(attrOrType)))
143  return;
144 
145  raw_ostream &os = getStream();
146  uint64_t posPrior = os.tell();
147  attrOrType.print(*this);
148  if (posPrior != os.tell())
149  return;
150 
151  // Fallback to printing with prefix if the above failed to write anything
152  // to the output stream.
153  *this << attrOrType;
154  }
155 
156  /// Print the provided array of attributes or types in the context of an
157  /// operation custom printer/parser: this will invoke directly the print
158  /// method on the attribute class and skip the `#dialect.mnemonic` prefix in
159  /// most cases.
160  template <typename AttrOrType,
161  std::enable_if_t<detect_has_print_method<AttrOrType>::value>
162  *sfinae = nullptr>
164  llvm::interleaveComma(
165  attrOrTypes, getStream(),
166  [this](AttrOrType attrOrType) { printStrippedAttrOrType(attrOrType); });
167  }
168 
169  /// SFINAE for printing the provided attribute in the context of an operation
170  /// custom printer in the case where the attribute does not define a print
171  /// method.
172  template <typename AttrOrType,
173  std::enable_if_t<!detect_has_print_method<AttrOrType>::value>
174  *sfinae = nullptr>
175  void printStrippedAttrOrType(AttrOrType attrOrType) {
176  *this << attrOrType;
177  }
178 
179  /// Print the given attribute without its type. The corresponding parser must
180  /// provide a valid type for the attribute.
181  virtual void printAttributeWithoutType(Attribute attr);
182 
183  /// Print the alias for the given attribute, return failure if no alias could
184  /// be printed.
185  virtual LogicalResult printAlias(Attribute attr);
186 
187  /// Print the alias for the given type, return failure if no alias could
188  /// be printed.
189  virtual LogicalResult printAlias(Type type);
190 
191  /// Print the given string as a keyword, or a quoted and escaped string if it
192  /// has any special or non-printable characters in it.
193  virtual void printKeywordOrString(StringRef keyword);
194 
195  /// Print the given string as a quoted string, escaping any special or
196  /// non-printable characters in it.
197  virtual void printString(StringRef string);
198 
199  /// Print the given string as a symbol reference, i.e. a form representable by
200  /// a SymbolRefAttr. A symbol reference is represented as a string prefixed
201  /// with '@'. The reference is surrounded with ""'s and escaped if it has any
202  /// special or non-printable characters in it.
203  virtual void printSymbolName(StringRef symbolRef);
204 
205  /// Print a handle to the given dialect resource.
206  virtual void printResourceHandle(const AsmDialectResourceHandle &resource);
207 
208  /// Print an optional arrow followed by a type list.
209  template <typename TypeRange>
211  if (types.begin() != types.end())
212  printArrowTypeList(types);
213  }
214  template <typename TypeRange>
216  auto &os = getStream() << " -> ";
217 
218  bool wrapped = !llvm::hasSingleElement(types) ||
219  llvm::isa<FunctionType>((*types.begin()));
220  if (wrapped)
221  os << '(';
222  llvm::interleaveComma(types, *this);
223  if (wrapped)
224  os << ')';
225  }
226 
227  /// Print the two given type ranges in a functional form.
228  template <typename InputRangeT, typename ResultRangeT>
229  void printFunctionalType(InputRangeT &&inputs, ResultRangeT &&results) {
230  auto &os = getStream();
231  os << '(';
232  llvm::interleaveComma(inputs, *this);
233  os << ')';
234  printArrowTypeList(results);
235  }
236 
238 
239  /// Class used to automatically end a cyclic region on destruction.
241  public:
242  explicit CyclicPrintReset(AsmPrinter *printer) : printer(printer) {}
243 
245  if (printer)
246  printer->popCyclicPrinting();
247  }
248 
250 
252 
254  : printer(std::exchange(rhs.printer, nullptr)) {}
255 
257  printer = std::exchange(rhs.printer, nullptr);
258  return *this;
259  }
260 
261  private:
262  AsmPrinter *printer;
263  };
264 
265  /// Attempts to start a cyclic printing region for `attrOrType`.
266  /// A cyclic printing region starts with this call and ends with the
267  /// destruction of the returned `CyclicPrintReset`. During this time,
268  /// calling `tryStartCyclicPrint` with the same attribute in any printer
269  /// will lead to returning failure.
270  ///
271  /// This makes it possible to break infinite recursions when trying to print
272  /// cyclic attributes or types by printing only immutable parameters if nested
273  /// within itself.
274  template <class AttrOrTypeT>
275  FailureOr<CyclicPrintReset> tryStartCyclicPrint(AttrOrTypeT attrOrType) {
276  static_assert(
277  std::is_base_of_v<AttributeTrait::IsMutable<AttrOrTypeT>,
278  AttrOrTypeT> ||
279  std::is_base_of_v<TypeTrait::IsMutable<AttrOrTypeT>, AttrOrTypeT>,
280  "Only mutable attributes or types can be cyclic");
281  if (failed(pushCyclicPrinting(attrOrType.getAsOpaquePointer())))
282  return failure();
283  return CyclicPrintReset(this);
284  }
285 
286 protected:
287  /// Initialize the printer with no internal implementation. In this case, all
288  /// virtual methods of this class must be overriden.
289  AsmPrinter() = default;
290 
291  /// Pushes a new attribute or type in the form of a type erased pointer
292  /// into an internal set.
293  /// Returns success if the type or attribute was inserted in the set or
294  /// failure if it was already contained.
295  virtual LogicalResult pushCyclicPrinting(const void *opaquePointer);
296 
297  /// Removes the element that was last inserted with a successful call to
298  /// `pushCyclicPrinting`. There must be exactly one `popCyclicPrinting` call
299  /// in reverse order of all successful `pushCyclicPrinting`.
300  virtual void popCyclicPrinting();
301 
302 private:
303  AsmPrinter(const AsmPrinter &) = delete;
304  void operator=(const AsmPrinter &) = delete;
305 
306  /// The internal implementation of the printer.
307  Impl *impl{nullptr};
308 };
309 
310 template <typename AsmPrinterT>
311 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
312  AsmPrinterT &>
313 operator<<(AsmPrinterT &p, Type type) {
314  p.printType(type);
315  return p;
316 }
317 
318 template <typename AsmPrinterT>
319 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
320  AsmPrinterT &>
321 operator<<(AsmPrinterT &p, Attribute attr) {
322  p.printAttribute(attr);
323  return p;
324 }
325 
326 template <typename AsmPrinterT>
327 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
328  AsmPrinterT &>
329 operator<<(AsmPrinterT &p, const APFloat &value) {
330  p.printFloat(value);
331  return p;
332 }
333 template <typename AsmPrinterT>
334 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
335  AsmPrinterT &>
336 operator<<(AsmPrinterT &p, float value) {
337  return p << APFloat(value);
338 }
339 template <typename AsmPrinterT>
340 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
341  AsmPrinterT &>
342 operator<<(AsmPrinterT &p, double value) {
343  return p << APFloat(value);
344 }
345 
346 // Support printing anything that isn't convertible to one of the other
347 // streamable types, even if it isn't exactly one of them. For example, we want
348 // to print FunctionType with the Type version above, not have it match this.
349 template <typename AsmPrinterT, typename T,
350  std::enable_if_t<!std::is_convertible<T &, Value &>::value &&
351  !std::is_convertible<T &, Type &>::value &&
352  !std::is_convertible<T &, Attribute &>::value &&
353  !std::is_convertible<T &, ValueRange>::value &&
354  !std::is_convertible<T &, APFloat &>::value &&
355  !llvm::is_one_of<T, bool, float, double>::value,
356  T> * = nullptr>
357 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
358  AsmPrinterT &>
359 operator<<(AsmPrinterT &p, const T &other) {
360  p.getStream() << other;
361  return p;
362 }
363 
364 template <typename AsmPrinterT>
365 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
366  AsmPrinterT &>
367 operator<<(AsmPrinterT &p, bool value) {
368  return p << (value ? StringRef("true") : "false");
369 }
370 
371 template <typename AsmPrinterT, typename ValueRangeT>
372 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
373  AsmPrinterT &>
374 operator<<(AsmPrinterT &p, const ValueTypeRange<ValueRangeT> &types) {
375  llvm::interleaveComma(types, p);
376  return p;
377 }
378 
379 template <typename AsmPrinterT>
380 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
381  AsmPrinterT &>
382 operator<<(AsmPrinterT &p, const TypeRange &types) {
383  llvm::interleaveComma(types, p);
384  return p;
385 }
386 
387 // Prevent matching the TypeRange version above for ValueRange
388 // printing through base AsmPrinter. This is needed so that the
389 // ValueRange printing behaviour does not change from printing
390 // the SSA values to printing the types for the operands when
391 // using AsmPrinter instead of OpAsmPrinter.
392 template <typename AsmPrinterT, typename T>
393 inline std::enable_if_t<std::is_same<AsmPrinter, AsmPrinterT>::value &&
394  std::is_convertible<T &, ValueRange>::value,
395  AsmPrinterT &>
396 operator<<(AsmPrinterT &p, const T &other) = delete;
397 
398 template <typename AsmPrinterT, typename ElementT>
399 inline std::enable_if_t<std::is_base_of<AsmPrinter, AsmPrinterT>::value,
400  AsmPrinterT &>
401 operator<<(AsmPrinterT &p, ArrayRef<ElementT> types) {
402  llvm::interleaveComma(types, p);
403  return p;
404 }
405 
406 //===----------------------------------------------------------------------===//
407 // OpAsmPrinter
408 //===----------------------------------------------------------------------===//
409 
410 /// This is a pure-virtual base class that exposes the asmprinter hooks
411 /// necessary to implement a custom print() method.
412 class OpAsmPrinter : public AsmPrinter {
413 public:
415  ~OpAsmPrinter() override;
416 
417  /// Print a loc(...) specifier if printing debug info is enabled.
419 
420  /// Print a newline and indent the printer to the start of the current
421  /// operation.
422  virtual void printNewline() = 0;
423 
424  /// Increase indentation.
425  virtual void increaseIndent() = 0;
426 
427  /// Decrease indentation.
428  virtual void decreaseIndent() = 0;
429 
430  /// Print a block argument in the usual format of:
431  /// %ssaName : type {attr1=42} loc("here")
432  /// where location printing is controlled by the standard internal option.
433  /// You may pass omitType=true to not print a type, and pass an empty
434  /// attribute list if you don't care for attributes.
436  ArrayRef<NamedAttribute> argAttrs = {},
437  bool omitType = false) = 0;
438 
439  /// Print implementations for various things an operation contains.
440  virtual void printOperand(Value value) = 0;
441  virtual void printOperand(Value value, raw_ostream &os) = 0;
442 
443  /// Print a comma separated list of operands.
444  template <typename ContainerType>
445  void printOperands(const ContainerType &container) {
446  printOperands(container.begin(), container.end());
447  }
448 
449  /// Print a comma separated list of operands.
450  template <typename IteratorType>
451  void printOperands(IteratorType it, IteratorType end) {
452  llvm::interleaveComma(llvm::make_range(it, end), getStream(),
453  [this](Value value) { printOperand(value); });
454  }
455 
456  /// Print the given successor.
457  virtual void printSuccessor(Block *successor) = 0;
458 
459  /// Print the successor and its operands.
460  virtual void printSuccessorAndUseList(Block *successor,
461  ValueRange succOperands) = 0;
462 
463  /// If the specified operation has attributes, print out an attribute
464  /// dictionary with their values. elidedAttrs allows the client to ignore
465  /// specific well known attributes, commonly used if the attribute value is
466  /// printed some other way (like as a fixed operand).
468  ArrayRef<StringRef> elidedAttrs = {}) = 0;
469 
470  /// If the specified operation has attributes, print out an attribute
471  /// dictionary prefixed with 'attributes'.
472  virtual void
474  ArrayRef<StringRef> elidedAttrs = {}) = 0;
475 
476  /// Prints the entire operation with the custom assembly form, if available,
477  /// or the generic assembly form, otherwise.
478  virtual void printCustomOrGenericOp(Operation *op) = 0;
479 
480  /// Print the entire operation with the default generic assembly form.
481  /// If `printOpName` is true, then the operation name is printed (the default)
482  /// otherwise it is omitted and the print will start with the operand list.
483  virtual void printGenericOp(Operation *op, bool printOpName = true) = 0;
484 
485  /// Prints a region.
486  /// If 'printEntryBlockArgs' is false, the arguments of the
487  /// block are not printed. If 'printBlockTerminator' is false, the terminator
488  /// operation of the block is not printed. If printEmptyBlock is true, then
489  /// the block header is printed even if the block is empty.
490  virtual void printRegion(Region &blocks, bool printEntryBlockArgs = true,
491  bool printBlockTerminators = true,
492  bool printEmptyBlock = false) = 0;
493 
494  /// Renumber the arguments for the specified region to the same names as the
495  /// SSA values in namesToUse. This may only be used for IsolatedFromAbove
496  /// operations. If any entry in namesToUse is null, the corresponding
497  /// argument name is left alone.
498  virtual void shadowRegionArgs(Region &region, ValueRange namesToUse) = 0;
499 
500  /// Prints an affine map of SSA ids, where SSA id names are used in place
501  /// of dims/symbols.
502  /// Operand values must come from single-result sources, and be valid
503  /// dimensions/symbol identifiers according to mlir::isValidDim/Symbol.
504  virtual void printAffineMapOfSSAIds(AffineMapAttr mapAttr,
505  ValueRange operands) = 0;
506 
507  /// Prints an affine expression of SSA ids with SSA id names used instead of
508  /// dims and symbols.
509  /// Operand values must come from single-result sources, and be valid
510  /// dimensions/symbol identifiers according to mlir::isValidDim/Symbol.
511  virtual void printAffineExprOfSSAIds(AffineExpr expr, ValueRange dimOperands,
512  ValueRange symOperands) = 0;
513 
514  /// Print the complete type of an operation in functional form.
515  void printFunctionalType(Operation *op);
517 };
518 
519 // Make the implementations convenient to use.
521  p.printOperand(value);
522  return p;
523 }
524 
525 template <typename T,
526  std::enable_if_t<std::is_convertible<T &, ValueRange>::value &&
527  !std::is_convertible<T &, Value &>::value,
528  T> * = nullptr>
529 inline OpAsmPrinter &operator<<(OpAsmPrinter &p, const T &values) {
530  p.printOperands(values);
531  return p;
532 }
533 
535  p.printSuccessor(value);
536  return p;
537 }
538 
539 //===----------------------------------------------------------------------===//
540 // AsmParser
541 //===----------------------------------------------------------------------===//
542 
543 /// This base class exposes generic asm parser hooks, usable across the various
544 /// derived parsers.
545 class AsmParser {
546 public:
547  AsmParser() = default;
548  virtual ~AsmParser();
549 
550  MLIRContext *getContext() const;
551 
552  /// Return the location of the original name token.
553  virtual SMLoc getNameLoc() const = 0;
554 
555  //===--------------------------------------------------------------------===//
556  // Utilities
557  //===--------------------------------------------------------------------===//
558 
559  /// Emit a diagnostic at the specified location and return failure.
560  virtual InFlightDiagnostic emitError(SMLoc loc,
561  const Twine &message = {}) = 0;
562 
563  /// Return a builder which provides useful access to MLIRContext, global
564  /// objects like types and attributes.
565  virtual Builder &getBuilder() const = 0;
566 
567  /// Get the location of the next token and store it into the argument. This
568  /// always succeeds.
569  virtual SMLoc getCurrentLocation() = 0;
570  ParseResult getCurrentLocation(SMLoc *loc) {
571  *loc = getCurrentLocation();
572  return success();
573  }
574 
575  /// Re-encode the given source location as an MLIR location and return it.
576  /// Note: This method should only be used when a `Location` is necessary, as
577  /// the encoding process is not efficient.
578  virtual Location getEncodedSourceLoc(SMLoc loc) = 0;
579 
580  //===--------------------------------------------------------------------===//
581  // Token Parsing
582  //===--------------------------------------------------------------------===//
583 
584  /// Parse a '->' token.
585  virtual ParseResult parseArrow() = 0;
586 
587  /// Parse a '->' token if present
588  virtual ParseResult parseOptionalArrow() = 0;
589 
590  /// Parse a `{` token.
591  virtual ParseResult parseLBrace() = 0;
592 
593  /// Parse a `{` token if present.
594  virtual ParseResult parseOptionalLBrace() = 0;
595 
596  /// Parse a `}` token.
597  virtual ParseResult parseRBrace() = 0;
598 
599  /// Parse a `}` token if present.
600  virtual ParseResult parseOptionalRBrace() = 0;
601 
602  /// Parse a `:` token.
603  virtual ParseResult parseColon() = 0;
604 
605  /// Parse a `:` token if present.
606  virtual ParseResult parseOptionalColon() = 0;
607 
608  /// Parse a `,` token.
609  virtual ParseResult parseComma() = 0;
610 
611  /// Parse a `,` token if present.
612  virtual ParseResult parseOptionalComma() = 0;
613 
614  /// Parse a `=` token.
615  virtual ParseResult parseEqual() = 0;
616 
617  /// Parse a `=` token if present.
618  virtual ParseResult parseOptionalEqual() = 0;
619 
620  /// Parse a '<' token.
621  virtual ParseResult parseLess() = 0;
622 
623  /// Parse a '<' token if present.
624  virtual ParseResult parseOptionalLess() = 0;
625 
626  /// Parse a '>' token.
627  virtual ParseResult parseGreater() = 0;
628 
629  /// Parse a '>' token if present.
630  virtual ParseResult parseOptionalGreater() = 0;
631 
632  /// Parse a '?' token.
633  virtual ParseResult parseQuestion() = 0;
634 
635  /// Parse a '?' token if present.
636  virtual ParseResult parseOptionalQuestion() = 0;
637 
638  /// Parse a '+' token.
639  virtual ParseResult parsePlus() = 0;
640 
641  /// Parse a '+' token if present.
642  virtual ParseResult parseOptionalPlus() = 0;
643 
644  /// Parse a '*' token.
645  virtual ParseResult parseStar() = 0;
646 
647  /// Parse a '*' token if present.
648  virtual ParseResult parseOptionalStar() = 0;
649 
650  /// Parse a '|' token.
651  virtual ParseResult parseVerticalBar() = 0;
652 
653  /// Parse a '|' token if present.
654  virtual ParseResult parseOptionalVerticalBar() = 0;
655 
656  /// Parse a quoted string token.
657  ParseResult parseString(std::string *string) {
658  auto loc = getCurrentLocation();
659  if (parseOptionalString(string))
660  return emitError(loc, "expected string");
661  return success();
662  }
663 
664  /// Parse a quoted string token if present.
665  virtual ParseResult parseOptionalString(std::string *string) = 0;
666 
667  /// Parses a Base64 encoded string of bytes.
668  virtual ParseResult parseBase64Bytes(std::vector<char> *bytes) = 0;
669 
670  /// Parse a `(` token.
671  virtual ParseResult parseLParen() = 0;
672 
673  /// Parse a `(` token if present.
674  virtual ParseResult parseOptionalLParen() = 0;
675 
676  /// Parse a `)` token.
677  virtual ParseResult parseRParen() = 0;
678 
679  /// Parse a `)` token if present.
680  virtual ParseResult parseOptionalRParen() = 0;
681 
682  /// Parse a `[` token.
683  virtual ParseResult parseLSquare() = 0;
684 
685  /// Parse a `[` token if present.
686  virtual ParseResult parseOptionalLSquare() = 0;
687 
688  /// Parse a `]` token.
689  virtual ParseResult parseRSquare() = 0;
690 
691  /// Parse a `]` token if present.
692  virtual ParseResult parseOptionalRSquare() = 0;
693 
694  /// Parse a `...` token.
695  virtual ParseResult parseEllipsis() = 0;
696 
697  /// Parse a `...` token if present;
698  virtual ParseResult parseOptionalEllipsis() = 0;
699 
700  /// Parse a floating point value from the stream.
701  virtual ParseResult parseFloat(double &result) = 0;
702 
703  /// Parse a floating point value into APFloat from the stream.
704  virtual ParseResult parseFloat(const llvm::fltSemantics &semantics,
705  APFloat &result) = 0;
706 
707  /// Parse an integer value from the stream.
708  template <typename IntT>
709  ParseResult parseInteger(IntT &result) {
710  auto loc = getCurrentLocation();
711  OptionalParseResult parseResult = parseOptionalInteger(result);
712  if (!parseResult.has_value())
713  return emitError(loc, "expected integer value");
714  return *parseResult;
715  }
716 
717  /// Parse a decimal integer value from the stream.
718  template <typename IntT>
719  ParseResult parseDecimalInteger(IntT &result) {
720  auto loc = getCurrentLocation();
721  OptionalParseResult parseResult = parseOptionalDecimalInteger(result);
722  if (!parseResult.has_value())
723  return emitError(loc, "expected decimal integer value");
724  return *parseResult;
725  }
726 
727  /// Parse an optional integer value from the stream.
728  virtual OptionalParseResult parseOptionalInteger(APInt &result) = 0;
730 
731  private:
732  template <typename IntT, typename ParseFn>
733  OptionalParseResult parseOptionalIntegerAndCheck(IntT &result,
734  ParseFn &&parseFn) {
735  auto loc = getCurrentLocation();
736  APInt uintResult;
737  OptionalParseResult parseResult = parseFn(uintResult);
738  if (!parseResult.has_value() || failed(*parseResult))
739  return parseResult;
740 
741  // Try to convert to the provided integer type. sextOrTrunc is correct even
742  // for unsigned types because parseOptionalInteger ensures the sign bit is
743  // zero for non-negated integers.
744  result =
745  (IntT)uintResult.sextOrTrunc(sizeof(IntT) * CHAR_BIT).getLimitedValue();
746  if (APInt(uintResult.getBitWidth(), result) != uintResult)
747  return emitError(loc, "integer value too large");
748  return success();
749  }
750 
751  public:
752  template <typename IntT>
754  return parseOptionalIntegerAndCheck(
755  result, [&](APInt &result) { return parseOptionalInteger(result); });
756  }
757 
758  template <typename IntT>
760  return parseOptionalIntegerAndCheck(result, [&](APInt &result) {
761  return parseOptionalDecimalInteger(result);
762  });
763  }
764 
765  /// These are the supported delimiters around operand lists and region
766  /// argument lists, used by parseOperandList.
767  enum class Delimiter {
768  /// Zero or more operands with no delimiters.
769  None,
770  /// Parens surrounding zero or more operands.
771  Paren,
772  /// Square brackets surrounding zero or more operands.
773  Square,
774  /// <> brackets surrounding zero or more operands.
775  LessGreater,
776  /// {} brackets surrounding zero or more operands.
777  Braces,
778  /// Parens supporting zero or more operands, or nothing.
780  /// Square brackets supporting zero or more ops, or nothing.
782  /// <> brackets supporting zero or more ops, or nothing.
784  /// {} brackets surrounding zero or more operands, or nothing.
786  };
787 
788  /// Parse a list of comma-separated items with an optional delimiter. If a
789  /// delimiter is provided, then an empty list is allowed. If not, then at
790  /// least one element will be parsed.
791  ///
792  /// contextMessage is an optional message appended to "expected '('" sorts of
793  /// diagnostics when parsing the delimeters.
794  virtual ParseResult
796  function_ref<ParseResult()> parseElementFn,
797  StringRef contextMessage = StringRef()) = 0;
798 
799  /// Parse a comma separated list of elements that must have at least one entry
800  /// in it.
801  ParseResult
802  parseCommaSeparatedList(function_ref<ParseResult()> parseElementFn) {
803  return parseCommaSeparatedList(Delimiter::None, parseElementFn);
804  }
805 
806  //===--------------------------------------------------------------------===//
807  // Keyword Parsing
808  //===--------------------------------------------------------------------===//
809 
810  /// This class represents a StringSwitch like class that is useful for parsing
811  /// expected keywords. On construction, unless a non-empty keyword is
812  /// provided, it invokes `parseKeyword` and processes each of the provided
813  /// cases statements until a match is hit. The provided `ResultT` must be
814  /// assignable from `failure()`.
815  template <typename ResultT = ParseResult>
817  public:
818  KeywordSwitch(AsmParser &parser, StringRef *keyword = nullptr)
819  : parser(parser), loc(parser.getCurrentLocation()) {
820  if (keyword && !keyword->empty())
821  this->keyword = *keyword;
822  else if (failed(parser.parseKeywordOrCompletion(&this->keyword)))
823  result = failure();
824  }
825  /// Case that uses the provided value when true.
826  KeywordSwitch &Case(StringLiteral str, ResultT value) {
827  return Case(str, [&](StringRef, SMLoc) { return std::move(value); });
828  }
829  KeywordSwitch &Default(ResultT value) {
830  return Default([&](StringRef, SMLoc) { return std::move(value); });
831  }
832  /// Case that invokes the provided functor when true. The parameters passed
833  /// to the functor are the keyword, and the location of the keyword (in case
834  /// any errors need to be emitted).
835  template <typename FnT>
836  std::enable_if_t<!std::is_convertible<FnT, ResultT>::value, KeywordSwitch &>
837  Case(StringLiteral str, FnT &&fn) {
838  if (result)
839  return *this;
840 
841  // If the word was empty, record this as a completion.
842  if (keyword.empty())
843  parser.codeCompleteExpectedTokens(str);
844  else if (keyword == str)
845  result.emplace(std::move(fn(keyword, loc)));
846  return *this;
847  }
848  template <typename FnT>
849  std::enable_if_t<!std::is_convertible<FnT, ResultT>::value, KeywordSwitch &>
850  Default(FnT &&fn) {
851  if (!result)
852  result.emplace(fn(keyword, loc));
853  return *this;
854  }
855 
856  /// Returns true if this switch has a value yet.
857  bool hasValue() const { return result.has_value(); }
858 
859  /// Return the result of the switch.
860  [[nodiscard]] operator ResultT() {
861  if (!result)
862  return parser.emitError(loc, "unexpected keyword: ") << keyword;
863  return std::move(*result);
864  }
865 
866  private:
867  /// The parser used to construct this switch.
868  AsmParser &parser;
869 
870  /// The location of the keyword, used to emit errors as necessary.
871  SMLoc loc;
872 
873  /// The parsed keyword itself.
874  StringRef keyword;
875 
876  /// The result of the switch statement or std::nullopt if currently unknown.
877  std::optional<ResultT> result;
878  };
879 
880  /// Parse a given keyword.
881  ParseResult parseKeyword(StringRef keyword) {
882  return parseKeyword(keyword, "");
883  }
884  virtual ParseResult parseKeyword(StringRef keyword, const Twine &msg) = 0;
885 
886  /// Parse a keyword into 'keyword'.
887  ParseResult parseKeyword(StringRef *keyword) {
888  auto loc = getCurrentLocation();
889  if (parseOptionalKeyword(keyword))
890  return emitError(loc, "expected valid keyword");
891  return success();
892  }
893 
894  /// Parse the given keyword if present.
895  virtual ParseResult parseOptionalKeyword(StringRef keyword) = 0;
896 
897  /// Parse a keyword, if present, into 'keyword'.
898  virtual ParseResult parseOptionalKeyword(StringRef *keyword) = 0;
899 
900  /// Parse a keyword, if present, and if one of the 'allowedValues',
901  /// into 'keyword'
902  virtual ParseResult
903  parseOptionalKeyword(StringRef *keyword,
904  ArrayRef<StringRef> allowedValues) = 0;
905 
906  /// Parse a keyword or a quoted string.
907  ParseResult parseKeywordOrString(std::string *result) {
908  if (failed(parseOptionalKeywordOrString(result)))
909  return emitError(getCurrentLocation())
910  << "expected valid keyword or string";
911  return success();
912  }
913 
914  /// Parse an optional keyword or string.
915  virtual ParseResult parseOptionalKeywordOrString(std::string *result) = 0;
916 
917  //===--------------------------------------------------------------------===//
918  // Attribute/Type Parsing
919  //===--------------------------------------------------------------------===//
920 
921  /// Invoke the `getChecked` method of the given Attribute or Type class, using
922  /// the provided location to emit errors in the case of failure. Note that
923  /// unlike `OpBuilder::getType`, this method does not implicitly insert a
924  /// context parameter.
925  template <typename T, typename... ParamsT>
926  auto getChecked(SMLoc loc, ParamsT &&...params) {
927  return T::getChecked([&] { return emitError(loc); },
928  std::forward<ParamsT>(params)...);
929  }
930  /// A variant of `getChecked` that uses the result of `getNameLoc` to emit
931  /// errors.
932  template <typename T, typename... ParamsT>
933  auto getChecked(ParamsT &&...params) {
934  return T::getChecked([&] { return emitError(getNameLoc()); },
935  std::forward<ParamsT>(params)...);
936  }
937 
938  //===--------------------------------------------------------------------===//
939  // Attribute Parsing
940  //===--------------------------------------------------------------------===//
941 
942  /// Parse an arbitrary attribute of a given type and return it in result.
943  virtual ParseResult parseAttribute(Attribute &result, Type type = {}) = 0;
944 
945  /// Parse a custom attribute with the provided callback, unless the next
946  /// token is `#`, in which case the generic parser is invoked.
947  virtual ParseResult parseCustomAttributeWithFallback(
948  Attribute &result, Type type,
949  function_ref<ParseResult(Attribute &result, Type type)>
950  parseAttribute) = 0;
951 
952  /// Parse an attribute of a specific kind and type.
953  template <typename AttrType>
954  ParseResult parseAttribute(AttrType &result, Type type = {}) {
955  SMLoc loc = getCurrentLocation();
956 
957  // Parse any kind of attribute.
958  Attribute attr;
959  if (parseAttribute(attr, type))
960  return failure();
961 
962  // Check for the right kind of attribute.
963  if (!(result = llvm::dyn_cast<AttrType>(attr)))
964  return emitError(loc, "invalid kind of attribute specified");
965 
966  return success();
967  }
968 
969  /// Parse an arbitrary attribute and return it in result. This also adds the
970  /// attribute to the specified attribute list with the specified name.
971  ParseResult parseAttribute(Attribute &result, StringRef attrName,
972  NamedAttrList &attrs) {
973  return parseAttribute(result, Type(), attrName, attrs);
974  }
975 
976  /// Parse an attribute of a specific kind and type.
977  template <typename AttrType>
978  ParseResult parseAttribute(AttrType &result, StringRef attrName,
979  NamedAttrList &attrs) {
980  return parseAttribute(result, Type(), attrName, attrs);
981  }
982 
983  /// Parse an arbitrary attribute of a given type and populate it in `result`.
984  /// This also adds the attribute to the specified attribute list with the
985  /// specified name.
986  template <typename AttrType>
987  ParseResult parseAttribute(AttrType &result, Type type, StringRef attrName,
988  NamedAttrList &attrs) {
989  SMLoc loc = getCurrentLocation();
990 
991  // Parse any kind of attribute.
992  Attribute attr;
993  if (parseAttribute(attr, type))
994  return failure();
995 
996  // Check for the right kind of attribute.
997  result = llvm::dyn_cast<AttrType>(attr);
998  if (!result)
999  return emitError(loc, "invalid kind of attribute specified");
1000 
1001  attrs.append(attrName, result);
1002  return success();
1003  }
1004 
1005  /// Trait to check if `AttrType` provides a `parse` method.
1006  template <typename AttrType>
1007  using has_parse_method = decltype(AttrType::parse(std::declval<AsmParser &>(),
1008  std::declval<Type>()));
1009  template <typename AttrType>
1010  using detect_has_parse_method = llvm::is_detected<has_parse_method, AttrType>;
1011 
1012  /// Parse a custom attribute of a given type unless the next token is `#`, in
1013  /// which case the generic parser is invoked. The parsed attribute is
1014  /// populated in `result` and also added to the specified attribute list with
1015  /// the specified name.
1016  template <typename AttrType>
1017  std::enable_if_t<detect_has_parse_method<AttrType>::value, ParseResult>
1018  parseCustomAttributeWithFallback(AttrType &result, Type type,
1019  StringRef attrName, NamedAttrList &attrs) {
1020  SMLoc loc = getCurrentLocation();
1021 
1022  // Parse any kind of attribute.
1023  Attribute attr;
1025  attr, type, [&](Attribute &result, Type type) -> ParseResult {
1026  result = AttrType::parse(*this, type);
1027  if (!result)
1028  return failure();
1029  return success();
1030  }))
1031  return failure();
1032 
1033  // Check for the right kind of attribute.
1034  result = llvm::dyn_cast<AttrType>(attr);
1035  if (!result)
1036  return emitError(loc, "invalid kind of attribute specified");
1037 
1038  attrs.append(attrName, result);
1039  return success();
1040  }
1041 
1042  /// SFINAE parsing method for Attribute that don't implement a parse method.
1043  template <typename AttrType>
1044  std::enable_if_t<!detect_has_parse_method<AttrType>::value, ParseResult>
1045  parseCustomAttributeWithFallback(AttrType &result, Type type,
1046  StringRef attrName, NamedAttrList &attrs) {
1047  return parseAttribute(result, type, attrName, attrs);
1048  }
1049 
1050  /// Parse a custom attribute of a given type unless the next token is `#`, in
1051  /// which case the generic parser is invoked. The parsed attribute is
1052  /// populated in `result`.
1053  template <typename AttrType>
1054  std::enable_if_t<detect_has_parse_method<AttrType>::value, ParseResult>
1055  parseCustomAttributeWithFallback(AttrType &result, Type type = {}) {
1056  SMLoc loc = getCurrentLocation();
1057 
1058  // Parse any kind of attribute.
1059  Attribute attr;
1061  attr, type, [&](Attribute &result, Type type) -> ParseResult {
1062  result = AttrType::parse(*this, type);
1063  return success(!!result);
1064  }))
1065  return failure();
1066 
1067  // Check for the right kind of attribute.
1068  result = llvm::dyn_cast<AttrType>(attr);
1069  if (!result)
1070  return emitError(loc, "invalid kind of attribute specified");
1071  return success();
1072  }
1073 
1074  /// SFINAE parsing method for Attribute that don't implement a parse method.
1075  template <typename AttrType>
1076  std::enable_if_t<!detect_has_parse_method<AttrType>::value, ParseResult>
1077  parseCustomAttributeWithFallback(AttrType &result, Type type = {}) {
1078  return parseAttribute(result, type);
1079  }
1080 
1081  /// Parse an arbitrary optional attribute of a given type and return it in
1082  /// result.
1084  Type type = {}) = 0;
1085 
1086  /// Parse an optional array attribute and return it in result.
1087  virtual OptionalParseResult parseOptionalAttribute(ArrayAttr &result,
1088  Type type = {}) = 0;
1089 
1090  /// Parse an optional string attribute and return it in result.
1091  virtual OptionalParseResult parseOptionalAttribute(StringAttr &result,
1092  Type type = {}) = 0;
1093 
1094  /// Parse an optional symbol ref attribute and return it in result.
1095  virtual OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result,
1096  Type type = {}) = 0;
1097 
1098  /// Parse an optional attribute of a specific type and add it to the list with
1099  /// the specified name.
1100  template <typename AttrType>
1102  StringRef attrName,
1103  NamedAttrList &attrs) {
1104  return parseOptionalAttribute(result, Type(), attrName, attrs);
1105  }
1106 
1107  /// Parse an optional attribute of a specific type and add it to the list with
1108  /// the specified name.
1109  template <typename AttrType>
1111  StringRef attrName,
1112  NamedAttrList &attrs) {
1113  OptionalParseResult parseResult = parseOptionalAttribute(result, type);
1114  if (parseResult.has_value() && succeeded(*parseResult))
1115  attrs.append(attrName, result);
1116  return parseResult;
1117  }
1118 
1119  /// Parse a named dictionary into 'result' if it is present.
1120  virtual ParseResult parseOptionalAttrDict(NamedAttrList &result) = 0;
1121 
1122  /// Parse a named dictionary into 'result' if the `attributes` keyword is
1123  /// present.
1124  virtual ParseResult
1126 
1127  /// Parse an affine map instance into 'map'.
1128  virtual ParseResult parseAffineMap(AffineMap &map) = 0;
1129 
1130  /// Parse an affine expr instance into 'expr' using the already computed
1131  /// mapping from symbols to affine expressions in 'symbolSet'.
1132  virtual ParseResult
1133  parseAffineExpr(ArrayRef<std::pair<StringRef, AffineExpr>> symbolSet,
1134  AffineExpr &expr) = 0;
1135 
1136  /// Parse an integer set instance into 'set'.
1137  virtual ParseResult parseIntegerSet(IntegerSet &set) = 0;
1138 
1139  //===--------------------------------------------------------------------===//
1140  // Identifier Parsing
1141  //===--------------------------------------------------------------------===//
1142 
1143  /// Parse an @-identifier and store it (without the '@' symbol) in a string
1144  /// attribute.
1145  ParseResult parseSymbolName(StringAttr &result) {
1146  if (failed(parseOptionalSymbolName(result)))
1147  return emitError(getCurrentLocation())
1148  << "expected valid '@'-identifier for symbol name";
1149  return success();
1150  }
1151 
1152  /// Parse an @-identifier and store it (without the '@' symbol) in a string
1153  /// attribute named 'attrName'.
1154  ParseResult parseSymbolName(StringAttr &result, StringRef attrName,
1155  NamedAttrList &attrs) {
1156  if (parseSymbolName(result))
1157  return failure();
1158  attrs.append(attrName, result);
1159  return success();
1160  }
1161 
1162  /// Parse an optional @-identifier and store it (without the '@' symbol) in a
1163  /// string attribute.
1164  virtual ParseResult parseOptionalSymbolName(StringAttr &result) = 0;
1165 
1166  /// Parse an optional @-identifier and store it (without the '@' symbol) in a
1167  /// string attribute named 'attrName'.
1168  ParseResult parseOptionalSymbolName(StringAttr &result, StringRef attrName,
1169  NamedAttrList &attrs) {
1170  if (succeeded(parseOptionalSymbolName(result))) {
1171  attrs.append(attrName, result);
1172  return success();
1173  }
1174  return failure();
1175  }
1176 
1177  //===--------------------------------------------------------------------===//
1178  // Resource Parsing
1179  //===--------------------------------------------------------------------===//
1180 
1181  /// Parse a handle to a resource within the assembly format.
1182  template <typename ResourceT>
1183  FailureOr<ResourceT> parseResourceHandle() {
1184  SMLoc handleLoc = getCurrentLocation();
1185 
1186  // Try to load the dialect that owns the handle.
1187  auto *dialect =
1188  getContext()->getOrLoadDialect<typename ResourceT::Dialect>();
1189  if (!dialect) {
1190  return emitError(handleLoc)
1191  << "dialect '" << ResourceT::Dialect::getDialectNamespace()
1192  << "' is unknown";
1193  }
1194 
1195  FailureOr<AsmDialectResourceHandle> handle = parseResourceHandle(dialect);
1196  if (failed(handle))
1197  return failure();
1198  if (auto *result = dyn_cast<ResourceT>(&*handle))
1199  return std::move(*result);
1200  return emitError(handleLoc) << "provided resource handle differs from the "
1201  "expected resource type";
1202  }
1203 
1204  //===--------------------------------------------------------------------===//
1205  // Type Parsing
1206  //===--------------------------------------------------------------------===//
1207 
1208  /// Parse a type.
1209  virtual ParseResult parseType(Type &result) = 0;
1210 
1211  /// Parse a custom type with the provided callback, unless the next
1212  /// token is `#`, in which case the generic parser is invoked.
1213  virtual ParseResult parseCustomTypeWithFallback(
1214  Type &result, function_ref<ParseResult(Type &result)> parseType) = 0;
1215 
1216  /// Parse an optional type.
1218 
1219  /// Parse a type of a specific type.
1220  template <typename TypeT>
1221  ParseResult parseType(TypeT &result) {
1222  SMLoc loc = getCurrentLocation();
1223 
1224  // Parse any kind of type.
1225  Type type;
1226  if (parseType(type))
1227  return failure();
1228 
1229  // Check for the right kind of type.
1230  result = llvm::dyn_cast<TypeT>(type);
1231  if (!result)
1232  return emitError(loc, "invalid kind of type specified");
1233 
1234  return success();
1235  }
1236 
1237  /// Trait to check if `TypeT` provides a `parse` method.
1238  template <typename TypeT>
1240  decltype(TypeT::parse(std::declval<AsmParser &>()));
1241  template <typename TypeT>
1243  llvm::is_detected<type_has_parse_method, TypeT>;
1244 
1245  /// Parse a custom Type of a given type unless the next token is `#`, in
1246  /// which case the generic parser is invoked. The parsed Type is
1247  /// populated in `result`.
1248  template <typename TypeT>
1249  std::enable_if_t<detect_type_has_parse_method<TypeT>::value, ParseResult>
1251  SMLoc loc = getCurrentLocation();
1252 
1253  // Parse any kind of Type.
1254  Type type;
1255  if (parseCustomTypeWithFallback(type, [&](Type &result) -> ParseResult {
1256  result = TypeT::parse(*this);
1257  return success(!!result);
1258  }))
1259  return failure();
1260 
1261  // Check for the right kind of Type.
1262  result = llvm::dyn_cast<TypeT>(type);
1263  if (!result)
1264  return emitError(loc, "invalid kind of Type specified");
1265  return success();
1266  }
1267 
1268  /// SFINAE parsing method for Type that don't implement a parse method.
1269  template <typename TypeT>
1270  std::enable_if_t<!detect_type_has_parse_method<TypeT>::value, ParseResult>
1272  return parseType(result);
1273  }
1274 
1275  /// Parse a type list.
1276  ParseResult parseTypeList(SmallVectorImpl<Type> &result);
1277 
1278  /// Parse an arrow followed by a type list.
1279  virtual ParseResult parseArrowTypeList(SmallVectorImpl<Type> &result) = 0;
1280 
1281  /// Parse an optional arrow followed by a type list.
1282  virtual ParseResult
1284 
1285  /// Parse a colon followed by a type.
1286  virtual ParseResult parseColonType(Type &result) = 0;
1287 
1288  /// Parse a colon followed by a type of a specific kind, e.g. a FunctionType.
1289  template <typename TypeType>
1290  ParseResult parseColonType(TypeType &result) {
1291  SMLoc loc = getCurrentLocation();
1292 
1293  // Parse any kind of type.
1294  Type type;
1295  if (parseColonType(type))
1296  return failure();
1297 
1298  // Check for the right kind of type.
1299  result = llvm::dyn_cast<TypeType>(type);
1300  if (!result)
1301  return emitError(loc, "invalid kind of type specified");
1302 
1303  return success();
1304  }
1305 
1306  /// Parse a colon followed by a type list, which must have at least one type.
1307  virtual ParseResult parseColonTypeList(SmallVectorImpl<Type> &result) = 0;
1308 
1309  /// Parse an optional colon followed by a type list, which if present must
1310  /// have at least one type.
1311  virtual ParseResult
1313 
1314  /// Parse a keyword followed by a type.
1315  ParseResult parseKeywordType(const char *keyword, Type &result) {
1316  return failure(parseKeyword(keyword) || parseType(result));
1317  }
1318 
1319  /// Add the specified type to the end of the specified type list and return
1320  /// success. This is a helper designed to allow parse methods to be simple
1321  /// and chain through || operators.
1322  ParseResult addTypeToList(Type type, SmallVectorImpl<Type> &result) {
1323  result.push_back(type);
1324  return success();
1325  }
1326 
1327  /// Add the specified types to the end of the specified type list and return
1328  /// success. This is a helper designed to allow parse methods to be simple
1329  /// and chain through || operators.
1330  ParseResult addTypesToList(ArrayRef<Type> types,
1331  SmallVectorImpl<Type> &result) {
1332  result.append(types.begin(), types.end());
1333  return success();
1334  }
1335 
1336  /// Parse a dimension list of a tensor or memref type. This populates the
1337  /// dimension list, using ShapedType::kDynamic for the `?` dimensions if
1338  /// `allowDynamic` is set and errors out on `?` otherwise. Parsing the
1339  /// trailing `x` is configurable.
1340  ///
1341  /// dimension-list ::= eps | dimension (`x` dimension)*
1342  /// dimension-list-with-trailing-x ::= (dimension `x`)*
1343  /// dimension ::= `?` | decimal-literal
1344  ///
1345  /// When `allowDynamic` is not set, this is used to parse:
1346  ///
1347  /// static-dimension-list ::= eps | decimal-literal (`x` decimal-literal)*
1348  /// static-dimension-list-with-trailing-x ::= (dimension `x`)*
1349  virtual ParseResult parseDimensionList(SmallVectorImpl<int64_t> &dimensions,
1350  bool allowDynamic = true,
1351  bool withTrailingX = true) = 0;
1352 
1353  /// Parse an 'x' token in a dimension list, handling the case where the x is
1354  /// juxtaposed with an element type, as in "xf32", leaving the "f32" as the
1355  /// next token.
1356  virtual ParseResult parseXInDimensionList() = 0;
1357 
1358  /// Class used to automatically end a cyclic region on destruction.
1360  public:
1361  explicit CyclicParseReset(AsmParser *parser) : parser(parser) {}
1362 
1364  if (parser)
1365  parser->popCyclicParsing();
1366  }
1367 
1371  : parser(std::exchange(rhs.parser, nullptr)) {}
1373  parser = std::exchange(rhs.parser, nullptr);
1374  return *this;
1375  }
1376 
1377  private:
1378  AsmParser *parser;
1379  };
1380 
1381  /// Attempts to start a cyclic parsing region for `attrOrType`.
1382  /// A cyclic parsing region starts with this call and ends with the
1383  /// destruction of the returned `CyclicParseReset`. During this time,
1384  /// calling `tryStartCyclicParse` with the same attribute in any parser
1385  /// will lead to returning failure.
1386  ///
1387  /// This makes it possible to parse cyclic attributes or types by parsing a
1388  /// short from if nested within itself.
1389  template <class AttrOrTypeT>
1390  FailureOr<CyclicParseReset> tryStartCyclicParse(AttrOrTypeT attrOrType) {
1391  static_assert(
1392  std::is_base_of_v<AttributeTrait::IsMutable<AttrOrTypeT>,
1393  AttrOrTypeT> ||
1394  std::is_base_of_v<TypeTrait::IsMutable<AttrOrTypeT>, AttrOrTypeT>,
1395  "Only mutable attributes or types can be cyclic");
1396  if (failed(pushCyclicParsing(attrOrType.getAsOpaquePointer())))
1397  return failure();
1398 
1399  return CyclicParseReset(this);
1400  }
1401 
1402 protected:
1403  /// Parse a handle to a resource within the assembly format for the given
1404  /// dialect.
1405  virtual FailureOr<AsmDialectResourceHandle>
1407 
1408  /// Pushes a new attribute or type in the form of a type erased pointer
1409  /// into an internal set.
1410  /// Returns success if the type or attribute was inserted in the set or
1411  /// failure if it was already contained.
1412  virtual LogicalResult pushCyclicParsing(const void *opaquePointer) = 0;
1413 
1414  /// Removes the element that was last inserted with a successful call to
1415  /// `pushCyclicParsing`. There must be exactly one `popCyclicParsing` call
1416  /// in reverse order of all successful `pushCyclicParsing`.
1417  virtual void popCyclicParsing() = 0;
1418 
1419  //===--------------------------------------------------------------------===//
1420  // Code Completion
1421  //===--------------------------------------------------------------------===//
1422 
1423  /// Parse a keyword, or an empty string if the current location signals a code
1424  /// completion.
1425  virtual ParseResult parseKeywordOrCompletion(StringRef *keyword) = 0;
1426 
1427  /// Signal the code completion of a set of expected tokens.
1429 
1430 private:
1431  AsmParser(const AsmParser &) = delete;
1432  void operator=(const AsmParser &) = delete;
1433 };
1434 
1435 //===----------------------------------------------------------------------===//
1436 // OpAsmParser
1437 //===----------------------------------------------------------------------===//
1438 
1439 /// The OpAsmParser has methods for interacting with the asm parser: parsing
1440 /// things from it, emitting errors etc. It has an intentionally high-level API
1441 /// that is designed to reduce/constrain syntax innovation in individual
1442 /// operations.
1443 ///
1444 /// For example, consider an op like this:
1445 ///
1446 /// %x = load %p[%1, %2] : memref<...>
1447 ///
1448 /// The "%x = load" tokens are already parsed and therefore invisible to the
1449 /// custom op parser. This can be supported by calling `parseOperandList` to
1450 /// parse the %p, then calling `parseOperandList` with a `SquareDelimiter` to
1451 /// parse the indices, then calling `parseColonTypeList` to parse the result
1452 /// type.
1453 ///
1454 class OpAsmParser : public AsmParser {
1455 public:
1456  using AsmParser::AsmParser;
1457  ~OpAsmParser() override;
1458 
1459  /// Parse a loc(...) specifier if present, filling in result if so.
1460  /// Location for BlockArgument and Operation may be deferred with an alias, in
1461  /// which case an OpaqueLoc is set and will be resolved when parsing
1462  /// completes.
1463  virtual ParseResult
1464  parseOptionalLocationSpecifier(std::optional<Location> &result) = 0;
1465 
1466  /// Return the name of the specified result in the specified syntax, as well
1467  /// as the sub-element in the name. It returns an empty string and ~0U for
1468  /// invalid result numbers. For example, in this operation:
1469  ///
1470  /// %x, %y:2, %z = foo.op
1471  ///
1472  /// getResultName(0) == {"x", 0 }
1473  /// getResultName(1) == {"y", 0 }
1474  /// getResultName(2) == {"y", 1 }
1475  /// getResultName(3) == {"z", 0 }
1476  /// getResultName(4) == {"", ~0U }
1477  virtual std::pair<StringRef, unsigned>
1478  getResultName(unsigned resultNo) const = 0;
1479 
1480  /// Return the number of declared SSA results. This returns 4 for the foo.op
1481  /// example in the comment for `getResultName`.
1482  virtual size_t getNumResults() const = 0;
1483 
1484  // These methods emit an error and return failure or success. This allows
1485  // these to be chained together into a linear sequence of || expressions in
1486  // many cases.
1487 
1488  /// Parse an operation in its generic form.
1489  /// The parsed operation is parsed in the current context and inserted in the
1490  /// provided block and insertion point. The results produced by this operation
1491  /// aren't mapped to any named value in the parser. Returns nullptr on
1492  /// failure.
1493  virtual Operation *parseGenericOperation(Block *insertBlock,
1494  Block::iterator insertPt) = 0;
1495 
1496  /// Parse the name of an operation, in the custom form. On success, return a
1497  /// an object of type 'OperationName'. Otherwise, failure is returned.
1498  virtual FailureOr<OperationName> parseCustomOperationName() = 0;
1499 
1500  //===--------------------------------------------------------------------===//
1501  // Operand Parsing
1502  //===--------------------------------------------------------------------===//
1503 
1504  /// This is the representation of an operand reference.
1506  SMLoc location; // Location of the token.
1507  StringRef name; // Value name, e.g. %42 or %abc
1508  unsigned number; // Number, e.g. 12 for an operand like %xyz#12
1509  };
1510 
1511  /// Parse different components, viz., use-info of operand(s), successor(s),
1512  /// region(s), attribute(s) and function-type, of the generic form of an
1513  /// operation instance and populate the input operation-state 'result' with
1514  /// those components. If any of the components is explicitly provided, then
1515  /// skip parsing that component.
1517  OperationState &result,
1518  std::optional<ArrayRef<UnresolvedOperand>> parsedOperandType =
1519  std::nullopt,
1520  std::optional<ArrayRef<Block *>> parsedSuccessors = std::nullopt,
1521  std::optional<MutableArrayRef<std::unique_ptr<Region>>> parsedRegions =
1522  std::nullopt,
1523  std::optional<ArrayRef<NamedAttribute>> parsedAttributes = std::nullopt,
1524  std::optional<Attribute> parsedPropertiesAttribute = std::nullopt,
1525  std::optional<FunctionType> parsedFnType = std::nullopt) = 0;
1526 
1527  /// Parse a single SSA value operand name along with a result number if
1528  /// `allowResultNumber` is true.
1529  virtual ParseResult parseOperand(UnresolvedOperand &result,
1530  bool allowResultNumber = true) = 0;
1531 
1532  /// Parse a single operand if present.
1533  virtual OptionalParseResult
1535  bool allowResultNumber = true) = 0;
1536 
1537  /// Parse zero or more SSA comma-separated operand references with a specified
1538  /// surrounding delimiter, and an optional required operand count.
1539  virtual ParseResult
1541  Delimiter delimiter = Delimiter::None,
1542  bool allowResultNumber = true,
1543  int requiredOperandCount = -1) = 0;
1544 
1545  /// Parse a specified number of comma separated operands.
1547  int requiredOperandCount,
1548  Delimiter delimiter = Delimiter::None) {
1549  return parseOperandList(result, delimiter,
1550  /*allowResultNumber=*/true, requiredOperandCount);
1551  }
1552 
1553  /// Parse zero or more trailing SSA comma-separated trailing operand
1554  /// references with a specified surrounding delimiter, and an optional
1555  /// required operand count. A leading comma is expected before the
1556  /// operands.
1557  ParseResult
1559  Delimiter delimiter = Delimiter::None) {
1560  if (failed(parseOptionalComma()))
1561  return success(); // The comma is optional.
1562  return parseOperandList(result, delimiter);
1563  }
1564 
1565  /// Resolve an operand to an SSA value, emitting an error on failure.
1566  virtual ParseResult resolveOperand(const UnresolvedOperand &operand,
1567  Type type,
1568  SmallVectorImpl<Value> &result) = 0;
1569 
1570  /// Resolve a list of operands to SSA values, emitting an error on failure, or
1571  /// appending the results to the list on success. This method should be used
1572  /// when all operands have the same type.
1573  template <typename Operands = ArrayRef<UnresolvedOperand>>
1574  ParseResult resolveOperands(Operands &&operands, Type type,
1575  SmallVectorImpl<Value> &result) {
1576  for (const UnresolvedOperand &operand : operands)
1577  if (resolveOperand(operand, type, result))
1578  return failure();
1579  return success();
1580  }
1581  template <typename Operands = ArrayRef<UnresolvedOperand>>
1582  ParseResult resolveOperands(Operands &&operands, Type type, SMLoc loc,
1583  SmallVectorImpl<Value> &result) {
1584  return resolveOperands(std::forward<Operands>(operands), type, result);
1585  }
1586 
1587  /// Resolve a list of operands and a list of operand types to SSA values,
1588  /// emitting an error and returning failure, or appending the results
1589  /// to the list on success.
1590  template <typename Operands = ArrayRef<UnresolvedOperand>,
1591  typename Types = ArrayRef<Type>>
1592  std::enable_if_t<!std::is_convertible<Types, Type>::value, ParseResult>
1593  resolveOperands(Operands &&operands, Types &&types, SMLoc loc,
1594  SmallVectorImpl<Value> &result) {
1595  size_t operandSize = llvm::range_size(operands);
1596  size_t typeSize = llvm::range_size(types);
1597  if (operandSize != typeSize)
1598  return emitError(loc)
1599  << operandSize << " operands present, but expected " << typeSize;
1600 
1601  for (auto [operand, type] : llvm::zip_equal(operands, types))
1602  if (resolveOperand(operand, type, result))
1603  return failure();
1604  return success();
1605  }
1606 
1607  /// Parses an affine map attribute where dims and symbols are SSA operands.
1608  /// Operand values must come from single-result sources, and be valid
1609  /// dimensions/symbol identifiers according to mlir::isValidDim/Symbol.
1610  virtual ParseResult
1612  Attribute &map, StringRef attrName,
1613  NamedAttrList &attrs,
1614  Delimiter delimiter = Delimiter::Square) = 0;
1615 
1616  /// Parses an affine expression where dims and symbols are SSA operands.
1617  /// Operand values must come from single-result sources, and be valid
1618  /// dimensions/symbol identifiers according to mlir::isValidDim/Symbol.
1619  virtual ParseResult
1621  SmallVectorImpl<UnresolvedOperand> &symbOperands,
1622  AffineExpr &expr) = 0;
1623 
1624  //===--------------------------------------------------------------------===//
1625  // Argument Parsing
1626  //===--------------------------------------------------------------------===//
1627 
1628  struct Argument {
1629  UnresolvedOperand ssaName; // SourceLoc, SSA name, result #.
1630  Type type; // Type.
1631  DictionaryAttr attrs; // Attributes if present.
1632  std::optional<Location> sourceLoc; // Source location specifier if present.
1633  };
1634 
1635  /// Parse a single argument with the following syntax:
1636  ///
1637  /// `%ssaName : !type { optionalAttrDict} loc(optionalSourceLoc)`
1638  ///
1639  /// If `allowType` is false or `allowAttrs` are false then the respective
1640  /// parts of the grammar are not parsed.
1641  virtual ParseResult parseArgument(Argument &result, bool allowType = false,
1642  bool allowAttrs = false) = 0;
1643 
1644  /// Parse a single argument if present.
1645  virtual OptionalParseResult
1646  parseOptionalArgument(Argument &result, bool allowType = false,
1647  bool allowAttrs = false) = 0;
1648 
1649  /// Parse zero or more arguments with a specified surrounding delimiter.
1650  virtual ParseResult parseArgumentList(SmallVectorImpl<Argument> &result,
1651  Delimiter delimiter = Delimiter::None,
1652  bool allowType = false,
1653  bool allowAttrs = false) = 0;
1654 
1655  //===--------------------------------------------------------------------===//
1656  // Region Parsing
1657  //===--------------------------------------------------------------------===//
1658 
1659  /// Parses a region. Any parsed blocks are appended to 'region' and must be
1660  /// moved to the op regions after the op is created. The first block of the
1661  /// region takes 'arguments'.
1662  ///
1663  /// If 'enableNameShadowing' is set to true, the argument names are allowed to
1664  /// shadow the names of other existing SSA values defined above the region
1665  /// scope. 'enableNameShadowing' can only be set to true for regions attached
1666  /// to operations that are 'IsolatedFromAbove'.
1667  virtual ParseResult parseRegion(Region &region,
1668  ArrayRef<Argument> arguments = {},
1669  bool enableNameShadowing = false) = 0;
1670 
1671  /// Parses a region if present.
1672  virtual OptionalParseResult
1674  bool enableNameShadowing = false) = 0;
1675 
1676  /// Parses a region if present. If the region is present, a new region is
1677  /// allocated and placed in `region`. If no region is present or on failure,
1678  /// `region` remains untouched.
1679  virtual OptionalParseResult
1680  parseOptionalRegion(std::unique_ptr<Region> &region,
1681  ArrayRef<Argument> arguments = {},
1682  bool enableNameShadowing = false) = 0;
1683 
1684  //===--------------------------------------------------------------------===//
1685  // Successor Parsing
1686  //===--------------------------------------------------------------------===//
1687 
1688  /// Parse a single operation successor.
1689  virtual ParseResult parseSuccessor(Block *&dest) = 0;
1690 
1691  /// Parse an optional operation successor.
1693 
1694  /// Parse a single operation successor and its operand list.
1695  virtual ParseResult
1697 
1698  //===--------------------------------------------------------------------===//
1699  // Type Parsing
1700  //===--------------------------------------------------------------------===//
1701 
1702  /// Parse a list of assignments of the form
1703  /// (%x1 = %y1, %x2 = %y2, ...)
1707  if (!result.has_value())
1708  return emitError(getCurrentLocation(), "expected '('");
1709  return result.value();
1710  }
1711 
1712  virtual OptionalParseResult
1715 };
1716 
1717 //===--------------------------------------------------------------------===//
1718 // Dialect OpAsm interface.
1719 //===--------------------------------------------------------------------===//
1720 
1721 /// A functor used to set the name of the start of a result group of an
1722 /// operation. See 'getAsmResultNames' below for more details.
1723 using OpAsmSetValueNameFn = function_ref<void(Value, StringRef)>;
1724 
1725 /// A functor used to set the name of blocks in regions directly nested under
1726 /// an operation.
1727 using OpAsmSetBlockNameFn = function_ref<void(Block *, StringRef)>;
1728 
1730  : public DialectInterface::Base<OpAsmDialectInterface> {
1731 public:
1732  OpAsmDialectInterface(Dialect *dialect) : Base(dialect) {}
1733 
1734  //===------------------------------------------------------------------===//
1735  // Aliases
1736  //===------------------------------------------------------------------===//
1737 
1738  /// Holds the result of `getAlias` hook call.
1739  enum class AliasResult {
1740  /// The object (type or attribute) is not supported by the hook
1741  /// and an alias was not provided.
1742  NoAlias,
1743  /// An alias was provided, but it might be overriden by other hook.
1745  /// An alias was provided and it should be used
1746  /// (no other hooks will be checked).
1747  FinalAlias
1748  };
1749 
1750  /// Hooks for getting an alias identifier alias for a given symbol, that is
1751  /// not necessarily a part of this dialect. The identifier is used in place of
1752  /// the symbol when printing textual IR. These aliases must not contain `.` or
1753  /// end with a numeric digit([0-9]+).
1754  virtual AliasResult getAlias(Attribute attr, raw_ostream &os) const {
1755  return AliasResult::NoAlias;
1756  }
1757  virtual AliasResult getAlias(Type type, raw_ostream &os) const {
1758  return AliasResult::NoAlias;
1759  }
1760 
1761  //===--------------------------------------------------------------------===//
1762  // Resources
1763  //===--------------------------------------------------------------------===//
1764 
1765  /// Declare a resource with the given key, returning a handle to use for any
1766  /// references of this resource key within the IR during parsing. The result
1767  /// of `getResourceKey` on the returned handle is permitted to be different
1768  /// than `key`.
1769  virtual FailureOr<AsmDialectResourceHandle>
1770  declareResource(StringRef key) const {
1771  return failure();
1772  }
1773 
1774  /// Return a key to use for the given resource. This key should uniquely
1775  /// identify this resource within the dialect.
1776  virtual std::string
1778  llvm_unreachable(
1779  "Dialect must implement `getResourceKey` when defining resources");
1780  }
1781 
1782  /// Hook for parsing resource entries. Returns failure if the entry was not
1783  /// valid, or could otherwise not be processed correctly. Any necessary errors
1784  /// can be emitted via the provided entry.
1785  virtual LogicalResult parseResource(AsmParsedResourceEntry &entry) const;
1786 
1787  /// Hook for building resources to use during printing. The given `op` may be
1788  /// inspected to help determine what information to include.
1789  /// `referencedResources` contains all of the resources detected when printing
1790  /// 'op'.
1791  virtual void
1793  const SetVector<AsmDialectResourceHandle> &referencedResources,
1794  AsmResourceBuilder &builder) const {}
1795 };
1796 
1797 //===--------------------------------------------------------------------===//
1798 // Custom printers and parsers.
1799 //===--------------------------------------------------------------------===//
1800 
1801 // Handles custom<DimensionList>(...) in TableGen.
1802 void printDimensionList(OpAsmPrinter &printer, Operation *op,
1803  ArrayRef<int64_t> dimensions);
1804 ParseResult parseDimensionList(OpAsmParser &parser,
1805  DenseI64ArrayAttr &dimensions);
1806 
1807 } // namespace mlir
1808 
1809 //===--------------------------------------------------------------------===//
1810 // Operation OpAsm interface.
1811 //===--------------------------------------------------------------------===//
1812 
1813 /// The OpAsmOpInterface, see OpAsmInterface.td for more details.
1814 #include "mlir/IR/OpAsmInterface.h.inc"
1815 
1816 namespace llvm {
1817 template <>
1818 struct DenseMapInfo<mlir::AsmDialectResourceHandle> {
1822  }
1826  }
1827  static unsigned getHashValue(const mlir::AsmDialectResourceHandle &handle) {
1829  }
1830  static bool isEqual(const mlir::AsmDialectResourceHandle &lhs,
1831  const mlir::AsmDialectResourceHandle &rhs) {
1832  return lhs.getResource() == rhs.getResource();
1833  }
1834 };
1835 } // namespace llvm
1836 
1837 #endif
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
Base type for affine expression.
Definition: AffineExpr.h:68
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
Definition: AffineMap.h:46
This class represents a CRTP base class for dialect resource handles.
const ResourceT * getResource() const
AsmDialectResourceHandleBase(AsmDialectResourceHandle handle)
ResourceT * getResource()
Return the resource referenced by this handle.
static bool classof(const AsmDialectResourceHandle *handle)
Support llvm style casting.
AsmDialectResourceHandleBase(ResourceT *resource, DialectT *dialect)
Construct a handle from a pointer to the resource.
DialectT * getDialect() const
Return the dialect that owns the resource.
This class represents an opaque handle to a dialect resource entry.
Dialect * getDialect() const
Return the dialect that owns the resource.
TypeID getTypeID() const
Return the type ID of the resource.
bool operator==(const AsmDialectResourceHandle &other) const
AsmDialectResourceHandle(void *resource, TypeID resourceID, Dialect *dialect)
void * getResource() const
Return an opaque pointer to the referenced resource.
This class represents a single parsed resource entry.
Definition: AsmState.h:283
Class used to automatically end a cyclic region on destruction.
CyclicParseReset & operator=(CyclicParseReset &&rhs)
CyclicParseReset & operator=(const CyclicParseReset &)=delete
CyclicParseReset(const CyclicParseReset &)=delete
CyclicParseReset(CyclicParseReset &&rhs)
This class represents a StringSwitch like class that is useful for parsing expected keywords.
std::enable_if_t<!std::is_convertible< FnT, ResultT >::value, KeywordSwitch & > Default(FnT &&fn)
KeywordSwitch & Case(StringLiteral str, ResultT value)
Case that uses the provided value when true.
bool hasValue() const
Returns true if this switch has a value yet.
std::enable_if_t<!std::is_convertible< FnT, ResultT >::value, KeywordSwitch & > Case(StringLiteral str, FnT &&fn)
Case that invokes the provided functor when true.
KeywordSwitch & Default(ResultT value)
KeywordSwitch(AsmParser &parser, StringRef *keyword=nullptr)
This base class exposes generic asm parser hooks, usable across the various derived parsers.
llvm::is_detected< has_parse_method, AttrType > detect_has_parse_method
ParseResult parseSymbolName(StringAttr &result)
Parse an -identifier and store it (without the '@' symbol) in a string attribute.
virtual ParseResult parseLBrace()=0
Parse a { token.
Delimiter
These are the supported delimiters around operand lists and region argument lists,...
@ Paren
Parens surrounding zero or more operands.
@ None
Zero or more operands with no delimiters.
@ OptionalLessGreater
<> brackets supporting zero or more ops, or nothing.
@ Braces
{} brackets surrounding zero or more operands.
@ OptionalBraces
{} brackets surrounding zero or more operands, or nothing.
@ OptionalParen
Parens supporting zero or more operands, or nothing.
@ Square
Square brackets surrounding zero or more operands.
@ LessGreater
<> brackets surrounding zero or more operands.
@ OptionalSquare
Square brackets supporting zero or more ops, or nothing.
virtual OptionalParseResult parseOptionalInteger(APInt &result)=0
Parse an optional integer value from the stream.
AsmParser()=default
virtual ParseResult parseColonTypeList(SmallVectorImpl< Type > &result)=0
Parse a colon followed by a type list, which must have at least one type.
virtual ParseResult parseIntegerSet(IntegerSet &set)=0
Parse an integer set instance into 'set'.
virtual ParseResult parseOptionalKeywordOrString(std::string *result)=0
Parse an optional keyword or string.
virtual ParseResult parseOptionalSymbolName(StringAttr &result)=0
Parse an optional -identifier and store it (without the '@' symbol) in a string attribute.
FailureOr< CyclicParseReset > tryStartCyclicParse(AttrOrTypeT attrOrType)
Attempts to start a cyclic parsing region for attrOrType.
virtual ParseResult parseOptionalRBrace()=0
Parse a } token if present.
ParseResult parseDecimalInteger(IntT &result)
Parse a decimal integer value from the stream.
virtual ParseResult parsePlus()=0
Parse a '+' token.
ParseResult parseKeyword(StringRef *keyword)
Parse a keyword into 'keyword'.
virtual ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())=0
Parse a list of comma-separated items with an optional delimiter.
decltype(AttrType::parse(std::declval< AsmParser & >(), std::declval< Type >())) has_parse_method
Trait to check if AttrType provides a parse method.
virtual void popCyclicParsing()=0
Removes the element that was last inserted with a successful call to pushCyclicParsing.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseOptionalAttrDict(NamedAttrList &result)=0
Parse a named dictionary into 'result' if it is present.
virtual ParseResult parseOptionalEqual()=0
Parse a = token if present.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
virtual OptionalParseResult parseOptionalType(Type &result)=0
Parse an optional type.
MLIRContext * getContext() const
Definition: AsmPrinter.cpp:73
virtual Location getEncodedSourceLoc(SMLoc loc)=0
Re-encode the given source location as an MLIR location and return it.
std::enable_if_t<!detect_type_has_parse_method< TypeT >::value, ParseResult > parseCustomTypeWithFallback(TypeT &result)
SFINAE parsing method for Type that don't implement a parse method.
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseOptionalColon()=0
Parse a : token if present.
virtual ParseResult parseLSquare()=0
Parse a [ token.
virtual ParseResult parseRSquare()=0
Parse a ] token.
virtual ParseResult parseOptionalColonTypeList(SmallVectorImpl< Type > &result)=0
Parse an optional colon followed by a type list, which if present must have at least one type.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseOptionalArrow()=0
Parse a '->' token if present.
ParseResult parseOptionalSymbolName(StringAttr &result, StringRef attrName, NamedAttrList &attrs)
Parse an optional -identifier and store it (without the '@' symbol) in a string attribute named 'attr...
virtual OptionalParseResult parseOptionalDecimalInteger(APInt &result)=0
ParseResult parseAttribute(AttrType &result, Type type, StringRef attrName, NamedAttrList &attrs)
Parse an arbitrary attribute of a given type and populate it in result.
ParseResult parseAttribute(AttrType &result, Type type={})
Parse an attribute of a specific kind and type.
ParseResult parseKeywordOrString(std::string *result)
Parse a keyword or a quoted string.
virtual void codeCompleteExpectedTokens(ArrayRef< StringRef > tokens)=0
Signal the code completion of a set of expected tokens.
virtual ParseResult parseRBrace()=0
Parse a } token.
virtual ParseResult parseAffineMap(AffineMap &map)=0
Parse an affine map instance into 'map'.
ParseResult addTypeToList(Type type, SmallVectorImpl< Type > &result)
Add the specified type to the end of the specified type list and return success.
virtual ParseResult parseOptionalRParen()=0
Parse a ) token if present.
virtual ParseResult parseCustomAttributeWithFallback(Attribute &result, Type type, function_ref< ParseResult(Attribute &result, Type type)> parseAttribute)=0
Parse a custom attribute with the provided callback, unless the next token is #, in which case the ge...
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual ParseResult parseDimensionList(SmallVectorImpl< int64_t > &dimensions, bool allowDynamic=true, bool withTrailingX=true)=0
Parse a dimension list of a tensor or memref type.
ParseResult parseString(std::string *string)
Parse a quoted string token.
virtual ParseResult parseOptionalPlus()=0
Parse a '+' token if present.
virtual ParseResult parseOptionalKeyword(StringRef *keyword, ArrayRef< StringRef > allowedValues)=0
Parse a keyword, if present, and if one of the 'allowedValues', into 'keyword'.
virtual ParseResult parseOptionalGreater()=0
Parse a '>' token if present.
virtual ParseResult parseEqual()=0
Parse a = token.
std::enable_if_t<!detect_has_parse_method< AttrType >::value, ParseResult > parseCustomAttributeWithFallback(AttrType &result, Type type, StringRef attrName, NamedAttrList &attrs)
SFINAE parsing method for Attribute that don't implement a parse method.
virtual ParseResult parseCustomTypeWithFallback(Type &result, function_ref< ParseResult(Type &result)> parseType)=0
Parse a custom type with the provided callback, unless the next token is #, in which case the generic...
virtual ParseResult parseFloat(const llvm::fltSemantics &semantics, APFloat &result)=0
Parse a floating point value into APFloat from the stream.
virtual OptionalParseResult parseOptionalAttribute(ArrayAttr &result, Type type={})=0
Parse an optional array attribute and return it in result.
virtual ParseResult parseStar()=0
Parse a '*' token.
virtual ParseResult parseOptionalAttrDictWithKeyword(NamedAttrList &result)=0
Parse a named dictionary into 'result' if the attributes keyword is present.
virtual ParseResult parseColonType(Type &result)=0
Parse a colon followed by a type.
virtual OptionalParseResult parseOptionalAttribute(Attribute &result, Type type={})=0
Parse an arbitrary optional attribute of a given type and return it in result.
std::enable_if_t< detect_has_parse_method< AttrType >::value, ParseResult > parseCustomAttributeWithFallback(AttrType &result, Type type={})
Parse a custom attribute of a given type unless the next token is #, in which case the generic parser...
ParseResult parseCommaSeparatedList(function_ref< ParseResult()> parseElementFn)
Parse a comma separated list of elements that must have at least one entry in it.
virtual ParseResult parseVerticalBar()=0
Parse a '|' token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseOptionalComma()=0
Parse a , token if present.
OptionalParseResult parseOptionalAttribute(AttrType &result, StringRef attrName, NamedAttrList &attrs)
Parse an optional attribute of a specific type and add it to the list with the specified name.
auto getChecked(SMLoc loc, ParamsT &&...params)
Invoke the getChecked method of the given Attribute or Type class, using the provided location to emi...
virtual ParseResult parseColon()=0
Parse a : token.
ParseResult addTypesToList(ArrayRef< Type > types, SmallVectorImpl< Type > &result)
Add the specified types to the end of the specified type list and return success.
std::enable_if_t< detect_type_has_parse_method< TypeT >::value, ParseResult > parseCustomTypeWithFallback(TypeT &result)
Parse a custom Type of a given type unless the next token is #, in which case the generic parser is i...
ParseResult parseAttribute(Attribute &result, StringRef attrName, NamedAttrList &attrs)
Parse an arbitrary attribute and return it in result.
virtual OptionalParseResult parseOptionalAttribute(StringAttr &result, Type type={})=0
Parse an optional string attribute and return it in result.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseOptionalString(std::string *string)=0
Parse a quoted string token if present.
OptionalParseResult parseOptionalInteger(IntT &result)
ParseResult getCurrentLocation(SMLoc *loc)
virtual ParseResult parseOptionalLess()=0
Parse a '<' token if present.
virtual ParseResult parseOptionalStar()=0
Parse a '*' token if present.
OptionalParseResult parseOptionalAttribute(AttrType &result, Type type, StringRef attrName, NamedAttrList &attrs)
Parse an optional attribute of a specific type and add it to the list with the specified name.
virtual OptionalParseResult parseOptionalAttribute(SymbolRefAttr &result, Type type={})=0
Parse an optional symbol ref attribute and return it in result.
virtual ParseResult parseQuestion()=0
Parse a '?' token.
ParseResult parseType(TypeT &result)
Parse a type of a specific type.
virtual ParseResult parseOptionalRSquare()=0
Parse a ] token if present.
virtual ParseResult parseArrow()=0
Parse a '->' token.
ParseResult parseColonType(TypeType &result)
Parse a colon followed by a type of a specific kind, e.g. a FunctionType.
virtual ParseResult parseGreater()=0
Parse a '>' token.
OptionalParseResult parseOptionalDecimalInteger(IntT &result)
virtual ParseResult parseLParen()=0
Parse a ( token.
virtual ParseResult parseOptionalEllipsis()=0
Parse a ... token if present;.
std::enable_if_t<!detect_has_parse_method< AttrType >::value, ParseResult > parseCustomAttributeWithFallback(AttrType &result, Type type={})
SFINAE parsing method for Attribute that don't implement a parse method.
virtual ParseResult parseType(Type &result)=0
Parse a type.
ParseResult parseAttribute(AttrType &result, StringRef attrName, NamedAttrList &attrs)
Parse an attribute of a specific kind and type.
virtual ParseResult parseEllipsis()=0
Parse a ... token.
auto getChecked(ParamsT &&...params)
A variant of getChecked that uses the result of getNameLoc to emit errors.
virtual ParseResult parseAffineExpr(ArrayRef< std::pair< StringRef, AffineExpr >> symbolSet, AffineExpr &expr)=0
Parse an affine expr instance into 'expr' using the already computed mapping from symbols to affine e...
virtual ParseResult parseComma()=0
Parse a , token.
virtual ParseResult parseOptionalArrowTypeList(SmallVectorImpl< Type > &result)=0
Parse an optional arrow followed by a type list.
virtual ParseResult parseOptionalLParen()=0
Parse a ( token if present.
ParseResult parseKeywordType(const char *keyword, Type &result)
Parse a keyword followed by a type.
virtual ParseResult parseArrowTypeList(SmallVectorImpl< Type > &result)=0
Parse an arrow followed by a type list.
virtual ~AsmParser()
virtual ParseResult parseOptionalVerticalBar()=0
Parse a '|' token if present.
ParseResult parseTypeList(SmallVectorImpl< Type > &result)
Parse a type list.
Definition: AsmPrinter.cpp:77
virtual ParseResult parseBase64Bytes(std::vector< char > *bytes)=0
Parses a Base64 encoded string of bytes.
llvm::is_detected< type_has_parse_method, TypeT > detect_type_has_parse_method
virtual ParseResult parseKeywordOrCompletion(StringRef *keyword)=0
Parse a keyword, or an empty string if the current location signals a code completion.
virtual ParseResult parseFloat(double &result)=0
Parse a floating point value from the stream.
ParseResult parseSymbolName(StringAttr &result, StringRef attrName, NamedAttrList &attrs)
Parse an -identifier and store it (without the '@' symbol) in a string attribute named 'attrName'.
virtual ParseResult parseOptionalKeyword(StringRef *keyword)=0
Parse a keyword, if present, into 'keyword'.
decltype(TypeT::parse(std::declval< AsmParser & >())) type_has_parse_method
Trait to check if TypeT provides a parse method.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
virtual ParseResult parseOptionalLSquare()=0
Parse a [ token if present.
virtual LogicalResult pushCyclicParsing(const void *opaquePointer)=0
Pushes a new attribute or type in the form of a type erased pointer into an internal set.
virtual FailureOr< AsmDialectResourceHandle > parseResourceHandle(Dialect *dialect)=0
Parse a handle to a resource within the assembly format for the given dialect.
virtual ParseResult parseOptionalQuestion()=0
Parse a '?' token if present.
std::enable_if_t< detect_has_parse_method< AttrType >::value, ParseResult > parseCustomAttributeWithFallback(AttrType &result, Type type, StringRef attrName, NamedAttrList &attrs)
Parse a custom attribute of a given type unless the next token is #, in which case the generic parser...
FailureOr< ResourceT > parseResourceHandle()
Parse a handle to a resource within the assembly format.
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
virtual ParseResult parseXInDimensionList()=0
Parse an 'x' token in a dimension list, handling the case where the x is juxtaposed with an element t...
virtual ParseResult parseOptionalLBrace()=0
Parse a { token if present.
virtual ParseResult parseKeyword(StringRef keyword, const Twine &msg)=0
Class used to automatically end a cyclic region on destruction.
CyclicPrintReset & operator=(const CyclicPrintReset &)=delete
CyclicPrintReset(const CyclicPrintReset &)=delete
CyclicPrintReset & operator=(CyclicPrintReset &&rhs)
CyclicPrintReset(CyclicPrintReset &&rhs)
This base class exposes generic asm printer hooks, usable across the various derived printers.
void printStrippedAttrOrType(ArrayRef< AttrOrType > attrOrTypes)
Print the provided array of attributes or types in the context of an operation custom printer/parser:...
FailureOr< CyclicPrintReset > tryStartCyclicPrint(AttrOrTypeT attrOrType)
Attempts to start a cyclic printing region for attrOrType.
virtual void printAttributeWithoutType(Attribute attr)
Print the given attribute without its type.
virtual LogicalResult printAlias(Attribute attr)
Print the alias for the given attribute, return failure if no alias could be printed.
virtual void popCyclicPrinting()
Removes the element that was last inserted with a successful call to pushCyclicPrinting.
AsmPrinter()=default
Initialize the printer with no internal implementation.
void printFunctionalType(InputRangeT &&inputs, ResultRangeT &&results)
Print the two given type ranges in a functional form.
virtual LogicalResult pushCyclicPrinting(const void *opaquePointer)
Pushes a new attribute or type in the form of a type erased pointer into an internal set.
virtual void printType(Type type)
virtual void printKeywordOrString(StringRef keyword)
Print the given string as a keyword, or a quoted and escaped string if it has any special or non-prin...
llvm::is_detected< has_print_method, AttrOrType > detect_has_print_method
virtual void printSymbolName(StringRef symbolRef)
Print the given string as a symbol reference, i.e.
virtual void printString(StringRef string)
Print the given string as a quoted string, escaping any special or non-printable characters in it.
void printOptionalArrowTypeList(TypeRange &&types)
Print an optional arrow followed by a type list.
virtual void printAttribute(Attribute attr)
void printDimensionList(ArrayRef< int64_t > shape)
void printArrowTypeList(TypeRange &&types)
virtual ~AsmPrinter()
virtual raw_ostream & getStream() const
Return the raw output stream used by this printer.
virtual void printResourceHandle(const AsmDialectResourceHandle &resource)
Print a handle to the given dialect resource.
decltype(std::declval< AttrOrType >().print(std::declval< AsmPrinter & >())) has_print_method
Trait to check if AttrType provides a print method.
virtual void printFloat(const APFloat &value)
Print the given floating point value in a stabilized form that can be roundtripped through the IR.
void printStrippedAttrOrType(AttrOrType attrOrType)
Print the provided attribute in the context of an operation custom printer/parser: this will invoke d...
AsmPrinter(Impl &impl)
Initialize the printer with the given internal implementation.
This class is used to build resource entries for use by the printer.
Definition: AsmState.h:239
Attributes are known-constant values of operations.
Definition: Attributes.h:25
This class represents an argument of a Block.
Definition: Value.h:319
Block represents an ordered list of Operations.
Definition: Block.h:31
OpListType::iterator iterator
Definition: Block.h:138
This class is a general helper class for creating context-global objects like types,...
Definition: Builders.h:50
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:313
An integer set representing a conjunction of one or more affine equalities and inequalities.
Definition: IntegerSet.h:44
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
T * getOrLoadDialect()
Get (or create) a dialect for the given derived dialect type.
Definition: MLIRContext.h:97
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
void append(StringRef name, Attribute attr)
Add an attribute with the specified name.
virtual void buildResources(Operation *op, const SetVector< AsmDialectResourceHandle > &referencedResources, AsmResourceBuilder &builder) const
Hook for building resources to use during printing.
virtual AliasResult getAlias(Attribute attr, raw_ostream &os) const
Hooks for getting an alias identifier alias for a given symbol, that is not necessarily a part of thi...
OpAsmDialectInterface(Dialect *dialect)
virtual AliasResult getAlias(Type type, raw_ostream &os) const
virtual std::string getResourceKey(const AsmDialectResourceHandle &handle) const
Return a key to use for the given resource.
virtual FailureOr< AsmDialectResourceHandle > declareResource(StringRef key) const
Declare a resource with the given key, returning a handle to use for any references of this resource ...
virtual LogicalResult parseResource(AsmParsedResourceEntry &entry) const
Hook for parsing resource entries.
Definition: AsmPrinter.cpp:130
AliasResult
Holds the result of getAlias hook call.
@ FinalAlias
An alias was provided and it should be used (no other hooks will be checked).
@ NoAlias
The object (type or attribute) is not supported by the hook and an alias was not provided.
@ OverridableAlias
An alias was provided, but it might be overriden by other hook.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
virtual std::pair< StringRef, unsigned > getResultName(unsigned resultNo) const =0
Return the name of the specified result in the specified syntax, as well as the sub-element in the na...
virtual size_t getNumResults() const =0
Return the number of declared SSA results.
virtual OptionalParseResult parseOptionalAssignmentList(SmallVectorImpl< Argument > &lhs, SmallVectorImpl< UnresolvedOperand > &rhs)=0
virtual ParseResult parseRegion(Region &region, ArrayRef< Argument > arguments={}, bool enableNameShadowing=false)=0
Parses a region.
~OpAsmParser() override
virtual ParseResult parseSuccessor(Block *&dest)=0
Parse a single operation successor.
virtual ParseResult parseArgument(Argument &result, bool allowType=false, bool allowAttrs=false)=0
Parse a single argument with the following syntax:
virtual ParseResult parseGenericOperationAfterOpName(OperationState &result, std::optional< ArrayRef< UnresolvedOperand >> parsedOperandType=std::nullopt, std::optional< ArrayRef< Block * >> parsedSuccessors=std::nullopt, std::optional< MutableArrayRef< std::unique_ptr< Region >>> parsedRegions=std::nullopt, std::optional< ArrayRef< NamedAttribute >> parsedAttributes=std::nullopt, std::optional< Attribute > parsedPropertiesAttribute=std::nullopt, std::optional< FunctionType > parsedFnType=std::nullopt)=0
Parse different components, viz., use-info of operand(s), successor(s), region(s),...
ParseResult parseTrailingOperandList(SmallVectorImpl< UnresolvedOperand > &result, Delimiter delimiter=Delimiter::None)
Parse zero or more trailing SSA comma-separated trailing operand references with a specified surround...
ParseResult resolveOperands(Operands &&operands, Type type, SMLoc loc, SmallVectorImpl< Value > &result)
virtual ParseResult parseArgumentList(SmallVectorImpl< Argument > &result, Delimiter delimiter=Delimiter::None, bool allowType=false, bool allowAttrs=false)=0
Parse zero or more arguments with a specified surrounding delimiter.
virtual ParseResult parseAffineMapOfSSAIds(SmallVectorImpl< UnresolvedOperand > &operands, Attribute &map, StringRef attrName, NamedAttrList &attrs, Delimiter delimiter=Delimiter::Square)=0
Parses an affine map attribute where dims and symbols are SSA operands.
virtual OptionalParseResult parseOptionalArgument(Argument &result, bool allowType=false, bool allowAttrs=false)=0
Parse a single argument if present.
virtual ParseResult parseOptionalLocationSpecifier(std::optional< Location > &result)=0
Parse a loc(...) specifier if present, filling in result if so.
ParseResult parseAssignmentList(SmallVectorImpl< Argument > &lhs, SmallVectorImpl< UnresolvedOperand > &rhs)
Parse a list of assignments of the form (x1 = y1, x2 = y2, ...)
virtual ParseResult resolveOperand(const UnresolvedOperand &operand, Type type, SmallVectorImpl< Value > &result)=0
Resolve an operand to an SSA value, emitting an error on failure.
virtual OptionalParseResult parseOptionalOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single operand if present.
virtual ParseResult parseSuccessorAndUseList(Block *&dest, SmallVectorImpl< Value > &operands)=0
Parse a single operation successor and its operand list.
virtual OptionalParseResult parseOptionalRegion(Region &region, ArrayRef< Argument > arguments={}, bool enableNameShadowing=false)=0
Parses a region if present.
virtual FailureOr< OperationName > parseCustomOperationName()=0
Parse the name of an operation, in the custom form.
ParseResult parseOperandList(SmallVectorImpl< UnresolvedOperand > &result, int requiredOperandCount, Delimiter delimiter=Delimiter::None)
Parse a specified number of comma separated operands.
ParseResult resolveOperands(Operands &&operands, Type type, SmallVectorImpl< Value > &result)
Resolve a list of operands to SSA values, emitting an error on failure, or appending the results to t...
virtual Operation * parseGenericOperation(Block *insertBlock, Block::iterator insertPt)=0
Parse an operation in its generic form.
virtual ParseResult parseOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single SSA value operand name along with a result number if allowResultNumber is true.
virtual ParseResult parseAffineExprOfSSAIds(SmallVectorImpl< UnresolvedOperand > &dimOperands, SmallVectorImpl< UnresolvedOperand > &symbOperands, AffineExpr &expr)=0
Parses an affine expression where dims and symbols are SSA operands.
virtual OptionalParseResult parseOptionalSuccessor(Block *&dest)=0
Parse an optional operation successor.
std::enable_if_t<!std::is_convertible< Types, Type >::value, ParseResult > resolveOperands(Operands &&operands, Types &&types, SMLoc loc, SmallVectorImpl< Value > &result)
Resolve a list of operands and a list of operand types to SSA values, emitting an error and returning...
virtual OptionalParseResult parseOptionalRegion(std::unique_ptr< Region > &region, ArrayRef< Argument > arguments={}, bool enableNameShadowing=false)=0
Parses a region if present.
virtual ParseResult parseOperandList(SmallVectorImpl< UnresolvedOperand > &result, Delimiter delimiter=Delimiter::None, bool allowResultNumber=true, int requiredOperandCount=-1)=0
Parse zero or more SSA comma-separated operand references with a specified surrounding delimiter,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
void printOperands(IteratorType it, IteratorType end)
Print a comma separated list of operands.
virtual void shadowRegionArgs(Region &region, ValueRange namesToUse)=0
Renumber the arguments for the specified region to the same names as the SSA values in namesToUse.
virtual void printNewline()=0
Print a newline and indent the printer to the start of the current operation.
virtual void increaseIndent()=0
Increase indentation.
virtual void printSuccessorAndUseList(Block *successor, ValueRange succOperands)=0
Print the successor and its operands.
void printOperands(const ContainerType &container)
Print a comma separated list of operands.
virtual void printOptionalAttrDictWithKeyword(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary prefixed with 'attribute...
virtual void printOptionalAttrDict(ArrayRef< NamedAttribute > attrs, ArrayRef< StringRef > elidedAttrs={})=0
If the specified operation has attributes, print out an attribute dictionary with their values.
virtual void printOptionalLocationSpecifier(Location loc)=0
Print a loc(...) specifier if printing debug info is enabled.
virtual void printCustomOrGenericOp(Operation *op)=0
Prints the entire operation with the custom assembly form, if available, or the generic assembly form...
virtual void decreaseIndent()=0
Decrease indentation.
virtual void printOperand(Value value, raw_ostream &os)=0
virtual void printSuccessor(Block *successor)=0
Print the given successor.
virtual void printAffineExprOfSSAIds(AffineExpr expr, ValueRange dimOperands, ValueRange symOperands)=0
Prints an affine expression of SSA ids with SSA id names used instead of dims and symbols.
void printFunctionalType(Operation *op)
Print the complete type of an operation in functional form.
Definition: AsmPrinter.cpp:94
virtual void printAffineMapOfSSAIds(AffineMapAttr mapAttr, ValueRange operands)=0
Prints an affine map of SSA ids, where SSA id names are used in place of dims/symbols.
virtual void printGenericOp(Operation *op, bool printOpName=true)=0
Print the entire operation with the default generic assembly form.
~OpAsmPrinter() override
virtual void printRegion(Region &blocks, bool printEntryBlockArgs=true, bool printBlockTerminators=true, bool printEmptyBlock=false)=0
Prints a region.
virtual void printRegionArgument(BlockArgument arg, ArrayRef< NamedAttribute > argAttrs={}, bool omitType=false)=0
Print a block argument in the usual format of: ssaName : type {attr1=42} loc("here") where location p...
virtual void printOperand(Value value)=0
Print implementations for various things an operation contains.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class implements Optional functionality for ParseResult.
Definition: OpDefinition.h:39
ParseResult value() const
Access the internal ParseResult value.
Definition: OpDefinition.h:52
bool has_value() const
Returns true if we contain a valid ParseResult value.
Definition: OpDefinition.h:49
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
This class provides an efficient unique identifier for a specific C++ type.
Definition: TypeID.h:104
This class provides an abstraction over the various different ranges of value types.
Definition: TypeRange.h:36
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
The base class used for all derived interface types.
Include the generated interface declarations.
Definition: CallGraph.h:229
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Definition: Query.cpp:20
Include the generated interface declarations.
ParseResult parseDimensionList(OpAsmParser &parser, DenseI64ArrayAttr &dimensions)
void printDimensionList(OpAsmPrinter &printer, Operation *op, ArrayRef< int64_t > dimensions)
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
llvm::hash_code hash_value(const AsmDialectResourceHandle &param)
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:260
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
Definition: AliasAnalysis.h:78
static mlir::AsmDialectResourceHandle getTombstoneKey()
static unsigned getHashValue(const mlir::AsmDialectResourceHandle &handle)
static mlir::AsmDialectResourceHandle getEmptyKey()
static bool isEqual(const mlir::AsmDialectResourceHandle &lhs, const mlir::AsmDialectResourceHandle &rhs)
std::optional< Location > sourceLoc
This is the representation of an operand reference.
This represents an operation in an abstracted form, suitable for use with the builder APIs.
This trait is used to determine if a storage user, like Type, is mutable or not.