1718292f2SDouglas Gregor //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===//
2718292f2SDouglas Gregor //
3718292f2SDouglas Gregor //                     The LLVM Compiler Infrastructure
4718292f2SDouglas Gregor //
5718292f2SDouglas Gregor // This file is distributed under the University of Illinois Open Source
6718292f2SDouglas Gregor // License. See LICENSE.TXT for details.
7718292f2SDouglas Gregor //
8718292f2SDouglas Gregor //===----------------------------------------------------------------------===//
9718292f2SDouglas Gregor //
10718292f2SDouglas Gregor // This file defines the ModuleMap implementation, which describes the layout
11718292f2SDouglas Gregor // of a module as it relates to headers.
12718292f2SDouglas Gregor //
13718292f2SDouglas Gregor //===----------------------------------------------------------------------===//
14718292f2SDouglas Gregor #include "clang/Lex/ModuleMap.h"
15718292f2SDouglas Gregor #include "clang/Lex/Lexer.h"
16718292f2SDouglas Gregor #include "clang/Lex/LiteralSupport.h"
17718292f2SDouglas Gregor #include "clang/Lex/LexDiagnostic.h"
18718292f2SDouglas Gregor #include "clang/Basic/Diagnostic.h"
19718292f2SDouglas Gregor #include "clang/Basic/FileManager.h"
20718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h"
21718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h"
22718292f2SDouglas Gregor #include "llvm/Support/Allocator.h"
23e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h"
24718292f2SDouglas Gregor #include "llvm/Support/Host.h"
255257fc63SDouglas Gregor #include "llvm/Support/PathV2.h"
26718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h"
27718292f2SDouglas Gregor #include "llvm/ADT/StringRef.h"
28718292f2SDouglas Gregor #include "llvm/ADT/StringSwitch.h"
29718292f2SDouglas Gregor using namespace clang;
30718292f2SDouglas Gregor 
312b82c2a5SDouglas Gregor Module::ExportDecl
322b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod,
332b82c2a5SDouglas Gregor                          const Module::UnresolvedExportDecl &Unresolved,
342b82c2a5SDouglas Gregor                          bool Complain) {
35f5eedd05SDouglas Gregor   // We may have just a wildcard.
36f5eedd05SDouglas Gregor   if (Unresolved.Id.empty()) {
37f5eedd05SDouglas Gregor     assert(Unresolved.Wildcard && "Invalid unresolved export");
38f5eedd05SDouglas Gregor     return Module::ExportDecl(0, true);
39f5eedd05SDouglas Gregor   }
40f5eedd05SDouglas Gregor 
412b82c2a5SDouglas Gregor   // Find the starting module.
422b82c2a5SDouglas Gregor   Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
432b82c2a5SDouglas Gregor   if (!Context) {
442b82c2a5SDouglas Gregor     if (Complain)
452b82c2a5SDouglas Gregor       Diags->Report(Unresolved.Id[0].second,
462b82c2a5SDouglas Gregor                     diag::err_mmap_missing_module_unqualified)
472b82c2a5SDouglas Gregor         << Unresolved.Id[0].first << Mod->getFullModuleName();
482b82c2a5SDouglas Gregor 
492b82c2a5SDouglas Gregor     return Module::ExportDecl();
502b82c2a5SDouglas Gregor   }
512b82c2a5SDouglas Gregor 
522b82c2a5SDouglas Gregor   // Dig into the module path.
532b82c2a5SDouglas Gregor   for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
542b82c2a5SDouglas Gregor     Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
552b82c2a5SDouglas Gregor                                         Context);
562b82c2a5SDouglas Gregor     if (!Sub) {
572b82c2a5SDouglas Gregor       if (Complain)
582b82c2a5SDouglas Gregor         Diags->Report(Unresolved.Id[I].second,
592b82c2a5SDouglas Gregor                       diag::err_mmap_missing_module_qualified)
602b82c2a5SDouglas Gregor           << Unresolved.Id[I].first << Context->getFullModuleName()
612b82c2a5SDouglas Gregor           << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
622b82c2a5SDouglas Gregor 
632b82c2a5SDouglas Gregor       return Module::ExportDecl();
642b82c2a5SDouglas Gregor     }
652b82c2a5SDouglas Gregor 
662b82c2a5SDouglas Gregor     Context = Sub;
672b82c2a5SDouglas Gregor   }
682b82c2a5SDouglas Gregor 
692b82c2a5SDouglas Gregor   return Module::ExportDecl(Context, Unresolved.Wildcard);
702b82c2a5SDouglas Gregor }
712b82c2a5SDouglas Gregor 
721fb5c3a6SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
731fb5c3a6SDouglas Gregor                      const LangOptions &LangOpts)
741fb5c3a6SDouglas Gregor   : LangOpts(LangOpts)
751fb5c3a6SDouglas Gregor {
76718292f2SDouglas Gregor   llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
77718292f2SDouglas Gregor   Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>(
78718292f2SDouglas Gregor             new DiagnosticsEngine(DiagIDs));
79718292f2SDouglas Gregor   Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
80718292f2SDouglas Gregor   SourceMgr = new SourceManager(*Diags, FileMgr);
81718292f2SDouglas Gregor }
82718292f2SDouglas Gregor 
83718292f2SDouglas Gregor ModuleMap::~ModuleMap() {
845acdf59eSDouglas Gregor   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
855acdf59eSDouglas Gregor                                         IEnd = Modules.end();
865acdf59eSDouglas Gregor        I != IEnd; ++I) {
875acdf59eSDouglas Gregor     delete I->getValue();
885acdf59eSDouglas Gregor   }
895acdf59eSDouglas Gregor 
90718292f2SDouglas Gregor   delete SourceMgr;
91718292f2SDouglas Gregor }
92718292f2SDouglas Gregor 
93de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
94ab0c8a84SDouglas Gregor   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
95ab0c8a84SDouglas Gregor     = Headers.find(File);
961fb5c3a6SDouglas Gregor   if (Known != Headers.end()) {
971fb5c3a6SDouglas Gregor     // If a header corresponds to an unavailable module, don't report
981fb5c3a6SDouglas Gregor     // that it maps to anything.
991fb5c3a6SDouglas Gregor     if (!Known->second->isAvailable())
1001fb5c3a6SDouglas Gregor       return 0;
1011fb5c3a6SDouglas Gregor 
102ab0c8a84SDouglas Gregor     return Known->second;
1031fb5c3a6SDouglas Gregor   }
104ab0c8a84SDouglas Gregor 
105b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
106b65dbfffSDouglas Gregor   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
107b65dbfffSDouglas Gregor   StringRef DirName = Dir->getName();
108a89c5ac4SDouglas Gregor 
109a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
110a89c5ac4SDouglas Gregor   // an umbrella header.
111b65dbfffSDouglas Gregor   do {
112a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
113a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
114a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
115a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
116930a85ccSDouglas Gregor 
117930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
11873141fa9SDouglas Gregor       // directory.
119930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
12073141fa9SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
121930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
122930a85ccSDouglas Gregor 
123930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
124a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
125a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
126a89c5ac4SDouglas Gregor         // the actual header is located.
1279458f82dSDouglas Gregor         bool Explicit = UmbrellaModule->InferExplicitSubmodules;
1289458f82dSDouglas Gregor 
1297033127bSDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
130a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
131a89c5ac4SDouglas Gregor           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
132a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
1339458f82dSDouglas Gregor                                       Explicit).first;
134a89c5ac4SDouglas Gregor 
135a89c5ac4SDouglas Gregor           // Associate the module and the directory.
136a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
137a89c5ac4SDouglas Gregor 
138a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
139a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
140930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
141a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
142a89c5ac4SDouglas Gregor         }
143a89c5ac4SDouglas Gregor 
144a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
145a89c5ac4SDouglas Gregor         StringRef Name = llvm::sys::path::stem(File->getName());
146a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
1479458f82dSDouglas Gregor                                     Explicit).first;
148a89c5ac4SDouglas Gregor 
149a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
150a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
151930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
152a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
153a89c5ac4SDouglas Gregor       } else {
154a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
155a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
156a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
157a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
158a89c5ac4SDouglas Gregor       }
159a89c5ac4SDouglas Gregor 
160a89c5ac4SDouglas Gregor       Headers[File] = Result;
1611fb5c3a6SDouglas Gregor 
1621fb5c3a6SDouglas Gregor       // If a header corresponds to an unavailable module, don't report
1631fb5c3a6SDouglas Gregor       // that it maps to anything.
1641fb5c3a6SDouglas Gregor       if (!Result->isAvailable())
1651fb5c3a6SDouglas Gregor         return 0;
1661fb5c3a6SDouglas Gregor 
167a89c5ac4SDouglas Gregor       return Result;
168a89c5ac4SDouglas Gregor     }
169a89c5ac4SDouglas Gregor 
170a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
171a89c5ac4SDouglas Gregor 
172b65dbfffSDouglas Gregor     // Retrieve our parent path.
173b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
174b65dbfffSDouglas Gregor     if (DirName.empty())
175b65dbfffSDouglas Gregor       break;
176b65dbfffSDouglas Gregor 
177b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
178b65dbfffSDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
179a89c5ac4SDouglas Gregor   } while (Dir);
180b65dbfffSDouglas Gregor 
181ab0c8a84SDouglas Gregor   return 0;
182ab0c8a84SDouglas Gregor }
183ab0c8a84SDouglas Gregor 
1841fb5c3a6SDouglas Gregor bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) {
1851fb5c3a6SDouglas Gregor   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
1861fb5c3a6SDouglas Gregor     = Headers.find(Header);
1871fb5c3a6SDouglas Gregor   if (Known != Headers.end())
1881fb5c3a6SDouglas Gregor     return !Known->second->isAvailable();
1891fb5c3a6SDouglas Gregor 
1901fb5c3a6SDouglas Gregor   const DirectoryEntry *Dir = Header->getDir();
1911fb5c3a6SDouglas Gregor   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
1921fb5c3a6SDouglas Gregor   StringRef DirName = Dir->getName();
1931fb5c3a6SDouglas Gregor 
1941fb5c3a6SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
1951fb5c3a6SDouglas Gregor   // an umbrella header.
1961fb5c3a6SDouglas Gregor   do {
1971fb5c3a6SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
1981fb5c3a6SDouglas Gregor       = UmbrellaDirs.find(Dir);
1991fb5c3a6SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
2001fb5c3a6SDouglas Gregor       Module *Found = KnownDir->second;
2011fb5c3a6SDouglas Gregor       if (!Found->isAvailable())
2021fb5c3a6SDouglas Gregor         return true;
2031fb5c3a6SDouglas Gregor 
2041fb5c3a6SDouglas Gregor       // Search up the module stack until we find a module with an umbrella
2051fb5c3a6SDouglas Gregor       // directory.
2061fb5c3a6SDouglas Gregor       Module *UmbrellaModule = Found;
2071fb5c3a6SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
2081fb5c3a6SDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
2091fb5c3a6SDouglas Gregor 
2101fb5c3a6SDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
2111fb5c3a6SDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
2121fb5c3a6SDouglas Gregor           // Find or create the module that corresponds to this directory name.
2131fb5c3a6SDouglas Gregor           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
2141fb5c3a6SDouglas Gregor           Found = lookupModuleQualified(Name, Found);
2151fb5c3a6SDouglas Gregor           if (!Found)
2161fb5c3a6SDouglas Gregor             return false;
2171fb5c3a6SDouglas Gregor           if (!Found->isAvailable())
2181fb5c3a6SDouglas Gregor             return true;
2191fb5c3a6SDouglas Gregor         }
2201fb5c3a6SDouglas Gregor 
2211fb5c3a6SDouglas Gregor         // Infer a submodule with the same name as this header file.
2221fb5c3a6SDouglas Gregor         StringRef Name = llvm::sys::path::stem(Header->getName());
2231fb5c3a6SDouglas Gregor         Found = lookupModuleQualified(Name, Found);
2241fb5c3a6SDouglas Gregor         if (!Found)
2251fb5c3a6SDouglas Gregor           return false;
2261fb5c3a6SDouglas Gregor       }
2271fb5c3a6SDouglas Gregor 
2281fb5c3a6SDouglas Gregor       return !Found->isAvailable();
2291fb5c3a6SDouglas Gregor     }
2301fb5c3a6SDouglas Gregor 
2311fb5c3a6SDouglas Gregor     SkippedDirs.push_back(Dir);
2321fb5c3a6SDouglas Gregor 
2331fb5c3a6SDouglas Gregor     // Retrieve our parent path.
2341fb5c3a6SDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
2351fb5c3a6SDouglas Gregor     if (DirName.empty())
2361fb5c3a6SDouglas Gregor       break;
2371fb5c3a6SDouglas Gregor 
2381fb5c3a6SDouglas Gregor     // Resolve the parent path to a directory entry.
2391fb5c3a6SDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
2401fb5c3a6SDouglas Gregor   } while (Dir);
2411fb5c3a6SDouglas Gregor 
2421fb5c3a6SDouglas Gregor   return false;
2431fb5c3a6SDouglas Gregor }
2441fb5c3a6SDouglas Gregor 
245de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) {
24688bdfb0eSDouglas Gregor   llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
24788bdfb0eSDouglas Gregor   if (Known != Modules.end())
24888bdfb0eSDouglas Gregor     return Known->getValue();
24988bdfb0eSDouglas Gregor 
25088bdfb0eSDouglas Gregor   return 0;
25188bdfb0eSDouglas Gregor }
25288bdfb0eSDouglas Gregor 
2532b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
2542b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
2552b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
2562b82c2a5SDouglas Gregor       return Sub;
2572b82c2a5SDouglas Gregor   }
2582b82c2a5SDouglas Gregor 
2592b82c2a5SDouglas Gregor   return findModule(Name);
2602b82c2a5SDouglas Gregor }
2612b82c2a5SDouglas Gregor 
2622b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
2632b82c2a5SDouglas Gregor   if (!Context)
2642b82c2a5SDouglas Gregor     return findModule(Name);
2652b82c2a5SDouglas Gregor 
266*eb90e830SDouglas Gregor   return Context->findSubmodule(Name);
2672b82c2a5SDouglas Gregor }
2682b82c2a5SDouglas Gregor 
269de3ef502SDouglas Gregor std::pair<Module *, bool>
27069021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
27169021974SDouglas Gregor                               bool IsExplicit) {
27269021974SDouglas Gregor   // Try to find an existing module with this name.
273*eb90e830SDouglas Gregor   if (Module *Sub = lookupModuleQualified(Name, Parent))
274*eb90e830SDouglas Gregor     return std::make_pair(Sub, false);
27569021974SDouglas Gregor 
27669021974SDouglas Gregor   // Create a new module with this name.
27769021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
27869021974SDouglas Gregor                               IsExplicit);
279*eb90e830SDouglas Gregor   if (!Parent)
28069021974SDouglas Gregor     Modules[Name] = Result;
28169021974SDouglas Gregor   return std::make_pair(Result, true);
28269021974SDouglas Gregor }
28369021974SDouglas Gregor 
284de3ef502SDouglas Gregor Module *
28556c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
286e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
287e89dbc1dSDouglas Gregor                                 Module *Parent) {
28856c64013SDouglas Gregor   // Check whether we've already found this module.
289e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
290e89dbc1dSDouglas Gregor     return Mod;
291e89dbc1dSDouglas Gregor 
292e89dbc1dSDouglas Gregor   FileManager &FileMgr = SourceMgr->getFileManager();
29356c64013SDouglas Gregor 
29456c64013SDouglas Gregor   // Look for an umbrella header.
29556c64013SDouglas Gregor   llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
29656c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, "Headers");
29756c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
298e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
29956c64013SDouglas Gregor 
30056c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
30156c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
30256c64013SDouglas Gregor   // idea.
30356c64013SDouglas Gregor   if (!UmbrellaHeader)
30456c64013SDouglas Gregor     return 0;
30556c64013SDouglas Gregor 
306e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
307e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
308*eb90e830SDouglas Gregor   if (!Parent)
309e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
310e89dbc1dSDouglas Gregor 
311322f633cSDouglas Gregor   // umbrella header "umbrella-header-name"
31273141fa9SDouglas Gregor   Result->Umbrella = UmbrellaHeader;
31356c64013SDouglas Gregor   Headers[UmbrellaHeader] = Result;
3144dc71835SDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
315d8bd7537SDouglas Gregor 
316d8bd7537SDouglas Gregor   // export *
317d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
318d8bd7537SDouglas Gregor 
319a89c5ac4SDouglas Gregor   // module * { export * }
320a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
321a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
322a89c5ac4SDouglas Gregor 
323e89dbc1dSDouglas Gregor   // Look for subframeworks.
324e89dbc1dSDouglas Gregor   llvm::error_code EC;
325ddaa69cbSDouglas Gregor   llvm::SmallString<128> SubframeworksDirName
326ddaa69cbSDouglas Gregor     = StringRef(FrameworkDir->getName());
327e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
328ddaa69cbSDouglas Gregor   llvm::SmallString<128> SubframeworksDirNameNative;
329ddaa69cbSDouglas Gregor   llvm::sys::path::native(SubframeworksDirName.str(),
330ddaa69cbSDouglas Gregor                           SubframeworksDirNameNative);
331ddaa69cbSDouglas Gregor   for (llvm::sys::fs::directory_iterator
332ddaa69cbSDouglas Gregor          Dir(SubframeworksDirNameNative.str(), EC), DirEnd;
333e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
334e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
335e89dbc1dSDouglas Gregor       continue;
336f2161a70SDouglas Gregor 
337e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
338e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
339e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
340e89dbc1dSDouglas Gregor       inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
341e89dbc1dSDouglas Gregor                            Result);
342e89dbc1dSDouglas Gregor     }
343e89dbc1dSDouglas Gregor   }
344e89dbc1dSDouglas Gregor 
3459458f82dSDouglas Gregor   // Look for private headers.
3469458f82dSDouglas Gregor   llvm::SmallString<128> PrivateHeadersDirName(FrameworkDir->getName());
3479458f82dSDouglas Gregor   llvm::sys::path::append(PrivateHeadersDirName, "PrivateHeaders");
3487033127bSDouglas Gregor   if (const DirectoryEntry *Dir = FileMgr.getDirectory(PrivateHeadersDirName)) {
3497033127bSDouglas Gregor     Module *Private = findOrCreateModule("Private", Result,
3509458f82dSDouglas Gregor                                          /*IsFramework=*/false,
3519458f82dSDouglas Gregor                                          /*IsExplicit=*/true).first;
3527033127bSDouglas Gregor     setUmbrellaDir(Private, Dir);
3537033127bSDouglas Gregor     Private->InferSubmodules = true;
3547033127bSDouglas Gregor     Private->InferExplicitSubmodules = true;
3557033127bSDouglas Gregor     Private->InferExportWildcard = true;
3569458f82dSDouglas Gregor   }
3579458f82dSDouglas Gregor 
35856c64013SDouglas Gregor   return Result;
35956c64013SDouglas Gregor }
36056c64013SDouglas Gregor 
361a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
362a89c5ac4SDouglas Gregor   Headers[UmbrellaHeader] = Mod;
36373141fa9SDouglas Gregor   Mod->Umbrella = UmbrellaHeader;
3647033127bSDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
365a89c5ac4SDouglas Gregor }
366a89c5ac4SDouglas Gregor 
367524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
368524e33e1SDouglas Gregor   Mod->Umbrella = UmbrellaDir;
369524e33e1SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
370524e33e1SDouglas Gregor }
371524e33e1SDouglas Gregor 
372a89c5ac4SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
373a89c5ac4SDouglas Gregor   Mod->Headers.push_back(Header);
374a89c5ac4SDouglas Gregor   Headers[Header] = Mod;
375a89c5ac4SDouglas Gregor }
376a89c5ac4SDouglas Gregor 
377514b636aSDouglas Gregor const FileEntry *
378de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) {
379514b636aSDouglas Gregor   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
380514b636aSDouglas Gregor     return 0;
381514b636aSDouglas Gregor 
382514b636aSDouglas Gregor   return SourceMgr->getFileEntryForID(
383514b636aSDouglas Gregor            SourceMgr->getFileID(Module->DefinitionLoc));
384514b636aSDouglas Gregor }
385514b636aSDouglas Gregor 
386718292f2SDouglas Gregor void ModuleMap::dump() {
387718292f2SDouglas Gregor   llvm::errs() << "Modules:";
388718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
389718292f2SDouglas Gregor                                         MEnd = Modules.end();
390718292f2SDouglas Gregor        M != MEnd; ++M)
391d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
392718292f2SDouglas Gregor 
393718292f2SDouglas Gregor   llvm::errs() << "Headers:";
394718292f2SDouglas Gregor   for (llvm::DenseMap<const FileEntry *, Module *>::iterator
395718292f2SDouglas Gregor             H = Headers.begin(),
396718292f2SDouglas Gregor          HEnd = Headers.end();
397718292f2SDouglas Gregor        H != HEnd; ++H) {
398718292f2SDouglas Gregor     llvm::errs() << "  \"" << H->first->getName() << "\" -> "
399718292f2SDouglas Gregor                  << H->second->getFullModuleName() << "\n";
400718292f2SDouglas Gregor   }
401718292f2SDouglas Gregor }
402718292f2SDouglas Gregor 
4032b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
4042b82c2a5SDouglas Gregor   bool HadError = false;
4052b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
4062b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
4072b82c2a5SDouglas Gregor                                               Complain);
408f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
4092b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
4102b82c2a5SDouglas Gregor     else
4112b82c2a5SDouglas Gregor       HadError = true;
4122b82c2a5SDouglas Gregor   }
4132b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
4142b82c2a5SDouglas Gregor   return HadError;
4152b82c2a5SDouglas Gregor }
4162b82c2a5SDouglas Gregor 
4170093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
4180093b3c7SDouglas Gregor   if (Loc.isInvalid())
4190093b3c7SDouglas Gregor     return 0;
4200093b3c7SDouglas Gregor 
4210093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
4220093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
4230093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
4240093b3c7SDouglas Gregor     return 0;
4250093b3c7SDouglas Gregor 
4260093b3c7SDouglas Gregor 
4270093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
4280093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
4290093b3c7SDouglas Gregor   const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID);
4300093b3c7SDouglas Gregor   if (!ExpansionFile)
4310093b3c7SDouglas Gregor     return 0;
4320093b3c7SDouglas Gregor 
4330093b3c7SDouglas Gregor   // Find the module that owns this header.
4340093b3c7SDouglas Gregor   return findModuleForHeader(ExpansionFile);
4350093b3c7SDouglas Gregor }
4360093b3c7SDouglas Gregor 
437718292f2SDouglas Gregor //----------------------------------------------------------------------------//
438718292f2SDouglas Gregor // Module map file parser
439718292f2SDouglas Gregor //----------------------------------------------------------------------------//
440718292f2SDouglas Gregor 
441718292f2SDouglas Gregor namespace clang {
442718292f2SDouglas Gregor   /// \brief A token in a module map file.
443718292f2SDouglas Gregor   struct MMToken {
444718292f2SDouglas Gregor     enum TokenKind {
4451fb5c3a6SDouglas Gregor       Comma,
446718292f2SDouglas Gregor       EndOfFile,
447718292f2SDouglas Gregor       HeaderKeyword,
448718292f2SDouglas Gregor       Identifier,
449718292f2SDouglas Gregor       ExplicitKeyword,
4502b82c2a5SDouglas Gregor       ExportKeyword,
451755b2055SDouglas Gregor       FrameworkKeyword,
452718292f2SDouglas Gregor       ModuleKeyword,
4532b82c2a5SDouglas Gregor       Period,
454718292f2SDouglas Gregor       UmbrellaKeyword,
4551fb5c3a6SDouglas Gregor       RequiresKeyword,
4562b82c2a5SDouglas Gregor       Star,
457718292f2SDouglas Gregor       StringLiteral,
458718292f2SDouglas Gregor       LBrace,
459718292f2SDouglas Gregor       RBrace
460718292f2SDouglas Gregor     } Kind;
461718292f2SDouglas Gregor 
462718292f2SDouglas Gregor     unsigned Location;
463718292f2SDouglas Gregor     unsigned StringLength;
464718292f2SDouglas Gregor     const char *StringData;
465718292f2SDouglas Gregor 
466718292f2SDouglas Gregor     void clear() {
467718292f2SDouglas Gregor       Kind = EndOfFile;
468718292f2SDouglas Gregor       Location = 0;
469718292f2SDouglas Gregor       StringLength = 0;
470718292f2SDouglas Gregor       StringData = 0;
471718292f2SDouglas Gregor     }
472718292f2SDouglas Gregor 
473718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
474718292f2SDouglas Gregor 
475718292f2SDouglas Gregor     SourceLocation getLocation() const {
476718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
477718292f2SDouglas Gregor     }
478718292f2SDouglas Gregor 
479718292f2SDouglas Gregor     StringRef getString() const {
480718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
481718292f2SDouglas Gregor     }
482718292f2SDouglas Gregor   };
483718292f2SDouglas Gregor 
484718292f2SDouglas Gregor   class ModuleMapParser {
485718292f2SDouglas Gregor     Lexer &L;
486718292f2SDouglas Gregor     SourceManager &SourceMgr;
487718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
488718292f2SDouglas Gregor     ModuleMap &Map;
489718292f2SDouglas Gregor 
4905257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
4915257fc63SDouglas Gregor     const DirectoryEntry *Directory;
4925257fc63SDouglas Gregor 
493718292f2SDouglas Gregor     /// \brief Whether an error occurred.
494718292f2SDouglas Gregor     bool HadError;
495718292f2SDouglas Gregor 
496718292f2SDouglas Gregor     /// \brief Default target information, used only for string literal
497718292f2SDouglas Gregor     /// parsing.
498718292f2SDouglas Gregor     TargetInfo *Target;
499718292f2SDouglas Gregor 
500718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
501718292f2SDouglas Gregor     /// during parsing.
502718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
503718292f2SDouglas Gregor 
504718292f2SDouglas Gregor     /// \brief The current token.
505718292f2SDouglas Gregor     MMToken Tok;
506718292f2SDouglas Gregor 
507718292f2SDouglas Gregor     /// \brief The active module.
508de3ef502SDouglas Gregor     Module *ActiveModule;
509718292f2SDouglas Gregor 
510718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
511718292f2SDouglas Gregor     SourceLocation consumeToken();
512718292f2SDouglas Gregor 
513718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
514718292f2SDouglas Gregor     /// (or the end of the file).
515718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
516718292f2SDouglas Gregor 
517e7ab3669SDouglas Gregor     typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
518e7ab3669SDouglas Gregor       ModuleId;
519e7ab3669SDouglas Gregor     bool parseModuleId(ModuleId &Id);
520718292f2SDouglas Gregor     void parseModuleDecl();
5211fb5c3a6SDouglas Gregor     void parseRequiresDecl();
522322f633cSDouglas Gregor     void parseHeaderDecl(SourceLocation UmbrellaLoc);
523524e33e1SDouglas Gregor     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
5242b82c2a5SDouglas Gregor     void parseExportDecl();
52573441091SDouglas Gregor     void parseInferredSubmoduleDecl(bool Explicit);
526718292f2SDouglas Gregor 
5277033127bSDouglas Gregor     const DirectoryEntry *getOverriddenHeaderSearchDir();
5287033127bSDouglas Gregor 
529718292f2SDouglas Gregor   public:
530718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
531718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
5325257fc63SDouglas Gregor                              ModuleMap &Map,
5335257fc63SDouglas Gregor                              const DirectoryEntry *Directory)
5345257fc63SDouglas Gregor       : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
5355257fc63SDouglas Gregor         Directory(Directory), HadError(false), ActiveModule(0)
536718292f2SDouglas Gregor     {
537718292f2SDouglas Gregor       TargetOptions TargetOpts;
538718292f2SDouglas Gregor       TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
539718292f2SDouglas Gregor       Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
540718292f2SDouglas Gregor 
541718292f2SDouglas Gregor       Tok.clear();
542718292f2SDouglas Gregor       consumeToken();
543718292f2SDouglas Gregor     }
544718292f2SDouglas Gregor 
545718292f2SDouglas Gregor     bool parseModuleMapFile();
546718292f2SDouglas Gregor   };
547718292f2SDouglas Gregor }
548718292f2SDouglas Gregor 
549718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
550718292f2SDouglas Gregor retry:
551718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
552718292f2SDouglas Gregor   Tok.clear();
553718292f2SDouglas Gregor 
554718292f2SDouglas Gregor   Token LToken;
555718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
556718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
557718292f2SDouglas Gregor   switch (LToken.getKind()) {
558718292f2SDouglas Gregor   case tok::raw_identifier:
559718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
560718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
561718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
562718292f2SDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
563718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
5642b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
565755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
566718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
5671fb5c3a6SDouglas Gregor                  .Case("requires", MMToken::RequiresKeyword)
568718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
569718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
570718292f2SDouglas Gregor     break;
571718292f2SDouglas Gregor 
5721fb5c3a6SDouglas Gregor   case tok::comma:
5731fb5c3a6SDouglas Gregor     Tok.Kind = MMToken::Comma;
5741fb5c3a6SDouglas Gregor     break;
5751fb5c3a6SDouglas Gregor 
576718292f2SDouglas Gregor   case tok::eof:
577718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
578718292f2SDouglas Gregor     break;
579718292f2SDouglas Gregor 
580718292f2SDouglas Gregor   case tok::l_brace:
581718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
582718292f2SDouglas Gregor     break;
583718292f2SDouglas Gregor 
5842b82c2a5SDouglas Gregor   case tok::period:
5852b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
5862b82c2a5SDouglas Gregor     break;
5872b82c2a5SDouglas Gregor 
588718292f2SDouglas Gregor   case tok::r_brace:
589718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
590718292f2SDouglas Gregor     break;
591718292f2SDouglas Gregor 
5922b82c2a5SDouglas Gregor   case tok::star:
5932b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
5942b82c2a5SDouglas Gregor     break;
5952b82c2a5SDouglas Gregor 
596718292f2SDouglas Gregor   case tok::string_literal: {
597718292f2SDouglas Gregor     // Parse the string literal.
598718292f2SDouglas Gregor     LangOptions LangOpts;
599718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
600718292f2SDouglas Gregor     if (StringLiteral.hadError)
601718292f2SDouglas Gregor       goto retry;
602718292f2SDouglas Gregor 
603718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
604718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
605718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
606718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
607718292f2SDouglas Gregor     Saved[Length] = 0;
608718292f2SDouglas Gregor 
609718292f2SDouglas Gregor     // Form the token.
610718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
611718292f2SDouglas Gregor     Tok.StringData = Saved;
612718292f2SDouglas Gregor     Tok.StringLength = Length;
613718292f2SDouglas Gregor     break;
614718292f2SDouglas Gregor   }
615718292f2SDouglas Gregor 
616718292f2SDouglas Gregor   case tok::comment:
617718292f2SDouglas Gregor     goto retry;
618718292f2SDouglas Gregor 
619718292f2SDouglas Gregor   default:
620718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
621718292f2SDouglas Gregor     HadError = true;
622718292f2SDouglas Gregor     goto retry;
623718292f2SDouglas Gregor   }
624718292f2SDouglas Gregor 
625718292f2SDouglas Gregor   return Result;
626718292f2SDouglas Gregor }
627718292f2SDouglas Gregor 
628718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
629718292f2SDouglas Gregor   unsigned braceDepth = 0;
630718292f2SDouglas Gregor   do {
631718292f2SDouglas Gregor     switch (Tok.Kind) {
632718292f2SDouglas Gregor     case MMToken::EndOfFile:
633718292f2SDouglas Gregor       return;
634718292f2SDouglas Gregor 
635718292f2SDouglas Gregor     case MMToken::LBrace:
636718292f2SDouglas Gregor       if (Tok.is(K) && braceDepth == 0)
637718292f2SDouglas Gregor         return;
638718292f2SDouglas Gregor 
639718292f2SDouglas Gregor       ++braceDepth;
640718292f2SDouglas Gregor       break;
641718292f2SDouglas Gregor 
642718292f2SDouglas Gregor     case MMToken::RBrace:
643718292f2SDouglas Gregor       if (braceDepth > 0)
644718292f2SDouglas Gregor         --braceDepth;
645718292f2SDouglas Gregor       else if (Tok.is(K))
646718292f2SDouglas Gregor         return;
647718292f2SDouglas Gregor       break;
648718292f2SDouglas Gregor 
649718292f2SDouglas Gregor     default:
650718292f2SDouglas Gregor       if (braceDepth == 0 && Tok.is(K))
651718292f2SDouglas Gregor         return;
652718292f2SDouglas Gregor       break;
653718292f2SDouglas Gregor     }
654718292f2SDouglas Gregor 
655718292f2SDouglas Gregor    consumeToken();
656718292f2SDouglas Gregor   } while (true);
657718292f2SDouglas Gregor }
658718292f2SDouglas Gregor 
659e7ab3669SDouglas Gregor /// \brief Parse a module-id.
660e7ab3669SDouglas Gregor ///
661e7ab3669SDouglas Gregor ///   module-id:
662e7ab3669SDouglas Gregor ///     identifier
663e7ab3669SDouglas Gregor ///     identifier '.' module-id
664e7ab3669SDouglas Gregor ///
665e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise.
666e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) {
667e7ab3669SDouglas Gregor   Id.clear();
668e7ab3669SDouglas Gregor   do {
669e7ab3669SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
670e7ab3669SDouglas Gregor       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
671e7ab3669SDouglas Gregor       consumeToken();
672e7ab3669SDouglas Gregor     } else {
673e7ab3669SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
674e7ab3669SDouglas Gregor       return true;
675e7ab3669SDouglas Gregor     }
676e7ab3669SDouglas Gregor 
677e7ab3669SDouglas Gregor     if (!Tok.is(MMToken::Period))
678e7ab3669SDouglas Gregor       break;
679e7ab3669SDouglas Gregor 
680e7ab3669SDouglas Gregor     consumeToken();
681e7ab3669SDouglas Gregor   } while (true);
682e7ab3669SDouglas Gregor 
683e7ab3669SDouglas Gregor   return false;
684e7ab3669SDouglas Gregor }
685e7ab3669SDouglas Gregor 
686718292f2SDouglas Gregor /// \brief Parse a module declaration.
687718292f2SDouglas Gregor ///
688718292f2SDouglas Gregor ///   module-declaration:
689e7ab3669SDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' module-id { module-member* }
690718292f2SDouglas Gregor ///
691718292f2SDouglas Gregor ///   module-member:
6921fb5c3a6SDouglas Gregor ///     requires-declaration
693718292f2SDouglas Gregor ///     header-declaration
694e7ab3669SDouglas Gregor ///     submodule-declaration
6952b82c2a5SDouglas Gregor ///     export-declaration
69673441091SDouglas Gregor ///
69773441091SDouglas Gregor ///   submodule-declaration:
69873441091SDouglas Gregor ///     module-declaration
69973441091SDouglas Gregor ///     inferred-submodule-declaration
700718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
701755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
702755b2055SDouglas Gregor          Tok.is(MMToken::FrameworkKeyword));
703f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
704e7ab3669SDouglas Gregor   SourceLocation ExplicitLoc;
705718292f2SDouglas Gregor   bool Explicit = false;
706f2161a70SDouglas Gregor   bool Framework = false;
707755b2055SDouglas Gregor 
708f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
709f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
710e7ab3669SDouglas Gregor     ExplicitLoc = consumeToken();
711f2161a70SDouglas Gregor     Explicit = true;
712f2161a70SDouglas Gregor   }
713f2161a70SDouglas Gregor 
714f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
715755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
716755b2055SDouglas Gregor     consumeToken();
717755b2055SDouglas Gregor     Framework = true;
718755b2055SDouglas Gregor   }
719718292f2SDouglas Gregor 
720718292f2SDouglas Gregor   // Parse 'module' keyword.
721718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
722d6343c99SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
723718292f2SDouglas Gregor     consumeToken();
724718292f2SDouglas Gregor     HadError = true;
725718292f2SDouglas Gregor     return;
726718292f2SDouglas Gregor   }
727718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
728718292f2SDouglas Gregor 
72973441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
73073441091SDouglas Gregor   // Parse it.
73173441091SDouglas Gregor   if (Tok.is(MMToken::Star))
73273441091SDouglas Gregor     return parseInferredSubmoduleDecl(Explicit);
73373441091SDouglas Gregor 
734718292f2SDouglas Gregor   // Parse the module name.
735e7ab3669SDouglas Gregor   ModuleId Id;
736e7ab3669SDouglas Gregor   if (parseModuleId(Id)) {
737718292f2SDouglas Gregor     HadError = true;
738718292f2SDouglas Gregor     return;
739718292f2SDouglas Gregor   }
740e7ab3669SDouglas Gregor 
741e7ab3669SDouglas Gregor   if (ActiveModule) {
742e7ab3669SDouglas Gregor     if (Id.size() > 1) {
743e7ab3669SDouglas Gregor       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
744e7ab3669SDouglas Gregor         << SourceRange(Id.front().second, Id.back().second);
745e7ab3669SDouglas Gregor 
746e7ab3669SDouglas Gregor       HadError = true;
747e7ab3669SDouglas Gregor       return;
748e7ab3669SDouglas Gregor     }
749e7ab3669SDouglas Gregor   } else if (Id.size() == 1 && Explicit) {
750e7ab3669SDouglas Gregor     // Top-level modules can't be explicit.
751e7ab3669SDouglas Gregor     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
752e7ab3669SDouglas Gregor     Explicit = false;
753e7ab3669SDouglas Gregor     ExplicitLoc = SourceLocation();
754e7ab3669SDouglas Gregor     HadError = true;
755e7ab3669SDouglas Gregor   }
756e7ab3669SDouglas Gregor 
757e7ab3669SDouglas Gregor   Module *PreviousActiveModule = ActiveModule;
758e7ab3669SDouglas Gregor   if (Id.size() > 1) {
759e7ab3669SDouglas Gregor     // This module map defines a submodule. Go find the module of which it
760e7ab3669SDouglas Gregor     // is a submodule.
761e7ab3669SDouglas Gregor     ActiveModule = 0;
762e7ab3669SDouglas Gregor     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
763e7ab3669SDouglas Gregor       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
764e7ab3669SDouglas Gregor         ActiveModule = Next;
765e7ab3669SDouglas Gregor         continue;
766e7ab3669SDouglas Gregor       }
767e7ab3669SDouglas Gregor 
768e7ab3669SDouglas Gregor       if (ActiveModule) {
769e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
770e7ab3669SDouglas Gregor           << Id[I].first << ActiveModule->getTopLevelModule();
771e7ab3669SDouglas Gregor       } else {
772e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
773e7ab3669SDouglas Gregor       }
774e7ab3669SDouglas Gregor       HadError = true;
775e7ab3669SDouglas Gregor       return;
776e7ab3669SDouglas Gregor     }
777e7ab3669SDouglas Gregor   }
778e7ab3669SDouglas Gregor 
779e7ab3669SDouglas Gregor   StringRef ModuleName = Id.back().first;
780e7ab3669SDouglas Gregor   SourceLocation ModuleNameLoc = Id.back().second;
781718292f2SDouglas Gregor 
782718292f2SDouglas Gregor   // Parse the opening brace.
783718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
784718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
785718292f2SDouglas Gregor       << ModuleName;
786718292f2SDouglas Gregor     HadError = true;
787718292f2SDouglas Gregor     return;
788718292f2SDouglas Gregor   }
789718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
790718292f2SDouglas Gregor 
791718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
792*eb90e830SDouglas Gregor   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
793718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
794718292f2SDouglas Gregor       << ModuleName;
795*eb90e830SDouglas Gregor     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
796718292f2SDouglas Gregor 
797718292f2SDouglas Gregor     // Skip the module definition.
798718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
799718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
800718292f2SDouglas Gregor       consumeToken();
801718292f2SDouglas Gregor 
802718292f2SDouglas Gregor     HadError = true;
803718292f2SDouglas Gregor     return;
804718292f2SDouglas Gregor   }
805718292f2SDouglas Gregor 
806718292f2SDouglas Gregor   // Start defining this module.
807*eb90e830SDouglas Gregor   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
808*eb90e830SDouglas Gregor                                         Explicit).first;
809*eb90e830SDouglas Gregor   ActiveModule->DefinitionLoc = ModuleNameLoc;
810718292f2SDouglas Gregor 
811718292f2SDouglas Gregor   bool Done = false;
812718292f2SDouglas Gregor   do {
813718292f2SDouglas Gregor     switch (Tok.Kind) {
814718292f2SDouglas Gregor     case MMToken::EndOfFile:
815718292f2SDouglas Gregor     case MMToken::RBrace:
816718292f2SDouglas Gregor       Done = true;
817718292f2SDouglas Gregor       break;
818718292f2SDouglas Gregor 
819718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
820f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
821718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
822718292f2SDouglas Gregor       parseModuleDecl();
823718292f2SDouglas Gregor       break;
824718292f2SDouglas Gregor 
8252b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
8262b82c2a5SDouglas Gregor       parseExportDecl();
8272b82c2a5SDouglas Gregor       break;
8282b82c2a5SDouglas Gregor 
8291fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
8301fb5c3a6SDouglas Gregor       parseRequiresDecl();
8311fb5c3a6SDouglas Gregor       break;
8321fb5c3a6SDouglas Gregor 
833524e33e1SDouglas Gregor     case MMToken::UmbrellaKeyword: {
834524e33e1SDouglas Gregor       SourceLocation UmbrellaLoc = consumeToken();
835524e33e1SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword))
836524e33e1SDouglas Gregor         parseHeaderDecl(UmbrellaLoc);
837524e33e1SDouglas Gregor       else
838524e33e1SDouglas Gregor         parseUmbrellaDirDecl(UmbrellaLoc);
839718292f2SDouglas Gregor       break;
840524e33e1SDouglas Gregor     }
841718292f2SDouglas Gregor 
842322f633cSDouglas Gregor     case MMToken::HeaderKeyword:
843322f633cSDouglas Gregor       parseHeaderDecl(SourceLocation());
844718292f2SDouglas Gregor       break;
845718292f2SDouglas Gregor 
846718292f2SDouglas Gregor     default:
847718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
848718292f2SDouglas Gregor       consumeToken();
849718292f2SDouglas Gregor       break;
850718292f2SDouglas Gregor     }
851718292f2SDouglas Gregor   } while (!Done);
852718292f2SDouglas Gregor 
853718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
854718292f2SDouglas Gregor     consumeToken();
855718292f2SDouglas Gregor   else {
856718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
857718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
858718292f2SDouglas Gregor     HadError = true;
859718292f2SDouglas Gregor   }
860718292f2SDouglas Gregor 
861e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
862e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
863718292f2SDouglas Gregor }
864718292f2SDouglas Gregor 
8651fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration.
8661fb5c3a6SDouglas Gregor ///
8671fb5c3a6SDouglas Gregor ///   requires-declaration:
8681fb5c3a6SDouglas Gregor ///     'requires' feature-list
8691fb5c3a6SDouglas Gregor ///
8701fb5c3a6SDouglas Gregor ///   feature-list:
8711fb5c3a6SDouglas Gregor ///     identifier ',' feature-list
8721fb5c3a6SDouglas Gregor ///     identifier
8731fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() {
8741fb5c3a6SDouglas Gregor   assert(Tok.is(MMToken::RequiresKeyword));
8751fb5c3a6SDouglas Gregor 
8761fb5c3a6SDouglas Gregor   // Parse 'requires' keyword.
8771fb5c3a6SDouglas Gregor   consumeToken();
8781fb5c3a6SDouglas Gregor 
8791fb5c3a6SDouglas Gregor   // Parse the feature-list.
8801fb5c3a6SDouglas Gregor   do {
8811fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
8821fb5c3a6SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
8831fb5c3a6SDouglas Gregor       HadError = true;
8841fb5c3a6SDouglas Gregor       return;
8851fb5c3a6SDouglas Gregor     }
8861fb5c3a6SDouglas Gregor 
8871fb5c3a6SDouglas Gregor     // Consume the feature name.
8881fb5c3a6SDouglas Gregor     std::string Feature = Tok.getString();
8891fb5c3a6SDouglas Gregor     consumeToken();
8901fb5c3a6SDouglas Gregor 
8911fb5c3a6SDouglas Gregor     // Add this feature.
8921fb5c3a6SDouglas Gregor     ActiveModule->addRequirement(Feature, Map.LangOpts);
8931fb5c3a6SDouglas Gregor 
8941fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Comma))
8951fb5c3a6SDouglas Gregor       break;
8961fb5c3a6SDouglas Gregor 
8971fb5c3a6SDouglas Gregor     // Consume the comma.
8981fb5c3a6SDouglas Gregor     consumeToken();
8991fb5c3a6SDouglas Gregor   } while (true);
9001fb5c3a6SDouglas Gregor }
9011fb5c3a6SDouglas Gregor 
902f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
903f2161a70SDouglas Gregor /// subframework in which the given module lives.
904f2161a70SDouglas Gregor void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) {
905f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
906f2161a70SDouglas Gregor   llvm::SmallVector<StringRef, 2> Paths;
907f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
908f2161a70SDouglas Gregor     if (Mod->IsFramework)
909f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
910f2161a70SDouglas Gregor   }
911f2161a70SDouglas Gregor 
912f2161a70SDouglas Gregor   if (Paths.empty())
913f2161a70SDouglas Gregor     return;
914f2161a70SDouglas Gregor 
915f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
916f2161a70SDouglas Gregor   for (unsigned I = Paths.size() - 1; I != 0; --I) {
917f2161a70SDouglas Gregor     llvm::sys::path::append(Path, "Frameworks");
918f2161a70SDouglas Gregor     llvm::sys::path::append(Path, Paths[I-1] + ".framework");
919f2161a70SDouglas Gregor   }
920f2161a70SDouglas Gregor }
921f2161a70SDouglas Gregor 
922718292f2SDouglas Gregor /// \brief Parse a header declaration.
923718292f2SDouglas Gregor ///
924718292f2SDouglas Gregor ///   header-declaration:
925322f633cSDouglas Gregor ///     'umbrella'[opt] 'header' string-literal
926322f633cSDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) {
927718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
9281871ed3dSBenjamin Kramer   consumeToken();
929718292f2SDouglas Gregor 
930322f633cSDouglas Gregor   bool Umbrella = UmbrellaLoc.isValid();
931322f633cSDouglas Gregor 
932718292f2SDouglas Gregor   // Parse the header name.
933718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
934718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
935718292f2SDouglas Gregor       << "header";
936718292f2SDouglas Gregor     HadError = true;
937718292f2SDouglas Gregor     return;
938718292f2SDouglas Gregor   }
939e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
940718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
941718292f2SDouglas Gregor 
942524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
943524e33e1SDouglas Gregor   if (Umbrella && ActiveModule->Umbrella) {
944524e33e1SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
945524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
946322f633cSDouglas Gregor     HadError = true;
947322f633cSDouglas Gregor     return;
948322f633cSDouglas Gregor   }
949322f633cSDouglas Gregor 
9505257fc63SDouglas Gregor   // Look for this file.
951e7ab3669SDouglas Gregor   const FileEntry *File = 0;
9525257fc63SDouglas Gregor   llvm::SmallString<128> PathName;
953e7ab3669SDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
954e7ab3669SDouglas Gregor     PathName = FileName;
955e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
9567033127bSDouglas Gregor   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
9577033127bSDouglas Gregor     PathName = Dir->getName();
9587033127bSDouglas Gregor     llvm::sys::path::append(PathName, FileName);
9597033127bSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
960e7ab3669SDouglas Gregor   } else {
961e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
9627033127bSDouglas Gregor     PathName = Directory->getName();
963e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
964755b2055SDouglas Gregor 
965f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
966f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
967755b2055SDouglas Gregor 
968e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
969e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
9705257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
971e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
972e7ab3669SDouglas Gregor 
973e7ab3669SDouglas Gregor       if (!File) {
974e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
975e7ab3669SDouglas Gregor         PathName.resize(PathLength);
976e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
977e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
978e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
979e7ab3669SDouglas Gregor       }
980e7ab3669SDouglas Gregor     } else {
981e7ab3669SDouglas Gregor       // Lookup for normal headers.
982e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
983e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
984e7ab3669SDouglas Gregor     }
985e7ab3669SDouglas Gregor   }
9865257fc63SDouglas Gregor 
9875257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
9885257fc63SDouglas Gregor   // Come up with a lazy way to do this.
989e7ab3669SDouglas Gregor   if (File) {
9905257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
9915257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
9925257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
9935257fc63SDouglas Gregor       HadError = true;
994322f633cSDouglas Gregor     } else if (Umbrella) {
995322f633cSDouglas Gregor       const DirectoryEntry *UmbrellaDir = File->getDir();
996322f633cSDouglas Gregor       if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
997322f633cSDouglas Gregor         Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
998322f633cSDouglas Gregor           << OwningModule->getFullModuleName();
999322f633cSDouglas Gregor         HadError = true;
10005257fc63SDouglas Gregor       } else {
1001322f633cSDouglas Gregor         // Record this umbrella header.
1002322f633cSDouglas Gregor         Map.setUmbrellaHeader(ActiveModule, File);
1003322f633cSDouglas Gregor       }
1004322f633cSDouglas Gregor     } else {
1005322f633cSDouglas Gregor       // Record this header.
1006a89c5ac4SDouglas Gregor       Map.addHeader(ActiveModule, File);
10075257fc63SDouglas Gregor     }
10085257fc63SDouglas Gregor   } else {
10095257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
1010524e33e1SDouglas Gregor       << Umbrella << FileName;
10115257fc63SDouglas Gregor     HadError = true;
10125257fc63SDouglas Gregor   }
1013718292f2SDouglas Gregor }
1014718292f2SDouglas Gregor 
1015524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration.
1016524e33e1SDouglas Gregor ///
1017524e33e1SDouglas Gregor ///   umbrella-dir-declaration:
1018524e33e1SDouglas Gregor ///     umbrella string-literal
1019524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1020524e33e1SDouglas Gregor   // Parse the directory name.
1021524e33e1SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1022524e33e1SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1023524e33e1SDouglas Gregor       << "umbrella";
1024524e33e1SDouglas Gregor     HadError = true;
1025524e33e1SDouglas Gregor     return;
1026524e33e1SDouglas Gregor   }
1027524e33e1SDouglas Gregor 
1028524e33e1SDouglas Gregor   std::string DirName = Tok.getString();
1029524e33e1SDouglas Gregor   SourceLocation DirNameLoc = consumeToken();
1030524e33e1SDouglas Gregor 
1031524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1032524e33e1SDouglas Gregor   if (ActiveModule->Umbrella) {
1033524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1034524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1035524e33e1SDouglas Gregor     HadError = true;
1036524e33e1SDouglas Gregor     return;
1037524e33e1SDouglas Gregor   }
1038524e33e1SDouglas Gregor 
1039524e33e1SDouglas Gregor   // Look for this file.
1040524e33e1SDouglas Gregor   const DirectoryEntry *Dir = 0;
1041524e33e1SDouglas Gregor   if (llvm::sys::path::is_absolute(DirName))
1042524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1043524e33e1SDouglas Gregor   else {
1044524e33e1SDouglas Gregor     llvm::SmallString<128> PathName;
1045524e33e1SDouglas Gregor     PathName = Directory->getName();
1046524e33e1SDouglas Gregor     llvm::sys::path::append(PathName, DirName);
1047524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1048524e33e1SDouglas Gregor   }
1049524e33e1SDouglas Gregor 
1050524e33e1SDouglas Gregor   if (!Dir) {
1051524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1052524e33e1SDouglas Gregor       << DirName;
1053524e33e1SDouglas Gregor     HadError = true;
1054524e33e1SDouglas Gregor     return;
1055524e33e1SDouglas Gregor   }
1056524e33e1SDouglas Gregor 
1057524e33e1SDouglas Gregor   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1058524e33e1SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1059524e33e1SDouglas Gregor       << OwningModule->getFullModuleName();
1060524e33e1SDouglas Gregor     HadError = true;
1061524e33e1SDouglas Gregor     return;
1062524e33e1SDouglas Gregor   }
1063524e33e1SDouglas Gregor 
1064524e33e1SDouglas Gregor   // Record this umbrella directory.
1065524e33e1SDouglas Gregor   Map.setUmbrellaDir(ActiveModule, Dir);
1066524e33e1SDouglas Gregor }
1067524e33e1SDouglas Gregor 
10682b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
10692b82c2a5SDouglas Gregor ///
10702b82c2a5SDouglas Gregor ///   export-declaration:
10712b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
10722b82c2a5SDouglas Gregor ///
10732b82c2a5SDouglas Gregor ///   wildcard-module-id:
10742b82c2a5SDouglas Gregor ///     identifier
10752b82c2a5SDouglas Gregor ///     '*'
10762b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
10772b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
10782b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
10792b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
10802b82c2a5SDouglas Gregor 
10812b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
10822b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
10832b82c2a5SDouglas Gregor   bool Wildcard = false;
10842b82c2a5SDouglas Gregor   do {
10852b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
10862b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
10872b82c2a5SDouglas Gregor                                               Tok.getLocation()));
10882b82c2a5SDouglas Gregor       consumeToken();
10892b82c2a5SDouglas Gregor 
10902b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
10912b82c2a5SDouglas Gregor         consumeToken();
10922b82c2a5SDouglas Gregor         continue;
10932b82c2a5SDouglas Gregor       }
10942b82c2a5SDouglas Gregor 
10952b82c2a5SDouglas Gregor       break;
10962b82c2a5SDouglas Gregor     }
10972b82c2a5SDouglas Gregor 
10982b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
10992b82c2a5SDouglas Gregor       Wildcard = true;
1100f5eedd05SDouglas Gregor       consumeToken();
11012b82c2a5SDouglas Gregor       break;
11022b82c2a5SDouglas Gregor     }
11032b82c2a5SDouglas Gregor 
11042b82c2a5SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
11052b82c2a5SDouglas Gregor     HadError = true;
11062b82c2a5SDouglas Gregor     return;
11072b82c2a5SDouglas Gregor   } while (true);
11082b82c2a5SDouglas Gregor 
11092b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
11102b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
11112b82c2a5SDouglas Gregor   };
11122b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
11132b82c2a5SDouglas Gregor }
11142b82c2a5SDouglas Gregor 
111573441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
111673441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
111773441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
111873441091SDouglas Gregor   bool Failed = false;
111973441091SDouglas Gregor 
112073441091SDouglas Gregor   // Inferred modules must be submodules.
112173441091SDouglas Gregor   if (!ActiveModule) {
112273441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
112373441091SDouglas Gregor     Failed = true;
112473441091SDouglas Gregor   }
112573441091SDouglas Gregor 
1126524e33e1SDouglas Gregor   // Inferred modules must have umbrella directories.
1127524e33e1SDouglas Gregor   if (!Failed && !ActiveModule->getUmbrellaDir()) {
112873441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
112973441091SDouglas Gregor     Failed = true;
113073441091SDouglas Gregor   }
113173441091SDouglas Gregor 
113273441091SDouglas Gregor   // Check for redefinition of an inferred module.
1133dd005f69SDouglas Gregor   if (!Failed && ActiveModule->InferSubmodules) {
113473441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1135dd005f69SDouglas Gregor     if (ActiveModule->InferredSubmoduleLoc.isValid())
1136dd005f69SDouglas Gregor       Diags.Report(ActiveModule->InferredSubmoduleLoc,
113773441091SDouglas Gregor                    diag::note_mmap_prev_definition);
113873441091SDouglas Gregor     Failed = true;
113973441091SDouglas Gregor   }
114073441091SDouglas Gregor 
114173441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
114273441091SDouglas Gregor   if (Failed) {
114373441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
114473441091SDouglas Gregor       consumeToken();
114573441091SDouglas Gregor       skipUntil(MMToken::RBrace);
114673441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
114773441091SDouglas Gregor         consumeToken();
114873441091SDouglas Gregor     }
114973441091SDouglas Gregor     HadError = true;
115073441091SDouglas Gregor     return;
115173441091SDouglas Gregor   }
115273441091SDouglas Gregor 
115373441091SDouglas Gregor   // Note that we have an inferred submodule.
1154dd005f69SDouglas Gregor   ActiveModule->InferSubmodules = true;
1155dd005f69SDouglas Gregor   ActiveModule->InferredSubmoduleLoc = StarLoc;
1156dd005f69SDouglas Gregor   ActiveModule->InferExplicitSubmodules = Explicit;
115773441091SDouglas Gregor 
115873441091SDouglas Gregor   // Parse the opening brace.
115973441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
116073441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
116173441091SDouglas Gregor     HadError = true;
116273441091SDouglas Gregor     return;
116373441091SDouglas Gregor   }
116473441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
116573441091SDouglas Gregor 
116673441091SDouglas Gregor   // Parse the body of the inferred submodule.
116773441091SDouglas Gregor   bool Done = false;
116873441091SDouglas Gregor   do {
116973441091SDouglas Gregor     switch (Tok.Kind) {
117073441091SDouglas Gregor     case MMToken::EndOfFile:
117173441091SDouglas Gregor     case MMToken::RBrace:
117273441091SDouglas Gregor       Done = true;
117373441091SDouglas Gregor       break;
117473441091SDouglas Gregor 
117573441091SDouglas Gregor     case MMToken::ExportKeyword: {
117673441091SDouglas Gregor       consumeToken();
117773441091SDouglas Gregor       if (Tok.is(MMToken::Star))
1178dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
117973441091SDouglas Gregor       else
118073441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
118173441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
118273441091SDouglas Gregor       consumeToken();
118373441091SDouglas Gregor       break;
118473441091SDouglas Gregor     }
118573441091SDouglas Gregor 
118673441091SDouglas Gregor     case MMToken::ExplicitKeyword:
118773441091SDouglas Gregor     case MMToken::ModuleKeyword:
118873441091SDouglas Gregor     case MMToken::HeaderKeyword:
118973441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
119073441091SDouglas Gregor     default:
119173441091SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
119273441091SDouglas Gregor       consumeToken();
119373441091SDouglas Gregor       break;
119473441091SDouglas Gregor     }
119573441091SDouglas Gregor   } while (!Done);
119673441091SDouglas Gregor 
119773441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
119873441091SDouglas Gregor     consumeToken();
119973441091SDouglas Gregor   else {
120073441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
120173441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
120273441091SDouglas Gregor     HadError = true;
120373441091SDouglas Gregor   }
120473441091SDouglas Gregor }
120573441091SDouglas Gregor 
12067033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence
12077033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
12087033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
12097033127bSDouglas Gregor   for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
12107033127bSDouglas Gregor     // If we have an umbrella directory, use that.
12117033127bSDouglas Gregor     if (Mod->hasUmbrellaDir())
12127033127bSDouglas Gregor       return Mod->getUmbrellaDir();
12137033127bSDouglas Gregor 
12147033127bSDouglas Gregor     // If we have a framework directory, stop looking.
12157033127bSDouglas Gregor     if (Mod->IsFramework)
12167033127bSDouglas Gregor       return 0;
12177033127bSDouglas Gregor   }
12187033127bSDouglas Gregor 
12197033127bSDouglas Gregor   return 0;
12207033127bSDouglas Gregor }
12217033127bSDouglas Gregor 
1222718292f2SDouglas Gregor /// \brief Parse a module map file.
1223718292f2SDouglas Gregor ///
1224718292f2SDouglas Gregor ///   module-map-file:
1225718292f2SDouglas Gregor ///     module-declaration*
1226718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
1227718292f2SDouglas Gregor   do {
1228718292f2SDouglas Gregor     switch (Tok.Kind) {
1229718292f2SDouglas Gregor     case MMToken::EndOfFile:
1230718292f2SDouglas Gregor       return HadError;
1231718292f2SDouglas Gregor 
1232e7ab3669SDouglas Gregor     case MMToken::ExplicitKeyword:
1233718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1234755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
1235718292f2SDouglas Gregor       parseModuleDecl();
1236718292f2SDouglas Gregor       break;
1237718292f2SDouglas Gregor 
12381fb5c3a6SDouglas Gregor     case MMToken::Comma:
12392b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
1240718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
1241718292f2SDouglas Gregor     case MMToken::Identifier:
1242718292f2SDouglas Gregor     case MMToken::LBrace:
12432b82c2a5SDouglas Gregor     case MMToken::Period:
1244718292f2SDouglas Gregor     case MMToken::RBrace:
12451fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
12462b82c2a5SDouglas Gregor     case MMToken::Star:
1247718292f2SDouglas Gregor     case MMToken::StringLiteral:
1248718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
1249718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1250718292f2SDouglas Gregor       HadError = true;
1251718292f2SDouglas Gregor       consumeToken();
1252718292f2SDouglas Gregor       break;
1253718292f2SDouglas Gregor     }
1254718292f2SDouglas Gregor   } while (true);
1255718292f2SDouglas Gregor 
1256718292f2SDouglas Gregor   return HadError;
1257718292f2SDouglas Gregor }
1258718292f2SDouglas Gregor 
1259718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
1260718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1261718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1262718292f2SDouglas Gregor   if (!Buffer)
1263718292f2SDouglas Gregor     return true;
1264718292f2SDouglas Gregor 
1265718292f2SDouglas Gregor   // Parse this module map file.
12661fb5c3a6SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
12671fb5c3a6SDouglas Gregor   Diags->getClient()->BeginSourceFile(MMapLangOpts);
12685257fc63SDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir());
1269718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
1270718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
1271718292f2SDouglas Gregor 
1272718292f2SDouglas Gregor   return Result;
1273718292f2SDouglas Gregor }
1274