1718292f2SDouglas Gregor //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===//
2718292f2SDouglas Gregor //
3718292f2SDouglas Gregor //                     The LLVM Compiler Infrastructure
4718292f2SDouglas Gregor //
5718292f2SDouglas Gregor // This file is distributed under the University of Illinois Open Source
6718292f2SDouglas Gregor // License. See LICENSE.TXT for details.
7718292f2SDouglas Gregor //
8718292f2SDouglas Gregor //===----------------------------------------------------------------------===//
9718292f2SDouglas Gregor //
10718292f2SDouglas Gregor // This file defines the ModuleMap implementation, which describes the layout
11718292f2SDouglas Gregor // of a module as it relates to headers.
12718292f2SDouglas Gregor //
13718292f2SDouglas Gregor //===----------------------------------------------------------------------===//
14718292f2SDouglas Gregor #include "clang/Lex/ModuleMap.h"
15718292f2SDouglas Gregor #include "clang/Lex/Lexer.h"
16718292f2SDouglas Gregor #include "clang/Lex/LiteralSupport.h"
17718292f2SDouglas Gregor #include "clang/Lex/LexDiagnostic.h"
18718292f2SDouglas Gregor #include "clang/Basic/Diagnostic.h"
19718292f2SDouglas Gregor #include "clang/Basic/FileManager.h"
20718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h"
21718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h"
22718292f2SDouglas Gregor #include "llvm/Support/Allocator.h"
23e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h"
24718292f2SDouglas Gregor #include "llvm/Support/Host.h"
255257fc63SDouglas Gregor #include "llvm/Support/PathV2.h"
26718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h"
27718292f2SDouglas Gregor #include "llvm/ADT/StringRef.h"
28718292f2SDouglas Gregor #include "llvm/ADT/StringSwitch.h"
29718292f2SDouglas Gregor using namespace clang;
30718292f2SDouglas Gregor 
312b82c2a5SDouglas Gregor Module::ExportDecl
322b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod,
332b82c2a5SDouglas Gregor                          const Module::UnresolvedExportDecl &Unresolved,
342b82c2a5SDouglas Gregor                          bool Complain) {
35f5eedd05SDouglas Gregor   // We may have just a wildcard.
36f5eedd05SDouglas Gregor   if (Unresolved.Id.empty()) {
37f5eedd05SDouglas Gregor     assert(Unresolved.Wildcard && "Invalid unresolved export");
38f5eedd05SDouglas Gregor     return Module::ExportDecl(0, true);
39f5eedd05SDouglas Gregor   }
40f5eedd05SDouglas Gregor 
412b82c2a5SDouglas Gregor   // Find the starting module.
422b82c2a5SDouglas Gregor   Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
432b82c2a5SDouglas Gregor   if (!Context) {
442b82c2a5SDouglas Gregor     if (Complain)
452b82c2a5SDouglas Gregor       Diags->Report(Unresolved.Id[0].second,
462b82c2a5SDouglas Gregor                     diag::err_mmap_missing_module_unqualified)
472b82c2a5SDouglas Gregor         << Unresolved.Id[0].first << Mod->getFullModuleName();
482b82c2a5SDouglas Gregor 
492b82c2a5SDouglas Gregor     return Module::ExportDecl();
502b82c2a5SDouglas Gregor   }
512b82c2a5SDouglas Gregor 
522b82c2a5SDouglas Gregor   // Dig into the module path.
532b82c2a5SDouglas Gregor   for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
542b82c2a5SDouglas Gregor     Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
552b82c2a5SDouglas Gregor                                         Context);
562b82c2a5SDouglas Gregor     if (!Sub) {
572b82c2a5SDouglas Gregor       if (Complain)
582b82c2a5SDouglas Gregor         Diags->Report(Unresolved.Id[I].second,
592b82c2a5SDouglas Gregor                       diag::err_mmap_missing_module_qualified)
602b82c2a5SDouglas Gregor           << Unresolved.Id[I].first << Context->getFullModuleName()
612b82c2a5SDouglas Gregor           << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
622b82c2a5SDouglas Gregor 
632b82c2a5SDouglas Gregor       return Module::ExportDecl();
642b82c2a5SDouglas Gregor     }
652b82c2a5SDouglas Gregor 
662b82c2a5SDouglas Gregor     Context = Sub;
672b82c2a5SDouglas Gregor   }
682b82c2a5SDouglas Gregor 
692b82c2a5SDouglas Gregor   return Module::ExportDecl(Context, Unresolved.Wildcard);
702b82c2a5SDouglas Gregor }
712b82c2a5SDouglas Gregor 
721fb5c3a6SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC,
73*89929282SDouglas Gregor                      const LangOptions &LangOpts, const TargetInfo *Target)
74*89929282SDouglas Gregor   : LangOpts(LangOpts), Target(Target)
751fb5c3a6SDouglas Gregor {
76718292f2SDouglas Gregor   llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
77718292f2SDouglas Gregor   Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>(
78718292f2SDouglas Gregor             new DiagnosticsEngine(DiagIDs));
79718292f2SDouglas Gregor   Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
80718292f2SDouglas Gregor   SourceMgr = new SourceManager(*Diags, FileMgr);
81718292f2SDouglas Gregor }
82718292f2SDouglas Gregor 
83718292f2SDouglas Gregor ModuleMap::~ModuleMap() {
845acdf59eSDouglas Gregor   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
855acdf59eSDouglas Gregor                                         IEnd = Modules.end();
865acdf59eSDouglas Gregor        I != IEnd; ++I) {
875acdf59eSDouglas Gregor     delete I->getValue();
885acdf59eSDouglas Gregor   }
895acdf59eSDouglas Gregor 
90718292f2SDouglas Gregor   delete SourceMgr;
91718292f2SDouglas Gregor }
92718292f2SDouglas Gregor 
93*89929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) {
94*89929282SDouglas Gregor   assert((!this->Target || this->Target == &Target) &&
95*89929282SDouglas Gregor          "Improper target override");
96*89929282SDouglas Gregor   this->Target = &Target;
97*89929282SDouglas Gregor }
98*89929282SDouglas Gregor 
99de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
100ab0c8a84SDouglas Gregor   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
101ab0c8a84SDouglas Gregor     = Headers.find(File);
1021fb5c3a6SDouglas Gregor   if (Known != Headers.end()) {
1031fb5c3a6SDouglas Gregor     // If a header corresponds to an unavailable module, don't report
1041fb5c3a6SDouglas Gregor     // that it maps to anything.
1051fb5c3a6SDouglas Gregor     if (!Known->second->isAvailable())
1061fb5c3a6SDouglas Gregor       return 0;
1071fb5c3a6SDouglas Gregor 
108ab0c8a84SDouglas Gregor     return Known->second;
1091fb5c3a6SDouglas Gregor   }
110ab0c8a84SDouglas Gregor 
111b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
112b65dbfffSDouglas Gregor   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
113b65dbfffSDouglas Gregor   StringRef DirName = Dir->getName();
114a89c5ac4SDouglas Gregor 
115a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
116a89c5ac4SDouglas Gregor   // an umbrella header.
117b65dbfffSDouglas Gregor   do {
118a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
119a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
120a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
121a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
122930a85ccSDouglas Gregor 
123930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
12473141fa9SDouglas Gregor       // directory.
125930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
12673141fa9SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
127930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
128930a85ccSDouglas Gregor 
129930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
130a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
131a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
132a89c5ac4SDouglas Gregor         // the actual header is located.
1339458f82dSDouglas Gregor         bool Explicit = UmbrellaModule->InferExplicitSubmodules;
1349458f82dSDouglas Gregor 
1357033127bSDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
136a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
137a89c5ac4SDouglas Gregor           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
138a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
1399458f82dSDouglas Gregor                                       Explicit).first;
140a89c5ac4SDouglas Gregor 
141a89c5ac4SDouglas Gregor           // Associate the module and the directory.
142a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
143a89c5ac4SDouglas Gregor 
144a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
145a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
146930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
147a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
148a89c5ac4SDouglas Gregor         }
149a89c5ac4SDouglas Gregor 
150a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
151a89c5ac4SDouglas Gregor         StringRef Name = llvm::sys::path::stem(File->getName());
152a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
1539458f82dSDouglas Gregor                                     Explicit).first;
154a89c5ac4SDouglas Gregor 
155a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
156a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
157930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
158a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
159a89c5ac4SDouglas Gregor       } else {
160a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
161a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
162a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
163a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
164a89c5ac4SDouglas Gregor       }
165a89c5ac4SDouglas Gregor 
166a89c5ac4SDouglas Gregor       Headers[File] = Result;
1671fb5c3a6SDouglas Gregor 
1681fb5c3a6SDouglas Gregor       // If a header corresponds to an unavailable module, don't report
1691fb5c3a6SDouglas Gregor       // that it maps to anything.
1701fb5c3a6SDouglas Gregor       if (!Result->isAvailable())
1711fb5c3a6SDouglas Gregor         return 0;
1721fb5c3a6SDouglas Gregor 
173a89c5ac4SDouglas Gregor       return Result;
174a89c5ac4SDouglas Gregor     }
175a89c5ac4SDouglas Gregor 
176a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
177a89c5ac4SDouglas Gregor 
178b65dbfffSDouglas Gregor     // Retrieve our parent path.
179b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
180b65dbfffSDouglas Gregor     if (DirName.empty())
181b65dbfffSDouglas Gregor       break;
182b65dbfffSDouglas Gregor 
183b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
184b65dbfffSDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
185a89c5ac4SDouglas Gregor   } while (Dir);
186b65dbfffSDouglas Gregor 
187ab0c8a84SDouglas Gregor   return 0;
188ab0c8a84SDouglas Gregor }
189ab0c8a84SDouglas Gregor 
1901fb5c3a6SDouglas Gregor bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) {
1911fb5c3a6SDouglas Gregor   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
1921fb5c3a6SDouglas Gregor     = Headers.find(Header);
1931fb5c3a6SDouglas Gregor   if (Known != Headers.end())
1941fb5c3a6SDouglas Gregor     return !Known->second->isAvailable();
1951fb5c3a6SDouglas Gregor 
1961fb5c3a6SDouglas Gregor   const DirectoryEntry *Dir = Header->getDir();
1971fb5c3a6SDouglas Gregor   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
1981fb5c3a6SDouglas Gregor   StringRef DirName = Dir->getName();
1991fb5c3a6SDouglas Gregor 
2001fb5c3a6SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
2011fb5c3a6SDouglas Gregor   // an umbrella header.
2021fb5c3a6SDouglas Gregor   do {
2031fb5c3a6SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
2041fb5c3a6SDouglas Gregor       = UmbrellaDirs.find(Dir);
2051fb5c3a6SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
2061fb5c3a6SDouglas Gregor       Module *Found = KnownDir->second;
2071fb5c3a6SDouglas Gregor       if (!Found->isAvailable())
2081fb5c3a6SDouglas Gregor         return true;
2091fb5c3a6SDouglas Gregor 
2101fb5c3a6SDouglas Gregor       // Search up the module stack until we find a module with an umbrella
2111fb5c3a6SDouglas Gregor       // directory.
2121fb5c3a6SDouglas Gregor       Module *UmbrellaModule = Found;
2131fb5c3a6SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
2141fb5c3a6SDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
2151fb5c3a6SDouglas Gregor 
2161fb5c3a6SDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
2171fb5c3a6SDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
2181fb5c3a6SDouglas Gregor           // Find or create the module that corresponds to this directory name.
2191fb5c3a6SDouglas Gregor           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
2201fb5c3a6SDouglas Gregor           Found = lookupModuleQualified(Name, Found);
2211fb5c3a6SDouglas Gregor           if (!Found)
2221fb5c3a6SDouglas Gregor             return false;
2231fb5c3a6SDouglas Gregor           if (!Found->isAvailable())
2241fb5c3a6SDouglas Gregor             return true;
2251fb5c3a6SDouglas Gregor         }
2261fb5c3a6SDouglas Gregor 
2271fb5c3a6SDouglas Gregor         // Infer a submodule with the same name as this header file.
2281fb5c3a6SDouglas Gregor         StringRef Name = llvm::sys::path::stem(Header->getName());
2291fb5c3a6SDouglas Gregor         Found = lookupModuleQualified(Name, Found);
2301fb5c3a6SDouglas Gregor         if (!Found)
2311fb5c3a6SDouglas Gregor           return false;
2321fb5c3a6SDouglas Gregor       }
2331fb5c3a6SDouglas Gregor 
2341fb5c3a6SDouglas Gregor       return !Found->isAvailable();
2351fb5c3a6SDouglas Gregor     }
2361fb5c3a6SDouglas Gregor 
2371fb5c3a6SDouglas Gregor     SkippedDirs.push_back(Dir);
2381fb5c3a6SDouglas Gregor 
2391fb5c3a6SDouglas Gregor     // Retrieve our parent path.
2401fb5c3a6SDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
2411fb5c3a6SDouglas Gregor     if (DirName.empty())
2421fb5c3a6SDouglas Gregor       break;
2431fb5c3a6SDouglas Gregor 
2441fb5c3a6SDouglas Gregor     // Resolve the parent path to a directory entry.
2451fb5c3a6SDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
2461fb5c3a6SDouglas Gregor   } while (Dir);
2471fb5c3a6SDouglas Gregor 
2481fb5c3a6SDouglas Gregor   return false;
2491fb5c3a6SDouglas Gregor }
2501fb5c3a6SDouglas Gregor 
251de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) {
25288bdfb0eSDouglas Gregor   llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
25388bdfb0eSDouglas Gregor   if (Known != Modules.end())
25488bdfb0eSDouglas Gregor     return Known->getValue();
25588bdfb0eSDouglas Gregor 
25688bdfb0eSDouglas Gregor   return 0;
25788bdfb0eSDouglas Gregor }
25888bdfb0eSDouglas Gregor 
2592b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
2602b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
2612b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
2622b82c2a5SDouglas Gregor       return Sub;
2632b82c2a5SDouglas Gregor   }
2642b82c2a5SDouglas Gregor 
2652b82c2a5SDouglas Gregor   return findModule(Name);
2662b82c2a5SDouglas Gregor }
2672b82c2a5SDouglas Gregor 
2682b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
2692b82c2a5SDouglas Gregor   if (!Context)
2702b82c2a5SDouglas Gregor     return findModule(Name);
2712b82c2a5SDouglas Gregor 
272eb90e830SDouglas Gregor   return Context->findSubmodule(Name);
2732b82c2a5SDouglas Gregor }
2742b82c2a5SDouglas Gregor 
275de3ef502SDouglas Gregor std::pair<Module *, bool>
27669021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
27769021974SDouglas Gregor                               bool IsExplicit) {
27869021974SDouglas Gregor   // Try to find an existing module with this name.
279eb90e830SDouglas Gregor   if (Module *Sub = lookupModuleQualified(Name, Parent))
280eb90e830SDouglas Gregor     return std::make_pair(Sub, false);
28169021974SDouglas Gregor 
28269021974SDouglas Gregor   // Create a new module with this name.
28369021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
28469021974SDouglas Gregor                               IsExplicit);
285eb90e830SDouglas Gregor   if (!Parent)
28669021974SDouglas Gregor     Modules[Name] = Result;
28769021974SDouglas Gregor   return std::make_pair(Result, true);
28869021974SDouglas Gregor }
28969021974SDouglas Gregor 
290de3ef502SDouglas Gregor Module *
29156c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
292e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
293a686e1b0SDouglas Gregor                                 bool IsSystem,
294e89dbc1dSDouglas Gregor                                 Module *Parent) {
29556c64013SDouglas Gregor   // Check whether we've already found this module.
296e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
297e89dbc1dSDouglas Gregor     return Mod;
298e89dbc1dSDouglas Gregor 
299e89dbc1dSDouglas Gregor   FileManager &FileMgr = SourceMgr->getFileManager();
30056c64013SDouglas Gregor 
30156c64013SDouglas Gregor   // Look for an umbrella header.
30256c64013SDouglas Gregor   llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
30356c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, "Headers");
30456c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
305e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
30656c64013SDouglas Gregor 
30756c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
30856c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
30956c64013SDouglas Gregor   // idea.
31056c64013SDouglas Gregor   if (!UmbrellaHeader)
31156c64013SDouglas Gregor     return 0;
31256c64013SDouglas Gregor 
313e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
314e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
315a686e1b0SDouglas Gregor   if (IsSystem)
316a686e1b0SDouglas Gregor     Result->IsSystem = IsSystem;
317a686e1b0SDouglas Gregor 
318eb90e830SDouglas Gregor   if (!Parent)
319e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
320e89dbc1dSDouglas Gregor 
321322f633cSDouglas Gregor   // umbrella header "umbrella-header-name"
32273141fa9SDouglas Gregor   Result->Umbrella = UmbrellaHeader;
32356c64013SDouglas Gregor   Headers[UmbrellaHeader] = Result;
3244dc71835SDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
325d8bd7537SDouglas Gregor 
326d8bd7537SDouglas Gregor   // export *
327d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
328d8bd7537SDouglas Gregor 
329a89c5ac4SDouglas Gregor   // module * { export * }
330a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
331a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
332a89c5ac4SDouglas Gregor 
333e89dbc1dSDouglas Gregor   // Look for subframeworks.
334e89dbc1dSDouglas Gregor   llvm::error_code EC;
335ddaa69cbSDouglas Gregor   llvm::SmallString<128> SubframeworksDirName
336ddaa69cbSDouglas Gregor     = StringRef(FrameworkDir->getName());
337e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
338ddaa69cbSDouglas Gregor   llvm::SmallString<128> SubframeworksDirNameNative;
339ddaa69cbSDouglas Gregor   llvm::sys::path::native(SubframeworksDirName.str(),
340ddaa69cbSDouglas Gregor                           SubframeworksDirNameNative);
341ddaa69cbSDouglas Gregor   for (llvm::sys::fs::directory_iterator
342ddaa69cbSDouglas Gregor          Dir(SubframeworksDirNameNative.str(), EC), DirEnd;
343e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
344e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
345e89dbc1dSDouglas Gregor       continue;
346f2161a70SDouglas Gregor 
347e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
348e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
349e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
350e89dbc1dSDouglas Gregor       inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
351a686e1b0SDouglas Gregor                            IsSystem, Result);
352e89dbc1dSDouglas Gregor     }
353e89dbc1dSDouglas Gregor   }
354e89dbc1dSDouglas Gregor 
35556c64013SDouglas Gregor   return Result;
35656c64013SDouglas Gregor }
35756c64013SDouglas Gregor 
358a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
359a89c5ac4SDouglas Gregor   Headers[UmbrellaHeader] = Mod;
36073141fa9SDouglas Gregor   Mod->Umbrella = UmbrellaHeader;
3617033127bSDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
362a89c5ac4SDouglas Gregor }
363a89c5ac4SDouglas Gregor 
364524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
365524e33e1SDouglas Gregor   Mod->Umbrella = UmbrellaDir;
366524e33e1SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
367524e33e1SDouglas Gregor }
368524e33e1SDouglas Gregor 
369a89c5ac4SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
370a89c5ac4SDouglas Gregor   Mod->Headers.push_back(Header);
371a89c5ac4SDouglas Gregor   Headers[Header] = Mod;
372a89c5ac4SDouglas Gregor }
373a89c5ac4SDouglas Gregor 
374514b636aSDouglas Gregor const FileEntry *
375de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) {
376514b636aSDouglas Gregor   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
377514b636aSDouglas Gregor     return 0;
378514b636aSDouglas Gregor 
379514b636aSDouglas Gregor   return SourceMgr->getFileEntryForID(
380514b636aSDouglas Gregor            SourceMgr->getFileID(Module->DefinitionLoc));
381514b636aSDouglas Gregor }
382514b636aSDouglas Gregor 
383718292f2SDouglas Gregor void ModuleMap::dump() {
384718292f2SDouglas Gregor   llvm::errs() << "Modules:";
385718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
386718292f2SDouglas Gregor                                         MEnd = Modules.end();
387718292f2SDouglas Gregor        M != MEnd; ++M)
388d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
389718292f2SDouglas Gregor 
390718292f2SDouglas Gregor   llvm::errs() << "Headers:";
391718292f2SDouglas Gregor   for (llvm::DenseMap<const FileEntry *, Module *>::iterator
392718292f2SDouglas Gregor             H = Headers.begin(),
393718292f2SDouglas Gregor          HEnd = Headers.end();
394718292f2SDouglas Gregor        H != HEnd; ++H) {
395718292f2SDouglas Gregor     llvm::errs() << "  \"" << H->first->getName() << "\" -> "
396718292f2SDouglas Gregor                  << H->second->getFullModuleName() << "\n";
397718292f2SDouglas Gregor   }
398718292f2SDouglas Gregor }
399718292f2SDouglas Gregor 
4002b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
4012b82c2a5SDouglas Gregor   bool HadError = false;
4022b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
4032b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
4042b82c2a5SDouglas Gregor                                               Complain);
405f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
4062b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
4072b82c2a5SDouglas Gregor     else
4082b82c2a5SDouglas Gregor       HadError = true;
4092b82c2a5SDouglas Gregor   }
4102b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
4112b82c2a5SDouglas Gregor   return HadError;
4122b82c2a5SDouglas Gregor }
4132b82c2a5SDouglas Gregor 
4140093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
4150093b3c7SDouglas Gregor   if (Loc.isInvalid())
4160093b3c7SDouglas Gregor     return 0;
4170093b3c7SDouglas Gregor 
4180093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
4190093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
4200093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
4210093b3c7SDouglas Gregor     return 0;
4220093b3c7SDouglas Gregor 
4230093b3c7SDouglas Gregor 
4240093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
4250093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
426224d8a74SDouglas Gregor 
427224d8a74SDouglas Gregor   while (const FileEntry *ExpansionFile
428224d8a74SDouglas Gregor            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
429224d8a74SDouglas Gregor     // Find the module that owns this header (if any).
430224d8a74SDouglas Gregor     if (Module *Mod = findModuleForHeader(ExpansionFile))
431224d8a74SDouglas Gregor       return Mod;
432224d8a74SDouglas Gregor 
433224d8a74SDouglas Gregor     // No module owns this header, so look up the inclusion chain to see if
434224d8a74SDouglas Gregor     // any included header has an associated module.
435224d8a74SDouglas Gregor     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
436224d8a74SDouglas Gregor     if (IncludeLoc.isInvalid())
4370093b3c7SDouglas Gregor       return 0;
4380093b3c7SDouglas Gregor 
439224d8a74SDouglas Gregor     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
440224d8a74SDouglas Gregor   }
441224d8a74SDouglas Gregor 
442224d8a74SDouglas Gregor   return 0;
4430093b3c7SDouglas Gregor }
4440093b3c7SDouglas Gregor 
445718292f2SDouglas Gregor //----------------------------------------------------------------------------//
446718292f2SDouglas Gregor // Module map file parser
447718292f2SDouglas Gregor //----------------------------------------------------------------------------//
448718292f2SDouglas Gregor 
449718292f2SDouglas Gregor namespace clang {
450718292f2SDouglas Gregor   /// \brief A token in a module map file.
451718292f2SDouglas Gregor   struct MMToken {
452718292f2SDouglas Gregor     enum TokenKind {
4531fb5c3a6SDouglas Gregor       Comma,
454718292f2SDouglas Gregor       EndOfFile,
455718292f2SDouglas Gregor       HeaderKeyword,
456718292f2SDouglas Gregor       Identifier,
457718292f2SDouglas Gregor       ExplicitKeyword,
4582b82c2a5SDouglas Gregor       ExportKeyword,
459755b2055SDouglas Gregor       FrameworkKeyword,
460718292f2SDouglas Gregor       ModuleKeyword,
4612b82c2a5SDouglas Gregor       Period,
462718292f2SDouglas Gregor       UmbrellaKeyword,
4631fb5c3a6SDouglas Gregor       RequiresKeyword,
4642b82c2a5SDouglas Gregor       Star,
465718292f2SDouglas Gregor       StringLiteral,
466718292f2SDouglas Gregor       LBrace,
467a686e1b0SDouglas Gregor       RBrace,
468a686e1b0SDouglas Gregor       LSquare,
469a686e1b0SDouglas Gregor       RSquare
470718292f2SDouglas Gregor     } Kind;
471718292f2SDouglas Gregor 
472718292f2SDouglas Gregor     unsigned Location;
473718292f2SDouglas Gregor     unsigned StringLength;
474718292f2SDouglas Gregor     const char *StringData;
475718292f2SDouglas Gregor 
476718292f2SDouglas Gregor     void clear() {
477718292f2SDouglas Gregor       Kind = EndOfFile;
478718292f2SDouglas Gregor       Location = 0;
479718292f2SDouglas Gregor       StringLength = 0;
480718292f2SDouglas Gregor       StringData = 0;
481718292f2SDouglas Gregor     }
482718292f2SDouglas Gregor 
483718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
484718292f2SDouglas Gregor 
485718292f2SDouglas Gregor     SourceLocation getLocation() const {
486718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
487718292f2SDouglas Gregor     }
488718292f2SDouglas Gregor 
489718292f2SDouglas Gregor     StringRef getString() const {
490718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
491718292f2SDouglas Gregor     }
492718292f2SDouglas Gregor   };
493718292f2SDouglas Gregor 
494718292f2SDouglas Gregor   class ModuleMapParser {
495718292f2SDouglas Gregor     Lexer &L;
496718292f2SDouglas Gregor     SourceManager &SourceMgr;
497718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
498718292f2SDouglas Gregor     ModuleMap &Map;
499718292f2SDouglas Gregor 
5005257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
5015257fc63SDouglas Gregor     const DirectoryEntry *Directory;
5025257fc63SDouglas Gregor 
503718292f2SDouglas Gregor     /// \brief Whether an error occurred.
504718292f2SDouglas Gregor     bool HadError;
505718292f2SDouglas Gregor 
506718292f2SDouglas Gregor     /// \brief Default target information, used only for string literal
507718292f2SDouglas Gregor     /// parsing.
508718292f2SDouglas Gregor     TargetInfo *Target;
509718292f2SDouglas Gregor 
510718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
511718292f2SDouglas Gregor     /// during parsing.
512718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
513718292f2SDouglas Gregor 
514718292f2SDouglas Gregor     /// \brief The current token.
515718292f2SDouglas Gregor     MMToken Tok;
516718292f2SDouglas Gregor 
517718292f2SDouglas Gregor     /// \brief The active module.
518de3ef502SDouglas Gregor     Module *ActiveModule;
519718292f2SDouglas Gregor 
520718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
521718292f2SDouglas Gregor     SourceLocation consumeToken();
522718292f2SDouglas Gregor 
523718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
524718292f2SDouglas Gregor     /// (or the end of the file).
525718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
526718292f2SDouglas Gregor 
527e7ab3669SDouglas Gregor     typedef llvm::SmallVector<std::pair<std::string, SourceLocation>, 2>
528e7ab3669SDouglas Gregor       ModuleId;
529e7ab3669SDouglas Gregor     bool parseModuleId(ModuleId &Id);
530718292f2SDouglas Gregor     void parseModuleDecl();
5311fb5c3a6SDouglas Gregor     void parseRequiresDecl();
532322f633cSDouglas Gregor     void parseHeaderDecl(SourceLocation UmbrellaLoc);
533524e33e1SDouglas Gregor     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
5342b82c2a5SDouglas Gregor     void parseExportDecl();
53573441091SDouglas Gregor     void parseInferredSubmoduleDecl(bool Explicit);
536718292f2SDouglas Gregor 
5377033127bSDouglas Gregor     const DirectoryEntry *getOverriddenHeaderSearchDir();
5387033127bSDouglas Gregor 
539718292f2SDouglas Gregor   public:
540718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
541718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
5425257fc63SDouglas Gregor                              ModuleMap &Map,
5435257fc63SDouglas Gregor                              const DirectoryEntry *Directory)
5445257fc63SDouglas Gregor       : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
5455257fc63SDouglas Gregor         Directory(Directory), HadError(false), ActiveModule(0)
546718292f2SDouglas Gregor     {
547718292f2SDouglas Gregor       TargetOptions TargetOpts;
548718292f2SDouglas Gregor       TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
549718292f2SDouglas Gregor       Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
550718292f2SDouglas Gregor 
551718292f2SDouglas Gregor       Tok.clear();
552718292f2SDouglas Gregor       consumeToken();
553718292f2SDouglas Gregor     }
554718292f2SDouglas Gregor 
555718292f2SDouglas Gregor     bool parseModuleMapFile();
556718292f2SDouglas Gregor   };
557718292f2SDouglas Gregor }
558718292f2SDouglas Gregor 
559718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
560718292f2SDouglas Gregor retry:
561718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
562718292f2SDouglas Gregor   Tok.clear();
563718292f2SDouglas Gregor 
564718292f2SDouglas Gregor   Token LToken;
565718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
566718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
567718292f2SDouglas Gregor   switch (LToken.getKind()) {
568718292f2SDouglas Gregor   case tok::raw_identifier:
569718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
570718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
571718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
572718292f2SDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
573718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
5742b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
575755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
576718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
5771fb5c3a6SDouglas Gregor                  .Case("requires", MMToken::RequiresKeyword)
578718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
579718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
580718292f2SDouglas Gregor     break;
581718292f2SDouglas Gregor 
5821fb5c3a6SDouglas Gregor   case tok::comma:
5831fb5c3a6SDouglas Gregor     Tok.Kind = MMToken::Comma;
5841fb5c3a6SDouglas Gregor     break;
5851fb5c3a6SDouglas Gregor 
586718292f2SDouglas Gregor   case tok::eof:
587718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
588718292f2SDouglas Gregor     break;
589718292f2SDouglas Gregor 
590718292f2SDouglas Gregor   case tok::l_brace:
591718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
592718292f2SDouglas Gregor     break;
593718292f2SDouglas Gregor 
594a686e1b0SDouglas Gregor   case tok::l_square:
595a686e1b0SDouglas Gregor     Tok.Kind = MMToken::LSquare;
596a686e1b0SDouglas Gregor     break;
597a686e1b0SDouglas Gregor 
5982b82c2a5SDouglas Gregor   case tok::period:
5992b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
6002b82c2a5SDouglas Gregor     break;
6012b82c2a5SDouglas Gregor 
602718292f2SDouglas Gregor   case tok::r_brace:
603718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
604718292f2SDouglas Gregor     break;
605718292f2SDouglas Gregor 
606a686e1b0SDouglas Gregor   case tok::r_square:
607a686e1b0SDouglas Gregor     Tok.Kind = MMToken::RSquare;
608a686e1b0SDouglas Gregor     break;
609a686e1b0SDouglas Gregor 
6102b82c2a5SDouglas Gregor   case tok::star:
6112b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
6122b82c2a5SDouglas Gregor     break;
6132b82c2a5SDouglas Gregor 
614718292f2SDouglas Gregor   case tok::string_literal: {
615718292f2SDouglas Gregor     // Parse the string literal.
616718292f2SDouglas Gregor     LangOptions LangOpts;
617718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
618718292f2SDouglas Gregor     if (StringLiteral.hadError)
619718292f2SDouglas Gregor       goto retry;
620718292f2SDouglas Gregor 
621718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
622718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
623718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
624718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
625718292f2SDouglas Gregor     Saved[Length] = 0;
626718292f2SDouglas Gregor 
627718292f2SDouglas Gregor     // Form the token.
628718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
629718292f2SDouglas Gregor     Tok.StringData = Saved;
630718292f2SDouglas Gregor     Tok.StringLength = Length;
631718292f2SDouglas Gregor     break;
632718292f2SDouglas Gregor   }
633718292f2SDouglas Gregor 
634718292f2SDouglas Gregor   case tok::comment:
635718292f2SDouglas Gregor     goto retry;
636718292f2SDouglas Gregor 
637718292f2SDouglas Gregor   default:
638718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
639718292f2SDouglas Gregor     HadError = true;
640718292f2SDouglas Gregor     goto retry;
641718292f2SDouglas Gregor   }
642718292f2SDouglas Gregor 
643718292f2SDouglas Gregor   return Result;
644718292f2SDouglas Gregor }
645718292f2SDouglas Gregor 
646718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
647718292f2SDouglas Gregor   unsigned braceDepth = 0;
648a686e1b0SDouglas Gregor   unsigned squareDepth = 0;
649718292f2SDouglas Gregor   do {
650718292f2SDouglas Gregor     switch (Tok.Kind) {
651718292f2SDouglas Gregor     case MMToken::EndOfFile:
652718292f2SDouglas Gregor       return;
653718292f2SDouglas Gregor 
654718292f2SDouglas Gregor     case MMToken::LBrace:
655a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
656718292f2SDouglas Gregor         return;
657718292f2SDouglas Gregor 
658718292f2SDouglas Gregor       ++braceDepth;
659718292f2SDouglas Gregor       break;
660718292f2SDouglas Gregor 
661a686e1b0SDouglas Gregor     case MMToken::LSquare:
662a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
663a686e1b0SDouglas Gregor         return;
664a686e1b0SDouglas Gregor 
665a686e1b0SDouglas Gregor       ++squareDepth;
666a686e1b0SDouglas Gregor       break;
667a686e1b0SDouglas Gregor 
668718292f2SDouglas Gregor     case MMToken::RBrace:
669718292f2SDouglas Gregor       if (braceDepth > 0)
670718292f2SDouglas Gregor         --braceDepth;
671718292f2SDouglas Gregor       else if (Tok.is(K))
672718292f2SDouglas Gregor         return;
673718292f2SDouglas Gregor       break;
674718292f2SDouglas Gregor 
675a686e1b0SDouglas Gregor     case MMToken::RSquare:
676a686e1b0SDouglas Gregor       if (squareDepth > 0)
677a686e1b0SDouglas Gregor         --squareDepth;
678a686e1b0SDouglas Gregor       else if (Tok.is(K))
679a686e1b0SDouglas Gregor         return;
680a686e1b0SDouglas Gregor       break;
681a686e1b0SDouglas Gregor 
682718292f2SDouglas Gregor     default:
683a686e1b0SDouglas Gregor       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
684718292f2SDouglas Gregor         return;
685718292f2SDouglas Gregor       break;
686718292f2SDouglas Gregor     }
687718292f2SDouglas Gregor 
688718292f2SDouglas Gregor    consumeToken();
689718292f2SDouglas Gregor   } while (true);
690718292f2SDouglas Gregor }
691718292f2SDouglas Gregor 
692e7ab3669SDouglas Gregor /// \brief Parse a module-id.
693e7ab3669SDouglas Gregor ///
694e7ab3669SDouglas Gregor ///   module-id:
695e7ab3669SDouglas Gregor ///     identifier
696e7ab3669SDouglas Gregor ///     identifier '.' module-id
697e7ab3669SDouglas Gregor ///
698e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise.
699e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) {
700e7ab3669SDouglas Gregor   Id.clear();
701e7ab3669SDouglas Gregor   do {
702e7ab3669SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
703e7ab3669SDouglas Gregor       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
704e7ab3669SDouglas Gregor       consumeToken();
705e7ab3669SDouglas Gregor     } else {
706e7ab3669SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
707e7ab3669SDouglas Gregor       return true;
708e7ab3669SDouglas Gregor     }
709e7ab3669SDouglas Gregor 
710e7ab3669SDouglas Gregor     if (!Tok.is(MMToken::Period))
711e7ab3669SDouglas Gregor       break;
712e7ab3669SDouglas Gregor 
713e7ab3669SDouglas Gregor     consumeToken();
714e7ab3669SDouglas Gregor   } while (true);
715e7ab3669SDouglas Gregor 
716e7ab3669SDouglas Gregor   return false;
717e7ab3669SDouglas Gregor }
718e7ab3669SDouglas Gregor 
719a686e1b0SDouglas Gregor namespace {
720a686e1b0SDouglas Gregor   /// \brief Enumerates the known attributes.
721a686e1b0SDouglas Gregor   enum AttributeKind {
722a686e1b0SDouglas Gregor     /// \brief An unknown attribute.
723a686e1b0SDouglas Gregor     AT_unknown,
724a686e1b0SDouglas Gregor     /// \brief The 'system' attribute.
725a686e1b0SDouglas Gregor     AT_system
726a686e1b0SDouglas Gregor   };
727a686e1b0SDouglas Gregor }
728a686e1b0SDouglas Gregor 
729718292f2SDouglas Gregor /// \brief Parse a module declaration.
730718292f2SDouglas Gregor ///
731718292f2SDouglas Gregor ///   module-declaration:
732a686e1b0SDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
733a686e1b0SDouglas Gregor ///       { module-member* }
734a686e1b0SDouglas Gregor ///
735a686e1b0SDouglas Gregor ///   attributes:
736a686e1b0SDouglas Gregor ///     attribute attributes
737a686e1b0SDouglas Gregor ///     attribute
738a686e1b0SDouglas Gregor ///
739a686e1b0SDouglas Gregor ///   attribute:
740a686e1b0SDouglas Gregor ///     [ identifier ]
741718292f2SDouglas Gregor ///
742718292f2SDouglas Gregor ///   module-member:
7431fb5c3a6SDouglas Gregor ///     requires-declaration
744718292f2SDouglas Gregor ///     header-declaration
745e7ab3669SDouglas Gregor ///     submodule-declaration
7462b82c2a5SDouglas Gregor ///     export-declaration
74773441091SDouglas Gregor ///
74873441091SDouglas Gregor ///   submodule-declaration:
74973441091SDouglas Gregor ///     module-declaration
75073441091SDouglas Gregor ///     inferred-submodule-declaration
751718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
752755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
753755b2055SDouglas Gregor          Tok.is(MMToken::FrameworkKeyword));
754f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
755e7ab3669SDouglas Gregor   SourceLocation ExplicitLoc;
756718292f2SDouglas Gregor   bool Explicit = false;
757f2161a70SDouglas Gregor   bool Framework = false;
758755b2055SDouglas Gregor 
759f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
760f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
761e7ab3669SDouglas Gregor     ExplicitLoc = consumeToken();
762f2161a70SDouglas Gregor     Explicit = true;
763f2161a70SDouglas Gregor   }
764f2161a70SDouglas Gregor 
765f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
766755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
767755b2055SDouglas Gregor     consumeToken();
768755b2055SDouglas Gregor     Framework = true;
769755b2055SDouglas Gregor   }
770718292f2SDouglas Gregor 
771718292f2SDouglas Gregor   // Parse 'module' keyword.
772718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
773d6343c99SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
774718292f2SDouglas Gregor     consumeToken();
775718292f2SDouglas Gregor     HadError = true;
776718292f2SDouglas Gregor     return;
777718292f2SDouglas Gregor   }
778718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
779718292f2SDouglas Gregor 
78073441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
78173441091SDouglas Gregor   // Parse it.
78273441091SDouglas Gregor   if (Tok.is(MMToken::Star))
78373441091SDouglas Gregor     return parseInferredSubmoduleDecl(Explicit);
78473441091SDouglas Gregor 
785718292f2SDouglas Gregor   // Parse the module name.
786e7ab3669SDouglas Gregor   ModuleId Id;
787e7ab3669SDouglas Gregor   if (parseModuleId(Id)) {
788718292f2SDouglas Gregor     HadError = true;
789718292f2SDouglas Gregor     return;
790718292f2SDouglas Gregor   }
791e7ab3669SDouglas Gregor 
792e7ab3669SDouglas Gregor   if (ActiveModule) {
793e7ab3669SDouglas Gregor     if (Id.size() > 1) {
794e7ab3669SDouglas Gregor       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
795e7ab3669SDouglas Gregor         << SourceRange(Id.front().second, Id.back().second);
796e7ab3669SDouglas Gregor 
797e7ab3669SDouglas Gregor       HadError = true;
798e7ab3669SDouglas Gregor       return;
799e7ab3669SDouglas Gregor     }
800e7ab3669SDouglas Gregor   } else if (Id.size() == 1 && Explicit) {
801e7ab3669SDouglas Gregor     // Top-level modules can't be explicit.
802e7ab3669SDouglas Gregor     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
803e7ab3669SDouglas Gregor     Explicit = false;
804e7ab3669SDouglas Gregor     ExplicitLoc = SourceLocation();
805e7ab3669SDouglas Gregor     HadError = true;
806e7ab3669SDouglas Gregor   }
807e7ab3669SDouglas Gregor 
808e7ab3669SDouglas Gregor   Module *PreviousActiveModule = ActiveModule;
809e7ab3669SDouglas Gregor   if (Id.size() > 1) {
810e7ab3669SDouglas Gregor     // This module map defines a submodule. Go find the module of which it
811e7ab3669SDouglas Gregor     // is a submodule.
812e7ab3669SDouglas Gregor     ActiveModule = 0;
813e7ab3669SDouglas Gregor     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
814e7ab3669SDouglas Gregor       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
815e7ab3669SDouglas Gregor         ActiveModule = Next;
816e7ab3669SDouglas Gregor         continue;
817e7ab3669SDouglas Gregor       }
818e7ab3669SDouglas Gregor 
819e7ab3669SDouglas Gregor       if (ActiveModule) {
820e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
821e7ab3669SDouglas Gregor           << Id[I].first << ActiveModule->getTopLevelModule();
822e7ab3669SDouglas Gregor       } else {
823e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
824e7ab3669SDouglas Gregor       }
825e7ab3669SDouglas Gregor       HadError = true;
826e7ab3669SDouglas Gregor       return;
827e7ab3669SDouglas Gregor     }
828e7ab3669SDouglas Gregor   }
829e7ab3669SDouglas Gregor 
830e7ab3669SDouglas Gregor   StringRef ModuleName = Id.back().first;
831e7ab3669SDouglas Gregor   SourceLocation ModuleNameLoc = Id.back().second;
832718292f2SDouglas Gregor 
833a686e1b0SDouglas Gregor   // Parse the optional attribute list.
834a686e1b0SDouglas Gregor   bool IsSystem = false;
835a686e1b0SDouglas Gregor   while (Tok.is(MMToken::LSquare)) {
836a686e1b0SDouglas Gregor     // Consume the '['.
837a686e1b0SDouglas Gregor     SourceLocation LSquareLoc = consumeToken();
838a686e1b0SDouglas Gregor 
839a686e1b0SDouglas Gregor     // Check whether we have an attribute name here.
840a686e1b0SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
841a686e1b0SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
842a686e1b0SDouglas Gregor       skipUntil(MMToken::RSquare);
843a686e1b0SDouglas Gregor       if (Tok.is(MMToken::RSquare))
844a686e1b0SDouglas Gregor         consumeToken();
845a686e1b0SDouglas Gregor       continue;
846a686e1b0SDouglas Gregor     }
847a686e1b0SDouglas Gregor 
848a686e1b0SDouglas Gregor     // Decode the attribute name.
849a686e1b0SDouglas Gregor     AttributeKind Attribute
850a686e1b0SDouglas Gregor       = llvm::StringSwitch<AttributeKind>(Tok.getString())
851a686e1b0SDouglas Gregor         .Case("system", AT_system)
852a686e1b0SDouglas Gregor         .Default(AT_unknown);
853a686e1b0SDouglas Gregor     switch (Attribute) {
854a686e1b0SDouglas Gregor     case AT_unknown:
855a686e1b0SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
856a686e1b0SDouglas Gregor         << Tok.getString();
857a686e1b0SDouglas Gregor       break;
858a686e1b0SDouglas Gregor 
859a686e1b0SDouglas Gregor     case AT_system:
860a686e1b0SDouglas Gregor       IsSystem = true;
861a686e1b0SDouglas Gregor       break;
862a686e1b0SDouglas Gregor     }
863a686e1b0SDouglas Gregor     consumeToken();
864a686e1b0SDouglas Gregor 
865a686e1b0SDouglas Gregor     // Consume the ']'.
866a686e1b0SDouglas Gregor     if (!Tok.is(MMToken::RSquare)) {
867a686e1b0SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
868a686e1b0SDouglas Gregor       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
869a686e1b0SDouglas Gregor       skipUntil(MMToken::RSquare);
870a686e1b0SDouglas Gregor     }
871a686e1b0SDouglas Gregor 
872a686e1b0SDouglas Gregor     if (Tok.is(MMToken::RSquare))
873a686e1b0SDouglas Gregor       consumeToken();
874a686e1b0SDouglas Gregor   }
875a686e1b0SDouglas Gregor 
876718292f2SDouglas Gregor   // Parse the opening brace.
877718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
878718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
879718292f2SDouglas Gregor       << ModuleName;
880718292f2SDouglas Gregor     HadError = true;
881718292f2SDouglas Gregor     return;
882718292f2SDouglas Gregor   }
883718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
884718292f2SDouglas Gregor 
885718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
886eb90e830SDouglas Gregor   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
887fcc54a3bSDouglas Gregor     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
888fcc54a3bSDouglas Gregor       // Skip the module definition.
889fcc54a3bSDouglas Gregor       skipUntil(MMToken::RBrace);
890fcc54a3bSDouglas Gregor       if (Tok.is(MMToken::RBrace))
891fcc54a3bSDouglas Gregor         consumeToken();
892fcc54a3bSDouglas Gregor       else {
893fcc54a3bSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
894fcc54a3bSDouglas Gregor         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
895fcc54a3bSDouglas Gregor         HadError = true;
896fcc54a3bSDouglas Gregor       }
897fcc54a3bSDouglas Gregor       return;
898fcc54a3bSDouglas Gregor     }
899fcc54a3bSDouglas Gregor 
900718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
901718292f2SDouglas Gregor       << ModuleName;
902eb90e830SDouglas Gregor     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
903718292f2SDouglas Gregor 
904718292f2SDouglas Gregor     // Skip the module definition.
905718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
906718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
907718292f2SDouglas Gregor       consumeToken();
908718292f2SDouglas Gregor 
909718292f2SDouglas Gregor     HadError = true;
910718292f2SDouglas Gregor     return;
911718292f2SDouglas Gregor   }
912718292f2SDouglas Gregor 
913718292f2SDouglas Gregor   // Start defining this module.
914eb90e830SDouglas Gregor   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
915eb90e830SDouglas Gregor                                         Explicit).first;
916eb90e830SDouglas Gregor   ActiveModule->DefinitionLoc = ModuleNameLoc;
917a686e1b0SDouglas Gregor   if (IsSystem)
918a686e1b0SDouglas Gregor     ActiveModule->IsSystem = true;
919718292f2SDouglas Gregor 
920718292f2SDouglas Gregor   bool Done = false;
921718292f2SDouglas Gregor   do {
922718292f2SDouglas Gregor     switch (Tok.Kind) {
923718292f2SDouglas Gregor     case MMToken::EndOfFile:
924718292f2SDouglas Gregor     case MMToken::RBrace:
925718292f2SDouglas Gregor       Done = true;
926718292f2SDouglas Gregor       break;
927718292f2SDouglas Gregor 
928718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
929f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
930718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
931718292f2SDouglas Gregor       parseModuleDecl();
932718292f2SDouglas Gregor       break;
933718292f2SDouglas Gregor 
9342b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
9352b82c2a5SDouglas Gregor       parseExportDecl();
9362b82c2a5SDouglas Gregor       break;
9372b82c2a5SDouglas Gregor 
9381fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
9391fb5c3a6SDouglas Gregor       parseRequiresDecl();
9401fb5c3a6SDouglas Gregor       break;
9411fb5c3a6SDouglas Gregor 
942524e33e1SDouglas Gregor     case MMToken::UmbrellaKeyword: {
943524e33e1SDouglas Gregor       SourceLocation UmbrellaLoc = consumeToken();
944524e33e1SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword))
945524e33e1SDouglas Gregor         parseHeaderDecl(UmbrellaLoc);
946524e33e1SDouglas Gregor       else
947524e33e1SDouglas Gregor         parseUmbrellaDirDecl(UmbrellaLoc);
948718292f2SDouglas Gregor       break;
949524e33e1SDouglas Gregor     }
950718292f2SDouglas Gregor 
951322f633cSDouglas Gregor     case MMToken::HeaderKeyword:
952322f633cSDouglas Gregor       parseHeaderDecl(SourceLocation());
953718292f2SDouglas Gregor       break;
954718292f2SDouglas Gregor 
955718292f2SDouglas Gregor     default:
956718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
957718292f2SDouglas Gregor       consumeToken();
958718292f2SDouglas Gregor       break;
959718292f2SDouglas Gregor     }
960718292f2SDouglas Gregor   } while (!Done);
961718292f2SDouglas Gregor 
962718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
963718292f2SDouglas Gregor     consumeToken();
964718292f2SDouglas Gregor   else {
965718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
966718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
967718292f2SDouglas Gregor     HadError = true;
968718292f2SDouglas Gregor   }
969718292f2SDouglas Gregor 
970e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
971e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
972718292f2SDouglas Gregor }
973718292f2SDouglas Gregor 
9741fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration.
9751fb5c3a6SDouglas Gregor ///
9761fb5c3a6SDouglas Gregor ///   requires-declaration:
9771fb5c3a6SDouglas Gregor ///     'requires' feature-list
9781fb5c3a6SDouglas Gregor ///
9791fb5c3a6SDouglas Gregor ///   feature-list:
9801fb5c3a6SDouglas Gregor ///     identifier ',' feature-list
9811fb5c3a6SDouglas Gregor ///     identifier
9821fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() {
9831fb5c3a6SDouglas Gregor   assert(Tok.is(MMToken::RequiresKeyword));
9841fb5c3a6SDouglas Gregor 
9851fb5c3a6SDouglas Gregor   // Parse 'requires' keyword.
9861fb5c3a6SDouglas Gregor   consumeToken();
9871fb5c3a6SDouglas Gregor 
9881fb5c3a6SDouglas Gregor   // Parse the feature-list.
9891fb5c3a6SDouglas Gregor   do {
9901fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
9911fb5c3a6SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
9921fb5c3a6SDouglas Gregor       HadError = true;
9931fb5c3a6SDouglas Gregor       return;
9941fb5c3a6SDouglas Gregor     }
9951fb5c3a6SDouglas Gregor 
9961fb5c3a6SDouglas Gregor     // Consume the feature name.
9971fb5c3a6SDouglas Gregor     std::string Feature = Tok.getString();
9981fb5c3a6SDouglas Gregor     consumeToken();
9991fb5c3a6SDouglas Gregor 
10001fb5c3a6SDouglas Gregor     // Add this feature.
1001*89929282SDouglas Gregor     ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target);
10021fb5c3a6SDouglas Gregor 
10031fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Comma))
10041fb5c3a6SDouglas Gregor       break;
10051fb5c3a6SDouglas Gregor 
10061fb5c3a6SDouglas Gregor     // Consume the comma.
10071fb5c3a6SDouglas Gregor     consumeToken();
10081fb5c3a6SDouglas Gregor   } while (true);
10091fb5c3a6SDouglas Gregor }
10101fb5c3a6SDouglas Gregor 
1011f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
1012f2161a70SDouglas Gregor /// subframework in which the given module lives.
1013f2161a70SDouglas Gregor void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) {
1014f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
1015f2161a70SDouglas Gregor   llvm::SmallVector<StringRef, 2> Paths;
1016f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
1017f2161a70SDouglas Gregor     if (Mod->IsFramework)
1018f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
1019f2161a70SDouglas Gregor   }
1020f2161a70SDouglas Gregor 
1021f2161a70SDouglas Gregor   if (Paths.empty())
1022f2161a70SDouglas Gregor     return;
1023f2161a70SDouglas Gregor 
1024f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
1025f2161a70SDouglas Gregor   for (unsigned I = Paths.size() - 1; I != 0; --I) {
1026f2161a70SDouglas Gregor     llvm::sys::path::append(Path, "Frameworks");
1027f2161a70SDouglas Gregor     llvm::sys::path::append(Path, Paths[I-1] + ".framework");
1028f2161a70SDouglas Gregor   }
1029f2161a70SDouglas Gregor }
1030f2161a70SDouglas Gregor 
1031718292f2SDouglas Gregor /// \brief Parse a header declaration.
1032718292f2SDouglas Gregor ///
1033718292f2SDouglas Gregor ///   header-declaration:
1034322f633cSDouglas Gregor ///     'umbrella'[opt] 'header' string-literal
1035322f633cSDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc) {
1036718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
10371871ed3dSBenjamin Kramer   consumeToken();
1038718292f2SDouglas Gregor 
1039322f633cSDouglas Gregor   bool Umbrella = UmbrellaLoc.isValid();
1040322f633cSDouglas Gregor 
1041718292f2SDouglas Gregor   // Parse the header name.
1042718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1043718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1044718292f2SDouglas Gregor       << "header";
1045718292f2SDouglas Gregor     HadError = true;
1046718292f2SDouglas Gregor     return;
1047718292f2SDouglas Gregor   }
1048e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
1049718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
1050718292f2SDouglas Gregor 
1051524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1052524e33e1SDouglas Gregor   if (Umbrella && ActiveModule->Umbrella) {
1053524e33e1SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
1054524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1055322f633cSDouglas Gregor     HadError = true;
1056322f633cSDouglas Gregor     return;
1057322f633cSDouglas Gregor   }
1058322f633cSDouglas Gregor 
10595257fc63SDouglas Gregor   // Look for this file.
1060e7ab3669SDouglas Gregor   const FileEntry *File = 0;
10615257fc63SDouglas Gregor   llvm::SmallString<128> PathName;
1062e7ab3669SDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
1063e7ab3669SDouglas Gregor     PathName = FileName;
1064e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
10657033127bSDouglas Gregor   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
10667033127bSDouglas Gregor     PathName = Dir->getName();
10677033127bSDouglas Gregor     llvm::sys::path::append(PathName, FileName);
10687033127bSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
1069e7ab3669SDouglas Gregor   } else {
1070e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
10717033127bSDouglas Gregor     PathName = Directory->getName();
1072e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
1073755b2055SDouglas Gregor 
1074f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
1075f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
1076755b2055SDouglas Gregor 
1077e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
1078e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
10795257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1080e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
1081e7ab3669SDouglas Gregor 
1082e7ab3669SDouglas Gregor       if (!File) {
1083e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
1084e7ab3669SDouglas Gregor         PathName.resize(PathLength);
1085e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
1086e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
1087e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
1088e7ab3669SDouglas Gregor       }
1089e7ab3669SDouglas Gregor     } else {
1090e7ab3669SDouglas Gregor       // Lookup for normal headers.
1091e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1092e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
1093e7ab3669SDouglas Gregor     }
1094e7ab3669SDouglas Gregor   }
10955257fc63SDouglas Gregor 
10965257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
10975257fc63SDouglas Gregor   // Come up with a lazy way to do this.
1098e7ab3669SDouglas Gregor   if (File) {
10995257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
11005257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
11015257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
11025257fc63SDouglas Gregor       HadError = true;
1103322f633cSDouglas Gregor     } else if (Umbrella) {
1104322f633cSDouglas Gregor       const DirectoryEntry *UmbrellaDir = File->getDir();
1105322f633cSDouglas Gregor       if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
1106322f633cSDouglas Gregor         Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1107322f633cSDouglas Gregor           << OwningModule->getFullModuleName();
1108322f633cSDouglas Gregor         HadError = true;
11095257fc63SDouglas Gregor       } else {
1110322f633cSDouglas Gregor         // Record this umbrella header.
1111322f633cSDouglas Gregor         Map.setUmbrellaHeader(ActiveModule, File);
1112322f633cSDouglas Gregor       }
1113322f633cSDouglas Gregor     } else {
1114322f633cSDouglas Gregor       // Record this header.
1115a89c5ac4SDouglas Gregor       Map.addHeader(ActiveModule, File);
11165257fc63SDouglas Gregor     }
11175257fc63SDouglas Gregor   } else {
11185257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
1119524e33e1SDouglas Gregor       << Umbrella << FileName;
11205257fc63SDouglas Gregor     HadError = true;
11215257fc63SDouglas Gregor   }
1122718292f2SDouglas Gregor }
1123718292f2SDouglas Gregor 
1124524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration.
1125524e33e1SDouglas Gregor ///
1126524e33e1SDouglas Gregor ///   umbrella-dir-declaration:
1127524e33e1SDouglas Gregor ///     umbrella string-literal
1128524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1129524e33e1SDouglas Gregor   // Parse the directory name.
1130524e33e1SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1131524e33e1SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1132524e33e1SDouglas Gregor       << "umbrella";
1133524e33e1SDouglas Gregor     HadError = true;
1134524e33e1SDouglas Gregor     return;
1135524e33e1SDouglas Gregor   }
1136524e33e1SDouglas Gregor 
1137524e33e1SDouglas Gregor   std::string DirName = Tok.getString();
1138524e33e1SDouglas Gregor   SourceLocation DirNameLoc = consumeToken();
1139524e33e1SDouglas Gregor 
1140524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1141524e33e1SDouglas Gregor   if (ActiveModule->Umbrella) {
1142524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1143524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1144524e33e1SDouglas Gregor     HadError = true;
1145524e33e1SDouglas Gregor     return;
1146524e33e1SDouglas Gregor   }
1147524e33e1SDouglas Gregor 
1148524e33e1SDouglas Gregor   // Look for this file.
1149524e33e1SDouglas Gregor   const DirectoryEntry *Dir = 0;
1150524e33e1SDouglas Gregor   if (llvm::sys::path::is_absolute(DirName))
1151524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1152524e33e1SDouglas Gregor   else {
1153524e33e1SDouglas Gregor     llvm::SmallString<128> PathName;
1154524e33e1SDouglas Gregor     PathName = Directory->getName();
1155524e33e1SDouglas Gregor     llvm::sys::path::append(PathName, DirName);
1156524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1157524e33e1SDouglas Gregor   }
1158524e33e1SDouglas Gregor 
1159524e33e1SDouglas Gregor   if (!Dir) {
1160524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1161524e33e1SDouglas Gregor       << DirName;
1162524e33e1SDouglas Gregor     HadError = true;
1163524e33e1SDouglas Gregor     return;
1164524e33e1SDouglas Gregor   }
1165524e33e1SDouglas Gregor 
1166524e33e1SDouglas Gregor   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1167524e33e1SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1168524e33e1SDouglas Gregor       << OwningModule->getFullModuleName();
1169524e33e1SDouglas Gregor     HadError = true;
1170524e33e1SDouglas Gregor     return;
1171524e33e1SDouglas Gregor   }
1172524e33e1SDouglas Gregor 
1173524e33e1SDouglas Gregor   // Record this umbrella directory.
1174524e33e1SDouglas Gregor   Map.setUmbrellaDir(ActiveModule, Dir);
1175524e33e1SDouglas Gregor }
1176524e33e1SDouglas Gregor 
11772b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
11782b82c2a5SDouglas Gregor ///
11792b82c2a5SDouglas Gregor ///   export-declaration:
11802b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
11812b82c2a5SDouglas Gregor ///
11822b82c2a5SDouglas Gregor ///   wildcard-module-id:
11832b82c2a5SDouglas Gregor ///     identifier
11842b82c2a5SDouglas Gregor ///     '*'
11852b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
11862b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
11872b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
11882b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
11892b82c2a5SDouglas Gregor 
11902b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
11912b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
11922b82c2a5SDouglas Gregor   bool Wildcard = false;
11932b82c2a5SDouglas Gregor   do {
11942b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
11952b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
11962b82c2a5SDouglas Gregor                                               Tok.getLocation()));
11972b82c2a5SDouglas Gregor       consumeToken();
11982b82c2a5SDouglas Gregor 
11992b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
12002b82c2a5SDouglas Gregor         consumeToken();
12012b82c2a5SDouglas Gregor         continue;
12022b82c2a5SDouglas Gregor       }
12032b82c2a5SDouglas Gregor 
12042b82c2a5SDouglas Gregor       break;
12052b82c2a5SDouglas Gregor     }
12062b82c2a5SDouglas Gregor 
12072b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
12082b82c2a5SDouglas Gregor       Wildcard = true;
1209f5eedd05SDouglas Gregor       consumeToken();
12102b82c2a5SDouglas Gregor       break;
12112b82c2a5SDouglas Gregor     }
12122b82c2a5SDouglas Gregor 
12132b82c2a5SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
12142b82c2a5SDouglas Gregor     HadError = true;
12152b82c2a5SDouglas Gregor     return;
12162b82c2a5SDouglas Gregor   } while (true);
12172b82c2a5SDouglas Gregor 
12182b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
12192b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
12202b82c2a5SDouglas Gregor   };
12212b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
12222b82c2a5SDouglas Gregor }
12232b82c2a5SDouglas Gregor 
122473441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
122573441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
122673441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
122773441091SDouglas Gregor   bool Failed = false;
122873441091SDouglas Gregor 
122973441091SDouglas Gregor   // Inferred modules must be submodules.
123073441091SDouglas Gregor   if (!ActiveModule) {
123173441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
123273441091SDouglas Gregor     Failed = true;
123373441091SDouglas Gregor   }
123473441091SDouglas Gregor 
1235524e33e1SDouglas Gregor   // Inferred modules must have umbrella directories.
1236524e33e1SDouglas Gregor   if (!Failed && !ActiveModule->getUmbrellaDir()) {
123773441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
123873441091SDouglas Gregor     Failed = true;
123973441091SDouglas Gregor   }
124073441091SDouglas Gregor 
124173441091SDouglas Gregor   // Check for redefinition of an inferred module.
1242dd005f69SDouglas Gregor   if (!Failed && ActiveModule->InferSubmodules) {
124373441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1244dd005f69SDouglas Gregor     if (ActiveModule->InferredSubmoduleLoc.isValid())
1245dd005f69SDouglas Gregor       Diags.Report(ActiveModule->InferredSubmoduleLoc,
124673441091SDouglas Gregor                    diag::note_mmap_prev_definition);
124773441091SDouglas Gregor     Failed = true;
124873441091SDouglas Gregor   }
124973441091SDouglas Gregor 
125073441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
125173441091SDouglas Gregor   if (Failed) {
125273441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
125373441091SDouglas Gregor       consumeToken();
125473441091SDouglas Gregor       skipUntil(MMToken::RBrace);
125573441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
125673441091SDouglas Gregor         consumeToken();
125773441091SDouglas Gregor     }
125873441091SDouglas Gregor     HadError = true;
125973441091SDouglas Gregor     return;
126073441091SDouglas Gregor   }
126173441091SDouglas Gregor 
126273441091SDouglas Gregor   // Note that we have an inferred submodule.
1263dd005f69SDouglas Gregor   ActiveModule->InferSubmodules = true;
1264dd005f69SDouglas Gregor   ActiveModule->InferredSubmoduleLoc = StarLoc;
1265dd005f69SDouglas Gregor   ActiveModule->InferExplicitSubmodules = Explicit;
126673441091SDouglas Gregor 
126773441091SDouglas Gregor   // Parse the opening brace.
126873441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
126973441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
127073441091SDouglas Gregor     HadError = true;
127173441091SDouglas Gregor     return;
127273441091SDouglas Gregor   }
127373441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
127473441091SDouglas Gregor 
127573441091SDouglas Gregor   // Parse the body of the inferred submodule.
127673441091SDouglas Gregor   bool Done = false;
127773441091SDouglas Gregor   do {
127873441091SDouglas Gregor     switch (Tok.Kind) {
127973441091SDouglas Gregor     case MMToken::EndOfFile:
128073441091SDouglas Gregor     case MMToken::RBrace:
128173441091SDouglas Gregor       Done = true;
128273441091SDouglas Gregor       break;
128373441091SDouglas Gregor 
128473441091SDouglas Gregor     case MMToken::ExportKeyword: {
128573441091SDouglas Gregor       consumeToken();
128673441091SDouglas Gregor       if (Tok.is(MMToken::Star))
1287dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
128873441091SDouglas Gregor       else
128973441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
129073441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
129173441091SDouglas Gregor       consumeToken();
129273441091SDouglas Gregor       break;
129373441091SDouglas Gregor     }
129473441091SDouglas Gregor 
129573441091SDouglas Gregor     case MMToken::ExplicitKeyword:
129673441091SDouglas Gregor     case MMToken::ModuleKeyword:
129773441091SDouglas Gregor     case MMToken::HeaderKeyword:
129873441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
129973441091SDouglas Gregor     default:
130073441091SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
130173441091SDouglas Gregor       consumeToken();
130273441091SDouglas Gregor       break;
130373441091SDouglas Gregor     }
130473441091SDouglas Gregor   } while (!Done);
130573441091SDouglas Gregor 
130673441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
130773441091SDouglas Gregor     consumeToken();
130873441091SDouglas Gregor   else {
130973441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
131073441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
131173441091SDouglas Gregor     HadError = true;
131273441091SDouglas Gregor   }
131373441091SDouglas Gregor }
131473441091SDouglas Gregor 
13157033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence
13167033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
13177033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
13187033127bSDouglas Gregor   for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
13197033127bSDouglas Gregor     // If we have an umbrella directory, use that.
13207033127bSDouglas Gregor     if (Mod->hasUmbrellaDir())
13217033127bSDouglas Gregor       return Mod->getUmbrellaDir();
13227033127bSDouglas Gregor 
13237033127bSDouglas Gregor     // If we have a framework directory, stop looking.
13247033127bSDouglas Gregor     if (Mod->IsFramework)
13257033127bSDouglas Gregor       return 0;
13267033127bSDouglas Gregor   }
13277033127bSDouglas Gregor 
13287033127bSDouglas Gregor   return 0;
13297033127bSDouglas Gregor }
13307033127bSDouglas Gregor 
1331718292f2SDouglas Gregor /// \brief Parse a module map file.
1332718292f2SDouglas Gregor ///
1333718292f2SDouglas Gregor ///   module-map-file:
1334718292f2SDouglas Gregor ///     module-declaration*
1335718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
1336718292f2SDouglas Gregor   do {
1337718292f2SDouglas Gregor     switch (Tok.Kind) {
1338718292f2SDouglas Gregor     case MMToken::EndOfFile:
1339718292f2SDouglas Gregor       return HadError;
1340718292f2SDouglas Gregor 
1341e7ab3669SDouglas Gregor     case MMToken::ExplicitKeyword:
1342718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1343755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
1344718292f2SDouglas Gregor       parseModuleDecl();
1345718292f2SDouglas Gregor       break;
1346718292f2SDouglas Gregor 
13471fb5c3a6SDouglas Gregor     case MMToken::Comma:
13482b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
1349718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
1350718292f2SDouglas Gregor     case MMToken::Identifier:
1351718292f2SDouglas Gregor     case MMToken::LBrace:
1352a686e1b0SDouglas Gregor     case MMToken::LSquare:
13532b82c2a5SDouglas Gregor     case MMToken::Period:
1354718292f2SDouglas Gregor     case MMToken::RBrace:
1355a686e1b0SDouglas Gregor     case MMToken::RSquare:
13561fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
13572b82c2a5SDouglas Gregor     case MMToken::Star:
1358718292f2SDouglas Gregor     case MMToken::StringLiteral:
1359718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
1360718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1361718292f2SDouglas Gregor       HadError = true;
1362718292f2SDouglas Gregor       consumeToken();
1363718292f2SDouglas Gregor       break;
1364718292f2SDouglas Gregor     }
1365718292f2SDouglas Gregor   } while (true);
1366718292f2SDouglas Gregor }
1367718292f2SDouglas Gregor 
1368718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
1369*89929282SDouglas Gregor   assert(Target != 0 && "Missing target information");
1370718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1371718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1372718292f2SDouglas Gregor   if (!Buffer)
1373718292f2SDouglas Gregor     return true;
1374718292f2SDouglas Gregor 
1375718292f2SDouglas Gregor   // Parse this module map file.
13761fb5c3a6SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
13771fb5c3a6SDouglas Gregor   Diags->getClient()->BeginSourceFile(MMapLangOpts);
13785257fc63SDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir());
1379718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
1380718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
1381718292f2SDouglas Gregor 
1382718292f2SDouglas Gregor   return Result;
1383718292f2SDouglas Gregor }
1384