|
| 1 | +//===--- ImportCache.h - Caching the import graph ---------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file defines interfaces for querying the module import graph in an |
| 14 | +// efficient manner. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_AST_IMPORT_CACHE_H |
| 19 | +#define SWIFT_AST_IMPORT_CACHE_H |
| 20 | + |
| 21 | +#include "swift/AST/Module.h" |
| 22 | +#include "llvm/ADT/DenseMap.h" |
| 23 | +#include "llvm/ADT/FoldingSet.h" |
| 24 | + |
| 25 | +namespace swift { |
| 26 | +class DeclContext; |
| 27 | + |
| 28 | +namespace namelookup { |
| 29 | + |
| 30 | +/// An object describing a set of modules visible from a DeclContext. |
| 31 | +/// |
| 32 | +/// This consists of two arrays of modules; the top-level imports and the |
| 33 | +/// transitive imports. |
| 34 | +/// |
| 35 | +/// The top-level imports contains all public imports of the parent module |
| 36 | +/// of 'dc', together with any private imports in the source file containing |
| 37 | +/// 'dc', if there is one. |
| 38 | +/// |
| 39 | +/// The transitive imports contains all public imports reachable from the |
| 40 | +/// set of top-level imports. |
| 41 | +/// |
| 42 | +/// Both sets only contain unique elements. The top-level imports always |
| 43 | +/// include the parent module of 'dc' explicitly. |
| 44 | +/// |
| 45 | +/// The set of transitive imports does not contain any elements found in |
| 46 | +/// the top-level imports. |
| 47 | +/// |
| 48 | +/// The Swift standard library module is not included in either set unless |
| 49 | +/// it was explicitly imported (or re-exported). |
| 50 | +class ImportSet final : |
| 51 | + public llvm::FoldingSetNode, |
| 52 | + private llvm::TrailingObjects<ImportSet, ModuleDecl::ImportedModule> { |
| 53 | + friend TrailingObjects; |
| 54 | + friend ImportCache; |
| 55 | + |
| 56 | + unsigned NumTopLevelImports; |
| 57 | + unsigned NumTransitiveImports; |
| 58 | + |
| 59 | + ImportSet(ArrayRef<ModuleDecl::ImportedModule> topLevelImports, |
| 60 | + ArrayRef<ModuleDecl::ImportedModule> transitiveImports); |
| 61 | + |
| 62 | + ImportSet(const ImportSet &) = delete; |
| 63 | + void operator=(const ImportSet &) = delete; |
| 64 | + |
| 65 | +public: |
| 66 | + void Profile(llvm::FoldingSetNodeID &ID) { |
| 67 | + Profile(ID, getTopLevelImports()); |
| 68 | + } |
| 69 | + static void Profile( |
| 70 | + llvm::FoldingSetNodeID &ID, |
| 71 | + ArrayRef<ModuleDecl::ImportedModule> topLevelImports); |
| 72 | + |
| 73 | + size_t numTrailingObjects(OverloadToken<ModuleDecl::ImportedModule>) const { |
| 74 | + return NumTopLevelImports + NumTransitiveImports; |
| 75 | + } |
| 76 | + |
| 77 | + ArrayRef<ModuleDecl::ImportedModule> getTopLevelImports() const { |
| 78 | + return {getTrailingObjects<ModuleDecl::ImportedModule>(), |
| 79 | + NumTopLevelImports}; |
| 80 | + } |
| 81 | + |
| 82 | + ArrayRef<ModuleDecl::ImportedModule> getTransitiveImports() const { |
| 83 | + return {getTrailingObjects<ModuleDecl::ImportedModule>() + |
| 84 | + NumTopLevelImports, |
| 85 | + NumTransitiveImports}; |
| 86 | + } |
| 87 | + |
| 88 | + ArrayRef<ModuleDecl::ImportedModule> getAllImports() const { |
| 89 | + return {getTrailingObjects<ModuleDecl::ImportedModule>(), |
| 90 | + NumTopLevelImports + NumTransitiveImports}; |
| 91 | + } |
| 92 | +}; |
| 93 | + |
| 94 | +class alignas(ModuleDecl::ImportedModule) ImportCache { |
| 95 | + ImportCache(const ImportCache &) = delete; |
| 96 | + void operator=(const ImportCache &) = delete; |
| 97 | + |
| 98 | + llvm::FoldingSet<ImportSet> ImportSets; |
| 99 | + llvm::DenseMap<const DeclContext *, ImportSet *> ImportSetForDC; |
| 100 | + llvm::DenseMap<std::tuple<const ModuleDecl *, |
| 101 | + const DeclContext *>, |
| 102 | + ArrayRef<ModuleDecl::AccessPathTy>> VisibilityCache; |
| 103 | + llvm::DenseMap<std::tuple<const ModuleDecl *, |
| 104 | + const ModuleDecl *, |
| 105 | + const DeclContext *>, |
| 106 | + ArrayRef<ModuleDecl::AccessPathTy>> ShadowCache; |
| 107 | + |
| 108 | + ModuleDecl::AccessPathTy EmptyAccessPath; |
| 109 | + |
| 110 | + ArrayRef<ModuleDecl::AccessPathTy> allocateArray( |
| 111 | + ASTContext &ctx, |
| 112 | + SmallVectorImpl<ModuleDecl::AccessPathTy> &results); |
| 113 | + |
| 114 | + ImportSet &getImportSet(ASTContext &ctx, |
| 115 | + ArrayRef<ModuleDecl::ImportedModule> topLevelImports); |
| 116 | + |
| 117 | +public: |
| 118 | + ImportCache() {} |
| 119 | + |
| 120 | + /// Returns an object descripting all modules transtiively imported |
| 121 | + /// from 'dc'. |
| 122 | + ImportSet &getImportSet(const DeclContext *dc); |
| 123 | + |
| 124 | + /// Returns all access paths into 'mod' that are visible from 'dc', |
| 125 | + /// including transitively, via re-exports. |
| 126 | + ArrayRef<ModuleDecl::AccessPathTy> |
| 127 | + getAllVisibleAccessPaths(const ModuleDecl *mod, const DeclContext *dc); |
| 128 | + |
| 129 | + bool isImportedBy(const ModuleDecl *mod, |
| 130 | + const DeclContext *dc) { |
| 131 | + return !getAllVisibleAccessPaths(mod, dc).empty(); |
| 132 | + } |
| 133 | + |
| 134 | + /// Determines if 'mod' is visible from 'dc' as a result of a scoped import. |
| 135 | + /// Note that if 'mod' was not imported from 'dc' at all, this also returns |
| 136 | + /// false. |
| 137 | + bool isScopedImport(const ModuleDecl *mod, DeclBaseName name, |
| 138 | + const DeclContext *dc) { |
| 139 | + auto accessPaths = getAllVisibleAccessPaths(mod, dc); |
| 140 | + for (auto accessPath : accessPaths) { |
| 141 | + if (accessPath.empty()) |
| 142 | + continue; |
| 143 | + if (ModuleDecl::matchesAccessPath(accessPath, name)) |
| 144 | + return true; |
| 145 | + } |
| 146 | + |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + /// Returns all access paths in 'mod' that are visible from 'dc' if we |
| 151 | + /// subtract imports of 'other'. |
| 152 | + ArrayRef<ModuleDecl::AccessPathTy> |
| 153 | + getAllAccessPathsNotShadowedBy(const ModuleDecl *mod, |
| 154 | + const ModuleDecl *other, |
| 155 | + const DeclContext *dc); |
| 156 | + |
| 157 | + /// Returns 'true' if a declaration named 'name' defined in 'other' shadows |
| 158 | + /// defined in 'mod', because no access paths can find 'name' in 'mod' from |
| 159 | + /// 'dc' if we ignore imports of 'other'. |
| 160 | + bool isShadowedBy(const ModuleDecl *mod, |
| 161 | + const ModuleDecl *other, |
| 162 | + DeclBaseName name, |
| 163 | + const DeclContext *dc) { |
| 164 | + auto accessPaths = getAllAccessPathsNotShadowedBy(mod, other, dc); |
| 165 | + return llvm::none_of(accessPaths, |
| 166 | + [&](ModuleDecl::AccessPathTy accessPath) { |
| 167 | + return ModuleDecl::matchesAccessPath(accessPath, name); |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + /// Qualified lookup into types uses a slightly different check that does not |
| 172 | + /// take access paths into account. |
| 173 | + bool isShadowedBy(const ModuleDecl *mod, |
| 174 | + const ModuleDecl *other, |
| 175 | + const DeclContext *dc) { |
| 176 | + auto accessPaths = getAllAccessPathsNotShadowedBy(mod, other, dc); |
| 177 | + return accessPaths.empty(); |
| 178 | + } |
| 179 | + |
| 180 | + /// This is a hack to cope with main file parsing and REPL parsing, where |
| 181 | + /// we can add ImportDecls after name binding. |
| 182 | + void clear() { |
| 183 | + ImportSetForDC.clear(); |
| 184 | + } |
| 185 | +}; |
| 186 | + |
| 187 | +} // namespace namelookup |
| 188 | + |
| 189 | +} // namespace swift |
| 190 | + |
| 191 | +#endif |
0 commit comments