blob: 97bddfa97ea10feeb095ee42f952bb3bf4978a58 [file] [log] [blame]
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001//===--- 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"
17using namespace clang;
18
19Module::~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
Douglas Gregor0adaa882011-12-05 17:28:06 +000028bool Module::isSubModuleOf(Module *Other) const {
29 const Module *This = this;
30 do {
31 if (This == Other)
32 return true;
33
34 This = This->Parent;
35 } while (This);
36
37 return false;
38}
39
Douglas Gregor1e123682011-12-05 22:27:44 +000040const Module *Module::getTopLevelModule() const {
41 const Module *Result = this;
42 while (Result->Parent)
43 Result = Result->Parent;
44
45 return Result;
46}
47
Douglas Gregor1a4761e2011-11-30 23:21:26 +000048std::string Module::getFullModuleName() const {
49 llvm::SmallVector<StringRef, 2> Names;
50
51 // Build up the set of module names (from innermost to outermost).
52 for (const Module *M = this; M; M = M->Parent)
53 Names.push_back(M->Name);
54
55 std::string Result;
56 for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
57 IEnd = Names.rend();
58 I != IEnd; ++I) {
59 if (!Result.empty())
60 Result += '.';
61
62 Result += *I;
63 }
64
65 return Result;
66}
67
Douglas Gregoraf13bfc2011-12-02 18:58:38 +000068static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
69 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
70 if (I)
71 OS << ".";
72 OS << Id[I].first;
73 }
74}
75
Douglas Gregor1a4761e2011-11-30 23:21:26 +000076void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
77 OS.indent(Indent);
78 if (IsFramework)
79 OS << "framework ";
80 if (IsExplicit)
81 OS << "explicit ";
82 OS << "module " << Name << " {\n";
83
84 if (UmbrellaHeader) {
85 OS.indent(Indent + 2);
86 OS << "umbrella \"";
87 OS.write_escaped(UmbrellaHeader->getName());
88 OS << "\"\n";
89 }
90
91 for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
92 OS.indent(Indent + 2);
93 OS << "header \"";
94 OS.write_escaped(Headers[I]->getName());
95 OS << "\"\n";
96 }
97
98 for (llvm::StringMap<Module *>::const_iterator MI = SubModules.begin(),
Douglas Gregor1e123682011-12-05 22:27:44 +000099 MIEnd = SubModules.end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000100 MI != MIEnd; ++MI)
101 MI->getValue()->print(OS, Indent + 2);
102
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000103 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
104 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000105 OS << "export ";
106 if (Module *Restriction = Exports[I].getPointer()) {
107 OS << Restriction->getFullModuleName();
108 if (Exports[I].getInt())
109 OS << ".*";
110 } else {
111 OS << "*";
112 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000113 OS << "\n";
114 }
115
116 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
117 OS.indent(Indent + 2);
118 OS << "export ";
119 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000120 if (UnresolvedExports[I].Wildcard) {
121 if (UnresolvedExports[I].Id.empty())
122 OS << "*";
123 else
124 OS << ".*";
125 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000126 OS << "\n";
127 }
128
Douglas Gregor1e123682011-12-05 22:27:44 +0000129 if (InferSubmodules) {
130 OS.indent(Indent + 2);
131 if (InferExplicitSubmodules)
132 OS << "explicit ";
133 OS << "module * {\n";
134 if (InferExportWildcard) {
135 OS.indent(Indent + 4);
136 OS << "export *\n";
137 }
138 OS.indent(Indent + 2);
139 OS << "}\n";
140 }
141
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000142 OS.indent(Indent);
143 OS << "}\n";
144}
145
146void Module::dump() const {
147 print(llvm::errs());
148}
149
150