Skip to content

Commit 922cc65

Browse files
committed
[NFC] Make templateArgument a template argument
This Boolean flag is used in ClangTypeConverter to indicate whether a type is being converted in the context of a template type parameter. This parameter can be made a template parameter because it is always a compile-time constant.
1 parent 44326e0 commit 922cc65

File tree

3 files changed

+49
-45
lines changed

3 files changed

+49
-45
lines changed

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6576,16 +6576,16 @@ const clang::Type *
65766576
ASTContext::getClangFunctionType(ArrayRef<AnyFunctionType::Param> params,
65776577
Type resultTy,
65786578
FunctionTypeRepresentation trueRep) {
6579-
return getClangTypeConverter().getFunctionType(params, resultTy, trueRep,
6580-
/*templateArgument=*/false);
6579+
return getClangTypeConverter().getFunctionType</*templateArgument=*/false>(
6580+
params, resultTy, trueRep);
65816581
}
65826582

65836583
const clang::Type *ASTContext::getCanonicalClangFunctionType(
65846584
ArrayRef<SILParameterInfo> params, std::optional<SILResultInfo> result,
65856585
SILFunctionType::Representation trueRep) {
65866586
auto *ty =
6587-
getClangTypeConverter().getFunctionType(params, result, trueRep,
6588-
/*templateArgument=*/false);
6587+
getClangTypeConverter().getFunctionType</*templateArgument=*/false>(
6588+
params, result, trueRep);
65896589
return ty ? ty->getCanonicalTypeInternal().getTypePtr() : nullptr;
65906590
}
65916591

lib/AST/ClangTypeConverter.cpp

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ const clang::ASTContext &clangCtx) {
123123

124124
} // end anonymous namespace
125125

