Skip to content

AST cleanup: simplify initialization contexts #4603

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 4 commits into from
Sep 2, 2016
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
1 change: 0 additions & 1 deletion include/swift/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "swift/AST/Builtins.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/ExprHandle.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParameterList.h"
Expand Down
15 changes: 0 additions & 15 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,21 +549,6 @@ class ASTContext {
addCleanup([&object]{ object.~T(); });
}

/// Create a context for the initializer of a non-local variable,
/// like a global or a field. To reduce memory usage, if the
/// context goes unused, it should be returned to the ASTContext
/// with destroyPatternBindingContext.
PatternBindingInitializer *createPatternBindingContext(DeclContext *parent);
void destroyPatternBindingContext(PatternBindingInitializer *DC);

/// Create a context for the initializer of the nth default argument
/// of the given function. To reduce memory usage, if the context
/// goes unused, it should be returned to the ASTContext with
/// destroyDefaultArgumentContext.
DefaultArgumentInitializer *createDefaultArgumentContext(DeclContext *fn,
unsigned index);
void destroyDefaultArgumentContext(DefaultArgumentInitializer *DC);

//===--------------------------------------------------------------------===//
// Diagnostics Helper functions
//===--------------------------------------------------------------------===//
Expand Down
62 changes: 48 additions & 14 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "swift/AST/ClangNode.h"
#include "swift/AST/ConcreteDeclRef.h"
#include "swift/AST/DefaultArgumentKind.h"
#include "swift/AST/ExprHandle.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/LazyResolver.h"
#include "swift/AST/TypeAlignments.h"
Expand Down Expand Up @@ -246,8 +245,11 @@ class alignas(1 << DeclAlignInBits) Decl {

/// \brief Whether 'static' or 'class' was used.
unsigned StaticSpelling : 2;

/// \brief The number of pattern binding declarations.
unsigned NumPatternEntries : 16;
};
enum { NumPatternBindingDeclBits = NumDeclBits + 3 };
enum { NumPatternBindingDeclBits = NumDeclBits + 19 };
static_assert(NumPatternBindingDeclBits <= 32, "fits in an unsigned");

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

/// The initializer context used for this pattern binding entry.
DeclContext *InitContext = nullptr;

friend class PatternBindingInitializer;

public:
PatternBindingEntry(Pattern *P, Expr *E)
: ThePattern(P), InitCheckedAndRemoved(E, {}) {}
PatternBindingEntry(Pattern *P, Expr *E, DeclContext *InitContext)
: ThePattern(P), InitCheckedAndRemoved(E, {}), InitContext(InitContext) {}

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

// Return the first variable initialized by this pattern.
VarDecl *getAnchoringVarDecl() const;

// Retrieve the declaration context for the intializer.
DeclContext *getInitContext() const { return InitContext; }

/// Override the initializer context.
void setInitContext(DeclContext *dc) { InitContext = dc; }
};

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

unsigned numPatternEntries;

friend class Decl;

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

static PatternBindingDecl *createDeserialized(
ASTContext &Ctx, SourceLoc StaticLoc,
StaticSpellingKind StaticSpelling,
SourceLoc VarLoc,
unsigned NumPatternEntries,
DeclContext *Parent);

SourceLoc getStartLoc() const {
return StaticLoc.isValid() ? StaticLoc : VarLoc;
}
SourceLoc getLoc() const { return VarLoc; }
SourceRange getSourceRange() const;

unsigned getNumPatternEntries() const { return numPatternEntries; }
unsigned getNumPatternEntries() const {
return PatternBindingDeclBits.NumPatternEntries;
}

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

void setPattern(unsigned i, Pattern *Pat);
void setPattern(unsigned i, Pattern *Pat, DeclContext *InitContext);

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

