blob: 7308665bd2d8710d57ee683215a55fb0f9dfe0eb [file] [log] [blame]
Chandler Carruth3a022472012-12-04 09:13:33 +00001//===--- Module.cpp - Describe a module -----------------------------------===//
Douglas Gregorde3ef502011-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//===----------------------------------------------------------------------===//
Richard Smitha3feee22013-10-28 22:18:19 +000014
Douglas Gregorde3ef502011-11-30 23:21:26 +000015#include "clang/Basic/Module.h"
16#include "clang/Basic/FileManager.h"
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000017#include "clang/Basic/LangOptions.h"
Douglas Gregor0070c0b2012-01-30 06:38:25 +000018#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +000019#include "llvm/ADT/ArrayRef.h"
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringSwitch.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
Richard Smitha3feee22013-10-28 22:18:19 +000024
Douglas Gregorde3ef502011-11-30 23:21:26 +000025using namespace clang;
26
Richard Smith77944862014-03-02 05:58:18 +000027Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
Richard Smitha7e2cc62015-05-01 01:53:09 +000028 bool IsFramework, bool IsExplicit, unsigned VisibilityID)
Richard Smith3c1a41a2014-12-02 00:08:08 +000029 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), Directory(),
Richard Smitha7e2cc62015-05-01 01:53:09 +000030 Umbrella(), ASTFile(nullptr), VisibilityID(VisibilityID),
31 IsMissingRequirement(false), IsAvailable(true), IsFromModuleFile(false),
32 IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
33 IsExternC(false), IsInferred(false), InferSubmodules(false),
34 InferExplicitSubmodules(false), InferExportWildcard(false),
35 ConfigMacrosExhaustive(false), NameVisibility(Hidden) {
Douglas Gregoreb90e832012-01-04 23:32:19 +000036 if (Parent) {
37 if (!Parent->isAvailable())
38 IsAvailable = false;
Douglas Gregor3ec66632012-02-02 18:42:48 +000039 if (Parent->IsSystem)
40 IsSystem = true;
Richard Smith9bca2982014-03-08 00:03:56 +000041 if (Parent->IsExternC)
42 IsExternC = true;
Ben Langmuir993055f2014-04-18 23:51:00 +000043 IsMissingRequirement = Parent->IsMissingRequirement;
Douglas Gregoreb90e832012-01-04 23:32:19 +000044
45 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
46 Parent->SubModules.push_back(this);
47 }
48}
49
Douglas Gregorde3ef502011-11-30 23:21:26 +000050Module::~Module() {
Douglas Gregoreb90e832012-01-04 23:32:19 +000051 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregorde3ef502011-11-30 23:21:26 +000052 I != IEnd; ++I) {
Douglas Gregoreb90e832012-01-04 23:32:19 +000053 delete *I;
Douglas Gregorde3ef502011-11-30 23:21:26 +000054 }
Douglas Gregorde3ef502011-11-30 23:21:26 +000055}
56
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000057/// \brief Determine whether a translation unit built using the current
58/// language options has the given feature.
Douglas Gregor89929282012-01-30 06:01:29 +000059static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
60 const TargetInfo &Target) {
Ben Langmuir532d2102015-02-02 21:56:15 +000061 bool HasFeature = llvm::StringSwitch<bool>(Feature)
62 .Case("altivec", LangOpts.AltiVec)
63 .Case("blocks", LangOpts.Blocks)
64 .Case("cplusplus", LangOpts.CPlusPlus)
65 .Case("cplusplus11", LangOpts.CPlusPlus11)
66 .Case("objc", LangOpts.ObjC1)
67 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
68 .Case("opencl", LangOpts.OpenCL)
69 .Case("tls", Target.isTLSSupported())
70 .Default(Target.hasFeature(Feature));
71 if (!HasFeature)
72 HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
73 LangOpts.ModuleFeatures.end(),
74 Feature) != LangOpts.ModuleFeatures.end();
75 return HasFeature;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000076}
77
Richard Smith3c1a41a2014-12-02 00:08:08 +000078bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
79 Requirement &Req,
80 UnresolvedHeaderDirective &MissingHeader) const {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000081 if (IsAvailable)
82 return true;
83
84 for (const Module *Current = this; Current; Current = Current->Parent) {
Daniel Jasper0761a8a2013-12-17 10:31:37 +000085 if (!Current->MissingHeaders.empty()) {
86 MissingHeader = Current->MissingHeaders.front();
87 return false;
88 }
Richard Smitha3feee22013-10-28 22:18:19 +000089 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
90 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
91 Current->Requirements[I].second) {
92 Req = Current->Requirements[I];
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000093 return false;
94 }
95 }
96 }
97
98 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000099}
100
Dmitri Gribenko62bcd922014-04-18 14:36:51 +0000101bool Module::isSubModuleOf(const Module *Other) const {
Douglas Gregorf5eedd02011-12-05 17:28:06 +0000102 const Module *This = this;
103 do {
104 if (This == Other)
105 return true;
106
107 This = This->Parent;
108 } while (This);
109
110 return false;
111}
112
Douglas Gregor73441092011-12-05 22:27:44 +0000113const Module *Module::getTopLevelModule() const {
114 const Module *Result = this;
115 while (Result->Parent)
116 Result = Result->Parent;
117
118 return Result;
119}
120
Douglas Gregorde3ef502011-11-30 23:21:26 +0000121std::string Module::getFullModuleName() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000122 SmallVector<StringRef, 2> Names;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000123
124 // Build up the set of module names (from innermost to outermost).
125 for (const Module *M = this; M; M = M->Parent)
126 Names.push_back(M->Name);
127
128 std::string Result;
Craig Topper61ac9062013-07-08 03:55:09 +0000129 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
130 IEnd = Names.rend();
Douglas Gregorde3ef502011-11-30 23:21:26 +0000131 I != IEnd; ++I) {
132 if (!Result.empty())
133 Result += '.';
134
135 Result += *I;
136 }
137
138 return Result;
139}
140
Richard Smith2b63d152015-05-16 02:28:53 +0000141Module::DirectoryName Module::getUmbrellaDir() const {
142 if (Header U = getUmbrellaHeader())
143 return {"", U.Entry->getDir()};
Douglas Gregor73141fa2011-12-08 17:39:04 +0000144
Richard Smith2b63d152015-05-16 02:28:53 +0000145 return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()};
Douglas Gregor73141fa2011-12-08 17:39:04 +0000146}
147
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +0000148ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
149 if (!TopHeaderNames.empty()) {
150 for (std::vector<std::string>::iterator
151 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
152 if (const FileEntry *FE = FileMgr.getFile(*I))
153 TopHeaders.insert(FE);
154 }
155 TopHeaderNames.clear();
156 }
157
158 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
159}
160
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000161bool Module::directlyUses(const Module *Requested) const {
162 auto *Top = getTopLevelModule();
163
164 // A top-level module implicitly uses itself.
165 if (Requested->isSubModuleOf(Top))
166 return true;
167
168 for (auto *Use : Top->DirectUses)
169 if (Requested->isSubModuleOf(Use))
170 return true;
171 return false;
172}
173
Richard Smitha3feee22013-10-28 22:18:19 +0000174void Module::addRequirement(StringRef Feature, bool RequiredState,
175 const LangOptions &LangOpts,
Douglas Gregor89929282012-01-30 06:01:29 +0000176 const TargetInfo &Target) {
Richard Smitha3feee22013-10-28 22:18:19 +0000177 Requirements.push_back(Requirement(Feature, RequiredState));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000178
179 // If this feature is currently available, we're done.
Richard Smitha3feee22013-10-28 22:18:19 +0000180 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000181 return;
182
Ben Langmuir993055f2014-04-18 23:51:00 +0000183 markUnavailable(/*MissingRequirement*/true);
Ben Langmuirec8c9752014-04-18 22:07:31 +0000184}
185
Ben Langmuir993055f2014-04-18 23:51:00 +0000186void Module::markUnavailable(bool MissingRequirement) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000187 if (!IsAvailable)
188 return;
189
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000190 SmallVector<Module *, 2> Stack;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000191 Stack.push_back(this);
192 while (!Stack.empty()) {
193 Module *Current = Stack.back();
194 Stack.pop_back();
195
196 if (!Current->IsAvailable)
197 continue;
198
199 Current->IsAvailable = false;
Ben Langmuir993055f2014-04-18 23:51:00 +0000200 Current->IsMissingRequirement |= MissingRequirement;
Douglas Gregoreb90e832012-01-04 23:32:19 +0000201 for (submodule_iterator Sub = Current->submodule_begin(),
202 SubEnd = Current->submodule_end();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000203 Sub != SubEnd; ++Sub) {
Douglas Gregoreb90e832012-01-04 23:32:19 +0000204 if ((*Sub)->IsAvailable)
205 Stack.push_back(*Sub);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000206 }
207 }
208}
209
Douglas Gregoreb90e832012-01-04 23:32:19 +0000210Module *Module::findSubmodule(StringRef Name) const {
211 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
212 if (Pos == SubModuleIndex.end())
Craig Topperf1186c52014-05-08 06:41:40 +0000213 return nullptr;
214
Douglas Gregoreb90e832012-01-04 23:32:19 +0000215 return SubModules[Pos->getValue()];
216}
217
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000218static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
Douglas Gregor24bb9232011-12-02 18:58:38 +0000219 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
220 if (I)
221 OS << ".";
222 OS << Id[I].first;
223 }
224}
225
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +0000226void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +0000227 // All non-explicit submodules are exported.
228 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
229 E = SubModules.end();
230 I != E; ++I) {
231 Module *Mod = *I;
232 if (!Mod->IsExplicit)
233 Exported.push_back(Mod);
234 }
235
236 // Find re-exported modules by filtering the list of imported modules.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +0000237 bool AnyWildcard = false;
238 bool UnrestrictedWildcard = false;
239 SmallVector<Module *, 4> WildcardRestrictions;
240 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
241 Module *Mod = Exports[I].getPointer();
242 if (!Exports[I].getInt()) {
243 // Export a named module directly; no wildcards involved.
244 Exported.push_back(Mod);
245
246 continue;
247 }
248
249 // Wildcard export: export all of the imported modules that match
250 // the given pattern.
251 AnyWildcard = true;
252 if (UnrestrictedWildcard)
253 continue;
254
255 if (Module *Restriction = Exports[I].getPointer())
256 WildcardRestrictions.push_back(Restriction);
257 else {
258 WildcardRestrictions.clear();
259 UnrestrictedWildcard = true;
260 }
261 }
262
263 // If there were any wildcards, push any imported modules that were
264 // re-exported by the wildcard restriction.
265 if (!AnyWildcard)
266 return;
267
268 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
269 Module *Mod = Imports[I];
270 bool Acceptable = UnrestrictedWildcard;
271 if (!Acceptable) {
272 // Check whether this module meets one of the restrictions.
273 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
274 Module *Restriction = WildcardRestrictions[R];
275 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
276 Acceptable = true;
277 break;
278 }
279 }
280 }
281
282 if (!Acceptable)
283 continue;
284
285 Exported.push_back(Mod);
286 }
287}
288
Richard Smith0e5d7b82013-07-25 23:08:39 +0000289void Module::buildVisibleModulesCache() const {
290 assert(VisibleModulesCache.empty() && "cache does not need building");
291
292 // This module is visible to itself.
293 VisibleModulesCache.insert(this);
294
Dmitri Gribenkodc360d52013-10-31 22:24:10 +0000295 // Every imported module is visible.
Richard Smithdde17e72013-11-01 02:19:14 +0000296 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
Dmitri Gribenkodc360d52013-10-31 22:24:10 +0000297 while (!Stack.empty()) {
298 Module *CurrModule = Stack.pop_back_val();
Richard Smith0e5d7b82013-07-25 23:08:39 +0000299
Richard Smithdde17e72013-11-01 02:19:14 +0000300 // Every module transitively exported by an imported module is visible.
301 if (VisibleModulesCache.insert(CurrModule).second)
302 CurrModule->getExportedModules(Stack);
Richard Smith0e5d7b82013-07-25 23:08:39 +0000303 }
304}
305
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000306void Module::print(raw_ostream &OS, unsigned Indent) const {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000307 OS.indent(Indent);
308 if (IsFramework)
309 OS << "framework ";
310 if (IsExplicit)
311 OS << "explicit ";
Douglas Gregora686e1b2012-01-27 19:52:33 +0000312 OS << "module " << Name;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000313
Ben Langmuir7615f002015-01-13 17:47:38 +0000314 if (IsSystem || IsExternC) {
Douglas Gregora686e1b2012-01-27 19:52:33 +0000315 OS.indent(Indent + 2);
Ben Langmuir7615f002015-01-13 17:47:38 +0000316 if (IsSystem)
317 OS << " [system]";
318 if (IsExternC)
319 OS << " [extern_c]";
Douglas Gregora686e1b2012-01-27 19:52:33 +0000320 }
321
322 OS << " {\n";
323
Richard Smitha3feee22013-10-28 22:18:19 +0000324 if (!Requirements.empty()) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000325 OS.indent(Indent + 2);
326 OS << "requires ";
Richard Smitha3feee22013-10-28 22:18:19 +0000327 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000328 if (I)
329 OS << ", ";
Richard Smitha3feee22013-10-28 22:18:19 +0000330 if (!Requirements[I].second)
331 OS << "!";
332 OS << Requirements[I].first;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000333 }
334 OS << "\n";
335 }
Douglas Gregorde3ef502011-11-30 23:21:26 +0000336
Richard Smith2b63d152015-05-16 02:28:53 +0000337 if (Header H = getUmbrellaHeader()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000338 OS.indent(Indent + 2);
Douglas Gregor322f6332011-12-08 18:00:48 +0000339 OS << "umbrella header \"";
Richard Smith2b63d152015-05-16 02:28:53 +0000340 OS.write_escaped(H.NameAsWritten);
Douglas Gregorde3ef502011-11-30 23:21:26 +0000341 OS << "\"\n";
Richard Smith2b63d152015-05-16 02:28:53 +0000342 } else if (DirectoryName D = getUmbrellaDir()) {
Douglas Gregor322f6332011-12-08 18:00:48 +0000343 OS.indent(Indent + 2);
344 OS << "umbrella \"";
Richard Smith2b63d152015-05-16 02:28:53 +0000345 OS.write_escaped(D.NameAsWritten);
Douglas Gregor322f6332011-12-08 18:00:48 +0000346 OS << "\"\n";
Douglas Gregorde3ef502011-11-30 23:21:26 +0000347 }
Douglas Gregor35b13ec2013-03-20 00:22:05 +0000348
349 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
350 OS.indent(Indent + 2);
351 OS << "config_macros ";
352 if (ConfigMacrosExhaustive)
Douglas Gregor8d932422013-03-20 03:59:18 +0000353 OS << "[exhaustive]";
Douglas Gregor35b13ec2013-03-20 00:22:05 +0000354 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
355 if (I)
356 OS << ", ";
357 OS << ConfigMacros[I];
358 }
Douglas Gregor8d932422013-03-20 03:59:18 +0000359 OS << "\n";
Douglas Gregor35b13ec2013-03-20 00:22:05 +0000360 }
361
Richard Smith3c1a41a2014-12-02 00:08:08 +0000362 struct {
Richard Smith306d8922014-10-22 23:50:56 +0000363 StringRef Prefix;
Richard Smith3c1a41a2014-12-02 00:08:08 +0000364 HeaderKind Kind;
365 } Kinds[] = {{"", HK_Normal},
366 {"textual ", HK_Textual},
367 {"private ", HK_Private},
368 {"private textual ", HK_PrivateTextual},
369 {"exclude ", HK_Excluded}};
Richard Smith306d8922014-10-22 23:50:56 +0000370
371 for (auto &K : Kinds) {
Richard Smith3c1a41a2014-12-02 00:08:08 +0000372 for (auto &H : Headers[K.Kind]) {
Richard Smith306d8922014-10-22 23:50:56 +0000373 OS.indent(Indent + 2);
374 OS << K.Prefix << "header \"";
Richard Smith3c1a41a2014-12-02 00:08:08 +0000375 OS.write_escaped(H.NameAsWritten);
Richard Smith306d8922014-10-22 23:50:56 +0000376 OS << "\"\n";
377 }
Douglas Gregorde3ef502011-11-30 23:21:26 +0000378 }
Douglas Gregor59527662012-10-15 06:28:11 +0000379
Douglas Gregoreb90e832012-01-04 23:32:19 +0000380 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregorde3ef502011-11-30 23:21:26 +0000381 MI != MIEnd; ++MI)
Ben Langmuir9d6448b2014-08-09 00:57:23 +0000382 // Print inferred subframework modules so that we don't need to re-infer
383 // them (requires expensive directory iteration + stat calls) when we build
384 // the module. Regular inferred submodules are OK, as we need to look at all
385 // those header files anyway.
386 if (!(*MI)->IsInferred || (*MI)->IsFramework)
Ben Langmuirffbafa22014-04-23 21:10:46 +0000387 (*MI)->print(OS, Indent + 2);
Douglas Gregorde3ef502011-11-30 23:21:26 +0000388
Douglas Gregor24bb9232011-12-02 18:58:38 +0000389 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
390 OS.indent(Indent + 2);
Douglas Gregor8c7c8352011-12-05 17:34:59 +0000391 OS << "export ";
392 if (Module *Restriction = Exports[I].getPointer()) {
393 OS << Restriction->getFullModuleName();
394 if (Exports[I].getInt())
395 OS << ".*";
396 } else {
397 OS << "*";
398 }
Douglas Gregor24bb9232011-12-02 18:58:38 +0000399 OS << "\n";
400 }
401
402 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
403 OS.indent(Indent + 2);
404 OS << "export ";
405 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregor8c7c8352011-12-05 17:34:59 +0000406 if (UnresolvedExports[I].Wildcard) {
407 if (UnresolvedExports[I].Id.empty())
408 OS << "*";
409 else
410 OS << ".*";
411 }
Douglas Gregor24bb9232011-12-02 18:58:38 +0000412 OS << "\n";
413 }
414
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000415 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
416 OS.indent(Indent + 2);
417 OS << "use ";
418 OS << DirectUses[I]->getFullModuleName();
419 OS << "\n";
420 }
421
422 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
423 OS.indent(Indent + 2);
424 OS << "use ";
425 printModuleId(OS, UnresolvedDirectUses[I]);
426 OS << "\n";
427 }
428
Douglas Gregor6ddfca92013-01-14 17:21:00 +0000429 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
430 OS.indent(Indent + 2);
431 OS << "link ";
432 if (LinkLibraries[I].IsFramework)
433 OS << "framework ";
434 OS << "\"";
435 OS.write_escaped(LinkLibraries[I].Library);
436 OS << "\"";
437 }
438
Douglas Gregorfb912652013-03-20 21:10:35 +0000439 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
440 OS.indent(Indent + 2);
441 OS << "conflict ";
442 printModuleId(OS, UnresolvedConflicts[I].Id);
443 OS << ", \"";
444 OS.write_escaped(UnresolvedConflicts[I].Message);
445 OS << "\"\n";
446 }
447
448 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
449 OS.indent(Indent + 2);
450 OS << "conflict ";
451 OS << Conflicts[I].Other->getFullModuleName();
452 OS << ", \"";
453 OS.write_escaped(Conflicts[I].Message);
454 OS << "\"\n";
455 }
456
Douglas Gregor73441092011-12-05 22:27:44 +0000457 if (InferSubmodules) {
458 OS.indent(Indent + 2);
459 if (InferExplicitSubmodules)
460 OS << "explicit ";
461 OS << "module * {\n";
462 if (InferExportWildcard) {
463 OS.indent(Indent + 4);
464 OS << "export *\n";
465 }
466 OS.indent(Indent + 2);
467 OS << "}\n";
468 }
469
Douglas Gregorde3ef502011-11-30 23:21:26 +0000470 OS.indent(Indent);
471 OS << "}\n";
472}
473
474void Module::dump() const {
475 print(llvm::errs());
476}
477
Richard Smitha7e2cc62015-05-01 01:53:09 +0000478void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
479 VisibleCallback Vis, ConflictCallback Cb) {
480 if (isVisible(M))
481 return;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000482
Richard Smitha7e2cc62015-05-01 01:53:09 +0000483 ++Generation;
484
485 struct Visiting {
486 Module *M;
487 Visiting *ExportedBy;
488 };
489
490 std::function<void(Visiting)> VisitModule = [&](Visiting V) {
491 // Modules that aren't available cannot be made visible.
492 if (!V.M->isAvailable())
493 return;
494
495 // Nothing to do for a module that's already visible.
496 unsigned ID = V.M->getVisibilityID();
497 if (ImportLocs.size() <= ID)
498 ImportLocs.resize(ID + 1);
499 else if (ImportLocs[ID].isValid())
500 return;
501
502 ImportLocs[ID] = Loc;
503 Vis(M);
504
505 // Make any exported modules visible.
506 SmallVector<Module *, 16> Exports;
507 V.M->getExportedModules(Exports);
508 for (Module *E : Exports)
509 VisitModule({E, &V});
510
511 for (auto &C : V.M->Conflicts) {
512 if (isVisible(C.Other)) {
513 llvm::SmallVector<Module*, 8> Path;
514 for (Visiting *I = &V; I; I = I->ExportedBy)
515 Path.push_back(I->M);
516 Cb(Path, C.Other, C.Message);
517 }
518 }
519 };
520 VisitModule({M, nullptr});
521}