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/DenseSet.h" 20 #include "llvm/ADT/PointerIntPair.h" 21 #include "llvm/ADT/PointerUnion.h" 22 #include "llvm/ADT/SetVector.h" 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/ADT/StringMap.h" 25 #include "llvm/ADT/StringRef.h" 26 #include <string> 27 #include <utility> 28 #include <vector> 29 30 namespace llvm { 31 class raw_ostream; 32 } 33 34 namespace clang { 35 36 class DirectoryEntry; 37 class FileEntry; 38 class FileManager; 39 class LangOptions; 40 class TargetInfo; 41 class IdentifierInfo; 42 43 /// \brief Describes the name of a module. 44 typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId; 45 46 /// \brief Describes a module or submodule. 47 class Module { 48 public: 49 /// \brief The name of this module. 50 std::string Name; 51 52 /// \brief The location of the module definition. 53 SourceLocation DefinitionLoc; 54 55 /// \brief The parent of this module. This will be NULL for the top-level 56 /// module. 57 Module *Parent; 58 59 /// \brief The umbrella header or directory. 60 llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella; 61 62 private: 63 /// \brief The submodules of this module, indexed by name. 64 std::vector<Module *> SubModules; 65 66 /// \brief A mapping from the submodule name to the index into the 67 /// \c SubModules vector at which that submodule resides. 68 llvm::StringMap<unsigned> SubModuleIndex; 69 70 /// \brief The AST file if this is a top-level module which has a 71 /// corresponding serialized AST file, or null otherwise. 72 const FileEntry *ASTFile; 73 74 /// \brief The top-level headers associated with this module. 75 llvm::SmallSetVector<const FileEntry *, 2> TopHeaders; 76 77 /// \brief top-level header filenames that aren't resolved to FileEntries yet. 78 std::vector<std::string> TopHeaderNames; 79 80 /// \brief Cache of modules visible to lookup in this module. 81 mutable llvm::DenseSet<const Module*> VisibleModulesCache; 82 83 public: 84 /// \brief The headers that are part of this module. 85 SmallVector<const FileEntry *, 2> NormalHeaders; 86 87 /// \brief The headers that are explicitly excluded from this module. 88 SmallVector<const FileEntry *, 2> ExcludedHeaders; 89 90 /// \brief The headers that are private to this module. 91 SmallVector<const FileEntry *, 2> PrivateHeaders; 92 93 /// \brief Information about a header directive as found in the module map 94 /// file. 95 struct HeaderDirective { 96 SourceLocation FileNameLoc; 97 std::string FileName; 98 bool IsUmbrella; 99 }; 100 101 /// \brief Headers that are mentioned in the module map file but could not be 102 /// found on the file system. 103 SmallVector<HeaderDirective, 1> MissingHeaders; 104 105 /// \brief An individual requirement: a feature name and a flag indicating 106 /// the required state of that feature. 107 typedef std::pair<std::string, bool> Requirement; 108 109 /// \brief The set of language features required to use this module. 110 /// 111 /// If any of these requirements are not available, the \c IsAvailable bit 112 /// will be false to indicate that this (sub)module is not available. 113 SmallVector<Requirement, 2> Requirements; 114 115 /// \brief Whether this module is available in the current 116 /// translation unit. 117 unsigned IsAvailable : 1; 118 119 /// \brief Whether this module was loaded from a module file. 120 unsigned IsFromModuleFile : 1; 121 122 /// \brief Whether this is a framework module. 123 unsigned IsFramework : 1; 124 125 /// \brief Whether this is an explicit submodule. 126 unsigned IsExplicit : 1; 127 128 /// \brief Whether this is a "system" module (which assumes that all 129 /// headers in it are system headers). 130 unsigned IsSystem : 1; 131 132 /// \brief Whether this is an 'extern "C"' module (which implicitly puts all 133 /// headers in it within an 'extern "C"' block, and allows the module to be 134 /// imported within such a block). 135 unsigned IsExternC : 1; 136 137 /// \brief Whether we should infer submodules for this module based on 138 /// the headers. 139 /// 140 /// Submodules can only be inferred for modules with an umbrella header. 141 unsigned InferSubmodules : 1; 142 143 /// \brief Whether, when inferring submodules, the inferred submodules 144 /// should be explicit. 145 unsigned InferExplicitSubmodules : 1; 146 147 /// \brief Whether, when inferring submodules, the inferr submodules should 148 /// export all modules they import (e.g., the equivalent of "export *"). 149 unsigned InferExportWildcard : 1; 150 151 /// \brief Whether the set of configuration macros is exhaustive. 152 /// 153 /// When the set of configuration macros is exhaustive, meaning 154 /// that no identifier not in this list should affect how the module is 155 /// built. 156 unsigned ConfigMacrosExhaustive : 1; 157 158 /// \brief Describes the visibility of the various names within a 159 /// particular module. 160 enum NameVisibilityKind { 161 /// \brief All of the names in this module are hidden. 162 /// 163 Hidden, 164 /// \brief Only the macro names in this module are visible. 165 MacrosVisible, 166 /// \brief All of the names in this module are visible. 167 AllVisible 168 }; 169 170 /// \brief The visibility of names within this particular module. 171 NameVisibilityKind NameVisibility; 172 173 /// \brief The location at which macros within this module became visible. 174 SourceLocation MacroVisibilityLoc; 175 176 /// \brief The location of the inferred submodule. 177 SourceLocation InferredSubmoduleLoc; 178 179 /// \brief The set of modules imported by this module, and on which this 180 /// module depends. 181 SmallVector<Module *, 2> Imports; 182 183 /// \brief Describes an exported module. 184 /// 185 /// The pointer is the module being re-exported, while the bit will be true 186 /// to indicate that this is a wildcard export. 187 typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl; 188 189 /// \brief The set of export declarations. 190 SmallVector<ExportDecl, 2> Exports; 191 192 /// \brief Describes an exported module that has not yet been resolved 193 /// (perhaps because the module it refers to has not yet been loaded). 194 struct UnresolvedExportDecl { 195 /// \brief The location of the 'export' keyword in the module map file. 196 SourceLocation ExportLoc; 197 198 /// \brief The name of the module. 199 ModuleId Id; 200 201 /// \brief Whether this export declaration ends in a wildcard, indicating 202 /// that all of its submodules should be exported (rather than the named 203 /// module itself). 204 bool Wildcard; 205 }; 206 207 /// \brief The set of export declarations that have yet to be resolved. 208 SmallVector<UnresolvedExportDecl, 2> UnresolvedExports; 209 210 /// \brief The directly used modules. 211 SmallVector<Module *, 2> DirectUses; 212 213 /// \brief The set of use declarations that have yet to be resolved. 214 SmallVector<ModuleId, 2> UnresolvedDirectUses; 215 216 /// \brief A library or framework to link against when an entity from this 217 /// module is used. 218 struct LinkLibrary { 219 LinkLibrary() : IsFramework(false) { } 220 LinkLibrary(const std::string &Library, bool IsFramework) 221 : Library(Library), IsFramework(IsFramework) { } 222 223 /// \brief The library to link against. 224 /// 225 /// This will typically be a library or framework name, but can also 226 /// be an absolute path to the library or framework. 227 std::string Library; 228 229 /// \brief Whether this is a framework rather than a library. 230 bool IsFramework; 231 }; 232 233 /// \brief The set of libraries or frameworks to link against when 234 /// an entity from this module is used. 235 llvm::SmallVector<LinkLibrary, 2> LinkLibraries; 236 237 /// \brief The set of "configuration macros", which are macros that 238 /// (intentionally) change how this module is built. 239 std::vector<std::string> ConfigMacros; 240 241 /// \brief An unresolved conflict with another module. 242 struct UnresolvedConflict { 243 /// \brief The (unresolved) module id. 244 ModuleId Id; 245 246 /// \brief The message provided to the user when there is a conflict. 247 std::string Message; 248 }; 249 250 /// \brief The list of conflicts for which the module-id has not yet been 251 /// resolved. 252 std::vector<UnresolvedConflict> UnresolvedConflicts; 253 254 /// \brief A conflict between two modules. 255 struct Conflict { 256 /// \brief The module that this module conflicts with. 257 Module *Other; 258 259 /// \brief The message provided to the user when there is a conflict. 260 std::string Message; 261 }; 262 263 /// \brief The list of conflicts. 264 std::vector<Conflict> Conflicts; 265 266 /// \brief Construct a new module or submodule. 267 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 268 bool IsFramework, bool IsExplicit); 269 270 ~Module(); 271 272 /// \brief Determine whether this module is available for use within the 273 /// current translation unit. 274 bool isAvailable() const { return IsAvailable; } 275 276 /// \brief Determine whether this module is available for use within the 277 /// current translation unit. 278 /// 279 /// \param LangOpts The language options used for the current 280 /// translation unit. 281 /// 282 /// \param Target The target options used for the current translation unit. 283 /// 284 /// \param Req If this module is unavailable, this parameter 285 /// will be set to one of the requirements that is not met for use of 286 /// this module. 287 bool isAvailable(const LangOptions &LangOpts, 288 const TargetInfo &Target, 289 Requirement &Req, 290 HeaderDirective &MissingHeader) const; 291 292 /// \brief Determine whether this module is a submodule. 293 bool isSubModule() const { return Parent != 0; } 294 295 /// \brief Determine whether this module is a submodule of the given other 296 /// module. 297 bool isSubModuleOf(Module *Other) const; 298 299 /// \brief Determine whether this module is a part of a framework, 300 /// either because it is a framework module or because it is a submodule 301 /// of a framework module. 302 bool isPartOfFramework() const { 303 for (const Module *Mod = this; Mod; Mod = Mod->Parent) 304 if (Mod->IsFramework) 305 return true; 306 307 return false; 308 } 309 310 /// \brief Determine whether this module is a subframework of another 311 /// framework. 312 bool isSubFramework() const { 313 return IsFramework && Parent && Parent->isPartOfFramework(); 314 } 315 316 /// \brief Retrieve the full name of this module, including the path from 317 /// its top-level module. 318 std::string getFullModuleName() const; 319 320 /// \brief Retrieve the top-level module for this (sub)module, which may 321 /// be this module. 322 Module *getTopLevelModule() { 323 return const_cast<Module *>( 324 const_cast<const Module *>(this)->getTopLevelModule()); 325 } 326 327 /// \brief Retrieve the top-level module for this (sub)module, which may 328 /// be this module. 329 const Module *getTopLevelModule() const; 330 331 /// \brief Retrieve the name of the top-level module. 332 /// 333 StringRef getTopLevelModuleName() const { 334 return getTopLevelModule()->Name; 335 } 336 337 /// \brief The serialized AST file for this module, if one was created. 338 const FileEntry *getASTFile() const { 339 return getTopLevelModule()->ASTFile; 340 } 341 342 /// \brief Set the serialized AST file for the top-level module of this module. 343 void setASTFile(const FileEntry *File) { 344 assert((getASTFile() == 0 || getASTFile() == File) && "file path changed"); 345 getTopLevelModule()->ASTFile = File; 346 } 347 348 /// \brief Retrieve the directory for which this module serves as the 349 /// umbrella. 350 const DirectoryEntry *getUmbrellaDir() const; 351 352 /// \brief Retrieve the header that serves as the umbrella header for this 353 /// module. 354 const FileEntry *getUmbrellaHeader() const { 355 return Umbrella.dyn_cast<const FileEntry *>(); 356 } 357 358 /// \brief Determine whether this module has an umbrella directory that is 359 /// not based on an umbrella header. 360 bool hasUmbrellaDir() const { 361 return Umbrella && Umbrella.is<const DirectoryEntry *>(); 362 } 363 364 /// \brief Add a top-level header associated with this module. 365 void addTopHeader(const FileEntry *File) { 366 assert(File); 367 TopHeaders.insert(File); 368 } 369 370 /// \brief Add a top-level header filename associated with this module. 371 void addTopHeaderFilename(StringRef Filename) { 372 TopHeaderNames.push_back(Filename); 373 } 374 375 /// \brief The top-level headers associated with this module. 376 ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr); 377 378 /// \brief Add the given feature requirement to the list of features 379 /// required by this module. 380 /// 381 /// \param Feature The feature that is required by this module (and 382 /// its submodules). 383 /// 384 /// \param RequiredState The required state of this feature: \c true 385 /// if it must be present, \c false if it must be absent. 386 /// 387 /// \param LangOpts The set of language options that will be used to 388 /// evaluate the availability of this feature. 389 /// 390 /// \param Target The target options that will be used to evaluate the 391 /// availability of this feature. 392 void addRequirement(StringRef Feature, bool RequiredState, 393 const LangOptions &LangOpts, 394 const TargetInfo &Target); 395 396 /// \brief Find the submodule with the given name. 397 /// 398 /// \returns The submodule if found, or NULL otherwise. 399 Module *findSubmodule(StringRef Name) const; 400 401 /// \brief Determine whether the specified module would be visible to 402 /// a lookup at the end of this module. 403 bool isModuleVisible(const Module *M) const { 404 if (VisibleModulesCache.empty()) 405 buildVisibleModulesCache(); 406 return VisibleModulesCache.count(M); 407 } 408 409 typedef std::vector<Module *>::iterator submodule_iterator; 410 typedef std::vector<Module *>::const_iterator submodule_const_iterator; 411 412 submodule_iterator submodule_begin() { return SubModules.begin(); } 413 submodule_const_iterator submodule_begin() const {return SubModules.begin();} 414 submodule_iterator submodule_end() { return SubModules.end(); } 415 submodule_const_iterator submodule_end() const { return SubModules.end(); } 416 417 /// \brief Appends this module's list of exported modules to \p Exported. 418 /// 419 /// This provides a subset of immediately imported modules (the ones that are 420 /// directly exported), not the complete set of exported modules. 421 void getExportedModules(SmallVectorImpl<Module *> &Exported) const; 422 423 static StringRef getModuleInputBufferName() { 424 return "<module-includes>"; 425 } 426 427 /// \brief Print the module map for this module to the given stream. 428 /// 429 void print(raw_ostream &OS, unsigned Indent = 0) const; 430 431 /// \brief Dump the contents of this module to the given output stream. 432 void dump() const; 433 434 private: 435 void buildVisibleModulesCache() const; 436 }; 437 438 } // end namespace clang 439 440 441 #endif // LLVM_CLANG_BASIC_MODULE_H 442