blob: 80bbc24f3db3256aa677c4763d64582dc0eb176e [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(),
Adrian Prantl15bcf702015-06-30 17:39:43 +000030 Umbrella(), Signature(0), ASTFile(nullptr), VisibilityID(VisibilityID),
Richard Smith8a308ec2015-11-05 00:54:55 +000031 IsMissingRequirement(false), HasIncompatibleModuleFile(false),
32 IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework),
33 IsExplicit(IsExplicit), IsSystem(false), IsExternC(false),
34 IsInferred(false), InferSubmodules(false), InferExplicitSubmodules(false),
35 InferExportWildcard(false), ConfigMacrosExhaustive(false),
Bruno Cardoso Lopesed84df02016-10-21 01:41:56 +000036 NoUndeclaredIncludes(false), NameVisibility(Hidden) {
Douglas Gregoreb90e832012-01-04 23:32:19 +000037 if (Parent) {
38 if (!Parent->isAvailable())
39 IsAvailable = false;
Douglas Gregor3ec66632012-02-02 18:42:48 +000040 if (Parent->IsSystem)
41 IsSystem = true;
Richard Smith9bca2982014-03-08 00:03:56 +000042 if (Parent->IsExternC)
43 IsExternC = true;
Bruno Cardoso Lopesed84df02016-10-21 01:41:56 +000044 if (Parent->NoUndeclaredIncludes)
45 NoUndeclaredIncludes = true;
Ben Langmuir993055f2014-04-18 23:51:00 +000046 IsMissingRequirement = Parent->IsMissingRequirement;
Douglas Gregoreb90e832012-01-04 23:32:19 +000047
48 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
49 Parent->SubModules.push_back(this);
50 }
51}
52
Douglas Gregorde3ef502011-11-30 23:21:26 +000053Module::~Module() {
Douglas Gregoreb90e832012-01-04 23:32:19 +000054 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregorde3ef502011-11-30 23:21:26 +000055 I != IEnd; ++I) {
Douglas Gregoreb90e832012-01-04 23:32:19 +000056 delete *I;
Douglas Gregorde3ef502011-11-30 23:21:26 +000057 }
Douglas Gregorde3ef502011-11-30 23:21:26 +000058}
59
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000060/// \brief Determine whether a translation unit built using the current
61/// language options has the given feature.
Douglas Gregor89929282012-01-30 06:01:29 +000062static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
63 const TargetInfo &Target) {
Ben Langmuir532d2102015-02-02 21:56:15 +000064 bool HasFeature = llvm::StringSwitch<bool>(Feature)
65 .Case("altivec", LangOpts.AltiVec)
66 .Case("blocks", LangOpts.Blocks)
67 .Case("cplusplus", LangOpts.CPlusPlus)
68 .Case("cplusplus11", LangOpts.CPlusPlus11)
Elad Cohenfb6358d2016-09-04 06:00:42 +000069 .Case("freestanding", LangOpts.Freestanding)
Bruno Cardoso Lopes6736e192016-08-30 21:25:42 +000070 .Case("gnuinlineasm", LangOpts.GNUAsm)
Ben Langmuir532d2102015-02-02 21:56:15 +000071 .Case("objc", LangOpts.ObjC1)
72 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
73 .Case("opencl", LangOpts.OpenCL)
74 .Case("tls", Target.isTLSSupported())
Ulrich Weigand3c5038a2015-07-30 14:08:36 +000075 .Case("zvector", LangOpts.ZVector)
Ben Langmuir532d2102015-02-02 21:56:15 +000076 .Default(Target.hasFeature(Feature));
77 if (!HasFeature)
78 HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
79 LangOpts.ModuleFeatures.end(),
80 Feature) != LangOpts.ModuleFeatures.end();
81 return HasFeature;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000082}
83
Richard Smith3c1a41a2014-12-02 00:08:08 +000084bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
85 Requirement &Req,
86 UnresolvedHeaderDirective &MissingHeader) const {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000087 if (IsAvailable)
88 return true;
89
90 for (const Module *Current = this; Current; Current = Current->Parent) {
Richard Smitha3feee22013-10-28 22:18:19 +000091 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
92 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
93 Current->Requirements[I].second) {
94 Req = Current->Requirements[I];
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000095 return false;
96 }
97 }
Ben Langmuir75a7e432015-07-13 19:48:52 +000098 if (!Current->MissingHeaders.empty()) {
99 MissingHeader = Current->MissingHeaders.front();
100 return false;
101 }
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000102 }
103
104 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000105}
106
Dmitri Gribenko62bcd922014-04-18 14:36:51 +0000107bool Module::isSubModuleOf(const Module *Other) const {
Douglas Gregorf5eedd02011-12-05 17:28:06 +0000108 const Module *This = this;
109 do {
110 if (This == Other)
111 return true;
112
113 This = This->Parent;
114 } while (This);
115
116 return false;
117}
118
Douglas Gregor73441092011-12-05 22:27:44 +0000119const Module *Module::getTopLevelModule() const {
120 const Module *Result = this;
121 while (Result->Parent)
122 Result = Result->Parent;
123
124 return Result;
125}
126
Douglas Gregorde3ef502011-11-30 23:21:26 +0000127std::string Module::getFullModuleName() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000128 SmallVector<StringRef, 2> Names;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000129
130 // Build up the set of module names (from innermost to outermost).
131 for (const Module *M = this; M; M = M->Parent)
132 Names.push_back(M->Name);
133
134 std::string Result;
Craig Topper61ac9062013-07-08 03:55:09 +0000135 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
136 IEnd = Names.rend();
Douglas Gregorde3ef502011-11-30 23:21:26 +0000137 I != IEnd; ++I) {
138 if (!Result.empty())
139 Result += '.';
140
141 Result += *I;
142 }
143
144 return Result;
145}
146
Ben Langmuir7ff29142015-08-13 17:13:33 +0000147bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
148 for (const Module *M = this; M; M = M->Parent) {
149 if (nameParts.empty() || M->Name != nameParts.back())
150 return false;
151 nameParts = nameParts.drop_back();
152 }
153 return nameParts.empty();
154}
155
Richard Smith2b63d152015-05-16 02:28:53 +0000156Module::DirectoryName Module::getUmbrellaDir() const {
157 if (Header U = getUmbrellaHeader())
158 return {"", U.Entry->getDir()};
Douglas Gregor73141fa2011-12-08 17:39:04 +0000159
Richard Smith2b63d152015-05-16 02:28:53 +0000160 return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()};
Douglas Gregor73141fa2011-12-08 17:39:04 +0000161}
162
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +0000163ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
164 if (!TopHeaderNames.empty()) {
165 for (std::vector<std::string>::iterator
166 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
167 if (const FileEntry *FE = FileMgr.getFile(*I))
168 TopHeaders.insert(FE);
169 }
170 TopHeaderNames.clear();
171 }
172
173 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
174}
175
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000176bool Module::directlyUses(const Module *Requested) const {
177 auto *Top = getTopLevelModule();
178
179 // A top-level module implicitly uses itself.
180 if (Requested->isSubModuleOf(Top))
181 return true;
182
183 for (auto *Use : Top->DirectUses)
184 if (Requested->isSubModuleOf(Use))
185 return true;
Bruno Cardoso Lopesed84df02016-10-21 01:41:56 +0000186
187 // Anyone is allowed to use our builtin stddef.h and its accompanying module.
188 if (!Requested->Parent && Requested->Name == "_Builtin_stddef_max_align_t")
189 return true;
190
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000191 return false;
192}
193
Richard Smitha3feee22013-10-28 22:18:19 +0000194void Module::addRequirement(StringRef Feature, bool RequiredState,
195 const LangOptions &LangOpts,
Douglas Gregor89929282012-01-30 06:01:29 +0000196 const TargetInfo &Target) {
Richard Smitha3feee22013-10-28 22:18:19 +0000197 Requirements.push_back(Requirement(Feature, RequiredState));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000198
199 // If this feature is currently available, we're done.
Richard Smitha3feee22013-10-28 22:18:19 +0000200 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000201 return;
202
Ben Langmuir993055f2014-04-18 23:51:00 +0000203 markUnavailable(/*MissingRequirement*/true);
Ben Langmuirec8c9752014-04-18 22:07:31 +0000204}
205
Ben Langmuir993055f2014-04-18 23:51:00 +0000206void Module::markUnavailable(bool MissingRequirement) {
Ben Langmuir75a7e432015-07-13 19:48:52 +0000207 auto needUpdate = [MissingRequirement](Module *M) {
208 return M->IsAvailable || (!M->IsMissingRequirement && MissingRequirement);
209 };
210
211 if (!needUpdate(this))
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000212 return;
213
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000214 SmallVector<Module *, 2> Stack;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000215 Stack.push_back(this);
216 while (!Stack.empty()) {
217 Module *Current = Stack.back();
218 Stack.pop_back();
219
Ben Langmuir75a7e432015-07-13 19:48:52 +0000220 if (!needUpdate(Current))
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000221 continue;
222
223 Current->IsAvailable = false;
Ben Langmuir993055f2014-04-18 23:51:00 +0000224 Current->IsMissingRequirement |= MissingRequirement;
Douglas Gregoreb90e832012-01-04 23:32:19 +0000225 for (submodule_iterator Sub = Current->submodule_begin(),
226 SubEnd = Current->submodule_end();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000227 Sub != SubEnd; ++Sub) {
Ben Langmuir75a7e432015-07-13 19:48:52 +0000228 if (needUpdate(*Sub))
Douglas Gregoreb90e832012-01-04 23:32:19 +0000229 Stack.push_back(*Sub);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000230 }
231 }
232}
233
Douglas Gregoreb90e832012-01-04 23:32:19 +0000234Module *Module::findSubmodule(StringRef Name) const {
235 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
236 if (Pos == SubModuleIndex.end())
Craig Topperf1186c52014-05-08 06:41:40 +0000237 return nullptr;
238
Douglas Gregoreb90e832012-01-04 23:32:19 +0000239 return SubModules[Pos->getValue()];
240}
241
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000242static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
Douglas Gregor24bb9232011-12-02 18:58:38 +0000243 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
244 if (I)
245 OS << ".";
246 OS << Id[I].first;
247 }
248}
249
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +0000250void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +0000251 // All non-explicit submodules are exported.
252 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
253 E = SubModules.end();
254 I != E; ++I) {
255 Module *Mod = *I;
256 if (!Mod->IsExplicit)
257 Exported.push_back(Mod);
258 }
259
260 // Find re-exported modules by filtering the list of imported modules.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +0000261 bool AnyWildcard = false;
262 bool UnrestrictedWildcard = false;
263 SmallVector<Module *, 4> WildcardRestrictions;
264 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
265 Module *Mod = Exports[I].getPointer();
266 if (!Exports[I].getInt()) {
267 // Export a named module directly; no wildcards involved.
268 Exported.push_back(Mod);
269
270 continue;
271 }
272
273 // Wildcard export: export all of the imported modules that match
274 // the given pattern.
275 AnyWildcard = true;
276 if (UnrestrictedWildcard)
277 continue;
278
279 if (Module *Restriction = Exports[I].getPointer())
280 WildcardRestrictions.push_back(Restriction);
281 else {
282 WildcardRestrictions.clear();
283 UnrestrictedWildcard = true;
284 }
285 }
286
287 // If there were any wildcards, push any imported modules that were
288 // re-exported by the wildcard restriction.
289 if (!AnyWildcard)
290 return;
291
292 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
293 Module *Mod = Imports[I];
294 bool Acceptable = UnrestrictedWildcard;
295 if (!Acceptable) {
296 // Check whether this module meets one of the restrictions.
297 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
298 Module *Restriction = WildcardRestrictions[R];
299 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
300 Acceptable = true;
301 break;
302 }
303 }
304 }
305
306 if (!Acceptable)
307 continue;
308
309 Exported.push_back(Mod);
310 }
311}
312
Richard Smith0e5d7b82013-07-25 23:08:39 +0000313void Module::buildVisibleModulesCache() const {
314 assert(VisibleModulesCache.empty() && "cache does not need building");
315
316 // This module is visible to itself.
317 VisibleModulesCache.insert(this);
318
Dmitri Gribenkodc360d52013-10-31 22:24:10 +0000319 // Every imported module is visible.
Richard Smithdde17e72013-11-01 02:19:14 +0000320 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
Dmitri Gribenkodc360d52013-10-31 22:24:10 +0000321 while (!Stack.empty()) {
322 Module *CurrModule = Stack.pop_back_val();
Richard Smith0e5d7b82013-07-25 23:08:39 +0000323
Richard Smithdde17e72013-11-01 02:19:14 +0000324 // Every module transitively exported by an imported module is visible.
325 if (VisibleModulesCache.insert(CurrModule).second)
326 CurrModule->getExportedModules(Stack);
Richard Smith0e5d7b82013-07-25 23:08:39 +0000327 }
328}
329
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000330void Module::print(raw_ostream &OS, unsigned Indent) const {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000331 OS.indent(Indent);
332 if (IsFramework)
333 OS << "framework ";
334 if (IsExplicit)
335 OS << "explicit ";
Douglas Gregora686e1b2012-01-27 19:52:33 +0000336 OS << "module " << Name;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000337
Ben Langmuir7615f002015-01-13 17:47:38 +0000338 if (IsSystem || IsExternC) {
Douglas Gregora686e1b2012-01-27 19:52:33 +0000339 OS.indent(Indent + 2);
Ben Langmuir7615f002015-01-13 17:47:38 +0000340 if (IsSystem)
341 OS << " [system]";
342 if (IsExternC)
343 OS << " [extern_c]";
Douglas Gregora686e1b2012-01-27 19:52:33 +0000344 }
345
346 OS << " {\n";
347
Richard Smitha3feee22013-10-28 22:18:19 +0000348 if (!Requirements.empty()) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000349 OS.indent(Indent + 2);
350 OS << "requires ";
Richard Smitha3feee22013-10-28 22:18:19 +0000351 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000352 if (I)
353 OS << ", ";
Richard Smitha3feee22013-10-28 22:18:19 +0000354 if (!Requirements[I].second)
355 OS << "!";
356 OS << Requirements[I].first;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000357 }
358 OS << "\n";
359 }
Douglas Gregorde3ef502011-11-30 23:21:26 +0000360
Richard Smith2b63d152015-05-16 02:28:53 +0000361 if (Header H = getUmbrellaHeader()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000362 OS.indent(Indent + 2);
Douglas Gregor322f6332011-12-08 18:00:48 +0000363 OS << "umbrella header \"";
Richard Smith2b63d152015-05-16 02:28:53 +0000364 OS.write_escaped(H.NameAsWritten);
Douglas Gregorde3ef502011-11-30 23:21:26 +0000365 OS << "\"\n";
Richard Smith2b63d152015-05-16 02:28:53 +0000366 } else if (DirectoryName D = getUmbrellaDir()) {
Douglas Gregor322f6332011-12-08 18:00:48 +0000367 OS.indent(Indent + 2);
368 OS << "umbrella \"";
Richard Smith2b63d152015-05-16 02:28:53 +0000369 OS.write_escaped(D.NameAsWritten);
Douglas Gregor322f6332011-12-08 18:00:48 +0000370 OS << "\"\n";
Douglas Gregorde3ef502011-11-30 23:21:26 +0000371 }
Douglas Gregor35b13ec2013-03-20 00:22:05 +0000372
373 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
374 OS.indent(Indent + 2);
375 OS << "config_macros ";
376 if (ConfigMacrosExhaustive)
Douglas Gregor8d932422013-03-20 03:59:18 +0000377 OS << "[exhaustive]";
Douglas Gregor35b13ec2013-03-20 00:22:05 +0000378 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
379 if (I)
380 OS << ", ";
381 OS << ConfigMacros[I];
382 }
Douglas Gregor8d932422013-03-20 03:59:18 +0000383 OS << "\n";
Douglas Gregor35b13ec2013-03-20 00:22:05 +0000384 }
385
Richard Smith3c1a41a2014-12-02 00:08:08 +0000386 struct {
Richard Smith306d8922014-10-22 23:50:56 +0000387 StringRef Prefix;
Richard Smith3c1a41a2014-12-02 00:08:08 +0000388 HeaderKind Kind;
389 } Kinds[] = {{"", HK_Normal},
390 {"textual ", HK_Textual},
391 {"private ", HK_Private},
392 {"private textual ", HK_PrivateTextual},
393 {"exclude ", HK_Excluded}};
Richard Smith306d8922014-10-22 23:50:56 +0000394
395 for (auto &K : Kinds) {
Richard Smith3c1a41a2014-12-02 00:08:08 +0000396 for (auto &H : Headers[K.Kind]) {
Richard Smith306d8922014-10-22 23:50:56 +0000397 OS.indent(Indent + 2);
398 OS << K.Prefix << "header \"";
Richard Smith3c1a41a2014-12-02 00:08:08 +0000399 OS.write_escaped(H.NameAsWritten);
Richard Smith306d8922014-10-22 23:50:56 +0000400 OS << "\"\n";
401 }
Douglas Gregorde3ef502011-11-30 23:21:26 +0000402 }
Douglas Gregor59527662012-10-15 06:28:11 +0000403
Douglas Gregoreb90e832012-01-04 23:32:19 +0000404 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregorde3ef502011-11-30 23:21:26 +0000405 MI != MIEnd; ++MI)
Ben Langmuir9d6448b2014-08-09 00:57:23 +0000406 // Print inferred subframework modules so that we don't need to re-infer
407 // them (requires expensive directory iteration + stat calls) when we build
408 // the module. Regular inferred submodules are OK, as we need to look at all
409 // those header files anyway.
410 if (!(*MI)->IsInferred || (*MI)->IsFramework)
Ben Langmuirffbafa22014-04-23 21:10:46 +0000411 (*MI)->print(OS, Indent + 2);
Douglas Gregorde3ef502011-11-30 23:21:26 +0000412
Douglas Gregor24bb9232011-12-02 18:58:38 +0000413 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
414 OS.indent(Indent + 2);
Douglas Gregor8c7c8352011-12-05 17:34:59 +0000415 OS << "export ";
416 if (Module *Restriction = Exports[I].getPointer()) {
417 OS << Restriction->getFullModuleName();
418 if (Exports[I].getInt())
419 OS << ".*";
420 } else {
421 OS << "*";
422 }
Douglas Gregor24bb9232011-12-02 18:58:38 +0000423 OS << "\n";
424 }
425
426 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
427 OS.indent(Indent + 2);
428 OS << "export ";
429 printModuleId(OS, UnresolvedExports[I].Id);
Davide Italiano7f96b392016-03-09 21:09:51 +0000430 if (UnresolvedExports[I].Wildcard)
431 OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*");
Douglas Gregor24bb9232011-12-02 18:58:38 +0000432 OS << "\n";
433 }
434
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000435 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
436 OS.indent(Indent + 2);
437 OS << "use ";
438 OS << DirectUses[I]->getFullModuleName();
439 OS << "\n";
440 }
441
442 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
443 OS.indent(Indent + 2);
444 OS << "use ";
445 printModuleId(OS, UnresolvedDirectUses[I]);
446 OS << "\n";
447 }
448
Douglas Gregor6ddfca92013-01-14 17:21:00 +0000449 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
450 OS.indent(Indent + 2);
451 OS << "link ";
452 if (LinkLibraries[I].IsFramework)
453 OS << "framework ";
454 OS << "\"";
455 OS.write_escaped(LinkLibraries[I].Library);
456 OS << "\"";
457 }
458
Douglas Gregorfb912652013-03-20 21:10:35 +0000459 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
460 OS.indent(Indent + 2);
461 OS << "conflict ";
462 printModuleId(OS, UnresolvedConflicts[I].Id);
463 OS << ", \"";
464 OS.write_escaped(UnresolvedConflicts[I].Message);
465 OS << "\"\n";
466 }
467
468 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
469 OS.indent(Indent + 2);
470 OS << "conflict ";
471 OS << Conflicts[I].Other->getFullModuleName();
472 OS << ", \"";
473 OS.write_escaped(Conflicts[I].Message);
474 OS << "\"\n";
475 }
476
Douglas Gregor73441092011-12-05 22:27:44 +0000477 if (InferSubmodules) {
478 OS.indent(Indent + 2);
479 if (InferExplicitSubmodules)
480 OS << "explicit ";
481 OS << "module * {\n";
482 if (InferExportWildcard) {
483 OS.indent(Indent + 4);
484 OS << "export *\n";
485 }
486 OS.indent(Indent + 2);
487 OS << "}\n";
488 }
489
Douglas Gregorde3ef502011-11-30 23:21:26 +0000490 OS.indent(Indent);
491 OS << "}\n";
492}
493
Yaron Kerencdae9412016-01-29 19:38:18 +0000494LLVM_DUMP_METHOD void Module::dump() const {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000495 print(llvm::errs());
496}
497
Richard Smitha7e2cc62015-05-01 01:53:09 +0000498void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
499 VisibleCallback Vis, ConflictCallback Cb) {
Ben Langmuir6d25fdc2016-02-11 17:04:42 +0000500 assert(Loc.isValid() && "setVisible expects a valid import location");
Richard Smitha7e2cc62015-05-01 01:53:09 +0000501 if (isVisible(M))
502 return;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000503
Richard Smitha7e2cc62015-05-01 01:53:09 +0000504 ++Generation;
505
506 struct Visiting {
507 Module *M;
508 Visiting *ExportedBy;
509 };
510
511 std::function<void(Visiting)> VisitModule = [&](Visiting V) {
512 // Modules that aren't available cannot be made visible.
513 if (!V.M->isAvailable())
514 return;
515
516 // Nothing to do for a module that's already visible.
517 unsigned ID = V.M->getVisibilityID();
518 if (ImportLocs.size() <= ID)
519 ImportLocs.resize(ID + 1);
520 else if (ImportLocs[ID].isValid())
521 return;
522
523 ImportLocs[ID] = Loc;
524 Vis(M);
525
526 // Make any exported modules visible.
527 SmallVector<Module *, 16> Exports;
528 V.M->getExportedModules(Exports);
529 for (Module *E : Exports)
530 VisitModule({E, &V});
531
532 for (auto &C : V.M->Conflicts) {
533 if (isVisible(C.Other)) {
534 llvm::SmallVector<Module*, 8> Path;
535 for (Visiting *I = &V; I; I = I->ExportedBy)
536 Path.push_back(I->M);
537 Cb(Path, C.Other, C.Message);
538 }
539 }
540 };
541 VisitModule({M, nullptr});
542}