blob: 1d96afd476ef4a7c2d17b9024755d94b39655a45 [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"
Richard Smith9565c75b2017-06-19 23:09:36 +000016#include "clang/Basic/CharInfo.h"
Douglas Gregorde3ef502011-11-30 23:21:26 +000017#include "clang/Basic/FileManager.h"
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000018#include "clang/Basic/LangOptions.h"
Douglas Gregor0070c0b2012-01-30 06:38:25 +000019#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +000020#include "llvm/ADT/ArrayRef.h"
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringSwitch.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
Richard Smitha3feee22013-10-28 22:18:19 +000025
Douglas Gregorde3ef502011-11-30 23:21:26 +000026using namespace clang;
27
Richard Smith77944862014-03-02 05:58:18 +000028Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
Richard Smitha7e2cc62015-05-01 01:53:09 +000029 bool IsFramework, bool IsExplicit, unsigned VisibilityID)
Richard Smith3c1a41a2014-12-02 00:08:08 +000030 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), Directory(),
Duncan P. N. Exon Smith60fa2882017-03-13 18:45:08 +000031 Umbrella(), ASTFile(nullptr), VisibilityID(VisibilityID),
Richard Smith8a308ec2015-11-05 00:54:55 +000032 IsMissingRequirement(false), HasIncompatibleModuleFile(false),
33 IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework),
34 IsExplicit(IsExplicit), IsSystem(false), IsExternC(false),
35 IsInferred(false), InferSubmodules(false), InferExplicitSubmodules(false),
36 InferExportWildcard(false), ConfigMacrosExhaustive(false),
David Blaikiee6b7c282017-04-11 20:46:34 +000037 NoUndeclaredIncludes(false), NameVisibility(Hidden) {
Douglas Gregoreb90e832012-01-04 23:32:19 +000038 if (Parent) {
39 if (!Parent->isAvailable())
40 IsAvailable = false;
Douglas Gregor3ec66632012-02-02 18:42:48 +000041 if (Parent->IsSystem)
42 IsSystem = true;
Richard Smith9bca2982014-03-08 00:03:56 +000043 if (Parent->IsExternC)
44 IsExternC = true;
Bruno Cardoso Lopesed84df02016-10-21 01:41:56 +000045 if (Parent->NoUndeclaredIncludes)
46 NoUndeclaredIncludes = true;
Ben Langmuir993055f2014-04-18 23:51:00 +000047 IsMissingRequirement = Parent->IsMissingRequirement;
Douglas Gregoreb90e832012-01-04 23:32:19 +000048
49 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
50 Parent->SubModules.push_back(this);
51 }
52}
53
Douglas Gregorde3ef502011-11-30 23:21:26 +000054Module::~Module() {
Douglas Gregoreb90e832012-01-04 23:32:19 +000055 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregorde3ef502011-11-30 23:21:26 +000056 I != IEnd; ++I) {
Douglas Gregoreb90e832012-01-04 23:32:19 +000057 delete *I;
Douglas Gregorde3ef502011-11-30 23:21:26 +000058 }
Douglas Gregorde3ef502011-11-30 23:21:26 +000059}
60
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000061/// \brief Determine whether a translation unit built using the current
62/// language options has the given feature.
Douglas Gregor89929282012-01-30 06:01:29 +000063static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
64 const TargetInfo &Target) {
Ben Langmuir532d2102015-02-02 21:56:15 +000065 bool HasFeature = llvm::StringSwitch<bool>(Feature)
66 .Case("altivec", LangOpts.AltiVec)
67 .Case("blocks", LangOpts.Blocks)
Eric Fiseliere38cea02017-05-28 21:07:22 +000068 .Case("coroutines", LangOpts.CoroutinesTS)
Ben Langmuir532d2102015-02-02 21:56:15 +000069 .Case("cplusplus", LangOpts.CPlusPlus)
70 .Case("cplusplus11", LangOpts.CPlusPlus11)
Elad Cohenfb6358d2016-09-04 06:00:42 +000071 .Case("freestanding", LangOpts.Freestanding)
Bruno Cardoso Lopes6736e192016-08-30 21:25:42 +000072 .Case("gnuinlineasm", LangOpts.GNUAsm)
Ben Langmuir532d2102015-02-02 21:56:15 +000073 .Case("objc", LangOpts.ObjC1)
74 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
75 .Case("opencl", LangOpts.OpenCL)
76 .Case("tls", Target.isTLSSupported())
Ulrich Weigand3c5038a2015-07-30 14:08:36 +000077 .Case("zvector", LangOpts.ZVector)
Ben Langmuir532d2102015-02-02 21:56:15 +000078 .Default(Target.hasFeature(Feature));
79 if (!HasFeature)
80 HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
81 LangOpts.ModuleFeatures.end(),
82 Feature) != LangOpts.ModuleFeatures.end();
83 return HasFeature;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000084}
85
Richard Smith3c1a41a2014-12-02 00:08:08 +000086bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
87 Requirement &Req,
88 UnresolvedHeaderDirective &MissingHeader) const {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000089 if (IsAvailable)
90 return true;
91
92 for (const Module *Current = this; Current; Current = Current->Parent) {
Richard Smitha3feee22013-10-28 22:18:19 +000093 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
94 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
95 Current->Requirements[I].second) {
96 Req = Current->Requirements[I];
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +000097 return false;
98 }
99 }
Ben Langmuir75a7e432015-07-13 19:48:52 +0000100 if (!Current->MissingHeaders.empty()) {
101 MissingHeader = Current->MissingHeaders.front();
102 return false;
103 }
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000104 }
105
106 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000107}
108
Dmitri Gribenko62bcd922014-04-18 14:36:51 +0000109bool Module::isSubModuleOf(const Module *Other) const {
Douglas Gregorf5eedd02011-12-05 17:28:06 +0000110 const Module *This = this;
111 do {
112 if (This == Other)
113 return true;
114
115 This = This->Parent;
116 } while (This);
117
118 return false;
119}
120
Douglas Gregor73441092011-12-05 22:27:44 +0000121const Module *Module::getTopLevelModule() const {
122 const Module *Result = this;
123 while (Result->Parent)
124 Result = Result->Parent;
125
126 return Result;
127}
128
Richard Smith9565c75b2017-06-19 23:09:36 +0000129static StringRef getModuleNameFromComponent(
130 const std::pair<std::string, SourceLocation> &IdComponent) {
131 return IdComponent.first;
132}
133static StringRef getModuleNameFromComponent(StringRef R) { return R; }
134
135template<typename InputIter>
136static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
137 bool AllowStringLiterals = true) {
138 for (InputIter It = Begin; It != End; ++It) {
139 if (It != Begin)
140 OS << ".";
141
142 StringRef Name = getModuleNameFromComponent(*It);
143 if (!AllowStringLiterals || isValidIdentifier(Name))
144 OS << Name;
145 else {
146 OS << '"';
147 OS.write_escaped(Name);
148 OS << '"';
149 }
150 }
151}
152
153template<typename Container>
154static void printModuleId(raw_ostream &OS, const Container &C) {
155 return printModuleId(OS, C.begin(), C.end());
156}
157
158std::string Module::getFullModuleName(bool AllowStringLiterals) const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000159 SmallVector<StringRef, 2> Names;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000160
161 // Build up the set of module names (from innermost to outermost).
162 for (const Module *M = this; M; M = M->Parent)
163 Names.push_back(M->Name);
164
165 std::string Result;
Richard Smith9565c75b2017-06-19 23:09:36 +0000166
167 llvm::raw_string_ostream Out(Result);
168 printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
169 Out.flush();
170
Douglas Gregorde3ef502011-11-30 23:21:26 +0000171 return Result;
172}
173
Ben Langmuir7ff29142015-08-13 17:13:33 +0000174bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
175 for (const Module *M = this; M; M = M->Parent) {
176 if (nameParts.empty() || M->Name != nameParts.back())
177 return false;
178 nameParts = nameParts.drop_back();
179 }
180 return nameParts.empty();
181}
182
Richard Smith2b63d152015-05-16 02:28:53 +0000183Module::DirectoryName Module::getUmbrellaDir() const {
184 if (Header U = getUmbrellaHeader())
185 return {"", U.Entry->getDir()};
Douglas Gregor73141fa2011-12-08 17:39:04 +0000186
Richard Smith2b63d152015-05-16 02:28:53 +0000187 return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()};
Douglas Gregor73141fa2011-12-08 17:39:04 +0000188}
189
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +0000190ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
191 if (!TopHeaderNames.empty()) {
192 for (std::vector<std::string>::iterator
193 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
194 if (const FileEntry *FE = FileMgr.getFile(*I))
195 TopHeaders.insert(FE);
196 }
197 TopHeaderNames.clear();
198 }
199
200 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
201}
202
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000203bool Module::directlyUses(const Module *Requested) const {
204 auto *Top = getTopLevelModule();
205
206 // A top-level module implicitly uses itself.
207 if (Requested->isSubModuleOf(Top))
208 return true;
209
210 for (auto *Use : Top->DirectUses)
211 if (Requested->isSubModuleOf(Use))
212 return true;
Bruno Cardoso Lopesed84df02016-10-21 01:41:56 +0000213
214 // Anyone is allowed to use our builtin stddef.h and its accompanying module.
215 if (!Requested->Parent && Requested->Name == "_Builtin_stddef_max_align_t")
216 return true;
217
Richard Smith8f4d3ff2015-03-26 22:10:01 +0000218 return false;
219}
220
Richard Smitha3feee22013-10-28 22:18:19 +0000221void Module::addRequirement(StringRef Feature, bool RequiredState,
222 const LangOptions &LangOpts,
Douglas Gregor89929282012-01-30 06:01:29 +0000223 const TargetInfo &Target) {
Richard Smitha3feee22013-10-28 22:18:19 +0000224 Requirements.push_back(Requirement(Feature, RequiredState));
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000225
226 // If this feature is currently available, we're done.
Richard Smitha3feee22013-10-28 22:18:19 +0000227 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000228 return;
229
Ben Langmuir993055f2014-04-18 23:51:00 +0000230 markUnavailable(/*MissingRequirement*/true);
Ben Langmuirec8c9752014-04-18 22:07:31 +0000231}
232
Ben Langmuir993055f2014-04-18 23:51:00 +0000233void Module::markUnavailable(bool MissingRequirement) {
Ben Langmuir75a7e432015-07-13 19:48:52 +0000234 auto needUpdate = [MissingRequirement](Module *M) {
235 return M->IsAvailable || (!M->IsMissingRequirement && MissingRequirement);
236 };
237
238 if (!needUpdate(this))
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000239 return;
240
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000241 SmallVector<Module *, 2> Stack;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000242 Stack.push_back(this);
243 while (!Stack.empty()) {
244 Module *Current = Stack.back();
245 Stack.pop_back();
246
Ben Langmuir75a7e432015-07-13 19:48:52 +0000247 if (!needUpdate(Current))
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000248 continue;
249
250 Current->IsAvailable = false;
Ben Langmuir993055f2014-04-18 23:51:00 +0000251 Current->IsMissingRequirement |= MissingRequirement;
Douglas Gregoreb90e832012-01-04 23:32:19 +0000252 for (submodule_iterator Sub = Current->submodule_begin(),
253 SubEnd = Current->submodule_end();
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000254 Sub != SubEnd; ++Sub) {
Ben Langmuir75a7e432015-07-13 19:48:52 +0000255 if (needUpdate(*Sub))
Douglas Gregoreb90e832012-01-04 23:32:19 +0000256 Stack.push_back(*Sub);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000257 }
258 }
259}
260
Douglas Gregoreb90e832012-01-04 23:32:19 +0000261Module *Module::findSubmodule(StringRef Name) const {
262 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
263 if (Pos == SubModuleIndex.end())
Craig Topperf1186c52014-05-08 06:41:40 +0000264 return nullptr;
265
Douglas Gregoreb90e832012-01-04 23:32:19 +0000266 return SubModules[Pos->getValue()];
267}
268
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +0000269void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
Dmitri Gribenkoe9bcf5b2013-11-04 21:51:33 +0000270 // All non-explicit submodules are exported.
271 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
272 E = SubModules.end();
273 I != E; ++I) {
274 Module *Mod = *I;
275 if (!Mod->IsExplicit)
276 Exported.push_back(Mod);
277 }
278
279 // Find re-exported modules by filtering the list of imported modules.
Argyrios Kyrtzidis8739f7b2013-02-19 19:34:40 +0000280 bool AnyWildcard = false;
281 bool UnrestrictedWildcard = false;
282 SmallVector<Module *, 4> WildcardRestrictions;
283 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
284 Module *Mod = Exports[I].getPointer();
285 if (!Exports[I].getInt()) {
286 // Export a named module directly; no wildcards involved.
287 Exported.push_back(Mod);
288
289 continue;
290 }
291
292 // Wildcard export: export all of the imported modules that match
293 // the given pattern.
294 AnyWildcard = true;
295 if (UnrestrictedWildcard)
296 continue;
297
298 if (Module *Restriction = Exports[I].getPointer())
299 WildcardRestrictions.push_back(Restriction);
300 else {
301 WildcardRestrictions.clear();
302 UnrestrictedWildcard = true;
303 }
304 }
305
306 // If there were any wildcards, push any imported modules that were
307 // re-exported by the wildcard restriction.
308 if (!AnyWildcard)
309 return;
310
311 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
312 Module *Mod = Imports[I];
313 bool Acceptable = UnrestrictedWildcard;
314 if (!Acceptable) {
315 // Check whether this module meets one of the restrictions.
316 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
317 Module *Restriction = WildcardRestrictions[R];
318 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
319 Acceptable = true;
320 break;
321 }
322 }
323 }
324
325 if (!Acceptable)
326 continue;
327
328 Exported.push_back(Mod);
329 }
330}
331
Richard Smith0e5d7b82013-07-25 23:08:39 +0000332void Module::buildVisibleModulesCache() const {
333 assert(VisibleModulesCache.empty() && "cache does not need building");
334
335 // This module is visible to itself.
336 VisibleModulesCache.insert(this);
337
Dmitri Gribenkodc360d52013-10-31 22:24:10 +0000338 // Every imported module is visible.
Richard Smithdde17e72013-11-01 02:19:14 +0000339 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
Dmitri Gribenkodc360d52013-10-31 22:24:10 +0000340 while (!Stack.empty()) {
341 Module *CurrModule = Stack.pop_back_val();
Richard Smith0e5d7b82013-07-25 23:08:39 +0000342
Richard Smithdde17e72013-11-01 02:19:14 +0000343 // Every module transitively exported by an imported module is visible.
344 if (VisibleModulesCache.insert(CurrModule).second)
345 CurrModule->getExportedModules(Stack);
Richard Smith0e5d7b82013-07-25 23:08:39 +0000346 }
347}
348
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000349void Module::print(raw_ostream &OS, unsigned Indent) const {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000350 OS.indent(Indent);
351 if (IsFramework)
352 OS << "framework ";
353 if (IsExplicit)
354 OS << "explicit ";
Richard Smith9565c75b2017-06-19 23:09:36 +0000355 OS << "module ";
356 printModuleId(OS, &Name, &Name + 1);
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000357
Ben Langmuir7615f002015-01-13 17:47:38 +0000358 if (IsSystem || IsExternC) {
Douglas Gregora686e1b2012-01-27 19:52:33 +0000359 OS.indent(Indent + 2);
Ben Langmuir7615f002015-01-13 17:47:38 +0000360 if (IsSystem)
361 OS << " [system]";
362 if (IsExternC)
363 OS << " [extern_c]";
Douglas Gregora686e1b2012-01-27 19:52:33 +0000364 }
365
366 OS << " {\n";
367
Richard Smitha3feee22013-10-28 22:18:19 +0000368 if (!Requirements.empty()) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000369 OS.indent(Indent + 2);
370 OS << "requires ";
Richard Smitha3feee22013-10-28 22:18:19 +0000371 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000372 if (I)
373 OS << ", ";
Richard Smitha3feee22013-10-28 22:18:19 +0000374 if (!Requirements[I].second)
375 OS << "!";
376 OS << Requirements[I].first;
Douglas Gregor1fb5c3a2011-12-31 04:05:44 +0000377 }
378 OS << "\n";
379 }
Douglas Gregorde3ef502011-11-30 23:21:26 +0000380
Richard Smith2b63d152015-05-16 02:28:53 +0000381 if (Header H = getUmbrellaHeader()) {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000382 OS.indent(Indent + 2);
Douglas Gregor322f6332011-12-08 18:00:48 +0000383 OS << "umbrella header \"";
Richard Smith2b63d152015-05-16 02:28:53 +0000384 OS.write_escaped(H.NameAsWritten);
Douglas Gregorde3ef502011-11-30 23:21:26 +0000385 OS << "\"\n";
Richard Smith2b63d152015-05-16 02:28:53 +0000386 } else if (DirectoryName D = getUmbrellaDir()) {
Douglas Gregor322f6332011-12-08 18:00:48 +0000387 OS.indent(Indent + 2);
388 OS << "umbrella \"";
Richard Smith2b63d152015-05-16 02:28:53 +0000389 OS.write_escaped(D.NameAsWritten);
Douglas Gregor322f6332011-12-08 18:00:48 +0000390 OS << "\"\n";
Douglas Gregorde3ef502011-11-30 23:21:26 +0000391 }
Douglas Gregor35b13ec2013-03-20 00:22:05 +0000392
393 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
394 OS.indent(Indent + 2);
395 OS << "config_macros ";
396 if (ConfigMacrosExhaustive)
Douglas Gregor8d932422013-03-20 03:59:18 +0000397 OS << "[exhaustive]";
Douglas Gregor35b13ec2013-03-20 00:22:05 +0000398 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
399 if (I)
400 OS << ", ";
401 OS << ConfigMacros[I];
402 }
Douglas Gregor8d932422013-03-20 03:59:18 +0000403 OS << "\n";
Douglas Gregor35b13ec2013-03-20 00:22:05 +0000404 }
405
Richard Smith3c1a41a2014-12-02 00:08:08 +0000406 struct {
Richard Smith306d8922014-10-22 23:50:56 +0000407 StringRef Prefix;
Richard Smith3c1a41a2014-12-02 00:08:08 +0000408 HeaderKind Kind;
409 } Kinds[] = {{"", HK_Normal},
410 {"textual ", HK_Textual},
411 {"private ", HK_Private},
412 {"private textual ", HK_PrivateTextual},
413 {"exclude ", HK_Excluded}};
Richard Smith306d8922014-10-22 23:50:56 +0000414
415 for (auto &K : Kinds) {
Richard Smith040e1262017-06-02 01:55:39 +0000416 assert(&K == &Kinds[K.Kind] && "kinds in wrong order");
Richard Smith3c1a41a2014-12-02 00:08:08 +0000417 for (auto &H : Headers[K.Kind]) {
Richard Smith306d8922014-10-22 23:50:56 +0000418 OS.indent(Indent + 2);
419 OS << K.Prefix << "header \"";
Richard Smith3c1a41a2014-12-02 00:08:08 +0000420 OS.write_escaped(H.NameAsWritten);
Richard Smith040e1262017-06-02 01:55:39 +0000421 OS << "\" { size " << H.Entry->getSize()
422 << " mtime " << H.Entry->getModificationTime() << " }\n";
423 }
424 }
425 for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
426 for (auto &U : *Unresolved) {
427 OS.indent(Indent + 2);
428 OS << Kinds[U.Kind].Prefix << "header \"";
429 OS.write_escaped(U.FileName);
430 OS << "\"";
431 if (U.Size || U.ModTime) {
432 OS << " {";
433 if (U.Size)
434 OS << " size " << *U.Size;
435 if (U.ModTime)
436 OS << " mtime " << *U.ModTime;
437 OS << " }";
438 }
439 OS << "\n";
Richard Smith306d8922014-10-22 23:50:56 +0000440 }
Douglas Gregorde3ef502011-11-30 23:21:26 +0000441 }
Douglas Gregor59527662012-10-15 06:28:11 +0000442
Douglas Gregoreb90e832012-01-04 23:32:19 +0000443 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregorde3ef502011-11-30 23:21:26 +0000444 MI != MIEnd; ++MI)
Ben Langmuir9d6448b2014-08-09 00:57:23 +0000445 // Print inferred subframework modules so that we don't need to re-infer
446 // them (requires expensive directory iteration + stat calls) when we build
447 // the module. Regular inferred submodules are OK, as we need to look at all
448 // those header files anyway.
449 if (!(*MI)->IsInferred || (*MI)->IsFramework)
Ben Langmuirffbafa22014-04-23 21:10:46 +0000450 (*MI)->print(OS, Indent + 2);
Douglas Gregorde3ef502011-11-30 23:21:26 +0000451
Douglas Gregor24bb9232011-12-02 18:58:38 +0000452 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
453 OS.indent(Indent + 2);
Douglas Gregor8c7c8352011-12-05 17:34:59 +0000454 OS << "export ";
455 if (Module *Restriction = Exports[I].getPointer()) {
Richard Smith9565c75b2017-06-19 23:09:36 +0000456 OS << Restriction->getFullModuleName(true);
Douglas Gregor8c7c8352011-12-05 17:34:59 +0000457 if (Exports[I].getInt())
458 OS << ".*";
459 } else {
460 OS << "*";
461 }
Douglas Gregor24bb9232011-12-02 18:58:38 +0000462 OS << "\n";
463 }
464
465 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
466 OS.indent(Indent + 2);
467 OS << "export ";
468 printModuleId(OS, UnresolvedExports[I].Id);
Davide Italiano7f96b392016-03-09 21:09:51 +0000469 if (UnresolvedExports[I].Wildcard)
470 OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*");
Douglas Gregor24bb9232011-12-02 18:58:38 +0000471 OS << "\n";
472 }
473
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000474 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
475 OS.indent(Indent + 2);
476 OS << "use ";
Richard Smith9565c75b2017-06-19 23:09:36 +0000477 OS << DirectUses[I]->getFullModuleName(true);
Daniel Jasperba7f2f72013-09-24 09:14:14 +0000478 OS << "\n";
479 }
480
481 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
482 OS.indent(Indent + 2);
483 OS << "use ";
484 printModuleId(OS, UnresolvedDirectUses[I]);
485 OS << "\n";
486 }
487
Douglas Gregor6ddfca92013-01-14 17:21:00 +0000488 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
489 OS.indent(Indent + 2);
490 OS << "link ";
491 if (LinkLibraries[I].IsFramework)
492 OS << "framework ";
493 OS << "\"";
494 OS.write_escaped(LinkLibraries[I].Library);
495 OS << "\"";
496 }
497
Douglas Gregorfb912652013-03-20 21:10:35 +0000498 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
499 OS.indent(Indent + 2);
500 OS << "conflict ";
501 printModuleId(OS, UnresolvedConflicts[I].Id);
502 OS << ", \"";
503 OS.write_escaped(UnresolvedConflicts[I].Message);
504 OS << "\"\n";
505 }
506
507 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
508 OS.indent(Indent + 2);
509 OS << "conflict ";
Richard Smith9565c75b2017-06-19 23:09:36 +0000510 OS << Conflicts[I].Other->getFullModuleName(true);
Douglas Gregorfb912652013-03-20 21:10:35 +0000511 OS << ", \"";
512 OS.write_escaped(Conflicts[I].Message);
513 OS << "\"\n";
514 }
515
Douglas Gregor73441092011-12-05 22:27:44 +0000516 if (InferSubmodules) {
517 OS.indent(Indent + 2);
518 if (InferExplicitSubmodules)
519 OS << "explicit ";
520 OS << "module * {\n";
521 if (InferExportWildcard) {
522 OS.indent(Indent + 4);
523 OS << "export *\n";
524 }
525 OS.indent(Indent + 2);
526 OS << "}\n";
527 }
528
Douglas Gregorde3ef502011-11-30 23:21:26 +0000529 OS.indent(Indent);
530 OS << "}\n";
531}
532
Yaron Kerencdae9412016-01-29 19:38:18 +0000533LLVM_DUMP_METHOD void Module::dump() const {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000534 print(llvm::errs());
535}
536
Richard Smitha7e2cc62015-05-01 01:53:09 +0000537void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
538 VisibleCallback Vis, ConflictCallback Cb) {
Ben Langmuir6d25fdc2016-02-11 17:04:42 +0000539 assert(Loc.isValid() && "setVisible expects a valid import location");
Richard Smitha7e2cc62015-05-01 01:53:09 +0000540 if (isVisible(M))
541 return;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000542
Richard Smitha7e2cc62015-05-01 01:53:09 +0000543 ++Generation;
544
545 struct Visiting {
546 Module *M;
547 Visiting *ExportedBy;
548 };
549
550 std::function<void(Visiting)> VisitModule = [&](Visiting V) {
551 // Modules that aren't available cannot be made visible.
552 if (!V.M->isAvailable())
553 return;
554
555 // Nothing to do for a module that's already visible.
556 unsigned ID = V.M->getVisibilityID();
557 if (ImportLocs.size() <= ID)
558 ImportLocs.resize(ID + 1);
559 else if (ImportLocs[ID].isValid())
560 return;
561
562 ImportLocs[ID] = Loc;
563 Vis(M);
564
565 // Make any exported modules visible.
566 SmallVector<Module *, 16> Exports;
567 V.M->getExportedModules(Exports);
568 for (Module *E : Exports)
569 VisitModule({E, &V});
570
571 for (auto &C : V.M->Conflicts) {
572 if (isVisible(C.Other)) {
573 llvm::SmallVector<Module*, 8> Path;
574 for (Visiting *I = &V; I; I = I->ExportedBy)
575 Path.push_back(I->M);
576 Cb(Path, C.Other, C.Message);
577 }
578 }
579 };
580 VisitModule({M, nullptr});
581}