13a02247dSChandler Carruth //===--- Module.cpp - Describe a module -----------------------------------===//
2de3ef502SDouglas Gregor //
3de3ef502SDouglas Gregor //                     The LLVM Compiler Infrastructure
4de3ef502SDouglas Gregor //
5de3ef502SDouglas Gregor // This file is distributed under the University of Illinois Open Source
6de3ef502SDouglas Gregor // License. See LICENSE.TXT for details.
7de3ef502SDouglas Gregor //
8de3ef502SDouglas Gregor //===----------------------------------------------------------------------===//
9de3ef502SDouglas Gregor //
10de3ef502SDouglas Gregor // This file defines the Module class, which describes a module in the source
11de3ef502SDouglas Gregor // code.
12de3ef502SDouglas Gregor //
13de3ef502SDouglas Gregor //===----------------------------------------------------------------------===//
14a3feee2aSRichard Smith 
15de3ef502SDouglas Gregor #include "clang/Basic/Module.h"
16de3ef502SDouglas Gregor #include "clang/Basic/FileManager.h"
171fb5c3a6SDouglas Gregor #include "clang/Basic/LangOptions.h"
180070c0bfSDouglas Gregor #include "clang/Basic/TargetInfo.h"
193c5305c1SArgyrios Kyrtzidis #include "llvm/ADT/ArrayRef.h"
201fb5c3a6SDouglas Gregor #include "llvm/ADT/SmallVector.h"
211fb5c3a6SDouglas Gregor #include "llvm/ADT/StringSwitch.h"
223a02247dSChandler Carruth #include "llvm/Support/ErrorHandling.h"
233a02247dSChandler Carruth #include "llvm/Support/raw_ostream.h"
24a3feee2aSRichard Smith 
25de3ef502SDouglas Gregor using namespace clang;
26de3ef502SDouglas Gregor 
27eb90e830SDouglas Gregor Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
28*a7e2cc68SRichard Smith                bool IsFramework, bool IsExplicit, unsigned VisibilityID)
293c1a41adSRichard Smith     : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), Directory(),
30*a7e2cc68SRichard Smith       Umbrella(), ASTFile(nullptr), VisibilityID(VisibilityID),
31*a7e2cc68SRichard Smith       IsMissingRequirement(false), IsAvailable(true), IsFromModuleFile(false),
32*a7e2cc68SRichard Smith       IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
33*a7e2cc68SRichard Smith       IsExternC(false), IsInferred(false), InferSubmodules(false),
34*a7e2cc68SRichard Smith       InferExplicitSubmodules(false), InferExportWildcard(false),
35*a7e2cc68SRichard Smith       ConfigMacrosExhaustive(false), NameVisibility(Hidden) {
36eb90e830SDouglas Gregor   if (Parent) {
37eb90e830SDouglas Gregor     if (!Parent->isAvailable())
38eb90e830SDouglas Gregor       IsAvailable = false;
393ec6663bSDouglas Gregor     if (Parent->IsSystem)
403ec6663bSDouglas Gregor       IsSystem = true;
419bca298fSRichard Smith     if (Parent->IsExternC)
429bca298fSRichard Smith       IsExternC = true;
43993055f8SBen Langmuir     IsMissingRequirement = Parent->IsMissingRequirement;
44eb90e830SDouglas Gregor 
45eb90e830SDouglas Gregor     Parent->SubModuleIndex[Name] = Parent->SubModules.size();
46eb90e830SDouglas Gregor     Parent->SubModules.push_back(this);
47eb90e830SDouglas Gregor   }
48eb90e830SDouglas Gregor }
49eb90e830SDouglas Gregor 
50de3ef502SDouglas Gregor Module::~Module() {
51eb90e830SDouglas Gregor   for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
52de3ef502SDouglas Gregor        I != IEnd; ++I) {
53eb90e830SDouglas Gregor     delete *I;
54de3ef502SDouglas Gregor   }
55de3ef502SDouglas Gregor }
56de3ef502SDouglas Gregor 
571fb5c3a6SDouglas Gregor /// \brief Determine whether a translation unit built using the current
581fb5c3a6SDouglas Gregor /// language options has the given feature.
5989929282SDouglas Gregor static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
6089929282SDouglas Gregor                        const TargetInfo &Target) {
61532d2104SBen Langmuir   bool HasFeature = llvm::StringSwitch<bool>(Feature)
620070c0bfSDouglas Gregor                         .Case("altivec", LangOpts.AltiVec)
631fb5c3a6SDouglas Gregor                         .Case("blocks", LangOpts.Blocks)
641fb5c3a6SDouglas Gregor                         .Case("cplusplus", LangOpts.CPlusPlus)
652bf7fdb7SRichard Smith                         .Case("cplusplus11", LangOpts.CPlusPlus11)
661fb5c3a6SDouglas Gregor                         .Case("objc", LangOpts.ObjC1)
671fb5c3a6SDouglas Gregor                         .Case("objc_arc", LangOpts.ObjCAutoRefCount)
680070c0bfSDouglas Gregor                         .Case("opencl", LangOpts.OpenCL)
690070c0bfSDouglas Gregor                         .Case("tls", Target.isTLSSupported())
700070c0bfSDouglas Gregor                         .Default(Target.hasFeature(Feature));
71532d2104SBen Langmuir   if (!HasFeature)
72532d2104SBen Langmuir     HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
73532d2104SBen Langmuir                            LangOpts.ModuleFeatures.end(),
74532d2104SBen Langmuir                            Feature) != LangOpts.ModuleFeatures.end();
75532d2104SBen Langmuir   return HasFeature;
761fb5c3a6SDouglas Gregor }
771fb5c3a6SDouglas Gregor 
783c1a41adSRichard Smith bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
793c1a41adSRichard Smith                          Requirement &Req,
803c1a41adSRichard Smith                          UnresolvedHeaderDirective &MissingHeader) const {
811fb5c3a6SDouglas Gregor   if (IsAvailable)
821fb5c3a6SDouglas Gregor     return true;
831fb5c3a6SDouglas Gregor 
841fb5c3a6SDouglas Gregor   for (const Module *Current = this; Current; Current = Current->Parent) {
850761a8a0SDaniel Jasper     if (!Current->MissingHeaders.empty()) {
860761a8a0SDaniel Jasper       MissingHeader = Current->MissingHeaders.front();
870761a8a0SDaniel Jasper       return false;
880761a8a0SDaniel Jasper     }
89a3feee2aSRichard Smith     for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
90a3feee2aSRichard Smith       if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
91a3feee2aSRichard Smith               Current->Requirements[I].second) {
92a3feee2aSRichard Smith         Req = Current->Requirements[I];
931fb5c3a6SDouglas Gregor         return false;
941fb5c3a6SDouglas Gregor       }
951fb5c3a6SDouglas Gregor     }
961fb5c3a6SDouglas Gregor   }
971fb5c3a6SDouglas Gregor 
981fb5c3a6SDouglas Gregor   llvm_unreachable("could not find a reason why module is unavailable");
991fb5c3a6SDouglas Gregor }
1001fb5c3a6SDouglas Gregor 
10162bcd925SDmitri Gribenko bool Module::isSubModuleOf(const Module *Other) const {
102f5eedd05SDouglas Gregor   const Module *This = this;
103f5eedd05SDouglas Gregor   do {
104f5eedd05SDouglas Gregor     if (This == Other)
105f5eedd05SDouglas Gregor       return true;
106f5eedd05SDouglas Gregor 
107f5eedd05SDouglas Gregor     This = This->Parent;
108f5eedd05SDouglas Gregor   } while (This);
109f5eedd05SDouglas Gregor 
110f5eedd05SDouglas Gregor   return false;
111f5eedd05SDouglas Gregor }
112f5eedd05SDouglas Gregor 
11373441091SDouglas Gregor const Module *Module::getTopLevelModule() const {
11473441091SDouglas Gregor   const Module *Result = this;
11573441091SDouglas Gregor   while (Result->Parent)
11673441091SDouglas Gregor     Result = Result->Parent;
11773441091SDouglas Gregor 
11873441091SDouglas Gregor   return Result;
11973441091SDouglas Gregor }
12073441091SDouglas Gregor 
121de3ef502SDouglas Gregor std::string Module::getFullModuleName() const {
122f857950dSDmitri Gribenko   SmallVector<StringRef, 2> Names;
123de3ef502SDouglas Gregor 
124de3ef502SDouglas Gregor   // Build up the set of module names (from innermost to outermost).
125de3ef502SDouglas Gregor   for (const Module *M = this; M; M = M->Parent)
126de3ef502SDouglas Gregor     Names.push_back(M->Name);
127de3ef502SDouglas Gregor 
128de3ef502SDouglas Gregor   std::string Result;
12961ac906bSCraig Topper   for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
130de3ef502SDouglas Gregor                                                  IEnd = Names.rend();
131de3ef502SDouglas Gregor        I != IEnd; ++I) {
132de3ef502SDouglas Gregor     if (!Result.empty())
133de3ef502SDouglas Gregor       Result += '.';
134de3ef502SDouglas Gregor 
135de3ef502SDouglas Gregor     Result += *I;
136de3ef502SDouglas Gregor   }
137de3ef502SDouglas Gregor 
138de3ef502SDouglas Gregor   return Result;
139de3ef502SDouglas Gregor }
140de3ef502SDouglas Gregor 
14173141fa9SDouglas Gregor const DirectoryEntry *Module::getUmbrellaDir() const {
14273141fa9SDouglas Gregor   if (const FileEntry *Header = getUmbrellaHeader())
14373141fa9SDouglas Gregor     return Header->getDir();
14473141fa9SDouglas Gregor 
14573141fa9SDouglas Gregor   return Umbrella.dyn_cast<const DirectoryEntry *>();
14673141fa9SDouglas Gregor }
14773141fa9SDouglas Gregor 
1483c5305c1SArgyrios Kyrtzidis ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
1493c5305c1SArgyrios Kyrtzidis   if (!TopHeaderNames.empty()) {
1503c5305c1SArgyrios Kyrtzidis     for (std::vector<std::string>::iterator
1513c5305c1SArgyrios Kyrtzidis            I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
1523c5305c1SArgyrios Kyrtzidis       if (const FileEntry *FE = FileMgr.getFile(*I))
1533c5305c1SArgyrios Kyrtzidis         TopHeaders.insert(FE);
1543c5305c1SArgyrios Kyrtzidis     }
1553c5305c1SArgyrios Kyrtzidis     TopHeaderNames.clear();
1563c5305c1SArgyrios Kyrtzidis   }
1573c5305c1SArgyrios Kyrtzidis 
1583c5305c1SArgyrios Kyrtzidis   return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
1593c5305c1SArgyrios Kyrtzidis }
1603c5305c1SArgyrios Kyrtzidis 
1618f4d3ff1SRichard Smith bool Module::directlyUses(const Module *Requested) const {
1628f4d3ff1SRichard Smith   auto *Top = getTopLevelModule();
1638f4d3ff1SRichard Smith 
1648f4d3ff1SRichard Smith   // A top-level module implicitly uses itself.
1658f4d3ff1SRichard Smith   if (Requested->isSubModuleOf(Top))
1668f4d3ff1SRichard Smith     return true;
1678f4d3ff1SRichard Smith 
1688f4d3ff1SRichard Smith   for (auto *Use : Top->DirectUses)
1698f4d3ff1SRichard Smith     if (Requested->isSubModuleOf(Use))
1708f4d3ff1SRichard Smith       return true;
1718f4d3ff1SRichard Smith   return false;
1728f4d3ff1SRichard Smith }
1738f4d3ff1SRichard Smith 
174a3feee2aSRichard Smith void Module::addRequirement(StringRef Feature, bool RequiredState,
175a3feee2aSRichard Smith                             const LangOptions &LangOpts,
17689929282SDouglas Gregor                             const TargetInfo &Target) {
177a3feee2aSRichard Smith   Requirements.push_back(Requirement(Feature, RequiredState));
1781fb5c3a6SDouglas Gregor 
1791fb5c3a6SDouglas Gregor   // If this feature is currently available, we're done.
180a3feee2aSRichard Smith   if (hasFeature(Feature, LangOpts, Target) == RequiredState)
1811fb5c3a6SDouglas Gregor     return;
1821fb5c3a6SDouglas Gregor 
183993055f8SBen Langmuir   markUnavailable(/*MissingRequirement*/true);
184ec8c9752SBen Langmuir }
185ec8c9752SBen Langmuir 
186993055f8SBen Langmuir void Module::markUnavailable(bool MissingRequirement) {
1871fb5c3a6SDouglas Gregor   if (!IsAvailable)
1881fb5c3a6SDouglas Gregor     return;
1891fb5c3a6SDouglas Gregor 
190f857950dSDmitri Gribenko   SmallVector<Module *, 2> Stack;
1911fb5c3a6SDouglas Gregor   Stack.push_back(this);
1921fb5c3a6SDouglas Gregor   while (!Stack.empty()) {
1931fb5c3a6SDouglas Gregor     Module *Current = Stack.back();
1941fb5c3a6SDouglas Gregor     Stack.pop_back();
1951fb5c3a6SDouglas Gregor 
1961fb5c3a6SDouglas Gregor     if (!Current->IsAvailable)
1971fb5c3a6SDouglas Gregor       continue;
1981fb5c3a6SDouglas Gregor 
1991fb5c3a6SDouglas Gregor     Current->IsAvailable = false;
200993055f8SBen Langmuir     Current->IsMissingRequirement |= MissingRequirement;
201eb90e830SDouglas Gregor     for (submodule_iterator Sub = Current->submodule_begin(),
202eb90e830SDouglas Gregor                          SubEnd = Current->submodule_end();
2031fb5c3a6SDouglas Gregor          Sub != SubEnd; ++Sub) {
204eb90e830SDouglas Gregor       if ((*Sub)->IsAvailable)
205eb90e830SDouglas Gregor         Stack.push_back(*Sub);
2061fb5c3a6SDouglas Gregor     }
2071fb5c3a6SDouglas Gregor   }
2081fb5c3a6SDouglas Gregor }
2091fb5c3a6SDouglas Gregor 
210eb90e830SDouglas Gregor Module *Module::findSubmodule(StringRef Name) const {
211eb90e830SDouglas Gregor   llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
212eb90e830SDouglas Gregor   if (Pos == SubModuleIndex.end())
213f1186c5aSCraig Topper     return nullptr;
214eb90e830SDouglas Gregor 
215eb90e830SDouglas Gregor   return SubModules[Pos->getValue()];
216eb90e830SDouglas Gregor }
217eb90e830SDouglas Gregor 
218f857950dSDmitri Gribenko static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
21924bb923aSDouglas Gregor   for (unsigned I = 0, N = Id.size(); I != N; ++I) {
22024bb923aSDouglas Gregor     if (I)
22124bb923aSDouglas Gregor       OS << ".";
22224bb923aSDouglas Gregor     OS << Id[I].first;
22324bb923aSDouglas Gregor   }
22424bb923aSDouglas Gregor }
22524bb923aSDouglas Gregor 
2268739f7b7SArgyrios Kyrtzidis void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
227e9bcf5b7SDmitri Gribenko   // All non-explicit submodules are exported.
228e9bcf5b7SDmitri Gribenko   for (std::vector<Module *>::const_iterator I = SubModules.begin(),
229e9bcf5b7SDmitri Gribenko                                              E = SubModules.end();
230e9bcf5b7SDmitri Gribenko        I != E; ++I) {
231e9bcf5b7SDmitri Gribenko     Module *Mod = *I;
232e9bcf5b7SDmitri Gribenko     if (!Mod->IsExplicit)
233e9bcf5b7SDmitri Gribenko       Exported.push_back(Mod);
234e9bcf5b7SDmitri Gribenko   }
235e9bcf5b7SDmitri Gribenko 
236e9bcf5b7SDmitri Gribenko   // Find re-exported modules by filtering the list of imported modules.
2378739f7b7SArgyrios Kyrtzidis   bool AnyWildcard = false;
2388739f7b7SArgyrios Kyrtzidis   bool UnrestrictedWildcard = false;
2398739f7b7SArgyrios Kyrtzidis   SmallVector<Module *, 4> WildcardRestrictions;
2408739f7b7SArgyrios Kyrtzidis   for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
2418739f7b7SArgyrios Kyrtzidis     Module *Mod = Exports[I].getPointer();
2428739f7b7SArgyrios Kyrtzidis     if (!Exports[I].getInt()) {
2438739f7b7SArgyrios Kyrtzidis       // Export a named module directly; no wildcards involved.
2448739f7b7SArgyrios Kyrtzidis       Exported.push_back(Mod);
2458739f7b7SArgyrios Kyrtzidis 
2468739f7b7SArgyrios Kyrtzidis       continue;
2478739f7b7SArgyrios Kyrtzidis     }
2488739f7b7SArgyrios Kyrtzidis 
2498739f7b7SArgyrios Kyrtzidis     // Wildcard export: export all of the imported modules that match
2508739f7b7SArgyrios Kyrtzidis     // the given pattern.
2518739f7b7SArgyrios Kyrtzidis     AnyWildcard = true;
2528739f7b7SArgyrios Kyrtzidis     if (UnrestrictedWildcard)
2538739f7b7SArgyrios Kyrtzidis       continue;
2548739f7b7SArgyrios Kyrtzidis 
2558739f7b7SArgyrios Kyrtzidis     if (Module *Restriction = Exports[I].getPointer())
2568739f7b7SArgyrios Kyrtzidis       WildcardRestrictions.push_back(Restriction);
2578739f7b7SArgyrios Kyrtzidis     else {
2588739f7b7SArgyrios Kyrtzidis       WildcardRestrictions.clear();
2598739f7b7SArgyrios Kyrtzidis       UnrestrictedWildcard = true;
2608739f7b7SArgyrios Kyrtzidis     }
2618739f7b7SArgyrios Kyrtzidis   }
2628739f7b7SArgyrios Kyrtzidis 
2638739f7b7SArgyrios Kyrtzidis   // If there were any wildcards, push any imported modules that were
2648739f7b7SArgyrios Kyrtzidis   // re-exported by the wildcard restriction.
2658739f7b7SArgyrios Kyrtzidis   if (!AnyWildcard)
2668739f7b7SArgyrios Kyrtzidis     return;
2678739f7b7SArgyrios Kyrtzidis 
2688739f7b7SArgyrios Kyrtzidis   for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
2698739f7b7SArgyrios Kyrtzidis     Module *Mod = Imports[I];
2708739f7b7SArgyrios Kyrtzidis     bool Acceptable = UnrestrictedWildcard;
2718739f7b7SArgyrios Kyrtzidis     if (!Acceptable) {
2728739f7b7SArgyrios Kyrtzidis       // Check whether this module meets one of the restrictions.
2738739f7b7SArgyrios Kyrtzidis       for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
2748739f7b7SArgyrios Kyrtzidis         Module *Restriction = WildcardRestrictions[R];
2758739f7b7SArgyrios Kyrtzidis         if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
2768739f7b7SArgyrios Kyrtzidis           Acceptable = true;
2778739f7b7SArgyrios Kyrtzidis           break;
2788739f7b7SArgyrios Kyrtzidis         }
2798739f7b7SArgyrios Kyrtzidis       }
2808739f7b7SArgyrios Kyrtzidis     }
2818739f7b7SArgyrios Kyrtzidis 
2828739f7b7SArgyrios Kyrtzidis     if (!Acceptable)
2838739f7b7SArgyrios Kyrtzidis       continue;
2848739f7b7SArgyrios Kyrtzidis 
2858739f7b7SArgyrios Kyrtzidis     Exported.push_back(Mod);
2868739f7b7SArgyrios Kyrtzidis   }
2878739f7b7SArgyrios Kyrtzidis }
2888739f7b7SArgyrios Kyrtzidis 
2890e5d7b8cSRichard Smith void Module::buildVisibleModulesCache() const {
2900e5d7b8cSRichard Smith   assert(VisibleModulesCache.empty() && "cache does not need building");
2910e5d7b8cSRichard Smith 
2920e5d7b8cSRichard Smith   // This module is visible to itself.
2930e5d7b8cSRichard Smith   VisibleModulesCache.insert(this);
2940e5d7b8cSRichard Smith 
2950e5d7b8cSRichard Smith   // Every imported module is visible.
296dde17e74SRichard Smith   SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
297dc360d57SDmitri Gribenko   while (!Stack.empty()) {
298dc360d57SDmitri Gribenko     Module *CurrModule = Stack.pop_back_val();
299dc360d57SDmitri Gribenko 
300dde17e74SRichard Smith     // Every module transitively exported by an imported module is visible.
301dde17e74SRichard Smith     if (VisibleModulesCache.insert(CurrModule).second)
302dde17e74SRichard Smith       CurrModule->getExportedModules(Stack);
3030e5d7b8cSRichard Smith   }
3040e5d7b8cSRichard Smith }
3050e5d7b8cSRichard Smith 
306f857950dSDmitri Gribenko void Module::print(raw_ostream &OS, unsigned Indent) const {
307de3ef502SDouglas Gregor   OS.indent(Indent);
308de3ef502SDouglas Gregor   if (IsFramework)
309de3ef502SDouglas Gregor     OS << "framework ";
310de3ef502SDouglas Gregor   if (IsExplicit)
311de3ef502SDouglas Gregor     OS << "explicit ";
312a686e1b0SDouglas Gregor   OS << "module " << Name;
313a686e1b0SDouglas Gregor 
3147615f00eSBen Langmuir   if (IsSystem || IsExternC) {
315a686e1b0SDouglas Gregor     OS.indent(Indent + 2);
3167615f00eSBen Langmuir     if (IsSystem)
317a686e1b0SDouglas Gregor       OS << " [system]";
3187615f00eSBen Langmuir     if (IsExternC)
3197615f00eSBen Langmuir       OS << " [extern_c]";
320a686e1b0SDouglas Gregor   }
321a686e1b0SDouglas Gregor 
322a686e1b0SDouglas Gregor   OS << " {\n";
323de3ef502SDouglas Gregor 
324a3feee2aSRichard Smith   if (!Requirements.empty()) {
3251fb5c3a6SDouglas Gregor     OS.indent(Indent + 2);
3261fb5c3a6SDouglas Gregor     OS << "requires ";
327a3feee2aSRichard Smith     for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
3281fb5c3a6SDouglas Gregor       if (I)
3291fb5c3a6SDouglas Gregor         OS << ", ";
330a3feee2aSRichard Smith       if (!Requirements[I].second)
331a3feee2aSRichard Smith         OS << "!";
332a3feee2aSRichard Smith       OS << Requirements[I].first;
3331fb5c3a6SDouglas Gregor     }
3341fb5c3a6SDouglas Gregor     OS << "\n";
3351fb5c3a6SDouglas Gregor   }
3361fb5c3a6SDouglas Gregor 
33773141fa9SDouglas Gregor   if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
338de3ef502SDouglas Gregor     OS.indent(Indent + 2);
339322f633cSDouglas Gregor     OS << "umbrella header \"";
340de3ef502SDouglas Gregor     OS.write_escaped(UmbrellaHeader->getName());
341de3ef502SDouglas Gregor     OS << "\"\n";
342322f633cSDouglas Gregor   } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
343322f633cSDouglas Gregor     OS.indent(Indent + 2);
344322f633cSDouglas Gregor     OS << "umbrella \"";
345322f633cSDouglas Gregor     OS.write_escaped(UmbrellaDir->getName());
346322f633cSDouglas Gregor     OS << "\"\n";
347de3ef502SDouglas Gregor   }
348de3ef502SDouglas Gregor 
34935b13eceSDouglas Gregor   if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
35035b13eceSDouglas Gregor     OS.indent(Indent + 2);
35135b13eceSDouglas Gregor     OS << "config_macros ";
35235b13eceSDouglas Gregor     if (ConfigMacrosExhaustive)
3538d932427SDouglas Gregor       OS << "[exhaustive]";
35435b13eceSDouglas Gregor     for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
35535b13eceSDouglas Gregor       if (I)
35635b13eceSDouglas Gregor         OS << ", ";
35735b13eceSDouglas Gregor       OS << ConfigMacros[I];
35835b13eceSDouglas Gregor     }
3598d932427SDouglas Gregor     OS << "\n";
36035b13eceSDouglas Gregor   }
36135b13eceSDouglas Gregor 
3623c1a41adSRichard Smith   struct {
363306d8920SRichard Smith     StringRef Prefix;
3643c1a41adSRichard Smith     HeaderKind Kind;
3653c1a41adSRichard Smith   } Kinds[] = {{"", HK_Normal},
3663c1a41adSRichard Smith                {"textual ", HK_Textual},
3673c1a41adSRichard Smith                {"private ", HK_Private},
3683c1a41adSRichard Smith                {"private textual ", HK_PrivateTextual},
3693c1a41adSRichard Smith                {"exclude ", HK_Excluded}};
370306d8920SRichard Smith 
371306d8920SRichard Smith   for (auto &K : Kinds) {
3723c1a41adSRichard Smith     for (auto &H : Headers[K.Kind]) {
373de3ef502SDouglas Gregor       OS.indent(Indent + 2);
374306d8920SRichard Smith       OS << K.Prefix << "header \"";
3753c1a41adSRichard Smith       OS.write_escaped(H.NameAsWritten);
376de3ef502SDouglas Gregor       OS << "\"\n";
377de3ef502SDouglas Gregor     }
378b53e5483SLawrence Crowl   }
379b53e5483SLawrence Crowl 
380eb90e830SDouglas Gregor   for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
381de3ef502SDouglas Gregor        MI != MIEnd; ++MI)
3829d6448b1SBen Langmuir     // Print inferred subframework modules so that we don't need to re-infer
3839d6448b1SBen Langmuir     // them (requires expensive directory iteration + stat calls) when we build
3849d6448b1SBen Langmuir     // the module. Regular inferred submodules are OK, as we need to look at all
3859d6448b1SBen Langmuir     // those header files anyway.
3869d6448b1SBen Langmuir     if (!(*MI)->IsInferred || (*MI)->IsFramework)
387eb90e830SDouglas Gregor       (*MI)->print(OS, Indent + 2);
388de3ef502SDouglas Gregor 
38924bb923aSDouglas Gregor   for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
39024bb923aSDouglas Gregor     OS.indent(Indent + 2);
3918c7c8352SDouglas Gregor     OS << "export ";
3928c7c8352SDouglas Gregor     if (Module *Restriction = Exports[I].getPointer()) {
3938c7c8352SDouglas Gregor       OS << Restriction->getFullModuleName();
39424bb923aSDouglas Gregor       if (Exports[I].getInt())
39524bb923aSDouglas Gregor         OS << ".*";
3968c7c8352SDouglas Gregor     } else {
3978c7c8352SDouglas Gregor       OS << "*";
3988c7c8352SDouglas Gregor     }
39924bb923aSDouglas Gregor     OS << "\n";
40024bb923aSDouglas Gregor   }
40124bb923aSDouglas Gregor 
40224bb923aSDouglas Gregor   for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
40324bb923aSDouglas Gregor     OS.indent(Indent + 2);
40424bb923aSDouglas Gregor     OS << "export ";
40524bb923aSDouglas Gregor     printModuleId(OS, UnresolvedExports[I].Id);
4068c7c8352SDouglas Gregor     if (UnresolvedExports[I].Wildcard) {
4078c7c8352SDouglas Gregor       if (UnresolvedExports[I].Id.empty())
4088c7c8352SDouglas Gregor         OS << "*";
4098c7c8352SDouglas Gregor       else
41024bb923aSDouglas Gregor         OS << ".*";
4118c7c8352SDouglas Gregor     }
41224bb923aSDouglas Gregor     OS << "\n";
41324bb923aSDouglas Gregor   }
41424bb923aSDouglas Gregor 
415ba7f2f71SDaniel Jasper   for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
416ba7f2f71SDaniel Jasper     OS.indent(Indent + 2);
417ba7f2f71SDaniel Jasper     OS << "use ";
418ba7f2f71SDaniel Jasper     OS << DirectUses[I]->getFullModuleName();
419ba7f2f71SDaniel Jasper     OS << "\n";
420ba7f2f71SDaniel Jasper   }
421ba7f2f71SDaniel Jasper 
422ba7f2f71SDaniel Jasper   for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
423ba7f2f71SDaniel Jasper     OS.indent(Indent + 2);
424ba7f2f71SDaniel Jasper     OS << "use ";
425ba7f2f71SDaniel Jasper     printModuleId(OS, UnresolvedDirectUses[I]);
426ba7f2f71SDaniel Jasper     OS << "\n";
427ba7f2f71SDaniel Jasper   }
428ba7f2f71SDaniel Jasper 
4296ddfca91SDouglas Gregor   for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
4306ddfca91SDouglas Gregor     OS.indent(Indent + 2);
4316ddfca91SDouglas Gregor     OS << "link ";
4326ddfca91SDouglas Gregor     if (LinkLibraries[I].IsFramework)
4336ddfca91SDouglas Gregor       OS << "framework ";
4346ddfca91SDouglas Gregor     OS << "\"";
4356ddfca91SDouglas Gregor     OS.write_escaped(LinkLibraries[I].Library);
4366ddfca91SDouglas Gregor     OS << "\"";
4376ddfca91SDouglas Gregor   }
4386ddfca91SDouglas Gregor 
439fb912657SDouglas Gregor   for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
440fb912657SDouglas Gregor     OS.indent(Indent + 2);
441fb912657SDouglas Gregor     OS << "conflict ";
442fb912657SDouglas Gregor     printModuleId(OS, UnresolvedConflicts[I].Id);
443fb912657SDouglas Gregor     OS << ", \"";
444fb912657SDouglas Gregor     OS.write_escaped(UnresolvedConflicts[I].Message);
445fb912657SDouglas Gregor     OS << "\"\n";
446fb912657SDouglas Gregor   }
447fb912657SDouglas Gregor 
448fb912657SDouglas Gregor   for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
449fb912657SDouglas Gregor     OS.indent(Indent + 2);
450fb912657SDouglas Gregor     OS << "conflict ";
451fb912657SDouglas Gregor     OS << Conflicts[I].Other->getFullModuleName();
452fb912657SDouglas Gregor     OS << ", \"";
453fb912657SDouglas Gregor     OS.write_escaped(Conflicts[I].Message);
454fb912657SDouglas Gregor     OS << "\"\n";
455fb912657SDouglas Gregor   }
456fb912657SDouglas Gregor 
45773441091SDouglas Gregor   if (InferSubmodules) {
45873441091SDouglas Gregor     OS.indent(Indent + 2);
45973441091SDouglas Gregor     if (InferExplicitSubmodules)
46073441091SDouglas Gregor       OS << "explicit ";
46173441091SDouglas Gregor     OS << "module * {\n";
46273441091SDouglas Gregor     if (InferExportWildcard) {
46373441091SDouglas Gregor       OS.indent(Indent + 4);
46473441091SDouglas Gregor       OS << "export *\n";
46573441091SDouglas Gregor     }
46673441091SDouglas Gregor     OS.indent(Indent + 2);
46773441091SDouglas Gregor     OS << "}\n";
46873441091SDouglas Gregor   }
46973441091SDouglas Gregor 
470de3ef502SDouglas Gregor   OS.indent(Indent);
471de3ef502SDouglas Gregor   OS << "}\n";
472de3ef502SDouglas Gregor }
473de3ef502SDouglas Gregor 
474de3ef502SDouglas Gregor void Module::dump() const {
475de3ef502SDouglas Gregor   print(llvm::errs());
476de3ef502SDouglas Gregor }
477de3ef502SDouglas Gregor 
478*a7e2cc68SRichard Smith void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
479*a7e2cc68SRichard Smith                                   VisibleCallback Vis, ConflictCallback Cb) {
480*a7e2cc68SRichard Smith   if (isVisible(M))
481*a7e2cc68SRichard Smith     return;
482de3ef502SDouglas Gregor 
483*a7e2cc68SRichard Smith   ++Generation;
484*a7e2cc68SRichard Smith 
485*a7e2cc68SRichard Smith   struct Visiting {
486*a7e2cc68SRichard Smith     Module *M;
487*a7e2cc68SRichard Smith     Visiting *ExportedBy;
488*a7e2cc68SRichard Smith   };
489*a7e2cc68SRichard Smith 
490*a7e2cc68SRichard Smith   std::function<void(Visiting)> VisitModule = [&](Visiting V) {
491*a7e2cc68SRichard Smith     // Modules that aren't available cannot be made visible.
492*a7e2cc68SRichard Smith     if (!V.M->isAvailable())
493*a7e2cc68SRichard Smith       return;
494*a7e2cc68SRichard Smith 
495*a7e2cc68SRichard Smith     // Nothing to do for a module that's already visible.
496*a7e2cc68SRichard Smith     unsigned ID = V.M->getVisibilityID();
497*a7e2cc68SRichard Smith     if (ImportLocs.size() <= ID)
498*a7e2cc68SRichard Smith       ImportLocs.resize(ID + 1);
499*a7e2cc68SRichard Smith     else if (ImportLocs[ID].isValid())
500*a7e2cc68SRichard Smith       return;
501*a7e2cc68SRichard Smith 
502*a7e2cc68SRichard Smith     ImportLocs[ID] = Loc;
503*a7e2cc68SRichard Smith     Vis(M);
504*a7e2cc68SRichard Smith 
505*a7e2cc68SRichard Smith     // Make any exported modules visible.
506*a7e2cc68SRichard Smith     SmallVector<Module *, 16> Exports;
507*a7e2cc68SRichard Smith     V.M->getExportedModules(Exports);
508*a7e2cc68SRichard Smith     for (Module *E : Exports)
509*a7e2cc68SRichard Smith       VisitModule({E, &V});
510*a7e2cc68SRichard Smith 
511*a7e2cc68SRichard Smith     for (auto &C : V.M->Conflicts) {
512*a7e2cc68SRichard Smith       if (isVisible(C.Other)) {
513*a7e2cc68SRichard Smith         llvm::SmallVector<Module*, 8> Path;
514*a7e2cc68SRichard Smith         for (Visiting *I = &V; I; I = I->ExportedBy)
515*a7e2cc68SRichard Smith           Path.push_back(I->M);
516*a7e2cc68SRichard Smith         Cb(Path, C.Other, C.Message);
517*a7e2cc68SRichard Smith       }
518*a7e2cc68SRichard Smith     }
519*a7e2cc68SRichard Smith   };
520*a7e2cc68SRichard Smith   VisitModule({M, nullptr});
521*a7e2cc68SRichard Smith }
522