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 
266eb90e830SDouglas 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.
273eb90e830SDouglas Gregor   if (Module *Sub = lookupModuleQualified(Name, Parent))
274eb90e830SDouglas 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);
279eb90e830SDouglas 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);
308eb90e830SDouglas 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.
792eb90e830SDouglas Gregor   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
793*fcc54a3bSDouglas Gregor     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
794*fcc54a3bSDouglas Gregor       // Skip the module definition.
795*fcc54a3bSDouglas Gregor       skipUntil(MMToken::RBrace);
796*fcc54a3bSDouglas Gregor       if (Tok.is(MMToken::RBrace))
797*fcc54a3bSDouglas Gregor         consumeToken();
798*fcc54a3bSDouglas Gregor       else {
799*fcc54a3bSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
800*fcc54a3bSDouglas Gregor         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
801*fcc54a3bSDouglas Gregor         HadError = true;
802*fcc54a3bSDouglas Gregor       }
803*fcc54a3bSDouglas Gregor       return;
804*fcc54a3bSDouglas Gregor     }
805*fcc54a3bSDouglas Gregor 
806718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
807718292f2SDouglas Gregor       << ModuleName;
808eb90e830SDouglas Gregor     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
809718292f2SDouglas Gregor 
810718292f2SDouglas Gregor     // Skip the module definition.
811718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
812718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
813718292f2SDouglas Gregor       consumeToken();
814718292f2SDouglas Gregor 
815718292f2SDouglas Gregor     HadError = true;
816718292f2SDouglas Gregor     return;
817718292f2SDouglas Gregor   }
818718292f2SDouglas Gregor 
819718292f2SDouglas Gregor   // Start defining this module.
820eb90e830SDouglas Gregor   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
821eb90e830SDouglas Gregor                                         Explicit).first;
822eb90e830SDouglas Gregor   ActiveModule->DefinitionLoc = ModuleNameLoc;
823718292f2SDouglas Gregor 
824718292f2SDouglas Gregor   bool Done = false;
825718292f2SDouglas Gregor   do {
826718292f2SDouglas Gregor     switch (Tok.Kind) {
827718292f2SDouglas Gregor     case MMToken::EndOfFile:
828718292f2SDouglas Gregor     case MMToken::RBrace:
829718292f2SDouglas Gregor       Done = true;
830718292f2SDouglas Gregor       break;
831718292f2SDouglas Gregor 
832718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
833f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
834718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
835718292f2SDouglas Gregor       parseModuleDecl();
836718292f2SDouglas Gregor       break;
837718292f2SDouglas Gregor 
8382b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
8392b82c2a5SDouglas Gregor       parseExportDecl();
8402b82c2a5SDouglas Gregor       break;
8412b82c2a5SDouglas Gregor 
8421fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
8431fb5c3a6SDouglas Gregor       parseRequiresDecl();
8441fb5c3a6SDouglas Gregor       break;
8451fb5c3a6SDouglas Gregor 
846524e33e1SDouglas Gregor     case MMToken::UmbrellaKeyword: {
847524e33e1SDouglas Gregor       SourceLocation UmbrellaLoc = consumeToken();
848524e33e1SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword))
849524e33e1SDouglas Gregor         parseHeaderDecl(UmbrellaLoc);
850524e33e1SDouglas Gregor       else
851524e33e1SDouglas Gregor         parseUmbrellaDirDecl(UmbrellaLoc);
852718292f2SDouglas Gregor       break;
853524e33e1SDouglas Gregor     }
854718292f2SDouglas Gregor 
855322f633cSDouglas Gregor     case MMToken::HeaderKeyword:
856322f633cSDouglas Gregor       parseHeaderDecl(SourceLocation());
857718292f2SDouglas Gregor       break;
858718292f2SDouglas Gregor 
859718292f2SDouglas Gregor     default:
860718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
861718292f2SDouglas Gregor       consumeToken();
862718292f2SDouglas Gregor       break;
863718292f2SDouglas Gregor     }
864718292f2SDouglas Gregor   } while (!Done);
865718292f2SDouglas Gregor 
866718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
867718292f2SDouglas Gregor     consumeToken();
868718292f2SDouglas Gregor   else {
869718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
870718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
871718292f2SDouglas Gregor     HadError = true;
872718292f2SDouglas Gregor   }
873718292f2SDouglas Gregor 
874e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
875e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
876718292f2SDouglas Gregor }
877718292f2SDouglas Gregor 
8781fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration.
8791fb5c3a6SDouglas Gregor ///
8801fb5c3a6SDouglas Gregor ///   requires-declaration:
8811fb5c3a6SDouglas Gregor ///     'requires' feature-list
8821fb5c3a6SDouglas Gregor ///
8831fb5c3a6SDouglas Gregor ///   feature-list:
8841fb5c3a6SDouglas Gregor ///     identifier ',' feature-list
8851fb5c3a6SDouglas Gregor ///     identifier
8861fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() {
8871fb5c3a6SDouglas Gregor   assert(Tok.is(MMToken::RequiresKeyword));
8881fb5c3a6SDouglas Gregor 
8891fb5c3a6SDouglas Gregor   // Parse 'requires' keyword.
8901fb5c3a6SDouglas Gregor   consumeToken();
8911fb5c3a6SDouglas Gregor 
8921fb5c3a6SDouglas Gregor   // Parse the feature-list.
8931fb5c3a6SDouglas Gregor   do {
8941fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
8951fb5c3a6SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
8961fb5c3a6SDouglas Gregor       HadError = true;
8971fb5c3a6SDouglas Gregor       return;
8981fb5c3a6SDouglas Gregor     }
8991fb5c3a6SDouglas Gregor 
9001fb5c3a6SDouglas Gregor     // Consume the feature name.
9011fb5c3a6SDouglas Gregor     std::string Feature = Tok.getString();
9021fb5c3a6SDouglas Gregor     consumeToken();
9031fb5c3a6SDouglas Gregor 
9041fb5c3a6SDouglas Gregor     // Add this feature.
9051fb5c3a6SDouglas Gregor     ActiveModule->addRequirement(Feature, Map.LangOpts);
9061fb5c3a6SDouglas Gregor 
9071fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Comma))
9081fb5c3a6SDouglas Gregor       break;
9091fb5c3a6SDouglas Gregor 
9101fb5c3a6SDouglas Gregor     // Consume the comma.
9111fb5c3a6SDouglas Gregor     consumeToken();
9121fb5c3a6SDouglas Gregor   } while (true);
9131fb5c3a6SDouglas Gregor }
9141fb5c3a6SDouglas Gregor 
915f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
916f2161a70SDouglas Gregor /// subframework in which the given module lives.
917f2161a70SDouglas Gregor void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) {
918f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
919f2161a70SDouglas Gregor   llvm::SmallVector<StringRef, 2> Paths;
920f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
921f2161a70SDouglas Gregor     if (Mod->IsFramework)
922f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
923f2161a70SDouglas Gregor   }
924f2161a70SDouglas Gregor 
925f2161a70SDouglas Gregor   if (Paths.empty())
926f2161a70SDouglas Gregor     return;
927f2161a70SDouglas Gregor 
928f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
929f2161a70SDouglas Gregor   for (unsigned I = Paths.size() - 1; I != 0; --I) {
930f2161a70SDouglas Gregor     llvm::sys::path::append(Path, "Frameworks");
931f2161a70SDouglas Gregor     llvm::sys::path::append(Path, Paths[I-1] + ".framework");
932f2161a70SDouglas Gregor   }
933f2161a70SDouglas Gregor }
934f2161a70SDouglas Gregor 
935718292f2SDouglas Gregor /// \brief Parse a header declaration.
936718292f2SDouglas Gregor ///
937718292f2SDouglas Gregor ///   header-declaration:
938322f633cSDouglas Gregor ///     'umbrella'[opt] 'header' string-literal
939322f633cSDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) {
940718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
9411871ed3dSBenjamin Kramer   consumeToken();
942718292f2SDouglas Gregor 
943322f633cSDouglas Gregor   bool Umbrella = UmbrellaLoc.isValid();
944322f633cSDouglas Gregor 
945718292f2SDouglas Gregor   // Parse the header name.
946718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
947718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
948718292f2SDouglas Gregor       << "header";
949718292f2SDouglas Gregor     HadError = true;
950718292f2SDouglas Gregor     return;
951718292f2SDouglas Gregor   }
952e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
953718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
954718292f2SDouglas Gregor 
955524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
956524e33e1SDouglas Gregor   if (Umbrella && ActiveModule->Umbrella) {
957524e33e1SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
958524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
959322f633cSDouglas Gregor     HadError = true;
960322f633cSDouglas Gregor     return;
961322f633cSDouglas Gregor   }
962322f633cSDouglas Gregor 
9635257fc63SDouglas Gregor   // Look for this file.
964e7ab3669SDouglas Gregor   const FileEntry *File = 0;
9655257fc63SDouglas Gregor   llvm::SmallString<128> PathName;
966e7ab3669SDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
967e7ab3669SDouglas Gregor     PathName = FileName;
968e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
9697033127bSDouglas Gregor   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
9707033127bSDouglas Gregor     PathName = Dir->getName();
9717033127bSDouglas Gregor     llvm::sys::path::append(PathName, FileName);
9727033127bSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
973e7ab3669SDouglas Gregor   } else {
974e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
9757033127bSDouglas Gregor     PathName = Directory->getName();
976e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
977755b2055SDouglas Gregor 
978f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
979f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
980755b2055SDouglas Gregor 
981e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
982e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
9835257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
984e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
985e7ab3669SDouglas Gregor 
986e7ab3669SDouglas Gregor       if (!File) {
987e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
988e7ab3669SDouglas Gregor         PathName.resize(PathLength);
989e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
990e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
991e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
992e7ab3669SDouglas Gregor       }
993e7ab3669SDouglas Gregor     } else {
994e7ab3669SDouglas Gregor       // Lookup for normal headers.
995e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
996e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
997e7ab3669SDouglas Gregor     }
998e7ab3669SDouglas Gregor   }
9995257fc63SDouglas Gregor 
10005257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
10015257fc63SDouglas Gregor   // Come up with a lazy way to do this.
1002e7ab3669SDouglas Gregor   if (File) {
10035257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
10045257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
10055257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
10065257fc63SDouglas Gregor       HadError = true;
1007322f633cSDouglas Gregor     } else if (Umbrella) {
1008322f633cSDouglas Gregor       const DirectoryEntry *UmbrellaDir = File->getDir();
1009322f633cSDouglas Gregor       if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
1010322f633cSDouglas Gregor         Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1011322f633cSDouglas Gregor           << OwningModule->getFullModuleName();
1012322f633cSDouglas Gregor         HadError = true;
10135257fc63SDouglas Gregor       } else {
1014322f633cSDouglas Gregor         // Record this umbrella header.
1015322f633cSDouglas Gregor         Map.setUmbrellaHeader(ActiveModule, File);
1016322f633cSDouglas Gregor       }
1017322f633cSDouglas Gregor     } else {
1018322f633cSDouglas Gregor       // Record this header.
1019a89c5ac4SDouglas Gregor       Map.addHeader(ActiveModule, File);
10205257fc63SDouglas Gregor     }
10215257fc63SDouglas Gregor   } else {
10225257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
1023524e33e1SDouglas Gregor       << Umbrella << FileName;
10245257fc63SDouglas Gregor     HadError = true;
10255257fc63SDouglas Gregor   }
1026718292f2SDouglas Gregor }
1027718292f2SDouglas Gregor 
1028524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration.
1029524e33e1SDouglas Gregor ///
1030524e33e1SDouglas Gregor ///   umbrella-dir-declaration:
1031524e33e1SDouglas Gregor ///     umbrella string-literal
1032524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1033524e33e1SDouglas Gregor   // Parse the directory name.
1034524e33e1SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1035524e33e1SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1036524e33e1SDouglas Gregor       << "umbrella";
1037524e33e1SDouglas Gregor     HadError = true;
1038524e33e1SDouglas Gregor     return;
1039524e33e1SDouglas Gregor   }
1040524e33e1SDouglas Gregor 
1041524e33e1SDouglas Gregor   std::string DirName = Tok.getString();
1042524e33e1SDouglas Gregor   SourceLocation DirNameLoc = consumeToken();
1043524e33e1SDouglas Gregor 
1044524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1045524e33e1SDouglas Gregor   if (ActiveModule->Umbrella) {
1046524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1047524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1048524e33e1SDouglas Gregor     HadError = true;
1049524e33e1SDouglas Gregor     return;
1050524e33e1SDouglas Gregor   }
1051524e33e1SDouglas Gregor 
1052524e33e1SDouglas Gregor   // Look for this file.
1053524e33e1SDouglas Gregor   const DirectoryEntry *Dir = 0;
1054524e33e1SDouglas Gregor   if (llvm::sys::path::is_absolute(DirName))
1055524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1056524e33e1SDouglas Gregor   else {
1057524e33e1SDouglas Gregor     llvm::SmallString<128> PathName;
1058524e33e1SDouglas Gregor     PathName = Directory->getName();
1059524e33e1SDouglas Gregor     llvm::sys::path::append(PathName, DirName);
1060524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1061524e33e1SDouglas Gregor   }
1062524e33e1SDouglas Gregor 
1063524e33e1SDouglas Gregor   if (!Dir) {
1064524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1065524e33e1SDouglas Gregor       << DirName;
1066524e33e1SDouglas Gregor     HadError = true;
1067524e33e1SDouglas Gregor     return;
1068524e33e1SDouglas Gregor   }
1069524e33e1SDouglas Gregor 
1070524e33e1SDouglas Gregor   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1071524e33e1SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1072524e33e1SDouglas Gregor       << OwningModule->getFullModuleName();
1073524e33e1SDouglas Gregor     HadError = true;
1074524e33e1SDouglas Gregor     return;
1075524e33e1SDouglas Gregor   }
1076524e33e1SDouglas Gregor 
1077524e33e1SDouglas Gregor   // Record this umbrella directory.
1078524e33e1SDouglas Gregor   Map.setUmbrellaDir(ActiveModule, Dir);
1079524e33e1SDouglas Gregor }
1080524e33e1SDouglas Gregor 
10812b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
10822b82c2a5SDouglas Gregor ///
10832b82c2a5SDouglas Gregor ///   export-declaration:
10842b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
10852b82c2a5SDouglas Gregor ///
10862b82c2a5SDouglas Gregor ///   wildcard-module-id:
10872b82c2a5SDouglas Gregor ///     identifier
10882b82c2a5SDouglas Gregor ///     '*'
10892b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
10902b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
10912b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
10922b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
10932b82c2a5SDouglas Gregor 
10942b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
10952b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
10962b82c2a5SDouglas Gregor   bool Wildcard = false;
10972b82c2a5SDouglas Gregor   do {
10982b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
10992b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
11002b82c2a5SDouglas Gregor                                               Tok.getLocation()));
11012b82c2a5SDouglas Gregor       consumeToken();
11022b82c2a5SDouglas Gregor 
11032b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
11042b82c2a5SDouglas Gregor         consumeToken();
11052b82c2a5SDouglas Gregor         continue;
11062b82c2a5SDouglas Gregor       }
11072b82c2a5SDouglas Gregor 
11082b82c2a5SDouglas Gregor       break;
11092b82c2a5SDouglas Gregor     }
11102b82c2a5SDouglas Gregor 
11112b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
11122b82c2a5SDouglas Gregor       Wildcard = true;
1113f5eedd05SDouglas Gregor       consumeToken();
11142b82c2a5SDouglas Gregor       break;
11152b82c2a5SDouglas Gregor     }
11162b82c2a5SDouglas Gregor 
11172b82c2a5SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
11182b82c2a5SDouglas Gregor     HadError = true;
11192b82c2a5SDouglas Gregor     return;
11202b82c2a5SDouglas Gregor   } while (true);
11212b82c2a5SDouglas Gregor 
11222b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
11232b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
11242b82c2a5SDouglas Gregor   };
11252b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
11262b82c2a5SDouglas Gregor }
11272b82c2a5SDouglas Gregor 
112873441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
112973441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
113073441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
113173441091SDouglas Gregor   bool Failed = false;
113273441091SDouglas Gregor 
113373441091SDouglas Gregor   // Inferred modules must be submodules.
113473441091SDouglas Gregor   if (!ActiveModule) {
113573441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
113673441091SDouglas Gregor     Failed = true;
113773441091SDouglas Gregor   }
113873441091SDouglas Gregor 
1139524e33e1SDouglas Gregor   // Inferred modules must have umbrella directories.
1140524e33e1SDouglas Gregor   if (!Failed && !ActiveModule->getUmbrellaDir()) {
114173441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
114273441091SDouglas Gregor     Failed = true;
114373441091SDouglas Gregor   }
114473441091SDouglas Gregor 
114573441091SDouglas Gregor   // Check for redefinition of an inferred module.
1146dd005f69SDouglas Gregor   if (!Failed && ActiveModule->InferSubmodules) {
114773441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1148dd005f69SDouglas Gregor     if (ActiveModule->InferredSubmoduleLoc.isValid())
1149dd005f69SDouglas Gregor       Diags.Report(ActiveModule->InferredSubmoduleLoc,
115073441091SDouglas Gregor                    diag::note_mmap_prev_definition);
115173441091SDouglas Gregor     Failed = true;
115273441091SDouglas Gregor   }
115373441091SDouglas Gregor 
115473441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
115573441091SDouglas Gregor   if (Failed) {
115673441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
115773441091SDouglas Gregor       consumeToken();
115873441091SDouglas Gregor       skipUntil(MMToken::RBrace);
115973441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
116073441091SDouglas Gregor         consumeToken();
116173441091SDouglas Gregor     }
116273441091SDouglas Gregor     HadError = true;
116373441091SDouglas Gregor     return;
116473441091SDouglas Gregor   }
116573441091SDouglas Gregor 
116673441091SDouglas Gregor   // Note that we have an inferred submodule.
1167dd005f69SDouglas Gregor   ActiveModule->InferSubmodules = true;
1168dd005f69SDouglas Gregor   ActiveModule->InferredSubmoduleLoc = StarLoc;
1169dd005f69SDouglas Gregor   ActiveModule->InferExplicitSubmodules = Explicit;
117073441091SDouglas Gregor 
117173441091SDouglas Gregor   // Parse the opening brace.
117273441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
117373441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
117473441091SDouglas Gregor     HadError = true;
117573441091SDouglas Gregor     return;
117673441091SDouglas Gregor   }
117773441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
117873441091SDouglas Gregor 
117973441091SDouglas Gregor   // Parse the body of the inferred submodule.
118073441091SDouglas Gregor   bool Done = false;
118173441091SDouglas Gregor   do {
118273441091SDouglas Gregor     switch (Tok.Kind) {
118373441091SDouglas Gregor     case MMToken::EndOfFile:
118473441091SDouglas Gregor     case MMToken::RBrace:
118573441091SDouglas Gregor       Done = true;
118673441091SDouglas Gregor       break;
118773441091SDouglas Gregor 
118873441091SDouglas Gregor     case MMToken::ExportKeyword: {
118973441091SDouglas Gregor       consumeToken();
119073441091SDouglas Gregor       if (Tok.is(MMToken::Star))
1191dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
119273441091SDouglas Gregor       else
119373441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
119473441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
119573441091SDouglas Gregor       consumeToken();
119673441091SDouglas Gregor       break;
119773441091SDouglas Gregor     }
119873441091SDouglas Gregor 
119973441091SDouglas Gregor     case MMToken::ExplicitKeyword:
120073441091SDouglas Gregor     case MMToken::ModuleKeyword:
120173441091SDouglas Gregor     case MMToken::HeaderKeyword:
120273441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
120373441091SDouglas Gregor     default:
120473441091SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
120573441091SDouglas Gregor       consumeToken();
120673441091SDouglas Gregor       break;
120773441091SDouglas Gregor     }
120873441091SDouglas Gregor   } while (!Done);
120973441091SDouglas Gregor 
121073441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
121173441091SDouglas Gregor     consumeToken();
121273441091SDouglas Gregor   else {
121373441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
121473441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
121573441091SDouglas Gregor     HadError = true;
121673441091SDouglas Gregor   }
121773441091SDouglas Gregor }
121873441091SDouglas Gregor 
12197033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence
12207033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
12217033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
12227033127bSDouglas Gregor   for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
12237033127bSDouglas Gregor     // If we have an umbrella directory, use that.
12247033127bSDouglas Gregor     if (Mod->hasUmbrellaDir())
12257033127bSDouglas Gregor       return Mod->getUmbrellaDir();
12267033127bSDouglas Gregor 
12277033127bSDouglas Gregor     // If we have a framework directory, stop looking.
12287033127bSDouglas Gregor     if (Mod->IsFramework)
12297033127bSDouglas Gregor       return 0;
12307033127bSDouglas Gregor   }
12317033127bSDouglas Gregor 
12327033127bSDouglas Gregor   return 0;
12337033127bSDouglas Gregor }
12347033127bSDouglas Gregor 
1235718292f2SDouglas Gregor /// \brief Parse a module map file.
1236718292f2SDouglas Gregor ///
1237718292f2SDouglas Gregor ///   module-map-file:
1238718292f2SDouglas Gregor ///     module-declaration*
1239718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
1240718292f2SDouglas Gregor   do {
1241718292f2SDouglas Gregor     switch (Tok.Kind) {
1242718292f2SDouglas Gregor     case MMToken::EndOfFile:
1243718292f2SDouglas Gregor       return HadError;
1244718292f2SDouglas Gregor 
1245e7ab3669SDouglas Gregor     case MMToken::ExplicitKeyword:
1246718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1247755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
1248718292f2SDouglas Gregor       parseModuleDecl();
1249718292f2SDouglas Gregor       break;
1250718292f2SDouglas Gregor 
12511fb5c3a6SDouglas Gregor     case MMToken::Comma:
12522b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
1253718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
1254718292f2SDouglas Gregor     case MMToken::Identifier:
1255718292f2SDouglas Gregor     case MMToken::LBrace:
12562b82c2a5SDouglas Gregor     case MMToken::Period:
1257718292f2SDouglas Gregor     case MMToken::RBrace:
12581fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
12592b82c2a5SDouglas Gregor     case MMToken::Star:
1260718292f2SDouglas Gregor     case MMToken::StringLiteral:
1261718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
1262718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1263718292f2SDouglas Gregor       HadError = true;
1264718292f2SDouglas Gregor       consumeToken();
1265718292f2SDouglas Gregor       break;
1266718292f2SDouglas Gregor     }
1267718292f2SDouglas Gregor   } while (true);
1268718292f2SDouglas Gregor 
1269718292f2SDouglas Gregor   return HadError;
1270718292f2SDouglas Gregor }
1271718292f2SDouglas Gregor 
1272718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
1273718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1274718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1275718292f2SDouglas Gregor   if (!Buffer)
1276718292f2SDouglas Gregor     return true;
1277718292f2SDouglas Gregor 
1278718292f2SDouglas Gregor   // Parse this module map file.
12791fb5c3a6SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
12801fb5c3a6SDouglas Gregor   Diags->getClient()->BeginSourceFile(MMapLangOpts);
12815257fc63SDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir());
1282718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
1283718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
1284718292f2SDouglas Gregor 
1285718292f2SDouglas Gregor   return Result;
1286718292f2SDouglas Gregor }
1287