Skip to content

Commit 1a4328b

Browse files
authored
Merge pull request #4603 from DougGregor/simplify-init-contexts
2 parents e013370 + 78b007f commit 1a4328b

33 files changed

+268
-330
lines changed

include/swift/AST/AST.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "swift/AST/Builtins.h"
2323
#include "swift/AST/Decl.h"
2424
#include "swift/AST/Expr.h"
25-
#include "swift/AST/ExprHandle.h"
2625
#include "swift/AST/Initializer.h"
2726
#include "swift/AST/Module.h"
2827
#include "swift/AST/ParameterList.h"

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -549,21 +549,6 @@ class ASTContext {
549549
addCleanup([&object]{ object.~T(); });
550550
}
551551

552-
/// Create a context for the initializer of a non-local variable,
553-
/// like a global or a field. To reduce memory usage, if the
554-
/// context goes unused, it should be returned to the ASTContext
555-
/// with destroyPatternBindingContext.
556-
PatternBindingInitializer *createPatternBindingContext(DeclContext *parent);
557-
void destroyPatternBindingContext(PatternBindingInitializer *DC);
558-
559-
/// Create a context for the initializer of the nth default argument
560-
/// of the given function. To reduce memory usage, if the context
561-
/// goes unused, it should be returned to the ASTContext with
562-
/// destroyDefaultArgumentContext.
563-
DefaultArgumentInitializer *createDefaultArgumentContext(DeclContext *fn,
564-
unsigned index);
565-
void destroyDefaultArgumentContext(DefaultArgumentInitializer *DC);
566-
567552
//===--------------------------------------------------------------------===//
568553
// Diagnostics Helper functions
569554
//===--------------------------------------------------------------------===//

include/swift/AST/Decl.h

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "swift/AST/ClangNode.h"
2222
#include "swift/AST/ConcreteDeclRef.h"
2323
#include "swift/AST/DefaultArgumentKind.h"
24-
#include "swift/AST/ExprHandle.h"
2524
#include "swift/AST/GenericSignature.h"
2625
#include "swift/AST/LazyResolver.h"
2726
#include "swift/AST/TypeAlignments.h"
@@ -246,8 +245,11 @@ class alignas(1 << DeclAlignInBits) Decl {
246245

247246
/// \brief Whether 'static' or 'class' was used.
248247
unsigned StaticSpelling : 2;
248+
249+
/// \brief The number of pattern binding declarations.
250+
unsigned NumPatternEntries : 16;
249251
};
250-
enum { NumPatternBindingDeclBits = NumDeclBits + 3 };
252+
enum { NumPatternBindingDeclBits = NumDeclBits + 19 };
251253
static_assert(NumPatternBindingDeclBits <= 32, "fits in an unsigned");
252254

