Module.h revision 6a1db484f32eb791840dd55a8d45c86ff5bd0834
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 26namespace llvm { 27 class raw_ostream; 28} 29 30namespace clang { 31 32class FileEntry; 33class DirectoryEntry; 34 35/// \brief Describes the name of a module. 36typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2> 37 ModuleId; 38 39/// \brief Describes a module or submodule. 40class Module { 41public: 42 /// \brief The name of this module. 43 std::string Name; 44 45 /// \brief The location of the module definition. 46 SourceLocation DefinitionLoc; 47 48 /// \brief The parent of this module. This will be NULL for the top-level 49 /// module. 50 Module *Parent; 51 52 /// \brief The umbrella header or directory. 53 llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella; 54 55 /// \brief The submodules of this module, indexed by name. 56 llvm::StringMap<Module *> SubModules; 57 58 /// \brief The headers that are part of this module. 59 llvm::SmallVector<const FileEntry *, 2> Headers; 60 61 /// \brief Whether this is a framework module. 62 unsigned IsFramework : 1; 63 64 /// \brief Whether this is an explicit submodule. 65 unsigned IsExplicit : 1; 66 67 /// \brief Whether we should infer submodules for this module based on 68 /// the headers. 69 /// 70 /// Submodules can only be inferred for modules with an umbrella header. 71 unsigned InferSubmodules : 1; 72 73 /// \brief Whether, when inferring submodules, the inferred submodules 74 /// should be explicit. 75 unsigned InferExplicitSubmodules : 1; 76 77 /// \brief Whether, when inferring submodules, the inferr submodules should 78 /// export all modules they import (e.g., the equivalent of "export *"). 79 unsigned InferExportWildcard : 1; 80 81 /// \brief Describes the visibility of the various names within a 82 /// particular module. 83 enum NameVisibilityKind { 84 /// \brief All of the names in this module are hidden. 85 /// 86 Hidden, 87 /// \brief Only the macro names in this module are visible. 88 MacrosVisible, 89 /// \brief All of the names in this module are visible. 90 AllVisible 91 }; 92 93 ///\ brief The visibility of names within this particular module. 94 NameVisibilityKind NameVisibility; 95 96 /// \brief The location of the inferred submodule. 97 SourceLocation InferredSubmoduleLoc; 98 99 /// \brief The set of modules imported by this module, and on which this 100 /// module depends. 101 llvm::SmallVector<Module *, 2> Imports; 102 103 /// \brief Describes an exported module. 104 /// 105 /// The pointer is the module being re-exported, while the bit will be true 106 /// to indicate that this is a wildcard export. 107 typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl; 108 109 /// \brief The set of export declarations. 110 llvm::SmallVector<ExportDecl, 2> Exports; 111 112 /// \brief Describes an exported module that has not yet been resolved 113 /// (perhaps because tASThe module it refers to has not yet been loaded). 114 struct UnresolvedExportDecl { 115 /// \brief The location of the 'export' keyword in the module map file. 116 SourceLocation ExportLoc; 117 118 /// \brief The name of the module. 119 ModuleId Id; 120 121 /// \brief Whether this export declaration ends in a wildcard, indicating 122 /// that all of its submodules should be exported (rather than the named 123 /// module itself). 124 bool Wildcard; 125 }; 126 127 /// \brief The set of export declarations that have yet to be resolved. 128 llvm::SmallVector<UnresolvedExportDecl, 2> UnresolvedExports; 129 130 /// \brief Construct a top-level module. 131 explicit Module(StringRef Name, SourceLocation DefinitionLoc, 132 bool IsFramework) 133 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(0), Umbrella(), 134 IsFramework(IsFramework), IsExplicit(false), InferSubmodules(false), 135 InferExplicitSubmodules(false), InferExportWildcard(false), 136 NameVisibility(Hidden) { } 137 138 /// \brief Construct a new module or submodule. 139 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 140 bool IsFramework, bool IsExplicit) 141 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), 142 Umbrella(), IsFramework(IsFramework), IsExplicit(IsExplicit), 143 InferSubmodules(false), InferExplicitSubmodules(false), 144 InferExportWildcard(false),NameVisibility(Hidden) { } 145 146 ~Module(); 147 148 /// \brief Determine whether this module is a submodule. 149 bool isSubModule() const { return Parent != 0; } 150 151 /// \brief Determine whether this module is a submodule of the given other 152 /// module. 153 bool isSubModuleOf(Module *Other) const; 154 155 /// \brief Determine whether this module is a part of a framework, 156 /// either because it is a framework module or because it is a submodule 157 /// of a framework module. 158 bool isPartOfFramework() const { 159 for (const Module *Mod = this; Mod; Mod = Mod->Parent) 160 if (Mod->IsFramework) 161 return true; 162 163 return false; 164 } 165 166 /// \brief Retrieve the full name of this module, including the path from 167 /// its top-level module. 168 std::string getFullModuleName() const; 169 170 /// \brief Retrieve the top-level module for this (sub)module, which may 171 /// be this module. 172 Module *getTopLevelModule() { 173 return const_cast<Module *>( 174 const_cast<const Module *>(this)->getTopLevelModule()); 175 } 176 177 /// \brief Retrieve the top-level module for this (sub)module, which may 178 /// be this module. 179 const Module *getTopLevelModule() const; 180 181 /// \brief Retrieve the name of the top-level module. 182 /// 183 StringRef getTopLevelModuleName() const { 184 return getTopLevelModule()->Name; 185 } 186 187 /// \brief Retrieve the directory for which this module serves as the 188 /// umbrella. 189 const DirectoryEntry *getUmbrellaDir() const; 190 191 /// \brief Retrieve the header that serves as the umbrella header for this 192 /// module. 193 const FileEntry *getUmbrellaHeader() const { 194 return Umbrella.dyn_cast<const FileEntry *>(); 195 } 196 197 /// \brief Determine whether this module has an umbrella directory that is 198 /// not based on an umbrella header. 199 bool hasUmbrellaDir() const { 200 return Umbrella && Umbrella.is<const DirectoryEntry *>(); 201 } 202 203 /// \brief Print the module map for this module to the given stream. 204 /// 205 void print(llvm::raw_ostream &OS, unsigned Indent = 0) const; 206 207 /// \brief Dump the contents of this module to the given output stream. 208 void dump() const; 209}; 210 211} // end namespace clang 212 213 214#endif // LLVM_CLANG_BASIC_MODULE_H 215