Skip to content

Commit f219e58

Browse files
[NFC] Refactor ExtInfo to use a builder-pattern based API.
Since the two ExtInfos share a common ClangTypeInfo, and C++ doesn't let us forward declare nested classes, we need to hoist out AnyFunctionType::ExtInfo and SILFunctionType::ExtInfo to the top-level. We also add some convenience APIs on (AST|SIL)ExtInfo for frequently used withXYZ methods. Note that all non-default construction still goes through the builder's build() method. We do not add any checks for invariants here; those will be added later.
1 parent aee1151 commit f219e58

34 files changed

+1109
-748
lines changed

include/swift/AST/ExtInfo.h

Lines changed: 711 additions & 0 deletions
Large diffs are not rendered by default.

include/swift/AST/Types.h

Lines changed: 46 additions & 516 deletions
Large diffs are not rendered by default.

include/swift/SIL/TypeLowering.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ CanAnyFunctionType adjustFunctionType(CanAnyFunctionType type,
5757
/// Change the given function type's representation.
5858
inline CanAnyFunctionType adjustFunctionType(CanAnyFunctionType t,
5959
SILFunctionType::Representation rep) {
60-
auto extInfo = t->getExtInfo().withSILRepresentation(rep);
60+
auto extInfo =
61+
t->getExtInfo().intoBuilder().withSILRepresentation(rep).build();
6162
return adjustFunctionType(t, extInfo);
6263
}
6364

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,10 +3113,9 @@ FunctionType *FunctionType::get(ArrayRef<AnyFunctionType::Param> params,
31133113
return funcTy;
31143114
}
31153115

3116-
Optional<ExtInfo::ClangTypeInfo> clangTypeInfo = info.getClangTypeInfo();
3116+
Optional<ClangTypeInfo> clangTypeInfo = info.getClangTypeInfo();
31173117

3118-
size_t allocSize =
3119-
totalSizeToAlloc<AnyFunctionType::Param, ExtInfo::ClangTypeInfo>(
3118+
size_t allocSize = totalSizeToAlloc<AnyFunctionType::Param, ClangTypeInfo>(
31203119
params.size(), clangTypeInfo.hasValue() ? 1 : 0);
31213120
void *mem = ctx.Allocate(allocSize, alignof(FunctionType), arena);
31223121

@@ -3146,7 +3145,7 @@ FunctionType::FunctionType(ArrayRef<AnyFunctionType::Param> params,
31463145
getTrailingObjects<AnyFunctionType::Param>());
31473146
auto clangTypeInfo = info.getClangTypeInfo();
31483147
if (clangTypeInfo.hasValue())
3149-
*getTrailingObjects<ExtInfo::ClangTypeInfo>() = clangTypeInfo.getValue();
3148+
*getTrailingObjects<ClangTypeInfo>() = clangTypeInfo.getValue();
31503149
}
31513150

31523151
void GenericFunctionType::Profile(llvm::FoldingSetNodeID &ID,
@@ -3239,11 +3238,6 @@ ArrayRef<Requirement> GenericFunctionType::getRequirements() const {
32393238
return Signature->getRequirements();
32403239
}
32413240

3242-
void SILFunctionType::ExtInfo::ClangTypeInfo::printType(
3243-
ClangModuleLoader *cml, llvm::raw_ostream &os) const {
3244-
cml->printClangType(ClangFunctionType, os);
3245-
}
3246-
32473241
void SILFunctionType::Profile(
32483242
llvm::FoldingSetNodeID &id,
32493243
GenericSignature genericParams,
@@ -3302,13 +3296,14 @@ SILFunctionType::SILFunctionType(
33023296
WitnessMethodConformance(witnessMethodConformance) {
33033297

33043298
Bits.SILFunctionType.HasErrorResult = errorResult.hasValue();
3305-
Bits.SILFunctionType.ExtInfoBits = ext.Bits;
3299+
Bits.SILFunctionType.ExtInfoBits = ext.getBits();
33063300
Bits.SILFunctionType.HasClangTypeInfo = false;
33073301
Bits.SILFunctionType.HasPatternSubs = (bool) patternSubs;
33083302
Bits.SILFunctionType.HasInvocationSubs = (bool) invocationSubs;
33093303
// The use of both assert() and static_assert() below is intentional.
3310-
assert(Bits.SILFunctionType.ExtInfoBits == ext.Bits && "Bits were dropped!");
3311-
static_assert(ExtInfo::NumMaskBits == NumSILExtInfoBits,
3304+
assert(Bits.SILFunctionType.ExtInfoBits == ext.getBits() &&
3305+
"Bits were dropped!");
3306+
static_assert(SILExtInfoBuilder::NumMaskBits == NumSILExtInfoBits,
33123307
"ExtInfo and SILFunctionTypeBitfields must agree on bit size");
33133308
Bits.SILFunctionType.CoroutineKind = unsigned(coroutineKind);
33143309
NumParameters = params.size();

lib/AST/ASTDemangler.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -400,18 +400,16 @@ Type ASTBuilder::createFunctionType(
400400
|| representation == FunctionTypeRepresentation::Block)
401401
&& !flags.isEscaping();
402402

403-
FunctionType::ExtInfo incompleteExtInfo(
404-
FunctionTypeRepresentation::Swift,
405-
noescape, flags.throws(), diffKind, /*clangFunctionType*/nullptr);
406-
407403
const clang::Type *clangFunctionType = nullptr;
408404
if (representation == FunctionTypeRepresentation::CFunctionPointer)
409405
clangFunctionType = Ctx.getClangFunctionType(funcParams, output,
410406
representation);
411407

412-
auto einfo = incompleteExtInfo.withRepresentation(representation)
413-
.withAsync(flags.async())
414-
.withClangFunctionType(clangFunctionType);
408+
auto einfo =
409+
FunctionType::ExtInfoBuilder(representation, noescape, flags.throws(),
410+
diffKind, clangFunctionType)
411+
.withAsync(flags.async())
412+
.build();
415413

416414
return FunctionType::get(funcParams, output, einfo);
417415
}
@@ -531,9 +529,10 @@ Type ASTBuilder::createImplFunctionType(
531529
}
532530

533531
// [TODO: Store-SIL-Clang-type]
534-
auto einfo = SILFunctionType::ExtInfo(representation, flags.isPseudogeneric(),
535-
!flags.isEscaping(), diffKind,
536-
/*clangFunctionType*/ nullptr);
532+
auto einfo = SILExtInfoBuilder(representation, flags.isPseudogeneric(),
533+
!flags.isEscaping(), diffKind,
534+
/*clangFunctionType*/ nullptr)
535+
.build();
537536

538537
llvm::SmallVector<SILParameterInfo, 8> funcParams;
539538
llvm::SmallVector<SILYieldInfo, 8> funcYields;

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3760,10 +3760,10 @@ namespace {
37603760

37613761
OS << "\n";
37623762
Indent += 2;
3763-
if (auto *cty = T->getClangFunctionType()) {
3763+
if (!T->getClangTypeInfo().empty()) {
37643764
std::string s;
37653765
llvm::raw_string_ostream os(s);
3766-
cty->dump(os);
3766+
T->getClangTypeInfo().dump(os);
37673767
printField("clang_type", os.str());
37683768
}
37693769
printAnyFunctionParams(T->getParams(), "input");

lib/AST/Builtins.cpp

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,22 +1036,23 @@ static ValueDecl *getAutoDiffApplyDerivativeFunction(
10361036
fnParamGens.push_back(T);
10371037
}
10381038
// Generator for the first argument, i.e. the `@differentiable` function.
1039-
BuiltinFunctionBuilder::LambdaGenerator firstArgGen {
1040-
// Generator for the function type at the argument position, i.e. the
1041-
// function being differentiated.
1042-
[=, &fnParamGens](BuiltinFunctionBuilder &builder) -> Type {
1043-
FunctionType::ExtInfo ext;
1044-
auto extInfo = FunctionType::ExtInfo()
1045-
.withDifferentiabilityKind(DifferentiabilityKind::Normal)
1046-
.withNoEscape().withThrows(throws);
1047-
SmallVector<FunctionType::Param, 2> params;
1048-
for (auto &paramGen : fnParamGens)
1049-
params.push_back(FunctionType::Param(paramGen.build(builder)));
1050-
auto innerFunction = FunctionType::get(params,
1051-
fnResultGen.build(builder));
1052-
return innerFunction->withExtInfo(extInfo);
1053-
}
1054-
};
1039+
BuiltinFunctionBuilder::LambdaGenerator firstArgGen{
1040+
// Generator for the function type at the argument position, i.e. the
1041+
// function being differentiated.
1042+
[=, &fnParamGens](BuiltinFunctionBuilder &builder) -> Type {
1043+
auto extInfo =
1044+
FunctionType::ExtInfoBuilder()
1045+
.withDifferentiabilityKind(DifferentiabilityKind::Normal)
1046+
.withNoEscape()
1047+
.withThrows(throws)
1048+
.build();
1049+
SmallVector<FunctionType::Param, 2> params;
1050+
for (auto &paramGen : fnParamGens)
1051+
params.push_back(FunctionType::Param(paramGen.build(builder)));
1052+
auto innerFunction =
1053+
FunctionType::get(params, fnResultGen.build(builder));
1054+
return innerFunction->withExtInfo(extInfo);
1055+
}};
10551056
// Eagerly build the type of the first arg, then use that to compute the type
10561057
// of the result.
10571058
auto *diffFnType =
@@ -1106,9 +1107,12 @@ static ValueDecl *getAutoDiffApplyTransposeFunction(
11061107
// function being differentiated.
11071108
[=, &linearFnParamGens](BuiltinFunctionBuilder &builder) -> Type {
11081109
FunctionType::ExtInfo ext;
1109-
auto extInfo = FunctionType::ExtInfo()
1110-
.withDifferentiabilityKind(DifferentiabilityKind::Linear)
1111-
.withNoEscape().withThrows(throws);
1110+
auto extInfo =
1111+
FunctionType::ExtInfoBuilder()
1112+
.withDifferentiabilityKind(DifferentiabilityKind::Linear)
1113+
.withNoEscape()
1114+
.withThrows(throws)
1115+
.build();
11121116
SmallVector<FunctionType::Param, 2> params;
11131117
for (auto &paramGen : linearFnParamGens)
11141118
params.push_back(FunctionType::Param(paramGen.build(builder)));
@@ -1164,8 +1168,9 @@ static ValueDecl *getDifferentiableFunctionConstructor(
11641168
for (auto &paramGen : fnArgGens)
11651169
params.push_back(FunctionType::Param(paramGen.build(builder)));
11661170
return FunctionType::get(params, origResultGen.build(builder))
1167-
->withExtInfo(
1168-
FunctionType::ExtInfo(FunctionTypeRepresentation::Swift, throws));
1171+
->withExtInfo(FunctionType::ExtInfoBuilder(
1172+
FunctionTypeRepresentation::Swift, throws)
1173+
.build());
11691174
}
11701175
};
11711176

@@ -1189,8 +1194,9 @@ static ValueDecl *getDifferentiableFunctionConstructor(
11891194
{TupleTypeElt(origResultType, Context.Id_value),
11901195
TupleTypeElt(differentialType, Context.Id_differential)}, Context);
11911196
return FunctionType::get(params, jvpResultType)
1192-
->withExtInfo(
1193-
FunctionType::ExtInfo(FunctionTypeRepresentation::Swift, throws));
1197+
->withExtInfo(FunctionType::ExtInfoBuilder(
1198+
FunctionTypeRepresentation::Swift, throws)
1199+
.build());
11941200
}
11951201
};
11961202

@@ -1217,8 +1223,9 @@ static ValueDecl *getDifferentiableFunctionConstructor(
12171223
{TupleTypeElt(origResultType, Context.Id_value),
12181224
TupleTypeElt(pullbackType, Context.Id_pullback)}, Context);
12191225
return FunctionType::get(params, vjpResultType)
1220-
->withExtInfo(
1221-
FunctionType::ExtInfo(FunctionTypeRepresentation::Swift, throws));
1226+
->withExtInfo(FunctionType::ExtInfoBuilder(
1227+
FunctionTypeRepresentation::Swift, throws)
1228+
.build());
12221229
}
12231230
};
12241231

@@ -1227,7 +1234,9 @@ static ValueDecl *getDifferentiableFunctionConstructor(
12271234
auto origFnType = origFnGen.build(builder)->castTo<FunctionType>();
12281235
return origFnType->withExtInfo(
12291236
origFnType->getExtInfo()
1230-
.withDifferentiabilityKind(DifferentiabilityKind::Normal));
1237+
.intoBuilder()
1238+
.withDifferentiabilityKind(DifferentiabilityKind::Normal)
1239+
.build());
12311240
}
12321241
};
12331242

@@ -1266,8 +1275,9 @@ static ValueDecl *getLinearFunctionConstructor(
12661275
for (auto &paramGen : fnArgGens)
12671276
params.push_back(FunctionType::Param(paramGen.build(builder)));
12681277
return FunctionType::get(params, origResultGen.build(builder))
1269-
->withExtInfo(
1270-
FunctionType::ExtInfo(FunctionTypeRepresentation::Swift, throws));
1278+
->withExtInfo(FunctionType::ExtInfoBuilder(
1279+
FunctionTypeRepresentation::Swift, throws)
1280+
.build());
12711281
}
12721282
};
12731283

@@ -1290,7 +1300,9 @@ static ValueDecl *getLinearFunctionConstructor(
12901300
auto origFnType = origFnGen.build(builder)->castTo<FunctionType>();
12911301
return origFnType->withExtInfo(
12921302
origFnType->getExtInfo()
1293-
.withDifferentiabilityKind(DifferentiabilityKind::Linear));
1303+
.intoBuilder()
1304+
.withDifferentiabilityKind(DifferentiabilityKind::Linear)
1305+
.build());
12941306
}
12951307
};
12961308

