blob: feec5f0258c1a6e37abc09446b84ffcd94ccf499 [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"
Douglas Gregor51f564f2011-12-31 04:05:44 +000016#include "clang/Basic/LangOptions.h"
17#include "llvm/Support/ErrorHandling.h"
Douglas Gregor1a4761e2011-11-30 23:21:26 +000018#include "llvm/Support/raw_ostream.h"
Douglas Gregor51f564f2011-12-31 04:05:44 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringSwitch.h"
Douglas Gregor1a4761e2011-11-30 23:21:26 +000021using namespace clang;
22
23Module::~Module() {
24 for (llvm::StringMap<Module *>::iterator I = SubModules.begin(),
25 IEnd = SubModules.end();
26 I != IEnd; ++I) {
27 delete I->getValue();
28 }
29
30}
31
Douglas Gregor51f564f2011-12-31 04:05:44 +000032/// \brief Determine whether a translation unit built using the current
33/// language options has the given feature.
34static bool hasFeature(StringRef Feature, const LangOptions &LangOpts) {
35 return llvm::StringSwitch<bool>(Feature)
36 .Case("blocks", LangOpts.Blocks)
37 .Case("cplusplus", LangOpts.CPlusPlus)
38 .Case("cplusplus11", LangOpts.CPlusPlus0x)
39 .Case("objc", LangOpts.ObjC1)
40 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
41 .Default(false);
42}
43
44bool
45Module::isAvailable(const LangOptions &LangOpts, StringRef &Feature) const {
46 if (IsAvailable)
47 return true;
48
49 for (const Module *Current = this; Current; Current = Current->Parent) {
50 for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
51 if (!hasFeature(Current->Requires[I], LangOpts)) {
52 Feature = Current->Requires[I];
53 return false;
54 }
55 }
56 }
57
58 llvm_unreachable("could not find a reason why module is unavailable");
59 return false;
60}
61
Douglas Gregor0adaa882011-12-05 17:28:06 +000062bool Module::isSubModuleOf(Module *Other) const {
63 const Module *This = this;
64 do {
65 if (This == Other)
66 return true;
67
68 This = This->Parent;
69 } while (This);
70
71 return false;
72}
73
Douglas Gregor1e123682011-12-05 22:27:44 +000074const Module *Module::getTopLevelModule() const {
75 const Module *Result = this;
76 while (Result->Parent)
77 Result = Result->Parent;
78
79 return Result;
80}
81
Douglas Gregor1a4761e2011-11-30 23:21:26 +000082std::string Module::getFullModuleName() const {
83 llvm::SmallVector<StringRef, 2> Names;
84
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
89 std::string Result;
90 for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
91 IEnd = Names.rend();
92 I != IEnd; ++I) {
93 if (!Result.empty())
94 Result += '.';
95
96 Result += *I;
97 }
98
99 return Result;
100}
101
Douglas Gregor10694ce2011-12-08 17:39:04 +0000102const DirectoryEntry *Module::getUmbrellaDir() const {
103 if (const FileEntry *Header = getUmbrellaHeader())
104 return Header->getDir();
105
106 return Umbrella.dyn_cast<const DirectoryEntry *>();
107}
108
Douglas Gregor51f564f2011-12-31 04:05:44 +0000109void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts) {
110 Requires.push_back(Feature);
111
112 // If this feature is currently available, we're done.
113 if (hasFeature(Feature, LangOpts))
114 return;
115
116 if (!IsAvailable)
117 return;
118
119 llvm::SmallVector<Module *, 2> Stack;
120 Stack.push_back(this);
121 while (!Stack.empty()) {
122 Module *Current = Stack.back();
123 Stack.pop_back();
124
125 if (!Current->IsAvailable)
126 continue;
127
128 Current->IsAvailable = false;
129 for (llvm::StringMap<Module *>::iterator Sub = Current->SubModules.begin(),
130 SubEnd = Current->SubModules.end();
131 Sub != SubEnd; ++Sub) {
132 if (Sub->second->IsAvailable)
133 Stack.push_back(Sub->second);
134 }
135 }
136}
137
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000138static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
139 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
140 if (I)
141 OS << ".";
142 OS << Id[I].first;
143 }
144}
145
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000146void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
147 OS.indent(Indent);
148 if (IsFramework)
149 OS << "framework ";
150 if (IsExplicit)
151 OS << "explicit ";
152 OS << "module " << Name << " {\n";
Douglas Gregor51f564f2011-12-31 04:05:44 +0000153
154 if (!Requires.empty()) {
155 OS.indent(Indent + 2);
156 OS << "requires ";
157 for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
158 if (I)
159 OS << ", ";
160 OS << Requires[I];
161 }
162 OS << "\n";
163 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000164
Douglas Gregor10694ce2011-12-08 17:39:04 +0000165 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000166 OS.indent(Indent + 2);
Douglas Gregor489ad432011-12-08 18:00:48 +0000167 OS << "umbrella header \"";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000168 OS.write_escaped(UmbrellaHeader->getName());
169 OS << "\"\n";
Douglas Gregor489ad432011-12-08 18:00:48 +0000170 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
171 OS.indent(Indent + 2);
172 OS << "umbrella \"";
173 OS.write_escaped(UmbrellaDir->getName());
174 OS << "\"\n";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000175 }
176
177 for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
178 OS.indent(Indent + 2);
179 OS << "header \"";
180 OS.write_escaped(Headers[I]->getName());
181 OS << "\"\n";
182 }
183
184 for (llvm::StringMap<Module *>::const_iterator MI = SubModules.begin(),
Douglas Gregor1e123682011-12-05 22:27:44 +0000185 MIEnd = SubModules.end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000186 MI != MIEnd; ++MI)
187 MI->getValue()->print(OS, Indent + 2);
188
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000189 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
190 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000191 OS << "export ";
192 if (Module *Restriction = Exports[I].getPointer()) {
193 OS << Restriction->getFullModuleName();
194 if (Exports[I].getInt())
195 OS << ".*";
196 } else {
197 OS << "*";
198 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000199 OS << "\n";
200 }
201
202 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
203 OS.indent(Indent + 2);
204 OS << "export ";
205 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000206 if (UnresolvedExports[I].Wildcard) {
207 if (UnresolvedExports[I].Id.empty())
208 OS << "*";
209 else
210 OS << ".*";
211 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000212 OS << "\n";
213 }
214
Douglas Gregor1e123682011-12-05 22:27:44 +0000215 if (InferSubmodules) {
216 OS.indent(Indent + 2);
217 if (InferExplicitSubmodules)
218 OS << "explicit ";
219 OS << "module * {\n";
220 if (InferExportWildcard) {
221 OS.indent(Indent + 4);
222 OS << "export *\n";
223 }
224 OS.indent(Indent + 2);
225 OS << "}\n";
226 }
227
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000228 OS.indent(Indent);
229 OS << "}\n";
230}
231
232void Module::dump() const {
233 print(llvm::errs());
234}
235
236