Skip to content

Commit 8062f18

Browse files
authored
Merge pull request #7139 from slavapestov/serialize-fewer-archetypes-and-tidy-up-casts
Serialize fewer archetypes and tidy up casts
2 parents 00bceec + a0a4169 commit 8062f18

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+155
-139
lines changed

include/swift/AST/Decl.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,11 @@ class alignas(1 << DeclAlignInBits) Decl {
325325
/// \brief Whether or not this element directly or indirectly references
326326
/// the enum type.
327327
unsigned Recursiveness : 2;
328+
329+
/// \brief Whther or not this element has an associated value.
330+
unsigned HasArgumentType : 1;
328331
};
329-
enum { NumEnumElementDeclBits = NumValueDeclBits + 2 };
332+
enum { NumEnumElementDeclBits = NumValueDeclBits + 3 };
330333
static_assert(NumEnumElementDeclBits <= 32, "fits in an unsigned");
331334

332335
class AbstractFunctionDeclBitfields {
@@ -5323,6 +5326,7 @@ class EnumElementDecl : public ValueDecl {
53235326
public:
53245327
EnumElementDecl(SourceLoc IdentifierLoc, Identifier Name,
53255328
TypeLoc ArgumentType,
5329+
bool HasArgumentType,
53265330
SourceLoc EqualsLoc,
53275331
LiteralExpr *RawValueExpr,
53285332
DeclContext *DC)
@@ -5333,14 +5337,13 @@ class EnumElementDecl : public ValueDecl {
53335337
{
53345338
EnumElementDeclBits.Recursiveness =
53355339
static_cast<unsigned>(ElementRecursiveness::NotRecursive);
5340+
EnumElementDeclBits.HasArgumentType = HasArgumentType;
53365341
}
53375342

53385343
/// \returns false if there was an error during the computation rendering the
53395344
/// EnumElementDecl invalid, true otherwise.
53405345
bool computeType();
53415346

5342-
bool hasArgumentType() const { return !ArgumentType.getType().isNull(); }
5343-
Type getArgumentType() const { return ArgumentType.getType(); }
53445347
Type getArgumentInterfaceType() const;
53455348

53465349
TypeLoc &getArgumentTypeLoc() { return ArgumentType; }

include/swift/Serialization/ModuleFormat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 308; // Last change: nested type table
57+
const uint16_t VERSION_MINOR = 309; // Last change: remove enum argument type
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;
@@ -975,8 +975,8 @@ namespace decls_block {
975975
ENUM_ELEMENT_DECL,
976976
IdentifierIDField, // name
977977
DeclContextIDField,// context decl
978-
TypeIDField, // argument type
979978
TypeIDField, // interface type
979+
BCFixed<1>, // has argument type?
980980
BCFixed<1>, // implicit?
981981
EnumElementRawValueKindField, // raw value kind
982982
BCFixed<1>, // negative raw value?

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ struct SynthesizedExtensionAnalyzer::Implementation {
325325

326326
std::unique_ptr<ExtensionInfoMap>
327327
collectSynthesizedExtensionInfo(MergeGroupVector &AllGroups) {
328-
if (Target->getKind() == DeclKind::Protocol) {
328+
if (isa<ProtocolDecl>(Target)) {
329329
return collectSynthesizedExtensionInfoForProtocol(AllGroups);
330330
}
331331
std::unique_ptr<ExtensionInfoMap> InfoMap(new ExtensionInfoMap());
@@ -553,7 +553,7 @@ void ASTPrinter::callPrintDeclPre(const Decl *D,
553553
Optional<BracketOptions> Bracket) {
554554
forceNewlines();
555555

556-
if (SynthesizeTarget && D->getKind() == DeclKind::Extension)
556+
if (SynthesizeTarget && isa<ExtensionDecl>(D))
557557
printSynthesizedExtensionPre(cast<ExtensionDecl>(D), SynthesizeTarget, Bracket);
558558
else
559559
printDeclPre(D, Bracket);
@@ -1006,7 +1006,7 @@ class PrintAST : public ASTVisitor<PrintAST> {
10061006
bool Synthesize =
10071007
Options.TransformContext &&
10081008
Options.TransformContext->isPrintingSynthesizedExtension() &&
1009-
D->getKind() == DeclKind::Extension;
1009+
isa<ExtensionDecl>(D);
10101010
if (Synthesize)
10111011
Printer.setSynthesizedTarget(Options.TransformContext->getNominal());
10121012

@@ -2016,7 +2016,7 @@ static void printExtendedTypeName(Type ExtendedType, ASTPrinter &Printer,
20162016
}
20172017

20182018
// Respect alias type.
2019-
if (ExtendedType->getKind() == TypeKind::NameAlias) {
2019+
if (isa<NameAliasType>(ExtendedType.getPointer())) {
20202020
ExtendedType.print(Printer, Options);
20212021
return;
20222022
}
@@ -2720,10 +2720,9 @@ void PrintAST::printEnumElement(EnumElementDecl *elt) {
27202720
Printer.printName(elt->getName());
27212721
});
27222722

2723-
if (elt->hasArgumentType()) {
2724-
Type Ty = elt->getArgumentType();
2725-
if (!Options.SkipPrivateStdlibDecls || !Ty.isPrivateStdlibType())
2726-
Ty.print(Printer, Options);
2723+
if (auto argTy = elt->getArgumentInterfaceType()) {
2724+
if (!Options.SkipPrivateStdlibDecls || !argTy.isPrivateStdlibType())
2725+
argTy.print(Printer, Options);
27272726
}
27282727

27292728
auto *raw = elt->getRawValueExpr();

lib/AST/ASTWalker.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,9 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
321321
}
322322

323323
bool visitEnumElementDecl(EnumElementDecl *ED) {
324-
if (ED->hasArgumentType()) {
325-
if (auto TR = ED->getArgumentTypeLoc().getTypeRepr()) {
326-
if (doIt(TR)) {
327-
return true;
328-
}
324+
if (auto TR = ED->getArgumentTypeLoc().getTypeRepr()) {
325+
if (doIt(TR)) {
326+
return true;
329327
}
330328
}
331329

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4702,7 +4702,7 @@ bool EnumElementDecl::computeType() {
47024702
Type selfTy = MetatypeType::get(resultTy);
47034703

47044704
// The type of the enum element is either (T) -> T or (T) -> ArgType -> T.
4705-
if (auto inputTy = getArgumentType()) {
4705+
if (auto inputTy = getArgumentTypeLoc().getType()) {
47064706
resultTy = FunctionType::get(ED->mapTypeOutOfContext(inputTy), resultTy);
47074707
}
47084708

@@ -4719,7 +4719,7 @@ bool EnumElementDecl::computeType() {
47194719
}
47204720

47214721
Type EnumElementDecl::getArgumentInterfaceType() const {
4722-
if (!hasArgumentType())
4722+
if (!EnumElementDeclBits.HasArgumentType)
47234723
return nullptr;
47244724

47254725
auto interfaceType = getInterfaceType();

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4837,8 +4837,8 @@ Decl *SwiftDeclConverter::importEnumCase(const clang::EnumConstantDecl *decl,
48374837
rawValueExpr->setNegative(SourceLoc());
48384838

48394839
auto element = Impl.createDeclWithClangNode<EnumElementDecl>(
4840-
decl, Accessibility::Public, SourceLoc(), name, TypeLoc(), SourceLoc(),
4841-
rawValueExpr, theEnum);
4840+
decl, Accessibility::Public, SourceLoc(), name, TypeLoc(), false,
4841+
SourceLoc(), rawValueExpr, theEnum);
48424842

48434843
// Give the enum element the appropriate type.
48444844
element->computeType();

lib/IDE/CodeCompletion.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,8 @@ static CodeCompletionResult::ExpectedTypeRelation calculateTypeRelation(
913913
Type ExpectedTy,
914914
DeclContext *DC) {
915915
if (Ty.isNull() || ExpectedTy.isNull() ||
916-
Ty->getKind() == TypeKind::Error ||
917-
ExpectedTy->getKind() == TypeKind::Error)
916+
Ty->is<ErrorType>() ||
917+
ExpectedTy->is<ErrorType>())
918918
return CodeCompletionResult::ExpectedTypeRelation::Unrelated;
919919
if (Ty->isEqual(ExpectedTy))
920920
return CodeCompletionResult::ExpectedTypeRelation::Identical;
@@ -2694,8 +2694,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
26942694
setClangDeclKeywords(EED, Pairs, Builder);
26952695
addLeadingDot(Builder);
26962696
Builder.addTextChunk(EED->getName().str());
2697-
if (EED->hasArgumentType())
2698-
addPatternFromType(Builder, EED->getArgumentType());
2697+
if (auto argTy = EED->getArgumentInterfaceType())
2698+
addPatternFromType(Builder, argTy);
26992699

27002700
// Enum element is of function type such as EnumName.type -> Int ->
27012701
// EnumName; however we should show Int -> EnumName as the type
@@ -2833,7 +2833,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
28332833
Optional<Type> Result = None;
28342834
if (auto AT = MT->getInstanceType()) {
28352835
if (!CD->getInterfaceType()->is<ErrorType>() &&
2836-
AT->getKind() == TypeKind::NameAlias &&
2836+
isa<NameAliasType>(AT.getPointer()) &&
28372837
AT->getDesugaredType() ==
28382838
CD->getResultInterfaceType().getPointer())
28392839
Result = AT;
@@ -3658,9 +3658,9 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
36583658
}
36593659

36603660
void unboxType(Type T) {
3661-
if (T->getKind() == TypeKind::Paren) {
3661+
if (isa<ParenType>(T.getPointer())) {
36623662
unboxType(T->getDesugaredType());
3663-
} else if (T->getKind() == TypeKind::Tuple) {
3663+
} else if (T->is<TupleType>()) {
36643664
for (auto Ele : T->getAs<TupleType>()->getElements()) {
36653665
unboxType(Ele.getType());
36663666
}
@@ -3838,7 +3838,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
38383838
DeclContext *DC) {
38393839
for (auto It = PossibleArgTypes.begin(); It != PossibleArgTypes.end(); ) {
38403840
llvm::SmallVector<Type, 3> ExpectedTypes;
3841-
if ((*It)->getKind() == TypeKind::Tuple) {
3841+
if (isa<TupleType>((*It).getPointer())) {
38423842
auto Elements = (*It)->getAs<TupleType>()->getElements();
38433843
for (auto Ele : Elements)
38443844
ExpectedTypes.push_back(Ele.getType());
@@ -3874,7 +3874,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
38743874
removeUnlikelyOverloads(PossibleTypes, TupleEleTypesBeforeTarget, &DC);
38753875
return !PossibleTypes.empty();
38763876
}
3877-
} else if (CallE->getArg()->getKind() == ExprKind::Paren) {
3877+
} else if (isa<ParenExpr>(CallE->getArg())) {
38783878
Position = 0;
38793879
HasName = false;
38803880
if (PossibleTypes.empty() &&
@@ -4230,7 +4230,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
42304230
// Implement swift::VisibleDeclConsumer.
42314231
void foundDecl(ValueDecl *D, DeclVisibilityKind Reason) override {
42324232
if (Reason == DeclVisibilityKind::MemberOfCurrentNominal) {
4233-
if (D->getKind() == DeclKind::TypeAlias) {
4233+
if (isa<TypeAliasDecl>(D)) {
42344234
ValueDecl *VD = dyn_cast<ValueDecl>(D);
42354235
SatisfiedAssociatedTypes.insert(VD->getName().str());
42364236
}

lib/IDE/Formatting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ class FormatContext {
420420
} else if (auto *Seq = dyn_cast_or_null<SequenceExpr>(Cursor->getAsExpr())) {
421421
ArrayRef<Expr*> Elements = Seq->getElements();
422422
if (Elements.size() == 3 &&
423-
Elements[1]->getKind() == ExprKind::Assign &&
423+
isa<AssignExpr>(Elements[1]) &&
424424
SM.getLineAndColumn(Elements[2]->getEndLoc()).first == Line) {
425425
return false;
426426
}

lib/IDE/TypeReconstruction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ static bool FindFirstNamedDeclWithKind(
514514
// If we didn't find any exact matches, accept any type aliases
515515
if (check_type_aliases) {
516516
for (auto decl : decls) {
517-
if (decl->getKind() == DeclKind::TypeAlias) {
517+
if (isa<TypeAliasDecl>(decl)) {
518518
result._decls.assign(1, decl);
519519
if (decl->hasInterfaceType()) {
520520
result._types.assign(1, decl->getInterfaceType());

lib/IRGen/DebugTypeInfo.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,29 @@ class DebugTypeInfo {
7878

7979
void unwrapLValueOrInOutType() {
8080
Type = Type->getLValueOrInOutObjectType().getPointer();
81-
}
81+
}
8282

83-
// Determine whether this type is an Archetype itself.
84-
bool isArchetype() const {
85-
return Type->getLValueOrInOutObjectType()->is<ArchetypeType>();
86-
}
83+
// Determine whether this type is an Archetype itself.
84+
bool isArchetype() const {
85+
return Type->getLValueOrInOutObjectType()->is<ArchetypeType>();
86+
}
8787

88-
/// LValues, inout args, and Archetypes are implicitly indirect by
89-
/// virtue of their DWARF type.
90-
bool isImplicitlyIndirect() const {
91-
return Type->isLValueType() || isArchetype() ||
92-
(Type->getKind() == TypeKind::InOut);
93-
}
88+
/// LValues, inout args, and Archetypes are implicitly indirect by
89+
/// virtue of their DWARF type.
90+
//
91+
// FIXME: Should this check if the lowered SILType is address only
92+
// instead? Otherwise optionals of archetypes etc will still have
93+
// 'isImplicitlyIndirect()' return false.
94+
bool isImplicitlyIndirect() const {
95+
return Type->isLValueType() || isArchetype() ||
96+
Type->is<InOutType>();
97+
}
9498

95-
bool isNull() const { return Type == nullptr; }
96-
bool operator==(DebugTypeInfo T) const;
97-
bool operator!=(DebugTypeInfo T) const;
99+
bool isNull() const { return Type == nullptr; }
100+
bool operator==(DebugTypeInfo T) const;
101+
bool operator!=(DebugTypeInfo T) const;
98102

99-
void dump() const;
103+
void dump() const;
100104
};
101105
}
102106
}

lib/IRGen/GenEnum.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ const {
187187

188188
// Empty payload addresses can be left undefined.
189189
if (payloadI == ElementsWithPayload.end()) {
190-
return IGF.getTypeInfoForUnlowered(elt->getArgumentType())
190+
auto argTy = elt->getParentEnum()->mapTypeIntoContext(
191+
elt->getArgumentInterfaceType());
192+
return IGF.getTypeInfoForUnlowered(argTy)
191193
.getUndefAddress();
192194
}
193195

@@ -208,7 +210,9 @@ const {
208210

209211
// Empty payload addresses can be left undefined.
210212
if (payloadI == ElementsWithPayload.end()) {
211-
return IGF.getTypeInfoForUnlowered(Case->getArgumentType())
213+
auto argTy = Case->getParentEnum()->mapTypeIntoContext(
214+
Case->getArgumentInterfaceType());
215+
return IGF.getTypeInfoForUnlowered(argTy)
212216
.getUndefAddress();
213217
}
214218

@@ -4980,7 +4984,7 @@ EnumImplStrategy::get(TypeConverter &TC, SILType type, EnumDecl *theEnum) {
49804984
// strategy. If the abstract layout of the enum is dependent on generic
49814985
// parameters, then we additionally need to constrain any layout
49824986
// optimizations we perform to things that are reproducible by the runtime.
4983-
Type origArgType = elt->getArgumentType();
4987+
Type origArgType = elt->getArgumentInterfaceType();
49844988
if (origArgType.isNull()) {
49854989
elementsWithNoPayload.push_back({elt, nullptr, nullptr});
49864990
continue;
@@ -4994,6 +4998,8 @@ EnumImplStrategy::get(TypeConverter &TC, SILType type, EnumDecl *theEnum) {
49944998
continue;
49954999
}
49965000

5001+
origArgType = theEnum->mapTypeIntoContext(origArgType);
5002+
49975003
auto origArgLoweredTy = TC.IGM.getLoweredType(origArgType);
49985004
const TypeInfo *origArgTI
49995005
= TC.tryGetCompleteTypeInfo(origArgLoweredTy.getSwiftRValueType());

lib/IRGen/GenMeta.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,16 +2458,18 @@ namespace {
24582458
// using the lowered cases, i.e. the cases for Optional<>.
24592459
if (type->classifyAsOptionalType() == OTK_ImplicitlyUnwrappedOptional) {
24602460
assert(enumElements.size() == 1);
2461-
auto caseType =
2462-
IGM.Context.getImplicitlyUnwrappedOptionalSomeDecl()
2463-
->getArgumentType()->getCanonicalType();
2461+
auto decl = IGM.Context.getImplicitlyUnwrappedOptionalSomeDecl();
2462+
auto caseType = decl->getParentEnum()->mapTypeIntoContext(
2463+
decl->getArgumentInterfaceType())
2464+
->getCanonicalType();
24642465
types.push_back(FieldTypeInfo(caseType, false, false));
24652466
return getFieldTypeAccessorFn(IGM, type, types);
24662467
}
24672468

24682469
for (auto &elt : enumElements) {
2469-
assert(elt.decl->hasArgumentType() && "enum case doesn't have arg?!");
2470-
auto caseType = elt.decl->getArgumentType()->getCanonicalType();
2470+
auto caseType = elt.decl->getParentEnum()->mapTypeIntoContext(
2471+
elt.decl->getArgumentInterfaceType())
2472+
->getCanonicalType();
24712473
bool isIndirect = elt.decl->isIndirect()
24722474
|| elt.decl->getParentEnum()->isIndirect();
24732475
types.push_back(FieldTypeInfo(caseType, isIndirect, /*weak*/ false));

lib/IRGen/GenType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ namespace {
14991499
return false;
15001500

15011501
for (auto elt : decl->getAllElements()) {
1502-
if (elt->hasArgumentType() &&
1502+
if (elt->getArgumentInterfaceType() &&
15031503
!elt->isIndirect() &&
15041504
visit(elt->getArgumentInterfaceType()->getCanonicalType()))
15051505
return true;
@@ -1902,7 +1902,7 @@ SILType irgen::getSingletonAggregateFieldType(IRGenModule &IGM, SILType t,
19021902

19031903
auto theCase = allCases.begin();
19041904
if (!allCases.empty() && std::next(theCase) == allCases.end()
1905-
&& (*theCase)->hasArgumentType())
1905+
&& (*theCase)->getArgumentInterfaceType())
19061906
return t.getEnumElementType(*theCase, IGM.getSILModule());
19071907

19081908
return SILType();

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,12 +1147,13 @@ llvm::DINodeArray IRGenDebugInfo::getEnumElements(DebugTypeInfo DbgTy,
11471147
// the storage size from the enum.
11481148
ElemDbgTy = DebugTypeInfo(ED, ED->getRawType(), DbgTy.StorageType,
11491149
DbgTy.size, DbgTy.align);
1150-
else if (ElemDecl->hasArgumentType()) {
1150+
else if (auto ArgTy = ElemDecl->getArgumentInterfaceType()) {
11511151
// A discriminated union. This should really be described as a
11521152
// DW_TAG_variant_type. For now only describing the data.
1153-
auto &TI = IGM.getTypeInfoForUnlowered(ElemDecl->getArgumentType());
1153+
ArgTy = ElemDecl->getParentEnum()->mapTypeIntoContext(ArgTy);
1154+
auto &TI = IGM.getTypeInfoForUnlowered(ArgTy);
11541155
ElemDbgTy =
1155-
DebugTypeInfo::getFromTypeInfo(ED, ElemDecl->getArgumentType(), TI);
1156+
DebugTypeInfo::getFromTypeInfo(ED, ArgTy, TI);
11561157
} else {
11571158
// Discriminated union case without argument. Fallback to Int
11581159
// as the element type; there is no storage here.

lib/IRGen/IRGenSIL.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3278,9 +3278,13 @@ void IRGenSILFunction::visitDebugValueAddrInst(DebugValueAddrInst *i) {
32783278
auto RealType = SILTy.getSwiftType();
32793279
// Unwrap implicitly indirect types and types that are passed by
32803280
// reference only at the SIL level and below.
3281+
//
3282+
// FIXME: Should this check if the lowered SILType is address only
3283+
// instead? Otherwise optionals of archetypes etc will still have
3284+
// 'Unwrap' set to false.
32813285
bool Unwrap =
32823286
i->getVarInfo().Constant ||
3283-
RealType->getLValueOrInOutObjectType()->getKind() == TypeKind::Archetype;
3287+
RealType->getLValueOrInOutObjectType()->is<ArchetypeType>();
32843288
auto DbgTy = DebugTypeInfo::getLocalVariable(
32853289
CurSILFn->getDeclContext(), Decl, RealType,
32863290
getTypeInfo(SILVal->getType()), Unwrap);

0 commit comments

Comments
 (0)