blob: 8c35b342ecb063620c15e68291fd3522c6670149 [file] [log] [blame]
Chandler Carruth55fc8732012-12-04 09:13:33 +00001//===--- Module.cpp - Describe a module -----------------------------------===//
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002//
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"
Douglas Gregore727d212012-01-30 06:38:25 +000017#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +000018#include "llvm/ADT/ArrayRef.h"
Douglas Gregor51f564f2011-12-31 04:05:44 +000019#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringSwitch.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/raw_ostream.h"
Douglas Gregor1a4761e2011-11-30 23:21:26 +000023using namespace clang;
24
Douglas Gregorb7a78192012-01-04 23:32:19 +000025Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
26 bool IsFramework, bool IsExplicit)
Argyrios Kyrtzidise2ac16b2012-09-29 01:06:04 +000027 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
28 Umbrella(), ASTFile(0), IsAvailable(true), IsFromModuleFile(false),
Douglas Gregora1f1fad2012-01-27 19:52:33 +000029 IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
30 InferSubmodules(false), InferExplicitSubmodules(false),
Douglas Gregor970e4412013-03-20 03:59:18 +000031 InferExportWildcard(false), ConfigMacrosExhaustive(false),
32 NameVisibility(Hidden)
Douglas Gregorb7a78192012-01-04 23:32:19 +000033{
34 if (Parent) {
35 if (!Parent->isAvailable())
36 IsAvailable = false;
Douglas Gregor2f04f182012-02-02 18:42:48 +000037 if (Parent->IsSystem)
38 IsSystem = true;
Douglas Gregorb7a78192012-01-04 23:32:19 +000039
40 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
41 Parent->SubModules.push_back(this);
42 }
43}
44
Douglas Gregor1a4761e2011-11-30 23:21:26 +000045Module::~Module() {
Douglas Gregorb7a78192012-01-04 23:32:19 +000046 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +000047 I != IEnd; ++I) {
Douglas Gregorb7a78192012-01-04 23:32:19 +000048 delete *I;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000049 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +000050}
51
Douglas Gregor51f564f2011-12-31 04:05:44 +000052/// \brief Determine whether a translation unit built using the current
53/// language options has the given feature.
Douglas Gregordc58aa72012-01-30 06:01:29 +000054static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
55 const TargetInfo &Target) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000056 return llvm::StringSwitch<bool>(Feature)
Douglas Gregore727d212012-01-30 06:38:25 +000057 .Case("altivec", LangOpts.AltiVec)
Douglas Gregor51f564f2011-12-31 04:05:44 +000058 .Case("blocks", LangOpts.Blocks)
59 .Case("cplusplus", LangOpts.CPlusPlus)
Richard Smith80ad52f2013-01-02 11:42:31 +000060 .Case("cplusplus11", LangOpts.CPlusPlus11)
Douglas Gregor51f564f2011-12-31 04:05:44 +000061 .Case("objc", LangOpts.ObjC1)
62 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
Douglas Gregore727d212012-01-30 06:38:25 +000063 .Case("opencl", LangOpts.OpenCL)
64 .Case("tls", Target.isTLSSupported())
65 .Default(Target.hasFeature(Feature));
Douglas Gregor51f564f2011-12-31 04:05:44 +000066}
67
68bool
Douglas Gregordc58aa72012-01-30 06:01:29 +000069Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
70 StringRef &Feature) const {
Douglas Gregor51f564f2011-12-31 04:05:44 +000071 if (IsAvailable)
72 return true;
73
74 for (const Module *Current = this; Current; Current = Current->Parent) {
75 for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
Douglas Gregordc58aa72012-01-30 06:01:29 +000076 if (!hasFeature(Current->Requires[I], LangOpts, Target)) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000077 Feature = Current->Requires[I];
78 return false;
79 }
80 }
81 }
82
83 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor51f564f2011-12-31 04:05:44 +000084}
85
Douglas Gregor0adaa882011-12-05 17:28:06 +000086bool Module::isSubModuleOf(Module *Other) const {
87 const Module *This = this;
88 do {
89 if (This == Other)
90 return true;
91
92 This = This->Parent;
93 } while (This);
94
95 return false;
96}
97
Douglas Gregor1e123682011-12-05 22:27:44 +000098const Module *Module::getTopLevelModule() const {
99 const Module *Result = this;
100 while (Result->Parent)
101 Result = Result->Parent;
102
103 return Result;
104}
105
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000106std::string Module::getFullModuleName() const {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000107 SmallVector<StringRef, 2> Names;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000108
109 // Build up the set of module names (from innermost to outermost).
110 for (const Module *M = this; M; M = M->Parent)
111 Names.push_back(M->Name);
112
113 std::string Result;
Craig Topper163fbf82013-07-08 03:55:09 +0000114 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
115 IEnd = Names.rend();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000116 I != IEnd; ++I) {
117 if (!Result.empty())
118 Result += '.';
119
120 Result += *I;
121 }
122
123 return Result;
124}
125
Douglas Gregor10694ce2011-12-08 17:39:04 +0000126const DirectoryEntry *Module::getUmbrellaDir() const {
127 if (const FileEntry *Header = getUmbrellaHeader())
128 return Header->getDir();
129
130 return Umbrella.dyn_cast<const DirectoryEntry *>();
131}
132
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +0000133ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
134 if (!TopHeaderNames.empty()) {
135 for (std::vector<std::string>::iterator
136 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
137 if (const FileEntry *FE = FileMgr.getFile(*I))
138 TopHeaders.insert(FE);
139 }
140 TopHeaderNames.clear();
141 }
142
143 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
144}
145
Douglas Gregordc58aa72012-01-30 06:01:29 +0000146void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts,
147 const TargetInfo &Target) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000148 Requires.push_back(Feature);
149
150 // If this feature is currently available, we're done.
Douglas Gregordc58aa72012-01-30 06:01:29 +0000151 if (hasFeature(Feature, LangOpts, Target))
Douglas Gregor51f564f2011-12-31 04:05:44 +0000152 return;
153
154 if (!IsAvailable)
155 return;
156
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000157 SmallVector<Module *, 2> Stack;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000158 Stack.push_back(this);
159 while (!Stack.empty()) {
160 Module *Current = Stack.back();
161 Stack.pop_back();
162
163 if (!Current->IsAvailable)
164 continue;
165
166 Current->IsAvailable = false;
Douglas Gregorb7a78192012-01-04 23:32:19 +0000167 for (submodule_iterator Sub = Current->submodule_begin(),
168 SubEnd = Current->submodule_end();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000169 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +0000170 if ((*Sub)->IsAvailable)
171 Stack.push_back(*Sub);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000172 }
173 }
174}
175
Douglas Gregorb7a78192012-01-04 23:32:19 +0000176Module *Module::findSubmodule(StringRef Name) const {
177 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
178 if (Pos == SubModuleIndex.end())
179 return 0;
180
181 return SubModules[Pos->getValue()];
182}
183
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000184static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000185 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
186 if (I)
187 OS << ".";
188 OS << Id[I].first;
189 }
190}
191
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +0000192void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
193 bool AnyWildcard = false;
194 bool UnrestrictedWildcard = false;
195 SmallVector<Module *, 4> WildcardRestrictions;
196 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
197 Module *Mod = Exports[I].getPointer();
198 if (!Exports[I].getInt()) {
199 // Export a named module directly; no wildcards involved.
200 Exported.push_back(Mod);
201
202 continue;
203 }
204
205 // Wildcard export: export all of the imported modules that match
206 // the given pattern.
207 AnyWildcard = true;
208 if (UnrestrictedWildcard)
209 continue;
210
211 if (Module *Restriction = Exports[I].getPointer())
212 WildcardRestrictions.push_back(Restriction);
213 else {
214 WildcardRestrictions.clear();
215 UnrestrictedWildcard = true;
216 }
217 }
218
219 // If there were any wildcards, push any imported modules that were
220 // re-exported by the wildcard restriction.
221 if (!AnyWildcard)
222 return;
223
224 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
225 Module *Mod = Imports[I];
226 bool Acceptable = UnrestrictedWildcard;
227 if (!Acceptable) {
228 // Check whether this module meets one of the restrictions.
229 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
230 Module *Restriction = WildcardRestrictions[R];
231 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
232 Acceptable = true;
233 break;
234 }
235 }
236 }
237
238 if (!Acceptable)
239 continue;
240
241 Exported.push_back(Mod);
242 }
243}
244
Richard Smithb7751002013-07-25 23:08:39 +0000245void Module::buildVisibleModulesCache() const {
246 assert(VisibleModulesCache.empty() && "cache does not need building");
247
248 // This module is visible to itself.
249 VisibleModulesCache.insert(this);
250
251 llvm::SmallVector<Module*, 4> Exported;
252 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
253 // Every imported module is visible.
254 VisibleModulesCache.insert(Imports[I]);
255
256 // Every module exported by an imported module is visible.
257 Imports[I]->getExportedModules(Exported);
258 VisibleModulesCache.insert(Exported.begin(), Exported.end());
259 Exported.clear();
260 }
261}
262
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000263void Module::print(raw_ostream &OS, unsigned Indent) const {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000264 OS.indent(Indent);
265 if (IsFramework)
266 OS << "framework ";
267 if (IsExplicit)
268 OS << "explicit ";
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000269 OS << "module " << Name;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000270
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000271 if (IsSystem) {
272 OS.indent(Indent + 2);
273 OS << " [system]";
274 }
275
276 OS << " {\n";
277
Douglas Gregor51f564f2011-12-31 04:05:44 +0000278 if (!Requires.empty()) {
279 OS.indent(Indent + 2);
280 OS << "requires ";
281 for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
282 if (I)
283 OS << ", ";
284 OS << Requires[I];
285 }
286 OS << "\n";
287 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000288
Douglas Gregor10694ce2011-12-08 17:39:04 +0000289 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000290 OS.indent(Indent + 2);
Douglas Gregor489ad432011-12-08 18:00:48 +0000291 OS << "umbrella header \"";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000292 OS.write_escaped(UmbrellaHeader->getName());
293 OS << "\"\n";
Douglas Gregor489ad432011-12-08 18:00:48 +0000294 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
295 OS.indent(Indent + 2);
296 OS << "umbrella \"";
297 OS.write_escaped(UmbrellaDir->getName());
298 OS << "\"\n";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000299 }
Douglas Gregor63a72682013-03-20 00:22:05 +0000300
301 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
302 OS.indent(Indent + 2);
303 OS << "config_macros ";
304 if (ConfigMacrosExhaustive)
Douglas Gregor970e4412013-03-20 03:59:18 +0000305 OS << "[exhaustive]";
Douglas Gregor63a72682013-03-20 00:22:05 +0000306 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
307 if (I)
308 OS << ", ";
309 OS << ConfigMacros[I];
310 }
Douglas Gregor970e4412013-03-20 03:59:18 +0000311 OS << "\n";
Douglas Gregor63a72682013-03-20 00:22:05 +0000312 }
313
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000314 for (unsigned I = 0, N = NormalHeaders.size(); I != N; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000315 OS.indent(Indent + 2);
316 OS << "header \"";
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000317 OS.write_escaped(NormalHeaders[I]->getName());
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000318 OS << "\"\n";
319 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000320
321 for (unsigned I = 0, N = ExcludedHeaders.size(); I != N; ++I) {
322 OS.indent(Indent + 2);
323 OS << "exclude header \"";
324 OS.write_escaped(ExcludedHeaders[I]->getName());
325 OS << "\"\n";
326 }
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000327
328 for (unsigned I = 0, N = PrivateHeaders.size(); I != N; ++I) {
329 OS.indent(Indent + 2);
330 OS << "private header \"";
331 OS.write_escaped(PrivateHeaders[I]->getName());
332 OS << "\"\n";
333 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000334
Douglas Gregorb7a78192012-01-04 23:32:19 +0000335 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000336 MI != MIEnd; ++MI)
Douglas Gregorb7a78192012-01-04 23:32:19 +0000337 (*MI)->print(OS, Indent + 2);
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000338
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000339 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
340 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000341 OS << "export ";
342 if (Module *Restriction = Exports[I].getPointer()) {
343 OS << Restriction->getFullModuleName();
344 if (Exports[I].getInt())
345 OS << ".*";
346 } else {
347 OS << "*";
348 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000349 OS << "\n";
350 }
351
352 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
353 OS.indent(Indent + 2);
354 OS << "export ";
355 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000356 if (UnresolvedExports[I].Wildcard) {
357 if (UnresolvedExports[I].Id.empty())
358 OS << "*";
359 else
360 OS << ".*";
361 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000362 OS << "\n";
363 }
364
Daniel Jasperddd2dfc2013-09-24 09:14:14 +0000365 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
366 OS.indent(Indent + 2);
367 OS << "use ";
368 OS << DirectUses[I]->getFullModuleName();
369 OS << "\n";
370 }
371
372 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
373 OS.indent(Indent + 2);
374 OS << "use ";
375 printModuleId(OS, UnresolvedDirectUses[I]);
376 OS << "\n";
377 }
378
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000379 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
380 OS.indent(Indent + 2);
381 OS << "link ";
382 if (LinkLibraries[I].IsFramework)
383 OS << "framework ";
384 OS << "\"";
385 OS.write_escaped(LinkLibraries[I].Library);
386 OS << "\"";
387 }
388
Douglas Gregor906d66a2013-03-20 21:10:35 +0000389 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
390 OS.indent(Indent + 2);
391 OS << "conflict ";
392 printModuleId(OS, UnresolvedConflicts[I].Id);
393 OS << ", \"";
394 OS.write_escaped(UnresolvedConflicts[I].Message);
395 OS << "\"\n";
396 }
397
398 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
399 OS.indent(Indent + 2);
400 OS << "conflict ";
401 OS << Conflicts[I].Other->getFullModuleName();
402 OS << ", \"";
403 OS.write_escaped(Conflicts[I].Message);
404 OS << "\"\n";
405 }
406
Douglas Gregor1e123682011-12-05 22:27:44 +0000407 if (InferSubmodules) {
408 OS.indent(Indent + 2);
409 if (InferExplicitSubmodules)
410 OS << "explicit ";
411 OS << "module * {\n";
412 if (InferExportWildcard) {
413 OS.indent(Indent + 4);
414 OS << "export *\n";
415 }
416 OS.indent(Indent + 2);
417 OS << "}\n";
418 }
419
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000420 OS.indent(Indent);
421 OS << "}\n";
422}
423
424void Module::dump() const {
425 print(llvm::errs());
426}
427
428