@@ -107,7 +107,8 @@ PrintOptions PrintOptions::printParseableInterfaceFile() {
107
107
result.FunctionDefinitions = true ;
108
108
result.CollapseSingleGetterProperty = false ;
109
109
result.VarInitializers = true ;
110
- result.PrintStableReferencesToOpaqueReturnTypes = true ;
110
+ result.OpaqueReturnTypePrinting =
111
+ OpaqueReturnTypePrintingMode::StableReference;
111
112
112
113
// We should print __consuming, __owned, etc for the module interface file.
113
114
result.SkipUnderscoredKeywords = false ;
@@ -1699,6 +1700,8 @@ void PrintAST::printMutatingModifiersIfNeeded(const AccessorDecl *accessor) {
1699
1700
void PrintAST::printAccessors (const AbstractStorageDecl *ASD) {
1700
1701
if (isa<VarDecl>(ASD) && !Options.PrintPropertyAccessors )
1701
1702
return ;
1703
+ if (isa<SubscriptDecl>(ASD) && !Options.PrintSubscriptAccessors )
1704
+ return ;
1702
1705
1703
1706
auto impl = ASD->getImplInfo ();
1704
1707
@@ -2519,6 +2522,7 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
2519
2522
if (!tyLoc.getTypeRepr ())
2520
2523
tyLoc = TypeLoc::withoutLoc (decl->getInterfaceType ());
2521
2524
2525
+ Printer.printDeclResultTypePre (decl, tyLoc);
2522
2526
if (decl->getAttrs ().hasAttribute <ImplicitlyUnwrappedOptionalAttr>())
2523
2527
printTypeLocForImplicitlyUnwrappedOptional (tyLoc);
2524
2528
else
@@ -2789,6 +2793,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
2789
2793
ResultTyLoc = TypeLoc::withoutLoc (ResultTy);
2790
2794
}
2791
2795
Printer << " -> " ;
2796
+
2797
+ Printer.printDeclResultTypePre (decl, ResultTyLoc);
2792
2798
Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
2793
2799
if (decl->getAttrs ().hasAttribute <ImplicitlyUnwrappedOptionalAttr>())
2794
2800
printTypeLocForImplicitlyUnwrappedOptional (ResultTyLoc);
@@ -2916,8 +2922,9 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
2916
2922
});
2917
2923
Printer << " -> " ;
2918
2924
2919
- Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
2920
2925
TypeLoc elementTy = decl->getElementTypeLoc ();
2926
+ Printer.printDeclResultTypePre (decl, elementTy);
2927
+ Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
2921
2928
if (!elementTy.getTypeRepr ())
2922
2929
elementTy = TypeLoc::withoutLoc (decl->getElementInterfaceType ());
2923
2930
if (decl->getAttrs ().hasAttribute <ImplicitlyUnwrappedOptionalAttr>())
@@ -3388,7 +3395,27 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
3388
3395
return ;
3389
3396
}
3390
3397
3391
- if (T->hasSimpleTypeRepr ()) {
3398
+ bool isSimple = T->hasSimpleTypeRepr ();
3399
+ if (isSimple && T->is <OpaqueTypeArchetypeType>()) {
3400
+ auto opaqueTy = T->castTo <OpaqueTypeArchetypeType>();
3401
+ auto opaqueDecl = opaqueTy->getDecl ();
3402
+ if (!opaqueDecl->hasName ()) {
3403
+ switch (Options.OpaqueReturnTypePrinting ) {
3404
+ case PrintOptions::OpaqueReturnTypePrintingMode::StableReference:
3405
+ case PrintOptions::OpaqueReturnTypePrintingMode::Description:
3406
+ isSimple = true ;
3407
+ break ;
3408
+ case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
3409
+ isSimple = false ;
3410
+ break ;
3411
+ case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: {
3412
+ isSimple = opaqueTy->getConformsTo ().size () < 2 ;
3413
+ }
3414
+ }
3415
+ }
3416
+ }
3417
+
3418
+ if (isSimple) {
3392
3419
visit (T);
3393
3420
} else {
3394
3421
Printer << " (" ;
@@ -4151,7 +4178,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
4151
4178
}
4152
4179
4153
4180
void visitNestedArchetypeType (NestedArchetypeType *T) {
4154
- visit (T->getParent ());
4181
+ printWithParensIfNotSimple (T->getParent ());
4155
4182
Printer << " ." ;
4156
4183
printArchetypeCommon (T);
4157
4184
}
@@ -4161,16 +4188,28 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
4161
4188
}
4162
4189
4163
4190
void visitOpaqueTypeArchetypeType (OpaqueTypeArchetypeType *T) {
4164
- // Print the type by referencing the opaque decl's synthetic name, if we
4165
- // were asked to.
4166
- OpaqueTypeDecl *decl = T->getDecl ();
4167
-
4168
- if (Options.PrintStableReferencesToOpaqueReturnTypes ) {
4191
+ switch (Options.OpaqueReturnTypePrinting ) {
4192
+ case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
4193
+ Printer << " some " ;
4194
+ LLVM_FALLTHROUGH;
4195
+ case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: {
4196
+ SmallVector<Type, 2 > types;
4197
+ for (auto proto : T->getConformsTo ())
4198
+ types.push_back (proto->TypeDecl ::getDeclaredInterfaceType ());
4199
+
4200
+ // Create and visit temporary ProtocolCompositionType.
4201
+ auto composition =
4202
+ ProtocolCompositionType::get (T->getASTContext (), types, false );
4203
+ visit (composition);
4204
+ return ;
4205
+ }
4206
+ case PrintOptions::OpaqueReturnTypePrintingMode::StableReference: {
4169
4207
// Print the source of the opaque return type as a mangled name.
4170
4208
// We'll use type reconstruction while parsing the attribute to
4171
4209
// turn this back into a reference to the naming decl for the opaque
4172
4210
// type.
4173
4211
Printer << " @_opaqueReturnTypeOf(" ;
4212
+ OpaqueTypeDecl *decl = T->getDecl ();
4174
4213
4175
4214
Printer.printEscapedStringLiteral (
4176
4215
decl->getOpaqueReturnTypeIdentifier ().str ());
@@ -4181,7 +4220,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
4181
4220
4182
4221
Printer << u8" ) \U0001F9B8 " ;
4183
4222
printGenericArgs (T->getSubstitutions ().getReplacementTypes ());
4184
- } else {
4223
+ return ;
4224
+ }
4225
+ case PrintOptions::OpaqueReturnTypePrintingMode::Description: {
4185
4226
// TODO(opaque): present opaque types with user-facing syntax. we should
4186
4227
// probably print this as `some P` and record the fact that we printed that
4187
4228
// so that diagnostics can add followup notes.
@@ -4195,6 +4236,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
4195
4236
[&] { Printer << " , " ; });
4196
4237
Printer << ' >' ;
4197
4238
}
4239
+ return ;
4240
+ }
4198
4241
}
4199
4242
}
4200
4243
0 commit comments