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"
23*e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h"
24718292f2SDouglas Gregor #include "llvm/Support/Host.h"
255257fc63SDouglas Gregor #include "llvm/Support/PathV2.h"
26718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h"
27718292f2SDouglas Gregor #include "llvm/ADT/StringRef.h"
28718292f2SDouglas Gregor #include "llvm/ADT/StringSwitch.h"
29718292f2SDouglas Gregor using namespace clang;
30718292f2SDouglas Gregor 
312b82c2a5SDouglas Gregor Module::ExportDecl
322b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod,
332b82c2a5SDouglas Gregor                          const Module::UnresolvedExportDecl &Unresolved,
342b82c2a5SDouglas Gregor                          bool Complain) {
35f5eedd05SDouglas Gregor   // We may have just a wildcard.
36f5eedd05SDouglas Gregor   if (Unresolved.Id.empty()) {
37f5eedd05SDouglas Gregor     assert(Unresolved.Wildcard && "Invalid unresolved export");
38f5eedd05SDouglas Gregor     return Module::ExportDecl(0, true);
39f5eedd05SDouglas Gregor   }
40f5eedd05SDouglas Gregor 
412b82c2a5SDouglas Gregor   // Find the starting module.
422b82c2a5SDouglas Gregor   Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
432b82c2a5SDouglas Gregor   if (!Context) {
442b82c2a5SDouglas Gregor     if (Complain)
452b82c2a5SDouglas Gregor       Diags->Report(Unresolved.Id[0].second,
462b82c2a5SDouglas Gregor                     diag::err_mmap_missing_module_unqualified)
472b82c2a5SDouglas Gregor         << Unresolved.Id[0].first << Mod->getFullModuleName();
482b82c2a5SDouglas Gregor 
492b82c2a5SDouglas Gregor     return Module::ExportDecl();
502b82c2a5SDouglas Gregor   }
512b82c2a5SDouglas Gregor 
522b82c2a5SDouglas Gregor   // Dig into the module path.
532b82c2a5SDouglas Gregor   for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
542b82c2a5SDouglas Gregor     Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
552b82c2a5SDouglas Gregor                                         Context);
562b82c2a5SDouglas Gregor     if (!Sub) {
572b82c2a5SDouglas Gregor       if (Complain)
582b82c2a5SDouglas Gregor         Diags->Report(Unresolved.Id[I].second,
592b82c2a5SDouglas Gregor                       diag::err_mmap_missing_module_qualified)
602b82c2a5SDouglas Gregor           << Unresolved.Id[I].first << Context->getFullModuleName()
612b82c2a5SDouglas Gregor           << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
622b82c2a5SDouglas Gregor 
632b82c2a5SDouglas Gregor       return Module::ExportDecl();
642b82c2a5SDouglas Gregor     }
652b82c2a5SDouglas Gregor 
662b82c2a5SDouglas Gregor     Context = Sub;
672b82c2a5SDouglas Gregor   }
682b82c2a5SDouglas Gregor 
692b82c2a5SDouglas Gregor   return Module::ExportDecl(Context, Unresolved.Wildcard);
702b82c2a5SDouglas Gregor }
712b82c2a5SDouglas Gregor 
72718292f2SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC) {
73718292f2SDouglas Gregor   llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
74718292f2SDouglas Gregor   Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>(
75718292f2SDouglas Gregor             new DiagnosticsEngine(DiagIDs));
76718292f2SDouglas Gregor   Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
77718292f2SDouglas Gregor   SourceMgr = new SourceManager(*Diags, FileMgr);
78718292f2SDouglas Gregor }
79718292f2SDouglas Gregor 
80718292f2SDouglas Gregor ModuleMap::~ModuleMap() {
815acdf59eSDouglas Gregor   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
825acdf59eSDouglas Gregor                                         IEnd = Modules.end();
835acdf59eSDouglas Gregor        I != IEnd; ++I) {
845acdf59eSDouglas Gregor     delete I->getValue();
855acdf59eSDouglas Gregor   }
865acdf59eSDouglas Gregor 
87718292f2SDouglas Gregor   delete SourceMgr;
88718292f2SDouglas Gregor }
89718292f2SDouglas Gregor 
90de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
91ab0c8a84SDouglas Gregor   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
92ab0c8a84SDouglas Gregor     = Headers.find(File);
93ab0c8a84SDouglas Gregor   if (Known != Headers.end())
94ab0c8a84SDouglas Gregor     return Known->second;
95ab0c8a84SDouglas Gregor 
96b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
97b65dbfffSDouglas Gregor   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
98b65dbfffSDouglas Gregor   StringRef DirName = Dir->getName();
99a89c5ac4SDouglas Gregor 
100a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
101a89c5ac4SDouglas Gregor   // an umbrella header.
102b65dbfffSDouglas Gregor   do {
103a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
104a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
105a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
106a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
107930a85ccSDouglas Gregor 
108930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
109930a85ccSDouglas Gregor       // header.
110930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
111930a85ccSDouglas Gregor       while (!UmbrellaModule->UmbrellaHeader && UmbrellaModule->Parent)
112930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
113930a85ccSDouglas Gregor 
114930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
115a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
116a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
117a89c5ac4SDouglas Gregor         // the actual header is located.
118a89c5ac4SDouglas Gregor 
119a89c5ac4SDouglas Gregor         // For a framework module, the umbrella directory is the framework
120a89c5ac4SDouglas Gregor         // directory, so strip off the "Headers" or "PrivateHeaders".
121a89c5ac4SDouglas Gregor         // FIXME: Should we tack on an "explicit" for PrivateHeaders? That
122a89c5ac4SDouglas Gregor         // might be what we want, but it feels like a hack.
123a89c5ac4SDouglas Gregor         unsigned LastSkippedDir = SkippedDirs.size();
124930a85ccSDouglas Gregor         if (LastSkippedDir && UmbrellaModule->IsFramework)
125a89c5ac4SDouglas Gregor           --LastSkippedDir;
126a89c5ac4SDouglas Gregor 
127a89c5ac4SDouglas Gregor         for (unsigned I = LastSkippedDir; I != 0; --I) {
128a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
129a89c5ac4SDouglas Gregor           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
130a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
131930a85ccSDouglas Gregor                                       UmbrellaModule->InferExplicitSubmodules).first;
132a89c5ac4SDouglas Gregor 
133a89c5ac4SDouglas Gregor           // Associate the module and the directory.
134a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
135a89c5ac4SDouglas Gregor 
136a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
137a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
138930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
139a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
140a89c5ac4SDouglas Gregor         }
141a89c5ac4SDouglas Gregor 
142a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
143a89c5ac4SDouglas Gregor         StringRef Name = llvm::sys::path::stem(File->getName());
144a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
145930a85ccSDouglas Gregor                                     UmbrellaModule->InferExplicitSubmodules).first;
146a89c5ac4SDouglas Gregor 
147a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
148a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
149930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
150a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
151a89c5ac4SDouglas Gregor       } else {
152a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
153a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
154a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
155a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
156a89c5ac4SDouglas Gregor       }
157a89c5ac4SDouglas Gregor 
158a89c5ac4SDouglas Gregor       Headers[File] = Result;
159a89c5ac4SDouglas Gregor       return Result;
160a89c5ac4SDouglas Gregor     }
161a89c5ac4SDouglas Gregor 
162a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
163a89c5ac4SDouglas Gregor 
164b65dbfffSDouglas Gregor     // Retrieve our parent path.
165b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
166b65dbfffSDouglas Gregor     if (DirName.empty())
167b65dbfffSDouglas Gregor       break;
168b65dbfffSDouglas Gregor 
169b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
170b65dbfffSDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
171a89c5ac4SDouglas Gregor   } while (Dir);
172b65dbfffSDouglas Gregor 
173ab0c8a84SDouglas Gregor   return 0;
174ab0c8a84SDouglas Gregor }
175ab0c8a84SDouglas Gregor 
176de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) {
17788bdfb0eSDouglas Gregor   llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
17888bdfb0eSDouglas Gregor   if (Known != Modules.end())
17988bdfb0eSDouglas Gregor     return Known->getValue();
18088bdfb0eSDouglas Gregor 
18188bdfb0eSDouglas Gregor   return 0;
18288bdfb0eSDouglas Gregor }
18388bdfb0eSDouglas Gregor 
1842b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
1852b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
1862b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
1872b82c2a5SDouglas Gregor       return Sub;
1882b82c2a5SDouglas Gregor   }
1892b82c2a5SDouglas Gregor 
1902b82c2a5SDouglas Gregor   return findModule(Name);
1912b82c2a5SDouglas Gregor }
1922b82c2a5SDouglas Gregor 
1932b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
1942b82c2a5SDouglas Gregor   if (!Context)
1952b82c2a5SDouglas Gregor     return findModule(Name);
1962b82c2a5SDouglas Gregor 
1972b82c2a5SDouglas Gregor   llvm::StringMap<Module *>::iterator Sub = Context->SubModules.find(Name);
1982b82c2a5SDouglas Gregor   if (Sub != Context->SubModules.end())
1992b82c2a5SDouglas Gregor     return Sub->getValue();
2002b82c2a5SDouglas Gregor 
2012b82c2a5SDouglas Gregor   return 0;
2022b82c2a5SDouglas Gregor }
2032b82c2a5SDouglas Gregor 
204de3ef502SDouglas Gregor std::pair<Module *, bool>
20569021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
20669021974SDouglas Gregor                               bool IsExplicit) {
20769021974SDouglas Gregor   // Try to find an existing module with this name.
20869021974SDouglas Gregor   if (Module *Found = Parent? Parent->SubModules[Name] : Modules[Name])
20969021974SDouglas Gregor     return std::make_pair(Found, false);
21069021974SDouglas Gregor 
21169021974SDouglas Gregor   // Create a new module with this name.
21269021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
21369021974SDouglas Gregor                               IsExplicit);
21469021974SDouglas Gregor   if (Parent)
21569021974SDouglas Gregor     Parent->SubModules[Name] = Result;
21669021974SDouglas Gregor   else
21769021974SDouglas Gregor     Modules[Name] = Result;
21869021974SDouglas Gregor   return std::make_pair(Result, true);
21969021974SDouglas Gregor }
22069021974SDouglas Gregor 
221de3ef502SDouglas Gregor Module *
22256c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
223*e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
224*e89dbc1dSDouglas Gregor                                 Module *Parent) {
22556c64013SDouglas Gregor   // Check whether we've already found this module.
226*e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
227*e89dbc1dSDouglas Gregor     return Mod;
228*e89dbc1dSDouglas Gregor 
229*e89dbc1dSDouglas Gregor   FileManager &FileMgr = SourceMgr->getFileManager();
23056c64013SDouglas Gregor 
23156c64013SDouglas Gregor   // Look for an umbrella header.
23256c64013SDouglas Gregor   llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
23356c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, "Headers");
23456c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
235*e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
23656c64013SDouglas Gregor 
23756c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
23856c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
23956c64013SDouglas Gregor   // idea.
24056c64013SDouglas Gregor   if (!UmbrellaHeader)
24156c64013SDouglas Gregor     return 0;
24256c64013SDouglas Gregor 
243*e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
244*e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
245*e89dbc1dSDouglas Gregor 
246*e89dbc1dSDouglas Gregor   if (Parent)
247*e89dbc1dSDouglas Gregor     Parent->SubModules[ModuleName] = Result;
248*e89dbc1dSDouglas Gregor   else
249*e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
250*e89dbc1dSDouglas Gregor 
251d8bd7537SDouglas Gregor   // umbrella "umbrella-header-name"
25256c64013SDouglas Gregor   Result->UmbrellaHeader = UmbrellaHeader;
25356c64013SDouglas Gregor   Headers[UmbrellaHeader] = Result;
25456c64013SDouglas Gregor   UmbrellaDirs[FrameworkDir] = Result;
255d8bd7537SDouglas Gregor 
256d8bd7537SDouglas Gregor   // export *
257d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
258d8bd7537SDouglas Gregor 
259a89c5ac4SDouglas Gregor   // module * { export * }
260a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
261a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
262a89c5ac4SDouglas Gregor 
263*e89dbc1dSDouglas Gregor   // Look for subframeworks.
264*e89dbc1dSDouglas Gregor   llvm::error_code EC;
265*e89dbc1dSDouglas Gregor   llvm::SmallString<128> SubframeworksDirName = StringRef(FrameworkDir->getName());
266*e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
267*e89dbc1dSDouglas Gregor   for (llvm::sys::fs::directory_iterator Dir(SubframeworksDirName.str(), EC),
268*e89dbc1dSDouglas Gregor                                          DirEnd;
269*e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
270*e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
271*e89dbc1dSDouglas Gregor       continue;
272f2161a70SDouglas Gregor 
273*e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
274*e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
275*e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
276*e89dbc1dSDouglas Gregor       inferFrameworkModule(llvm::sys::path::stem(Dir->path()), SubframeworkDir,
277*e89dbc1dSDouglas Gregor                            Result);
278*e89dbc1dSDouglas Gregor     }
279*e89dbc1dSDouglas Gregor   }
280*e89dbc1dSDouglas Gregor 
28156c64013SDouglas Gregor   return Result;
28256c64013SDouglas Gregor }
28356c64013SDouglas Gregor 
284a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
285a89c5ac4SDouglas Gregor   Headers[UmbrellaHeader] = Mod;
286a89c5ac4SDouglas Gregor   Mod->UmbrellaHeader = UmbrellaHeader;
287a89c5ac4SDouglas Gregor 
288a89c5ac4SDouglas Gregor   const DirectoryEntry *UmbrellaDir = UmbrellaHeader->getDir();
289a89c5ac4SDouglas Gregor   if (Mod->IsFramework)
290a89c5ac4SDouglas Gregor     UmbrellaDir = SourceMgr->getFileManager().getDirectory(
291a89c5ac4SDouglas Gregor                     llvm::sys::path::parent_path(UmbrellaDir->getName()));
292a89c5ac4SDouglas Gregor 
293a89c5ac4SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
294a89c5ac4SDouglas Gregor }
295a89c5ac4SDouglas Gregor 
296a89c5ac4SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
297a89c5ac4SDouglas Gregor   Mod->Headers.push_back(Header);
298a89c5ac4SDouglas Gregor   Headers[Header] = Mod;
299a89c5ac4SDouglas Gregor }
300a89c5ac4SDouglas Gregor 
301514b636aSDouglas Gregor const FileEntry *
302de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) {
303514b636aSDouglas Gregor   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
304514b636aSDouglas Gregor     return 0;
305514b636aSDouglas Gregor 
306514b636aSDouglas Gregor   return SourceMgr->getFileEntryForID(
307514b636aSDouglas Gregor            SourceMgr->getFileID(Module->DefinitionLoc));
308514b636aSDouglas Gregor }
309514b636aSDouglas Gregor 
310718292f2SDouglas Gregor void ModuleMap::dump() {
311718292f2SDouglas Gregor   llvm::errs() << "Modules:";
312718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
313718292f2SDouglas Gregor                                         MEnd = Modules.end();
314718292f2SDouglas Gregor        M != MEnd; ++M)
315d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
316718292f2SDouglas Gregor 
317718292f2SDouglas Gregor   llvm::errs() << "Headers:";
318718292f2SDouglas Gregor   for (llvm::DenseMap<const FileEntry *, Module *>::iterator
319718292f2SDouglas Gregor             H = Headers.begin(),
320718292f2SDouglas Gregor          HEnd = Headers.end();
321718292f2SDouglas Gregor        H != HEnd; ++H) {
322718292f2SDouglas Gregor     llvm::errs() << "  \"" << H->first->getName() << "\" -> "
323718292f2SDouglas Gregor                  << H->second->getFullModuleName() << "\n";
324718292f2SDouglas Gregor   }
325718292f2SDouglas Gregor }
326718292f2SDouglas Gregor 
3272b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
3282b82c2a5SDouglas Gregor   bool HadError = false;
3292b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
3302b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
3312b82c2a5SDouglas Gregor                                               Complain);
332f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
3332b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
3342b82c2a5SDouglas Gregor     else
3352b82c2a5SDouglas Gregor       HadError = true;
3362b82c2a5SDouglas Gregor   }
3372b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
3382b82c2a5SDouglas Gregor   return HadError;
3392b82c2a5SDouglas Gregor }
3402b82c2a5SDouglas Gregor 
3410093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
3420093b3c7SDouglas Gregor   if (Loc.isInvalid())
3430093b3c7SDouglas Gregor     return 0;
3440093b3c7SDouglas Gregor 
3450093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
3460093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
3470093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
3480093b3c7SDouglas Gregor     return 0;
3490093b3c7SDouglas Gregor 
3500093b3c7SDouglas Gregor 
3510093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
3520093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
3530093b3c7SDouglas Gregor   const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID);
3540093b3c7SDouglas Gregor   if (!ExpansionFile)
3550093b3c7SDouglas Gregor     return 0;
3560093b3c7SDouglas Gregor 
3570093b3c7SDouglas Gregor   // Find the module that owns this header.
3580093b3c7SDouglas Gregor   return findModuleForHeader(ExpansionFile);
3590093b3c7SDouglas Gregor }
3600093b3c7SDouglas Gregor 
361718292f2SDouglas Gregor //----------------------------------------------------------------------------//
362718292f2SDouglas Gregor // Module map file parser
363718292f2SDouglas Gregor //----------------------------------------------------------------------------//
364718292f2SDouglas Gregor 
365718292f2SDouglas Gregor namespace clang {
366718292f2SDouglas Gregor   /// \brief A token in a module map file.
367718292f2SDouglas Gregor   struct MMToken {
368718292f2SDouglas Gregor     enum TokenKind {
369718292f2SDouglas Gregor       EndOfFile,
370718292f2SDouglas Gregor       HeaderKeyword,
371718292f2SDouglas Gregor       Identifier,
372718292f2SDouglas Gregor       ExplicitKeyword,
3732b82c2a5SDouglas Gregor       ExportKeyword,
374755b2055SDouglas Gregor       FrameworkKeyword,
375718292f2SDouglas Gregor       ModuleKeyword,
3762b82c2a5SDouglas Gregor       Period,
377718292f2SDouglas Gregor       UmbrellaKeyword,
3782b82c2a5SDouglas Gregor       Star,
379718292f2SDouglas Gregor       StringLiteral,
380718292f2SDouglas Gregor       LBrace,
381718292f2SDouglas Gregor       RBrace
382718292f2SDouglas Gregor     } Kind;
383718292f2SDouglas Gregor 
384718292f2SDouglas Gregor     unsigned Location;
385718292f2SDouglas Gregor     unsigned StringLength;
386718292f2SDouglas Gregor     const char *StringData;
387718292f2SDouglas Gregor 
388718292f2SDouglas Gregor     void clear() {
389718292f2SDouglas Gregor       Kind = EndOfFile;
390718292f2SDouglas Gregor       Location = 0;
391718292f2SDouglas Gregor       StringLength = 0;
392718292f2SDouglas Gregor       StringData = 0;
393718292f2SDouglas Gregor     }
394718292f2SDouglas Gregor 
395718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
396718292f2SDouglas Gregor 
397718292f2SDouglas Gregor     SourceLocation getLocation() const {
398718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
399718292f2SDouglas Gregor     }
400718292f2SDouglas Gregor 
401718292f2SDouglas Gregor     StringRef getString() const {
402718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
403718292f2SDouglas Gregor     }
404718292f2SDouglas Gregor   };
405718292f2SDouglas Gregor 
406718292f2SDouglas Gregor   class ModuleMapParser {
407718292f2SDouglas Gregor     Lexer &L;
408718292f2SDouglas Gregor     SourceManager &SourceMgr;
409718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
410718292f2SDouglas Gregor     ModuleMap &Map;
411718292f2SDouglas Gregor 
4125257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
4135257fc63SDouglas Gregor     const DirectoryEntry *Directory;
4145257fc63SDouglas Gregor 
415718292f2SDouglas Gregor     /// \brief Whether an error occurred.
416718292f2SDouglas Gregor     bool HadError;
417718292f2SDouglas Gregor 
418718292f2SDouglas Gregor     /// \brief Default target information, used only for string literal
419718292f2SDouglas Gregor     /// parsing.
420718292f2SDouglas Gregor     TargetInfo *Target;
421718292f2SDouglas Gregor 
422718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
423718292f2SDouglas Gregor     /// during parsing.
424718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
425718292f2SDouglas Gregor 
426718292f2SDouglas Gregor     /// \brief The current token.
427718292f2SDouglas Gregor     MMToken Tok;
428718292f2SDouglas Gregor 
429718292f2SDouglas Gregor     /// \brief The active module.
430de3ef502SDouglas Gregor     Module *ActiveModule;
431718292f2SDouglas Gregor 
432718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
433718292f2SDouglas Gregor     SourceLocation consumeToken();
434718292f2SDouglas Gregor 
435718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
436718292f2SDouglas Gregor     /// (or the end of the file).
437718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
438718292f2SDouglas Gregor 
439718292f2SDouglas Gregor     void parseModuleDecl();
440718292f2SDouglas Gregor     void parseUmbrellaDecl();
441718292f2SDouglas Gregor     void parseHeaderDecl();
4422b82c2a5SDouglas Gregor     void parseExportDecl();
44373441091SDouglas Gregor     void parseInferredSubmoduleDecl(bool Explicit);
444718292f2SDouglas Gregor 
445718292f2SDouglas Gregor   public:
446718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
447718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
4485257fc63SDouglas Gregor                              ModuleMap &Map,
4495257fc63SDouglas Gregor                              const DirectoryEntry *Directory)
4505257fc63SDouglas Gregor       : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
4515257fc63SDouglas Gregor         Directory(Directory), HadError(false), ActiveModule(0)
452718292f2SDouglas Gregor     {
453718292f2SDouglas Gregor       TargetOptions TargetOpts;
454718292f2SDouglas Gregor       TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
455718292f2SDouglas Gregor       Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
456718292f2SDouglas Gregor 
457718292f2SDouglas Gregor       Tok.clear();
458718292f2SDouglas Gregor       consumeToken();
459718292f2SDouglas Gregor     }
460718292f2SDouglas Gregor 
461718292f2SDouglas Gregor     bool parseModuleMapFile();
462718292f2SDouglas Gregor   };
463718292f2SDouglas Gregor }
464718292f2SDouglas Gregor 
465718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
466718292f2SDouglas Gregor retry:
467718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
468718292f2SDouglas Gregor   Tok.clear();
469718292f2SDouglas Gregor 
470718292f2SDouglas Gregor   Token LToken;
471718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
472718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
473718292f2SDouglas Gregor   switch (LToken.getKind()) {
474718292f2SDouglas Gregor   case tok::raw_identifier:
475718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
476718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
477718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
478718292f2SDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
479718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
4802b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
481755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
482718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
483718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
484718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
485718292f2SDouglas Gregor     break;
486718292f2SDouglas Gregor 
487718292f2SDouglas Gregor   case tok::eof:
488718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
489718292f2SDouglas Gregor     break;
490718292f2SDouglas Gregor 
491718292f2SDouglas Gregor   case tok::l_brace:
492718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
493718292f2SDouglas Gregor     break;
494718292f2SDouglas Gregor 
4952b82c2a5SDouglas Gregor   case tok::period:
4962b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
4972b82c2a5SDouglas Gregor     break;
4982b82c2a5SDouglas Gregor 
499718292f2SDouglas Gregor   case tok::r_brace:
500718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
501718292f2SDouglas Gregor     break;
502718292f2SDouglas Gregor 
5032b82c2a5SDouglas Gregor   case tok::star:
5042b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
5052b82c2a5SDouglas Gregor     break;
5062b82c2a5SDouglas Gregor 
507718292f2SDouglas Gregor   case tok::string_literal: {
508718292f2SDouglas Gregor     // Parse the string literal.
509718292f2SDouglas Gregor     LangOptions LangOpts;
510718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
511718292f2SDouglas Gregor     if (StringLiteral.hadError)
512718292f2SDouglas Gregor       goto retry;
513718292f2SDouglas Gregor 
514718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
515718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
516718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
517718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
518718292f2SDouglas Gregor     Saved[Length] = 0;
519718292f2SDouglas Gregor 
520718292f2SDouglas Gregor     // Form the token.
521718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
522718292f2SDouglas Gregor     Tok.StringData = Saved;
523718292f2SDouglas Gregor     Tok.StringLength = Length;
524718292f2SDouglas Gregor     break;
525718292f2SDouglas Gregor   }
526718292f2SDouglas Gregor 
527718292f2SDouglas Gregor   case tok::comment:
528718292f2SDouglas Gregor     goto retry;
529718292f2SDouglas Gregor 
530718292f2SDouglas Gregor   default:
531718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
532718292f2SDouglas Gregor     HadError = true;
533718292f2SDouglas Gregor     goto retry;
534718292f2SDouglas Gregor   }
535718292f2SDouglas Gregor 
536718292f2SDouglas Gregor   return Result;
537718292f2SDouglas Gregor }
538718292f2SDouglas Gregor 
539718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
540718292f2SDouglas Gregor   unsigned braceDepth = 0;
541718292f2SDouglas Gregor   do {
542718292f2SDouglas Gregor     switch (Tok.Kind) {
543718292f2SDouglas Gregor     case MMToken::EndOfFile:
544718292f2SDouglas Gregor       return;
545718292f2SDouglas Gregor 
546718292f2SDouglas Gregor     case MMToken::LBrace:
547718292f2SDouglas Gregor       if (Tok.is(K) && braceDepth == 0)
548718292f2SDouglas Gregor         return;
549718292f2SDouglas Gregor 
550718292f2SDouglas Gregor       ++braceDepth;
551718292f2SDouglas Gregor       break;
552718292f2SDouglas Gregor 
553718292f2SDouglas Gregor     case MMToken::RBrace:
554718292f2SDouglas Gregor       if (braceDepth > 0)
555718292f2SDouglas Gregor         --braceDepth;
556718292f2SDouglas Gregor       else if (Tok.is(K))
557718292f2SDouglas Gregor         return;
558718292f2SDouglas Gregor       break;
559718292f2SDouglas Gregor 
560718292f2SDouglas Gregor     default:
561718292f2SDouglas Gregor       if (braceDepth == 0 && Tok.is(K))
562718292f2SDouglas Gregor         return;
563718292f2SDouglas Gregor       break;
564718292f2SDouglas Gregor     }
565718292f2SDouglas Gregor 
566718292f2SDouglas Gregor    consumeToken();
567718292f2SDouglas Gregor   } while (true);
568718292f2SDouglas Gregor }
569718292f2SDouglas Gregor 
570718292f2SDouglas Gregor /// \brief Parse a module declaration.
571718292f2SDouglas Gregor ///
572718292f2SDouglas Gregor ///   module-declaration:
573755b2055SDouglas Gregor ///     'framework'[opt] 'module' identifier { module-member* }
574718292f2SDouglas Gregor ///
575718292f2SDouglas Gregor ///   module-member:
576718292f2SDouglas Gregor ///     umbrella-declaration
577718292f2SDouglas Gregor ///     header-declaration
57873441091SDouglas Gregor ///     'explicit'[opt] submodule-declaration
5792b82c2a5SDouglas Gregor ///     export-declaration
58073441091SDouglas Gregor ///
58173441091SDouglas Gregor ///   submodule-declaration:
58273441091SDouglas Gregor ///     module-declaration
58373441091SDouglas Gregor ///     inferred-submodule-declaration
584718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
585755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
586755b2055SDouglas Gregor          Tok.is(MMToken::FrameworkKeyword));
587718292f2SDouglas Gregor 
588f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
589718292f2SDouglas Gregor   bool Explicit = false;
590f2161a70SDouglas Gregor   bool Framework = false;
591755b2055SDouglas Gregor 
592f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
593f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
594f2161a70SDouglas Gregor     consumeToken();
595f2161a70SDouglas Gregor     Explicit = true;
596f2161a70SDouglas Gregor   }
597f2161a70SDouglas Gregor 
598f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
599755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
600755b2055SDouglas Gregor     consumeToken();
601755b2055SDouglas Gregor     Framework = true;
602755b2055SDouglas Gregor   }
603718292f2SDouglas Gregor 
604718292f2SDouglas Gregor   // Parse 'module' keyword.
605718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
606718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(),
607718292f2SDouglas Gregor                  diag::err_mmap_expected_module_after_explicit);
608718292f2SDouglas Gregor     consumeToken();
609718292f2SDouglas Gregor     HadError = true;
610718292f2SDouglas Gregor     return;
611718292f2SDouglas Gregor   }
612718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
613718292f2SDouglas Gregor 
61473441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
61573441091SDouglas Gregor   // Parse it.
61673441091SDouglas Gregor   if (Tok.is(MMToken::Star))
61773441091SDouglas Gregor     return parseInferredSubmoduleDecl(Explicit);
61873441091SDouglas Gregor 
619718292f2SDouglas Gregor   // Parse the module name.
620718292f2SDouglas Gregor   if (!Tok.is(MMToken::Identifier)) {
621718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
622718292f2SDouglas Gregor     HadError = true;
623718292f2SDouglas Gregor     return;
624718292f2SDouglas Gregor   }
625718292f2SDouglas Gregor   StringRef ModuleName = Tok.getString();
626718292f2SDouglas Gregor   SourceLocation ModuleNameLoc = consumeToken();
627718292f2SDouglas Gregor 
628718292f2SDouglas Gregor   // Parse the opening brace.
629718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
630718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
631718292f2SDouglas Gregor       << ModuleName;
632718292f2SDouglas Gregor     HadError = true;
633718292f2SDouglas Gregor     return;
634718292f2SDouglas Gregor   }
635718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
636718292f2SDouglas Gregor 
637718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
638718292f2SDouglas Gregor   llvm::StringMap<Module *> &ModuleSpace
639718292f2SDouglas Gregor     = ActiveModule? ActiveModule->SubModules : Map.Modules;
640718292f2SDouglas Gregor   llvm::StringMap<Module *>::iterator ExistingModule
641718292f2SDouglas Gregor     = ModuleSpace.find(ModuleName);
642718292f2SDouglas Gregor   if (ExistingModule != ModuleSpace.end()) {
643718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
644718292f2SDouglas Gregor       << ModuleName;
645718292f2SDouglas Gregor     Diags.Report(ExistingModule->getValue()->DefinitionLoc,
646718292f2SDouglas Gregor                  diag::note_mmap_prev_definition);
647718292f2SDouglas Gregor 
648718292f2SDouglas Gregor     // Skip the module definition.
649718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
650718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
651718292f2SDouglas Gregor       consumeToken();
652718292f2SDouglas Gregor 
653718292f2SDouglas Gregor     HadError = true;
654718292f2SDouglas Gregor     return;
655718292f2SDouglas Gregor   }
656718292f2SDouglas Gregor 
657718292f2SDouglas Gregor   // Start defining this module.
658755b2055SDouglas Gregor   ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Framework,
659755b2055SDouglas Gregor                             Explicit);
660718292f2SDouglas Gregor   ModuleSpace[ModuleName] = ActiveModule;
661718292f2SDouglas Gregor 
662718292f2SDouglas Gregor   bool Done = false;
663718292f2SDouglas Gregor   do {
664718292f2SDouglas Gregor     switch (Tok.Kind) {
665718292f2SDouglas Gregor     case MMToken::EndOfFile:
666718292f2SDouglas Gregor     case MMToken::RBrace:
667718292f2SDouglas Gregor       Done = true;
668718292f2SDouglas Gregor       break;
669718292f2SDouglas Gregor 
670718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
671f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
672718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
673718292f2SDouglas Gregor       parseModuleDecl();
674718292f2SDouglas Gregor       break;
675718292f2SDouglas Gregor 
6762b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
6772b82c2a5SDouglas Gregor       parseExportDecl();
6782b82c2a5SDouglas Gregor       break;
6792b82c2a5SDouglas Gregor 
680718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
681718292f2SDouglas Gregor       parseHeaderDecl();
682718292f2SDouglas Gregor       break;
683718292f2SDouglas Gregor 
684718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
685718292f2SDouglas Gregor       parseUmbrellaDecl();
686718292f2SDouglas Gregor       break;
687718292f2SDouglas Gregor 
688718292f2SDouglas Gregor     default:
689718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
690718292f2SDouglas Gregor       consumeToken();
691718292f2SDouglas Gregor       break;
692718292f2SDouglas Gregor     }
693718292f2SDouglas Gregor   } while (!Done);
694718292f2SDouglas Gregor 
695718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
696718292f2SDouglas Gregor     consumeToken();
697718292f2SDouglas Gregor   else {
698718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
699718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
700718292f2SDouglas Gregor     HadError = true;
701718292f2SDouglas Gregor   }
702718292f2SDouglas Gregor 
703718292f2SDouglas Gregor   // We're done parsing this module. Pop back to our parent scope.
704718292f2SDouglas Gregor   ActiveModule = ActiveModule->Parent;
705718292f2SDouglas Gregor }
706718292f2SDouglas Gregor 
707f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
708f2161a70SDouglas Gregor /// subframework in which the given module lives.
709f2161a70SDouglas Gregor void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) {
710f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
711f2161a70SDouglas Gregor   llvm::SmallVector<StringRef, 2> Paths;
712f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
713f2161a70SDouglas Gregor     if (Mod->IsFramework)
714f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
715f2161a70SDouglas Gregor   }
716f2161a70SDouglas Gregor 
717f2161a70SDouglas Gregor   if (Paths.empty())
718f2161a70SDouglas Gregor     return;
719f2161a70SDouglas Gregor 
720f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
721f2161a70SDouglas Gregor   for (unsigned I = Paths.size() - 1; I != 0; --I) {
722f2161a70SDouglas Gregor     llvm::sys::path::append(Path, "Frameworks");
723f2161a70SDouglas Gregor     llvm::sys::path::append(Path, Paths[I-1] + ".framework");
724f2161a70SDouglas Gregor   }
725f2161a70SDouglas Gregor }
726f2161a70SDouglas Gregor 
727718292f2SDouglas Gregor /// \brief Parse an umbrella header declaration.
728718292f2SDouglas Gregor ///
729718292f2SDouglas Gregor ///   umbrella-declaration:
730718292f2SDouglas Gregor ///     'umbrella' string-literal
731718292f2SDouglas Gregor void ModuleMapParser::parseUmbrellaDecl() {
732718292f2SDouglas Gregor   assert(Tok.is(MMToken::UmbrellaKeyword));
733718292f2SDouglas Gregor   SourceLocation UmbrellaLoc = consumeToken();
734718292f2SDouglas Gregor 
735718292f2SDouglas Gregor   // Parse the header name.
736718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
737718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
738718292f2SDouglas Gregor       << "umbrella";
739718292f2SDouglas Gregor     HadError = true;
740718292f2SDouglas Gregor     return;
741718292f2SDouglas Gregor   }
742718292f2SDouglas Gregor   StringRef FileName = Tok.getString();
743718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
744718292f2SDouglas Gregor 
7455257fc63SDouglas Gregor   // Check whether we already have an umbrella header.
7465257fc63SDouglas Gregor   if (ActiveModule->UmbrellaHeader) {
7475257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_header_conflict)
7485257fc63SDouglas Gregor       << ActiveModule->getFullModuleName()
7495257fc63SDouglas Gregor       << ActiveModule->UmbrellaHeader->getName();
7505257fc63SDouglas Gregor     HadError = true;
7515257fc63SDouglas Gregor     return;
7525257fc63SDouglas Gregor   }
7535257fc63SDouglas Gregor 
7545257fc63SDouglas Gregor   // Look for this file.
7555257fc63SDouglas Gregor   llvm::SmallString<128> PathName;
756f545f67dSDouglas Gregor   const FileEntry *File = 0;
757f545f67dSDouglas Gregor 
758f545f67dSDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
759f545f67dSDouglas Gregor     PathName = FileName;
760f545f67dSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
761f545f67dSDouglas Gregor   } else {
762f545f67dSDouglas Gregor     // Search for the header file within the search directory.
7635257fc63SDouglas Gregor     PathName += Directory->getName();
764755b2055SDouglas Gregor     unsigned PathLength = PathName.size();
765f2161a70SDouglas Gregor 
766755b2055SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
767f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
768f2161a70SDouglas Gregor 
769755b2055SDouglas Gregor       // Check whether this file is in the public headers.
770755b2055SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
7715257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
772755b2055SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
773755b2055SDouglas Gregor 
774755b2055SDouglas Gregor       if (!File) {
775755b2055SDouglas Gregor         // Check whether this file is in the private headers.
776755b2055SDouglas Gregor         PathName.resize(PathLength);
777755b2055SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
778755b2055SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
779755b2055SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
780755b2055SDouglas Gregor       }
781755b2055SDouglas Gregor     } else {
782755b2055SDouglas Gregor       // Lookup for normal headers.
783755b2055SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
784755b2055SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
785755b2055SDouglas Gregor     }
786f545f67dSDouglas Gregor   }
7875257fc63SDouglas Gregor 
7885257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
7895257fc63SDouglas Gregor   // Come up with a lazy way to do this.
790755b2055SDouglas Gregor   if (File) {
791a89c5ac4SDouglas Gregor     const DirectoryEntry *UmbrellaDir = File->getDir();
792a89c5ac4SDouglas Gregor     if (ActiveModule->IsFramework) {
793a89c5ac4SDouglas Gregor       // For framework modules, use the framework directory as the umbrella
794a89c5ac4SDouglas Gregor       // directory.
795a89c5ac4SDouglas Gregor       UmbrellaDir = SourceMgr.getFileManager().getDirectory(
796a89c5ac4SDouglas Gregor                       llvm::sys::path::parent_path(UmbrellaDir->getName()));
797a89c5ac4SDouglas Gregor     }
798a89c5ac4SDouglas Gregor 
7995257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
8005257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
8015257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
8025257fc63SDouglas Gregor       HadError = true;
803a89c5ac4SDouglas Gregor     } else if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
804b65dbfffSDouglas Gregor       Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
805b65dbfffSDouglas Gregor         << OwningModule->getFullModuleName();
806b65dbfffSDouglas Gregor       HadError = true;
8075257fc63SDouglas Gregor     } else {
8085257fc63SDouglas Gregor       // Record this umbrella header.
809a89c5ac4SDouglas Gregor       Map.setUmbrellaHeader(ActiveModule, File);
8105257fc63SDouglas Gregor     }
8115257fc63SDouglas Gregor   } else {
8125257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
8135257fc63SDouglas Gregor       << true << FileName;
8145257fc63SDouglas Gregor     HadError = true;
8155257fc63SDouglas Gregor   }
816718292f2SDouglas Gregor }
817718292f2SDouglas Gregor 
818718292f2SDouglas Gregor /// \brief Parse a header declaration.
819718292f2SDouglas Gregor ///
820718292f2SDouglas Gregor ///   header-declaration:
821718292f2SDouglas Gregor ///     'header' string-literal
822718292f2SDouglas Gregor void ModuleMapParser::parseHeaderDecl() {
823718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
8241871ed3dSBenjamin Kramer   consumeToken();
825718292f2SDouglas Gregor 
826718292f2SDouglas Gregor   // Parse the header name.
827718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
828718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
829718292f2SDouglas Gregor       << "header";
830718292f2SDouglas Gregor     HadError = true;
831718292f2SDouglas Gregor     return;
832718292f2SDouglas Gregor   }
833718292f2SDouglas Gregor   StringRef FileName = Tok.getString();
834718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
835718292f2SDouglas Gregor 
8365257fc63SDouglas Gregor   // Look for this file.
8375257fc63SDouglas Gregor   llvm::SmallString<128> PathName;
838f545f67dSDouglas Gregor   if (llvm::sys::path::is_relative(FileName)) {
839f545f67dSDouglas Gregor     // FIXME: Change this search to also look for private headers!
8405257fc63SDouglas Gregor     PathName += Directory->getName();
841755b2055SDouglas Gregor 
842f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
843f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
844755b2055SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
845f545f67dSDouglas Gregor     }
846f2161a70SDouglas Gregor   }
847755b2055SDouglas Gregor 
8485257fc63SDouglas Gregor   llvm::sys::path::append(PathName, FileName);
8495257fc63SDouglas Gregor 
8505257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
8515257fc63SDouglas Gregor   // Come up with a lazy way to do this.
8525257fc63SDouglas Gregor   if (const FileEntry *File = SourceMgr.getFileManager().getFile(PathName)) {
8535257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
8545257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
8555257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
8565257fc63SDouglas Gregor       HadError = true;
8575257fc63SDouglas Gregor     } else {
8585257fc63SDouglas Gregor       // Record this file.
859a89c5ac4SDouglas Gregor       Map.addHeader(ActiveModule, File);
8605257fc63SDouglas Gregor     }
8615257fc63SDouglas Gregor   } else {
8625257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
8635257fc63SDouglas Gregor       << false << FileName;
8645257fc63SDouglas Gregor     HadError = true;
8655257fc63SDouglas Gregor   }
866718292f2SDouglas Gregor }
867718292f2SDouglas Gregor 
8682b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
8692b82c2a5SDouglas Gregor ///
8702b82c2a5SDouglas Gregor ///   export-declaration:
8712b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
8722b82c2a5SDouglas Gregor ///
8732b82c2a5SDouglas Gregor ///   wildcard-module-id:
8742b82c2a5SDouglas Gregor ///     identifier
8752b82c2a5SDouglas Gregor ///     '*'
8762b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
8772b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
8782b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
8792b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
8802b82c2a5SDouglas Gregor 
8812b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
8822b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
8832b82c2a5SDouglas Gregor   bool Wildcard = false;
8842b82c2a5SDouglas Gregor   do {
8852b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
8862b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
8872b82c2a5SDouglas Gregor                                               Tok.getLocation()));
8882b82c2a5SDouglas Gregor       consumeToken();
8892b82c2a5SDouglas Gregor 
8902b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
8912b82c2a5SDouglas Gregor         consumeToken();
8922b82c2a5SDouglas Gregor         continue;
8932b82c2a5SDouglas Gregor       }
8942b82c2a5SDouglas Gregor 
8952b82c2a5SDouglas Gregor       break;
8962b82c2a5SDouglas Gregor     }
8972b82c2a5SDouglas Gregor 
8982b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
8992b82c2a5SDouglas Gregor       Wildcard = true;
900f5eedd05SDouglas Gregor       consumeToken();
9012b82c2a5SDouglas Gregor       break;
9022b82c2a5SDouglas Gregor     }
9032b82c2a5SDouglas Gregor 
9042b82c2a5SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
9052b82c2a5SDouglas Gregor     HadError = true;
9062b82c2a5SDouglas Gregor     return;
9072b82c2a5SDouglas Gregor   } while (true);
9082b82c2a5SDouglas Gregor 
9092b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
9102b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
9112b82c2a5SDouglas Gregor   };
9122b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
9132b82c2a5SDouglas Gregor }
9142b82c2a5SDouglas Gregor 
91573441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
91673441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
91773441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
91873441091SDouglas Gregor   bool Failed = false;
91973441091SDouglas Gregor 
92073441091SDouglas Gregor   // Inferred modules must be submodules.
92173441091SDouglas Gregor   if (!ActiveModule) {
92273441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
92373441091SDouglas Gregor     Failed = true;
92473441091SDouglas Gregor   }
92573441091SDouglas Gregor 
92673441091SDouglas Gregor   // Inferred modules must have umbrella headers.
927dd005f69SDouglas Gregor   if (!Failed && !ActiveModule->UmbrellaHeader) {
92873441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
92973441091SDouglas Gregor     Failed = true;
93073441091SDouglas Gregor   }
93173441091SDouglas Gregor 
93273441091SDouglas Gregor   // Check for redefinition of an inferred module.
933dd005f69SDouglas Gregor   if (!Failed && ActiveModule->InferSubmodules) {
93473441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
935dd005f69SDouglas Gregor     if (ActiveModule->InferredSubmoduleLoc.isValid())
936dd005f69SDouglas Gregor       Diags.Report(ActiveModule->InferredSubmoduleLoc,
93773441091SDouglas Gregor                    diag::note_mmap_prev_definition);
93873441091SDouglas Gregor     Failed = true;
93973441091SDouglas Gregor   }
94073441091SDouglas Gregor 
94173441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
94273441091SDouglas Gregor   if (Failed) {
94373441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
94473441091SDouglas Gregor       consumeToken();
94573441091SDouglas Gregor       skipUntil(MMToken::RBrace);
94673441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
94773441091SDouglas Gregor         consumeToken();
94873441091SDouglas Gregor     }
94973441091SDouglas Gregor     HadError = true;
95073441091SDouglas Gregor     return;
95173441091SDouglas Gregor   }
95273441091SDouglas Gregor 
95373441091SDouglas Gregor   // Note that we have an inferred submodule.
954dd005f69SDouglas Gregor   ActiveModule->InferSubmodules = true;
955dd005f69SDouglas Gregor   ActiveModule->InferredSubmoduleLoc = StarLoc;
956dd005f69SDouglas Gregor   ActiveModule->InferExplicitSubmodules = Explicit;
95773441091SDouglas Gregor 
95873441091SDouglas Gregor   // Parse the opening brace.
95973441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
96073441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
96173441091SDouglas Gregor     HadError = true;
96273441091SDouglas Gregor     return;
96373441091SDouglas Gregor   }
96473441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
96573441091SDouglas Gregor 
96673441091SDouglas Gregor   // Parse the body of the inferred submodule.
96773441091SDouglas Gregor   bool Done = false;
96873441091SDouglas Gregor   do {
96973441091SDouglas Gregor     switch (Tok.Kind) {
97073441091SDouglas Gregor     case MMToken::EndOfFile:
97173441091SDouglas Gregor     case MMToken::RBrace:
97273441091SDouglas Gregor       Done = true;
97373441091SDouglas Gregor       break;
97473441091SDouglas Gregor 
97573441091SDouglas Gregor     case MMToken::ExportKeyword: {
97673441091SDouglas Gregor       consumeToken();
97773441091SDouglas Gregor       if (Tok.is(MMToken::Star))
978dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
97973441091SDouglas Gregor       else
98073441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
98173441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
98273441091SDouglas Gregor       consumeToken();
98373441091SDouglas Gregor       break;
98473441091SDouglas Gregor     }
98573441091SDouglas Gregor 
98673441091SDouglas Gregor     case MMToken::ExplicitKeyword:
98773441091SDouglas Gregor     case MMToken::ModuleKeyword:
98873441091SDouglas Gregor     case MMToken::HeaderKeyword:
98973441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
99073441091SDouglas Gregor     default:
99173441091SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
99273441091SDouglas Gregor       consumeToken();
99373441091SDouglas Gregor       break;
99473441091SDouglas Gregor     }
99573441091SDouglas Gregor   } while (!Done);
99673441091SDouglas Gregor 
99773441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
99873441091SDouglas Gregor     consumeToken();
99973441091SDouglas Gregor   else {
100073441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
100173441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
100273441091SDouglas Gregor     HadError = true;
100373441091SDouglas Gregor   }
100473441091SDouglas Gregor }
100573441091SDouglas Gregor 
1006718292f2SDouglas Gregor /// \brief Parse a module map file.
1007718292f2SDouglas Gregor ///
1008718292f2SDouglas Gregor ///   module-map-file:
1009718292f2SDouglas Gregor ///     module-declaration*
1010718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
1011718292f2SDouglas Gregor   do {
1012718292f2SDouglas Gregor     switch (Tok.Kind) {
1013718292f2SDouglas Gregor     case MMToken::EndOfFile:
1014718292f2SDouglas Gregor       return HadError;
1015718292f2SDouglas Gregor 
1016718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1017755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
1018718292f2SDouglas Gregor       parseModuleDecl();
1019718292f2SDouglas Gregor       break;
1020718292f2SDouglas Gregor 
1021718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
10222b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
1023718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
1024718292f2SDouglas Gregor     case MMToken::Identifier:
1025718292f2SDouglas Gregor     case MMToken::LBrace:
10262b82c2a5SDouglas Gregor     case MMToken::Period:
1027718292f2SDouglas Gregor     case MMToken::RBrace:
10282b82c2a5SDouglas Gregor     case MMToken::Star:
1029718292f2SDouglas Gregor     case MMToken::StringLiteral:
1030718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
1031718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1032718292f2SDouglas Gregor       HadError = true;
1033718292f2SDouglas Gregor       consumeToken();
1034718292f2SDouglas Gregor       break;
1035718292f2SDouglas Gregor     }
1036718292f2SDouglas Gregor   } while (true);
1037718292f2SDouglas Gregor 
1038718292f2SDouglas Gregor   return HadError;
1039718292f2SDouglas Gregor }
1040718292f2SDouglas Gregor 
1041718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
1042718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1043718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1044718292f2SDouglas Gregor   if (!Buffer)
1045718292f2SDouglas Gregor     return true;
1046718292f2SDouglas Gregor 
1047718292f2SDouglas Gregor   // Parse this module map file.
1048718292f2SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts);
1049718292f2SDouglas Gregor   Diags->getClient()->BeginSourceFile(LangOpts);
10505257fc63SDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir());
1051718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
1052718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
1053718292f2SDouglas Gregor 
1054718292f2SDouglas Gregor   return Result;
1055718292f2SDouglas Gregor }
1056