@@ -393,6 +393,19 @@ class AsmPrinter::Impl {
393393 // / Returns the output stream of the printer.
394394 raw_ostream &getStream () { return os; }
395395
396+ // / Print a newline and indent the printer to the start of the current
397+ // / operation.
398+ void printNewline () {
399+ os << newLine;
400+ os.indent (currentIndent);
401+ }
402+
403+ // / Increase indentation.
404+ void increaseIndent () { currentIndent += indentWidth; }
405+
406+ // / Decrease indentation.
407+ void decreaseIndent () { currentIndent -= indentWidth; }
408+
396409 template <typename Container, typename UnaryFunctor>
397410 inline void interleaveComma (const Container &c, UnaryFunctor eachFn) const {
398411 llvm::interleaveComma (c, os, eachFn);
@@ -507,6 +520,12 @@ class AsmPrinter::Impl {
507520
508521 // / A tracker for the number of new lines emitted during printing.
509522 NewLineCounter newLine;
523+
524+ // / The number of spaces used for indenting nested operations.
525+ const static unsigned indentWidth = 2 ;
526+
527+ // / This is the current indentation level for nested structures.
528+ unsigned currentIndent = 0 ;
510529};
511530} // namespace mlir
512531
@@ -959,6 +978,9 @@ class DummyAliasDialectAsmPrinter : public DialectAsmPrinter {
959978
960979 // / The following are hooks of `DialectAsmPrinter` that are not necessary for
961980 // / determining potential aliases.
981+ void printNewline () override {}
982+ void increaseIndent () override {}
983+ void decreaseIndent () override {}
962984 void printFloat (const APFloat &) override {}
963985 void printKeywordOrString (StringRef) override {}
964986 void printString (StringRef) override {}
@@ -2739,6 +2761,13 @@ void AsmPrinter::Impl::printDialectAttribute(Attribute attr) {
27392761 {
27402762 llvm::raw_string_ostream attrNameStr (attrName);
27412763 Impl subPrinter (attrNameStr, state);
2764+
2765+ // The values of currentIndent and newLine are assigned to the created
2766+ // subprinter, so that the indent level and number of printed lines can be
2767+ // tracked.
2768+ subPrinter.currentIndent = currentIndent;
2769+ subPrinter.newLine = newLine;
2770+
27422771 DialectAsmPrinter printer (subPrinter);
27432772 dialect.printAttribute (attr, printer);
27442773 }
@@ -2753,6 +2782,13 @@ void AsmPrinter::Impl::printDialectType(Type type) {
27532782 {
27542783 llvm::raw_string_ostream typeNameStr (typeName);
27552784 Impl subPrinter (typeNameStr, state);
2785+
2786+ // The values of currentIndent and newLine are assigned to the created
2787+ // subprinter, so that the indent level and number of printed lines can be
2788+ // tracked.
2789+ subPrinter.currentIndent = currentIndent;
2790+ subPrinter.newLine = newLine;
2791+
27562792 DialectAsmPrinter printer (subPrinter);
27572793 dialect.printType (type, printer);
27582794 }
@@ -2793,6 +2829,21 @@ raw_ostream &AsmPrinter::getStream() const {
27932829 return impl->getStream ();
27942830}
27952831
2832+ void AsmPrinter::printNewline () {
2833+ assert (impl && " expected AsmPrinter::printNewLine to be overriden" );
2834+ impl->printNewline ();
2835+ }
2836+
2837+ void AsmPrinter::increaseIndent () {
2838+ assert (impl && " expected AsmPrinter::increaseIndent to be overriden" );
2839+ impl->increaseIndent ();
2840+ }
2841+
2842+ void AsmPrinter::decreaseIndent () {
2843+ assert (impl && " expected AsmPrinter::decreaseIndent to be overriden" );
2844+ impl->decreaseIndent ();
2845+ }
2846+
27962847// / Print the given floating point value in a stablized form.
27972848void AsmPrinter::printFloat (const APFloat &value) {
27982849 assert (impl && " expected AsmPrinter::printFloat to be overriden" );
@@ -3118,19 +3169,6 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
31183169 printTrailingLocation (loc);
31193170 }
31203171
3121- // / Print a newline and indent the printer to the start of the current
3122- // / operation.
3123- void printNewline () override {
3124- os << newLine;
3125- os.indent (currentIndent);
3126- }
3127-
3128- // / Increase indentation.
3129- void increaseIndent () override { currentIndent += indentWidth; }
3130-
3131- // / Decrease indentation.
3132- void decreaseIndent () override { currentIndent -= indentWidth; }
3133-
31343172 // / Print a block argument in the usual format of:
31353173 // / %ssaName : type {attr1=42} loc("here")
31363174 // / where location printing is controlled by the standard internal option.
@@ -3256,12 +3294,6 @@ class OperationPrinter : public AsmPrinter::Impl, private OpAsmPrinter {
32563294 // top-level we start with "builtin" as the default, so that the top-level
32573295 // `module` operation prints as-is.
32583296 SmallVector<StringRef> defaultDialectStack{" builtin" };
3259-
3260- // / The number of spaces used for indenting nested operations.
3261- const static unsigned indentWidth = 2 ;
3262-
3263- // This is the current indentation level for nested structures.
3264- unsigned currentIndent = 0 ;
32653297};
32663298} // namespace
32673299
0 commit comments