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 
72718292f2SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC) {
73718292f2SDouglas Gregor   llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
74718292f2SDouglas Gregor   Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>(
75718292f2SDouglas Gregor             new DiagnosticsEngine(DiagIDs));
76718292f2SDouglas Gregor   Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
77718292f2SDouglas Gregor   SourceMgr = new SourceManager(*Diags, FileMgr);
78718292f2SDouglas Gregor }
79718292f2SDouglas Gregor 
80718292f2SDouglas Gregor ModuleMap::~ModuleMap() {
815acdf59eSDouglas Gregor   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
825acdf59eSDouglas Gregor                                         IEnd = Modules.end();
835acdf59eSDouglas Gregor        I != IEnd; ++I) {
845acdf59eSDouglas Gregor     delete I->getValue();
855acdf59eSDouglas Gregor   }
865acdf59eSDouglas Gregor 
87718292f2SDouglas Gregor   delete SourceMgr;
88718292f2SDouglas Gregor }
89718292f2SDouglas Gregor 
90de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
91ab0c8a84SDouglas Gregor   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
92ab0c8a84SDouglas Gregor     = Headers.find(File);
93ab0c8a84SDouglas Gregor   if (Known != Headers.end())
94ab0c8a84SDouglas Gregor     return Known->second;
95ab0c8a84SDouglas Gregor 
96b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
97b65dbfffSDouglas Gregor   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
98b65dbfffSDouglas Gregor   StringRef DirName = Dir->getName();
99a89c5ac4SDouglas Gregor 
100a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
101a89c5ac4SDouglas Gregor   // an umbrella header.
102b65dbfffSDouglas Gregor   do {
103a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
104a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
105a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
106a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
107930a85ccSDouglas Gregor 
108930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
109930a85ccSDouglas Gregor       // header.
110930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
111930a85ccSDouglas Gregor       while (!UmbrellaModule->UmbrellaHeader && UmbrellaModule->Parent)
112930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
113930a85ccSDouglas Gregor 
114930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
115a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
116a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
117a89c5ac4SDouglas Gregor         // the actual header is located.
118a89c5ac4SDouglas Gregor 
119a89c5ac4SDouglas Gregor         // For a framework module, the umbrella directory is the framework
120a89c5ac4SDouglas Gregor         // directory, so strip off the "Headers" or "PrivateHeaders".
121a89c5ac4SDouglas Gregor         // FIXME: Should we tack on an "explicit" for PrivateHeaders? That
122a89c5ac4SDouglas Gregor         // might be what we want, but it feels like a hack.
123a89c5ac4SDouglas Gregor         unsigned LastSkippedDir = SkippedDirs.size();
124930a85ccSDouglas Gregor         if (LastSkippedDir && UmbrellaModule->IsFramework)
125a89c5ac4SDouglas Gregor           --LastSkippedDir;
126a89c5ac4SDouglas Gregor 
127a89c5ac4SDouglas Gregor         for (unsigned I = LastSkippedDir; I != 0; --I) {
128a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
129a89c5ac4SDouglas Gregor           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
130a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
131930a85ccSDouglas Gregor                                       UmbrellaModule->InferExplicitSubmodules).first;
132a89c5ac4SDouglas Gregor 
133a89c5ac4SDouglas Gregor           // Associate the module and the directory.
134a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
135a89c5ac4SDouglas Gregor 
136a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
137a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
138930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
139a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
140a89c5ac4SDouglas Gregor         }
141a89c5ac4SDouglas Gregor 
142a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
143a89c5ac4SDouglas Gregor         StringRef Name = llvm::sys::path::stem(File->getName());
144a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
145930a85ccSDouglas Gregor                                     UmbrellaModule->InferExplicitSubmodules).first;
146a89c5ac4SDouglas Gregor 
147a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
148a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
149930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
150a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
151a89c5ac4SDouglas Gregor       } else {
152a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
153a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
154a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
155a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
156a89c5ac4SDouglas Gregor       }
157a89c5ac4SDouglas Gregor 
158a89c5ac4SDouglas Gregor       Headers[File] = Result;
159a89c5ac4SDouglas Gregor       return Result;
160a89c5ac4SDouglas Gregor     }
161a89c5ac4SDouglas Gregor 
162a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
163a89c5ac4SDouglas Gregor 
164b65dbfffSDouglas Gregor     // Retrieve our parent path.
165b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
166b65dbfffSDouglas Gregor     if (DirName.empty())
167b65dbfffSDouglas Gregor       break;
168b65dbfffSDouglas Gregor 
169b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
170b65dbfffSDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
171a89c5ac4SDouglas Gregor   } while (Dir);
172b65dbfffSDouglas Gregor 
173ab0c8a84SDouglas Gregor   return 0;
174ab0c8a84SDouglas Gregor }
175ab0c8a84SDouglas Gregor 
176de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) {
17788bdfb0eSDouglas Gregor   llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
17888bdfb0eSDouglas Gregor   if (Known != Modules.end())
17988bdfb0eSDouglas Gregor     return Known->getValue();
18088bdfb0eSDouglas Gregor 
18188bdfb0eSDouglas Gregor   return 0;
18288bdfb0eSDouglas Gregor }
18388bdfb0eSDouglas Gregor 
1842b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
1852b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
1862b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
1872b82c2a5SDouglas Gregor       return Sub;
1882b82c2a5SDouglas Gregor   }
1892b82c2a5SDouglas Gregor 
1902b82c2a5SDouglas Gregor   return findModule(Name);
1912b82c2a5SDouglas Gregor }
1922b82c2a5SDouglas Gregor 
1932b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
1942b82c2a5SDouglas Gregor   if (!Context)
1952b82c2a5SDouglas Gregor     return findModule(Name);
1962b82c2a5SDouglas Gregor 
1972b82c2a5SDouglas Gregor   llvm::StringMap<Module *>::iterator Sub = Context->SubModules.find(Name);
1982b82c2a5SDouglas Gregor   if (Sub != Context->SubModules.end())
1992b82c2a5SDouglas Gregor     return Sub->getValue();
2002b82c2a5SDouglas Gregor 
2012b82c2a5SDouglas Gregor   return 0;
2022b82c2a5SDouglas Gregor }
2032b82c2a5SDouglas Gregor 
204de3ef502SDouglas Gregor std::pair<Module *, bool>
20569021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
20669021974SDouglas Gregor                               bool IsExplicit) {
20769021974SDouglas Gregor   // Try to find an existing module with this name.
20869021974SDouglas Gregor   if (Module *Found = Parent? Parent->SubModules[Name] : Modules[Name])
20969021974SDouglas Gregor     return std::make_pair(Found, false);
21069021974SDouglas Gregor 
21169021974SDouglas Gregor   // Create a new module with this name.
21269021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
21369021974SDouglas Gregor                               IsExplicit);
21469021974SDouglas Gregor   if (Parent)
21569021974SDouglas Gregor     Parent->SubModules[Name] = Result;
21669021974SDouglas Gregor   else
21769021974SDouglas Gregor     Modules[Name] = Result;
21869021974SDouglas Gregor   return std::make_pair(Result, true);
21969021974SDouglas Gregor }
22069021974SDouglas Gregor 
221de3ef502SDouglas Gregor Module *
22256c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
223e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
224e89dbc1dSDouglas Gregor                                 Module *Parent) {
22556c64013SDouglas Gregor   // Check whether we've already found this module.
226e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
227e89dbc1dSDouglas Gregor     return Mod;
228e89dbc1dSDouglas Gregor 
229e89dbc1dSDouglas Gregor   FileManager &FileMgr = SourceMgr->getFileManager();
23056c64013SDouglas Gregor 
23156c64013SDouglas Gregor   // Look for an umbrella header.
23256c64013SDouglas Gregor   llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
23356c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, "Headers");
23456c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
235e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
23656c64013SDouglas Gregor 
23756c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
23856c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
23956c64013SDouglas Gregor   // idea.
24056c64013SDouglas Gregor   if (!UmbrellaHeader)
24156c64013SDouglas Gregor     return 0;
24256c64013SDouglas Gregor 
243e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
244e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
245e89dbc1dSDouglas Gregor 
246e89dbc1dSDouglas Gregor   if (Parent)
247e89dbc1dSDouglas Gregor     Parent->SubModules[ModuleName] = Result;
248e89dbc1dSDouglas Gregor   else
249e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
250e89dbc1dSDouglas Gregor 
251d8bd7537SDouglas Gregor   // umbrella "umbrella-header-name"
25256c64013SDouglas Gregor   Result->UmbrellaHeader = UmbrellaHeader;
25356c64013SDouglas Gregor   Headers[UmbrellaHeader] = Result;
25456c64013SDouglas Gregor   UmbrellaDirs[FrameworkDir] = Result;
255d8bd7537SDouglas Gregor 
256d8bd7537SDouglas Gregor   // export *
257d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
258d8bd7537SDouglas Gregor 
259a89c5ac4SDouglas Gregor   // module * { export * }
260a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
261a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
262a89c5ac4SDouglas Gregor 
263e89dbc1dSDouglas Gregor   // Look for subframeworks.
264e89dbc1dSDouglas Gregor   llvm::error_code EC;
265e89dbc1dSDouglas Gregor   llvm::SmallString<128> SubframeworksDirName = StringRef(FrameworkDir->getName());
266e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
267e89dbc1dSDouglas Gregor   for (llvm::sys::fs::directory_iterator Dir(SubframeworksDirName.str(), EC),
268e89dbc1dSDouglas Gregor                                          DirEnd;
269e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
270e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
271e89dbc1dSDouglas Gregor       continue;
272f2161a70SDouglas Gregor 
273e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
274e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
275e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
276e89dbc1dSDouglas Gregor       inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
277e89dbc1dSDouglas Gregor                            Result);
278e89dbc1dSDouglas Gregor     }
279e89dbc1dSDouglas Gregor   }
280e89dbc1dSDouglas Gregor 
28156c64013SDouglas Gregor   return Result;
28256c64013SDouglas Gregor }
28356c64013SDouglas Gregor 
284a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
285a89c5ac4SDouglas Gregor   Headers[UmbrellaHeader] = Mod;
286a89c5ac4SDouglas Gregor   Mod->UmbrellaHeader = UmbrellaHeader;
287a89c5ac4SDouglas Gregor 
288a89c5ac4SDouglas Gregor   const DirectoryEntry *UmbrellaDir = UmbrellaHeader->getDir();
289a89c5ac4SDouglas Gregor   if (Mod->IsFramework)
290a89c5ac4SDouglas Gregor     UmbrellaDir = SourceMgr->getFileManager().getDirectory(
291a89c5ac4SDouglas Gregor                     llvm::sys::path::parent_path(UmbrellaDir->getName()));
292a89c5ac4SDouglas Gregor 
293a89c5ac4SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
294a89c5ac4SDouglas Gregor }
295a89c5ac4SDouglas Gregor 
296a89c5ac4SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
297a89c5ac4SDouglas Gregor   Mod->Headers.push_back(Header);
298a89c5ac4SDouglas Gregor   Headers[Header] = Mod;
299a89c5ac4SDouglas Gregor }
300a89c5ac4SDouglas Gregor 
301514b636aSDouglas Gregor const FileEntry *
302de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) {
303514b636aSDouglas Gregor   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
304514b636aSDouglas Gregor     return 0;
305514b636aSDouglas Gregor 
306514b636aSDouglas Gregor   return SourceMgr->getFileEntryForID(
307514b636aSDouglas Gregor            SourceMgr->getFileID(Module->DefinitionLoc));
308514b636aSDouglas Gregor }
309514b636aSDouglas Gregor 
310718292f2SDouglas Gregor void ModuleMap::dump() {
311718292f2SDouglas Gregor   llvm::errs() << "Modules:";
312718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
313718292f2SDouglas Gregor                                         MEnd = Modules.end();
314718292f2SDouglas Gregor        M != MEnd; ++M)
315d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
316718292f2SDouglas Gregor 
317718292f2SDouglas Gregor   llvm::errs() << "Headers:";
318718292f2SDouglas Gregor   for (llvm::DenseMap<const FileEntry *, Module *>::iterator
319718292f2SDouglas Gregor             H = Headers.begin(),
320718292f2SDouglas Gregor          HEnd = Headers.end();
321718292f2SDouglas Gregor        H != HEnd; ++H) {
322718292f2SDouglas Gregor     llvm::errs() << "  \"" << H->first->getName() << "\" -> "
323718292f2SDouglas Gregor                  << H->second->getFullModuleName() << "\n";
324718292f2SDouglas Gregor   }
325718292f2SDouglas Gregor }
326718292f2SDouglas Gregor 
3272b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
3282b82c2a5SDouglas Gregor   bool HadError = false;
3292b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
3302b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
3312b82c2a5SDouglas Gregor                                               Complain);
332f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
3332b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
3342b82c2a5SDouglas Gregor     else
3352b82c2a5SDouglas Gregor       HadError = true;
3362b82c2a5SDouglas Gregor   }
3372b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
3382b82c2a5SDouglas Gregor   return HadError;
3392b82c2a5SDouglas Gregor }
3402b82c2a5SDouglas Gregor 
3410093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
3420093b3c7SDouglas Gregor   if (Loc.isInvalid())
3430093b3c7SDouglas Gregor     return 0;
3440093b3c7SDouglas Gregor 
3450093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
3460093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
3470093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
3480093b3c7SDouglas Gregor     return 0;
3490093b3c7SDouglas Gregor 
3500093b3c7SDouglas Gregor 
3510093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
3520093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
3530093b3c7SDouglas Gregor   const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID);
3540093b3c7SDouglas Gregor   if (!ExpansionFile)
3550093b3c7SDouglas Gregor     return 0;
3560093b3c7SDouglas Gregor 
3570093b3c7SDouglas Gregor   // Find the module that owns this header.
3580093b3c7SDouglas Gregor   return findModuleForHeader(ExpansionFile);
3590093b3c7SDouglas Gregor }
3600093b3c7SDouglas Gregor 
361718292f2SDouglas Gregor //----------------------------------------------------------------------------//
362718292f2SDouglas Gregor // Module map file parser
363718292f2SDouglas Gregor //----------------------------------------------------------------------------//
364718292f2SDouglas Gregor 
365718292f2SDouglas Gregor namespace clang {
366718292f2SDouglas Gregor   /// \brief A token in a module map file.
367718292f2SDouglas Gregor   struct MMToken {
368718292f2SDouglas Gregor     enum TokenKind {
369718292f2SDouglas Gregor       EndOfFile,
370718292f2SDouglas Gregor       HeaderKeyword,
371718292f2SDouglas Gregor       Identifier,
372718292f2SDouglas Gregor       ExplicitKeyword,
3732b82c2a5SDouglas Gregor       ExportKeyword,
374755b2055SDouglas Gregor       FrameworkKeyword,
375718292f2SDouglas Gregor       ModuleKeyword,
3762b82c2a5SDouglas Gregor       Period,
377718292f2SDouglas Gregor       UmbrellaKeyword,
3782b82c2a5SDouglas Gregor       Star,
379718292f2SDouglas Gregor       StringLiteral,
380718292f2SDouglas Gregor       LBrace,
381718292f2SDouglas Gregor       RBrace
382718292f2SDouglas Gregor     } Kind;
383718292f2SDouglas Gregor 
384718292f2SDouglas Gregor     unsigned Location;
385718292f2SDouglas Gregor     unsigned StringLength;
386718292f2SDouglas Gregor     const char *StringData;
387718292f2SDouglas Gregor 
388718292f2SDouglas Gregor     void clear() {
389718292f2SDouglas Gregor       Kind = EndOfFile;
390718292f2SDouglas Gregor       Location = 0;
391718292f2SDouglas Gregor       StringLength = 0;
392718292f2SDouglas Gregor       StringData = 0;
393718292f2SDouglas Gregor     }
394718292f2SDouglas Gregor 
395718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
396718292f2SDouglas Gregor 
397718292f2SDouglas Gregor     SourceLocation getLocation() const {
398718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
399718292f2SDouglas Gregor     }
400718292f2SDouglas Gregor 
401718292f2SDouglas Gregor     StringRef getString() const {
402718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
403718292f2SDouglas Gregor     }
404718292f2SDouglas Gregor   };
405718292f2SDouglas Gregor 
406718292f2SDouglas Gregor   class ModuleMapParser {
407718292f2SDouglas Gregor     Lexer &L;
408718292f2SDouglas Gregor     SourceManager &SourceMgr;
409718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
410718292f2SDouglas Gregor     ModuleMap &Map;
411718292f2SDouglas Gregor 
4125257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
4135257fc63SDouglas Gregor     const DirectoryEntry *Directory;
4145257fc63SDouglas Gregor 
415718292f2SDouglas Gregor     /// \brief Whether an error occurred.
416718292f2SDouglas Gregor     bool HadError;
417718292f2SDouglas Gregor 
418718292f2SDouglas Gregor     /// \brief Default target information, used only for string literal
419718292f2SDouglas Gregor     /// parsing.
420718292f2SDouglas Gregor     TargetInfo *Target;
421718292f2SDouglas Gregor 
422718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
423718292f2SDouglas Gregor     /// during parsing.
424718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
425718292f2SDouglas Gregor 
426718292f2SDouglas Gregor     /// \brief The current token.
427718292f2SDouglas Gregor     MMToken Tok;
428718292f2SDouglas Gregor 
429718292f2SDouglas Gregor     /// \brief The active module.
430de3ef502SDouglas Gregor     Module *ActiveModule;
431718292f2SDouglas Gregor 
432718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
433718292f2SDouglas Gregor     SourceLocation consumeToken();
434718292f2SDouglas Gregor 
435718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
436718292f2SDouglas Gregor     /// (or the end of the file).
437718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
438718292f2SDouglas Gregor 
439*e7ab3669SDouglas Gregor     typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
440*e7ab3669SDouglas Gregor       ModuleId;
441*e7ab3669SDouglas Gregor     bool parseModuleId(ModuleId &Id);
442718292f2SDouglas Gregor     void parseModuleDecl();
443718292f2SDouglas Gregor     void parseUmbrellaDecl();
444718292f2SDouglas Gregor     void parseHeaderDecl();
4452b82c2a5SDouglas Gregor     void parseExportDecl();
44673441091SDouglas Gregor     void parseInferredSubmoduleDecl(bool Explicit);
447718292f2SDouglas Gregor 
448718292f2SDouglas Gregor   public:
449718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
450718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
4515257fc63SDouglas Gregor                              ModuleMap &Map,
4525257fc63SDouglas Gregor                              const DirectoryEntry *Directory)
4535257fc63SDouglas Gregor       : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
4545257fc63SDouglas Gregor         Directory(Directory), HadError(false), ActiveModule(0)
455718292f2SDouglas Gregor     {
456718292f2SDouglas Gregor       TargetOptions TargetOpts;
457718292f2SDouglas Gregor       TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
458718292f2SDouglas Gregor       Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
459718292f2SDouglas Gregor 
460718292f2SDouglas Gregor       Tok.clear();
461718292f2SDouglas Gregor       consumeToken();
462718292f2SDouglas Gregor     }
463718292f2SDouglas Gregor 
464718292f2SDouglas Gregor     bool parseModuleMapFile();
465718292f2SDouglas Gregor   };
466718292f2SDouglas Gregor }
467718292f2SDouglas Gregor 
468718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
469718292f2SDouglas Gregor retry:
470718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
471718292f2SDouglas Gregor   Tok.clear();
472718292f2SDouglas Gregor 
473718292f2SDouglas Gregor   Token LToken;
474718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
475718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
476718292f2SDouglas Gregor   switch (LToken.getKind()) {
477718292f2SDouglas Gregor   case tok::raw_identifier:
478718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
479718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
480718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
481718292f2SDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
482718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
4832b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
484755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
485718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
486718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
487718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
488718292f2SDouglas Gregor     break;
489718292f2SDouglas Gregor 
490718292f2SDouglas Gregor   case tok::eof:
491718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
492718292f2SDouglas Gregor     break;
493718292f2SDouglas Gregor 
494718292f2SDouglas Gregor   case tok::l_brace:
495718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
496718292f2SDouglas Gregor     break;
497718292f2SDouglas Gregor 
4982b82c2a5SDouglas Gregor   case tok::period:
4992b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
5002b82c2a5SDouglas Gregor     break;
5012b82c2a5SDouglas Gregor 
502718292f2SDouglas Gregor   case tok::r_brace:
503718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
504718292f2SDouglas Gregor     break;
505718292f2SDouglas Gregor 
5062b82c2a5SDouglas Gregor   case tok::star:
5072b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
5082b82c2a5SDouglas Gregor     break;
5092b82c2a5SDouglas Gregor 
510718292f2SDouglas Gregor   case tok::string_literal: {
511718292f2SDouglas Gregor     // Parse the string literal.
512718292f2SDouglas Gregor     LangOptions LangOpts;
513718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
514718292f2SDouglas Gregor     if (StringLiteral.hadError)
515718292f2SDouglas Gregor       goto retry;
516718292f2SDouglas Gregor 
517718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
518718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
519718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
520718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
521718292f2SDouglas Gregor     Saved[Length] = 0;
522718292f2SDouglas Gregor 
523718292f2SDouglas Gregor     // Form the token.
524718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
525718292f2SDouglas Gregor     Tok.StringData = Saved;
526718292f2SDouglas Gregor     Tok.StringLength = Length;
527718292f2SDouglas Gregor     break;
528718292f2SDouglas Gregor   }
529718292f2SDouglas Gregor 
530718292f2SDouglas Gregor   case tok::comment:
531718292f2SDouglas Gregor     goto retry;
532718292f2SDouglas Gregor 
533718292f2SDouglas Gregor   default:
534718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
535718292f2SDouglas Gregor     HadError = true;
536718292f2SDouglas Gregor     goto retry;
537718292f2SDouglas Gregor   }
538718292f2SDouglas Gregor 
539718292f2SDouglas Gregor   return Result;
540718292f2SDouglas Gregor }
541718292f2SDouglas Gregor 
542718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
543718292f2SDouglas Gregor   unsigned braceDepth = 0;
544718292f2SDouglas Gregor   do {
545718292f2SDouglas Gregor     switch (Tok.Kind) {
546718292f2SDouglas Gregor     case MMToken::EndOfFile:
547718292f2SDouglas Gregor       return;
548718292f2SDouglas Gregor 
549718292f2SDouglas Gregor     case MMToken::LBrace:
550718292f2SDouglas Gregor       if (Tok.is(K) && braceDepth == 0)
551718292f2SDouglas Gregor         return;
552718292f2SDouglas Gregor 
553718292f2SDouglas Gregor       ++braceDepth;
554718292f2SDouglas Gregor       break;
555718292f2SDouglas Gregor 
556718292f2SDouglas Gregor     case MMToken::RBrace:
557718292f2SDouglas Gregor       if (braceDepth > 0)
558718292f2SDouglas Gregor         --braceDepth;
559718292f2SDouglas Gregor       else if (Tok.is(K))
560718292f2SDouglas Gregor         return;
561718292f2SDouglas Gregor       break;
562718292f2SDouglas Gregor 
563718292f2SDouglas Gregor     default:
564718292f2SDouglas Gregor       if (braceDepth == 0 && Tok.is(K))
565718292f2SDouglas Gregor         return;
566718292f2SDouglas Gregor       break;
567718292f2SDouglas Gregor     }
568718292f2SDouglas Gregor 
569718292f2SDouglas Gregor    consumeToken();
570718292f2SDouglas Gregor   } while (true);
571718292f2SDouglas Gregor }
572718292f2SDouglas Gregor 
573*e7ab3669SDouglas Gregor /// \brief Parse a module-id.
574*e7ab3669SDouglas Gregor ///
575*e7ab3669SDouglas Gregor ///   module-id:
576*e7ab3669SDouglas Gregor ///     identifier
577*e7ab3669SDouglas Gregor ///     identifier '.' module-id
578*e7ab3669SDouglas Gregor ///
579*e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise.
580*e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) {
581*e7ab3669SDouglas Gregor   Id.clear();
582*e7ab3669SDouglas Gregor   do {
583*e7ab3669SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
584*e7ab3669SDouglas Gregor       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
585*e7ab3669SDouglas Gregor       consumeToken();
586*e7ab3669SDouglas Gregor     } else {
587*e7ab3669SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
588*e7ab3669SDouglas Gregor       return true;
589*e7ab3669SDouglas Gregor     }
590*e7ab3669SDouglas Gregor 
591*e7ab3669SDouglas Gregor     if (!Tok.is(MMToken::Period))
592*e7ab3669SDouglas Gregor       break;
593*e7ab3669SDouglas Gregor 
594*e7ab3669SDouglas Gregor     consumeToken();
595*e7ab3669SDouglas Gregor   } while (true);
596*e7ab3669SDouglas Gregor 
597*e7ab3669SDouglas Gregor   return false;
598*e7ab3669SDouglas Gregor }
599*e7ab3669SDouglas Gregor 
600718292f2SDouglas Gregor /// \brief Parse a module declaration.
601718292f2SDouglas Gregor ///
602718292f2SDouglas Gregor ///   module-declaration:
603*e7ab3669SDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' module-id { module-member* }
604718292f2SDouglas Gregor ///
605718292f2SDouglas Gregor ///   module-member:
606718292f2SDouglas Gregor ///     umbrella-declaration
607718292f2SDouglas Gregor ///     header-declaration
608*e7ab3669SDouglas Gregor ///     submodule-declaration
6092b82c2a5SDouglas Gregor ///     export-declaration
61073441091SDouglas Gregor ///
61173441091SDouglas Gregor ///   submodule-declaration:
61273441091SDouglas Gregor ///     module-declaration
61373441091SDouglas Gregor ///     inferred-submodule-declaration
614718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
615755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
616755b2055SDouglas Gregor          Tok.is(MMToken::FrameworkKeyword));
617f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
618*e7ab3669SDouglas Gregor   SourceLocation ExplicitLoc;
619718292f2SDouglas Gregor   bool Explicit = false;
620f2161a70SDouglas Gregor   bool Framework = false;
621755b2055SDouglas Gregor 
622f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
623f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
624*e7ab3669SDouglas Gregor     ExplicitLoc = consumeToken();
625f2161a70SDouglas Gregor     Explicit = true;
626f2161a70SDouglas Gregor   }
627f2161a70SDouglas Gregor 
628f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
629755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
630755b2055SDouglas Gregor     consumeToken();
631755b2055SDouglas Gregor     Framework = true;
632755b2055SDouglas Gregor   }
633718292f2SDouglas Gregor 
634718292f2SDouglas Gregor   // Parse 'module' keyword.
635718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
636d6343c99SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
637718292f2SDouglas Gregor     consumeToken();
638718292f2SDouglas Gregor     HadError = true;
639718292f2SDouglas Gregor     return;
640718292f2SDouglas Gregor   }
641718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
642718292f2SDouglas Gregor 
64373441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
64473441091SDouglas Gregor   // Parse it.
64573441091SDouglas Gregor   if (Tok.is(MMToken::Star))
64673441091SDouglas Gregor     return parseInferredSubmoduleDecl(Explicit);
64773441091SDouglas Gregor 
648718292f2SDouglas Gregor   // Parse the module name.
649*e7ab3669SDouglas Gregor   ModuleId Id;
650*e7ab3669SDouglas Gregor   if (parseModuleId(Id)) {
651718292f2SDouglas Gregor     HadError = true;
652718292f2SDouglas Gregor     return;
653718292f2SDouglas Gregor   }
654*e7ab3669SDouglas Gregor 
655*e7ab3669SDouglas Gregor   if (ActiveModule) {
656*e7ab3669SDouglas Gregor     if (Id.size() > 1) {
657*e7ab3669SDouglas Gregor       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
658*e7ab3669SDouglas Gregor         << SourceRange(Id.front().second, Id.back().second);
659*e7ab3669SDouglas Gregor 
660*e7ab3669SDouglas Gregor       HadError = true;
661*e7ab3669SDouglas Gregor       return;
662*e7ab3669SDouglas Gregor     }
663*e7ab3669SDouglas Gregor   } else if (Id.size() == 1 && Explicit) {
664*e7ab3669SDouglas Gregor     // Top-level modules can't be explicit.
665*e7ab3669SDouglas Gregor     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
666*e7ab3669SDouglas Gregor     Explicit = false;
667*e7ab3669SDouglas Gregor     ExplicitLoc = SourceLocation();
668*e7ab3669SDouglas Gregor     HadError = true;
669*e7ab3669SDouglas Gregor   }
670*e7ab3669SDouglas Gregor 
671*e7ab3669SDouglas Gregor   Module *PreviousActiveModule = ActiveModule;
672*e7ab3669SDouglas Gregor   if (Id.size() > 1) {
673*e7ab3669SDouglas Gregor     // This module map defines a submodule. Go find the module of which it
674*e7ab3669SDouglas Gregor     // is a submodule.
675*e7ab3669SDouglas Gregor     ActiveModule = 0;
676*e7ab3669SDouglas Gregor     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
677*e7ab3669SDouglas Gregor       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
678*e7ab3669SDouglas Gregor         ActiveModule = Next;
679*e7ab3669SDouglas Gregor         continue;
680*e7ab3669SDouglas Gregor       }
681*e7ab3669SDouglas Gregor 
682*e7ab3669SDouglas Gregor       if (ActiveModule) {
683*e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
684*e7ab3669SDouglas Gregor           << Id[I].first << ActiveModule->getTopLevelModule();
685*e7ab3669SDouglas Gregor       } else {
686*e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
687*e7ab3669SDouglas Gregor       }
688*e7ab3669SDouglas Gregor       HadError = true;
689*e7ab3669SDouglas Gregor       return;
690*e7ab3669SDouglas Gregor     }
691*e7ab3669SDouglas Gregor   }
692*e7ab3669SDouglas Gregor 
693*e7ab3669SDouglas Gregor   StringRef ModuleName = Id.back().first;
694*e7ab3669SDouglas Gregor   SourceLocation ModuleNameLoc = Id.back().second;
695718292f2SDouglas Gregor 
696718292f2SDouglas Gregor   // Parse the opening brace.
697718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
698718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
699718292f2SDouglas Gregor       << ModuleName;
700718292f2SDouglas Gregor     HadError = true;
701718292f2SDouglas Gregor     return;
702718292f2SDouglas Gregor   }
703718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
704718292f2SDouglas Gregor 
705718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
706718292f2SDouglas Gregor   llvm::StringMap<Module *> &ModuleSpace
707718292f2SDouglas Gregor     = ActiveModule? ActiveModule->SubModules : Map.Modules;
708718292f2SDouglas Gregor   llvm::StringMap<Module *>::iterator ExistingModule
709718292f2SDouglas Gregor     = ModuleSpace.find(ModuleName);
710718292f2SDouglas Gregor   if (ExistingModule != ModuleSpace.end()) {
711718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
712718292f2SDouglas Gregor       << ModuleName;
713718292f2SDouglas Gregor     Diags.Report(ExistingModule->getValue()->DefinitionLoc,
714718292f2SDouglas Gregor                  diag::note_mmap_prev_definition);
715718292f2SDouglas Gregor 
716718292f2SDouglas Gregor     // Skip the module definition.
717718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
718718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
719718292f2SDouglas Gregor       consumeToken();
720718292f2SDouglas Gregor 
721718292f2SDouglas Gregor     HadError = true;
722718292f2SDouglas Gregor     return;
723718292f2SDouglas Gregor   }
724718292f2SDouglas Gregor 
725718292f2SDouglas Gregor   // Start defining this module.
726755b2055SDouglas Gregor   ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Framework,
727755b2055SDouglas Gregor                             Explicit);
728718292f2SDouglas Gregor   ModuleSpace[ModuleName] = ActiveModule;
729718292f2SDouglas Gregor 
730718292f2SDouglas Gregor   bool Done = false;
731718292f2SDouglas Gregor   do {
732718292f2SDouglas Gregor     switch (Tok.Kind) {
733718292f2SDouglas Gregor     case MMToken::EndOfFile:
734718292f2SDouglas Gregor     case MMToken::RBrace:
735718292f2SDouglas Gregor       Done = true;
736718292f2SDouglas Gregor       break;
737718292f2SDouglas Gregor 
738718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
739f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
740718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
741718292f2SDouglas Gregor       parseModuleDecl();
742718292f2SDouglas Gregor       break;
743718292f2SDouglas Gregor 
7442b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
7452b82c2a5SDouglas Gregor       parseExportDecl();
7462b82c2a5SDouglas Gregor       break;
7472b82c2a5SDouglas Gregor 
748718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
749718292f2SDouglas Gregor       parseHeaderDecl();
750718292f2SDouglas Gregor       break;
751718292f2SDouglas Gregor 
752718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
753718292f2SDouglas Gregor       parseUmbrellaDecl();
754718292f2SDouglas Gregor       break;
755718292f2SDouglas Gregor 
756718292f2SDouglas Gregor     default:
757718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
758718292f2SDouglas Gregor       consumeToken();
759718292f2SDouglas Gregor       break;
760718292f2SDouglas Gregor     }
761718292f2SDouglas Gregor   } while (!Done);
762718292f2SDouglas Gregor 
763718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
764718292f2SDouglas Gregor     consumeToken();
765718292f2SDouglas Gregor   else {
766718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
767718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
768718292f2SDouglas Gregor     HadError = true;
769718292f2SDouglas Gregor   }
770718292f2SDouglas Gregor 
771*e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
772*e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
773718292f2SDouglas Gregor }
774718292f2SDouglas Gregor 
775f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
776f2161a70SDouglas Gregor /// subframework in which the given module lives.
777f2161a70SDouglas Gregor void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) {
778f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
779f2161a70SDouglas Gregor   llvm::SmallVector<StringRef, 2> Paths;
780f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
781f2161a70SDouglas Gregor     if (Mod->IsFramework)
782f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
783f2161a70SDouglas Gregor   }
784f2161a70SDouglas Gregor 
785f2161a70SDouglas Gregor   if (Paths.empty())
786f2161a70SDouglas Gregor     return;
787f2161a70SDouglas Gregor 
788f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
789f2161a70SDouglas Gregor   for (unsigned I = Paths.size() - 1; I != 0; --I) {
790f2161a70SDouglas Gregor     llvm::sys::path::append(Path, "Frameworks");
791f2161a70SDouglas Gregor     llvm::sys::path::append(Path, Paths[I-1] + ".framework");
792f2161a70SDouglas Gregor   }
793f2161a70SDouglas Gregor }
794f2161a70SDouglas Gregor 
795718292f2SDouglas Gregor /// \brief Parse an umbrella header declaration.
796718292f2SDouglas Gregor ///
797718292f2SDouglas Gregor ///   umbrella-declaration:
798718292f2SDouglas Gregor ///     'umbrella' string-literal
799718292f2SDouglas Gregor void ModuleMapParser::parseUmbrellaDecl() {
800718292f2SDouglas Gregor   assert(Tok.is(MMToken::UmbrellaKeyword));
801718292f2SDouglas Gregor   SourceLocation UmbrellaLoc = consumeToken();
802718292f2SDouglas Gregor 
803718292f2SDouglas Gregor   // Parse the header name.
804718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
805718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
806718292f2SDouglas Gregor       << "umbrella";
807718292f2SDouglas Gregor     HadError = true;
808718292f2SDouglas Gregor     return;
809718292f2SDouglas Gregor   }
810*e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
811718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
812718292f2SDouglas Gregor 
8135257fc63SDouglas Gregor   // Check whether we already have an umbrella header.
8145257fc63SDouglas Gregor   if (ActiveModule->UmbrellaHeader) {
8155257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_header_conflict)
8165257fc63SDouglas Gregor       << ActiveModule->getFullModuleName()
8175257fc63SDouglas Gregor       << ActiveModule->UmbrellaHeader->getName();
8185257fc63SDouglas Gregor     HadError = true;
8195257fc63SDouglas Gregor     return;
8205257fc63SDouglas Gregor   }
8215257fc63SDouglas Gregor 
8225257fc63SDouglas Gregor   // Look for this file.
8235257fc63SDouglas Gregor   llvm::SmallString<128> PathName;
824f545f67dSDouglas Gregor   const FileEntry *File = 0;
825f545f67dSDouglas Gregor 
826f545f67dSDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
827f545f67dSDouglas Gregor     PathName = FileName;
828f545f67dSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
829f545f67dSDouglas Gregor   } else {
830f545f67dSDouglas Gregor     // Search for the header file within the search directory.
8315257fc63SDouglas Gregor     PathName += Directory->getName();
832755b2055SDouglas Gregor     unsigned PathLength = PathName.size();
833f2161a70SDouglas Gregor 
834755b2055SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
835f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
836f2161a70SDouglas Gregor 
837755b2055SDouglas Gregor       // Check whether this file is in the public headers.
838755b2055SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
8395257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
840755b2055SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
841755b2055SDouglas Gregor 
842755b2055SDouglas Gregor       if (!File) {
843755b2055SDouglas Gregor         // Check whether this file is in the private headers.
844755b2055SDouglas Gregor         PathName.resize(PathLength);
845755b2055SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
846755b2055SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
847755b2055SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
848755b2055SDouglas Gregor       }
849755b2055SDouglas Gregor     } else {
850755b2055SDouglas Gregor       // Lookup for normal headers.
851755b2055SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
852755b2055SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
853755b2055SDouglas Gregor     }
854f545f67dSDouglas Gregor   }
8555257fc63SDouglas Gregor 
8565257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
8575257fc63SDouglas Gregor   // Come up with a lazy way to do this.
858755b2055SDouglas Gregor   if (File) {
859a89c5ac4SDouglas Gregor     const DirectoryEntry *UmbrellaDir = File->getDir();
860a89c5ac4SDouglas Gregor     if (ActiveModule->IsFramework) {
861a89c5ac4SDouglas Gregor       // For framework modules, use the framework directory as the umbrella
862a89c5ac4SDouglas Gregor       // directory.
863a89c5ac4SDouglas Gregor       UmbrellaDir = SourceMgr.getFileManager().getDirectory(
864a89c5ac4SDouglas Gregor                       llvm::sys::path::parent_path(UmbrellaDir->getName()));
865a89c5ac4SDouglas Gregor     }
866a89c5ac4SDouglas Gregor 
8675257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
8685257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
8695257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
8705257fc63SDouglas Gregor       HadError = true;
871a89c5ac4SDouglas Gregor     } else if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
872b65dbfffSDouglas Gregor       Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
873b65dbfffSDouglas Gregor         << OwningModule->getFullModuleName();
874b65dbfffSDouglas Gregor       HadError = true;
8755257fc63SDouglas Gregor     } else {
8765257fc63SDouglas Gregor       // Record this umbrella header.
877a89c5ac4SDouglas Gregor       Map.setUmbrellaHeader(ActiveModule, File);
8785257fc63SDouglas Gregor     }
8795257fc63SDouglas Gregor   } else {
8805257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
8815257fc63SDouglas Gregor       << true << FileName;
8825257fc63SDouglas Gregor     HadError = true;
8835257fc63SDouglas Gregor   }
884718292f2SDouglas Gregor }
885718292f2SDouglas Gregor 
886718292f2SDouglas Gregor /// \brief Parse a header declaration.
887718292f2SDouglas Gregor ///
888718292f2SDouglas Gregor ///   header-declaration:
889718292f2SDouglas Gregor ///     'header' string-literal
890718292f2SDouglas Gregor void ModuleMapParser::parseHeaderDecl() {
891718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
8921871ed3dSBenjamin Kramer   consumeToken();
893718292f2SDouglas Gregor 
894718292f2SDouglas Gregor   // Parse the header name.
895718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
896718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
897718292f2SDouglas Gregor       << "header";
898718292f2SDouglas Gregor     HadError = true;
899718292f2SDouglas Gregor     return;
900718292f2SDouglas Gregor   }
901*e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
902718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
903718292f2SDouglas Gregor 
9045257fc63SDouglas Gregor   // Look for this file.
905*e7ab3669SDouglas Gregor   const FileEntry *File = 0;
9065257fc63SDouglas Gregor   llvm::SmallString<128> PathName;
907*e7ab3669SDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
908*e7ab3669SDouglas Gregor     PathName = FileName;
909*e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
910*e7ab3669SDouglas Gregor   } else {
911*e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
9125257fc63SDouglas Gregor     PathName += Directory->getName();
913*e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
914755b2055SDouglas Gregor 
915f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
916f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
917755b2055SDouglas Gregor 
918*e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
919*e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
9205257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
921*e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
922*e7ab3669SDouglas Gregor 
923*e7ab3669SDouglas Gregor       if (!File) {
924*e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
925*e7ab3669SDouglas Gregor         PathName.resize(PathLength);
926*e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
927*e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
928*e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
929*e7ab3669SDouglas Gregor       }
930*e7ab3669SDouglas Gregor     } else {
931*e7ab3669SDouglas Gregor       // Lookup for normal headers.
932*e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
933*e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
934*e7ab3669SDouglas Gregor     }
935*e7ab3669SDouglas Gregor   }
9365257fc63SDouglas Gregor 
9375257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
9385257fc63SDouglas Gregor   // Come up with a lazy way to do this.
939*e7ab3669SDouglas Gregor   if (File) {
9405257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
9415257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
9425257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
9435257fc63SDouglas Gregor       HadError = true;
9445257fc63SDouglas Gregor     } else {
9455257fc63SDouglas Gregor       // Record this file.
946a89c5ac4SDouglas Gregor       Map.addHeader(ActiveModule, File);
9475257fc63SDouglas Gregor     }
9485257fc63SDouglas Gregor   } else {
9495257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
9505257fc63SDouglas Gregor       << false << FileName;
9515257fc63SDouglas Gregor     HadError = true;
9525257fc63SDouglas Gregor   }
953718292f2SDouglas Gregor }
954718292f2SDouglas Gregor 
9552b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
9562b82c2a5SDouglas Gregor ///
9572b82c2a5SDouglas Gregor ///   export-declaration:
9582b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
9592b82c2a5SDouglas Gregor ///
9602b82c2a5SDouglas Gregor ///   wildcard-module-id:
9612b82c2a5SDouglas Gregor ///     identifier
9622b82c2a5SDouglas Gregor ///     '*'
9632b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
9642b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
9652b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
9662b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
9672b82c2a5SDouglas Gregor 
9682b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
9692b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
9702b82c2a5SDouglas Gregor   bool Wildcard = false;
9712b82c2a5SDouglas Gregor   do {
9722b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
9732b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
9742b82c2a5SDouglas Gregor                                               Tok.getLocation()));
9752b82c2a5SDouglas Gregor       consumeToken();
9762b82c2a5SDouglas Gregor 
9772b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
9782b82c2a5SDouglas Gregor         consumeToken();
9792b82c2a5SDouglas Gregor         continue;
9802b82c2a5SDouglas Gregor       }
9812b82c2a5SDouglas Gregor 
9822b82c2a5SDouglas Gregor       break;
9832b82c2a5SDouglas Gregor     }
9842b82c2a5SDouglas Gregor 
9852b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
9862b82c2a5SDouglas Gregor       Wildcard = true;
987f5eedd05SDouglas Gregor       consumeToken();
9882b82c2a5SDouglas Gregor       break;
9892b82c2a5SDouglas Gregor     }
9902b82c2a5SDouglas Gregor 
9912b82c2a5SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
9922b82c2a5SDouglas Gregor     HadError = true;
9932b82c2a5SDouglas Gregor     return;
9942b82c2a5SDouglas Gregor   } while (true);
9952b82c2a5SDouglas Gregor 
9962b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
9972b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
9982b82c2a5SDouglas Gregor   };
9992b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
10002b82c2a5SDouglas Gregor }
10012b82c2a5SDouglas Gregor 
100273441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
100373441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
100473441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
100573441091SDouglas Gregor   bool Failed = false;
100673441091SDouglas Gregor 
100773441091SDouglas Gregor   // Inferred modules must be submodules.
100873441091SDouglas Gregor   if (!ActiveModule) {
100973441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
101073441091SDouglas Gregor     Failed = true;
101173441091SDouglas Gregor   }
101273441091SDouglas Gregor 
101373441091SDouglas Gregor   // Inferred modules must have umbrella headers.
1014dd005f69SDouglas Gregor   if (!Failed && !ActiveModule->UmbrellaHeader) {
101573441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
101673441091SDouglas Gregor     Failed = true;
101773441091SDouglas Gregor   }
101873441091SDouglas Gregor 
101973441091SDouglas Gregor   // Check for redefinition of an inferred module.
1020dd005f69SDouglas Gregor   if (!Failed && ActiveModule->InferSubmodules) {
102173441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1022dd005f69SDouglas Gregor     if (ActiveModule->InferredSubmoduleLoc.isValid())
1023dd005f69SDouglas Gregor       Diags.Report(ActiveModule->InferredSubmoduleLoc,
102473441091SDouglas Gregor                    diag::note_mmap_prev_definition);
102573441091SDouglas Gregor     Failed = true;
102673441091SDouglas Gregor   }
102773441091SDouglas Gregor 
102873441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
102973441091SDouglas Gregor   if (Failed) {
103073441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
103173441091SDouglas Gregor       consumeToken();
103273441091SDouglas Gregor       skipUntil(MMToken::RBrace);
103373441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
103473441091SDouglas Gregor         consumeToken();
103573441091SDouglas Gregor     }
103673441091SDouglas Gregor     HadError = true;
103773441091SDouglas Gregor     return;
103873441091SDouglas Gregor   }
103973441091SDouglas Gregor 
104073441091SDouglas Gregor   // Note that we have an inferred submodule.
1041dd005f69SDouglas Gregor   ActiveModule->InferSubmodules = true;
1042dd005f69SDouglas Gregor   ActiveModule->InferredSubmoduleLoc = StarLoc;
1043dd005f69SDouglas Gregor   ActiveModule->InferExplicitSubmodules = Explicit;
104473441091SDouglas Gregor 
104573441091SDouglas Gregor   // Parse the opening brace.
104673441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
104773441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
104873441091SDouglas Gregor     HadError = true;
104973441091SDouglas Gregor     return;
105073441091SDouglas Gregor   }
105173441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
105273441091SDouglas Gregor 
105373441091SDouglas Gregor   // Parse the body of the inferred submodule.
105473441091SDouglas Gregor   bool Done = false;
105573441091SDouglas Gregor   do {
105673441091SDouglas Gregor     switch (Tok.Kind) {
105773441091SDouglas Gregor     case MMToken::EndOfFile:
105873441091SDouglas Gregor     case MMToken::RBrace:
105973441091SDouglas Gregor       Done = true;
106073441091SDouglas Gregor       break;
106173441091SDouglas Gregor 
106273441091SDouglas Gregor     case MMToken::ExportKeyword: {
106373441091SDouglas Gregor       consumeToken();
106473441091SDouglas Gregor       if (Tok.is(MMToken::Star))
1065dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
106673441091SDouglas Gregor       else
106773441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
106873441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
106973441091SDouglas Gregor       consumeToken();
107073441091SDouglas Gregor       break;
107173441091SDouglas Gregor     }
107273441091SDouglas Gregor 
107373441091SDouglas Gregor     case MMToken::ExplicitKeyword:
107473441091SDouglas Gregor     case MMToken::ModuleKeyword:
107573441091SDouglas Gregor     case MMToken::HeaderKeyword:
107673441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
107773441091SDouglas Gregor     default:
107873441091SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
107973441091SDouglas Gregor       consumeToken();
108073441091SDouglas Gregor       break;
108173441091SDouglas Gregor     }
108273441091SDouglas Gregor   } while (!Done);
108373441091SDouglas Gregor 
108473441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
108573441091SDouglas Gregor     consumeToken();
108673441091SDouglas Gregor   else {
108773441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
108873441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
108973441091SDouglas Gregor     HadError = true;
109073441091SDouglas Gregor   }
109173441091SDouglas Gregor }
109273441091SDouglas Gregor 
1093718292f2SDouglas Gregor /// \brief Parse a module map file.
1094718292f2SDouglas Gregor ///
1095718292f2SDouglas Gregor ///   module-map-file:
1096718292f2SDouglas Gregor ///     module-declaration*
1097718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
1098718292f2SDouglas Gregor   do {
1099718292f2SDouglas Gregor     switch (Tok.Kind) {
1100718292f2SDouglas Gregor     case MMToken::EndOfFile:
1101718292f2SDouglas Gregor       return HadError;
1102718292f2SDouglas Gregor 
1103*e7ab3669SDouglas Gregor     case MMToken::ExplicitKeyword:
1104718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1105755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
1106718292f2SDouglas Gregor       parseModuleDecl();
1107718292f2SDouglas Gregor       break;
1108718292f2SDouglas Gregor 
11092b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
1110718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
1111718292f2SDouglas Gregor     case MMToken::Identifier:
1112718292f2SDouglas Gregor     case MMToken::LBrace:
11132b82c2a5SDouglas Gregor     case MMToken::Period:
1114718292f2SDouglas Gregor     case MMToken::RBrace:
11152b82c2a5SDouglas Gregor     case MMToken::Star:
1116718292f2SDouglas Gregor     case MMToken::StringLiteral:
1117718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
1118718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1119718292f2SDouglas Gregor       HadError = true;
1120718292f2SDouglas Gregor       consumeToken();
1121718292f2SDouglas Gregor       break;
1122718292f2SDouglas Gregor     }
1123718292f2SDouglas Gregor   } while (true);
1124718292f2SDouglas Gregor 
1125718292f2SDouglas Gregor   return HadError;
1126718292f2SDouglas Gregor }
1127718292f2SDouglas Gregor 
1128718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
1129718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1130718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1131718292f2SDouglas Gregor   if (!Buffer)
1132718292f2SDouglas Gregor     return true;
1133718292f2SDouglas Gregor 
1134718292f2SDouglas Gregor   // Parse this module map file.
1135718292f2SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts);
1136718292f2SDouglas Gregor   Diags->getClient()->BeginSourceFile(LangOpts);
11375257fc63SDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir());
1138718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
1139718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
1140718292f2SDouglas Gregor 
1141718292f2SDouglas Gregor   return Result;
1142718292f2SDouglas Gregor }
1143