@@ -1540,8 +1552,10 @@ static ValueDecl *getOnceOperation(ASTContext &Context,
15401552

15411553
auto HandleTy = Context.TheRawPointerType;
15421554
auto VoidTy = Context.TheEmptyTupleType;
1543-
auto Thin = FunctionType::ExtInfo(FunctionTypeRepresentation::CFunctionPointer,
1544-
/*throws*/ false);
1555+
auto Thin =
1556+
FunctionType::ExtInfoBuilder(FunctionTypeRepresentation::CFunctionPointer,
1557+
/*throws*/ false)
1558+
.build();
15451559
if (withContext) {
15461560
auto ContextTy = Context.TheRawPointerType;
15471561
auto ContextArg = FunctionType::Param(ContextTy);

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ add_swift_host_library(swiftAST STATIC
4444
DocComment.cpp
4545
Evaluator.cpp
4646
Expr.cpp
47+
ExtInfo.cpp
4748
FineGrainedDependencies.cpp
4849
FineGrainedDependencyFormat.cpp
4950
FrontendSourceFileDepGraphFactory.cpp

lib/AST/ClangTypeConverter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ clang::QualType ClangTypeConverter::visitEnumType(EnumType *type) {
572572

573573
clang::QualType ClangTypeConverter::visitFunctionType(FunctionType *type) {
574574
// We must've already computed it before if applicable.
575-
return clang::QualType(type->getClangFunctionType(), 0);
575+
return clang::QualType(type->getClangTypeInfo().getType(), 0);
576576
}
577577

578578
clang::QualType ClangTypeConverter::visitSILFunctionType(SILFunctionType *type) {

lib/AST/Decl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,10 +2576,11 @@ mapSignatureExtInfo(AnyFunctionType::ExtInfo info,
25762576
bool topLevelFunction) {
25772577
if (topLevelFunction)
25782578
return AnyFunctionType::ExtInfo();
2579-
return AnyFunctionType::ExtInfo()
2579+
return AnyFunctionType::ExtInfoBuilder()
25802580
.withRepresentation(info.getRepresentation())
25812581
.withAsync(info.async())
2582-
.withThrows(info.throws());
2582+
.withThrows(info.throws())
2583+
.build();
25832584
}
25842585

25852586
/// Map a function's type to the type used for computing signatures,

0 commit comments

Comments
 (0)