Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 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 | |
| 19 | Module::~Module() { |
| 20 | for (llvm::StringMap<Module *>::iterator I = SubModules.begin(), |
| 21 | IEnd = SubModules.end(); |
| 22 | I != IEnd; ++I) { |
| 23 | delete I->getValue(); |
| 24 | } |
| 25 | |
| 26 | } |
| 27 | |
| 28 | std::string Module::getFullModuleName() const { |
| 29 | llvm::SmallVector<StringRef, 2> Names; |
| 30 | |
| 31 | // Build up the set of module names (from innermost to outermost). |
| 32 | for (const Module *M = this; M; M = M->Parent) |
| 33 | Names.push_back(M->Name); |
| 34 | |
| 35 | std::string Result; |
| 36 | for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(), |
| 37 | IEnd = Names.rend(); |
| 38 | I != IEnd; ++I) { |
| 39 | if (!Result.empty()) |
| 40 | Result += '.'; |
| 41 | |
| 42 | Result += *I; |
| 43 | } |
| 44 | |
| 45 | return Result; |
| 46 | } |
| 47 | |
| 48 | StringRef Module::getTopLevelModuleName() const { |
| 49 | const Module *Top = this; |
| 50 | while (Top->Parent) |
| 51 | Top = Top->Parent; |
| 52 | |
| 53 | return Top->Name; |
| 54 | } |
| 55 | |
| 56 | void Module::print(llvm::raw_ostream &OS, unsigned Indent) const { |
| 57 | OS.indent(Indent); |
| 58 | if (IsFramework) |
| 59 | OS << "framework "; |
| 60 | if (IsExplicit) |
| 61 | OS << "explicit "; |
| 62 | OS << "module " << Name << " {\n"; |
| 63 | |
| 64 | if (UmbrellaHeader) { |
| 65 | OS.indent(Indent + 2); |
| 66 | OS << "umbrella \""; |
| 67 | OS.write_escaped(UmbrellaHeader->getName()); |
| 68 | OS << "\"\n"; |
| 69 | } |
| 70 | |
| 71 | for (unsigned I = 0, N = Headers.size(); I != N; ++I) { |
| 72 | OS.indent(Indent + 2); |
| 73 | OS << "header \""; |
| 74 | OS.write_escaped(Headers[I]->getName()); |
| 75 | OS << "\"\n"; |
| 76 | } |
| 77 | |
| 78 | for (llvm::StringMap<Module *>::const_iterator MI = SubModules.begin(), |
| 79 | MIEnd = SubModules.end(); |
| 80 | MI != MIEnd; ++MI) |
| 81 | MI->getValue()->print(OS, Indent + 2); |
| 82 | |
| 83 | OS.indent(Indent); |
| 84 | OS << "}\n"; |
| 85 | } |
| 86 | |
| 87 | void Module::dump() const { |
| 88 | print(llvm::errs()); |
| 89 | } |
| 90 | |
| 91 | |