Skip to content

[NFC] Rename ExtInfo::Uncommon to ExtInfo::ClangTypeInfo. #33117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 34 additions & 36 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
/// Extra information which affects how the function is called, like
/// regparm and the calling convention.
ExtInfoBits : NumAFTExtInfoBits,
HasUncommonInfo : 1,
HasClangTypeInfo : 1,
: NumPadBits,
NumParams : 16
);
Expand All @@ -365,7 +365,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {

SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+1+3+1+2+1+1,
ExtInfoBits : NumSILExtInfoBits,
HasUncommonInfo : 1,
HasClangTypeInfo : 1,
CalleeConvention : 3,
HasErrorResult : 1,
CoroutineKind : 2,
Expand Down Expand Up @@ -2987,7 +2987,7 @@ class AnyFunctionType : public TypeBase {
unsigned Bits; // Naturally sized for speed.

public:
class Uncommon {
class ClangTypeInfo {
friend ExtInfo;
friend class AnyFunctionType;
friend class FunctionType;
Expand All @@ -2996,27 +2996,26 @@ class AnyFunctionType : public TypeBase {
// 2. The actual type being stored is [ignoring sugar] either a
// clang::PointerType, a clang::BlockPointerType, or a
// clang::ReferenceType which points to a clang::FunctionType.
const clang::Type *ClangFunctionType;
const clang::Type *type;

bool empty() const { return !ClangFunctionType; }
Uncommon(const clang::Type *type) : ClangFunctionType(type) {}
bool empty() const { return !type; }
ClangTypeInfo(const clang::Type *type) : type(type) {}

public:
/// Use the ClangModuleLoader to print the Clang type as a string.
void printClangFunctionType(ClangModuleLoader *cml,
llvm::raw_ostream &os);
void printType(ClangModuleLoader *cml, llvm::raw_ostream &os) const;
};

private:
Uncommon Other;
ClangTypeInfo Other;

static void assertIsFunctionType(const clang::Type *);

ExtInfo(unsigned Bits, Uncommon Other) : Bits(Bits), Other(Other) {
ExtInfo(unsigned Bits, ClangTypeInfo Other) : Bits(Bits), Other(Other) {
// [TODO: Clang-type-plumbing] Assert that the pointer is non-null.
auto Rep = Representation(Bits & RepresentationMask);
if ((Rep == Representation::CFunctionPointer) && Other.ClangFunctionType)
assertIsFunctionType(Other.ClangFunctionType);
if ((Rep == Representation::CFunctionPointer) && Other.type)
assertIsFunctionType(Other.type);
}

friend AnyFunctionType;
Expand Down Expand Up @@ -3045,7 +3044,7 @@ class AnyFunctionType : public TypeBase {
| (Throws ? ThrowsMask : 0)
| (((unsigned)DiffKind << DifferentiabilityMaskOffset)
& DifferentiabilityMask),
Uncommon(type)) {
ClangTypeInfo(type)) {
}

bool isNoEscape() const { return Bits & NoEscapeMask; }
Expand All @@ -3065,9 +3064,9 @@ class AnyFunctionType : public TypeBase {
return Representation(rawRep);
}

/// Return the underlying Uncommon value if it is not the default value.
Optional<Uncommon> getUncommonInfo() const {
return Other.empty() ? Optional<Uncommon>() : Other;
/// Get the underlying ClangTypeInfo value if it is not the default value.
Optional<ClangTypeInfo> getClangTypeInfo() const {
return Other.empty() ? Optional<ClangTypeInfo>() : Other;
}

bool hasSelfParam() const {
Expand Down Expand Up @@ -3124,7 +3123,7 @@ class AnyFunctionType : public TypeBase {
}
LLVM_NODISCARD
ExtInfo withClangFunctionType(const clang::Type *type) const {
return ExtInfo(Bits, Uncommon(type));
return ExtInfo(Bits, ClangTypeInfo(type));
}
LLVM_NODISCARD
ExtInfo
Expand All @@ -3136,7 +3135,7 @@ class AnyFunctionType : public TypeBase {
}

std::pair<unsigned, const void *> getFuncAttrKey() const {
return std::make_pair(Bits, Other.ClangFunctionType);
return std::make_pair(Bits, Other.type);
}

/// Put a SIL representation in the ExtInfo.
Expand Down Expand Up @@ -3166,13 +3165,13 @@ class AnyFunctionType : public TypeBase {
/// Create an AnyFunctionType.
///
/// Subclasses are responsible for storing and retrieving the
/// ExtInfo::Uncommon value if one is present.
/// ExtInfo::ClangTypeInfo value if one is present.
AnyFunctionType(TypeKind Kind, const ASTContext *CanTypeContext,
Type Output, RecursiveTypeProperties properties,
unsigned NumParams, ExtInfo Info)
: TypeBase(Kind, CanTypeContext, properties), Output(Output) {
Bits.AnyFunctionType.ExtInfoBits = Info.Bits;
Bits.AnyFunctionType.HasUncommonInfo = Info.getUncommonInfo().hasValue();
Bits.AnyFunctionType.HasClangTypeInfo = Info.getClangTypeInfo().hasValue();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @varungandhi-apple, looking at this I was wondering if maybe worth to ExtInfo to have an hasClangTypeInfo() API? Just a thought, maybe something like this could not be a relevant gain but could be an improvement over the getClangTypeInfo().hasValue() calls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I deliberately avoided that. IMO it doesn't make sense to implement hasXYZ(), getXYZ() for every type and optional field; that's precisely what Optional is for, so you can just return an Optional and have the caller use that. If it was the case that a large number of callers want to check only for existence, I would've been fine with adding a convenience method, but that's not the case here.

Bits.AnyFunctionType.NumParams = NumParams;
assert(Bits.AnyFunctionType.NumParams == NumParams && "Params dropped!");
// The use of both assert() and static_assert() is intentional.
Expand Down Expand Up @@ -3216,7 +3215,7 @@ class AnyFunctionType : public TypeBase {
GenericSignature getOptGenericSignature() const;

bool hasClangFunctionType() const {
return Bits.AnyFunctionType.HasUncommonInfo;
return Bits.AnyFunctionType.HasClangTypeInfo;
}

const clang::Type *getClangFunctionType() const;
Expand Down Expand Up @@ -3435,15 +3434,15 @@ inline AnyFunctionType::CanYield AnyFunctionType::Yield::getCanonical() const {
class FunctionType final : public AnyFunctionType,
public llvm::FoldingSetNode,
private llvm::TrailingObjects<FunctionType, AnyFunctionType::Param,
AnyFunctionType::ExtInfo::Uncommon> {
AnyFunctionType::ExtInfo::ClangTypeInfo> {
friend TrailingObjects;


size_t numTrailingObjects(OverloadToken<AnyFunctionType::Param>) const {
return getNumParams();
}

size_t numTrailingObjects(OverloadToken<ExtInfo::Uncommon>) const {
size_t numTrailingObjects(OverloadToken<ExtInfo::ClangTypeInfo>) const {
return hasClangFunctionType() ? 1 : 0;
}

Expand All @@ -3460,7 +3459,7 @@ class FunctionType final : public AnyFunctionType,
const clang::Type *getClangFunctionType() const {
if (!hasClangFunctionType())
return nullptr;
auto *type = getTrailingObjects<ExtInfo::Uncommon>()->ClangFunctionType;
auto *type = getTrailingObjects<ExtInfo::ClangTypeInfo>()->type;
assert(type && "If the pointer was null, we shouldn't have stored it.");
return type;
}
Expand Down Expand Up @@ -4218,7 +4217,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,

unsigned Bits; // Naturally sized for speed.

class Uncommon {
class ClangTypeInfo {
friend ExtInfo;
friend class SILFunctionType;

Expand All @@ -4228,22 +4227,21 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
const clang::FunctionType *ClangFunctionType;

bool empty() const { return !ClangFunctionType; }
Uncommon(const clang::FunctionType *type) : ClangFunctionType(type) {}
ClangTypeInfo(const clang::FunctionType *type) : ClangFunctionType(type) {}

public:
/// Analog of AnyFunctionType::ExtInfo::Uncommon::printClangFunctionType.
void printClangFunctionType(ClangModuleLoader *cml,
llvm::raw_ostream &os) const;
/// Analog of AnyFunctionType::ExtInfo::ClangTypeInfo::printType.
void printType(ClangModuleLoader *cml, llvm::raw_ostream &os) const;
};

Uncommon Other;
ClangTypeInfo Other;

ExtInfo(unsigned Bits, Uncommon Other) : Bits(Bits), Other(Other) {}
ExtInfo(unsigned Bits, ClangTypeInfo Other) : Bits(Bits), Other(Other) {}

friend class SILFunctionType;
public:
// Constructor with all defaults.
ExtInfo() : Bits(0), Other(Uncommon(nullptr)) { }
ExtInfo() : Bits(0), Other(ClangTypeInfo(nullptr)) { }

// Constructor for polymorphic type.
ExtInfo(Representation rep, bool isPseudogeneric, bool isNoEscape,
Expand All @@ -4254,7 +4252,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
| (isNoEscape ? NoEscapeMask : 0)
| (((unsigned)diffKind << DifferentiabilityMaskOffset)
& DifferentiabilityMask),
Uncommon(type)) {
ClangTypeInfo(type)) {
}

static ExtInfo getThin() {
Expand Down Expand Up @@ -4287,9 +4285,9 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
return getSILFunctionLanguage(getRepresentation());
}

/// Return the underlying Uncommon value if it is not the default value.
Optional<Uncommon> getUncommonInfo() const {
return Other.empty() ? Optional<Uncommon>() : Other;
/// Get the underlying ClangTypeInfo value if it is not the default value.
Optional<ClangTypeInfo> getClangTypeInfo() const {
return Other.empty() ? Optional<ClangTypeInfo>() : Other;
}

bool hasSelfParam() const {
Expand Down
20 changes: 10 additions & 10 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3082,17 +3082,17 @@ FunctionType *FunctionType::get(ArrayRef<AnyFunctionType::Param> params,
return funcTy;
}

Optional<ExtInfo::Uncommon> uncommon = info.getUncommonInfo();
Optional<ExtInfo::ClangTypeInfo> clangTypeInfo = info.getClangTypeInfo();

size_t allocSize =
totalSizeToAlloc<AnyFunctionType::Param, ExtInfo::Uncommon>(
params.size(), uncommon.hasValue() ? 1 : 0);
totalSizeToAlloc<AnyFunctionType::Param, ExtInfo::ClangTypeInfo>(
params.size(), clangTypeInfo.hasValue() ? 1 : 0);
void *mem = ctx.Allocate(allocSize, alignof(FunctionType), arena);

bool isCanonical = isFunctionTypeCanonical(params, result);
if (uncommon.hasValue()) {
if (clangTypeInfo.hasValue()) {
if (ctx.LangOpts.UseClangFunctionTypes)
isCanonical &= uncommon->ClangFunctionType->isCanonicalUnqualified();
isCanonical &= clangTypeInfo->type->isCanonicalUnqualified();
else
isCanonical = false;
}
Expand All @@ -3113,9 +3113,9 @@ FunctionType::FunctionType(ArrayRef<AnyFunctionType::Param> params,
output, properties, params.size(), info) {
std::uninitialized_copy(params.begin(), params.end(),
getTrailingObjects<AnyFunctionType::Param>());
Optional<ExtInfo::Uncommon> uncommon = info.getUncommonInfo();
if (uncommon.hasValue())
*getTrailingObjects<ExtInfo::Uncommon>() = uncommon.getValue();
auto clangTypeInfo = info.getClangTypeInfo();
if (clangTypeInfo.hasValue())
*getTrailingObjects<ExtInfo::ClangTypeInfo>() = clangTypeInfo.getValue();
}

void GenericFunctionType::Profile(llvm::FoldingSetNodeID &ID,
Expand Down Expand Up @@ -3208,7 +3208,7 @@ ArrayRef<Requirement> GenericFunctionType::getRequirements() const {
return Signature->getRequirements();
}

void SILFunctionType::ExtInfo::Uncommon::printClangFunctionType(
void SILFunctionType::ExtInfo::ClangTypeInfo::printType(
ClangModuleLoader *cml, llvm::raw_ostream &os) const {
cml->printClangType(ClangFunctionType, os);
}
Expand Down Expand Up @@ -3272,7 +3272,7 @@ SILFunctionType::SILFunctionType(

Bits.SILFunctionType.HasErrorResult = errorResult.hasValue();
Bits.SILFunctionType.ExtInfoBits = ext.Bits;
Bits.SILFunctionType.HasUncommonInfo = false;
Bits.SILFunctionType.HasClangTypeInfo = false;
Bits.SILFunctionType.HasPatternSubs = (bool) patternSubs;
Bits.SILFunctionType.HasInvocationSubs = (bool) invocationSubs;
// The use of both assert() and static_assert() below is intentional.
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3598,7 +3598,7 @@ void printCType(ASTContext &Ctx, ASTPrinter &Printer, ExtInfo &info) {
auto *cml = Ctx.getClangModuleLoader();
SmallString<64> buf;
llvm::raw_svector_ostream os(buf);
info.getUncommonInfo().getValue().printClangFunctionType(cml, os);
info.getClangTypeInfo().getValue().printType(cml, os);
Printer << ", cType: " << QuotedString(os.str());
}

Expand Down Expand Up @@ -4020,7 +4020,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
case SILFunctionType::Representation::CFunctionPointer:
Printer << "c";
// [TODO: Clang-type-plumbing] Remove the second check.
if (printNameOnly || !info.getUncommonInfo().hasValue())
if (printNameOnly || !info.getClangTypeInfo().hasValue())
break;
printCType(Ctx, Printer, info);
break;
Expand Down Expand Up @@ -4086,7 +4086,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
case SILFunctionType::Representation::CFunctionPointer:
Printer << "c";
// [TODO: Clang-type-plumbing] Remove the second check.
if (printNameOnly || !info.getUncommonInfo().hasValue())
if (printNameOnly || !info.getClangTypeInfo().hasValue())
break;
printCType(Ctx, Printer, info);
break;
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3369,9 +3369,9 @@ Type ProtocolCompositionType::get(const ASTContext &C,
return build(C, CanTypes, HasExplicitAnyObject);
}

void AnyFunctionType::ExtInfo::Uncommon::printClangFunctionType(
ClangModuleLoader *cml, llvm::raw_ostream &os) {
cml->printClangType(ClangFunctionType, os);
void AnyFunctionType::ExtInfo::ClangTypeInfo::printType(
ClangModuleLoader *cml, llvm::raw_ostream &os) const {
cml->printClangType(type, os);
}

void
Expand Down