@@ -624,7 +624,9 @@ class PrintAST : public ASTVisitor<PrintAST> {
624
624
void printOneParameter (const ParamDecl *param, ParameterTypeFlags paramFlags,
625
625
bool Curried, bool ArgNameIsAPIByDefault);
626
626
627
- void printParameterList (ParameterList *PL, Type paramListTy, bool isCurried,
627
+ void printParameterList (ParameterList *PL,
628
+ ArrayRef<AnyFunctionType::Param> params,
629
+ bool isCurried,
628
630
llvm::function_ref<bool ()> isAPINameByDefault);
629
631
630
632
// / \brief Print the function parameters in curried or selector style,
@@ -2309,30 +2311,19 @@ void PrintAST::printOneParameter(const ParamDecl *param,
2309
2311
}
2310
2312
}
2311
2313
2312
- void PrintAST::printParameterList (ParameterList *PL, Type paramListTy,
2314
+ void PrintAST::printParameterList (ParameterList *PL,
2315
+ ArrayRef<AnyFunctionType::Param> params,
2313
2316
bool isCurried,
2314
2317
llvm::function_ref<bool ()> isAPINameByDefault) {
2315
- SmallVector<ParameterTypeFlags, 4 > paramFlags;
2316
- if (paramListTy && !paramListTy->hasError ()) {
2317
- if (auto parenTy = dyn_cast<ParenType>(paramListTy.getPointer ())) {
2318
- paramFlags.push_back (parenTy->getParameterFlags ());
2319
- } else if (auto tupleTy = paramListTy->getAs <TupleType>()) {
2320
- for (auto elt : tupleTy->getElements ())
2321
- paramFlags.push_back (elt.getParameterFlags ());
2322
- } else {
2323
- paramFlags.push_back ({});
2324
- }
2325
- } else {
2326
- // Malformed AST, just use default flags
2327
- paramFlags.resize (PL->size ());
2328
- }
2329
-
2330
2318
Printer << " (" ;
2319
+ const unsigned paramSize = params.size ();
2331
2320
for (unsigned i = 0 , e = PL->size (); i != e; ++i) {
2332
2321
if (i > 0 )
2333
2322
Printer << " , " ;
2334
-
2335
- printOneParameter (PL->get (i), paramFlags[i], isCurried,
2323
+ auto paramFlags = (i < paramSize)
2324
+ ? params[i].getParameterFlags ()
2325
+ : ParameterTypeFlags ();
2326
+ printOneParameter (PL->get (i), paramFlags, isCurried,
2336
2327
isAPINameByDefault ());
2337
2328
}
2338
2329
Printer << " )" ;
@@ -2350,15 +2341,13 @@ void PrintAST::printFunctionParameters(AbstractFunctionDecl *AFD) {
2350
2341
curTy = funTy->getResult ();
2351
2342
}
2352
2343
2353
- SmallVector<Type , 4 > parameterListTypes;
2344
+ SmallVector<ArrayRef<AnyFunctionType::Param> , 4 > parameterListTypes;
2354
2345
for (unsigned i = 0 ; i < BodyParams.size (); ++i) {
2355
2346
if (curTy) {
2356
2347
if (auto funTy = curTy->getAs <AnyFunctionType>()) {
2357
- parameterListTypes.push_back (funTy->getInput ());
2348
+ parameterListTypes.push_back (funTy->getParams ());
2358
2349
if (i < BodyParams.size () - 1 )
2359
2350
curTy = funTy->getResult ();
2360
- } else {
2361
- parameterListTypes.push_back (curTy);
2362
2351
}
2363
2352
}
2364
2353
}
@@ -2368,7 +2357,7 @@ void PrintAST::printFunctionParameters(AbstractFunctionDecl *AFD) {
2368
2357
// Be extra careful in the event of printing mal-formed ASTs
2369
2358
auto paramListType = CurrPattern < parameterListTypes.size ()
2370
2359
? parameterListTypes[CurrPattern]
2371
- : nullptr ;
2360
+ : ArrayRef<AnyFunctionType::Param>() ;
2372
2361
printParameterList (BodyParams[CurrPattern], paramListType,
2373
2362
/* isCurried=*/ CurrPattern > 0 ,
2374
2363
[&]()->bool {
@@ -2563,11 +2552,18 @@ void PrintAST::printEnumElement(EnumElementDecl *elt) {
2563
2552
llvm::SaveAndRestore<PrintOptions::ArgAndParamPrintingMode>
2564
2553
mode (Options.ArgAndParamPrinting ,
2565
2554
PrintOptions::ArgAndParamPrintingMode::EnumElement);
2566
- printParameterList (PL,
2567
- elt->hasInterfaceType ()
2568
- ? elt->getArgumentInterfaceType ()
2569
- : nullptr ,
2570
- /* isCurried=*/ false ,
2555
+
2556
+
2557
+ auto params = ArrayRef<AnyFunctionType::Param>();
2558
+ if (elt->hasInterfaceType () && !elt->getInterfaceType ()->hasError ()) {
2559
+ // Walk to the params of the associated values.
2560
+ // (EnumMetaType) -> (AssocValues) -> Enum
2561
+ params = elt->getInterfaceType ()->castTo <AnyFunctionType>()
2562
+ ->getResult ()
2563
+ ->castTo <AnyFunctionType>()
2564
+ ->getParams ();
2565
+ }
2566
+ printParameterList (PL, params, /* isCurried=*/ false ,
2571
2567
/* isAPINameByDefault*/ []()->bool {return true ;});
2572
2568
}
2573
2569
@@ -2636,11 +2632,12 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
2636
2632
Printer << " subscript" ;
2637
2633
}, [&] { // Parameters
2638
2634
printGenericDeclGenericParams (decl);
2639
- printParameterList (decl->getIndices (),
2640
- decl->hasInterfaceType ()
2641
- ? decl->getIndicesInterfaceType ()
2642
- : nullptr ,
2643
- /* isCurried=*/ false ,
2635
+ auto params = ArrayRef<AnyFunctionType::Param>();
2636
+ if (decl->hasInterfaceType () && !decl->getInterfaceType ()->hasError ()) {
2637
+ // Walk to the params of the subscript's indices.
2638
+ params = decl->getInterfaceType ()->castTo <AnyFunctionType>()->getParams ();
2639
+ }
2640
+ printParameterList (decl->getIndices (), params, /* isCurried=*/ false ,
2644
2641
/* isAPINameByDefault*/ []()->bool {return false ;});
2645
2642
});
2646
2643
Printer << " -> " ;
@@ -3522,6 +3519,35 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
3522
3519
}
3523
3520
}
3524
3521
3522
+ void visitAnyFunctionTypeParams (ArrayRef<AnyFunctionType::Param> Params,
3523
+ bool printLabels) {
3524
+ Printer << " (" ;
3525
+
3526
+ for (unsigned i = 0 , e = Params.size (); i != e; ++i) {
3527
+ if (i)
3528
+ Printer << " , " ;
3529
+ const AnyFunctionType::Param &Param = Params[i];
3530
+
3531
+ Printer.callPrintStructurePre (PrintStructureKind::FunctionParameter);
3532
+ SWIFT_DEFER {
3533
+ Printer.printStructurePost (PrintStructureKind::FunctionParameter);
3534
+ };
3535
+
3536
+ if (printLabels && Param.hasLabel ()) {
3537
+ Printer.printName (Param.getLabel (),
3538
+ PrintNameContext::FunctionParameterExternal);
3539
+ Printer << " : " ;
3540
+ }
3541
+
3542
+ printParameterFlags (Printer, Options, Param.getParameterFlags ());
3543
+ visit (Param.getType ());
3544
+ if (Param.isVariadic ())
3545
+ Printer << " ..." ;
3546
+ }
3547
+
3548
+ Printer << " )" ;
3549
+ }
3550
+
3525
3551
void visitFunctionType (FunctionType *T) {
3526
3552
Printer.callPrintStructurePre (PrintStructureKind::FunctionType);
3527
3553
SWIFT_DEFER {
@@ -3531,26 +3557,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
3531
3557
printFunctionExtInfo (T->getExtInfo ());
3532
3558
3533
3559
// If we're stripping argument labels from types, do it when printing.
3534
- Type inputType = T->getInput ();
3535
- if (auto tupleTy = dyn_cast<TupleType>(inputType.getPointer ())) {
3536
- SmallVector<TupleTypeElt, 4 > elements;
3537
- elements.reserve (tupleTy->getNumElements ());
3538
- for (const auto &elt : tupleTy->getElements ())
3539
- elements.push_back (elt.getWithoutName ());
3540
- inputType = TupleType::get (elements, inputType->getASTContext ());
3541
- }
3542
-
3543
- bool needsParens =
3544
- !inputType->hasParenSugar () &&
3545
- !inputType->is <TupleType>();
3546
-
3547
- if (needsParens)
3548
- Printer << " (" ;
3549
-
3550
- visit (inputType);
3551
-
3552
- if (needsParens)
3553
- Printer << " )" ;
3560
+ visitAnyFunctionTypeParams (T->getParams (), /* printLabels*/ false );
3554
3561
3555
3562
if (T->throws ())
3556
3563
Printer << " " << tok::kw_throws;
@@ -3579,17 +3586,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
3579
3586
PrintAST::PrintRequirements);
3580
3587
Printer << " " ;
3581
3588
3582
- bool needsParens =
3583
- !T->getInput ()->hasParenSugar () &&
3584
- !T->getInput ()->is <TupleType>();
3585
-
3586
- if (needsParens)
3587
- Printer << " (" ;
3588
-
3589
- visit (T->getInput ());
3590
-
3591
- if (needsParens)
3592
- Printer << " )" ;
3589
+ visitAnyFunctionTypeParams (T->getParams (), /* printLabels*/ true );
3593
3590
3594
3591
if (T->throws ())
3595
3592
Printer << " " << tok::kw_throws;
0 commit comments