Skip to content

Commit e9650cf

Browse files
authored
Merge pull request swiftlang#33716 from brentdax/access-path-denied
2 parents cfd75a1 + 6f28536 commit e9650cf

Some content is hidden

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

54 files changed

+745
-465
lines changed

docs/Lexicon.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,28 @@ introduces [reabstraction](#reabstraction) conversions when a value is used with
4545
different abstraction pattern. (This is where the infamous "reabstraction
4646
thunk helpers" sometimes seen in Swift backtraces come from.)
4747

48+
## access path
49+
50+
Broadly, an "access path" is a list of "accesses" which must be chained together
51+
to compute some output from an input. For instance, the generics system has a
52+
type called a `ConformanceAccessPath` which explains how to, for example,
53+
walk from `T: Collection` to `T: Sequence` to `T.Iterator: IteratorProtocol`.
54+
There are several different kinds of "access path" in different parts of the compiler,
55+
but they all follow this basic theme.
56+
57+
In the specific context of imports, an "access path" is the `Bar` portion of a scoped
58+
import like `import class Foo.Bar`. Theoretically, it could have several identifiers
59+
to designate a nested type, although the compiler doesn't currently support this. It can
60+
also be empty, matching all top-level declarations in the module.
61+
62+
Note, however, that there has historically been some confusion about the meaning of
63+
"access path" with regards to imports. You might see some code use "access path"
64+
to include the `Foo` part or even to describe a chain of submodule names where a
65+
declaration is not valid at all. (Strictly, the chain of module names is a "module path"
66+
and the combination of module path + access path is an "import path".)
67+
68+
See `ImportPath` and the types nested inside it for more on this.
69+
4870
## archetype
4971

5072
A placeholder for a generic parameter or an associated type within a

include/swift/AST/ASTContext.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/Evaluator.h"
2121
#include "swift/AST/GenericSignature.h"
2222
#include "swift/AST/Identifier.h"
23+
#include "swift/AST/Import.h"
2324
#include "swift/AST/SearchPathOptions.h"
2425
#include "swift/AST/Type.h"
2526
#include "swift/AST/Types.h"
@@ -859,12 +860,12 @@ class ASTContext final {
859860
///
860861
/// Note that even if this check succeeds, errors may still occur if the
861862
/// module is loaded in full.
862-
bool canImportModule(Located<Identifier> ModulePath);
863+
bool canImportModule(ImportPath::Element ModulePath);
863864

864865
/// \returns a module with a given name that was already loaded. If the
865866
/// module was not loaded, returns nullptr.
866867
ModuleDecl *getLoadedModule(
867-
ArrayRef<Located<Identifier>> ModulePath) const;
868+
ImportPath::Module ModulePath) const;
868869

869870
ModuleDecl *getLoadedModule(Identifier ModuleName) const;
870871

@@ -874,10 +875,12 @@ class ASTContext final {
874875
/// be returned.
875876
///
876877
/// \returns The requested module, or NULL if the module cannot be found.
877-
ModuleDecl *getModule(ArrayRef<Located<Identifier>> ModulePath);
878+
ModuleDecl *getModule(ImportPath::Module ModulePath);
878879

879880
ModuleDecl *getModuleByName(StringRef ModuleName);
880881

882+
ModuleDecl *getModuleByIdentifier(Identifier ModuleID);
883+
881884
/// Returns the standard library module, or null if the library isn't present.
882885
///
883886
/// If \p loadIfAbsent is true, the ASTContext will attempt to load the module

include/swift/AST/Decl.h

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,46 +1457,28 @@ class GenericContext : private _GenericContext, public DeclContext {
14571457
static_assert(sizeof(_GenericContext) + sizeof(DeclContext) ==
14581458
sizeof(GenericContext), "Please add fields to _GenericContext");
14591459

1460-
/// Describes what kind of name is being imported.
1461-
///
1462-
/// If the enumerators here are changed, make sure to update all diagnostics
1463-
/// using ImportKind as a select index.
1464-
enum class ImportKind : uint8_t {
1465-
Module = 0,
1466-
Type,
1467-
Struct,
1468-
Class,
1469-
Enum,
1470-
Protocol,
1471-
Var,
1472-
Func
1473-
};
1474-
14751460
/// ImportDecl - This represents a single import declaration, e.g.:
14761461
/// import Swift
14771462
/// import typealias Swift.Int
14781463
class ImportDecl final : public Decl,
1479-
private llvm::TrailingObjects<ImportDecl, Located<Identifier>> {
1464+
private llvm::TrailingObjects<ImportDecl, ImportPath::Element> {
14801465
friend TrailingObjects;
14811466
friend class Decl;
1482-
public:
1483-
typedef Located<Identifier> AccessPathElement;
14841467

1485-
private:
14861468
SourceLoc ImportLoc;
14871469
SourceLoc KindLoc;
14881470

14891471
/// The resolved module.
14901472
ModuleDecl *Mod = nullptr;
14911473

14921474
ImportDecl(DeclContext *DC, SourceLoc ImportLoc, ImportKind K,
1493-
SourceLoc KindLoc, ArrayRef<AccessPathElement> Path);
1475+
SourceLoc KindLoc, ImportPath Path);
14941476

14951477
public:
14961478
static ImportDecl *create(ASTContext &C, DeclContext *DC,
14971479
SourceLoc ImportLoc, ImportKind Kind,
14981480
SourceLoc KindLoc,
1499-
ArrayRef<AccessPathElement> Path,
1481+
ImportPath Path,
15001482
ClangNode ClangN = ClangNode());
15011483

15021484
/// Returns the import kind that is most appropriate for \p VD.
@@ -1511,26 +1493,21 @@ class ImportDecl final : public Decl,
15111493
/// cannot be overloaded, returns None.
15121494
static Optional<ImportKind> findBestImportKind(ArrayRef<ValueDecl *> Decls);
15131495

1514-
ArrayRef<AccessPathElement> getFullAccessPath() const {
1515-
return {getTrailingObjects<AccessPathElement>(),
1516-
static_cast<size_t>(Bits.ImportDecl.NumPathElements)};
1496+
ImportKind getImportKind() const {
1497+
return static_cast<ImportKind>(Bits.ImportDecl.ImportKind);
15171498
}
15181499

1519-
ArrayRef<AccessPathElement> getModulePath() const {
1520-
auto result = getFullAccessPath();
1521-
if (getImportKind() != ImportKind::Module)
1522-
result = result.slice(0, result.size()-1);
1523-
return result;
1500+
ImportPath getImportPath() const {
1501+
return ImportPath({ getTrailingObjects<ImportPath::Element>(),
1502+
static_cast<size_t>(Bits.ImportDecl.NumPathElements) });
15241503
}
15251504

1526-
ArrayRef<AccessPathElement> getDeclPath() const {
1527-
if (getImportKind() == ImportKind::Module)
1528-
return {};
1529-
return getFullAccessPath().back();
1505+
ImportPath::Module getModulePath() const {
1506+
return getImportPath().getModulePath(getImportKind());
15301507
}
15311508

1532-
ImportKind getImportKind() const {
1533-
return static_cast<ImportKind>(Bits.ImportDecl.ImportKind);
1509+
ImportPath::Access getAccessPath() const {
1510+
return getImportPath().getAccessPath(getImportKind());
15341511
}
15351512

15361513
bool isExported() const {
@@ -1549,9 +1526,11 @@ class ImportDecl final : public Decl,
15491526
}
15501527

15511528
SourceLoc getStartLoc() const { return ImportLoc; }
1552-
SourceLoc getLocFromSource() const { return getFullAccessPath().front().Loc; }
1529+
SourceLoc getLocFromSource() const {
1530+
return getImportPath().getSourceRange().Start;
1531+
}
15531532
SourceRange getSourceRange() const {
1554-
return SourceRange(ImportLoc, getFullAccessPath().back().Loc);
1533+
return SourceRange(ImportLoc, getImportPath().getSourceRange().End);
15551534
}
15561535
SourceLoc getKindLoc() const { return KindLoc; }
15571536

include/swift/AST/FileUnit.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,20 @@ class FileUnit : public DeclContext {
8282
/// Find ValueDecls in the module and pass them to the given consumer object.
8383
///
8484
/// This does a simple local lookup, not recursively looking through imports.
85-
virtual void lookupVisibleDecls(ModuleDecl::AccessPathTy accessPath,
85+
virtual void lookupVisibleDecls(ImportPath::Access accessPath,
8686
VisibleDeclConsumer &consumer,
8787
NLKind lookupKind) const {}
8888

8989
/// Finds all class members defined in this file.
9090
///
9191
/// This does a simple local lookup, not recursively looking through imports.
92-
virtual void lookupClassMembers(ModuleDecl::AccessPathTy accessPath,
92+
virtual void lookupClassMembers(ImportPath::Access accessPath,
9393
VisibleDeclConsumer &consumer) const {}
9494

9595
/// Finds class members defined in this file with the given name.
9696
///
9797
/// This does a simple local lookup, not recursively looking through imports.
98-
virtual void lookupClassMember(ModuleDecl::AccessPathTy accessPath,
98+
virtual void lookupClassMember(ImportPath::Access accessPath,
9999
DeclName name,
100100
SmallVectorImpl<ValueDecl*> &results) const {}
101101

0 commit comments

Comments
 (0)