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 // This file defines the Module class, which describes a module in the source 11 // code. 12 // 13 //===----------------------------------------------------------------------===// 14 #ifndef LLVM_CLANG_BASIC_MODULE_H 15 #define LLVM_CLANG_BASIC_MODULE_H 16 17 #include "clang/Basic/SourceLocation.h" 18 #include "llvm/ADT/PointerIntPair.h" 19 #include "llvm/ADT/PointerUnion.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringMap.h" 22 #include "llvm/ADT/StringRef.h" 23 #include <string> 24 #include <utility> 25 #include <vector> 26 27 namespace llvm { 28 class raw_ostream; 29 } 30 31 namespace clang { 32 33 class DirectoryEntry; 34 class FileEntry; 35 class LangOptions; 36 class TargetInfo; 37 38 /// \brief Describes the name of a module. 39 typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2> 40 ModuleId; 41 42 /// \brief Describes a module or submodule. 43 class Module { 44 public: 45 /// \brief The name of this module. 46 std::string Name; 47 48 /// \brief The location of the module definition. 49 SourceLocation DefinitionLoc; 50 51 /// \brief The parent of this module. This will be NULL for the top-level 52 /// module. 53 Module *Parent; 54 55 /// \brief The umbrella header or directory. 56 llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella; 57 58 private: 59 /// \brief The submodules of this module, indexed by name. 60 std::vector<Module *> SubModules; 61 62 /// \brief A mapping from the submodule name to the index into the 63 /// \c SubModules vector at which that submodule resides. 64 llvm::StringMap<unsigned> SubModuleIndex; 65 66 public: 67 /// \brief The headers that are part of this module. 68 llvm::SmallVector<const FileEntry *, 2> Headers; 69 70 /// \brief The set of language features required to use this module. 71 /// 72 /// If any of these features is not present, the \c IsAvailable bit 73 /// will be false to indicate that this (sub)module is not 74 /// available. 75 llvm::SmallVector<std::string, 2> Requires; 76 77 /// \brief Whether this module is available in the current 78 /// translation unit. 79 unsigned IsAvailable : 1; 80 81 /// \brief Whether this module was loaded from a module file. 82 unsigned IsFromModuleFile : 1; 83 84 /// \brief Whether this is a framework module. 85 unsigned IsFramework : 1; 86 87 /// \brief Whether this is an explicit submodule. 88 unsigned IsExplicit : 1; 89 90 /// \brief Whether this is a "system" module (which assumes that all 91 /// headers in it are system headers). 92 unsigned IsSystem : 1; 93 94 /// \brief Whether we should infer submodules for this module based on 95 /// the headers. 96 /// 97 /// Submodules can only be inferred for modules with an umbrella header. 98 unsigned InferSubmodules : 1; 99 100 /// \brief Whether, when inferring submodules, the inferred submodules 101 /// should be explicit. 102 unsigned InferExplicitSubmodules : 1; 103 104 /// \brief Whether, when inferring submodules, the inferr submodules should 105 /// export all modules they import (e.g., the equivalent of "export *"). 106 unsigned InferExportWildcard : 1; 107 108 /// \brief Describes the visibility of the various names within a 109 /// particular module. 110 enum NameVisibilityKind { 111 /// \brief All of the names in this module are hidden. 112 /// 113 Hidden, 114 /// \brief Only the macro names in this module are visible. 115 MacrosVisible, 116 /// \brief All of the names in this module are visible. 117 AllVisible 118 }; 119 120 ///\ brief The visibility of names within this particular module. 121 NameVisibilityKind NameVisibility; 122 123 /// \brief The location of the inferred submodule. 124 SourceLocation InferredSubmoduleLoc; 125 126 /// \brief The set of modules imported by this module, and on which this 127 /// module depends. 128 llvm::SmallVector<Module *, 2> Imports; 129 130 /// \brief Describes an exported module. 131 /// 132 /// The pointer is the module being re-exported, while the bit will be true 133 /// to indicate that this is a wildcard export. 134 typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl; 135 136 /// \brief The set of export declarations. 137 llvm::SmallVector<ExportDecl, 2> Exports; 138 139 /// \brief Describes an exported module that has not yet been resolved 140 /// (perhaps because tASThe module it refers to has not yet been loaded). 141 struct UnresolvedExportDecl { 142 /// \brief The location of the 'export' keyword in the module map file. 143 SourceLocation ExportLoc; 144 145 /// \brief The name of the module. 146 ModuleId Id; 147 148 /// \brief Whether this export declaration ends in a wildcard, indicating 149 /// that all of its submodules should be exported (rather than the named 150 /// module itself). 151 bool Wildcard; 152 }; 153 154 /// \brief The set of export declarations that have yet to be resolved. 155 llvm::SmallVector<UnresolvedExportDecl, 2> UnresolvedExports; 156 157 /// \brief Construct a top-level module. Module(StringRef Name,SourceLocation DefinitionLoc,bool IsFramework)158 explicit Module(StringRef Name, SourceLocation DefinitionLoc, 159 bool IsFramework) 160 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), Umbrella(), 161 IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework), 162 IsExplicit(false), IsSystem(false), 163 InferSubmodules(false), InferExplicitSubmodules(false), 164 InferExportWildcard(false), NameVisibility(Hidden) { } 165 166 /// \brief Construct a new module or submodule. 167 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 168 bool IsFramework, bool IsExplicit); 169 170 ~Module(); 171 172 /// \brief Determine whether this module is available for use within the 173 /// current translation unit. isAvailable()174 bool isAvailable() const { return IsAvailable; } 175 176 /// \brief Determine whether this module is available for use within the 177 /// current translation unit. 178 /// 179 /// \param LangOpts The language options used for the current 180 /// translation unit. 181 /// 182 /// \param Target The target options used for the current translation unit. 183 /// 184 /// \param Feature If this module is unavailable, this parameter 185 /// will be set to one of the features that is required for use of 186 /// this module (but is not available). 187 bool isAvailable(const LangOptions &LangOpts, 188 const TargetInfo &Target, 189 StringRef &Feature) const; 190 191 /// \brief Determine whether this module is a submodule. isSubModule()192 bool isSubModule() const { return Parent != 0; } 193 194 /// \brief Determine whether this module is a submodule of the given other 195 /// module. 196 bool isSubModuleOf(Module *Other) const; 197 198 /// \brief Determine whether this module is a part of a framework, 199 /// either because it is a framework module or because it is a submodule 200 /// of a framework module. isPartOfFramework()201 bool isPartOfFramework() const { 202 for (const Module *Mod = this; Mod; Mod = Mod->Parent) 203 if (Mod->IsFramework) 204 return true; 205 206 return false; 207 } 208 209 /// \brief Retrieve the full name of this module, including the path from 210 /// its top-level module. 211 std::string getFullModuleName() const; 212 213 /// \brief Retrieve the top-level module for this (sub)module, which may 214 /// be this module. getTopLevelModule()215 Module *getTopLevelModule() { 216 return const_cast<Module *>( 217 const_cast<const Module *>(this)->getTopLevelModule()); 218 } 219 220 /// \brief Retrieve the top-level module for this (sub)module, which may 221 /// be this module. 222 const Module *getTopLevelModule() const; 223 224 /// \brief Retrieve the name of the top-level module. 225 /// getTopLevelModuleName()226 StringRef getTopLevelModuleName() const { 227 return getTopLevelModule()->Name; 228 } 229 230 /// \brief Retrieve the directory for which this module serves as the 231 /// umbrella. 232 const DirectoryEntry *getUmbrellaDir() const; 233 234 /// \brief Retrieve the header that serves as the umbrella header for this 235 /// module. getUmbrellaHeader()236 const FileEntry *getUmbrellaHeader() const { 237 return Umbrella.dyn_cast<const FileEntry *>(); 238 } 239 240 /// \brief Determine whether this module has an umbrella directory that is 241 /// not based on an umbrella header. hasUmbrellaDir()242 bool hasUmbrellaDir() const { 243 return Umbrella && Umbrella.is<const DirectoryEntry *>(); 244 } 245 246 /// \briaf Add the given feature requirement to the list of features 247 /// required by this module. 248 /// 249 /// \param Feature The feature that is required by this module (and 250 /// its submodules). 251 /// 252 /// \param LangOpts The set of language options that will be used to 253 /// evaluate the availability of this feature. 254 /// 255 /// \param Target The target options that will be used to evaluate the 256 /// availability of this feature. 257 void addRequirement(StringRef Feature, const LangOptions &LangOpts, 258 const TargetInfo &Target); 259 260 /// \brief Find the submodule with the given name. 261 /// 262 /// \returns The submodule if found, or NULL otherwise. 263 Module *findSubmodule(StringRef Name) const; 264 265 typedef std::vector<Module *>::iterator submodule_iterator; 266 typedef std::vector<Module *>::const_iterator submodule_const_iterator; 267 submodule_begin()268 submodule_iterator submodule_begin() { return SubModules.begin(); } submodule_begin()269 submodule_const_iterator submodule_begin() const {return SubModules.begin();} submodule_end()270 submodule_iterator submodule_end() { return SubModules.end(); } submodule_end()271 submodule_const_iterator submodule_end() const { return SubModules.end(); } 272 273 /// \brief Print the module map for this module to the given stream. 274 /// 275 void print(llvm::raw_ostream &OS, unsigned Indent = 0) const; 276 277 /// \brief Dump the contents of this module to the given output stream. 278 void dump() const; 279 }; 280 281 } // end namespace clang 282 283 284 #endif // LLVM_CLANG_BASIC_MODULE_H 285