17
17
18
18
#include " swift/AST/ASTContext.h"
19
19
#include " swift/AST/Decl.h"
20
+ #include " swift/AST/GenericParamList.h"
20
21
#include " swift/AST/ParameterList.h"
21
22
#include " swift/AST/Type.h"
22
23
#include " swift/AST/Types.h"
@@ -35,6 +36,24 @@ struct PackTypeParameterCollector: TypeWalker {
35
36
if (t->is <PackExpansionType>())
36
37
return Action::SkipChildren;
37
38
39
+ if (auto *boundGenericType = dyn_cast<BoundGenericType>(t.getPointer ())) {
40
+ if (auto parentType = boundGenericType->getParent ())
41
+ parentType.walk (*this );
42
+
43
+ Type (boundGenericType->getExpandedGenericArgsPack ()).walk (*this );
44
+ return Action::SkipChildren;
45
+ }
46
+
47
+ if (auto *typeAliasType = dyn_cast<TypeAliasType>(t.getPointer ())) {
48
+ if (typeAliasType->getDecl ()->isGeneric ()) {
49
+ if (auto parentType = typeAliasType->getParent ())
50
+ parentType.walk (*this );
51
+
52
+ Type (typeAliasType->getExpandedGenericArgsPack ()).walk (*this );
53
+ return Action::SkipChildren;
54
+ }
55
+ }
56
+
38
57
if (auto *paramTy = t->getAs <GenericTypeParamType>()) {
39
58
if (paramTy->isParameterPack ())
40
59
typeParams.insert (paramTy);
@@ -348,3 +367,51 @@ unsigned ParameterList::getOrigParamIndex(SubstitutionMap subMap,
348
367
dump (llvm::errs ());
349
368
abort ();
350
369
}
370
+
371
+ // / <T...> Foo<T, Pack{Int, String}> => Pack{T..., Int, String}
372
+ PackType *BoundGenericType::getExpandedGenericArgsPack () {
373
+ // It would be nicer to use genericSig.getInnermostGenericParams() here,
374
+ // but that triggers a request cycle if we're in the middle of computing
375
+ // the generic signature already.
376
+ SmallVector<Type, 2 > params;
377
+ for (auto *paramDecl : getDecl ()->getGenericParams ()->getParams ()) {
378
+ params.push_back (paramDecl->getDeclaredInterfaceType ());
379
+ }
380
+
381
+ return PackType::get (getASTContext (),
382
+ TypeArrayView<GenericTypeParamType>(params),
383
+ getGenericArgs ());
384
+ }
385
+
386
+ // / <T...> Foo<T, Pack{Int, String}> => Pack{T..., Int, String}
387
+ PackType *TypeAliasType::getExpandedGenericArgsPack () {
388
+ if (!getDecl ()->isGeneric ())
389
+ return nullptr ;
390
+
391
+ auto genericSig = getGenericSignature ();
392
+ return PackType::get (getASTContext (),
393
+ genericSig.getInnermostGenericParams (),
394
+ getDirectGenericArgs ());
395
+ }
396
+
397
+ // / <T...> Pack{T, Pack{Int, String}} => Pack{T..., Int, String}
398
+ PackType *PackType::get (const ASTContext &C,
399
+ TypeArrayView<GenericTypeParamType> params,
400
+ ArrayRef<Type> args) {
401
+ SmallVector<Type, 2 > wrappedArgs;
402
+
403
+ assert (params.size () == args.size ());
404
+ for (unsigned i = 0 , e = params.size (); i < e; ++i) {
405
+ auto arg = args[i];
406
+
407
+ if (params[i]->isParameterPack ()) {
408
+ wrappedArgs.push_back (PackExpansionType::get (
409
+ arg, arg->getReducedShape ()));
410
+ continue ;
411
+ }
412
+
413
+ wrappedArgs.push_back (arg);
414
+ }
415
+
416
+ return get (C, wrappedArgs)->flattenPackTypes ();
417
+ }
0 commit comments