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"
2907c22b78SDouglas Gregor #include <stdlib.h>
30718292f2SDouglas Gregor using namespace clang;
31718292f2SDouglas Gregor 
322b82c2a5SDouglas Gregor Module::ExportDecl
332b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod,
342b82c2a5SDouglas Gregor                          const Module::UnresolvedExportDecl &Unresolved,
352b82c2a5SDouglas Gregor                          bool Complain) {
36f5eedd05SDouglas Gregor   // We may have just a wildcard.
37f5eedd05SDouglas Gregor   if (Unresolved.Id.empty()) {
38f5eedd05SDouglas Gregor     assert(Unresolved.Wildcard && "Invalid unresolved export");
39f5eedd05SDouglas Gregor     return Module::ExportDecl(0, true);
40f5eedd05SDouglas Gregor   }
41f5eedd05SDouglas Gregor 
422b82c2a5SDouglas Gregor   // Find the starting module.
432b82c2a5SDouglas Gregor   Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
442b82c2a5SDouglas Gregor   if (!Context) {
452b82c2a5SDouglas Gregor     if (Complain)
462b82c2a5SDouglas Gregor       Diags->Report(Unresolved.Id[0].second,
472b82c2a5SDouglas Gregor                     diag::err_mmap_missing_module_unqualified)
482b82c2a5SDouglas Gregor         << Unresolved.Id[0].first << Mod->getFullModuleName();
492b82c2a5SDouglas Gregor 
502b82c2a5SDouglas Gregor     return Module::ExportDecl();
512b82c2a5SDouglas Gregor   }
522b82c2a5SDouglas Gregor 
532b82c2a5SDouglas Gregor   // Dig into the module path.
542b82c2a5SDouglas Gregor   for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
552b82c2a5SDouglas Gregor     Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
562b82c2a5SDouglas Gregor                                         Context);
572b82c2a5SDouglas Gregor     if (!Sub) {
582b82c2a5SDouglas Gregor       if (Complain)
592b82c2a5SDouglas Gregor         Diags->Report(Unresolved.Id[I].second,
602b82c2a5SDouglas Gregor                       diag::err_mmap_missing_module_qualified)
612b82c2a5SDouglas Gregor           << Unresolved.Id[I].first << Context->getFullModuleName()
622b82c2a5SDouglas Gregor           << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
632b82c2a5SDouglas Gregor 
642b82c2a5SDouglas Gregor       return Module::ExportDecl();
652b82c2a5SDouglas Gregor     }
662b82c2a5SDouglas Gregor 
672b82c2a5SDouglas Gregor     Context = Sub;
682b82c2a5SDouglas Gregor   }
692b82c2a5SDouglas Gregor 
702b82c2a5SDouglas Gregor   return Module::ExportDecl(Context, Unresolved.Wildcard);
712b82c2a5SDouglas Gregor }
722b82c2a5SDouglas Gregor 
731fb5c3a6SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
7489929282SDouglas Gregor                      const LangOptions &LangOpts, const TargetInfo *Target)
753ec6663bSDouglas Gregor   : LangOpts(LangOpts), Target(Target), BuiltinIncludeDir(0)
761fb5c3a6SDouglas Gregor {
77c95d8192SDylan Noblesmith   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
78c95d8192SDylan Noblesmith   Diags = IntrusiveRefCntPtr<DiagnosticsEngine>(
79718292f2SDouglas Gregor             new DiagnosticsEngine(DiagIDs));
80718292f2SDouglas Gregor   Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
81718292f2SDouglas Gregor   SourceMgr = new SourceManager(*Diags, FileMgr);
82718292f2SDouglas Gregor }
83718292f2SDouglas Gregor 
84718292f2SDouglas Gregor ModuleMap::~ModuleMap() {
855acdf59eSDouglas Gregor   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
865acdf59eSDouglas Gregor                                         IEnd = Modules.end();
875acdf59eSDouglas Gregor        I != IEnd; ++I) {
885acdf59eSDouglas Gregor     delete I->getValue();
895acdf59eSDouglas Gregor   }
905acdf59eSDouglas Gregor 
91718292f2SDouglas Gregor   delete SourceMgr;
92718292f2SDouglas Gregor }
93718292f2SDouglas Gregor 
9489929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) {
9589929282SDouglas Gregor   assert((!this->Target || this->Target == &Target) &&
9689929282SDouglas Gregor          "Improper target override");
9789929282SDouglas Gregor   this->Target = &Target;
9889929282SDouglas Gregor }
9989929282SDouglas Gregor 
100de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
101ab0c8a84SDouglas Gregor   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
102ab0c8a84SDouglas Gregor     = Headers.find(File);
1031fb5c3a6SDouglas Gregor   if (Known != Headers.end()) {
1041fb5c3a6SDouglas Gregor     // If a header corresponds to an unavailable module, don't report
1051fb5c3a6SDouglas Gregor     // that it maps to anything.
1061fb5c3a6SDouglas Gregor     if (!Known->second->isAvailable())
1071fb5c3a6SDouglas Gregor       return 0;
1081fb5c3a6SDouglas Gregor 
109ab0c8a84SDouglas Gregor     return Known->second;
1101fb5c3a6SDouglas Gregor   }
111ab0c8a84SDouglas Gregor 
112b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
113b65dbfffSDouglas Gregor   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
114b65dbfffSDouglas Gregor   StringRef DirName = Dir->getName();
115a89c5ac4SDouglas Gregor 
116a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
117a89c5ac4SDouglas Gregor   // an umbrella header.
118b65dbfffSDouglas Gregor   do {
119a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
120a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
121a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
122a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
123930a85ccSDouglas Gregor 
124930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
12573141fa9SDouglas Gregor       // directory.
126930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
12773141fa9SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
128930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
129930a85ccSDouglas Gregor 
130930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
131a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
132a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
133a89c5ac4SDouglas Gregor         // the actual header is located.
1349458f82dSDouglas Gregor         bool Explicit = UmbrellaModule->InferExplicitSubmodules;
1359458f82dSDouglas Gregor 
1367033127bSDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
137a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
138a89c5ac4SDouglas Gregor           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
139a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
1409458f82dSDouglas Gregor                                       Explicit).first;
141a89c5ac4SDouglas Gregor 
142a89c5ac4SDouglas Gregor           // Associate the module and the directory.
143a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
144a89c5ac4SDouglas Gregor 
145a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
146a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
147930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
148a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
149a89c5ac4SDouglas Gregor         }
150a89c5ac4SDouglas Gregor 
151a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
152a89c5ac4SDouglas Gregor         StringRef Name = llvm::sys::path::stem(File->getName());
153a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
1549458f82dSDouglas Gregor                                     Explicit).first;
155*c597c8c4SArgyrios Kyrtzidis         Result->TopHeaders.insert(File);
156a89c5ac4SDouglas Gregor 
157a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
158a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
159930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
160a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
161a89c5ac4SDouglas Gregor       } else {
162a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
163a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
164a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
165a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
166a89c5ac4SDouglas Gregor       }
167a89c5ac4SDouglas Gregor 
168a89c5ac4SDouglas Gregor       Headers[File] = Result;
1691fb5c3a6SDouglas Gregor 
1701fb5c3a6SDouglas Gregor       // If a header corresponds to an unavailable module, don't report
1711fb5c3a6SDouglas Gregor       // that it maps to anything.
1721fb5c3a6SDouglas Gregor       if (!Result->isAvailable())
1731fb5c3a6SDouglas Gregor         return 0;
1741fb5c3a6SDouglas Gregor 
175a89c5ac4SDouglas Gregor       return Result;
176a89c5ac4SDouglas Gregor     }
177a89c5ac4SDouglas Gregor 
178a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
179a89c5ac4SDouglas Gregor 
180b65dbfffSDouglas Gregor     // Retrieve our parent path.
181b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
182b65dbfffSDouglas Gregor     if (DirName.empty())
183b65dbfffSDouglas Gregor       break;
184b65dbfffSDouglas Gregor 
185b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
186b65dbfffSDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
187a89c5ac4SDouglas Gregor   } while (Dir);
188b65dbfffSDouglas Gregor 
189ab0c8a84SDouglas Gregor   return 0;
190ab0c8a84SDouglas Gregor }
191ab0c8a84SDouglas Gregor 
1921fb5c3a6SDouglas Gregor bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) {
1931fb5c3a6SDouglas Gregor   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
1941fb5c3a6SDouglas Gregor     = Headers.find(Header);
1951fb5c3a6SDouglas Gregor   if (Known != Headers.end())
1961fb5c3a6SDouglas Gregor     return !Known->second->isAvailable();
1971fb5c3a6SDouglas Gregor 
1981fb5c3a6SDouglas Gregor   const DirectoryEntry *Dir = Header->getDir();
1991fb5c3a6SDouglas Gregor   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
2001fb5c3a6SDouglas Gregor   StringRef DirName = Dir->getName();
2011fb5c3a6SDouglas Gregor 
2021fb5c3a6SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
2031fb5c3a6SDouglas Gregor   // an umbrella header.
2041fb5c3a6SDouglas Gregor   do {
2051fb5c3a6SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
2061fb5c3a6SDouglas Gregor       = UmbrellaDirs.find(Dir);
2071fb5c3a6SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
2081fb5c3a6SDouglas Gregor       Module *Found = KnownDir->second;
2091fb5c3a6SDouglas Gregor       if (!Found->isAvailable())
2101fb5c3a6SDouglas Gregor         return true;
2111fb5c3a6SDouglas Gregor 
2121fb5c3a6SDouglas Gregor       // Search up the module stack until we find a module with an umbrella
2131fb5c3a6SDouglas Gregor       // directory.
2141fb5c3a6SDouglas Gregor       Module *UmbrellaModule = Found;
2151fb5c3a6SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
2161fb5c3a6SDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
2171fb5c3a6SDouglas Gregor 
2181fb5c3a6SDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
2191fb5c3a6SDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
2201fb5c3a6SDouglas Gregor           // Find or create the module that corresponds to this directory name.
2211fb5c3a6SDouglas Gregor           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
2221fb5c3a6SDouglas Gregor           Found = lookupModuleQualified(Name, Found);
2231fb5c3a6SDouglas Gregor           if (!Found)
2241fb5c3a6SDouglas Gregor             return false;
2251fb5c3a6SDouglas Gregor           if (!Found->isAvailable())
2261fb5c3a6SDouglas Gregor             return true;
2271fb5c3a6SDouglas Gregor         }
2281fb5c3a6SDouglas Gregor 
2291fb5c3a6SDouglas Gregor         // Infer a submodule with the same name as this header file.
2301fb5c3a6SDouglas Gregor         StringRef Name = llvm::sys::path::stem(Header->getName());
2311fb5c3a6SDouglas Gregor         Found = lookupModuleQualified(Name, Found);
2321fb5c3a6SDouglas Gregor         if (!Found)
2331fb5c3a6SDouglas Gregor           return false;
2341fb5c3a6SDouglas Gregor       }
2351fb5c3a6SDouglas Gregor 
2361fb5c3a6SDouglas Gregor       return !Found->isAvailable();
2371fb5c3a6SDouglas Gregor     }
2381fb5c3a6SDouglas Gregor 
2391fb5c3a6SDouglas Gregor     SkippedDirs.push_back(Dir);
2401fb5c3a6SDouglas Gregor 
2411fb5c3a6SDouglas Gregor     // Retrieve our parent path.
2421fb5c3a6SDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
2431fb5c3a6SDouglas Gregor     if (DirName.empty())
2441fb5c3a6SDouglas Gregor       break;
2451fb5c3a6SDouglas Gregor 
2461fb5c3a6SDouglas Gregor     // Resolve the parent path to a directory entry.
2471fb5c3a6SDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
2481fb5c3a6SDouglas Gregor   } while (Dir);
2491fb5c3a6SDouglas Gregor 
2501fb5c3a6SDouglas Gregor   return false;
2511fb5c3a6SDouglas Gregor }
2521fb5c3a6SDouglas Gregor 
253de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) {
25488bdfb0eSDouglas Gregor   llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
25588bdfb0eSDouglas Gregor   if (Known != Modules.end())
25688bdfb0eSDouglas Gregor     return Known->getValue();
25788bdfb0eSDouglas Gregor 
25888bdfb0eSDouglas Gregor   return 0;
25988bdfb0eSDouglas Gregor }
26088bdfb0eSDouglas Gregor 
2612b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
2622b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
2632b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
2642b82c2a5SDouglas Gregor       return Sub;
2652b82c2a5SDouglas Gregor   }
2662b82c2a5SDouglas Gregor 
2672b82c2a5SDouglas Gregor   return findModule(Name);
2682b82c2a5SDouglas Gregor }
2692b82c2a5SDouglas Gregor 
2702b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
2712b82c2a5SDouglas Gregor   if (!Context)
2722b82c2a5SDouglas Gregor     return findModule(Name);
2732b82c2a5SDouglas Gregor 
274eb90e830SDouglas Gregor   return Context->findSubmodule(Name);
2752b82c2a5SDouglas Gregor }
2762b82c2a5SDouglas Gregor 
277de3ef502SDouglas Gregor std::pair<Module *, bool>
27869021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
27969021974SDouglas Gregor                               bool IsExplicit) {
28069021974SDouglas Gregor   // Try to find an existing module with this name.
281eb90e830SDouglas Gregor   if (Module *Sub = lookupModuleQualified(Name, Parent))
282eb90e830SDouglas Gregor     return std::make_pair(Sub, false);
28369021974SDouglas Gregor 
28469021974SDouglas Gregor   // Create a new module with this name.
28569021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
28669021974SDouglas Gregor                               IsExplicit);
287eb90e830SDouglas Gregor   if (!Parent)
28869021974SDouglas Gregor     Modules[Name] = Result;
28969021974SDouglas Gregor   return std::make_pair(Result, true);
29069021974SDouglas Gregor }
29169021974SDouglas Gregor 
292de3ef502SDouglas Gregor Module *
29356c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
294e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
295a686e1b0SDouglas Gregor                                 bool IsSystem,
296e89dbc1dSDouglas Gregor                                 Module *Parent) {
29756c64013SDouglas Gregor   // Check whether we've already found this module.
298e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
299e89dbc1dSDouglas Gregor     return Mod;
300e89dbc1dSDouglas Gregor 
301e89dbc1dSDouglas Gregor   FileManager &FileMgr = SourceMgr->getFileManager();
30256c64013SDouglas Gregor 
30356c64013SDouglas Gregor   // Look for an umbrella header.
3042c1dd271SDylan Noblesmith   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
30556c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, "Headers");
30656c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
307e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
30856c64013SDouglas Gregor 
30956c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
31056c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
31156c64013SDouglas Gregor   // idea.
31256c64013SDouglas Gregor   if (!UmbrellaHeader)
31356c64013SDouglas Gregor     return 0;
31456c64013SDouglas Gregor 
315e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
316e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
317a686e1b0SDouglas Gregor   if (IsSystem)
318a686e1b0SDouglas Gregor     Result->IsSystem = IsSystem;
319a686e1b0SDouglas Gregor 
320eb90e830SDouglas Gregor   if (!Parent)
321e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
322e89dbc1dSDouglas Gregor 
323322f633cSDouglas Gregor   // umbrella header "umbrella-header-name"
32473141fa9SDouglas Gregor   Result->Umbrella = UmbrellaHeader;
32556c64013SDouglas Gregor   Headers[UmbrellaHeader] = Result;
3264dc71835SDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
327d8bd7537SDouglas Gregor 
328d8bd7537SDouglas Gregor   // export *
329d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
330d8bd7537SDouglas Gregor 
331a89c5ac4SDouglas Gregor   // module * { export * }
332a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
333a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
334a89c5ac4SDouglas Gregor 
335e89dbc1dSDouglas Gregor   // Look for subframeworks.
336e89dbc1dSDouglas Gregor   llvm::error_code EC;
3372c1dd271SDylan Noblesmith   SmallString<128> SubframeworksDirName
338ddaa69cbSDouglas Gregor     = StringRef(FrameworkDir->getName());
339e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
3402c1dd271SDylan Noblesmith   SmallString<128> SubframeworksDirNameNative;
341ddaa69cbSDouglas Gregor   llvm::sys::path::native(SubframeworksDirName.str(),
342ddaa69cbSDouglas Gregor                           SubframeworksDirNameNative);
343ddaa69cbSDouglas Gregor   for (llvm::sys::fs::directory_iterator
344ddaa69cbSDouglas Gregor          Dir(SubframeworksDirNameNative.str(), EC), DirEnd;
345e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
346e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
347e89dbc1dSDouglas Gregor       continue;
348f2161a70SDouglas Gregor 
349e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
350e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
35107c22b78SDouglas Gregor       // Note: as an egregious but useful hack, we use the real path here and
35207c22b78SDouglas Gregor       // check whether it is actually a subdirectory of the parent directory.
35307c22b78SDouglas Gregor       // This will not be the case if the 'subframework' is actually a symlink
35407c22b78SDouglas Gregor       // out to a top-level framework.
35507c22b78SDouglas Gregor #ifdef LLVM_ON_UNIX
35607c22b78SDouglas Gregor       char RealSubframeworkDirName[PATH_MAX];
35707c22b78SDouglas Gregor       if (realpath(Dir->path().c_str(), RealSubframeworkDirName)) {
35807c22b78SDouglas Gregor         StringRef SubframeworkDirName = RealSubframeworkDirName;
35907c22b78SDouglas Gregor 
36007c22b78SDouglas Gregor         bool FoundParent = false;
36107c22b78SDouglas Gregor         do {
36207c22b78SDouglas Gregor           // Get the parent directory name.
36307c22b78SDouglas Gregor           SubframeworkDirName
36407c22b78SDouglas Gregor             = llvm::sys::path::parent_path(SubframeworkDirName);
36507c22b78SDouglas Gregor           if (SubframeworkDirName.empty())
36607c22b78SDouglas Gregor             break;
36707c22b78SDouglas Gregor 
36807c22b78SDouglas Gregor           if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
36907c22b78SDouglas Gregor             FoundParent = true;
37007c22b78SDouglas Gregor             break;
37107c22b78SDouglas Gregor           }
37207c22b78SDouglas Gregor         } while (true);
37307c22b78SDouglas Gregor 
37407c22b78SDouglas Gregor         if (!FoundParent)
37507c22b78SDouglas Gregor           continue;
37607c22b78SDouglas Gregor       }
37707c22b78SDouglas Gregor #endif
37807c22b78SDouglas Gregor 
379e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
380e89dbc1dSDouglas Gregor       inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
381a686e1b0SDouglas Gregor                            IsSystem, Result);
382e89dbc1dSDouglas Gregor     }
383e89dbc1dSDouglas Gregor   }
384e89dbc1dSDouglas Gregor 
38556c64013SDouglas Gregor   return Result;
38656c64013SDouglas Gregor }
38756c64013SDouglas Gregor 
388a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
389a89c5ac4SDouglas Gregor   Headers[UmbrellaHeader] = Mod;
39073141fa9SDouglas Gregor   Mod->Umbrella = UmbrellaHeader;
3917033127bSDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
392a89c5ac4SDouglas Gregor }
393a89c5ac4SDouglas Gregor 
394524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
395524e33e1SDouglas Gregor   Mod->Umbrella = UmbrellaDir;
396524e33e1SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
397524e33e1SDouglas Gregor }
398524e33e1SDouglas Gregor 
399a89c5ac4SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
400a89c5ac4SDouglas Gregor   Mod->Headers.push_back(Header);
401a89c5ac4SDouglas Gregor   Headers[Header] = Mod;
402a89c5ac4SDouglas Gregor }
403a89c5ac4SDouglas Gregor 
404514b636aSDouglas Gregor const FileEntry *
405de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) {
406514b636aSDouglas Gregor   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
407514b636aSDouglas Gregor     return 0;
408514b636aSDouglas Gregor 
409514b636aSDouglas Gregor   return SourceMgr->getFileEntryForID(
410514b636aSDouglas Gregor            SourceMgr->getFileID(Module->DefinitionLoc));
411514b636aSDouglas Gregor }
412514b636aSDouglas Gregor 
413718292f2SDouglas Gregor void ModuleMap::dump() {
414718292f2SDouglas Gregor   llvm::errs() << "Modules:";
415718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
416718292f2SDouglas Gregor                                         MEnd = Modules.end();
417718292f2SDouglas Gregor        M != MEnd; ++M)
418d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
419718292f2SDouglas Gregor 
420718292f2SDouglas Gregor   llvm::errs() << "Headers:";
421718292f2SDouglas Gregor   for (llvm::DenseMap<const FileEntry *, Module *>::iterator
422718292f2SDouglas Gregor             H = Headers.begin(),
423718292f2SDouglas Gregor          HEnd = Headers.end();
424718292f2SDouglas Gregor        H != HEnd; ++H) {
425718292f2SDouglas Gregor     llvm::errs() << "  \"" << H->first->getName() << "\" -> "
426718292f2SDouglas Gregor                  << H->second->getFullModuleName() << "\n";
427718292f2SDouglas Gregor   }
428718292f2SDouglas Gregor }
429718292f2SDouglas Gregor 
4302b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
4312b82c2a5SDouglas Gregor   bool HadError = false;
4322b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
4332b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
4342b82c2a5SDouglas Gregor                                               Complain);
435f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
4362b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
4372b82c2a5SDouglas Gregor     else
4382b82c2a5SDouglas Gregor       HadError = true;
4392b82c2a5SDouglas Gregor   }
4402b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
4412b82c2a5SDouglas Gregor   return HadError;
4422b82c2a5SDouglas Gregor }
4432b82c2a5SDouglas Gregor 
4440093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
4450093b3c7SDouglas Gregor   if (Loc.isInvalid())
4460093b3c7SDouglas Gregor     return 0;
4470093b3c7SDouglas Gregor 
4480093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
4490093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
4500093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
4510093b3c7SDouglas Gregor     return 0;
4520093b3c7SDouglas Gregor 
4530093b3c7SDouglas Gregor 
4540093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
4550093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
456224d8a74SDouglas Gregor 
457224d8a74SDouglas Gregor   while (const FileEntry *ExpansionFile
458224d8a74SDouglas Gregor            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
459224d8a74SDouglas Gregor     // Find the module that owns this header (if any).
460224d8a74SDouglas Gregor     if (Module *Mod = findModuleForHeader(ExpansionFile))
461224d8a74SDouglas Gregor       return Mod;
462224d8a74SDouglas Gregor 
463224d8a74SDouglas Gregor     // No module owns this header, so look up the inclusion chain to see if
464224d8a74SDouglas Gregor     // any included header has an associated module.
465224d8a74SDouglas Gregor     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
466224d8a74SDouglas Gregor     if (IncludeLoc.isInvalid())
4670093b3c7SDouglas Gregor       return 0;
4680093b3c7SDouglas Gregor 
469224d8a74SDouglas Gregor     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
470224d8a74SDouglas Gregor   }
471224d8a74SDouglas Gregor 
472224d8a74SDouglas Gregor   return 0;
4730093b3c7SDouglas Gregor }
4740093b3c7SDouglas Gregor 
475718292f2SDouglas Gregor //----------------------------------------------------------------------------//
476718292f2SDouglas Gregor // Module map file parser
477718292f2SDouglas Gregor //----------------------------------------------------------------------------//
478718292f2SDouglas Gregor 
479718292f2SDouglas Gregor namespace clang {
480718292f2SDouglas Gregor   /// \brief A token in a module map file.
481718292f2SDouglas Gregor   struct MMToken {
482718292f2SDouglas Gregor     enum TokenKind {
4831fb5c3a6SDouglas Gregor       Comma,
484718292f2SDouglas Gregor       EndOfFile,
485718292f2SDouglas Gregor       HeaderKeyword,
486718292f2SDouglas Gregor       Identifier,
487718292f2SDouglas Gregor       ExplicitKeyword,
4882b82c2a5SDouglas Gregor       ExportKeyword,
489755b2055SDouglas Gregor       FrameworkKeyword,
490718292f2SDouglas Gregor       ModuleKeyword,
4912b82c2a5SDouglas Gregor       Period,
492718292f2SDouglas Gregor       UmbrellaKeyword,
4931fb5c3a6SDouglas Gregor       RequiresKeyword,
4942b82c2a5SDouglas Gregor       Star,
495718292f2SDouglas Gregor       StringLiteral,
496718292f2SDouglas Gregor       LBrace,
497a686e1b0SDouglas Gregor       RBrace,
498a686e1b0SDouglas Gregor       LSquare,
499a686e1b0SDouglas Gregor       RSquare
500718292f2SDouglas Gregor     } Kind;
501718292f2SDouglas Gregor 
502718292f2SDouglas Gregor     unsigned Location;
503718292f2SDouglas Gregor     unsigned StringLength;
504718292f2SDouglas Gregor     const char *StringData;
505718292f2SDouglas Gregor 
506718292f2SDouglas Gregor     void clear() {
507718292f2SDouglas Gregor       Kind = EndOfFile;
508718292f2SDouglas Gregor       Location = 0;
509718292f2SDouglas Gregor       StringLength = 0;
510718292f2SDouglas Gregor       StringData = 0;
511718292f2SDouglas Gregor     }
512718292f2SDouglas Gregor 
513718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
514718292f2SDouglas Gregor 
515718292f2SDouglas Gregor     SourceLocation getLocation() const {
516718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
517718292f2SDouglas Gregor     }
518718292f2SDouglas Gregor 
519718292f2SDouglas Gregor     StringRef getString() const {
520718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
521718292f2SDouglas Gregor     }
522718292f2SDouglas Gregor   };
523718292f2SDouglas Gregor 
524718292f2SDouglas Gregor   class ModuleMapParser {
525718292f2SDouglas Gregor     Lexer &L;
526718292f2SDouglas Gregor     SourceManager &SourceMgr;
527718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
528718292f2SDouglas Gregor     ModuleMap &Map;
529718292f2SDouglas Gregor 
5305257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
5315257fc63SDouglas Gregor     const DirectoryEntry *Directory;
5325257fc63SDouglas Gregor 
5333ec6663bSDouglas Gregor     /// \brief The directory containing Clang-supplied headers.
5343ec6663bSDouglas Gregor     const DirectoryEntry *BuiltinIncludeDir;
5353ec6663bSDouglas Gregor 
536718292f2SDouglas Gregor     /// \brief Whether an error occurred.
537718292f2SDouglas Gregor     bool HadError;
538718292f2SDouglas Gregor 
539718292f2SDouglas Gregor     /// \brief Default target information, used only for string literal
540718292f2SDouglas Gregor     /// parsing.
5413e2320c8SBenjamin Kramer     OwningPtr<TargetInfo> Target;
542718292f2SDouglas Gregor 
543718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
544718292f2SDouglas Gregor     /// during parsing.
545718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
546718292f2SDouglas Gregor 
547718292f2SDouglas Gregor     /// \brief The current token.
548718292f2SDouglas Gregor     MMToken Tok;
549718292f2SDouglas Gregor 
550718292f2SDouglas Gregor     /// \brief The active module.
551de3ef502SDouglas Gregor     Module *ActiveModule;
552718292f2SDouglas Gregor 
553718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
554718292f2SDouglas Gregor     SourceLocation consumeToken();
555718292f2SDouglas Gregor 
556718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
557718292f2SDouglas Gregor     /// (or the end of the file).
558718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
559718292f2SDouglas Gregor 
560e7ab3669SDouglas Gregor     typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
561e7ab3669SDouglas Gregor       ModuleId;
562e7ab3669SDouglas Gregor     bool parseModuleId(ModuleId &Id);
563718292f2SDouglas Gregor     void parseModuleDecl();
5641fb5c3a6SDouglas Gregor     void parseRequiresDecl();
565322f633cSDouglas Gregor     void parseHeaderDecl(SourceLocation UmbrellaLoc);
566524e33e1SDouglas Gregor     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
5672b82c2a5SDouglas Gregor     void parseExportDecl();
56873441091SDouglas Gregor     void parseInferredSubmoduleDecl(bool Explicit);
569718292f2SDouglas Gregor 
5707033127bSDouglas Gregor     const DirectoryEntry *getOverriddenHeaderSearchDir();
5717033127bSDouglas Gregor 
572718292f2SDouglas Gregor   public:
573718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
574718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
5755257fc63SDouglas Gregor                              ModuleMap &Map,
5763ec6663bSDouglas Gregor                              const DirectoryEntry *Directory,
5773ec6663bSDouglas Gregor                              const DirectoryEntry *BuiltinIncludeDir)
5785257fc63SDouglas Gregor       : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
5793ec6663bSDouglas Gregor         Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir),
5803ec6663bSDouglas Gregor         HadError(false), ActiveModule(0)
581718292f2SDouglas Gregor     {
582718292f2SDouglas Gregor       TargetOptions TargetOpts;
583718292f2SDouglas Gregor       TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
5843e2320c8SBenjamin Kramer       Target.reset(TargetInfo::CreateTargetInfo(Diags, TargetOpts));
585718292f2SDouglas Gregor 
586718292f2SDouglas Gregor       Tok.clear();
587718292f2SDouglas Gregor       consumeToken();
588718292f2SDouglas Gregor     }
589718292f2SDouglas Gregor 
590718292f2SDouglas Gregor     bool parseModuleMapFile();
591718292f2SDouglas Gregor   };
592718292f2SDouglas Gregor }
593718292f2SDouglas Gregor 
594718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
595718292f2SDouglas Gregor retry:
596718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
597718292f2SDouglas Gregor   Tok.clear();
598718292f2SDouglas Gregor 
599718292f2SDouglas Gregor   Token LToken;
600718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
601718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
602718292f2SDouglas Gregor   switch (LToken.getKind()) {
603718292f2SDouglas Gregor   case tok::raw_identifier:
604718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
605718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
606718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
607718292f2SDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
608718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
6092b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
610755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
611718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
6121fb5c3a6SDouglas Gregor                  .Case("requires", MMToken::RequiresKeyword)
613718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
614718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
615718292f2SDouglas Gregor     break;
616718292f2SDouglas Gregor 
6171fb5c3a6SDouglas Gregor   case tok::comma:
6181fb5c3a6SDouglas Gregor     Tok.Kind = MMToken::Comma;
6191fb5c3a6SDouglas Gregor     break;
6201fb5c3a6SDouglas Gregor 
621718292f2SDouglas Gregor   case tok::eof:
622718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
623718292f2SDouglas Gregor     break;
624718292f2SDouglas Gregor 
625718292f2SDouglas Gregor   case tok::l_brace:
626718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
627718292f2SDouglas Gregor     break;
628718292f2SDouglas Gregor 
629a686e1b0SDouglas Gregor   case tok::l_square:
630a686e1b0SDouglas Gregor     Tok.Kind = MMToken::LSquare;
631a686e1b0SDouglas Gregor     break;
632a686e1b0SDouglas Gregor 
6332b82c2a5SDouglas Gregor   case tok::period:
6342b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
6352b82c2a5SDouglas Gregor     break;
6362b82c2a5SDouglas Gregor 
637718292f2SDouglas Gregor   case tok::r_brace:
638718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
639718292f2SDouglas Gregor     break;
640718292f2SDouglas Gregor 
641a686e1b0SDouglas Gregor   case tok::r_square:
642a686e1b0SDouglas Gregor     Tok.Kind = MMToken::RSquare;
643a686e1b0SDouglas Gregor     break;
644a686e1b0SDouglas Gregor 
6452b82c2a5SDouglas Gregor   case tok::star:
6462b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
6472b82c2a5SDouglas Gregor     break;
6482b82c2a5SDouglas Gregor 
649718292f2SDouglas Gregor   case tok::string_literal: {
650d67aea28SRichard Smith     if (LToken.hasUDSuffix()) {
651d67aea28SRichard Smith       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
652d67aea28SRichard Smith       HadError = true;
653d67aea28SRichard Smith       goto retry;
654d67aea28SRichard Smith     }
655d67aea28SRichard Smith 
656718292f2SDouglas Gregor     // Parse the string literal.
657718292f2SDouglas Gregor     LangOptions LangOpts;
658718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
659718292f2SDouglas Gregor     if (StringLiteral.hadError)
660718292f2SDouglas Gregor       goto retry;
661718292f2SDouglas Gregor 
662718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
663718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
664718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
665718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
666718292f2SDouglas Gregor     Saved[Length] = 0;
667718292f2SDouglas Gregor 
668718292f2SDouglas Gregor     // Form the token.
669718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
670718292f2SDouglas Gregor     Tok.StringData = Saved;
671718292f2SDouglas Gregor     Tok.StringLength = Length;
672718292f2SDouglas Gregor     break;
673718292f2SDouglas Gregor   }
674718292f2SDouglas Gregor 
675718292f2SDouglas Gregor   case tok::comment:
676718292f2SDouglas Gregor     goto retry;
677718292f2SDouglas Gregor 
678718292f2SDouglas Gregor   default:
679718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
680718292f2SDouglas Gregor     HadError = true;
681718292f2SDouglas Gregor     goto retry;
682718292f2SDouglas Gregor   }
683718292f2SDouglas Gregor 
684718292f2SDouglas Gregor   return Result;
685718292f2SDouglas Gregor }
686718292f2SDouglas Gregor 
687718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
688718292f2SDouglas Gregor   unsigned braceDepth = 0;
689a686e1b0SDouglas Gregor   unsigned squareDepth = 0;
690718292f2SDouglas Gregor   do {
691718292f2SDouglas Gregor     switch (Tok.Kind) {
692718292f2SDouglas Gregor     case MMToken::EndOfFile:
693718292f2SDouglas Gregor       return;
694718292f2SDouglas Gregor 
695718292f2SDouglas Gregor     case MMToken::LBrace:
696a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
697718292f2SDouglas Gregor         return;
698718292f2SDouglas Gregor 
699718292f2SDouglas Gregor       ++braceDepth;
700718292f2SDouglas Gregor       break;
701718292f2SDouglas Gregor 
702a686e1b0SDouglas Gregor     case MMToken::LSquare:
703a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
704a686e1b0SDouglas Gregor         return;
705a686e1b0SDouglas Gregor 
706a686e1b0SDouglas Gregor       ++squareDepth;
707a686e1b0SDouglas Gregor       break;
708a686e1b0SDouglas Gregor 
709718292f2SDouglas Gregor     case MMToken::RBrace:
710718292f2SDouglas Gregor       if (braceDepth > 0)
711718292f2SDouglas Gregor         --braceDepth;
712718292f2SDouglas Gregor       else if (Tok.is(K))
713718292f2SDouglas Gregor         return;
714718292f2SDouglas Gregor       break;
715718292f2SDouglas Gregor 
716a686e1b0SDouglas Gregor     case MMToken::RSquare:
717a686e1b0SDouglas Gregor       if (squareDepth > 0)
718a686e1b0SDouglas Gregor         --squareDepth;
719a686e1b0SDouglas Gregor       else if (Tok.is(K))
720a686e1b0SDouglas Gregor         return;
721a686e1b0SDouglas Gregor       break;
722a686e1b0SDouglas Gregor 
723718292f2SDouglas Gregor     default:
724a686e1b0SDouglas Gregor       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
725718292f2SDouglas Gregor         return;
726718292f2SDouglas Gregor       break;
727718292f2SDouglas Gregor     }
728718292f2SDouglas Gregor 
729718292f2SDouglas Gregor    consumeToken();
730718292f2SDouglas Gregor   } while (true);
731718292f2SDouglas Gregor }
732718292f2SDouglas Gregor 
733e7ab3669SDouglas Gregor /// \brief Parse a module-id.
734e7ab3669SDouglas Gregor ///
735e7ab3669SDouglas Gregor ///   module-id:
736e7ab3669SDouglas Gregor ///     identifier
737e7ab3669SDouglas Gregor ///     identifier '.' module-id
738e7ab3669SDouglas Gregor ///
739e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise.
740e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) {
741e7ab3669SDouglas Gregor   Id.clear();
742e7ab3669SDouglas Gregor   do {
743e7ab3669SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
744e7ab3669SDouglas Gregor       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
745e7ab3669SDouglas Gregor       consumeToken();
746e7ab3669SDouglas Gregor     } else {
747e7ab3669SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
748e7ab3669SDouglas Gregor       return true;
749e7ab3669SDouglas Gregor     }
750e7ab3669SDouglas Gregor 
751e7ab3669SDouglas Gregor     if (!Tok.is(MMToken::Period))
752e7ab3669SDouglas Gregor       break;
753e7ab3669SDouglas Gregor 
754e7ab3669SDouglas Gregor     consumeToken();
755e7ab3669SDouglas Gregor   } while (true);
756e7ab3669SDouglas Gregor 
757e7ab3669SDouglas Gregor   return false;
758e7ab3669SDouglas Gregor }
759e7ab3669SDouglas Gregor 
760a686e1b0SDouglas Gregor namespace {
761a686e1b0SDouglas Gregor   /// \brief Enumerates the known attributes.
762a686e1b0SDouglas Gregor   enum AttributeKind {
763a686e1b0SDouglas Gregor     /// \brief An unknown attribute.
764a686e1b0SDouglas Gregor     AT_unknown,
765a686e1b0SDouglas Gregor     /// \brief The 'system' attribute.
766a686e1b0SDouglas Gregor     AT_system
767a686e1b0SDouglas Gregor   };
768a686e1b0SDouglas Gregor }
769a686e1b0SDouglas Gregor 
770718292f2SDouglas Gregor /// \brief Parse a module declaration.
771718292f2SDouglas Gregor ///
772718292f2SDouglas Gregor ///   module-declaration:
773a686e1b0SDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
774a686e1b0SDouglas Gregor ///       { module-member* }
775a686e1b0SDouglas Gregor ///
776a686e1b0SDouglas Gregor ///   attributes:
777a686e1b0SDouglas Gregor ///     attribute attributes
778a686e1b0SDouglas Gregor ///     attribute
779a686e1b0SDouglas Gregor ///
780a686e1b0SDouglas Gregor ///   attribute:
781a686e1b0SDouglas Gregor ///     [ identifier ]
782718292f2SDouglas Gregor ///
783718292f2SDouglas Gregor ///   module-member:
7841fb5c3a6SDouglas Gregor ///     requires-declaration
785718292f2SDouglas Gregor ///     header-declaration
786e7ab3669SDouglas Gregor ///     submodule-declaration
7872b82c2a5SDouglas Gregor ///     export-declaration
78873441091SDouglas Gregor ///
78973441091SDouglas Gregor ///   submodule-declaration:
79073441091SDouglas Gregor ///     module-declaration
79173441091SDouglas Gregor ///     inferred-submodule-declaration
792718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
793755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
794755b2055SDouglas Gregor          Tok.is(MMToken::FrameworkKeyword));
795f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
796e7ab3669SDouglas Gregor   SourceLocation ExplicitLoc;
797718292f2SDouglas Gregor   bool Explicit = false;
798f2161a70SDouglas Gregor   bool Framework = false;
799755b2055SDouglas Gregor 
800f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
801f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
802e7ab3669SDouglas Gregor     ExplicitLoc = consumeToken();
803f2161a70SDouglas Gregor     Explicit = true;
804f2161a70SDouglas Gregor   }
805f2161a70SDouglas Gregor 
806f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
807755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
808755b2055SDouglas Gregor     consumeToken();
809755b2055SDouglas Gregor     Framework = true;
810755b2055SDouglas Gregor   }
811718292f2SDouglas Gregor 
812718292f2SDouglas Gregor   // Parse 'module' keyword.
813718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
814d6343c99SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
815718292f2SDouglas Gregor     consumeToken();
816718292f2SDouglas Gregor     HadError = true;
817718292f2SDouglas Gregor     return;
818718292f2SDouglas Gregor   }
819718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
820718292f2SDouglas Gregor 
82173441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
82273441091SDouglas Gregor   // Parse it.
82373441091SDouglas Gregor   if (Tok.is(MMToken::Star))
82473441091SDouglas Gregor     return parseInferredSubmoduleDecl(Explicit);
82573441091SDouglas Gregor 
826718292f2SDouglas Gregor   // Parse the module name.
827e7ab3669SDouglas Gregor   ModuleId Id;
828e7ab3669SDouglas Gregor   if (parseModuleId(Id)) {
829718292f2SDouglas Gregor     HadError = true;
830718292f2SDouglas Gregor     return;
831718292f2SDouglas Gregor   }
832e7ab3669SDouglas Gregor 
833e7ab3669SDouglas Gregor   if (ActiveModule) {
834e7ab3669SDouglas Gregor     if (Id.size() > 1) {
835e7ab3669SDouglas Gregor       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
836e7ab3669SDouglas Gregor         << SourceRange(Id.front().second, Id.back().second);
837e7ab3669SDouglas Gregor 
838e7ab3669SDouglas Gregor       HadError = true;
839e7ab3669SDouglas Gregor       return;
840e7ab3669SDouglas Gregor     }
841e7ab3669SDouglas Gregor   } else if (Id.size() == 1 && Explicit) {
842e7ab3669SDouglas Gregor     // Top-level modules can't be explicit.
843e7ab3669SDouglas Gregor     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
844e7ab3669SDouglas Gregor     Explicit = false;
845e7ab3669SDouglas Gregor     ExplicitLoc = SourceLocation();
846e7ab3669SDouglas Gregor     HadError = true;
847e7ab3669SDouglas Gregor   }
848e7ab3669SDouglas Gregor 
849e7ab3669SDouglas Gregor   Module *PreviousActiveModule = ActiveModule;
850e7ab3669SDouglas Gregor   if (Id.size() > 1) {
851e7ab3669SDouglas Gregor     // This module map defines a submodule. Go find the module of which it
852e7ab3669SDouglas Gregor     // is a submodule.
853e7ab3669SDouglas Gregor     ActiveModule = 0;
854e7ab3669SDouglas Gregor     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
855e7ab3669SDouglas Gregor       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
856e7ab3669SDouglas Gregor         ActiveModule = Next;
857e7ab3669SDouglas Gregor         continue;
858e7ab3669SDouglas Gregor       }
859e7ab3669SDouglas Gregor 
860e7ab3669SDouglas Gregor       if (ActiveModule) {
861e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
862e7ab3669SDouglas Gregor           << Id[I].first << ActiveModule->getTopLevelModule();
863e7ab3669SDouglas Gregor       } else {
864e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
865e7ab3669SDouglas Gregor       }
866e7ab3669SDouglas Gregor       HadError = true;
867e7ab3669SDouglas Gregor       return;
868e7ab3669SDouglas Gregor     }
869e7ab3669SDouglas Gregor   }
870e7ab3669SDouglas Gregor 
871e7ab3669SDouglas Gregor   StringRef ModuleName = Id.back().first;
872e7ab3669SDouglas Gregor   SourceLocation ModuleNameLoc = Id.back().second;
873718292f2SDouglas Gregor 
874a686e1b0SDouglas Gregor   // Parse the optional attribute list.
875a686e1b0SDouglas Gregor   bool IsSystem = false;
876a686e1b0SDouglas Gregor   while (Tok.is(MMToken::LSquare)) {
877a686e1b0SDouglas Gregor     // Consume the '['.
878a686e1b0SDouglas Gregor     SourceLocation LSquareLoc = consumeToken();
879a686e1b0SDouglas Gregor 
880a686e1b0SDouglas Gregor     // Check whether we have an attribute name here.
881a686e1b0SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
882a686e1b0SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
883a686e1b0SDouglas Gregor       skipUntil(MMToken::RSquare);
884a686e1b0SDouglas Gregor       if (Tok.is(MMToken::RSquare))
885a686e1b0SDouglas Gregor         consumeToken();
886a686e1b0SDouglas Gregor       continue;
887a686e1b0SDouglas Gregor     }
888a686e1b0SDouglas Gregor 
889a686e1b0SDouglas Gregor     // Decode the attribute name.
890a686e1b0SDouglas Gregor     AttributeKind Attribute
891a686e1b0SDouglas Gregor       = llvm::StringSwitch<AttributeKind>(Tok.getString())
892a686e1b0SDouglas Gregor         .Case("system", AT_system)
893a686e1b0SDouglas Gregor         .Default(AT_unknown);
894a686e1b0SDouglas Gregor     switch (Attribute) {
895a686e1b0SDouglas Gregor     case AT_unknown:
896a686e1b0SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
897a686e1b0SDouglas Gregor         << Tok.getString();
898a686e1b0SDouglas Gregor       break;
899a686e1b0SDouglas Gregor 
900a686e1b0SDouglas Gregor     case AT_system:
901a686e1b0SDouglas Gregor       IsSystem = true;
902a686e1b0SDouglas Gregor       break;
903a686e1b0SDouglas Gregor     }
904a686e1b0SDouglas Gregor     consumeToken();
905a686e1b0SDouglas Gregor 
906a686e1b0SDouglas Gregor     // Consume the ']'.
907a686e1b0SDouglas Gregor     if (!Tok.is(MMToken::RSquare)) {
908a686e1b0SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
909a686e1b0SDouglas Gregor       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
910a686e1b0SDouglas Gregor       skipUntil(MMToken::RSquare);
911a686e1b0SDouglas Gregor     }
912a686e1b0SDouglas Gregor 
913a686e1b0SDouglas Gregor     if (Tok.is(MMToken::RSquare))
914a686e1b0SDouglas Gregor       consumeToken();
915a686e1b0SDouglas Gregor   }
916a686e1b0SDouglas Gregor 
917718292f2SDouglas Gregor   // Parse the opening brace.
918718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
919718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
920718292f2SDouglas Gregor       << ModuleName;
921718292f2SDouglas Gregor     HadError = true;
922718292f2SDouglas Gregor     return;
923718292f2SDouglas Gregor   }
924718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
925718292f2SDouglas Gregor 
926718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
927eb90e830SDouglas Gregor   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
928fcc54a3bSDouglas Gregor     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
929fcc54a3bSDouglas Gregor       // Skip the module definition.
930fcc54a3bSDouglas Gregor       skipUntil(MMToken::RBrace);
931fcc54a3bSDouglas Gregor       if (Tok.is(MMToken::RBrace))
932fcc54a3bSDouglas Gregor         consumeToken();
933fcc54a3bSDouglas Gregor       else {
934fcc54a3bSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
935fcc54a3bSDouglas Gregor         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
936fcc54a3bSDouglas Gregor         HadError = true;
937fcc54a3bSDouglas Gregor       }
938fcc54a3bSDouglas Gregor       return;
939fcc54a3bSDouglas Gregor     }
940fcc54a3bSDouglas Gregor 
941718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
942718292f2SDouglas Gregor       << ModuleName;
943eb90e830SDouglas Gregor     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
944718292f2SDouglas Gregor 
945718292f2SDouglas Gregor     // Skip the module definition.
946718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
947718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
948718292f2SDouglas Gregor       consumeToken();
949718292f2SDouglas Gregor 
950718292f2SDouglas Gregor     HadError = true;
951718292f2SDouglas Gregor     return;
952718292f2SDouglas Gregor   }
953718292f2SDouglas Gregor 
954718292f2SDouglas Gregor   // Start defining this module.
955eb90e830SDouglas Gregor   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
956eb90e830SDouglas Gregor                                         Explicit).first;
957eb90e830SDouglas Gregor   ActiveModule->DefinitionLoc = ModuleNameLoc;
958a686e1b0SDouglas Gregor   if (IsSystem)
959a686e1b0SDouglas Gregor     ActiveModule->IsSystem = true;
960718292f2SDouglas Gregor 
961718292f2SDouglas Gregor   bool Done = false;
962718292f2SDouglas Gregor   do {
963718292f2SDouglas Gregor     switch (Tok.Kind) {
964718292f2SDouglas Gregor     case MMToken::EndOfFile:
965718292f2SDouglas Gregor     case MMToken::RBrace:
966718292f2SDouglas Gregor       Done = true;
967718292f2SDouglas Gregor       break;
968718292f2SDouglas Gregor 
969718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
970f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
971718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
972718292f2SDouglas Gregor       parseModuleDecl();
973718292f2SDouglas Gregor       break;
974718292f2SDouglas Gregor 
9752b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
9762b82c2a5SDouglas Gregor       parseExportDecl();
9772b82c2a5SDouglas Gregor       break;
9782b82c2a5SDouglas Gregor 
9791fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
9801fb5c3a6SDouglas Gregor       parseRequiresDecl();
9811fb5c3a6SDouglas Gregor       break;
9821fb5c3a6SDouglas Gregor 
983524e33e1SDouglas Gregor     case MMToken::UmbrellaKeyword: {
984524e33e1SDouglas Gregor       SourceLocation UmbrellaLoc = consumeToken();
985524e33e1SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword))
986524e33e1SDouglas Gregor         parseHeaderDecl(UmbrellaLoc);
987524e33e1SDouglas Gregor       else
988524e33e1SDouglas Gregor         parseUmbrellaDirDecl(UmbrellaLoc);
989718292f2SDouglas Gregor       break;
990524e33e1SDouglas Gregor     }
991718292f2SDouglas Gregor 
992322f633cSDouglas Gregor     case MMToken::HeaderKeyword:
993322f633cSDouglas Gregor       parseHeaderDecl(SourceLocation());
994718292f2SDouglas Gregor       break;
995718292f2SDouglas Gregor 
996718292f2SDouglas Gregor     default:
997718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
998718292f2SDouglas Gregor       consumeToken();
999718292f2SDouglas Gregor       break;
1000718292f2SDouglas Gregor     }
1001718292f2SDouglas Gregor   } while (!Done);
1002718292f2SDouglas Gregor 
1003718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
1004718292f2SDouglas Gregor     consumeToken();
1005718292f2SDouglas Gregor   else {
1006718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1007718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1008718292f2SDouglas Gregor     HadError = true;
1009718292f2SDouglas Gregor   }
1010718292f2SDouglas Gregor 
1011e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
1012e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
1013718292f2SDouglas Gregor }
1014718292f2SDouglas Gregor 
10151fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration.
10161fb5c3a6SDouglas Gregor ///
10171fb5c3a6SDouglas Gregor ///   requires-declaration:
10181fb5c3a6SDouglas Gregor ///     'requires' feature-list
10191fb5c3a6SDouglas Gregor ///
10201fb5c3a6SDouglas Gregor ///   feature-list:
10211fb5c3a6SDouglas Gregor ///     identifier ',' feature-list
10221fb5c3a6SDouglas Gregor ///     identifier
10231fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() {
10241fb5c3a6SDouglas Gregor   assert(Tok.is(MMToken::RequiresKeyword));
10251fb5c3a6SDouglas Gregor 
10261fb5c3a6SDouglas Gregor   // Parse 'requires' keyword.
10271fb5c3a6SDouglas Gregor   consumeToken();
10281fb5c3a6SDouglas Gregor 
10291fb5c3a6SDouglas Gregor   // Parse the feature-list.
10301fb5c3a6SDouglas Gregor   do {
10311fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
10321fb5c3a6SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
10331fb5c3a6SDouglas Gregor       HadError = true;
10341fb5c3a6SDouglas Gregor       return;
10351fb5c3a6SDouglas Gregor     }
10361fb5c3a6SDouglas Gregor 
10371fb5c3a6SDouglas Gregor     // Consume the feature name.
10381fb5c3a6SDouglas Gregor     std::string Feature = Tok.getString();
10391fb5c3a6SDouglas Gregor     consumeToken();
10401fb5c3a6SDouglas Gregor 
10411fb5c3a6SDouglas Gregor     // Add this feature.
104289929282SDouglas Gregor     ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target);
10431fb5c3a6SDouglas Gregor 
10441fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Comma))
10451fb5c3a6SDouglas Gregor       break;
10461fb5c3a6SDouglas Gregor 
10471fb5c3a6SDouglas Gregor     // Consume the comma.
10481fb5c3a6SDouglas Gregor     consumeToken();
10491fb5c3a6SDouglas Gregor   } while (true);
10501fb5c3a6SDouglas Gregor }
10511fb5c3a6SDouglas Gregor 
1052f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
1053f2161a70SDouglas Gregor /// subframework in which the given module lives.
1054bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod,
1055bf8da9d7SBenjamin Kramer                                     llvm::SmallVectorImpl<char> &Path) {
1056f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
1057f2161a70SDouglas Gregor   llvm::SmallVector<StringRef, 2> Paths;
1058f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
1059f2161a70SDouglas Gregor     if (Mod->IsFramework)
1060f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
1061f2161a70SDouglas Gregor   }
1062f2161a70SDouglas Gregor 
1063f2161a70SDouglas Gregor   if (Paths.empty())
1064f2161a70SDouglas Gregor     return;
1065f2161a70SDouglas Gregor 
1066f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
1067f2161a70SDouglas Gregor   for (unsigned I = Paths.size() - 1; I != 0; --I) {
1068f2161a70SDouglas Gregor     llvm::sys::path::append(Path, "Frameworks");
1069f2161a70SDouglas Gregor     llvm::sys::path::append(Path, Paths[I-1] + ".framework");
1070f2161a70SDouglas Gregor   }
1071f2161a70SDouglas Gregor }
1072f2161a70SDouglas Gregor 
10733ec6663bSDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin
10743ec6663bSDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system
10753ec6663bSDouglas Gregor /// headers.
10763ec6663bSDouglas Gregor static bool isBuiltinHeader(StringRef FileName) {
10773ec6663bSDouglas Gregor   return llvm::StringSwitch<bool>(FileName)
10783ec6663bSDouglas Gregor       .Case("float.h", true)
10793ec6663bSDouglas Gregor       .Case("iso646.h", true)
10803ec6663bSDouglas Gregor       .Case("limits.h", true)
10813ec6663bSDouglas Gregor       .Case("stdalign.h", true)
10823ec6663bSDouglas Gregor       .Case("stdarg.h", true)
10833ec6663bSDouglas Gregor       .Case("stdbool.h", true)
10843ec6663bSDouglas Gregor       .Case("stddef.h", true)
10853ec6663bSDouglas Gregor       .Case("stdint.h", true)
10863ec6663bSDouglas Gregor       .Case("tgmath.h", true)
10873ec6663bSDouglas Gregor       .Case("unwind.h", true)
10883ec6663bSDouglas Gregor       .Default(false);
10893ec6663bSDouglas Gregor }
10903ec6663bSDouglas Gregor 
1091718292f2SDouglas Gregor /// \brief Parse a header declaration.
1092718292f2SDouglas Gregor ///
1093718292f2SDouglas Gregor ///   header-declaration:
1094322f633cSDouglas Gregor ///     'umbrella'[opt] 'header' string-literal
1095322f633cSDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) {
1096718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
10971871ed3dSBenjamin Kramer   consumeToken();
1098718292f2SDouglas Gregor 
1099322f633cSDouglas Gregor   bool Umbrella = UmbrellaLoc.isValid();
1100322f633cSDouglas Gregor 
1101718292f2SDouglas Gregor   // Parse the header name.
1102718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1103718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1104718292f2SDouglas Gregor       << "header";
1105718292f2SDouglas Gregor     HadError = true;
1106718292f2SDouglas Gregor     return;
1107718292f2SDouglas Gregor   }
1108e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
1109718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
1110718292f2SDouglas Gregor 
1111524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1112524e33e1SDouglas Gregor   if (Umbrella && ActiveModule->Umbrella) {
1113524e33e1SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
1114524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1115322f633cSDouglas Gregor     HadError = true;
1116322f633cSDouglas Gregor     return;
1117322f633cSDouglas Gregor   }
1118322f633cSDouglas Gregor 
11195257fc63SDouglas Gregor   // Look for this file.
1120e7ab3669SDouglas Gregor   const FileEntry *File = 0;
11213ec6663bSDouglas Gregor   const FileEntry *BuiltinFile = 0;
11222c1dd271SDylan Noblesmith   SmallString<128> PathName;
1123e7ab3669SDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
1124e7ab3669SDouglas Gregor     PathName = FileName;
1125e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
11267033127bSDouglas Gregor   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
11277033127bSDouglas Gregor     PathName = Dir->getName();
11287033127bSDouglas Gregor     llvm::sys::path::append(PathName, FileName);
11297033127bSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
1130e7ab3669SDouglas Gregor   } else {
1131e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
11327033127bSDouglas Gregor     PathName = Directory->getName();
1133e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
1134755b2055SDouglas Gregor 
1135f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
1136f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
1137755b2055SDouglas Gregor 
1138e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
1139e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
11405257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1141e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
1142e7ab3669SDouglas Gregor 
1143e7ab3669SDouglas Gregor       if (!File) {
1144e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
1145e7ab3669SDouglas Gregor         PathName.resize(PathLength);
1146e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
1147e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
1148e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
1149e7ab3669SDouglas Gregor       }
1150e7ab3669SDouglas Gregor     } else {
1151e7ab3669SDouglas Gregor       // Lookup for normal headers.
1152e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1153e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
11543ec6663bSDouglas Gregor 
11553ec6663bSDouglas Gregor       // If this is a system module with a top-level header, this header
11563ec6663bSDouglas Gregor       // may have a counterpart (or replacement) in the set of headers
11573ec6663bSDouglas Gregor       // supplied by Clang. Find that builtin header.
11583ec6663bSDouglas Gregor       if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir &&
11593ec6663bSDouglas Gregor           BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) {
11602c1dd271SDylan Noblesmith         SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
11613ec6663bSDouglas Gregor         llvm::sys::path::append(BuiltinPathName, FileName);
11623ec6663bSDouglas Gregor         BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
11633ec6663bSDouglas Gregor 
11643ec6663bSDouglas Gregor         // If Clang supplies this header but the underlying system does not,
11653ec6663bSDouglas Gregor         // just silently swap in our builtin version. Otherwise, we'll end
11663ec6663bSDouglas Gregor         // up adding both (later).
11673ec6663bSDouglas Gregor         if (!File && BuiltinFile) {
11683ec6663bSDouglas Gregor           File = BuiltinFile;
11693ec6663bSDouglas Gregor           BuiltinFile = 0;
11703ec6663bSDouglas Gregor         }
11713ec6663bSDouglas Gregor       }
1172e7ab3669SDouglas Gregor     }
1173e7ab3669SDouglas Gregor   }
11745257fc63SDouglas Gregor 
11755257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
11765257fc63SDouglas Gregor   // Come up with a lazy way to do this.
1177e7ab3669SDouglas Gregor   if (File) {
11785257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
11795257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
11805257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
11815257fc63SDouglas Gregor       HadError = true;
1182322f633cSDouglas Gregor     } else if (Umbrella) {
1183322f633cSDouglas Gregor       const DirectoryEntry *UmbrellaDir = File->getDir();
1184322f633cSDouglas Gregor       if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
1185322f633cSDouglas Gregor         Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1186322f633cSDouglas Gregor           << OwningModule->getFullModuleName();
1187322f633cSDouglas Gregor         HadError = true;
11885257fc63SDouglas Gregor       } else {
1189322f633cSDouglas Gregor         // Record this umbrella header.
1190322f633cSDouglas Gregor         Map.setUmbrellaHeader(ActiveModule, File);
1191322f633cSDouglas Gregor       }
1192322f633cSDouglas Gregor     } else {
1193322f633cSDouglas Gregor       // Record this header.
1194a89c5ac4SDouglas Gregor       Map.addHeader(ActiveModule, File);
11953ec6663bSDouglas Gregor 
11963ec6663bSDouglas Gregor       // If there is a builtin counterpart to this file, add it now.
11973ec6663bSDouglas Gregor       if (BuiltinFile)
11983ec6663bSDouglas Gregor         Map.addHeader(ActiveModule, BuiltinFile);
11995257fc63SDouglas Gregor     }
12005257fc63SDouglas Gregor   } else {
12015257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
1202524e33e1SDouglas Gregor       << Umbrella << FileName;
12035257fc63SDouglas Gregor     HadError = true;
12045257fc63SDouglas Gregor   }
1205718292f2SDouglas Gregor }
1206718292f2SDouglas Gregor 
1207524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration.
1208524e33e1SDouglas Gregor ///
1209524e33e1SDouglas Gregor ///   umbrella-dir-declaration:
1210524e33e1SDouglas Gregor ///     umbrella string-literal
1211524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1212524e33e1SDouglas Gregor   // Parse the directory name.
1213524e33e1SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1214524e33e1SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1215524e33e1SDouglas Gregor       << "umbrella";
1216524e33e1SDouglas Gregor     HadError = true;
1217524e33e1SDouglas Gregor     return;
1218524e33e1SDouglas Gregor   }
1219524e33e1SDouglas Gregor 
1220524e33e1SDouglas Gregor   std::string DirName = Tok.getString();
1221524e33e1SDouglas Gregor   SourceLocation DirNameLoc = consumeToken();
1222524e33e1SDouglas Gregor 
1223524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1224524e33e1SDouglas Gregor   if (ActiveModule->Umbrella) {
1225524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1226524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1227524e33e1SDouglas Gregor     HadError = true;
1228524e33e1SDouglas Gregor     return;
1229524e33e1SDouglas Gregor   }
1230524e33e1SDouglas Gregor 
1231524e33e1SDouglas Gregor   // Look for this file.
1232524e33e1SDouglas Gregor   const DirectoryEntry *Dir = 0;
1233524e33e1SDouglas Gregor   if (llvm::sys::path::is_absolute(DirName))
1234524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1235524e33e1SDouglas Gregor   else {
12362c1dd271SDylan Noblesmith     SmallString<128> PathName;
1237524e33e1SDouglas Gregor     PathName = Directory->getName();
1238524e33e1SDouglas Gregor     llvm::sys::path::append(PathName, DirName);
1239524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1240524e33e1SDouglas Gregor   }
1241524e33e1SDouglas Gregor 
1242524e33e1SDouglas Gregor   if (!Dir) {
1243524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1244524e33e1SDouglas Gregor       << DirName;
1245524e33e1SDouglas Gregor     HadError = true;
1246524e33e1SDouglas Gregor     return;
1247524e33e1SDouglas Gregor   }
1248524e33e1SDouglas Gregor 
1249524e33e1SDouglas Gregor   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1250524e33e1SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1251524e33e1SDouglas Gregor       << OwningModule->getFullModuleName();
1252524e33e1SDouglas Gregor     HadError = true;
1253524e33e1SDouglas Gregor     return;
1254524e33e1SDouglas Gregor   }
1255524e33e1SDouglas Gregor 
1256524e33e1SDouglas Gregor   // Record this umbrella directory.
1257524e33e1SDouglas Gregor   Map.setUmbrellaDir(ActiveModule, Dir);
1258524e33e1SDouglas Gregor }
1259524e33e1SDouglas Gregor 
12602b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
12612b82c2a5SDouglas Gregor ///
12622b82c2a5SDouglas Gregor ///   export-declaration:
12632b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
12642b82c2a5SDouglas Gregor ///
12652b82c2a5SDouglas Gregor ///   wildcard-module-id:
12662b82c2a5SDouglas Gregor ///     identifier
12672b82c2a5SDouglas Gregor ///     '*'
12682b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
12692b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
12702b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
12712b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
12722b82c2a5SDouglas Gregor 
12732b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
12742b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
12752b82c2a5SDouglas Gregor   bool Wildcard = false;
12762b82c2a5SDouglas Gregor   do {
12772b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
12782b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
12792b82c2a5SDouglas Gregor                                               Tok.getLocation()));
12802b82c2a5SDouglas Gregor       consumeToken();
12812b82c2a5SDouglas Gregor 
12822b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
12832b82c2a5SDouglas Gregor         consumeToken();
12842b82c2a5SDouglas Gregor         continue;
12852b82c2a5SDouglas Gregor       }
12862b82c2a5SDouglas Gregor 
12872b82c2a5SDouglas Gregor       break;
12882b82c2a5SDouglas Gregor     }
12892b82c2a5SDouglas Gregor 
12902b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
12912b82c2a5SDouglas Gregor       Wildcard = true;
1292f5eedd05SDouglas Gregor       consumeToken();
12932b82c2a5SDouglas Gregor       break;
12942b82c2a5SDouglas Gregor     }
12952b82c2a5SDouglas Gregor 
12962b82c2a5SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
12972b82c2a5SDouglas Gregor     HadError = true;
12982b82c2a5SDouglas Gregor     return;
12992b82c2a5SDouglas Gregor   } while (true);
13002b82c2a5SDouglas Gregor 
13012b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
13022b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
13032b82c2a5SDouglas Gregor   };
13042b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
13052b82c2a5SDouglas Gregor }
13062b82c2a5SDouglas Gregor 
130773441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
130873441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
130973441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
131073441091SDouglas Gregor   bool Failed = false;
131173441091SDouglas Gregor 
131273441091SDouglas Gregor   // Inferred modules must be submodules.
131373441091SDouglas Gregor   if (!ActiveModule) {
131473441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
131573441091SDouglas Gregor     Failed = true;
131673441091SDouglas Gregor   }
131773441091SDouglas Gregor 
1318524e33e1SDouglas Gregor   // Inferred modules must have umbrella directories.
1319524e33e1SDouglas Gregor   if (!Failed && !ActiveModule->getUmbrellaDir()) {
132073441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
132173441091SDouglas Gregor     Failed = true;
132273441091SDouglas Gregor   }
132373441091SDouglas Gregor 
132473441091SDouglas Gregor   // Check for redefinition of an inferred module.
1325dd005f69SDouglas Gregor   if (!Failed && ActiveModule->InferSubmodules) {
132673441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1327dd005f69SDouglas Gregor     if (ActiveModule->InferredSubmoduleLoc.isValid())
1328dd005f69SDouglas Gregor       Diags.Report(ActiveModule->InferredSubmoduleLoc,
132973441091SDouglas Gregor                    diag::note_mmap_prev_definition);
133073441091SDouglas Gregor     Failed = true;
133173441091SDouglas Gregor   }
133273441091SDouglas Gregor 
133373441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
133473441091SDouglas Gregor   if (Failed) {
133573441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
133673441091SDouglas Gregor       consumeToken();
133773441091SDouglas Gregor       skipUntil(MMToken::RBrace);
133873441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
133973441091SDouglas Gregor         consumeToken();
134073441091SDouglas Gregor     }
134173441091SDouglas Gregor     HadError = true;
134273441091SDouglas Gregor     return;
134373441091SDouglas Gregor   }
134473441091SDouglas Gregor 
134573441091SDouglas Gregor   // Note that we have an inferred submodule.
1346dd005f69SDouglas Gregor   ActiveModule->InferSubmodules = true;
1347dd005f69SDouglas Gregor   ActiveModule->InferredSubmoduleLoc = StarLoc;
1348dd005f69SDouglas Gregor   ActiveModule->InferExplicitSubmodules = Explicit;
134973441091SDouglas Gregor 
135073441091SDouglas Gregor   // Parse the opening brace.
135173441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
135273441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
135373441091SDouglas Gregor     HadError = true;
135473441091SDouglas Gregor     return;
135573441091SDouglas Gregor   }
135673441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
135773441091SDouglas Gregor 
135873441091SDouglas Gregor   // Parse the body of the inferred submodule.
135973441091SDouglas Gregor   bool Done = false;
136073441091SDouglas Gregor   do {
136173441091SDouglas Gregor     switch (Tok.Kind) {
136273441091SDouglas Gregor     case MMToken::EndOfFile:
136373441091SDouglas Gregor     case MMToken::RBrace:
136473441091SDouglas Gregor       Done = true;
136573441091SDouglas Gregor       break;
136673441091SDouglas Gregor 
136773441091SDouglas Gregor     case MMToken::ExportKeyword: {
136873441091SDouglas Gregor       consumeToken();
136973441091SDouglas Gregor       if (Tok.is(MMToken::Star))
1370dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
137173441091SDouglas Gregor       else
137273441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
137373441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
137473441091SDouglas Gregor       consumeToken();
137573441091SDouglas Gregor       break;
137673441091SDouglas Gregor     }
137773441091SDouglas Gregor 
137873441091SDouglas Gregor     case MMToken::ExplicitKeyword:
137973441091SDouglas Gregor     case MMToken::ModuleKeyword:
138073441091SDouglas Gregor     case MMToken::HeaderKeyword:
138173441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
138273441091SDouglas Gregor     default:
138373441091SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
138473441091SDouglas Gregor       consumeToken();
138573441091SDouglas Gregor       break;
138673441091SDouglas Gregor     }
138773441091SDouglas Gregor   } while (!Done);
138873441091SDouglas Gregor 
138973441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
139073441091SDouglas Gregor     consumeToken();
139173441091SDouglas Gregor   else {
139273441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
139373441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
139473441091SDouglas Gregor     HadError = true;
139573441091SDouglas Gregor   }
139673441091SDouglas Gregor }
139773441091SDouglas Gregor 
13987033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence
13997033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
14007033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
14017033127bSDouglas Gregor   for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
14027033127bSDouglas Gregor     // If we have an umbrella directory, use that.
14037033127bSDouglas Gregor     if (Mod->hasUmbrellaDir())
14047033127bSDouglas Gregor       return Mod->getUmbrellaDir();
14057033127bSDouglas Gregor 
14067033127bSDouglas Gregor     // If we have a framework directory, stop looking.
14077033127bSDouglas Gregor     if (Mod->IsFramework)
14087033127bSDouglas Gregor       return 0;
14097033127bSDouglas Gregor   }
14107033127bSDouglas Gregor 
14117033127bSDouglas Gregor   return 0;
14127033127bSDouglas Gregor }
14137033127bSDouglas Gregor 
1414718292f2SDouglas Gregor /// \brief Parse a module map file.
1415718292f2SDouglas Gregor ///
1416718292f2SDouglas Gregor ///   module-map-file:
1417718292f2SDouglas Gregor ///     module-declaration*
1418718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
1419718292f2SDouglas Gregor   do {
1420718292f2SDouglas Gregor     switch (Tok.Kind) {
1421718292f2SDouglas Gregor     case MMToken::EndOfFile:
1422718292f2SDouglas Gregor       return HadError;
1423718292f2SDouglas Gregor 
1424e7ab3669SDouglas Gregor     case MMToken::ExplicitKeyword:
1425718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1426755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
1427718292f2SDouglas Gregor       parseModuleDecl();
1428718292f2SDouglas Gregor       break;
1429718292f2SDouglas Gregor 
14301fb5c3a6SDouglas Gregor     case MMToken::Comma:
14312b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
1432718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
1433718292f2SDouglas Gregor     case MMToken::Identifier:
1434718292f2SDouglas Gregor     case MMToken::LBrace:
1435a686e1b0SDouglas Gregor     case MMToken::LSquare:
14362b82c2a5SDouglas Gregor     case MMToken::Period:
1437718292f2SDouglas Gregor     case MMToken::RBrace:
1438a686e1b0SDouglas Gregor     case MMToken::RSquare:
14391fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
14402b82c2a5SDouglas Gregor     case MMToken::Star:
1441718292f2SDouglas Gregor     case MMToken::StringLiteral:
1442718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
1443718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1444718292f2SDouglas Gregor       HadError = true;
1445718292f2SDouglas Gregor       consumeToken();
1446718292f2SDouglas Gregor       break;
1447718292f2SDouglas Gregor     }
1448718292f2SDouglas Gregor   } while (true);
1449718292f2SDouglas Gregor }
1450718292f2SDouglas Gregor 
1451718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
145289929282SDouglas Gregor   assert(Target != 0 && "Missing target information");
1453718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1454718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1455718292f2SDouglas Gregor   if (!Buffer)
1456718292f2SDouglas Gregor     return true;
1457718292f2SDouglas Gregor 
1458718292f2SDouglas Gregor   // Parse this module map file.
14591fb5c3a6SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
14601fb5c3a6SDouglas Gregor   Diags->getClient()->BeginSourceFile(MMapLangOpts);
14613ec6663bSDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir(),
14623ec6663bSDouglas Gregor                          BuiltinIncludeDir);
1463718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
1464718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
1465718292f2SDouglas Gregor 
1466718292f2SDouglas Gregor   return Result;
1467718292f2SDouglas Gregor }
1468