@@ -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 ;
@@ -1713,6 +1714,8 @@ void PrintAST::printMutatingModifiersIfNeeded(const AccessorDecl *accessor) {
1713
1714
void PrintAST::printAccessors (const AbstractStorageDecl *ASD) {
1714
1715
if (isa<VarDecl>(ASD) && !Options.PrintPropertyAccessors )
1715
1716
return ;
1717
+ if (isa<SubscriptDecl>(ASD) && !Options.PrintSubscriptAccessors )
1718
+ return ;
1716
1719
1717
1720
auto impl = ASD->getImplInfo ();
1718
1721
@@ -2533,6 +2536,7 @@ void PrintAST::visitVarDecl(VarDecl *decl) {
2533
2536
if (!tyLoc.getTypeRepr ())
2534
2537
tyLoc = TypeLoc::withoutLoc (decl->getInterfaceType ());
2535
2538
2539
+ Printer.printDeclResultTypePre (decl, tyLoc);
2536
2540
if (decl->getAttrs ().hasAttribute <ImplicitlyUnwrappedOptionalAttr>())
2537
2541
printTypeLocForImplicitlyUnwrappedOptional (tyLoc);
2538
2542
else
@@ -2803,6 +2807,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
2803
2807
ResultTyLoc = TypeLoc::withoutLoc (ResultTy);
2804
2808
}
2805
2809
Printer << " -> " ;
2810
+
2811
+ Printer.printDeclResultTypePre (decl, ResultTyLoc);
2806
2812
Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
2807
2813
if (decl->getAttrs ().hasAttribute <ImplicitlyUnwrappedOptionalAttr>())
2808
2814
printTypeLocForImplicitlyUnwrappedOptional (ResultTyLoc);
@@ -2930,8 +2936,9 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
2930
2936
});
2931
2937
Printer << " -> " ;
2932
2938
2933
- Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
2934
2939
TypeLoc elementTy = decl->getElementTypeLoc ();
2940
+ Printer.printDeclResultTypePre (decl, elementTy);
2941
+ Printer.callPrintStructurePre (PrintStructureKind::FunctionReturnType);
2935
2942
if (!elementTy.getTypeRepr ())
2936
2943
elementTy = TypeLoc::withoutLoc (decl->getElementInterfaceType ());
2937
2944
if (decl->getAttrs ().hasAttribute <ImplicitlyUnwrappedOptionalAttr>())
@@ -3402,7 +3409,27 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
3402
3409
return ;
3403
3410
}
3404
3411
3405
- if (T->hasSimpleTypeRepr ()) {
3412
+ bool isSimple = T->hasSimpleTypeRepr ();
3413
+ if (isSimple && T->is <OpaqueTypeArchetypeType>()) {
3414
+ auto opaqueTy = T->castTo <OpaqueTypeArchetypeType>();
3415
+ auto opaqueDecl = opaqueTy->getDecl ();
3416
+ if (!opaqueDecl->hasName ()) {
3417
+ switch (Options.OpaqueReturnTypePrinting ) {
3418
+ case PrintOptions::OpaqueReturnTypePrintingMode::StableReference:
3419
+ case PrintOptions::OpaqueReturnTypePrintingMode::Description:
3420
+ isSimple = true ;
3421
+ break ;
3422
+ case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
3423
+ isSimple = false ;
3424
+ break ;
3425
+ case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: {
3426
+ isSimple = opaqueTy->getConformsTo ().size () < 2 ;
3427
+ }
3428
+ }
3429
+ }
3430
+ }
3431
+
3432
+ if (isSimple) {
3406
3433
visit (T);
3407
3434
} else {
3408
3435
Printer << " (" ;
@@ -4165,7 +4192,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
4165
4192
}
4166
4193
4167
4194
void visitNestedArchetypeType (NestedArchetypeType *T) {
4168
- visit (T->getParent ());
4195
+ printWithParensIfNotSimple (T->getParent ());
4169
4196
Printer << " ." ;
4170
4197
printArchetypeCommon (T);
4171
4198
}
@@ -4175,16 +4202,28 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
4175
4202
}
4176
4203
4177
4204
void visitOpaqueTypeArchetypeType (OpaqueTypeArchetypeType *T) {
4178
- // Print the type by referencing the opaque decl's synthetic name, if we
4179
- // were asked to.
4180
- OpaqueTypeDecl *decl = T->getDecl ();
4181
-
4182
- if (Options.PrintStableReferencesToOpaqueReturnTypes ) {
4205
+ switch (Options.OpaqueReturnTypePrinting ) {
4206
+ case PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword:
4207
+ Printer << " some " ;
4208
+ LLVM_FALLTHROUGH;
4209
+ case PrintOptions::OpaqueReturnTypePrintingMode::WithoutOpaqueKeyword: {
4210
+ SmallVector<Type, 2 > types;
4211
+ for (auto proto : T->getConformsTo ())
4212
+ types.push_back (proto->TypeDecl ::getDeclaredInterfaceType ());
4213
+
4214
+ // Create and visit temporary ProtocolCompositionType.
4215
+ auto composition =
4216
+ ProtocolCompositionType::get (T->getASTContext (), types, false );
4217
+ visit (composition);
4218
+ return ;
4219
+ }
4220
+ case PrintOptions::OpaqueReturnTypePrintingMode::StableReference: {
4183
4221
// Print the source of the opaque return type as a mangled name.
4184
4222
// We'll use type reconstruction while parsing the attribute to
4185
4223
// turn this back into a reference to the naming decl for the opaque
4186
4224
// type.
4187
4225
Printer << " @_opaqueReturnTypeOf(" ;
4226
+ OpaqueTypeDecl *decl = T->getDecl ();
4188
4227
4189
4228
Printer.printEscapedStringLiteral (
4190
4229
decl->getOpaqueReturnTypeIdentifier ().str ());
@@ -4195,7 +4234,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
4195
4234
4196
4235
Printer << u8" ) \U0001F9B8 " ;
4197
4236
printGenericArgs (T->getSubstitutions ().getReplacementTypes ());
4198
- } else {
4237
+ return ;
4238
+ }
4239
+ case PrintOptions::OpaqueReturnTypePrintingMode::Description: {
4199
4240
// TODO(opaque): present opaque types with user-facing syntax. we should
4200
4241
// probably print this as `some P` and record the fact that we printed that
4201
4242
// so that diagnostics can add followup notes.
@@ -4209,6 +4250,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
4209
4250
[&] { Printer << " , " ; });
4210
4251
Printer << ' >' ;
4211
4252
}
4253
+ return ;
4254
+ }
4212
4255
}
4213
4256
}
4214
4257
0 commit comments