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