1 //===--- Module.h - Describe a module ---------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief Defines the clang::Module class, which describes a module in the 12 /// source code. 13 /// 14 //===----------------------------------------------------------------------===// 15 #ifndef LLVM_CLANG_BASIC_MODULE_H 16 #define LLVM_CLANG_BASIC_MODULE_H 17 18 #include "clang/Basic/SourceLocation.h" 19 #include "llvm/ADT/PointerIntPair.h" 20 #include "llvm/ADT/PointerUnion.h" 21 #include "llvm/ADT/SetVector.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/StringMap.h" 24 #include "llvm/ADT/StringRef.h" 25 #include <string> 26 #include <utility> 27 #include <vector> 28 29 namespace llvm { 30 class raw_ostream; 31 } 32 33 namespace clang { 34 35 class DirectoryEntry; 36 class FileEntry; 37 class FileManager; 38 class LangOptions; 39 class TargetInfo; 40 41 /// \brief Describes the name of a module. 42 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 43 44 /// \brief Describes a module or submodule. 45 class Module { 46 public: 47 /// \brief The name of this module. 48 std::string Name; 49 50 /// \brief The location of the module definition. 51 SourceLocation DefinitionLoc; 52 53 /// \brief The parent of this module. This will be NULL for the top-level 54 /// module. 55 Module *Parent; 56 57 /// \brief The umbrella header or directory. 58 llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella; 59 60 private: 61 /// \brief The submodules of this module, indexed by name. 62 std::vector<Module *> SubModules; 63 64 /// \brief A mapping from the submodule name to the index into the 65 /// \c SubModules vector at which that submodule resides. 66 llvm::StringMap<unsigned> SubModuleIndex; 67 68 /// \brief The AST file if this is a top-level module which has a 69 /// corresponding serialized AST file, or null otherwise. 70 const FileEntry *ASTFile; 71 72 /// \brief The top-level headers associated with this module. 73 llvm::SmallSetVector<const FileEntry *, 2> TopHeaders; 74 75 /// \brief top-level header filenames that aren't resolved to FileEntries yet. 76 std::vector<std::string> TopHeaderNames; 77 78 public: 79 /// \brief The headers that are part of this module. 80 SmallVector<const FileEntry *, 2> Headers; 81 82 /// \brief The headers that are explicitly excluded from this module. 83 SmallVector<const FileEntry *, 2> ExcludedHeaders; 84 85 /// \brief The set of language features required to use this module. 86 /// 87 /// If any of these features is not present, the \c IsAvailable bit 88 /// will be false to indicate that this (sub)module is not 89 /// available. 90 SmallVector<std::string, 2> Requires; 91 92 /// \brief Whether this module is available in the current 93 /// translation unit. 94 unsigned IsAvailable : 1; 95 96 /// \brief Whether this module was loaded from a module file. 97 unsigned IsFromModuleFile : 1; 98 99 /// \brief Whether this is a framework module. 100 unsigned IsFramework : 1; 101 102 /// \brief Whether this is an explicit submodule. 103 unsigned IsExplicit : 1; 104 105 /// \brief Whether this is a "system" module (which assumes that all 106 /// headers in it are system headers). 107 unsigned IsSystem : 1; 108 109 /// \brief Whether we should infer submodules for this module based on 110 /// the headers. 111 /// 112 /// Submodules can only be inferred for modules with an umbrella header. 113 unsigned InferSubmodules : 1; 114 115 /// \brief Whether, when inferring submodules, the inferred submodules 116 /// should be explicit. 117 unsigned InferExplicitSubmodules : 1; 118 119 /// \brief Whether, when inferring submodules, the inferr submodules should 120 /// export all modules they import (e.g., the equivalent of "export *"). 121 unsigned InferExportWildcard : 1; 122 123 /// \brief Describes the visibility of the various names within a 124 /// particular module. 125 enum NameVisibilityKind { 126 /// \brief All of the names in this module are hidden. 127 /// 128 Hidden, 129 /// \brief Only the macro names in this module are visible. 130 MacrosVisible, 131 /// \brief All of the names in this module are visible. 132 AllVisible 133 }; 134 135 ///\ brief The visibility of names within this particular module. 136 NameVisibilityKind NameVisibility; 137 138 /// \brief The location of the inferred submodule. 139 SourceLocation InferredSubmoduleLoc; 140 141 /// \brief The set of modules imported by this module, and on which this 142 /// module depends. 143 SmallVector<Module *, 2> Imports; 144 145 /// \brief Describes an exported module. 146 /// 147 /// The pointer is the module being re-exported, while the bit will be true 148 /// to indicate that this is a wildcard export. 149 typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl; 150 151 /// \brief The set of export declarations. 152 SmallVector<ExportDecl, 2> Exports; 153 154 /// \brief Describes an exported module that has not yet been resolved 155 /// (perhaps because the module it refers to has not yet been loaded). 156 struct UnresolvedExportDecl { 157 /// \brief The location of the 'export' keyword in the module map file. 158 SourceLocation ExportLoc; 159 160 /// \brief The name of the module. 161 ModuleId Id; 162 163 /// \brief Whether this export declaration ends in a wildcard, indicating 164 /// that all of its submodules should be exported (rather than the named 165 /// module itself). 166 bool Wildcard; 167 }; 168 169 /// \brief The set of export declarations that have yet to be resolved. 170 SmallVector<UnresolvedExportDecl, 2> UnresolvedExports; 171 172 /// \brief A library or framework to link against when an entity from this 173 /// module is used. 174 struct LinkLibrary { LinkLibraryLinkLibrary175 LinkLibrary() : IsFramework(false) { } LinkLibraryLinkLibrary176 LinkLibrary(const std::string &Library, bool IsFramework) 177 : Library(Library), IsFramework(IsFramework) { } 178 179 /// \brief The library to link against. 180 /// 181 /// This will typically be a library or framework name, but can also 182 /// be an absolute path to the library or framework. 183 std::string Library; 184 185 /// \brief Whether this is a framework rather than a library. 186 bool IsFramework; 187 }; 188 189 /// \brief The set of libraries or frameworks to link against when 190 /// an entity from this module is used. 191 llvm::SmallVector<LinkLibrary, 2> LinkLibraries; 192 193 /// \brief Construct a top-level module. Module(StringRef Name,SourceLocation DefinitionLoc,bool IsFramework)194 explicit Module(StringRef Name, SourceLocation DefinitionLoc, 195 bool IsFramework) 196 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0),Umbrella(),ASTFile(0), 197 IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework), 198 IsExplicit(false), IsSystem(false), 199 InferSubmodules(false), InferExplicitSubmodules(false), 200 InferExportWildcard(false), NameVisibility(Hidden) { } 201 202 /// \brief Construct a new module or submodule. 203 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 204 bool IsFramework, bool IsExplicit); 205 206 ~Module(); 207 208 /// \brief Determine whether this module is available for use within the 209 /// current translation unit. isAvailable()210 bool isAvailable() const { return IsAvailable; } 211 212 /// \brief Determine whether this module is available for use within the 213 /// current translation unit. 214 /// 215 /// \param LangOpts The language options used for the current 216 /// translation unit. 217 /// 218 /// \param Target The target options used for the current translation unit. 219 /// 220 /// \param Feature If this module is unavailable, this parameter 221 /// will be set to one of the features that is required for use of 222 /// this module (but is not available). 223 bool isAvailable(const LangOptions &LangOpts, 224 const TargetInfo &Target, 225 StringRef &Feature) const; 226 227 /// \brief Determine whether this module is a submodule. isSubModule()228 bool isSubModule() const { return Parent != 0; } 229 230 /// \brief Determine whether this module is a submodule of the given other 231 /// module. 232 bool isSubModuleOf(Module *Other) const; 233 234 /// \brief Determine whether this module is a part of a framework, 235 /// either because it is a framework module or because it is a submodule 236 /// of a framework module. isPartOfFramework()237 bool isPartOfFramework() const { 238 for (const Module *Mod = this; Mod; Mod = Mod->Parent) 239 if (Mod->IsFramework) 240 return true; 241 242 return false; 243 } 244 245 /// \brief Determine whether this module is a subframework of another 246 /// framework. isSubFramework()247 bool isSubFramework() const { 248 return IsFramework && Parent && Parent->isPartOfFramework(); 249 } 250 251 /// \brief Retrieve the full name of this module, including the path from 252 /// its top-level module. 253 std::string getFullModuleName() const; 254 255 /// \brief Retrieve the top-level module for this (sub)module, which may 256 /// be this module. getTopLevelModule()257 Module *getTopLevelModule() { 258 return const_cast<Module *>( 259 const_cast<const Module *>(this)->getTopLevelModule()); 260 } 261 262 /// \brief Retrieve the top-level module for this (sub)module, which may 263 /// be this module. 264 const Module *getTopLevelModule() const; 265 266 /// \brief Retrieve the name of the top-level module. 267 /// getTopLevelModuleName()268 StringRef getTopLevelModuleName() const { 269 return getTopLevelModule()->Name; 270 } 271 272 /// \brief The serialized AST file for this module, if one was created. getASTFile()273 const FileEntry *getASTFile() const { 274 return getTopLevelModule()->ASTFile; 275 } 276 277 /// \brief Set the serialized AST file for the top-level module of this module. setASTFile(const FileEntry * File)278 void setASTFile(const FileEntry *File) { 279 assert((getASTFile() == 0 || getASTFile() == File) && "file path changed"); 280 getTopLevelModule()->ASTFile = File; 281 } 282 283 /// \brief Retrieve the directory for which this module serves as the 284 /// umbrella. 285 const DirectoryEntry *getUmbrellaDir() const; 286 287 /// \brief Retrieve the header that serves as the umbrella header for this 288 /// module. getUmbrellaHeader()289 const FileEntry *getUmbrellaHeader() const { 290 return Umbrella.dyn_cast<const FileEntry *>(); 291 } 292 293 /// \brief Determine whether this module has an umbrella directory that is 294 /// not based on an umbrella header. hasUmbrellaDir()295 bool hasUmbrellaDir() const { 296 return Umbrella && Umbrella.is<const DirectoryEntry *>(); 297 } 298 299 /// \brief Add a top-level header associated with this module. addTopHeader(const FileEntry * File)300 void addTopHeader(const FileEntry *File) { 301 assert(File); 302 TopHeaders.insert(File); 303 } 304 305 /// \brief Add a top-level header filename associated with this module. addTopHeaderFilename(StringRef Filename)306 void addTopHeaderFilename(StringRef Filename) { 307 TopHeaderNames.push_back(Filename); 308 } 309 310 /// \brief The top-level headers associated with this module. 311 ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr); 312 313 /// \brief Add the given feature requirement to the list of features 314 /// required by this module. 315 /// 316 /// \param Feature The feature that is required by this module (and 317 /// its submodules). 318 /// 319 /// \param LangOpts The set of language options that will be used to 320 /// evaluate the availability of this feature. 321 /// 322 /// \param Target The target options that will be used to evaluate the 323 /// availability of this feature. 324 void addRequirement(StringRef Feature, const LangOptions &LangOpts, 325 const TargetInfo &Target); 326 327 /// \brief Find the submodule with the given name. 328 /// 329 /// \returns The submodule if found, or NULL otherwise. 330 Module *findSubmodule(StringRef Name) const; 331 332 typedef std::vector<Module *>::iterator submodule_iterator; 333 typedef std::vector<Module *>::const_iterator submodule_const_iterator; 334 submodule_begin()335 submodule_iterator submodule_begin() { return SubModules.begin(); } submodule_begin()336 submodule_const_iterator submodule_begin() const {return SubModules.begin();} submodule_end()337 submodule_iterator submodule_end() { return SubModules.end(); } submodule_end()338 submodule_const_iterator submodule_end() const { return SubModules.end(); } 339 340 /// \brief Returns the exported modules based on the wildcard restrictions. 341 void getExportedModules(SmallVectorImpl<Module *> &Exported) const; 342 getModuleInputBufferName()343 static StringRef getModuleInputBufferName() { 344 return "<module-includes>"; 345 } 346 347 /// \brief Print the module map for this module to the given stream. 348 /// 349 void print(raw_ostream &OS, unsigned Indent = 0) const; 350 351 /// \brief Dump the contents of this module to the given output stream. 352 void dump() const; 353 }; 354 355 } // end namespace clang 356 357 358 #endif // LLVM_CLANG_BASIC_MODULE_H 359