@@ -235,21 +235,25 @@ static const char * const GenericParamNames[] = {
235
235
};
236
236
237
237
static GenericTypeParamDecl*
238
- createGenericParam (ASTContext &ctx, const char *name, unsigned index) {
238
+ createGenericParam (ASTContext &ctx, const char *name, unsigned index,
239
+ bool isParameterPack = false ) {
239
240
ModuleDecl *M = ctx.TheBuiltinModule ;
240
241
Identifier ident = ctx.getIdentifier (name);
241
242
return GenericTypeParamDecl::createImplicit (
242
- &M->getMainFile (FileUnitKind::Builtin), ident, /* depth*/ 0 , index);
243
+ &M->getMainFile (FileUnitKind::Builtin), ident, /* depth*/ 0 , index,
244
+ isParameterPack);
243
245
}
244
246
245
247
// / Create a generic parameter list with multiple generic parameters.
246
248
static GenericParamList *getGenericParams (ASTContext &ctx,
247
- unsigned numParameters) {
249
+ unsigned numParameters,
250
+ bool areParameterPacks = false ) {
248
251
assert (numParameters <= std::size (GenericParamNames));
249
252
250
253
SmallVector<GenericTypeParamDecl *, 2 > genericParams;
251
254
for (unsigned i = 0 ; i != numParameters; ++i)
252
- genericParams.push_back (createGenericParam (ctx, GenericParamNames[i], i));
255
+ genericParams.push_back (createGenericParam (ctx, GenericParamNames[i], i,
256
+ areParameterPacks));
253
257
254
258
auto paramList = GenericParamList::create (ctx, SourceLoc (), genericParams,
255
259
SourceLoc ());
@@ -678,9 +682,11 @@ namespace {
678
682
679
683
public:
680
684
BuiltinFunctionBuilder (ASTContext &ctx, unsigned numGenericParams = 1 ,
681
- bool wantsAdditionalAnyObjectRequirement = false )
685
+ bool wantsAdditionalAnyObjectRequirement = false ,
686
+ bool areParametersPacks = false )
682
687
: Context(ctx) {
683
- TheGenericParamList = getGenericParams (ctx, numGenericParams);
688
+ TheGenericParamList = getGenericParams (ctx, numGenericParams,
689
+ areParametersPacks);
684
690
if (wantsAdditionalAnyObjectRequirement) {
685
691
Requirement req (RequirementKind::Conformance,
686
692
TheGenericParamList->getParams ()[0 ]->getInterfaceType (),
@@ -750,7 +756,7 @@ namespace {
750
756
unsigned Index;
751
757
Type build (BuiltinFunctionBuilder &builder) const {
752
758
return builder.TheGenericParamList ->getParams ()[Index]
753
- ->getDeclaredInterfaceType ();
759
+ ->getDeclaredInterfaceType ();
754
760
}
755
761
};
756
762
struct LambdaGenerator {
@@ -767,6 +773,16 @@ namespace {
767
773
return MetatypeType::get (Object.build (builder), Repr);
768
774
}
769
775
};
776
+ template <class T >
777
+ struct PackExpansionGenerator {
778
+ T Object;
779
+ Type build (BuiltinFunctionBuilder &builder) const {
780
+ auto patternTy = Object.build (builder);
781
+ SmallVector<Type, 2 > packs;
782
+ patternTy->getTypeParameterPacks (packs);
783
+ return PackExpansionType::get (patternTy, packs[0 ]);
784
+ }
785
+ };
770
786
};
771
787
} // end anonymous namespace
772
788
@@ -814,6 +830,12 @@ makeMetatype(const T &object,
814
830
return { object, repr };
815
831
}
816
832
833
+ template <class T >
834
+ static BuiltinFunctionBuilder::PackExpansionGenerator<T>
835
+ makePackExpansion (const T &object) {
836
+ return { object };
837
+ }
838
+
817
839
// / Create a function with type <T> T -> ().
818
840
static ValueDecl *getRefCountingOperation (ASTContext &ctx, Identifier id) {
819
841
return getBuiltinFunction (ctx, id, _thin,
@@ -1934,6 +1956,18 @@ static ValueDecl *getHopToActor(ASTContext &ctx, Identifier id) {
1934
1956
return builder.build (id);
1935
1957
}
1936
1958
1959
+ static ValueDecl *getPackLength (ASTContext &ctx, Identifier id) {
1960
+ BuiltinFunctionBuilder builder (ctx, /* genericParamCount */ 1 ,
1961
+ /* anyObject */ false ,
1962
+ /* areParametersPack */ true );
1963
+
1964
+ auto paramTy = makeMetatype (makeTuple (makePackExpansion (makeGenericParam ())));
1965
+ builder.addParameter (paramTy);
1966
+ builder.setResult (makeConcrete (BuiltinIntegerType::getWordType (ctx)));
1967
+
1968
+ return builder.build (id);
1969
+ }
1970
+
1937
1971
// / An array of the overloaded builtin kinds.
1938
1972
static const OverloadedBuiltinKind OverloadedBuiltinKinds[] = {
1939
1973
OverloadedBuiltinKind::None,
@@ -2975,6 +3009,9 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
2975
3009
2976
3010
case BuiltinValueKind::AutoDiffAllocateSubcontextWithType:
2977
3011
return getAutoDiffAllocateSubcontext (Context, Id);
3012
+
3013
+ case BuiltinValueKind::PackLength:
3014
+ return getPackLength (Context, Id);
2978
3015
}
2979
3016
2980
3017
llvm_unreachable (" bad builtin value!" );
0 commit comments