Expand Down Expand Up @@ -4255,8 +4275,13 @@ class ParamDecl : public VarDecl {
SourceLoc ArgumentNameLoc;
SourceLoc LetVarInOutLoc;

struct StoredDefaultArgument {
Expr *DefaultArg = nullptr;
Initializer *InitContext = nullptr;
};

/// The default value, if any, along with whether this is varargs.
llvm::PointerIntPair<ExprHandle *, 1, bool> DefaultValueAndIsVariadic;
llvm::PointerIntPair<StoredDefaultArgument *, 1> DefaultValueAndIsVariadic;

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

void setDefaultValue(ExprHandle *H) {
DefaultValueAndIsVariadic.setPointer(H);
Expr *getDefaultValue() const {
if (auto stored = DefaultValueAndIsVariadic.getPointer())
return stored->DefaultArg;
return nullptr;
}
ExprHandle *getDefaultValue() const {
return DefaultValueAndIsVariadic.getPointer();

void setDefaultValue(Expr *E);

Initializer *getDefaultArgumentInitContext() const {
if (auto stored = DefaultValueAndIsVariadic.getPointer())
return stored->InitContext;
return nullptr;
}

void setDefaultArgumentInitContext(Initializer *initContext);

/// Whether or not this parameter is varargs.
bool isVariadic() const { return DefaultValueAndIsVariadic.getInt(); }
void setVariadic(bool value = true) {DefaultValueAndIsVariadic.setInt(value);}
Expand Down
9 changes: 6 additions & 3 deletions include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,19 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
/// deserialization.
class SerializedLocalDeclContext : public DeclContext {
private:
const LocalDeclContextKind LocalKind;
unsigned LocalKind : 3;

protected:
unsigned SpareBits : 29;

public:
SerializedLocalDeclContext(LocalDeclContextKind LocalKind,
DeclContext *Parent)
: DeclContext(DeclContextKind::SerializedLocal, Parent),
LocalKind(LocalKind) {}
LocalKind(static_cast<unsigned>(LocalKind)) {}

LocalDeclContextKind getLocalDeclContextKind() const {
return LocalKind;
return static_cast<LocalDeclContextKind>(LocalKind);
}

static bool classof(const DeclContext *DC) {
Expand Down
5 changes: 0 additions & 5 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,6 @@ class alignas(8) Expr {
/// walk into the body of it (unless it is single-expression).
void forEachChildExpr(const std::function<Expr*(Expr*)> &callback);

/// findExistingInitializerContext - Given that this expression is
/// an initializer that belongs in some sort of Initializer
/// context, look through it for any existing context object.
Initializer *findExistingInitializerContext();

/// Determine whether this expression refers to a type by name.
///
/// This distinguishes static references to types, like Int, from metatype
Expand Down
63 changes: 0 additions & 63 deletions include/swift/AST/ExprHandle.h

This file was deleted.

23 changes: 15 additions & 8 deletions include/swift/AST/Initializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,20 @@ class PatternBindingInitializer : public Initializer {
explicit PatternBindingInitializer(DeclContext *parent)
: Initializer(InitializerKind::PatternBinding, parent),
Binding(nullptr) {
SpareBits = 0;
}


void setBinding(PatternBindingDecl *binding) {
void setBinding(PatternBindingDecl *binding, unsigned bindingIndex) {
setParent(binding->getDeclContext());
Binding = binding;
SpareBits = bindingIndex;
}

PatternBindingDecl *getBinding() const { return Binding; }

unsigned getBindingIndex() const { return SpareBits; }

static bool classof(const DeclContext *DC) {
if (auto init = dyn_cast<Initializer>(DC))
return classof(init);
Expand All @@ -108,15 +112,21 @@ class SerializedPatternBindingInitializer : public SerializedLocalDeclContext {
PatternBindingDecl *Binding;

public:
SerializedPatternBindingInitializer(PatternBindingDecl *Binding)
SerializedPatternBindingInitializer(PatternBindingDecl *Binding,
unsigned bindingIndex)
: SerializedLocalDeclContext(LocalDeclContextKind::PatternBindingInitializer,
Binding->getDeclContext()),
Binding(Binding) {}
Binding(Binding) {
SpareBits = bindingIndex;
}

PatternBindingDecl *getBinding() const {
return Binding;
}

unsigned getBindingIndex() const { return SpareBits; }


static bool classof(const DeclContext *DC) {
if (auto LDC = dyn_cast<SerializedLocalDeclContext>(DC))
return LDC->getLocalDeclContextKind() ==
Expand Down Expand Up @@ -145,11 +155,8 @@ class DefaultArgumentInitializer : public Initializer {
/// Change the parent of this context. This is necessary because
/// the function signature is parsed before the function
/// declaration/expression itself is built.
void changeFunction(DeclContext *parent) {
assert(parent->isLocalContext());
setParent(parent);
}

void changeFunction(AbstractFunctionDecl *parent);

static bool classof(const DeclContext *DC) {
if (auto init = dyn_cast<Initializer>(DC))
return classof(init);
Expand Down
1 change: 0 additions & 1 deletion include/swift/AST/TypeRepr.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ namespace swift {
class DeclContext;
class IdentTypeRepr;
class ValueDecl;
class ExprHandle;
class NamedTypeRepr;

enum class TypeReprKind : uint8_t {
Expand Down
1 change: 0 additions & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ namespace swift {
class AssociatedTypeDecl;
class ASTContext;
class ClassDecl;
class ExprHandle;
class GenericTypeParamDecl;
class GenericTypeParamType;
class GenericParamList;
Expand Down
4 changes: 2 additions & 2 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ class Parser {

/// Set the parsed context for all the initializers to the given
/// function.
void setFunctionContext(DeclContext *DC);
void setFunctionContext(AbstractFunctionDecl *AFD);

DefaultArgumentInfo(bool inTypeContext) {
NextIndex = inTypeContext ? 1 : 0;
Expand Down Expand Up @@ -970,7 +970,7 @@ class Parser {
TypeRepr *Type = nullptr;

/// The default argument for this parameter.
ExprHandle *DefaultArg = nullptr;
Expr *DefaultArg = nullptr;

/// True if we emitted a parse error about this parameter.
bool isInvalid = false;
Expand Down
11 changes: 6 additions & 5 deletions include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const uint16_t VERSION_MAJOR = 0;
/// in source control, you should also update the comment to briefly
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
const uint16_t VERSION_MINOR = 264; // Last change: remove AllArchetypes
const uint16_t VERSION_MINOR = 266; // Last change: pattern binding init contexts

using DeclID = PointerEmbeddedInt<unsigned, 31>;
using DeclIDField = BCFixed<31>;
Expand Down Expand Up @@ -927,9 +927,9 @@ namespace decls_block {
BCFixed<1>, // implicit flag
BCFixed<1>, // static?
StaticSpellingKindField, // spelling of 'static' or 'class'
BCVBR<3> // numpatterns

// The pattern trails the record.
BCVBR<3>, // numpatterns
BCArray<DeclContextIDField> // init contexts
// The patterns and decl-contexts trail the record.
>;

template <unsigned Code>
Expand Down Expand Up @@ -1306,7 +1306,8 @@ namespace decls_block {

using PatternBindingInitializerLayout = BCRecordLayout<
PATTERN_BINDING_INITIALIZER_CONTEXT,
DeclIDField // parent pattern binding decl
DeclIDField, // parent pattern binding decl
BCVBR<3> // binding index in the pattern binding decl
>;

using DefaultArgumentInitializerLayout = BCRecordLayout<
Expand Down
Loading