blob: 3052532650ea6fc8683273956fc9f757b71c3c3f [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
Douglas Gregorb7a78192012-01-04 23:32:19 +000023Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
24 bool IsFramework, bool IsExplicit)
25 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
26 Umbrella(), IsAvailable(true), IsFromModuleFile(false),
Douglas Gregora1f1fad2012-01-27 19:52:33 +000027 IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
28 InferSubmodules(false), InferExplicitSubmodules(false),
29 InferExportWildcard(false), NameVisibility(Hidden)
Douglas Gregorb7a78192012-01-04 23:32:19 +000030{
31 if (Parent) {
32 if (!Parent->isAvailable())
33 IsAvailable = false;
34
35 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
36 Parent->SubModules.push_back(this);
37 }
38}
39
Douglas Gregor1a4761e2011-11-30 23:21:26 +000040Module::~Module() {
Douglas Gregorb7a78192012-01-04 23:32:19 +000041 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +000042 I != IEnd; ++I) {
Douglas Gregorb7a78192012-01-04 23:32:19 +000043 delete *I;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000044 }
45
46}
47
Douglas Gregor51f564f2011-12-31 04:05:44 +000048/// \brief Determine whether a translation unit built using the current
49/// language options has the given feature.
Douglas Gregordc58aa72012-01-30 06:01:29 +000050static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
51 const TargetInfo &Target) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000052 return llvm::StringSwitch<bool>(Feature)
53 .Case("blocks", LangOpts.Blocks)
54 .Case("cplusplus", LangOpts.CPlusPlus)
55 .Case("cplusplus11", LangOpts.CPlusPlus0x)
56 .Case("objc", LangOpts.ObjC1)
57 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
58 .Default(false);
59}
60
61bool
Douglas Gregordc58aa72012-01-30 06:01:29 +000062Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
63 StringRef &Feature) const {
Douglas Gregor51f564f2011-12-31 04:05:44 +000064 if (IsAvailable)
65 return true;
66
67 for (const Module *Current = this; Current; Current = Current->Parent) {
68 for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
Douglas Gregordc58aa72012-01-30 06:01:29 +000069 if (!hasFeature(Current->Requires[I], LangOpts, Target)) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000070 Feature = Current->Requires[I];
71 return false;
72 }
73 }
74 }
75
76 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor51f564f2011-12-31 04:05:44 +000077}
78
Douglas Gregor0adaa882011-12-05 17:28:06 +000079bool Module::isSubModuleOf(Module *Other) const {
80 const Module *This = this;
81 do {
82 if (This == Other)
83 return true;
84
85 This = This->Parent;
86 } while (This);
87
88 return false;
89}
90
Douglas Gregor1e123682011-12-05 22:27:44 +000091const Module *Module::getTopLevelModule() const {
92 const Module *Result = this;
93 while (Result->Parent)
94 Result = Result->Parent;
95
96 return Result;
97}
98
Douglas Gregor1a4761e2011-11-30 23:21:26 +000099std::string Module::getFullModuleName() const {
100 llvm::SmallVector<StringRef, 2> Names;
101
102 // Build up the set of module names (from innermost to outermost).
103 for (const Module *M = this; M; M = M->Parent)
104 Names.push_back(M->Name);
105
106 std::string Result;
107 for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
108 IEnd = Names.rend();
109 I != IEnd; ++I) {
110 if (!Result.empty())
111 Result += '.';
112
113 Result += *I;
114 }
115
116 return Result;
117}
118
Douglas Gregor10694ce2011-12-08 17:39:04 +0000119const DirectoryEntry *Module::getUmbrellaDir() const {
120 if (const FileEntry *Header = getUmbrellaHeader())
121 return Header->getDir();
122
123 return Umbrella.dyn_cast<const DirectoryEntry *>();
124}
125
Douglas Gregordc58aa72012-01-30 06:01:29 +0000126void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts,
127 const TargetInfo &Target) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000128 Requires.push_back(Feature);
129
130 // If this feature is currently available, we're done.
Douglas Gregordc58aa72012-01-30 06:01:29 +0000131 if (hasFeature(Feature, LangOpts, Target))
Douglas Gregor51f564f2011-12-31 04:05:44 +0000132 return;
133
134 if (!IsAvailable)
135 return;
136
137 llvm::SmallVector<Module *, 2> Stack;
138 Stack.push_back(this);
139 while (!Stack.empty()) {
140 Module *Current = Stack.back();
141 Stack.pop_back();
142
143 if (!Current->IsAvailable)
144 continue;
145
146 Current->IsAvailable = false;
Douglas Gregorb7a78192012-01-04 23:32:19 +0000147 for (submodule_iterator Sub = Current->submodule_begin(),
148 SubEnd = Current->submodule_end();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000149 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +0000150 if ((*Sub)->IsAvailable)
151 Stack.push_back(*Sub);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000152 }
153 }
154}
155
Douglas Gregorb7a78192012-01-04 23:32:19 +0000156Module *Module::findSubmodule(StringRef Name) const {
157 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
158 if (Pos == SubModuleIndex.end())
159 return 0;
160
161 return SubModules[Pos->getValue()];
162}
163
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000164static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
165 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
166 if (I)
167 OS << ".";
168 OS << Id[I].first;
169 }
170}
171
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000172void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
173 OS.indent(Indent);
174 if (IsFramework)
175 OS << "framework ";
176 if (IsExplicit)
177 OS << "explicit ";
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000178 OS << "module " << Name;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000179
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000180 if (IsSystem) {
181 OS.indent(Indent + 2);
182 OS << " [system]";
183 }
184
185 OS << " {\n";
186
Douglas Gregor51f564f2011-12-31 04:05:44 +0000187 if (!Requires.empty()) {
188 OS.indent(Indent + 2);
189 OS << "requires ";
190 for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
191 if (I)
192 OS << ", ";
193 OS << Requires[I];
194 }
195 OS << "\n";
196 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000197
Douglas Gregor10694ce2011-12-08 17:39:04 +0000198 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000199 OS.indent(Indent + 2);
Douglas Gregor489ad432011-12-08 18:00:48 +0000200 OS << "umbrella header \"";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000201 OS.write_escaped(UmbrellaHeader->getName());
202 OS << "\"\n";
Douglas Gregor489ad432011-12-08 18:00:48 +0000203 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
204 OS.indent(Indent + 2);
205 OS << "umbrella \"";
206 OS.write_escaped(UmbrellaDir->getName());
207 OS << "\"\n";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000208 }
209
210 for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
211 OS.indent(Indent + 2);
212 OS << "header \"";
213 OS.write_escaped(Headers[I]->getName());
214 OS << "\"\n";
215 }
216
Douglas Gregorb7a78192012-01-04 23:32:19 +0000217 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000218 MI != MIEnd; ++MI)
Douglas Gregorb7a78192012-01-04 23:32:19 +0000219 (*MI)->print(OS, Indent + 2);
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000220
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000221 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
222 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000223 OS << "export ";
224 if (Module *Restriction = Exports[I].getPointer()) {
225 OS << Restriction->getFullModuleName();
226 if (Exports[I].getInt())
227 OS << ".*";
228 } else {
229 OS << "*";
230 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000231 OS << "\n";
232 }
233
234 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
235 OS.indent(Indent + 2);
236 OS << "export ";
237 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000238 if (UnresolvedExports[I].Wildcard) {
239 if (UnresolvedExports[I].Id.empty())
240 OS << "*";
241 else
242 OS << ".*";
243 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000244 OS << "\n";
245 }
246
Douglas Gregor1e123682011-12-05 22:27:44 +0000247 if (InferSubmodules) {
248 OS.indent(Indent + 2);
249 if (InferExplicitSubmodules)
250 OS << "explicit ";
251 OS << "module * {\n";
252 if (InferExportWildcard) {
253 OS.indent(Indent + 4);
254 OS << "export *\n";
255 }
256 OS.indent(Indent + 2);
257 OS << "}\n";
258 }
259
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000260 OS.indent(Indent);
261 OS << "}\n";
262}
263
264void Module::dump() const {
265 print(llvm::errs());
266}
267
268