|
| 1 | +//===--- FileUnit.h - The contents of a module ------------------*- 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 | +#ifndef SWIFT_AST_FILEUNIT_H |
| 14 | +#define SWIFT_AST_FILEUNIT_H |
| 15 | + |
| 16 | +#include "swift/AST/Module.h" |
| 17 | + |
| 18 | +namespace swift { |
| 19 | +static inline unsigned alignOfFileUnit(); |
| 20 | + |
| 21 | +/// A container for module-scope declarations that itself provides a scope; the |
| 22 | +/// smallest unit of code organization. |
| 23 | +/// |
| 24 | +/// FileUnit is an abstract base class; its subclasses represent different |
| 25 | +/// sorts of containers that can each provide a set of decls, e.g. a source |
| 26 | +/// file. A module can contain several file-units. |
| 27 | +#pragma clang diagnostic push |
| 28 | +#pragma clang diagnostic ignored "-Wnon-virtual-dtor" |
| 29 | +class FileUnit : public DeclContext { |
| 30 | +#pragma clang diagnostic pop |
| 31 | + virtual void anchor(); |
| 32 | + |
| 33 | + // FIXME: Stick this in a PointerIntPair. |
| 34 | + const FileUnitKind Kind; |
| 35 | + |
| 36 | +protected: |
| 37 | + FileUnit(FileUnitKind kind, ModuleDecl &M) |
| 38 | + : DeclContext(DeclContextKind::FileUnit, &M), Kind(kind) { |
| 39 | + } |
| 40 | + |
| 41 | +public: |
| 42 | + FileUnitKind getKind() const { |
| 43 | + return Kind; |
| 44 | + } |
| 45 | + |
| 46 | + /// Look up a (possibly overloaded) value set at top-level scope |
| 47 | + /// (but with the specified access path, which may come from an import decl) |
| 48 | + /// within this file. |
| 49 | + /// |
| 50 | + /// This does a simple local lookup, not recursively looking through imports. |
| 51 | + virtual void lookupValue(DeclName name, NLKind lookupKind, |
| 52 | + SmallVectorImpl<ValueDecl*> &result) const = 0; |
| 53 | + |
| 54 | + /// Look up a local type declaration by its mangled name. |
| 55 | + /// |
| 56 | + /// This does a simple local lookup, not recursively looking through imports. |
| 57 | + virtual TypeDecl *lookupLocalType(StringRef MangledName) const { |
| 58 | + return nullptr; |
| 59 | + } |
| 60 | + |
| 61 | + /// Look up an opaque return type by the mangled name of the declaration |
| 62 | + /// that defines it. |
| 63 | + virtual OpaqueTypeDecl *lookupOpaqueResultType(StringRef MangledName, |
| 64 | + LazyResolver *resolver) { |
| 65 | + return nullptr; |
| 66 | + } |
| 67 | + |
| 68 | + /// Directly look for a nested type declared within this module inside the |
| 69 | + /// given nominal type (including any extensions). |
| 70 | + /// |
| 71 | + /// This is a fast-path hack to avoid circular dependencies in deserialization |
| 72 | + /// and the Clang importer. |
| 73 | + /// |
| 74 | + /// Private and fileprivate types should not be returned by this lookup. |
| 75 | + virtual TypeDecl *lookupNestedType(Identifier name, |
| 76 | + const NominalTypeDecl *parent) const { |
| 77 | + return nullptr; |
| 78 | + } |
| 79 | + |
| 80 | + /// Find ValueDecls in the module and pass them to the given consumer object. |
| 81 | + /// |
| 82 | + /// This does a simple local lookup, not recursively looking through imports. |
| 83 | + virtual void lookupVisibleDecls(ModuleDecl::AccessPathTy accessPath, |
| 84 | + VisibleDeclConsumer &consumer, |
| 85 | + NLKind lookupKind) const {} |
| 86 | + |
| 87 | + /// Finds all class members defined in this file. |
| 88 | + /// |
| 89 | + /// This does a simple local lookup, not recursively looking through imports. |
| 90 | + virtual void lookupClassMembers(ModuleDecl::AccessPathTy accessPath, |
| 91 | + VisibleDeclConsumer &consumer) const {} |
| 92 | + |
| 93 | + /// Finds class members defined in this file with the given name. |
| 94 | + /// |
| 95 | + /// This does a simple local lookup, not recursively looking through imports. |
| 96 | + virtual void lookupClassMember(ModuleDecl::AccessPathTy accessPath, |
| 97 | + DeclName name, |
| 98 | + SmallVectorImpl<ValueDecl*> &results) const {} |
| 99 | + |
| 100 | + /// Find all Objective-C methods with the given selector. |
| 101 | + virtual void lookupObjCMethods( |
| 102 | + ObjCSelector selector, |
| 103 | + SmallVectorImpl<AbstractFunctionDecl *> &results) const = 0; |
| 104 | + |
| 105 | + /// Returns the comment attached to the given declaration. |
| 106 | + /// |
| 107 | + /// This function is an implementation detail for comment serialization. |
| 108 | + /// If you just want to get a comment attached to a decl, use |
| 109 | + /// \c Decl::getRawComment() or \c Decl::getBriefComment(). |
| 110 | + virtual Optional<CommentInfo> |
| 111 | + getCommentForDecl(const Decl *D) const { |
| 112 | + return None; |
| 113 | + } |
| 114 | + |
| 115 | + virtual Optional<StringRef> |
| 116 | + getGroupNameForDecl(const Decl *D) const { |
| 117 | + return None; |
| 118 | + } |
| 119 | + |
| 120 | + virtual Optional<StringRef> |
| 121 | + getSourceFileNameForDecl(const Decl *D) const { |
| 122 | + return None; |
| 123 | + } |
| 124 | + |
| 125 | + virtual Optional<unsigned> |
| 126 | + getSourceOrderForDecl(const Decl *D) const { |
| 127 | + return None; |
| 128 | + } |
| 129 | + |
| 130 | + virtual Optional<StringRef> |
| 131 | + getGroupNameByUSR(StringRef USR) const { |
| 132 | + return None; |
| 133 | + } |
| 134 | + |
| 135 | + virtual void collectAllGroups(std::vector<StringRef> &Names) const {} |
| 136 | + |
| 137 | + /// Returns an implementation-defined "discriminator" for \p D, which |
| 138 | + /// distinguishes \p D from other declarations in the same module with the |
| 139 | + /// same name. |
| 140 | + /// |
| 141 | + /// Since this value is used in name mangling, it should be a valid ASCII-only |
| 142 | + /// identifier. |
| 143 | + virtual Identifier |
| 144 | + getDiscriminatorForPrivateValue(const ValueDecl *D) const = 0; |
| 145 | + |
| 146 | + /// Finds all top-level decls in this file. |
| 147 | + /// |
| 148 | + /// This does a simple local lookup, not recursively looking through imports. |
| 149 | + /// The order of the results is not guaranteed to be meaningful. |
| 150 | + virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const {} |
| 151 | + |
| 152 | + |
| 153 | + /// Finds all precedence group decls in this file. |
| 154 | + /// |
| 155 | + /// This does a simple local lookup, not recursively looking through imports. |
| 156 | + /// The order of the results is not guaranteed to be meaningful. |
| 157 | + virtual void |
| 158 | + getPrecedenceGroups(SmallVectorImpl<PrecedenceGroupDecl*> &Results) const {} |
| 159 | + |
| 160 | + /// Finds all local type decls in this file. |
| 161 | + /// |
| 162 | + /// This does a simple local lookup, not recursively looking through imports. |
| 163 | + /// The order of the results is not guaranteed to be meaningful. |
| 164 | + virtual void getLocalTypeDecls(SmallVectorImpl<TypeDecl*> &results) const {} |
| 165 | + |
| 166 | + virtual void |
| 167 | + getOpaqueReturnTypeDecls(SmallVectorImpl<OpaqueTypeDecl*> &results) const {} |
| 168 | + |
| 169 | + /// Adds all top-level decls to the given vector. |
| 170 | + /// |
| 171 | + /// This includes all decls that should be displayed to clients of the module. |
| 172 | + /// The order of the results is not guaranteed to be meaningful. |
| 173 | + /// |
| 174 | + /// This can differ from \c getTopLevelDecls, e.g. it returns decls from a |
| 175 | + /// shadowed clang module. |
| 176 | + virtual void getDisplayDecls(SmallVectorImpl<Decl*> &results) const { |
| 177 | + getTopLevelDecls(results); |
| 178 | + } |
| 179 | + |
| 180 | + /// Looks up which modules are imported by this file. |
| 181 | + /// |
| 182 | + /// \p filter controls whether public, private, or any imports are included |
| 183 | + /// in this list. |
| 184 | + virtual void |
| 185 | + getImportedModules(SmallVectorImpl<ModuleDecl::ImportedModule> &imports, |
| 186 | + ModuleDecl::ImportFilter filter) const {} |
| 187 | + |
| 188 | + /// \see ModuleDecl::getImportedModulesForLookup |
| 189 | + virtual void getImportedModulesForLookup( |
| 190 | + SmallVectorImpl<ModuleDecl::ImportedModule> &imports) const { |
| 191 | + return getImportedModules(imports, ModuleDecl::ImportFilterKind::Public); |
| 192 | + } |
| 193 | + |
| 194 | + /// Generates the list of libraries needed to link this file, based on its |
| 195 | + /// imports. |
| 196 | + virtual void |
| 197 | + collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const {} |
| 198 | + |
| 199 | + /// True if this file contains the main class for the module. |
| 200 | + bool hasMainClass() const { |
| 201 | + return getMainClass(); |
| 202 | + } |
| 203 | + virtual ClassDecl *getMainClass() const { |
| 204 | + assert(hasEntryPoint()); |
| 205 | + return nullptr; |
| 206 | + } |
| 207 | + virtual bool hasEntryPoint() const { |
| 208 | + return false; |
| 209 | + } |
| 210 | + |
| 211 | + /// Returns the associated clang module if one exists. |
| 212 | + virtual const clang::Module *getUnderlyingClangModule() const { |
| 213 | + return nullptr; |
| 214 | + } |
| 215 | + |
| 216 | + /// Returns the name to use when referencing entities in this file. |
| 217 | + /// |
| 218 | + /// Usually this is the module name itself, but certain Clang features allow |
| 219 | + /// substituting another name instead. |
| 220 | + virtual StringRef getExportedModuleName() const { |
| 221 | + return getParentModule()->getName().str(); |
| 222 | + } |
| 223 | + |
| 224 | + /// Traverse the decls within this file. |
| 225 | + /// |
| 226 | + /// \returns true if traversal was aborted, false if it completed |
| 227 | + /// successfully. |
| 228 | + virtual bool walk(ASTWalker &walker); |
| 229 | + |
| 230 | + // Efficiency override for DeclContext::getParentModule(). |
| 231 | + ModuleDecl *getParentModule() const { |
| 232 | + return const_cast<ModuleDecl *>(cast<ModuleDecl>(getParent())); |
| 233 | + } |
| 234 | + |
| 235 | + static bool classof(const DeclContext *DC) { |
| 236 | + return DC->getContextKind() == DeclContextKind::FileUnit; |
| 237 | + } |
| 238 | + |
| 239 | +private: |
| 240 | + // Make placement new and vanilla new/delete illegal for FileUnits. |
| 241 | + void *operator new(size_t Bytes) throw() = delete; |
| 242 | + void *operator new(size_t Bytes, void *Mem) throw() = delete; |
| 243 | + void operator delete(void *Data) throw() = delete; |
| 244 | + |
| 245 | +public: |
| 246 | + // Only allow allocation of FileUnits using the allocator in ASTContext |
| 247 | + // or by doing a placement new. |
| 248 | + void *operator new(size_t Bytes, ASTContext &C, |
| 249 | + unsigned Alignment = alignOfFileUnit()); |
| 250 | +}; |
| 251 | + |
| 252 | +static inline unsigned alignOfFileUnit() { |
| 253 | + return alignof(FileUnit&); |
| 254 | +} |
| 255 | + |
| 256 | +/// This represents the compiler's implicitly generated declarations in the |
| 257 | +/// Builtin module. |
| 258 | +class BuiltinUnit final : public FileUnit { |
| 259 | +public: |
| 260 | + class LookupCache; |
| 261 | + |
| 262 | +private: |
| 263 | + std::unique_ptr<LookupCache> Cache; |
| 264 | + LookupCache &getCache() const; |
| 265 | + |
| 266 | + friend ASTContext; |
| 267 | + ~BuiltinUnit() = default; |
| 268 | + |
| 269 | +public: |
| 270 | + explicit BuiltinUnit(ModuleDecl &M); |
| 271 | + |
| 272 | + virtual void lookupValue(DeclName name, NLKind lookupKind, |
| 273 | + SmallVectorImpl<ValueDecl*> &result) const override; |
| 274 | + |
| 275 | + /// Find all Objective-C methods with the given selector. |
| 276 | + void lookupObjCMethods( |
| 277 | + ObjCSelector selector, |
| 278 | + SmallVectorImpl<AbstractFunctionDecl *> &results) const override; |
| 279 | + |
| 280 | + Identifier |
| 281 | + getDiscriminatorForPrivateValue(const ValueDecl *D) const override { |
| 282 | + llvm_unreachable("no private values in the Builtin module"); |
| 283 | + } |
| 284 | + |
| 285 | + static bool classof(const FileUnit *file) { |
| 286 | + return file->getKind() == FileUnitKind::Builtin; |
| 287 | + } |
| 288 | + |
| 289 | + static bool classof(const DeclContext *DC) { |
| 290 | + return isa<FileUnit>(DC) && classof(cast<FileUnit>(DC)); |
| 291 | + } |
| 292 | +}; |
| 293 | + |
| 294 | +/// Represents an externally-loaded file of some kind. |
| 295 | +#pragma clang diagnostic push |
| 296 | +#pragma clang diagnostic ignored "-Wnon-virtual-dtor" |
| 297 | +class LoadedFile : public FileUnit { |
| 298 | +#pragma clang diagnostic pop |
| 299 | +protected: |
| 300 | + LoadedFile(FileUnitKind Kind, ModuleDecl &M) noexcept |
| 301 | + : FileUnit(Kind, M) { |
| 302 | + assert(classof(this) && "invalid kind"); |
| 303 | + } |
| 304 | +public: |
| 305 | + /// Returns an arbitrary string representing the storage backing this file. |
| 306 | + /// |
| 307 | + /// This is usually a filesystem path. |
| 308 | + virtual StringRef getFilename() const; |
| 309 | + |
| 310 | + virtual StringRef getFilenameForPrivateDecl(const ValueDecl *decl) const { |
| 311 | + return StringRef(); |
| 312 | + } |
| 313 | + |
| 314 | + /// Look up an operator declaration. |
| 315 | + /// |
| 316 | + /// \param name The operator name ("+", ">>", etc.) |
| 317 | + /// |
| 318 | + /// \param fixity One of PrefixOperator, InfixOperator, or PostfixOperator. |
| 319 | + virtual OperatorDecl *lookupOperator(Identifier name, DeclKind fixity) const { |
| 320 | + return nullptr; |
| 321 | + } |
| 322 | + |
| 323 | + /// Look up a precedence group. |
| 324 | + /// |
| 325 | + /// \param name The precedence group name. |
| 326 | + virtual PrecedenceGroupDecl *lookupPrecedenceGroup(Identifier name) const { |
| 327 | + return nullptr; |
| 328 | + } |
| 329 | + |
| 330 | + /// Returns the Swift module that overlays a Clang module. |
| 331 | + virtual ModuleDecl *getOverlayModule() const { return nullptr; } |
| 332 | + |
| 333 | + virtual bool isSystemModule() const { return false; } |
| 334 | + |
| 335 | + /// Retrieve the set of generic signatures stored within this module. |
| 336 | + /// |
| 337 | + /// \returns \c true if this module file supports retrieving all of the |
| 338 | + /// generic signatures, \c false otherwise. |
| 339 | + virtual bool getAllGenericSignatures( |
| 340 | + SmallVectorImpl<GenericSignature*> &genericSignatures) { |
| 341 | + return false; |
| 342 | + } |
| 343 | + |
| 344 | + static bool classof(const FileUnit *file) { |
| 345 | + return file->getKind() == FileUnitKind::SerializedAST || |
| 346 | + file->getKind() == FileUnitKind::ClangModule || |
| 347 | + file->getKind() == FileUnitKind::DWARFModule; |
| 348 | + } |
| 349 | + static bool classof(const DeclContext *DC) { |
| 350 | + return isa<FileUnit>(DC) && classof(cast<FileUnit>(DC)); |
| 351 | + } |
| 352 | +}; |
| 353 | + |
| 354 | + |
| 355 | +inline FileUnit &ModuleDecl::getMainFile(FileUnitKind expectedKind) const { |
| 356 | + assert(expectedKind != FileUnitKind::Source && |
| 357 | + "must use specific source kind; see getMainSourceFile"); |
| 358 | + assert(!Files.empty() && "No files added yet"); |
| 359 | + assert(Files.front()->getKind() == expectedKind); |
| 360 | + return *Files.front(); |
| 361 | +} |
| 362 | + |
| 363 | +} // end namespace swift |
| 364 | + |
| 365 | +#endif |
0 commit comments