Module.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "llvm/Support/raw_ostream.h"
  17. using namespace clang;
  18. Module::~Module() {
  19. for (llvm::StringMap<Module *>::iterator I = SubModules.begin(),
  20. IEnd = SubModules.end();
  21. I != IEnd; ++I) {
  22. delete I->getValue();
  23. }
  24. }
  25. bool Module::isSubModuleOf(Module *Other) const {
  26. const Module *This = this;
  27. do {
  28. if (This == Other)
  29. return true;
  30. This = This->Parent;
  31. } while (This);
  32. return false;
  33. }
  34. std::string Module::getFullModuleName() const {
  35. llvm::SmallVector<StringRef, 2> Names;
  36. // Build up the set of module names (from innermost to outermost).
  37. for (const Module *M = this; M; M = M->Parent)
  38. Names.push_back(M->Name);
  39. std::string Result;
  40. for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
  41. IEnd = Names.rend();
  42. I != IEnd; ++I) {
  43. if (!Result.empty())
  44. Result += '.';
  45. Result += *I;
  46. }
  47. return Result;
  48. }
  49. StringRef Module::getTopLevelModuleName() const {
  50. const Module *Top = this;
  51. while (Top->Parent)
  52. Top = Top->Parent;
  53. return Top->Name;
  54. }
  55. static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
  56. for (unsigned I = 0, N = Id.size(); I != N; ++I) {
  57. if (I)
  58. OS << ".";
  59. OS << Id[I].first;
  60. }
  61. }
  62. void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
  63. OS.indent(Indent);
  64. if (IsFramework)
  65. OS << "framework ";
  66. if (IsExplicit)
  67. OS << "explicit ";
  68. OS << "module " << Name << " {\n";
  69. if (UmbrellaHeader) {
  70. OS.indent(Indent + 2);
  71. OS << "umbrella \"";
  72. OS.write_escaped(UmbrellaHeader->getName());
  73. OS << "\"\n";
  74. }
  75. for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
  76. OS.indent(Indent + 2);
  77. OS << "header \"";
  78. OS.write_escaped(Headers[I]->getName());
  79. OS << "\"\n";
  80. }
  81. for (llvm::StringMap<Module *>::const_iterator MI = SubModules.begin(),
  82. MIEnd = SubModules.end();
  83. MI != MIEnd; ++MI)
  84. MI->getValue()->print(OS, Indent + 2);
  85. for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
  86. OS.indent(Indent + 2);
  87. OS << "export " << Exports[I].getPointer()->getFullModuleName();
  88. if (Exports[I].getInt())
  89. OS << ".*";
  90. OS << "\n";
  91. }
  92. for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
  93. OS.indent(Indent + 2);
  94. OS << "export ";
  95. printModuleId(OS, UnresolvedExports[I].Id);
  96. if (UnresolvedExports[I].Wildcard)
  97. OS << ".*";
  98. OS << "\n";
  99. }
  100. OS.indent(Indent);
  101. OS << "}\n";
  102. }
  103. void Module::dump() const {
  104. print(llvm::errs());
  105. }