Skip to content

Commit d6dad24

Browse files
authored
Merge pull request #17682 from DougGregor/factor-objc-override
[AST] Centralize storage of the overridden declarations of a declaration
2 parents f230abf + 764f148 commit d6dad24

File tree

15 files changed

+258
-267
lines changed

15 files changed

+258
-267
lines changed

include/swift/AST/Decl.h

Lines changed: 63 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "swift/AST/TypeAlignments.h"
3232
#include "swift/AST/TypeWalker.h"
3333
#include "swift/AST/Witness.h"
34+
#include "swift/Basic/ArrayRefView.h"
3435
#include "swift/Basic/Compiler.h"
3536
#include "swift/Basic/InlineBitfield.h"
3637
#include "swift/Basic/OptionalEnum.h"
@@ -307,7 +308,7 @@ class alignas(1 << DeclAlignInBits) Decl {
307308
NumElements : 32
308309
);
309310

310-
SWIFT_INLINE_BITFIELD(ValueDecl, Decl, 1+1+1+1+1,
311+
SWIFT_INLINE_BITFIELD(ValueDecl, Decl, 1+1+1,
311312
AlreadyInLookupTable : 1,
312313

313314
/// Whether we have already checked whether this declaration is a
@@ -316,19 +317,10 @@ class alignas(1 << DeclAlignInBits) Decl {
316317

317318
/// Whether the decl can be accessed by swift users; for instance,
318319
/// a.storage for lazy var a is a decl that cannot be accessed.
319-
IsUserAccessible : 1,
320-
321-
/// Whether the "IsObjC" bit has been computed yet.
322-
IsObjCComputed : 1,
323-
324-
/// Whether this declaration is exposed to Objective-C.
325-
IsObjC : 1
320+
IsUserAccessible : 1
326321
);
327322

328-
SWIFT_INLINE_BITFIELD(AbstractStorageDecl, ValueDecl, 1+1+1+1+1,
329-
/// Whether we are overridden later
330-
Overridden : 1,
331-
323+
SWIFT_INLINE_BITFIELD(AbstractStorageDecl, ValueDecl, 1+1+1+1,
332324
/// Whether the getter is mutating.
333325
IsGetterMutating : 1,
334326

@@ -588,11 +580,6 @@ class alignas(1 << DeclAlignInBits) Decl {
588580
Associativity : 2
589581
);
590582

591-
SWIFT_INLINE_BITFIELD(AssociatedTypeDecl, ValueDecl, 1+1,
592-
ComputedOverridden : 1,
593-
HasOverridden : 1
594-
);
595-
596583
SWIFT_INLINE_BITFIELD(ImportDecl, Decl, 3+8,
597584
ImportKind : 3,
598585

@@ -2181,6 +2168,21 @@ class ValueDecl : public Decl {
21812168
llvm::PointerIntPair<Type, 3, OptionalEnum<AccessLevel>> TypeAndAccess;
21822169
unsigned LocalDiscriminator = 0;
21832170

2171+
struct {
2172+
/// Whether the "IsObjC" bit has been computed yet.
2173+
unsigned isObjCComputed : 1;
2174+
2175+
/// Whether this declaration is exposed to Objective-C.
2176+
unsigned isObjC : 1;
2177+
2178+
/// Whether the "overridden" declarations have been computed already.
2179+
unsigned hasOverriddenComputed : 1;
2180+
2181+
/// Whether there are any "overridden" declarations. The actual overridden
2182+
/// declarations are kept in a side table in the ASTContext.
2183+
unsigned hasOverridden : 1;
2184+
} LazySemanticInfo;
2185+
21842186
protected:
21852187
ValueDecl(DeclKind K,
21862188
llvm::PointerUnion<DeclContext *, ASTContext *> context,
@@ -2189,8 +2191,10 @@ class ValueDecl : public Decl {
21892191
Bits.ValueDecl.AlreadyInLookupTable = false;
21902192
Bits.ValueDecl.CheckedRedeclaration = false;
21912193
Bits.ValueDecl.IsUserAccessible = true;
2192-
Bits.ValueDecl.IsObjCComputed = false;
2193-
Bits.ValueDecl.IsObjC = false;
2194+
LazySemanticInfo.isObjCComputed = false;
2195+
LazySemanticInfo.isObjC = false;
2196+
LazySemanticInfo.hasOverriddenComputed = false;
2197+
LazySemanticInfo.hasOverridden = false;
21942198
}
21952199

21962200
// MemberLookupTable borrows a bit from this type
@@ -2414,6 +2418,23 @@ class ValueDecl : public Decl {
24142418
/// Retrieve the declaration that this declaration overrides, if any.
24152419
ValueDecl *getOverriddenDecl() const;
24162420

2421+
/// Retrieve the declarations that this declaration overrides, if any.
2422+
ArrayRef<ValueDecl *> getOverriddenDecls() const;
2423+
2424+
/// Set the declaration that this declaration overrides.
2425+
void setOverriddenDecl(ValueDecl *overridden) {
2426+
(void)setOverriddenDecls(overridden);
2427+
}
2428+
2429+
/// Set the declarations that this declaration overrides.
2430+
///
2431+
/// \returns the ASTContext-allocated version of the array of overridden
2432+
/// declarations.
2433+
ArrayRef<ValueDecl *> setOverriddenDecls(ArrayRef<ValueDecl *> overridden);
2434+
2435+
/// Whether the overridden declarations have already been computed.
2436+
bool overriddenDeclsComputed() const;
2437+
24172438
/// Compute the untyped overload signature for this declaration.
24182439
OverloadSignature getOverloadSignature() const;
24192440

@@ -2807,23 +2828,19 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
28072828
AssociatedTypeDecl *getAssociatedTypeAnchor() const;
28082829

28092830
/// Retrieve the (first) overridden associated type declaration, if any.
2810-
AssociatedTypeDecl *getOverriddenDecl() const;
2831+
AssociatedTypeDecl *getOverriddenDecl() const {
2832+
return cast_or_null<AssociatedTypeDecl>(
2833+
AbstractTypeParamDecl::getOverriddenDecl());
2834+
}
28112835

28122836
/// Retrieve the set of associated types overridden by this associated
28132837
/// type.
2814-
ArrayRef<AssociatedTypeDecl *> getOverriddenDecls() const;
2815-
2816-
/// Whether the overridden declarations have already been computed.
2817-
bool overriddenDeclsComputed() const {
2818-
return Bits.AssociatedTypeDecl.ComputedOverridden;
2838+
CastArrayRefView<ValueDecl *, AssociatedTypeDecl>
2839+
getOverriddenDecls() const {
2840+
return CastArrayRefView<ValueDecl *, AssociatedTypeDecl>(
2841+
AbstractTypeParamDecl::getOverriddenDecls());
28192842
}
28202843

2821-
/// Record the set of overridden declarations.
2822-
///
2823-
/// \returns the array recorded in the AST.
2824-
ArrayRef<AssociatedTypeDecl *> setOverriddenDecls(
2825-
ArrayRef<AssociatedTypeDecl *> overridden);
2826-
28272844
SourceLoc getStartLoc() const { return KeywordLoc; }
28282845
SourceRange getSourceRange() const;
28292846

@@ -4020,8 +4037,6 @@ class AbstractStorageDecl : public ValueDecl {
40204037
public:
40214038
static const size_t MaxNumAccessors = 255;
40224039
private:
4023-
AbstractStorageDecl *OverriddenDecl;
4024-
40254040
/// A record of the accessors for the declaration.
40264041
class alignas(1 << 3) AccessorRecord final :
40274042
private llvm::TrailingObjects<AccessorRecord, AccessorDecl*> {
@@ -4092,12 +4107,11 @@ class AbstractStorageDecl : public ValueDecl {
40924107
protected:
40934108
AbstractStorageDecl(DeclKind Kind, DeclContext *DC, DeclName Name,
40944109
SourceLoc NameLoc, bool supportsMutation)
4095-
: ValueDecl(Kind, DC, Name, NameLoc), OverriddenDecl(nullptr) {
4110+
: ValueDecl(Kind, DC, Name, NameLoc) {
40964111
Bits.AbstractStorageDecl.HasStorage = true;
40974112
Bits.AbstractStorageDecl.SupportsMutation = supportsMutation;
40984113
Bits.AbstractStorageDecl.IsGetterMutating = false;
40994114
Bits.AbstractStorageDecl.IsSetterMutating = true;
4100-
Bits.AbstractStorageDecl.Overridden = false;
41014115
}
41024116

41034117
void setSupportsMutationIfStillStored(bool supportsMutation) {
@@ -4292,28 +4306,9 @@ class AbstractStorageDecl : public ValueDecl {
42924306
getObjCSetterSelector(Identifier preferredName = Identifier()) const;
42934307

42944308
AbstractStorageDecl *getOverriddenDecl() const {
4295-
return OverriddenDecl;
4296-
}
4297-
void setOverriddenDecl(AbstractStorageDecl *over) {
4298-
// FIXME: Hack due to broken class circularity checking.
4299-
if (over == this) return;
4300-
OverriddenDecl = over;
4301-
over->setIsOverridden();
4302-
}
4303-
4304-
/// The declaration has been overridden in the module
4305-
///
4306-
/// Resolved during type checking
4307-
void setIsOverridden() {
4308-
Bits.AbstractStorageDecl.Overridden = true;
4309+
return cast_or_null<AbstractStorageDecl>(ValueDecl::getOverriddenDecl());
43094310
}
43104311

4311-
/// Whether the declaration is later overridden in the module
4312-
///
4313-
/// Overrides are resolved during type checking; only query this field after
4314-
/// the whole module has been checked
4315-
bool isOverridden() const { return Bits.AbstractStorageDecl.Overridden; }
4316-
43174312
/// Returns the location of 'override' keyword, if any.
43184313
SourceLoc getOverrideLoc() const;
43194314

@@ -5171,7 +5166,9 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
51715166
ParamDecl *getImplicitSelfDecl();
51725167

51735168
/// Retrieve the declaration that this method overrides, if any.
5174-
AbstractFunctionDecl *getOverriddenDecl() const;
5169+
AbstractFunctionDecl *getOverriddenDecl() const {
5170+
return cast_or_null<AbstractFunctionDecl>(ValueDecl::getOverriddenDecl());
5171+
}
51755172

51765173
/// Returns true if a function declaration overrides a given
51775174
/// method from its direct or indirect superclass.
@@ -5259,9 +5256,8 @@ class FuncDecl : public AbstractFunctionDecl {
52595256

52605257
/// \brief If this FuncDecl is an accessor for a property, this indicates
52615258
/// which property and what kind of accessor.
5262-
llvm::PointerUnion<FuncDecl *, BehaviorRecord *>
5263-
OverriddenOrBehaviorParamDecl;
5264-
OperatorDecl *Operator;
5259+
BehaviorRecord *BehaviorParamDecl = nullptr;
5260+
OperatorDecl *Operator = nullptr;
52655261

52665262
protected:
52675263
ParameterList **getParameterListBuffer(); // defined inline below
@@ -5280,9 +5276,7 @@ class FuncDecl : public AbstractFunctionDecl {
52805276
Name, NameLoc,
52815277
Throws, ThrowsLoc,
52825278
NumParameterLists, GenericParams),
5283-
StaticLoc(StaticLoc), FuncLoc(FuncLoc),
5284-
OverriddenOrBehaviorParamDecl(),
5285-
Operator(nullptr) {
5279+
StaticLoc(StaticLoc), FuncLoc(FuncLoc) {
52865280
assert(!Name.getBaseName().isSpecial());
52875281

52885282
Bits.FuncDecl.IsStatic =
@@ -5440,34 +5434,17 @@ class FuncDecl : public AbstractFunctionDecl {
54405434

54415435
/// Get the supertype method this method overrides, if any.
54425436
FuncDecl *getOverriddenDecl() const {
5443-
return OverriddenOrBehaviorParamDecl.dyn_cast<FuncDecl *>();
5437+
return cast_or_null<FuncDecl>(AbstractFunctionDecl::getOverriddenDecl());
54445438
}
5445-
void setOverriddenDecl(FuncDecl *over) {
5446-
// FIXME: Hack due to broken class circularity checking.
5447-
if (over == this) return;
54485439

5449-
// A function cannot be an override if it is also a derived global decl
5450-
// (since derived decls are at global scope).
5451-
assert((!OverriddenOrBehaviorParamDecl
5452-
|| OverriddenOrBehaviorParamDecl.get<FuncDecl*>() == over)
5453-
&& "function can only be one of override, derived, or behavior param");
5454-
OverriddenOrBehaviorParamDecl = over;
5455-
over->setIsOverridden();
5456-
}
5457-
54585440
/// Get the property behavior this function serves as a parameter for, if
54595441
/// any.
54605442
BehaviorRecord *getParamBehavior() const {
5461-
return OverriddenOrBehaviorParamDecl
5462-
.dyn_cast<BehaviorRecord *>();
5443+
return BehaviorParamDecl;
54635444
}
54645445

54655446
void setParamBehavior(BehaviorRecord *behavior) {
5466-
// Behavior param blocks cannot be overrides or derived.
5467-
assert((!OverriddenOrBehaviorParamDecl
5468-
|| OverriddenOrBehaviorParamDecl.is<BehaviorRecord *>())
5469-
&& "function can only be one of override, derived, or behavior param");
5470-
OverriddenOrBehaviorParamDecl = behavior;
5447+
BehaviorParamDecl = behavior;
54715448
}
54725449

54735450
OperatorDecl *getOperatorDecl() const {
@@ -5861,10 +5838,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
58615838
/// inserted at the end of the initializer by SILGen.
58625839
Expr *CallToSuperInit = nullptr;
58635840

5864-
/// The constructor this overrides, which only makes sense when
5865-
/// both the overriding and the overridden constructors are abstract.
5866-
ConstructorDecl *OverriddenDecl = nullptr;
5867-
58685841
public:
58695842
ConstructorDecl(DeclName Name, SourceLoc ConstructorLoc,
58705843
OptionalTypeKind Failability, SourceLoc FailabilityLoc,
@@ -6017,13 +5990,9 @@ class ConstructorDecl : public AbstractFunctionDecl {
60175990
Bits.ConstructorDecl.HasStubImplementation = stub;
60185991
}
60195992

6020-
ConstructorDecl *getOverriddenDecl() const { return OverriddenDecl; }
6021-
void setOverriddenDecl(ConstructorDecl *over) {
6022-
// FIXME: Hack due to broken class circularity checking.
6023-
if (over == this) return;
6024-
6025-
OverriddenDecl = over;
6026-
over->setIsOverridden();
5993+
ConstructorDecl *getOverriddenDecl() const {
5994+
return cast_or_null<ConstructorDecl>(
5995+
AbstractFunctionDecl::getOverriddenDecl());
60275996
}
60285997

60295998
/// Determine whether this initializer falls into the special case for
@@ -6650,7 +6619,7 @@ std::pair<DefaultArgumentKind, Type>
66506619
getDefaultArgumentInfo(ValueDecl *source, unsigned Index);
66516620

66526621
/// Display ValueDecl subclasses.
6653-
void simple_display(llvm::raw_ostream &out, const ValueDecl *&decl);
6622+
void simple_display(llvm::raw_ostream &out, const ValueDecl *decl);
66546623

66556624
} // end namespace swift
66566625

include/swift/AST/Module.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ enum class FileUnitKind {
9494
SerializedAST,
9595
/// An imported Clang module.
9696
ClangModule,
97-
/// A derived declaration.
98-
Derived,
9997
};
10098

10199
enum class SourceFileKind {

include/swift/Basic/ArrayRefView.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SWIFT_BASIC_ARRAYREFVIEW_H
2020

2121
#include "llvm/ADT/ArrayRef.h"
22+
#include "llvm/Support/Casting.h"
2223

2324
namespace swift {
2425

@@ -152,6 +153,20 @@ class ArrayRefView {
152153
}
153154
};
154155

156+
/// Helper for \c CastArrayRefView that casts the original type to the
157+
/// projected type.
158+
template<typename Projected, typename Orig>
159+
inline Projected *arrayRefViewCastHelper(const Orig &value) {
160+
using llvm::cast_or_null;
161+
return cast_or_null<Projected>(value);
162+
}
163+
164+
/// An ArrayRefView that performs a cast_or_null on each element in the
165+
/// underlying ArrayRef.
166+
template<typename Orig, typename Projected>
167+
using CastArrayRefView =
168+
ArrayRefView<Orig, Projected *, arrayRefViewCastHelper<Projected, Orig>>;
169+
155170
} // end namespace swift
156171

157172
#endif // SWIFT_BASIC_ARRAYREFVIEW_H

0 commit comments

Comments
 (0)