253255
class ValueDeclBitfields {
@@ -1668,9 +1670,14 @@ class PatternBindingEntry {
16681670
// initializer is ASTContext-allocated it is safe.
16691671
llvm::PointerIntPair<Expr *, 2, OptionSet<Flags>> InitCheckedAndRemoved;
16701672

1673+
/// The initializer context used for this pattern binding entry.
1674+
DeclContext *InitContext = nullptr;
1675+
1676+
friend class PatternBindingInitializer;
1677+
16711678
public:
1672-
PatternBindingEntry(Pattern *P, Expr *E)
1673-
: ThePattern(P), InitCheckedAndRemoved(E, {}) {}
1679+
PatternBindingEntry(Pattern *P, Expr *E, DeclContext *InitContext)
1680+
: ThePattern(P), InitCheckedAndRemoved(E, {}), InitContext(InitContext) {}
16741681

16751682
Pattern *getPattern() const { return ThePattern; }
16761683
void setPattern(Pattern *P) { ThePattern = P; }
@@ -1690,6 +1697,12 @@ class PatternBindingEntry {
16901697

16911698
// Return the first variable initialized by this pattern.
16921699
VarDecl *getAnchoringVarDecl() const;
1700+
1701+
// Retrieve the declaration context for the intializer.
1702+
DeclContext *getInitContext() const { return InitContext; }
1703+
1704+
/// Override the initializer context.
1705+
void setInitContext(DeclContext *dc) { InitContext = dc; }
16931706
};
16941707

16951708
/// \brief This decl contains a pattern and optional initializer for a set
@@ -1711,8 +1724,6 @@ class PatternBindingDecl final : public Decl,
17111724
SourceLoc StaticLoc; ///< Location of the 'static/class' keyword, if present.
17121725
SourceLoc VarLoc; ///< Location of the 'var' keyword.
17131726

1714-
unsigned numPatternEntries;
1715-
17161727
friend class Decl;
17171728

17181729
PatternBindingDecl(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
@@ -1732,13 +1743,22 @@ class PatternBindingDecl final : public Decl,
17321743
Pattern *Pat, Expr *E,
17331744
DeclContext *Parent);
17341745

1746+
static PatternBindingDecl *createDeserialized(
1747+
ASTContext &Ctx, SourceLoc StaticLoc,
1748+
StaticSpellingKind StaticSpelling,
1749+
SourceLoc VarLoc,
1750+
unsigned NumPatternEntries,
1751+
DeclContext *Parent);
1752+
17351753
SourceLoc getStartLoc() const {
17361754
return StaticLoc.isValid() ? StaticLoc : VarLoc;
17371755
}
17381756
SourceLoc getLoc() const { return VarLoc; }
17391757
SourceRange getSourceRange() const;
17401758

1741-
unsigned getNumPatternEntries() const { return numPatternEntries; }
1759+
unsigned getNumPatternEntries() const {
1760+
return PatternBindingDeclBits.NumPatternEntries;
1761+
}
17421762

17431763
ArrayRef<PatternBindingEntry> getPatternList() const {
17441764
return const_cast<PatternBindingDecl*>(this)->getMutablePatternList();
@@ -1760,7 +1780,7 @@ class PatternBindingDecl final : public Decl,
17601780
return getPatternList()[i].getPattern();
17611781
}
17621782

1763-
void setPattern(unsigned i, Pattern *Pat);
1783+
void setPattern(unsigned i, Pattern *Pat, DeclContext *InitContext);
17641784

17651785
/// Given that this PBD is the parent pattern for the specified VarDecl,
17661786
/// return the entry of the VarDecl in our PatternList. For example, in:
@@ -1810,7 +1830,7 @@ class PatternBindingDecl final : public Decl,
18101830
private:
18111831
MutableArrayRef<PatternBindingEntry> getMutablePatternList() {
18121832
// Pattern entries are tail allocated.
1813-
return {getTrailingObjects<PatternBindingEntry>(), numPatternEntries};
1833+
return {getTrailingObjects<PatternBindingEntry>(), getNumPatternEntries()};
18141834
}
18151835
};
18161836

@@ -4255,8 +4275,13 @@ class ParamDecl : public VarDecl {
42554275
SourceLoc ArgumentNameLoc;
42564276
SourceLoc LetVarInOutLoc;
42574277

4278+
struct StoredDefaultArgument {
4279+
Expr *DefaultArg = nullptr;
4280+
Initializer *InitContext = nullptr;
4281+
};
4282+
42584283
/// The default value, if any, along with whether this is varargs.
4259-
llvm::PointerIntPair<ExprHandle *, 1, bool> DefaultValueAndIsVariadic;
4284+
llvm::PointerIntPair<StoredDefaultArgument *, 1> DefaultValueAndIsVariadic;
42604285

42614286
/// True if the type is implicitly specified in the source, but this has an
42624287
/// apparently valid typeRepr. This is used in accessors, which look like:
@@ -4302,13 +4327,22 @@ class ParamDecl : public VarDecl {
43024327
defaultArgumentKind = K;
43034328
}
43044329

4305-
void setDefaultValue(ExprHandle *H) {
4306-
DefaultValueAndIsVariadic.setPointer(H);
4330+
Expr *getDefaultValue() const {
4331+
if (auto stored = DefaultValueAndIsVariadic.getPointer())
4332+
return stored->DefaultArg;
4333+
return nullptr;
43074334
}
4308-
ExprHandle *getDefaultValue() const {
4309-
return DefaultValueAndIsVariadic.getPointer();
4335+
4336+
void setDefaultValue(Expr *E);
4337+
4338+
Initializer *getDefaultArgumentInitContext() const {
4339+
if (auto stored = DefaultValueAndIsVariadic.getPointer())
4340+
return stored->InitContext;
4341+
return nullptr;
43104342
}
43114343

4344+
void setDefaultArgumentInitContext(Initializer *initContext);
4345+
43124346
/// Whether or not this parameter is varargs.
43134347
bool isVariadic() const { return DefaultValueAndIsVariadic.getInt(); }
43144348
void setVariadic(bool value = true) {DefaultValueAndIsVariadic.setInt(value);}

include/swift/AST/DeclContext.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,16 +494,19 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
494494
/// deserialization.
495495
class SerializedLocalDeclContext : public DeclContext {
496496
private:
497-
const LocalDeclContextKind LocalKind;
497+
unsigned LocalKind : 3;
498+
499+
protected:
500+
unsigned SpareBits : 29;
498501

499502
public:
500503
SerializedLocalDeclContext(LocalDeclContextKind LocalKind,
501504
DeclContext *Parent)
502505
: DeclContext(DeclContextKind::SerializedLocal, Parent),
503-
LocalKind(LocalKind) {}
506+
LocalKind(static_cast<unsigned>(LocalKind)) {}
504507

505508
LocalDeclContextKind getLocalDeclContextKind() const {
506-
return LocalKind;
509+
return static_cast<LocalDeclContextKind>(LocalKind);
507510
}
508511

509512
static bool classof(const DeclContext *DC) {

include/swift/AST/Expr.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,6 @@ class alignas(8) Expr {
539539
/// walk into the body of it (unless it is single-expression).
540540
void forEachChildExpr(const std::function<Expr*(Expr*)> &callback);
541541

542-
/// findExistingInitializerContext - Given that this expression is
543-
/// an initializer that belongs in some sort of Initializer
544-
/// context, look through it for any existing context object.
545-
Initializer *findExistingInitializerContext();
546-
547542
/// Determine whether this expression refers to a type by name.
548543
///
549544
/// This distinguishes static references to types, like Int, from metatype

include/swift/AST/ExprHandle.h

Lines changed: 0 additions & 63 deletions
This file was deleted.

include/swift/AST/Initializer.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,20 @@ class PatternBindingInitializer : public Initializer {
8080
explicit PatternBindingInitializer(DeclContext *parent)
8181
: Initializer(InitializerKind::PatternBinding, parent),
8282
Binding(nullptr) {
83+
SpareBits = 0;
8384
}
8485

8586

86-
void setBinding(PatternBindingDecl *binding) {
87+
void setBinding(PatternBindingDecl *binding, unsigned bindingIndex) {
8788
setParent(binding->getDeclContext());
8889
Binding = binding;
90+
SpareBits = bindingIndex;
8991
}
9092

9193
PatternBindingDecl *getBinding() const { return Binding; }
9294

95+
unsigned getBindingIndex() const { return SpareBits; }
96+
9397
static bool classof(const DeclContext *DC) {
9498
if (auto init = dyn_cast<Initializer>(DC))
9599
return classof(init);
@@ -108,15 +112,21 @@ class SerializedPatternBindingInitializer : public SerializedLocalDeclContext {
108112
PatternBindingDecl *Binding;
109113

110114
public:
111-
SerializedPatternBindingInitializer(PatternBindingDecl *Binding)
115+
SerializedPatternBindingInitializer(PatternBindingDecl *Binding,
116+
unsigned bindingIndex)
112117
: SerializedLocalDeclContext(LocalDeclContextKind::PatternBindingInitializer,
113118
Binding->getDeclContext()),
114-
Binding(Binding) {}
119+
Binding(Binding) {
120+
SpareBits = bindingIndex;
121+
}
115122

116123
PatternBindingDecl *getBinding() const {
117124
return Binding;
118125
}
119126

127+
unsigned getBindingIndex() const { return SpareBits; }
128+
129+
120130
static bool classof(const DeclContext *DC) {
121131
if (auto LDC = dyn_cast<SerializedLocalDeclContext>(DC))
122132
return LDC->getLocalDeclContextKind() ==
@@ -145,11 +155,8 @@ class DefaultArgumentInitializer : public Initializer {
145155
/// Change the parent of this context. This is necessary because
146156
/// the function signature is parsed before the function
147157
/// declaration/expression itself is built.
148-
void changeFunction(DeclContext *parent) {
149-
assert(parent->isLocalContext());
150-
setParent(parent);
151-
}
152-
158+
void changeFunction(AbstractFunctionDecl *parent);
159+
153160
static bool classof(const DeclContext *DC) {
154161
if (auto init = dyn_cast<Initializer>(DC))
155162
return classof(init);

include/swift/AST/TypeRepr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ namespace swift {
3232
class DeclContext;
3333
class IdentTypeRepr;
3434
class ValueDecl;
35-
class ExprHandle;
3635
class NamedTypeRepr;
3736

3837
enum class TypeReprKind : uint8_t {

include/swift/AST/Types.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ namespace swift {
4242
class AssociatedTypeDecl;
4343
class ASTContext;
4444
class ClassDecl;
45-
class ExprHandle;
4645
class GenericTypeParamDecl;
4746
class GenericTypeParamType;
4847
class GenericParamList;

include/swift/Parse/Parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ class Parser {
924924

925925
/// Set the parsed context for all the initializers to the given
926926
/// function.
927-
void setFunctionContext(DeclContext *DC);
927+
void setFunctionContext(AbstractFunctionDecl *AFD);
928928

929929
DefaultArgumentInfo(bool inTypeContext) {
930930
NextIndex = inTypeContext ? 1 : 0;
@@ -970,7 +970,7 @@ class Parser {
970970
TypeRepr *Type = nullptr;
971971

972972
/// The default argument for this parameter.
973-
ExprHandle *DefaultArg = nullptr;
973+
Expr *DefaultArg = nullptr;
974974

975975
/// True if we emitted a parse error about this parameter.
976976
bool isInvalid = false;

include/swift/Serialization/ModuleFormat.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const uint16_t VERSION_MAJOR = 0;
5353
/// in source control, you should also update the comment to briefly
5454
/// describe what change you made. The content of this comment isn't important;
5555
/// it just ensures a conflict if two people change the module format.
56-
const uint16_t VERSION_MINOR = 264; // Last change: remove AllArchetypes
56+
const uint16_t VERSION_MINOR = 266; // Last change: pattern binding init contexts
5757

5858
using DeclID = PointerEmbeddedInt<unsigned, 31>;
5959
using DeclIDField = BCFixed<31>;
@@ -927,9 +927,9 @@ namespace decls_block {
927927
BCFixed<1>, // implicit flag
928928
BCFixed<1>, // static?
929929
StaticSpellingKindField, // spelling of 'static' or 'class'
930-
BCVBR<3> // numpatterns
931-
932-
// The pattern trails the record.
930+
BCVBR<3>, // numpatterns
931+
BCArray<DeclContextIDField> // init contexts
932+
// The patterns and decl-contexts trail the record.
933933
>;
934934

935935
template <unsigned Code>
@@ -1306,7 +1306,8 @@ namespace decls_block {
13061306

13071307
using PatternBindingInitializerLayout = BCRecordLayout<
13081308
PATTERN_BINDING_INITIALIZER_CONTEXT,
1309-
DeclIDField // parent pattern binding decl
1309+
DeclIDField, // parent pattern binding decl
1310+
BCVBR<3> // binding index in the pattern binding decl
13101311
>;
13111312

13121313
using DefaultArgumentInitializerLayout = BCRecordLayout<

0 commit comments

Comments
 (0)