Skip to content

Commit b80ca8e

Browse files
authored
Merge pull request swiftlang#36234 from davezarzycki/pr36234
[AST] NFC: Make ExtInfo param Optional<>
2 parents f843759 + c0ec6c3 commit b80ca8e

23 files changed

+392
-206
lines changed

include/swift/AST/Types.h

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "llvm/ADT/ArrayRef.h"
3838
#include "llvm/ADT/DenseMapInfo.h"
3939
#include "llvm/ADT/FoldingSet.h"
40+
#include "llvm/ADT/Optional.h"
4041
#include "llvm/ADT/PointerEmbeddedInt.h"
4142
#include "llvm/ADT/PointerUnion.h"
4243
#include "llvm/ADT/SmallBitVector.h"
@@ -347,10 +348,11 @@ class alignas(1 << TypeAlignInBits) TypeBase {
347348
Flags : NumFlagBits
348349
);
349350

350-
SWIFT_INLINE_BITFIELD_FULL(AnyFunctionType, TypeBase, NumAFTExtInfoBits+1+16,
351+
SWIFT_INLINE_BITFIELD_FULL(AnyFunctionType, TypeBase, NumAFTExtInfoBits+1+1+16,
351352
/// Extra information which affects how the function is called, like
352353
/// regparm and the calling convention.
353354
ExtInfoBits : NumAFTExtInfoBits,
355+
HasExtInfo : 1,
354356
HasClangTypeInfo : 1,
355357
: NumPadBits,
356358
NumParams : 16
@@ -2922,20 +2924,28 @@ class AnyFunctionType : public TypeBase {
29222924
///
29232925
/// Subclasses are responsible for storing and retrieving the
29242926
/// ClangTypeInfo value if one is present.
2925-
AnyFunctionType(TypeKind Kind, const ASTContext *CanTypeContext,
2926-
Type Output, RecursiveTypeProperties properties,
2927-
unsigned NumParams, ExtInfo Info)
2928-
: TypeBase(Kind, CanTypeContext, properties), Output(Output) {
2929-
Bits.AnyFunctionType.ExtInfoBits = Info.getBits();
2930-
Bits.AnyFunctionType.HasClangTypeInfo = !Info.getClangTypeInfo().empty();
2927+
AnyFunctionType(TypeKind Kind, const ASTContext *CanTypeContext, Type Output,
2928+
RecursiveTypeProperties properties, unsigned NumParams,
2929+
Optional<ExtInfo> Info)
2930+
: TypeBase(Kind, CanTypeContext, properties), Output(Output) {
2931+
if (Info.hasValue()) {
2932+
Bits.AnyFunctionType.HasExtInfo = true;
2933+
Bits.AnyFunctionType.ExtInfoBits = Info.getValue().getBits();
2934+
Bits.AnyFunctionType.HasClangTypeInfo =
2935+
!Info.getValue().getClangTypeInfo().empty();
2936+
// The use of both assert() and static_assert() is intentional.
2937+
assert(Bits.AnyFunctionType.ExtInfoBits == Info.getValue().getBits() &&
2938+
"Bits were dropped!");
2939+
static_assert(
2940+
ASTExtInfoBuilder::NumMaskBits == NumAFTExtInfoBits,
2941+
"ExtInfo and AnyFunctionTypeBitfields must agree on bit size");
2942+
} else {
2943+
Bits.AnyFunctionType.HasExtInfo = false;
2944+
Bits.AnyFunctionType.HasClangTypeInfo = false;
2945+
Bits.AnyFunctionType.ExtInfoBits = 0;
2946+
}
29312947
Bits.AnyFunctionType.NumParams = NumParams;
29322948
assert(Bits.AnyFunctionType.NumParams == NumParams && "Params dropped!");
2933-
// The use of both assert() and static_assert() is intentional.
2934-
assert(Bits.AnyFunctionType.ExtInfoBits == Info.getBits() &&
2935-
"Bits were dropped!");
2936-
static_assert(
2937-
ASTExtInfoBuilder::NumMaskBits == NumAFTExtInfoBits,
2938-
"ExtInfo and AnyFunctionTypeBitfields must agree on bit size");
29392949
}
29402950

29412951
public:
@@ -2996,7 +3006,10 @@ class AnyFunctionType : public TypeBase {
29963006
/// outer function type's mangling doesn't need to duplicate that information.
29973007
bool hasNonDerivableClangType();
29983008

3009+
bool hasExtInfo() const { return Bits.AnyFunctionType.HasExtInfo; }
3010+
29993011
ExtInfo getExtInfo() const {
3012+
assert(hasExtInfo());
30003013
return ExtInfo(Bits.AnyFunctionType.ExtInfoBits, getClangTypeInfo());
30013014
}
30023015

@@ -3005,6 +3018,7 @@ class AnyFunctionType : public TypeBase {
30053018
/// The parameter useClangFunctionType is present only for staging purposes.
30063019
/// In the future, we will always use the canonical clang function type.
30073020
ExtInfo getCanonicalExtInfo(bool useClangFunctionType) const {
3021+
assert(hasExtInfo());
30083022
return ExtInfo(Bits.AnyFunctionType.ExtInfoBits,
30093023
useClangFunctionType ? getCanonicalClangTypeInfo()
30103024
: ClangTypeInfo());
@@ -3196,7 +3210,7 @@ BEGIN_CAN_TYPE_WRAPPER(AnyFunctionType, Type)
31963210
static CanAnyFunctionType get(CanGenericSignature signature,
31973211
CanParamArrayRef params,
31983212
CanType result,
3199-
ExtInfo info = ExtInfo());
3213+
Optional<ExtInfo> info = None);
32003214

32013215
CanGenericSignature getOptGenericSignature() const;
32023216

@@ -3237,7 +3251,7 @@ class FunctionType final
32373251
public:
32383252
/// 'Constructor' Factory Function
32393253
static FunctionType *get(ArrayRef<Param> params, Type result,
3240-
ExtInfo info = ExtInfo());
3254+
Optional<ExtInfo> info = None);
32413255

32423256
// Retrieve the input parameters of this function type.
32433257
ArrayRef<Param> getParams() const {
@@ -3254,25 +3268,28 @@ class FunctionType final
32543268
}
32553269

32563270
void Profile(llvm::FoldingSetNodeID &ID) {
3257-
Profile(ID, getParams(), getResult(), getExtInfo());
3271+
Optional<ExtInfo> info = None;
3272+
if (hasExtInfo())
3273+
info = getExtInfo();
3274+
Profile(ID, getParams(), getResult(), info);
32583275
}
32593276
static void Profile(llvm::FoldingSetNodeID &ID,
32603277
ArrayRef<Param> params,
32613278
Type result,
3262-
ExtInfo info);
3279+
Optional<ExtInfo> info);
32633280

32643281
// Implement isa/cast/dyncast/etc.
32653282
static bool classof(const TypeBase *T) {
32663283
return T->getKind() == TypeKind::Function;
32673284
}
32683285

32693286
private:
3270-
FunctionType(ArrayRef<Param> params, Type result, ExtInfo info,
3287+
FunctionType(ArrayRef<Param> params, Type result, Optional<ExtInfo> info,
32713288
const ASTContext *ctx, RecursiveTypeProperties properties);
32723289
};
32733290
BEGIN_CAN_TYPE_WRAPPER(FunctionType, AnyFunctionType)
32743291
static CanFunctionType get(CanParamArrayRef params, CanType result,
3275-
ExtInfo info = ExtInfo()) {
3292+
Optional<ExtInfo> info = None) {
32763293
auto fnType = FunctionType::get(params.getOriginalArray(), result, info);
32773294
return cast<FunctionType>(fnType->getCanonicalType());
32783295
}
@@ -3337,7 +3354,7 @@ class GenericFunctionType final : public AnyFunctionType,
33373354
GenericFunctionType(GenericSignature sig,
33383355
ArrayRef<Param> params,
33393356
Type result,
3340-
ExtInfo info,
3357+
Optional<ExtInfo> info,
33413358
const ASTContext *ctx,
33423359
RecursiveTypeProperties properties);
33433360

@@ -3346,7 +3363,7 @@ class GenericFunctionType final : public AnyFunctionType,
33463363
static GenericFunctionType *get(GenericSignature sig,
33473364
ArrayRef<Param> params,
33483365
Type result,
3349-
ExtInfo info = ExtInfo());
3366+
Optional<ExtInfo> info = None);
33503367

33513368
// Retrieve the input parameters of this function type.
33523369
ArrayRef<Param> getParams() const {
@@ -3370,14 +3387,16 @@ class GenericFunctionType final : public AnyFunctionType,
33703387
FunctionType *substGenericArgs(llvm::function_ref<Type(Type)> substFn) const;
33713388

33723389
void Profile(llvm::FoldingSetNodeID &ID) {
3373-
Profile(ID, getGenericSignature(), getParams(), getResult(),
3374-
getExtInfo());
3390+
Optional<ExtInfo> info = None;
3391+
if (hasExtInfo())
3392+
info = getExtInfo();
3393+
Profile(ID, getGenericSignature(), getParams(), getResult(), info);
33753394
}
33763395
static void Profile(llvm::FoldingSetNodeID &ID,
33773396
GenericSignature sig,
33783397
ArrayRef<Param> params,
33793398
Type result,
3380-
ExtInfo info);
3399+
Optional<ExtInfo> info);
33813400

33823401
// Implement isa/cast/dyncast/etc.
33833402
static bool classof(const TypeBase *T) {
@@ -3390,11 +3409,11 @@ BEGIN_CAN_TYPE_WRAPPER(GenericFunctionType, AnyFunctionType)
33903409
static CanGenericFunctionType get(CanGenericSignature sig,
33913410
CanParamArrayRef params,
33923411
CanType result,
3393-
ExtInfo info = ExtInfo()) {
3412+
Optional<ExtInfo> info = None) {
33943413
// Knowing that the argument types are independently canonical is
33953414
// not sufficient to guarantee that the function type will be canonical.
3396-
auto fnType = GenericFunctionType::get(sig, params.getOriginalArray(),
3397-
result, info);
3415+
auto fnType =
3416+
GenericFunctionType::get(sig, params.getOriginalArray(), result, info);
33983417
return cast<GenericFunctionType>(fnType->getCanonicalType());
33993418
}
34003419

@@ -3416,7 +3435,7 @@ END_CAN_TYPE_WRAPPER(GenericFunctionType, AnyFunctionType)
34163435

34173436
inline CanAnyFunctionType
34183437
CanAnyFunctionType::get(CanGenericSignature signature, CanParamArrayRef params,
3419-
CanType result, ExtInfo extInfo) {
3438+
CanType result, Optional<ExtInfo> extInfo) {
34203439
if (signature) {
34213440
return CanGenericFunctionType::get(signature, params, result, extInfo);
34223441
} else {

lib/AST/ASTContext.cpp

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3245,18 +3245,19 @@ static void profileParams(llvm::FoldingSetNodeID &ID,
32453245
}
32463246

32473247
void FunctionType::Profile(llvm::FoldingSetNodeID &ID,
3248-
ArrayRef<AnyFunctionType::Param> params,
3249-
Type result,
3250-
ExtInfo info) {
3248+
ArrayRef<AnyFunctionType::Param> params, Type result,
3249+
Optional<ExtInfo> info) {
32513250
profileParams(ID, params);
32523251
ID.AddPointer(result.getPointer());
3253-
auto infoKey = info.getFuncAttrKey();
3254-
ID.AddInteger(infoKey.first);
3255-
ID.AddPointer(infoKey.second);
3252+
if (info.hasValue()) {
3253+
auto infoKey = info.getValue().getFuncAttrKey();
3254+
ID.AddInteger(infoKey.first);
3255+
ID.AddPointer(infoKey.second);
3256+
}
32563257
}
32573258

32583259
FunctionType *FunctionType::get(ArrayRef<AnyFunctionType::Param> params,
3259-
Type result, ExtInfo info) {
3260+
Type result, Optional<ExtInfo> info) {
32603261
auto properties = getFunctionRecursiveProperties(params, result);
32613262
auto arena = getArena(properties);
32623263

@@ -3272,10 +3273,15 @@ FunctionType *FunctionType::get(ArrayRef<AnyFunctionType::Param> params,
32723273
return funcTy;
32733274
}
32743275

3275-
auto clangTypeInfo = info.getClangTypeInfo();
3276+
ClangTypeInfo clangTypeInfo;
3277+
if (info.hasValue())
3278+
clangTypeInfo = info.getValue().getClangTypeInfo();
3279+
3280+
bool hasClangInfo =
3281+
info.hasValue() && !info.getValue().getClangTypeInfo().empty();
32763282

32773283
size_t allocSize = totalSizeToAlloc<AnyFunctionType::Param, ClangTypeInfo>(
3278-
params.size(), clangTypeInfo.empty() ? 0 : 1);
3284+
params.size(), hasClangInfo ? 1 : 0);
32793285
void *mem = ctx.Allocate(allocSize, alignof(FunctionType), arena);
32803286

32813287
bool isCanonical = isFunctionTypeCanonical(params, result);
@@ -3294,36 +3300,38 @@ FunctionType *FunctionType::get(ArrayRef<AnyFunctionType::Param> params,
32943300
}
32953301

32963302
// If the input and result types are canonical, then so is the result.
3297-
FunctionType::FunctionType(ArrayRef<AnyFunctionType::Param> params,
3298-
Type output, ExtInfo info,
3299-
const ASTContext *ctx,
3303+
FunctionType::FunctionType(ArrayRef<AnyFunctionType::Param> params, Type output,
3304+
Optional<ExtInfo> info, const ASTContext *ctx,
33003305
RecursiveTypeProperties properties)
3301-
: AnyFunctionType(TypeKind::Function, ctx,
3302-
output, properties, params.size(), info) {
3306+
: AnyFunctionType(TypeKind::Function, ctx, output, properties,
3307+
params.size(), info) {
33033308
std::uninitialized_copy(params.begin(), params.end(),
33043309
getTrailingObjects<AnyFunctionType::Param>());
3305-
auto clangTypeInfo = info.getClangTypeInfo();
3306-
if (!clangTypeInfo.empty())
3307-
*getTrailingObjects<ClangTypeInfo>() = clangTypeInfo;
3310+
if (info.hasValue()) {
3311+
auto clangTypeInfo = info.getValue().getClangTypeInfo();
3312+
if (!clangTypeInfo.empty())
3313+
*getTrailingObjects<ClangTypeInfo>() = clangTypeInfo;
3314+
}
33083315
}
33093316

33103317
void GenericFunctionType::Profile(llvm::FoldingSetNodeID &ID,
33113318
GenericSignature sig,
33123319
ArrayRef<AnyFunctionType::Param> params,
3313-
Type result,
3314-
ExtInfo info) {
3320+
Type result, Optional<ExtInfo> info) {
33153321
ID.AddPointer(sig.getPointer());
33163322
profileParams(ID, params);
33173323
ID.AddPointer(result.getPointer());
3318-
auto infoKey = info.getFuncAttrKey();
3319-
ID.AddInteger(infoKey.first);
3320-
ID.AddPointer(infoKey.second);
3324+
if (info.hasValue()) {
3325+
auto infoKey = info.getValue().getFuncAttrKey();
3326+
ID.AddInteger(infoKey.first);
3327+
ID.AddPointer(infoKey.second);
3328+
}
33213329
}
33223330

33233331
GenericFunctionType *GenericFunctionType::get(GenericSignature sig,
33243332
ArrayRef<Param> params,
33253333
Type result,
3326-
ExtInfo info) {
3334+
Optional<ExtInfo> info) {
33273335
assert(sig && "no generic signature for generic function type?!");
33283336
assert(!result->hasTypeVariable());
33293337
assert(!result->hasPlaceholder());
@@ -3346,7 +3354,7 @@ GenericFunctionType *GenericFunctionType::get(GenericSignature sig,
33463354
// point.
33473355
bool isCanonical = isGenericFunctionTypeCanonical(sig, params, result);
33483356

3349-
assert(info.getClangTypeInfo().empty() &&
3357+
assert((!info.hasValue() || info.getValue().getClangTypeInfo().empty()) &&
33503358
"Generic functions do not have Clang types at the moment.");
33513359

33523360
if (auto funcTy
@@ -3370,7 +3378,7 @@ GenericFunctionType::GenericFunctionType(
33703378
GenericSignature sig,
33713379
ArrayRef<AnyFunctionType::Param> params,
33723380
Type result,
3373-
ExtInfo info,
3381+
Optional<ExtInfo> info,
33743382
const ASTContext *ctx,
33753383
RecursiveTypeProperties properties)
33763384
: AnyFunctionType(TypeKind::GenericFunction, ctx, result,

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,9 +2846,12 @@ CanType ASTMangler::getDeclTypeForMangling(
28462846

28472847
auto &C = decl->getASTContext();
28482848
if (decl->isInvalid()) {
2849-
if (isa<AbstractFunctionDecl>(decl))
2849+
if (isa<AbstractFunctionDecl>(decl)) {
2850+
// FIXME: Verify ExtInfo state is correct, not working by accident.
2851+
CanFunctionType::ExtInfo info;
28502852
return CanFunctionType::get({AnyFunctionType::Param(C.TheErrorType)},
2851-
C.TheErrorType);
2853+
C.TheErrorType, info);
2854+
}
28522855
return C.TheErrorType;
28532856
}
28542857

lib/AST/ASTPrinter.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4474,6 +4474,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
44744474
}
44754475

44764476
void printFunctionExtInfo(AnyFunctionType *fnType) {
4477+
if (!fnType->hasExtInfo()) {
4478+
Printer << "@_NO_EXTINFO ";
4479+
return;
4480+
}
44774481
auto &ctx = fnType->getASTContext();
44784482
auto info = fnType->getExtInfo();
44794483
if (Options.SkipAttributes)
@@ -4693,13 +4697,15 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
46934697
// If we're stripping argument labels from types, do it when printing.
46944698
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/false);
46954699

4696-
if (T->isAsync()) {
4697-
Printer << " ";
4698-
Printer.printKeyword("async", Options);
4699-
}
4700+
if (T->hasExtInfo()) {
4701+
if (T->isAsync()) {
4702+
Printer << " ";
4703+
Printer.printKeyword("async", Options);
4704+
}
47004705

4701-
if (T->isThrowing())
4702-
Printer << " " << tok::kw_throws;
4706+
if (T->isThrowing())
4707+
Printer << " " << tok::kw_throws;
4708+
}
47034709

47044710
Printer << " -> ";
47054711

@@ -4738,13 +4744,15 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
47384744

47394745
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/true);
47404746

4741-
if (T->isAsync()) {
4742-
Printer << " ";
4743-
Printer.printKeyword("async", Options);
4744-
}
4747+
if (T->hasExtInfo()) {
4748+
if (T->isAsync()) {
4749+
Printer << " ";
4750+
Printer.printKeyword("async", Options);
4751+
}
47454752

4746-
if (T->isThrowing())
4747-
Printer << " " << tok::kw_throws;
4753+
if (T->isThrowing())
4754+
Printer << " " << tok::kw_throws;
4755+
}
47484756

47494757
Printer << " -> ";
47504758
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);

0 commit comments

Comments
 (0)