Skip to content

Commit

Permalink
Merge pull request #1818 from swiftwasm/master
Browse files Browse the repository at this point in the history
[pull] swiftwasm from master
  • Loading branch information
pull[bot] authored Sep 24, 2020
2 parents 3fc1272 + 734b8a2 commit 82e8a18
Show file tree
Hide file tree
Showing 45 changed files with 497 additions and 422 deletions.
100 changes: 17 additions & 83 deletions include/swift/AST/ASTScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,11 @@ class ASTScopeImpl {
virtual SourceRange sourceRangeForDeferredExpansion() const;

public:
virtual NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const;

bool isATypeDeclScope() const;

private:
virtual ScopeCreator &getScopeCreator();

#pragma mark - - creation queries
public:
virtual bool isThisAnAbstractStorageDecl() const { return false; }

#pragma mark - lookup

public:
Expand Down Expand Up @@ -849,9 +842,6 @@ class GenericParamScope final : public ASTScopeImpl {
void printSpecifics(llvm::raw_ostream &out) const override;

public:
NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;

NullablePtr<const void> addressForPrinting() const override {
return paramList;
}
Expand Down Expand Up @@ -887,9 +877,6 @@ class AbstractFunctionDeclScope final : public ASTScopeImpl {
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }

NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;

NullablePtr<const void> getReferrent() const override;

protected:
Expand All @@ -914,8 +901,7 @@ class ParameterListScope final : public ASTScopeImpl {
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;

private:
AnnotatedInsertionPoint
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
SourceLoc fixupEndForBadInput(SourceRange) const;

public:
Expand All @@ -924,17 +910,16 @@ class ParameterListScope final : public ASTScopeImpl {
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
virtual NullablePtr<DeclContext> getDeclContext() const override;

NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;
NullablePtr<const void> addressForPrinting() const override { return params; }
};

class AbstractFunctionBodyScope : public ASTScopeImpl {
/// Body of functions, methods, constructors, destructors and accessors.
class FunctionBodyScope : public ASTScopeImpl {
public:
AbstractFunctionDecl *const decl;

AbstractFunctionBodyScope(AbstractFunctionDecl *e) : decl(e) {}
virtual ~AbstractFunctionBodyScope() {}
FunctionBodyScope(AbstractFunctionDecl *e) : decl(e) {}
virtual ~FunctionBodyScope() {}

protected:
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
Expand All @@ -956,19 +941,11 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
bool lookupLocalsOrMembers(DeclConsumer) const override;

public:
std::string getClassName() const override;
NullablePtr<ASTScopeImpl> insertionPointForDeferredExpansion() override;
SourceRange sourceRangeForDeferredExpansion() const override;
};

/// Body of functions and methods.
class FunctionBodyScope final : public AbstractFunctionBodyScope {
public:
FunctionBodyScope(AbstractFunctionDecl *e)
: AbstractFunctionBodyScope(e) {}
std::string getClassName() const override;
bool lookupLocalsOrMembers(DeclConsumer consumer) const override;
};

class DefaultArgumentInitializerScope final : public ASTScopeImpl {
public:
ParamDecl *const decl;
Expand Down Expand Up @@ -1001,17 +978,19 @@ class DefaultArgumentInitializerScope final : public ASTScopeImpl {

class AttachedPropertyWrapperScope final : public ASTScopeImpl {
public:
VarDecl *const decl;
CustomAttr *attr;
VarDecl *decl;

/// Because we have to avoid request cycles, we approximate the test for an
/// AttachedPropertyWrapper with one based on source location. We might get
/// false positives, that that doesn't hurt anything. However, the result of
/// the conservative source range computation doesn't seem to be stable. So
/// keep the original here, and use it for source range queries.

const SourceRange sourceRangeWhenCreated;

AttachedPropertyWrapperScope(VarDecl *e)
: decl(e), sourceRangeWhenCreated(getSourceRangeOfVarDecl(e)) {
AttachedPropertyWrapperScope(CustomAttr *attr, VarDecl *decl)
: attr(attr), decl(decl),
sourceRangeWhenCreated(attr->getTypeRepr()->getSourceRange()) {
ASTScopeAssert(sourceRangeWhenCreated.isValid(),
"VarDecls must have ranges to be looked-up");
}
Expand All @@ -1027,7 +1006,10 @@ class AttachedPropertyWrapperScope final : public ASTScopeImpl {
NullablePtr<const void> addressForPrinting() const override { return decl; }
virtual NullablePtr<DeclContext> getDeclContext() const override;

static SourceRange getSourceRangeOfVarDecl(const VarDecl *);
NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
return attr;
}
NullablePtr<const void> getReferrent() const override;

private:
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
Expand Down Expand Up @@ -1068,11 +1050,8 @@ class AbstractPatternEntryScope : public ASTScopeImpl {

protected:
void printSpecifics(llvm::raw_ostream &out) const override;
void forEachVarDeclWithLocalizableAccessors(
ScopeCreator &scopeCreator, function_ref<void(VarDecl *)> foundOne) const;

public:
bool isLastEntry() const;
NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }
};
Expand Down Expand Up @@ -1116,8 +1095,7 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;

private:
AnnotatedInsertionPoint
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);

public:
std::string getClassName() const override;
Expand Down Expand Up @@ -1296,9 +1274,6 @@ class SpecializeAttributeScope final : public ASTScopeImpl {
return specializeAttr;
}

NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;

NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
return specializeAttr;
}
Expand Down Expand Up @@ -1329,9 +1304,6 @@ class DifferentiableAttributeScope final : public ASTScopeImpl {
return differentiableAttr;
}

NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;

NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
return differentiableAttr;
}
Expand Down Expand Up @@ -1373,44 +1345,6 @@ class SubscriptDeclScope final : public ASTScopeImpl {

protected:
NullablePtr<const GenericParamList> genericParams() const override;
NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override {
return decl;
}
public:
bool isThisAnAbstractStorageDecl() const override { return true; }
};

class VarDeclScope final : public ASTScopeImpl {

public:
VarDecl *const decl;
VarDeclScope(VarDecl *e) : decl(e) {}
virtual ~VarDeclScope() {}

protected:
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;

private:
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);

public:
std::string getClassName() const override;
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;

protected:
void printSpecifics(llvm::raw_ostream &out) const override;

public:
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }
NullablePtr<const void> getReferrent() const override;
NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override {
return decl;
}
bool isThisAnAbstractStorageDecl() const override { return true; }
};

class EnumElementScope : public ASTScopeImpl {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/FileUnit.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class FileUnit : public DeclContext {
/// \see ModuleDecl::getImportedModulesForLookup
virtual void getImportedModulesForLookup(
SmallVectorImpl<ModuleDecl::ImportedModule> &imports) const {
return getImportedModules(imports, ModuleDecl::ImportFilterKind::Public);
return getImportedModules(imports, ModuleDecl::ImportFilterKind::Exported);
}

/// Generates the list of libraries needed to link this file, based on its
Expand Down
45 changes: 35 additions & 10 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -648,27 +648,52 @@ class ModuleDecl : public DeclContext, public TypeDecl {
/// \sa getImportedModules
enum class ImportFilterKind {
/// Include imports declared with `@_exported`.
Public = 1 << 0,
Exported = 1 << 0,
/// Include "regular" imports with no special annotation.
Private = 1 << 1,
Default = 1 << 1,
/// Include imports declared with `@_implementationOnly`.
ImplementationOnly = 1 << 2,
/// Include imports of SPIs declared with `@_spi`
/// Include imports of SPIs declared with `@_spi`. Non-SPI imports are
/// included whether or not this flag is specified.
SPIAccessControl = 1 << 3,
/// Include imports shadowed by a separately-imported overlay (i.e. a
/// cross-import overlay). Unshadowed imports are included whether or not
/// this flag is specified.
ShadowedBySeparateOverlay = 1 << 4
/// Include imports shadowed by a cross-import overlay. Unshadowed imports
/// are included whether or not this flag is specified.
ShadowedByCrossImportOverlay = 1 << 4
};
/// \sa getImportedModules
using ImportFilter = OptionSet<ImportFilterKind>;

/// Looks up which modules are imported by this module.
///
/// \p filter controls whether public, private, or any imports are included
/// in this list.
/// \p filter controls which imports are included in the list.
///
/// There are three axes for categorizing imports:
/// 1. Privacy: Exported/Private/ImplementationOnly (mutually exclusive).
/// 2. SPI/non-SPI: An import of any privacy level may be @_spi("SPIName").
/// 3. Shadowed/Non-shadowed: An import of any privacy level may be shadowed
/// by a cross-import overlay.
///
/// It is also possible for SPI imports to be shadowed by a cross-import
/// overlay.
///
/// If \p filter contains multiple privacy levels, modules at all the privacy
/// levels are included.
///
/// If \p filter contains \c ImportFilterKind::SPIAccessControl, then both
/// SPI and non-SPI imports are included. Otherwise, only non-SPI imports are
/// included.
///
/// If \p filter contains \c ImportFilterKind::ShadowedByCrossImportOverlay,
/// both shadowed and non-shadowed imports are included. Otherwise, only
/// non-shadowed imports are included.
///
/// Clang modules have some additional complexities; see the implementation of
/// \c ClangModuleUnit::getImportedModules for details.
///
/// \pre \p filter must contain at least one privacy level, i.e. one of
/// \c Exported or \c Private or \c ImplementationOnly.
void getImportedModules(SmallVectorImpl<ImportedModule> &imports,
ImportFilter filter = ImportFilterKind::Public) const;
ImportFilter filter = ImportFilterKind::Exported) const;

/// Looks up which modules are imported by this module, ignoring any that
/// won't contain top-level decls.
Expand Down
6 changes: 3 additions & 3 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ struct PrintOptions {
}

/// Retrieve the set of options suitable for diagnostics printing.
static PrintOptions printForDiagnostics() {
static PrintOptions printForDiagnostics(AccessLevel accessFilter) {
PrintOptions result = printVerbose();
result.PrintAccess = true;
result.Indent = 4;
Expand All @@ -512,7 +512,7 @@ struct PrintOptions {
result.ExcludeAttrList.push_back(DAK_Optimize);
result.ExcludeAttrList.push_back(DAK_Rethrows);
result.PrintOverrideKeyword = false;
result.AccessFilter = AccessLevel::Public;
result.AccessFilter = accessFilter;
result.PrintIfConfig = false;
result.ShouldQualifyNestedDeclarations =
QualifyNestedDeclarations::TypesOnly;
Expand All @@ -522,7 +522,7 @@ struct PrintOptions {

/// Retrieve the set of options suitable for interface generation.
static PrintOptions printInterface() {
PrintOptions result = printForDiagnostics();
PrintOptions result = printForDiagnostics(AccessLevel::Public);
result.SkipUnavailable = true;
result.SkipImplicit = true;
result.SkipSwiftPrivateClangDecls = true;
Expand Down
9 changes: 9 additions & 0 deletions include/swift/Basic/OptionSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "llvm/ADT/None.h"

#include <cassert>
#include <type_traits>
#include <cstdint>
#include <initializer_list>
Expand Down Expand Up @@ -98,6 +99,14 @@ class OptionSet {
return Storage == set.Storage;
}

/// Check if this option set contains any options from \p set.
///
/// \pre \p set must be non-empty.
bool containsAny(OptionSet set) const {
assert((bool)set && "argument must be non-empty");
return (bool)((*this) & set);
}

// '==' and '!=' are deliberately not defined because they provide a pitfall
// where someone might use '==' but really want 'contains'. If you actually
// want '==' behavior, use 'containsOnly'.
Expand Down
14 changes: 0 additions & 14 deletions lib/AST/ASTScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,6 @@ NullablePtr<ClosureExpr> ASTScopeImpl::getClosureIfClosureScope() const {
return nullptr;
}

// Conservative, because using precise info would be circular
SourceRange
AttachedPropertyWrapperScope::getSourceRangeOfVarDecl(const VarDecl *const vd) {
SourceRange sr;
for (auto *attr : vd->getAttrs().getAttributes<CustomAttr>()) {
if (sr.isInvalid())
sr = attr->getTypeRepr()->getSourceRange();
else
sr.widen(attr->getTypeRepr()->getSourceRange());
}
return sr;
}

SourceManager &ASTScopeImpl::getSourceManager() const {
return getASTContext().SourceMgr;
}
Expand Down Expand Up @@ -211,7 +198,6 @@ DEFINE_GET_CLASS_NAME(TopLevelCodeScope)
DEFINE_GET_CLASS_NAME(SpecializeAttributeScope)
DEFINE_GET_CLASS_NAME(DifferentiableAttributeScope)
DEFINE_GET_CLASS_NAME(SubscriptDeclScope)
DEFINE_GET_CLASS_NAME(VarDeclScope)
DEFINE_GET_CLASS_NAME(EnumElementScope)
DEFINE_GET_CLASS_NAME(IfStmtScope)
DEFINE_GET_CLASS_NAME(WhileStmtScope)
Expand Down
Loading

0 comments on commit 82e8a18

Please sign in to comment.