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