126-
const clang::Type *ClangTypeConverter::getFunctionType(
127-
ArrayRef<AnyFunctionType::Param> params, Type resultTy,
128-
AnyFunctionType::Representation repr, bool templateArgument) {
126+
template <bool templateArgument>
127+
const clang::Type *
128+
ClangTypeConverter::getFunctionType(ArrayRef<AnyFunctionType::Param> params,
129+
Type resultTy,
130+
AnyFunctionType::Representation repr) {
129131
auto resultClangTy =
130132
templateArgument ? convertTemplateArgument(resultTy) : convert(resultTy);
131133
if (resultClangTy.isNull())
@@ -167,9 +169,11 @@ const clang::Type *ClangTypeConverter::getFunctionType(
167169
llvm_unreachable("invalid representation");
168170
}
169171

170-
const clang::Type *ClangTypeConverter::getFunctionType(
171-
ArrayRef<SILParameterInfo> params, std::optional<SILResultInfo> result,
172-
SILFunctionType::Representation repr, bool templateArgument) {
172+
template <bool templateArgument>
173+
const clang::Type *
174+
ClangTypeConverter::getFunctionType(ArrayRef<SILParameterInfo> params,
175+
std::optional<SILResultInfo> result,
176+
SILFunctionType::Representation repr) {
173177
clang::QualType resultClangTy = ClangASTContext.VoidTy;
174178
if (result) {
175179
// Using the interface type is sufficient as type parameters get mapped to
@@ -571,18 +575,18 @@ ClangTypeConverter::visitBoundGenericType(BoundGenericType *type) {
571575
}
572576

573577
if (auto kind = classifyPointer(type))
574-
return convertPointerType(argType, kind.value(),
575-
/*templateArgument=*/false);
578+
return convertPointerType</*templateArgument=*/false>(argType,
579+
kind.value());
576580

577581
if (auto width = classifySIMD(type))
578-
return convertSIMDType(argType, width.value(), /*templateArgument=*/false);
582+
return convertSIMDType</*templateArgument=*/false>(argType, width.value());
579583

580584
return clang::QualType();
581585
}
582586

587+
template <bool templateArgument>
583588
clang::QualType ClangTypeConverter::convertSIMDType(CanType scalarType,
584-
unsigned width,
585-
bool templateArgument) {
589+
unsigned width) {
586590
clang::QualType scalarTy = templateArgument
587591
? convertTemplateArgument(scalarType)
588592
: convert(scalarType);
@@ -594,9 +598,9 @@ clang::QualType ClangTypeConverter::convertSIMDType(CanType scalarType,
594598
return vectorTy;
595599
}
596600

601+
template <bool templateArgument>
597602
clang::QualType ClangTypeConverter::convertPointerType(CanType pointeeType,
598-
PointerKind kind,
599-
bool templateArgument) {
603+
PointerKind kind) {
600604
switch (kind) {
601605
case PointerKind::Unmanaged:
602606
return templateArgument ? clang::QualType() : convert(pointeeType);
@@ -657,8 +661,8 @@ clang::QualType ClangTypeConverter::visitEnumType(EnumType *type) {
657661
return convert(type->getDecl()->getRawType());
658662
}
659663

660-
clang::QualType ClangTypeConverter::visitFunctionType(FunctionType *type,
661-
bool templateArgument) {
664+
template <bool templateArgument>
665+
clang::QualType ClangTypeConverter::visitFunctionType(FunctionType *type) {
662666
const clang::Type *clangTy = nullptr;
663667
auto repr = type->getRepresentation();
664668
bool useClangTypes = type->getASTContext().LangOpts.UseClangFunctionTypes;
@@ -672,15 +676,15 @@ clang::QualType ClangTypeConverter::visitFunctionType(FunctionType *type,
672676
auto newRepr = (repr == FunctionTypeRepresentation::Swift
673677
? FunctionTypeRepresentation::Block
674678
: repr);
675-
clangTy = getFunctionType(type->getParams(), type->getResult(), newRepr,
676-
templateArgument);
679+
clangTy = getFunctionType<templateArgument>(type->getParams(),
680+
type->getResult(), newRepr);
677681
}
678682
return clang::QualType(clangTy, 0);
679683
}
680684

685+
template <bool templateArgument>
681686
clang::QualType
682-
ClangTypeConverter::visitSILFunctionType(SILFunctionType *type,
683-
bool templateArgument) {
687+
ClangTypeConverter::visitSILFunctionType(SILFunctionType *type) {
684688
const clang::Type *clangTy = nullptr;
685689
auto repr = type->getRepresentation();
686690
bool useClangTypes = type->getASTContext().LangOpts.UseClangFunctionTypes;
@@ -698,8 +702,8 @@ ClangTypeConverter::visitSILFunctionType(SILFunctionType *type,
698702
auto optionalResult = results.empty()
699703
? std::nullopt
700704
: std::optional<SILResultInfo>(results[0]);
701-
clangTy = getFunctionType(type->getParameters(), optionalResult, newRepr,
702-
templateArgument);
705+
clangTy = getFunctionType<templateArgument>(type->getParameters(),
706+
optionalResult, newRepr);
703707
}
704708
return clang::QualType(clangTy, 0);
705709
}
@@ -984,8 +988,8 @@ clang::QualType ClangTypeConverter::convertTemplateArgument(Type type) {
984988
auto pointeeType = argType->getAs<BoundGenericType>()
985989
->getGenericArgs()[0]
986990
->getCanonicalType();
987-
return convertPointerType(pointeeType, kind.value(),
988-
/*templateArgument=*/true);
991+
return convertPointerType</*templateArgument=*/true>(pointeeType,
992+
kind.value());
989993
});
990994

991995
// Arbitrary optional types are not (yet) supported
@@ -994,28 +998,28 @@ clang::QualType ClangTypeConverter::convertTemplateArgument(Type type) {
994998

995999
if (auto kind = classifyPointer(boundGenericType))
9961000
return withCache([&]() {
997-
return convertPointerType(argType, kind.value(),
998-
/*templateArgument=*/true);
1001+
return convertPointerType</*templateArgument=*/true>(argType,
1002+
kind.value());
9991003
});
10001004

10011005
if (auto width = classifySIMD(boundGenericType))
10021006
return withCache([&]() {
1003-
return convertSIMDType(argType, width.value(),
1004-
/*templateArgument=*/true);
1007+
return convertSIMDType</*templateArgument=*/true>(argType,
1008+
width.value());
10051009
});
10061010

10071011
return clang::QualType();
10081012
}
10091013

10101014
if (auto functionType = type->getAs<FunctionType>()) {
10111015
return withCache([&]() {
1012-
return visitFunctionType(functionType, /*templateArgument=*/true);
1016+
return visitFunctionType</*templateArgument=*/true>(functionType);
10131017
});
10141018
}
10151019

10161020
if (auto functionType = type->getAs<SILFunctionType>()) {
10171021
return withCache([&]() {
1018-
return visitSILFunctionType(functionType, /*templateArgument=*/true);
1022+
return visitSILFunctionType</*templateArgument=*/true>(functionType);
10191023
});
10201024
}
10211025

lib/AST/ClangTypeConverter.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,16 @@ class ClangTypeConverter :
7070
/// \returns The appropriate clang type on success, nullptr on failure.
7171
///
7272
/// Precondition: The representation argument must be C-compatible.
73+
template <bool templateArgument>
7374
const clang::Type *getFunctionType(ArrayRef<AnyFunctionType::Param> params,
7475
Type resultTy,
75-
AnyFunctionType::Representation repr,
76-
bool templateArgument);
76+
AnyFunctionType::Representation repr);
7777

7878
/// Compute the C function type for a SIL function type.
79+
template <bool templateArgument>
7980
const clang::Type *getFunctionType(ArrayRef<SILParameterInfo> params,
8081
std::optional<SILResultInfo> result,
81-
SILFunctionType::Representation repr,
82-
bool templateArgument);
82+
SILFunctionType::Representation repr);
8383

8484
/// Check whether the given Clang declaration is an export of a Swift
8585
/// declaration introduced by this converter, and if so, return the original
@@ -127,11 +127,11 @@ class ClangTypeConverter :
127127

128128
clang::QualType convertClangDecl(Type type, const clang::Decl *decl);
129129

130-
clang::QualType convertSIMDType(CanType scalarType, unsigned width,
131-
bool templateArgument);
130+
template <bool templateArgument>
131+
clang::QualType convertSIMDType(CanType scalarType, unsigned width);
132132

133-
clang::QualType convertPointerType(CanType pointeeType, PointerKind kind,
134-
bool templateArgument);
133+
template <bool templateArgument>
134+
clang::QualType convertPointerType(CanType pointeeType, PointerKind kind);
135135

136136
void registerExportedClangDecl(Decl *swiftDecl,
137137
const clang::Decl *clangDecl);
@@ -150,17 +150,17 @@ class ClangTypeConverter :
150150
clang::QualType visitBoundGenericClassType(BoundGenericClassType *type);
151151
clang::QualType visitBoundGenericType(BoundGenericType *type);
152152
clang::QualType visitEnumType(EnumType *type);
153-
clang::QualType visitFunctionType(FunctionType *type,
154-
bool templateArgument = false);
153+
template <bool templateArgument = false>
154+
clang::QualType visitFunctionType(FunctionType *type);
155155
clang::QualType visitProtocolCompositionType(ProtocolCompositionType *type);
156156
clang::QualType visitExistentialType(ExistentialType *type);
157157
clang::QualType visitBuiltinRawPointerType(BuiltinRawPointerType *type);
158158
clang::QualType visitBuiltinIntegerType(BuiltinIntegerType *type);
159159
clang::QualType visitBuiltinFloatType(BuiltinFloatType *type);
160160
clang::QualType visitArchetypeType(ArchetypeType *type);
161161
clang::QualType visitDependentMemberType(DependentMemberType *type);
162-
clang::QualType visitSILFunctionType(SILFunctionType *type,
163-
bool templateArgument = false);
162+
template <bool templateArgument = false>
163+
clang::QualType visitSILFunctionType(SILFunctionType *type);
164164
clang::QualType visitGenericTypeParamType(GenericTypeParamType *type);
165165
clang::QualType visitDynamicSelfType(DynamicSelfType *type);
166166
clang::QualType visitSILBlockStorageType(SILBlockStorageType *type);

0 commit comments

Comments
 (0)