Skip to content

Commit 1f165b4

Browse files
committed
Simplify getBuiltinFunction to take an array of types instead of an array
of TupleTypeElt, in prep for a later change. NFC.
1 parent 14a3ab6 commit 1f165b4

File tree

1 file changed

+38
-65
lines changed

1 file changed

+38
-65
lines changed

lib/AST/Builtins.cpp

Lines changed: 38 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,31 @@ StringRef swift::getBuiltinBaseName(ASTContext &C, StringRef Name,
132132

133133
/// Build a builtin function declaration.
134134
static FuncDecl *
135-
getBuiltinFunction(Identifier Id,
136-
ArrayRef<TupleTypeElt> ArgTypes,
137-
Type ResType,
135+
getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,
138136
FunctionType::ExtInfo Info = FunctionType::ExtInfo()) {
139137
auto &Context = ResType->getASTContext();
140-
Type ArgType = TupleType::get(ArgTypes, Context);
138+
139+
SmallVector<TupleTypeElt, 4> tupleElts;
140+
for (Type argType : argTypes)
141+
tupleElts.push_back(argType);
142+
143+
Type ArgType = TupleType::get(tupleElts, Context);
141144
Type FnType;
142145
FnType = FunctionType::get(ArgType, ResType, Info);
143146

144147
Module *M = Context.TheBuiltinModule;
145148
DeclContext *DC = &M->getMainFile(FileUnitKind::Builtin);
146149

147150
SmallVector<TuplePatternElt, 4> ParamPatternElts;
148-
for (auto &ArgTupleElt : ArgTypes) {
151+
for (Type argType : argTypes) {
149152
auto PD = new (Context) ParamDecl(/*IsLet*/true, SourceLoc(),
150153
Identifier(), SourceLoc(),
151-
Identifier(), ArgTupleElt.getType(),
154+
Identifier(), argType,
152155
DC);
153156
PD->setImplicit();
154157
Pattern *Pat = new (Context) NamedPattern(PD, /*implicit=*/true);
155-
Pat = new (Context) TypedPattern(Pat,
156-
TypeLoc::withoutLoc(ArgTupleElt.getType()), /*implicit=*/true);
158+
Pat = new (Context) TypedPattern(Pat, TypeLoc::withoutLoc(argType),
159+
/*implicit=*/true);
157160
PD->setParamParentPattern(Pat);
158161

159162
ParamPatternElts.push_back(TuplePatternElt(Pat));
@@ -254,41 +257,37 @@ static ValueDecl *getGepOperation(Identifier Id, Type ArgType) {
254257
auto &Context = ArgType->getASTContext();
255258

256259
// This is always "(i8*, IntTy) -> i8*"
257-
TupleTypeElt ArgElts[] = { Context.TheRawPointerType, ArgType };
260+
Type ArgElts[] = { Context.TheRawPointerType, ArgType };
258261
Type ResultTy = Context.TheRawPointerType;
259262
return getBuiltinFunction(Id, ArgElts, ResultTy);
260263
}
261264

262265
/// Build a binary operation declaration.
263266
static ValueDecl *getBinaryOperation(Identifier Id, Type ArgType) {
264-
TupleTypeElt ArgElts[] = { ArgType, ArgType };
265-
Type ResultTy = ArgType;
266-
return getBuiltinFunction(Id, ArgElts, ResultTy);
267+
return getBuiltinFunction(Id, { ArgType, ArgType }, ArgType);
267268
}
268269

269270
/// Build a declaration for a binary operation with overflow.
270271
static ValueDecl *getBinaryOperationWithOverflow(Identifier Id,
271272
Type ArgType) {
272273
auto &Context = ArgType->getASTContext();
273274
Type ShouldCheckForOverflowTy = BuiltinIntegerType::get(1, Context);
274-
TupleTypeElt ArgElts[] = { ArgType, ArgType, ShouldCheckForOverflowTy };
275+
Type ArgElts[] = { ArgType, ArgType, ShouldCheckForOverflowTy };
275276
Type OverflowBitTy = BuiltinIntegerType::get(1, Context);
276277
TupleTypeElt ResultElts[] = { ArgType, OverflowBitTy };
277278
Type ResultTy = TupleType::get(ResultElts, Context);
278279
return getBuiltinFunction(Id, ArgElts, ResultTy);
279280
}
280281

281282
static ValueDecl *getUnaryOperation(Identifier Id, Type ArgType) {
282-
TupleTypeElt ArgElts[] = { ArgType };
283-
Type ResultTy = ArgType;
284-
return getBuiltinFunction(Id, ArgElts, ResultTy);
283+
return getBuiltinFunction(Id, { ArgType }, ArgType);
285284
}
286285

287286
/// Build a binary predicate declaration.
288287
static ValueDecl *getBinaryPredicate(Identifier Id, Type ArgType) {
289288
auto &Context = ArgType->getASTContext();
290289

291-
TupleTypeElt ArgElts[] = { ArgType, ArgType };
290+
Type ArgElts[] = { ArgType, ArgType };
292291
Type ResultTy = BuiltinIntegerType::get(1, Context);
293292
if (auto VecTy = ArgType->getAs<BuiltinVectorType>()) {
294293
ResultTy = BuiltinVectorType::get(Context, ResultTy,
@@ -425,8 +424,7 @@ static ValueDecl *getCastOperation(ASTContext &Context, Identifier Id,
425424
return nullptr;
426425
}
427426

428-
TupleTypeElt ArgElts[] = { Input };
429-
return getBuiltinFunction(Id, ArgElts, Output);
427+
return getBuiltinFunction(Id, { Input }, Output);
430428
}
431429

432430
static const char * const GenericParamNames[] = {
@@ -692,14 +690,13 @@ static ValueDecl *getIsOptionalOperation(ASTContext &Context, Identifier Id) {
692690

693691
static ValueDecl *getAllocOperation(ASTContext &Context, Identifier Id) {
694692
Type PtrSizeTy = BuiltinIntegerType::getWordType(Context);
695-
TupleTypeElt ArgElts[] = { PtrSizeTy, PtrSizeTy };
696693
Type ResultTy = Context.TheRawPointerType;
697-
return getBuiltinFunction(Id, ArgElts, ResultTy);
694+
return getBuiltinFunction(Id, { PtrSizeTy, PtrSizeTy }, ResultTy);
698695
}
699696

700697
static ValueDecl *getDeallocOperation(ASTContext &Context, Identifier Id) {
701698
auto PtrSizeTy = BuiltinIntegerType::getWordType(Context);
702-
TupleTypeElt ArgElts[] = { Context.TheRawPointerType, PtrSizeTy, PtrSizeTy };
699+
Type ArgElts[] = { Context.TheRawPointerType, PtrSizeTy, PtrSizeTy };
703700
Type ResultTy = TupleType::getEmpty(Context);
704701
return getBuiltinFunction(Id, ArgElts, ResultTy);
705702
}
@@ -722,32 +719,26 @@ static ValueDecl *getUnexpectedErrorOperation(ASTContext &Context,
722719

723720
static ValueDecl *getCmpXChgOperation(ASTContext &Context, Identifier Id,
724721
Type T) {
725-
TupleTypeElt ArgElts[] = { Context.TheRawPointerType, T, T };
722+
Type ArgElts[] = { Context.TheRawPointerType, T, T };
726723
Type BoolTy = BuiltinIntegerType::get(1, Context);
727-
TupleTypeElt ResultElts[] = { T, BoolTy };
728-
Type ResultTy = TupleType::get(ResultElts, Context);
724+
Type ResultTy = TupleType::get({ T, BoolTy }, Context);
729725
return getBuiltinFunction(Id, ArgElts, ResultTy);
730726
}
731727

732728
static ValueDecl *getAtomicRMWOperation(ASTContext &Context, Identifier Id,
733729
Type T) {
734-
TupleTypeElt ArgElts[] = { Context.TheRawPointerType, T };
735-
Type ResultTy = T;
736-
return getBuiltinFunction(Id, ArgElts, ResultTy);
730+
return getBuiltinFunction(Id, { Context.TheRawPointerType, T }, T);
737731
}
738732

739733
static ValueDecl *getAtomicLoadOperation(ASTContext &Context, Identifier Id,
740734
Type T) {
741-
TupleTypeElt ArgElts[] = { Context.TheRawPointerType };
742-
Type ResultTy = T;
743-
return getBuiltinFunction(Id, ArgElts, ResultTy);
735+
return getBuiltinFunction(Id, { Type(Context.TheRawPointerType) }, T);
744736
}
745737

746738
static ValueDecl *getAtomicStoreOperation(ASTContext &Context, Identifier Id,
747739
Type T) {
748-
TupleTypeElt ArgElts[] = { Context.TheRawPointerType, T };
749-
Type ResultTy = Context.TheEmptyTupleType;
750-
return getBuiltinFunction(Id, ArgElts, ResultTy);
740+
return getBuiltinFunction(Id, { Context.TheRawPointerType, T },
741+
Context.TheEmptyTupleType);
751742
}
752743

753744
static ValueDecl *getNativeObjectCast(ASTContext &Context, Identifier Id,
@@ -790,8 +781,6 @@ static ValueDecl *getCastFromBridgeObjectOperation(ASTContext &C,
790781
Identifier Id,
791782
BuiltinValueKind BV) {
792783
Type BridgeTy = C.TheBridgeObjectType;
793-
TupleTypeElt ArgElts[] = { BridgeTy };
794-
795784
switch (BV) {
796785
case BuiltinValueKind::CastReferenceFromBridgeObject: {
797786
GenericSignatureBuilder builder(C);
@@ -802,7 +791,7 @@ static ValueDecl *getCastFromBridgeObjectOperation(ASTContext &C,
802791

803792
case BuiltinValueKind::CastBitPatternFromBridgeObject: {
804793
Type WordTy = BuiltinIntegerType::get(BuiltinIntegerWidth::pointer(), C);
805-
return getBuiltinFunction(Id, ArgElts, WordTy);
794+
return getBuiltinFunction(Id, { BridgeTy }, WordTy);
806795
}
807796

808797
default:
@@ -868,16 +857,13 @@ static ValueDecl *getCondFailOperation(ASTContext &C, Identifier Id) {
868857
// Int1 -> ()
869858
auto CondTy = BuiltinIntegerType::get(1, C);
870859
auto VoidTy = TupleType::getEmpty(C);
871-
TupleTypeElt CondElt(CondTy);
872-
return getBuiltinFunction(Id, CondElt, VoidTy);
860+
return getBuiltinFunction(Id, {CondTy}, VoidTy);
873861
}
874862

875863
static ValueDecl *getAssertConfOperation(ASTContext &C, Identifier Id) {
876864
// () -> Int32
877865
auto Int32Ty = BuiltinIntegerType::get(32, C);
878-
auto VoidTy = TupleType::getEmpty(C);
879-
TupleTypeElt EmptyElt(VoidTy);
880-
return getBuiltinFunction(Id, EmptyElt, Int32Ty);
866+
return getBuiltinFunction(Id, {}, Int32Ty);
881867
}
882868

883869
static ValueDecl *getFixLifetimeOperation(ASTContext &C, Identifier Id) {
@@ -899,9 +885,8 @@ static ValueDecl *getExtractElementOperation(ASTContext &Context, Identifier Id,
899885
if (!IndexTy || !IndexTy->isFixedWidth() || IndexTy->getFixedWidth() != 32)
900886
return nullptr;
901887

902-
TupleTypeElt ArgElts[] = { VecTy, IndexTy };
903888
Type ResultTy = VecTy->getElementType();
904-
return getBuiltinFunction(Id, ArgElts, ResultTy);
889+
return getBuiltinFunction(Id, { VecTy, IndexTy }, ResultTy);
905890
}
906891

907892
static ValueDecl *getInsertElementOperation(ASTContext &Context, Identifier Id,
@@ -920,16 +905,15 @@ static ValueDecl *getInsertElementOperation(ASTContext &Context, Identifier Id,
920905
if (!IndexTy || !IndexTy->isFixedWidth() || IndexTy->getFixedWidth() != 32)
921906
return nullptr;
922907

923-
TupleTypeElt ArgElts[] = { VecTy, ElementTy, IndexTy };
924-
Type ResultTy = VecTy;
925-
return getBuiltinFunction(Id, ArgElts, ResultTy);
908+
Type ArgElts[] = { VecTy, ElementTy, IndexTy };
909+
return getBuiltinFunction(Id, ArgElts, VecTy);
926910
}
927911

928912
static ValueDecl *getStaticReportOperation(ASTContext &Context, Identifier Id) {
929913
auto BoolTy = BuiltinIntegerType::get(1, Context);
930914
auto MessageTy = Context.TheRawPointerType;
931915

932-
TupleTypeElt ArgElts[] = { BoolTy, BoolTy, MessageTy };
916+
Type ArgElts[] = { BoolTy, BoolTy, MessageTy };
933917
Type ResultTy = TupleType::getEmpty(Context);
934918

935919
return getBuiltinFunction(Id, ArgElts, ResultTy);
@@ -946,12 +930,10 @@ static ValueDecl *getCheckedTruncOperation(ASTContext &Context,
946930
if (InTy->getLeastWidth() < OutTy->getGreatestWidth())
947931
return nullptr;
948932

949-
TupleTypeElt ArgElts[] = { InTy };
950933
Type OverflowBitTy = BuiltinIntegerType::get(1, Context);
951934
TupleTypeElt ResultElts[] = { OutTy, OverflowBitTy };
952935
Type ResultTy = TupleType::get(ResultElts, Context);
953-
954-
return getBuiltinFunction(Id, ArgElts, ResultTy);
936+
return getBuiltinFunction(Id, { InTy }, ResultTy);
955937
}
956938

957939
static ValueDecl *getCheckedConversionOperation(ASTContext &Context,
@@ -961,12 +943,10 @@ static ValueDecl *getCheckedConversionOperation(ASTContext &Context,
961943
if (!BuiltinTy)
962944
return nullptr;
963945

964-
TupleTypeElt ArgElts[] = { BuiltinTy };
965946
Type SignErrorBitTy = BuiltinIntegerType::get(1, Context);
966947
TupleTypeElt ResultElts[] = { BuiltinTy, SignErrorBitTy };
967948
Type ResultTy = TupleType::get(ResultElts, Context);
968-
969-
return getBuiltinFunction(Id, ArgElts, ResultTy);
949+
return getBuiltinFunction(Id, { BuiltinTy }, ResultTy);
970950
}
971951

972952
static ValueDecl *getIntToFPWithOverflowOperation(ASTContext &Context,
@@ -977,10 +957,7 @@ static ValueDecl *getIntToFPWithOverflowOperation(ASTContext &Context,
977957
if (!InTy || !OutTy)
978958
return nullptr;
979959

980-
TupleTypeElt ArgElts[] = { InTy };
981-
Type ResultTy = OutTy;
982-
983-
return getBuiltinFunction(Id, ArgElts, ResultTy);
960+
return getBuiltinFunction(Id, { InTy }, OutTy);
984961
}
985962

986963
static ValueDecl *getUnreachableOperation(ASTContext &Context,
@@ -1001,11 +978,7 @@ static ValueDecl *getOnceOperation(ASTContext &Context,
1001978
/*noreturn*/ false, /*throws*/ false);
1002979

1003980
auto BlockTy = FunctionType::get(VoidTy, VoidTy, Thin);
1004-
1005-
TupleTypeElt InFields[] = {HandleTy, BlockTy};
1006-
auto OutTy = VoidTy;
1007-
1008-
return getBuiltinFunction(Id, InFields, OutTy);
981+
return getBuiltinFunction(Id, {HandleTy, BlockTy}, VoidTy);
1009982
}
1010983

1011984
static ValueDecl *getTryPinOperation(ASTContext &ctx, Identifier name) {
@@ -1232,7 +1205,7 @@ static Type DecodeIntrinsicType(ArrayRef<llvm::Intrinsic::IITDescriptor> &Table,
12321205
static bool
12331206
getSwiftFunctionTypeForIntrinsic(unsigned iid, ArrayRef<Type> TypeArgs,
12341207
ASTContext &Context,
1235-
SmallVectorImpl<TupleTypeElt> &ArgElts,
1208+
SmallVectorImpl<Type> &ArgElts,
12361209
Type &ResultTy, FunctionType::ExtInfo &Info) {
12371210
llvm::Intrinsic::ID ID = (llvm::Intrinsic::ID)iid;
12381211

@@ -1327,7 +1300,7 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
13271300
// If this is the name of an LLVM intrinsic, cons up a swift function with a
13281301
// type that matches the IR types.
13291302
if (unsigned ID = getLLVMIntrinsicID(OperationName, !Types.empty())) {
1330-
SmallVector<TupleTypeElt, 8> ArgElts;
1303+
SmallVector<Type, 8> ArgElts;
13311304
Type ResultTy;
13321305
FunctionType::ExtInfo Info;
13331306
if (getSwiftFunctionTypeForIntrinsic(ID, Types, Context, ArgElts, ResultTy,

0 commit comments

Comments
 (0)