Module.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. #include "clang/Basic/Module.h"
  15. #include "clang/Basic/FileManager.h"
  16. #include "clang/Basic/LangOptions.h"
  17. #include "llvm/Support/ErrorHandling.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include "llvm/ADT/SmallVector.h"
  20. #include "llvm/ADT/StringSwitch.h"
  21. using namespace clang;
  22. Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
  23. bool IsFramework, bool IsExplicit)
  24. : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
  25. Umbrella(), IsAvailable(true), IsFromModuleFile(false),
  26. IsFramework(IsFramework), IsExplicit(IsExplicit), InferSubmodules(false),
  27. InferExplicitSubmodules(false), InferExportWildcard(false),
  28. NameVisibility(Hidden)
  29. {
  30. if (Parent) {
  31. if (!Parent->isAvailable())
  32. IsAvailable = false;
  33. Parent->SubModuleIndex[Name] = Parent->SubModules.size();
  34. Parent->SubModules.push_back(this);
  35. }
  36. }
  37. Module::~Module() {
  38. for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
  39. I != IEnd; ++I) {
  40. delete *I;
  41. }
  42. }
  43. /// \brief Determine whether a translation unit built using the current
  44. /// language options has the given feature.
  45. static bool hasFeature(StringRef Feature, const LangOptions &LangOpts) {
  46. return llvm::StringSwitch<bool>(Feature)
  47. .Case("blocks", LangOpts.Blocks)
  48. .Case("cplusplus", LangOpts.CPlusPlus)
  49. .Case("cplusplus11", LangOpts.CPlusPlus0x)
  50. .Case("objc", LangOpts.ObjC1)
  51. .Case("objc_arc", LangOpts.ObjCAutoRefCount)
  52. .Default(false);
  53. }
  54. bool
  55. Module::isAvailable(const LangOptions &LangOpts, StringRef &Feature) const {
  56. if (IsAvailable)
  57. return true;
  58. for (const Module *Current = this; Current; Current = Current->Parent) {
  59. for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
  60. if (!hasFeature(Current->Requires[I], LangOpts)) {
  61. Feature = Current->Requires[I];
  62. return false;
  63. }
  64. }
  65. }
  66. llvm_unreachable("could not find a reason why module is unavailable");
  67. }
  68. bool Module::isSubModuleOf(Module *Other) const {
  69. const Module *This = this;
  70. do {
  71. if (This == Other)
  72. return true;
  73. This = This->Parent;
  74. } while (This);
  75. return false;
  76. }
  77. const Module *Module::getTopLevelModule() const {
  78. const Module *Result = this;
  79. while (Result->Parent)
  80. Result = Result->Parent;
  81. return Result;
  82. }
  83. std::string Module::getFullModuleName() const {
  84. llvm::SmallVector<StringRef, 2> Names;
  85. // Build up the set of module names (from innermost to outermost).
  86. for (const Module *M = this; M; M = M->Parent)
  87. Names.push_back(M->Name);
  88. std::string Result;
  89. for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
  90. IEnd = Names.rend();
  91. I != IEnd; ++I) {
  92. if (!Result.empty())
  93. Result += '.';
  94. Result += *I;
  95. }
  96. return Result;
  97. }
  98. const DirectoryEntry *Module::getUmbrellaDir() const {
  99. if (const FileEntry *Header = getUmbrellaHeader())
  100. return Header->getDir();
  101. return Umbrella.dyn_cast<const DirectoryEntry *>();
  102. }
  103. void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts) {
  104. Requires.push_back(Feature);
  105. // If this feature is currently available, we're done.
  106. if (hasFeature(Feature, LangOpts))
  107. return;
  108. if (!IsAvailable)
  109. return;
  110. llvm::SmallVector<Module *, 2> Stack;
  111. Stack.push_back(this);
  112. while (!Stack.empty()) {
  113. Module *Current = Stack.back();
  114. Stack.pop_back();
  115. if (!Current->IsAvailable)
  116. continue;
  117. Current->IsAvailable = false;
  118. for (submodule_iterator Sub = Current->submodule_begin(),
  119. SubEnd = Current->submodule_end();
  120. Sub != SubEnd; ++Sub) {
  121. if ((*Sub)->IsAvailable)
  122. Stack.push_back(*Sub);
  123. }
  124. }
  125. }
  126. Module *Module::findSubmodule(StringRef Name) const {
  127. llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
  128. if (Pos == SubModuleIndex.end())
  129. return 0;
  130. return SubModules[Pos->getValue()];
  131. }
  132. static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
  133. for (unsigned I = 0, N = Id.size(); I != N; ++I) {
  134. if (I)
  135. OS << ".";
  136. OS << Id[I].first;
  137. }
  138. }
  139. void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
  140. OS.indent(Indent);
  141. if (IsFramework)
  142. OS << "framework ";
  143. if (IsExplicit)
  144. OS << "explicit ";
  145. OS << "module " << Name << " {\n";
  146. if (!Requires.empty()) {
  147. OS.indent(Indent + 2);
  148. OS << "requires ";
  149. for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
  150. if (I)
  151. OS << ", ";
  152. OS << Requires[I];
  153. }
  154. OS << "\n";
  155. }
  156. if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
  157. OS.indent(Indent + 2);
  158. OS << "umbrella header \"";
  159. OS.write_escaped(UmbrellaHeader->getName());
  160. OS << "\"\n";
  161. } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
  162. OS.indent(Indent + 2);
  163. OS << "umbrella \"";
  164. OS.write_escaped(UmbrellaDir->getName());
  165. OS << "\"\n";
  166. }
  167. for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
  168. OS.indent(Indent + 2);
  169. OS << "header \"";
  170. OS.write_escaped(Headers[I]->getName());
  171. OS << "\"\n";
  172. }
  173. for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
  174. MI != MIEnd; ++MI)
  175. (*MI)->print(OS, Indent + 2);
  176. for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
  177. OS.indent(Indent + 2);
  178. OS << "export ";
  179. if (Module *Restriction = Exports[I].getPointer()) {
  180. OS << Restriction->getFullModuleName();
  181. if (Exports[I].getInt())
  182. OS << ".*";
  183. } else {
  184. OS << "*";
  185. }
  186. OS << "\n";
  187. }
  188. for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
  189. OS.indent(Indent + 2);
  190. OS << "export ";
  191. printModuleId(OS, UnresolvedExports[I].Id);
  192. if (UnresolvedExports[I].Wildcard) {
  193. if (UnresolvedExports[I].Id.empty())
  194. OS << "*";
  195. else
  196. OS << ".*";
  197. }
  198. OS << "\n";
  199. }
  200. if (InferSubmodules) {
  201. OS.indent(Indent + 2);
  202. if (InferExplicitSubmodules)
  203. OS << "explicit ";
  204. OS << "module * {\n";
  205. if (InferExportWildcard) {
  206. OS.indent(Indent + 4);
  207. OS << "export *\n";
  208. }
  209. OS.indent(Indent + 2);
  210. OS << "}\n";
  211. }
  212. OS.indent(Indent);
  213. OS << "}\n";
  214. }
  215. void Module::dump() const {
  216. print(llvm::errs());
  217. }