Skip to content

Commit f85deb0

Browse files
committed
[AST Printer] Add ability to print desugared arrays, dictionaries, optionals
1 parent eecde02 commit f85deb0

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,16 @@ struct PrintOptions {
533533
/// parameter.
534534
bool PrintSpecializeAttributeWithAvailability = true;
535535

536+
/// Whether to always desugar array types from `[base_type]` to `Array<base_type>`
537+
bool AlwaysDesugarArraySliceTypes = false;
538+
539+
/// Whether to always desugar dictionary types
540+
/// from `[key_type:value_type]` to `Dictionary<key_type,value_type>`
541+
bool AlwaysDesugarDictionaryTypes = false;
542+
543+
/// Whether to always desugar optional types from `base_type?` to `Optional<base_type>`
544+
bool AlwaysDesugarOptionalTypes = false;
545+
536546
/// \see ShouldQualifyNestedDeclarations
537547
enum class QualifyNestedDeclarations {
538548
Never,

lib/AST/ASTPrinter.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6445,34 +6445,45 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
64456445
}
64466446

64476447
void visitArraySliceType(ArraySliceType *T) {
6448-
Printer << "[";
6449-
visit(T->getBaseType());
6450-
Printer << "]";
6448+
if (Options.AlwaysDesugarDictionaryTypes) {
6449+
visit(T->getDesugaredType());
6450+
} else {
6451+
Printer << "[";
6452+
visit(T->getBaseType());
6453+
Printer << "]";
6454+
}
64516455
}
64526456

64536457
void visitDictionaryType(DictionaryType *T) {
6454-
Printer << "[";
6455-
visit(T->getKeyType());
6456-
Printer << " : ";
6457-
visit(T->getValueType());
6458-
Printer << "]";
6458+
if (Options.AlwaysDesugarDictionaryTypes) {
6459+
visit(T->getDesugaredType());
6460+
} else {
6461+
Printer << "[";
6462+
visit(T->getKeyType());
6463+
Printer << " : ";
6464+
visit(T->getValueType());
6465+
Printer << "]";
6466+
}
64596467
}
64606468

64616469
void visitOptionalType(OptionalType *T) {
64626470
auto printAsIUO = Options.PrintOptionalAsImplicitlyUnwrapped;
6463-
6464-
// Printing optionals with a trailing '!' applies only to
6465-
// top-level optionals, not to any nested within.
6466-
const_cast<PrintOptions &>(Options).PrintOptionalAsImplicitlyUnwrapped =
6467-
false;
6468-
printWithParensIfNotSimple(T->getBaseType());
6469-
const_cast<PrintOptions &>(Options).PrintOptionalAsImplicitlyUnwrapped =
6470-
printAsIUO;
6471-
6472-
if (printAsIUO)
6473-
Printer << "!";
6474-
else
6475-
Printer << "?";
6471+
if (Options.AlwaysDesugarOptionalTypes) {
6472+
visit(T->getDesugaredType());
6473+
return;
6474+
} else {
6475+
// Printing optionals with a trailing '!' applies only to
6476+
// top-level optionals, not to any nested within.
6477+
const_cast<PrintOptions &>(Options).PrintOptionalAsImplicitlyUnwrapped =
6478+
false;
6479+
printWithParensIfNotSimple(T->getBaseType());
6480+
const_cast<PrintOptions &>(Options).PrintOptionalAsImplicitlyUnwrapped =
6481+
printAsIUO;
6482+
if (printAsIUO)
6483+
Printer << "!";
6484+
else
6485+
Printer << "?";
6486+
}
64766487
}
64776488

64786489
void visitVariadicSequenceType(VariadicSequenceType *T) {

0 commit comments

Comments
 (0)