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"
23718292f2SDouglas Gregor #include "llvm/Support/Host.h"
245257fc63SDouglas Gregor #include "llvm/Support/PathV2.h"
25718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h"
26718292f2SDouglas Gregor #include "llvm/ADT/StringRef.h"
27718292f2SDouglas Gregor #include "llvm/ADT/StringSwitch.h"
28718292f2SDouglas Gregor using namespace clang;
29718292f2SDouglas Gregor 
302b82c2a5SDouglas Gregor Module::ExportDecl
312b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod,
322b82c2a5SDouglas Gregor                          const Module::UnresolvedExportDecl &Unresolved,
332b82c2a5SDouglas Gregor                          bool Complain) {
34f5eedd05SDouglas Gregor   // We may have just a wildcard.
35f5eedd05SDouglas Gregor   if (Unresolved.Id.empty()) {
36f5eedd05SDouglas Gregor     assert(Unresolved.Wildcard && "Invalid unresolved export");
37f5eedd05SDouglas Gregor     return Module::ExportDecl(0, true);
38f5eedd05SDouglas Gregor   }
39f5eedd05SDouglas Gregor 
402b82c2a5SDouglas Gregor   // Find the starting module.
412b82c2a5SDouglas Gregor   Module *Context = lookupModuleUnqualified(Unresolved.Id[0].first, Mod);
422b82c2a5SDouglas Gregor   if (!Context) {
432b82c2a5SDouglas Gregor     if (Complain)
442b82c2a5SDouglas Gregor       Diags->Report(Unresolved.Id[0].second,
452b82c2a5SDouglas Gregor                     diag::err_mmap_missing_module_unqualified)
462b82c2a5SDouglas Gregor         << Unresolved.Id[0].first << Mod->getFullModuleName();
472b82c2a5SDouglas Gregor 
482b82c2a5SDouglas Gregor     return Module::ExportDecl();
492b82c2a5SDouglas Gregor   }
502b82c2a5SDouglas Gregor 
512b82c2a5SDouglas Gregor   // Dig into the module path.
522b82c2a5SDouglas Gregor   for (unsigned I = 1, N = Unresolved.Id.size(); I != N; ++I) {
532b82c2a5SDouglas Gregor     Module *Sub = lookupModuleQualified(Unresolved.Id[I].first,
542b82c2a5SDouglas Gregor                                         Context);
552b82c2a5SDouglas Gregor     if (!Sub) {
562b82c2a5SDouglas Gregor       if (Complain)
572b82c2a5SDouglas Gregor         Diags->Report(Unresolved.Id[I].second,
582b82c2a5SDouglas Gregor                       diag::err_mmap_missing_module_qualified)
592b82c2a5SDouglas Gregor           << Unresolved.Id[I].first << Context->getFullModuleName()
602b82c2a5SDouglas Gregor           << SourceRange(Unresolved.Id[0].second, Unresolved.Id[I-1].second);
612b82c2a5SDouglas Gregor 
622b82c2a5SDouglas Gregor       return Module::ExportDecl();
632b82c2a5SDouglas Gregor     }
642b82c2a5SDouglas Gregor 
652b82c2a5SDouglas Gregor     Context = Sub;
662b82c2a5SDouglas Gregor   }
672b82c2a5SDouglas Gregor 
682b82c2a5SDouglas Gregor   return Module::ExportDecl(Context, Unresolved.Wildcard);
692b82c2a5SDouglas Gregor }
702b82c2a5SDouglas Gregor 
71718292f2SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, const DiagnosticConsumer &DC) {
72718292f2SDouglas Gregor   llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
73718292f2SDouglas Gregor   Diags = llvm::IntrusiveRefCntPtr<DiagnosticsEngine>(
74718292f2SDouglas Gregor             new DiagnosticsEngine(DiagIDs));
75718292f2SDouglas Gregor   Diags->setClient(DC.clone(*Diags), /*ShouldOwnClient=*/true);
76718292f2SDouglas Gregor   SourceMgr = new SourceManager(*Diags, FileMgr);
77718292f2SDouglas Gregor }
78718292f2SDouglas Gregor 
79718292f2SDouglas Gregor ModuleMap::~ModuleMap() {
805acdf59eSDouglas Gregor   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
815acdf59eSDouglas Gregor                                         IEnd = Modules.end();
825acdf59eSDouglas Gregor        I != IEnd; ++I) {
835acdf59eSDouglas Gregor     delete I->getValue();
845acdf59eSDouglas Gregor   }
855acdf59eSDouglas Gregor 
86718292f2SDouglas Gregor   delete SourceMgr;
87718292f2SDouglas Gregor }
88718292f2SDouglas Gregor 
89de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
90ab0c8a84SDouglas Gregor   llvm::DenseMap<const FileEntry *, Module *>::iterator Known
91ab0c8a84SDouglas Gregor     = Headers.find(File);
92ab0c8a84SDouglas Gregor   if (Known != Headers.end())
93ab0c8a84SDouglas Gregor     return Known->second;
94ab0c8a84SDouglas Gregor 
95b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
96b65dbfffSDouglas Gregor   llvm::SmallVector<const DirectoryEntry *, 2> SkippedDirs;
97b65dbfffSDouglas Gregor   StringRef DirName = Dir->getName();
98a89c5ac4SDouglas Gregor 
99a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
100a89c5ac4SDouglas Gregor   // an umbrella header.
101b65dbfffSDouglas Gregor   do {
102a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
103a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
104a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
105a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
106*930a85ccSDouglas Gregor 
107*930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
108*930a85ccSDouglas Gregor       // header.
109*930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
110*930a85ccSDouglas Gregor       while (!UmbrellaModule->UmbrellaHeader && UmbrellaModule->Parent)
111*930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
112*930a85ccSDouglas Gregor 
113*930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
114a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
115a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
116a89c5ac4SDouglas Gregor         // the actual header is located.
117a89c5ac4SDouglas Gregor 
118a89c5ac4SDouglas Gregor         // For a framework module, the umbrella directory is the framework
119a89c5ac4SDouglas Gregor         // directory, so strip off the "Headers" or "PrivateHeaders".
120a89c5ac4SDouglas Gregor         // FIXME: Should we tack on an "explicit" for PrivateHeaders? That
121a89c5ac4SDouglas Gregor         // might be what we want, but it feels like a hack.
122a89c5ac4SDouglas Gregor         unsigned LastSkippedDir = SkippedDirs.size();
123*930a85ccSDouglas Gregor         if (LastSkippedDir && UmbrellaModule->IsFramework)
124a89c5ac4SDouglas Gregor           --LastSkippedDir;
125a89c5ac4SDouglas Gregor 
126a89c5ac4SDouglas Gregor         for (unsigned I = LastSkippedDir; I != 0; --I) {
127a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
128a89c5ac4SDouglas Gregor           StringRef Name = llvm::sys::path::stem(SkippedDirs[I-1]->getName());
129a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
130*930a85ccSDouglas Gregor                                       UmbrellaModule->InferExplicitSubmodules).first;
131a89c5ac4SDouglas Gregor 
132a89c5ac4SDouglas Gregor           // Associate the module and the directory.
133a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
134a89c5ac4SDouglas Gregor 
135a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
136a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
137*930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
138a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
139a89c5ac4SDouglas Gregor         }
140a89c5ac4SDouglas Gregor 
141a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
142a89c5ac4SDouglas Gregor         StringRef Name = llvm::sys::path::stem(File->getName());
143a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
144*930a85ccSDouglas Gregor                                     UmbrellaModule->InferExplicitSubmodules).first;
145a89c5ac4SDouglas Gregor 
146a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
147a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
148*930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
149a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
150a89c5ac4SDouglas Gregor       } else {
151a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
152a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
153a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
154a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
155a89c5ac4SDouglas Gregor       }
156a89c5ac4SDouglas Gregor 
157a89c5ac4SDouglas Gregor       Headers[File] = Result;
158a89c5ac4SDouglas Gregor       return Result;
159a89c5ac4SDouglas Gregor     }
160a89c5ac4SDouglas Gregor 
161a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
162a89c5ac4SDouglas Gregor 
163b65dbfffSDouglas Gregor     // Retrieve our parent path.
164b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
165b65dbfffSDouglas Gregor     if (DirName.empty())
166b65dbfffSDouglas Gregor       break;
167b65dbfffSDouglas Gregor 
168b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
169b65dbfffSDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
170a89c5ac4SDouglas Gregor   } while (Dir);
171b65dbfffSDouglas Gregor 
172ab0c8a84SDouglas Gregor   return 0;
173ab0c8a84SDouglas Gregor }
174ab0c8a84SDouglas Gregor 
175de3ef502SDouglas Gregor Module *ModuleMap::findModule(StringRef Name) {
17688bdfb0eSDouglas Gregor   llvm::StringMap<Module *>::iterator Known = Modules.find(Name);
17788bdfb0eSDouglas Gregor   if (Known != Modules.end())
17888bdfb0eSDouglas Gregor     return Known->getValue();
17988bdfb0eSDouglas Gregor 
18088bdfb0eSDouglas Gregor   return 0;
18188bdfb0eSDouglas Gregor }
18288bdfb0eSDouglas Gregor 
1832b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleUnqualified(StringRef Name, Module *Context) {
1842b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
1852b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
1862b82c2a5SDouglas Gregor       return Sub;
1872b82c2a5SDouglas Gregor   }
1882b82c2a5SDouglas Gregor 
1892b82c2a5SDouglas Gregor   return findModule(Name);
1902b82c2a5SDouglas Gregor }
1912b82c2a5SDouglas Gregor 
1922b82c2a5SDouglas Gregor Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) {
1932b82c2a5SDouglas Gregor   if (!Context)
1942b82c2a5SDouglas Gregor     return findModule(Name);
1952b82c2a5SDouglas Gregor 
1962b82c2a5SDouglas Gregor   llvm::StringMap<Module *>::iterator Sub = Context->SubModules.find(Name);
1972b82c2a5SDouglas Gregor   if (Sub != Context->SubModules.end())
1982b82c2a5SDouglas Gregor     return Sub->getValue();
1992b82c2a5SDouglas Gregor 
2002b82c2a5SDouglas Gregor   return 0;
2012b82c2a5SDouglas Gregor }
2022b82c2a5SDouglas Gregor 
203de3ef502SDouglas Gregor std::pair<Module *, bool>
20469021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
20569021974SDouglas Gregor                               bool IsExplicit) {
20669021974SDouglas Gregor   // Try to find an existing module with this name.
20769021974SDouglas Gregor   if (Module *Found = Parent? Parent->SubModules[Name] : Modules[Name])
20869021974SDouglas Gregor     return std::make_pair(Found, false);
20969021974SDouglas Gregor 
21069021974SDouglas Gregor   // Create a new module with this name.
21169021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
21269021974SDouglas Gregor                               IsExplicit);
21369021974SDouglas Gregor   if (Parent)
21469021974SDouglas Gregor     Parent->SubModules[Name] = Result;
21569021974SDouglas Gregor   else
21669021974SDouglas Gregor     Modules[Name] = Result;
21769021974SDouglas Gregor   return std::make_pair(Result, true);
21869021974SDouglas Gregor }
21969021974SDouglas Gregor 
220de3ef502SDouglas Gregor Module *
22156c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
22256c64013SDouglas Gregor                                 const DirectoryEntry *FrameworkDir) {
22356c64013SDouglas Gregor   // Check whether we've already found this module.
22456c64013SDouglas Gregor   if (Module *Module = findModule(ModuleName))
22556c64013SDouglas Gregor     return Module;
22656c64013SDouglas Gregor 
22756c64013SDouglas Gregor   // Look for an umbrella header.
22856c64013SDouglas Gregor   llvm::SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
22956c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, "Headers");
23056c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
23156c64013SDouglas Gregor   const FileEntry *UmbrellaHeader
23256c64013SDouglas Gregor     = SourceMgr->getFileManager().getFile(UmbrellaName);
23356c64013SDouglas Gregor 
23456c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
23556c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
23656c64013SDouglas Gregor   // idea.
23756c64013SDouglas Gregor   if (!UmbrellaHeader)
23856c64013SDouglas Gregor     return 0;
23956c64013SDouglas Gregor 
240755b2055SDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(),
241755b2055SDouglas Gregor                               /*IsFramework=*/true);
242d8bd7537SDouglas Gregor   // umbrella "umbrella-header-name"
24356c64013SDouglas Gregor   Result->UmbrellaHeader = UmbrellaHeader;
24456c64013SDouglas Gregor   Headers[UmbrellaHeader] = Result;
24556c64013SDouglas Gregor   UmbrellaDirs[FrameworkDir] = Result;
246d8bd7537SDouglas Gregor 
247d8bd7537SDouglas Gregor   // export *
248d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
249d8bd7537SDouglas Gregor 
250a89c5ac4SDouglas Gregor   // module * { export * }
251a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
252a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
253a89c5ac4SDouglas Gregor 
25456c64013SDouglas Gregor   Modules[ModuleName] = Result;
25556c64013SDouglas Gregor   return Result;
25656c64013SDouglas Gregor }
25756c64013SDouglas Gregor 
258a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
259a89c5ac4SDouglas Gregor   Headers[UmbrellaHeader] = Mod;
260a89c5ac4SDouglas Gregor   Mod->UmbrellaHeader = UmbrellaHeader;
261a89c5ac4SDouglas Gregor 
262a89c5ac4SDouglas Gregor   const DirectoryEntry *UmbrellaDir = UmbrellaHeader->getDir();
263a89c5ac4SDouglas Gregor   if (Mod->IsFramework)
264a89c5ac4SDouglas Gregor     UmbrellaDir = SourceMgr->getFileManager().getDirectory(
265a89c5ac4SDouglas Gregor                     llvm::sys::path::parent_path(UmbrellaDir->getName()));
266a89c5ac4SDouglas Gregor 
267a89c5ac4SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
268a89c5ac4SDouglas Gregor }
269a89c5ac4SDouglas Gregor 
270a89c5ac4SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) {
271a89c5ac4SDouglas Gregor   Mod->Headers.push_back(Header);
272a89c5ac4SDouglas Gregor   Headers[Header] = Mod;
273a89c5ac4SDouglas Gregor }
274a89c5ac4SDouglas Gregor 
275514b636aSDouglas Gregor const FileEntry *
276de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) {
277514b636aSDouglas Gregor   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
278514b636aSDouglas Gregor     return 0;
279514b636aSDouglas Gregor 
280514b636aSDouglas Gregor   return SourceMgr->getFileEntryForID(
281514b636aSDouglas Gregor            SourceMgr->getFileID(Module->DefinitionLoc));
282514b636aSDouglas Gregor }
283514b636aSDouglas Gregor 
284718292f2SDouglas Gregor void ModuleMap::dump() {
285718292f2SDouglas Gregor   llvm::errs() << "Modules:";
286718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
287718292f2SDouglas Gregor                                         MEnd = Modules.end();
288718292f2SDouglas Gregor        M != MEnd; ++M)
289d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
290718292f2SDouglas Gregor 
291718292f2SDouglas Gregor   llvm::errs() << "Headers:";
292718292f2SDouglas Gregor   for (llvm::DenseMap<const FileEntry *, Module *>::iterator
293718292f2SDouglas Gregor             H = Headers.begin(),
294718292f2SDouglas Gregor          HEnd = Headers.end();
295718292f2SDouglas Gregor        H != HEnd; ++H) {
296718292f2SDouglas Gregor     llvm::errs() << "  \"" << H->first->getName() << "\" -> "
297718292f2SDouglas Gregor                  << H->second->getFullModuleName() << "\n";
298718292f2SDouglas Gregor   }
299718292f2SDouglas Gregor }
300718292f2SDouglas Gregor 
3012b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
3022b82c2a5SDouglas Gregor   bool HadError = false;
3032b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
3042b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
3052b82c2a5SDouglas Gregor                                               Complain);
306f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
3072b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
3082b82c2a5SDouglas Gregor     else
3092b82c2a5SDouglas Gregor       HadError = true;
3102b82c2a5SDouglas Gregor   }
3112b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
3122b82c2a5SDouglas Gregor   return HadError;
3132b82c2a5SDouglas Gregor }
3142b82c2a5SDouglas Gregor 
3150093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
3160093b3c7SDouglas Gregor   if (Loc.isInvalid())
3170093b3c7SDouglas Gregor     return 0;
3180093b3c7SDouglas Gregor 
3190093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
3200093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
3210093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
3220093b3c7SDouglas Gregor     return 0;
3230093b3c7SDouglas Gregor 
3240093b3c7SDouglas Gregor 
3250093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
3260093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
3270093b3c7SDouglas Gregor   const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID);
3280093b3c7SDouglas Gregor   if (!ExpansionFile)
3290093b3c7SDouglas Gregor     return 0;
3300093b3c7SDouglas Gregor 
3310093b3c7SDouglas Gregor   // Find the module that owns this header.
3320093b3c7SDouglas Gregor   return findModuleForHeader(ExpansionFile);
3330093b3c7SDouglas Gregor }
3340093b3c7SDouglas Gregor 
335718292f2SDouglas Gregor //----------------------------------------------------------------------------//
336718292f2SDouglas Gregor // Module map file parser
337718292f2SDouglas Gregor //----------------------------------------------------------------------------//
338718292f2SDouglas Gregor 
339718292f2SDouglas Gregor namespace clang {
340718292f2SDouglas Gregor   /// \brief A token in a module map file.
341718292f2SDouglas Gregor   struct MMToken {
342718292f2SDouglas Gregor     enum TokenKind {
343718292f2SDouglas Gregor       EndOfFile,
344718292f2SDouglas Gregor       HeaderKeyword,
345718292f2SDouglas Gregor       Identifier,
346718292f2SDouglas Gregor       ExplicitKeyword,
3472b82c2a5SDouglas Gregor       ExportKeyword,
348755b2055SDouglas Gregor       FrameworkKeyword,
349718292f2SDouglas Gregor       ModuleKeyword,
3502b82c2a5SDouglas Gregor       Period,
351718292f2SDouglas Gregor       UmbrellaKeyword,
3522b82c2a5SDouglas Gregor       Star,
353718292f2SDouglas Gregor       StringLiteral,
354718292f2SDouglas Gregor       LBrace,
355718292f2SDouglas Gregor       RBrace
356718292f2SDouglas Gregor     } Kind;
357718292f2SDouglas Gregor 
358718292f2SDouglas Gregor     unsigned Location;
359718292f2SDouglas Gregor     unsigned StringLength;
360718292f2SDouglas Gregor     const char *StringData;
361718292f2SDouglas Gregor 
362718292f2SDouglas Gregor     void clear() {
363718292f2SDouglas Gregor       Kind = EndOfFile;
364718292f2SDouglas Gregor       Location = 0;
365718292f2SDouglas Gregor       StringLength = 0;
366718292f2SDouglas Gregor       StringData = 0;
367718292f2SDouglas Gregor     }
368718292f2SDouglas Gregor 
369718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
370718292f2SDouglas Gregor 
371718292f2SDouglas Gregor     SourceLocation getLocation() const {
372718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
373718292f2SDouglas Gregor     }
374718292f2SDouglas Gregor 
375718292f2SDouglas Gregor     StringRef getString() const {
376718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
377718292f2SDouglas Gregor     }
378718292f2SDouglas Gregor   };
379718292f2SDouglas Gregor 
380718292f2SDouglas Gregor   class ModuleMapParser {
381718292f2SDouglas Gregor     Lexer &L;
382718292f2SDouglas Gregor     SourceManager &SourceMgr;
383718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
384718292f2SDouglas Gregor     ModuleMap &Map;
385718292f2SDouglas Gregor 
3865257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
3875257fc63SDouglas Gregor     const DirectoryEntry *Directory;
3885257fc63SDouglas Gregor 
389718292f2SDouglas Gregor     /// \brief Whether an error occurred.
390718292f2SDouglas Gregor     bool HadError;
391718292f2SDouglas Gregor 
392718292f2SDouglas Gregor     /// \brief Default target information, used only for string literal
393718292f2SDouglas Gregor     /// parsing.
394718292f2SDouglas Gregor     TargetInfo *Target;
395718292f2SDouglas Gregor 
396718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
397718292f2SDouglas Gregor     /// during parsing.
398718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
399718292f2SDouglas Gregor 
400718292f2SDouglas Gregor     /// \brief The current token.
401718292f2SDouglas Gregor     MMToken Tok;
402718292f2SDouglas Gregor 
403718292f2SDouglas Gregor     /// \brief The active module.
404de3ef502SDouglas Gregor     Module *ActiveModule;
405718292f2SDouglas Gregor 
406718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
407718292f2SDouglas Gregor     SourceLocation consumeToken();
408718292f2SDouglas Gregor 
409718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
410718292f2SDouglas Gregor     /// (or the end of the file).
411718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
412718292f2SDouglas Gregor 
413718292f2SDouglas Gregor     void parseModuleDecl();
414718292f2SDouglas Gregor     void parseUmbrellaDecl();
415718292f2SDouglas Gregor     void parseHeaderDecl();
4162b82c2a5SDouglas Gregor     void parseExportDecl();
41773441091SDouglas Gregor     void parseInferredSubmoduleDecl(bool Explicit);
418718292f2SDouglas Gregor 
419718292f2SDouglas Gregor   public:
420718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
421718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
4225257fc63SDouglas Gregor                              ModuleMap &Map,
4235257fc63SDouglas Gregor                              const DirectoryEntry *Directory)
4245257fc63SDouglas Gregor       : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map),
4255257fc63SDouglas Gregor         Directory(Directory), HadError(false), ActiveModule(0)
426718292f2SDouglas Gregor     {
427718292f2SDouglas Gregor       TargetOptions TargetOpts;
428718292f2SDouglas Gregor       TargetOpts.Triple = llvm::sys::getDefaultTargetTriple();
429718292f2SDouglas Gregor       Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
430718292f2SDouglas Gregor 
431718292f2SDouglas Gregor       Tok.clear();
432718292f2SDouglas Gregor       consumeToken();
433718292f2SDouglas Gregor     }
434718292f2SDouglas Gregor 
435718292f2SDouglas Gregor     bool parseModuleMapFile();
436718292f2SDouglas Gregor   };
437718292f2SDouglas Gregor }
438718292f2SDouglas Gregor 
439718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
440718292f2SDouglas Gregor retry:
441718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
442718292f2SDouglas Gregor   Tok.clear();
443718292f2SDouglas Gregor 
444718292f2SDouglas Gregor   Token LToken;
445718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
446718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
447718292f2SDouglas Gregor   switch (LToken.getKind()) {
448718292f2SDouglas Gregor   case tok::raw_identifier:
449718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
450718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
451718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
452718292f2SDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
453718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
4542b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
455755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
456718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
457718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
458718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
459718292f2SDouglas Gregor     break;
460718292f2SDouglas Gregor 
461718292f2SDouglas Gregor   case tok::eof:
462718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
463718292f2SDouglas Gregor     break;
464718292f2SDouglas Gregor 
465718292f2SDouglas Gregor   case tok::l_brace:
466718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
467718292f2SDouglas Gregor     break;
468718292f2SDouglas Gregor 
4692b82c2a5SDouglas Gregor   case tok::period:
4702b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
4712b82c2a5SDouglas Gregor     break;
4722b82c2a5SDouglas Gregor 
473718292f2SDouglas Gregor   case tok::r_brace:
474718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
475718292f2SDouglas Gregor     break;
476718292f2SDouglas Gregor 
4772b82c2a5SDouglas Gregor   case tok::star:
4782b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
4792b82c2a5SDouglas Gregor     break;
4802b82c2a5SDouglas Gregor 
481718292f2SDouglas Gregor   case tok::string_literal: {
482718292f2SDouglas Gregor     // Parse the string literal.
483718292f2SDouglas Gregor     LangOptions LangOpts;
484718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
485718292f2SDouglas Gregor     if (StringLiteral.hadError)
486718292f2SDouglas Gregor       goto retry;
487718292f2SDouglas Gregor 
488718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
489718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
490718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
491718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
492718292f2SDouglas Gregor     Saved[Length] = 0;
493718292f2SDouglas Gregor 
494718292f2SDouglas Gregor     // Form the token.
495718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
496718292f2SDouglas Gregor     Tok.StringData = Saved;
497718292f2SDouglas Gregor     Tok.StringLength = Length;
498718292f2SDouglas Gregor     break;
499718292f2SDouglas Gregor   }
500718292f2SDouglas Gregor 
501718292f2SDouglas Gregor   case tok::comment:
502718292f2SDouglas Gregor     goto retry;
503718292f2SDouglas Gregor 
504718292f2SDouglas Gregor   default:
505718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
506718292f2SDouglas Gregor     HadError = true;
507718292f2SDouglas Gregor     goto retry;
508718292f2SDouglas Gregor   }
509718292f2SDouglas Gregor 
510718292f2SDouglas Gregor   return Result;
511718292f2SDouglas Gregor }
512718292f2SDouglas Gregor 
513718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
514718292f2SDouglas Gregor   unsigned braceDepth = 0;
515718292f2SDouglas Gregor   do {
516718292f2SDouglas Gregor     switch (Tok.Kind) {
517718292f2SDouglas Gregor     case MMToken::EndOfFile:
518718292f2SDouglas Gregor       return;
519718292f2SDouglas Gregor 
520718292f2SDouglas Gregor     case MMToken::LBrace:
521718292f2SDouglas Gregor       if (Tok.is(K) && braceDepth == 0)
522718292f2SDouglas Gregor         return;
523718292f2SDouglas Gregor 
524718292f2SDouglas Gregor       ++braceDepth;
525718292f2SDouglas Gregor       break;
526718292f2SDouglas Gregor 
527718292f2SDouglas Gregor     case MMToken::RBrace:
528718292f2SDouglas Gregor       if (braceDepth > 0)
529718292f2SDouglas Gregor         --braceDepth;
530718292f2SDouglas Gregor       else if (Tok.is(K))
531718292f2SDouglas Gregor         return;
532718292f2SDouglas Gregor       break;
533718292f2SDouglas Gregor 
534718292f2SDouglas Gregor     default:
535718292f2SDouglas Gregor       if (braceDepth == 0 && Tok.is(K))
536718292f2SDouglas Gregor         return;
537718292f2SDouglas Gregor       break;
538718292f2SDouglas Gregor     }
539718292f2SDouglas Gregor 
540718292f2SDouglas Gregor    consumeToken();
541718292f2SDouglas Gregor   } while (true);
542718292f2SDouglas Gregor }
543718292f2SDouglas Gregor 
544718292f2SDouglas Gregor /// \brief Parse a module declaration.
545718292f2SDouglas Gregor ///
546718292f2SDouglas Gregor ///   module-declaration:
547755b2055SDouglas Gregor ///     'framework'[opt] 'module' identifier { module-member* }
548718292f2SDouglas Gregor ///
549718292f2SDouglas Gregor ///   module-member:
550718292f2SDouglas Gregor ///     umbrella-declaration
551718292f2SDouglas Gregor ///     header-declaration
55273441091SDouglas Gregor ///     'explicit'[opt] submodule-declaration
5532b82c2a5SDouglas Gregor ///     export-declaration
55473441091SDouglas Gregor ///
55573441091SDouglas Gregor ///   submodule-declaration:
55673441091SDouglas Gregor ///     module-declaration
55773441091SDouglas Gregor ///     inferred-submodule-declaration
558718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
559755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
560755b2055SDouglas Gregor          Tok.is(MMToken::FrameworkKeyword));
561718292f2SDouglas Gregor 
562755b2055SDouglas Gregor   // Parse 'framework' or 'explicit' keyword, if present.
563755b2055SDouglas Gregor   bool Framework = false;
564718292f2SDouglas Gregor   bool Explicit = false;
565755b2055SDouglas Gregor 
566755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
567755b2055SDouglas Gregor     consumeToken();
568755b2055SDouglas Gregor     Framework = true;
569755b2055SDouglas Gregor   }
570755b2055SDouglas Gregor   // Parse 'explicit' keyword, if present.
571755b2055SDouglas Gregor   else if (Tok.is(MMToken::ExplicitKeyword)) {
572718292f2SDouglas Gregor     consumeToken();
573718292f2SDouglas Gregor     Explicit = true;
574718292f2SDouglas Gregor   }
575718292f2SDouglas Gregor 
576718292f2SDouglas Gregor   // Parse 'module' keyword.
577718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
578718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(),
579718292f2SDouglas Gregor                  diag::err_mmap_expected_module_after_explicit);
580718292f2SDouglas Gregor     consumeToken();
581718292f2SDouglas Gregor     HadError = true;
582718292f2SDouglas Gregor     return;
583718292f2SDouglas Gregor   }
584718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
585718292f2SDouglas Gregor 
58673441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
58773441091SDouglas Gregor   // Parse it.
58873441091SDouglas Gregor   if (Tok.is(MMToken::Star))
58973441091SDouglas Gregor     return parseInferredSubmoduleDecl(Explicit);
59073441091SDouglas Gregor 
591718292f2SDouglas Gregor   // Parse the module name.
592718292f2SDouglas Gregor   if (!Tok.is(MMToken::Identifier)) {
593718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
594718292f2SDouglas Gregor     HadError = true;
595718292f2SDouglas Gregor     return;
596718292f2SDouglas Gregor   }
597718292f2SDouglas Gregor   StringRef ModuleName = Tok.getString();
598718292f2SDouglas Gregor   SourceLocation ModuleNameLoc = consumeToken();
599718292f2SDouglas Gregor 
600718292f2SDouglas Gregor   // Parse the opening brace.
601718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
602718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
603718292f2SDouglas Gregor       << ModuleName;
604718292f2SDouglas Gregor     HadError = true;
605718292f2SDouglas Gregor     return;
606718292f2SDouglas Gregor   }
607718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
608718292f2SDouglas Gregor 
609718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
610718292f2SDouglas Gregor   llvm::StringMap<Module *> &ModuleSpace
611718292f2SDouglas Gregor     = ActiveModule? ActiveModule->SubModules : Map.Modules;
612718292f2SDouglas Gregor   llvm::StringMap<Module *>::iterator ExistingModule
613718292f2SDouglas Gregor     = ModuleSpace.find(ModuleName);
614718292f2SDouglas Gregor   if (ExistingModule != ModuleSpace.end()) {
615718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
616718292f2SDouglas Gregor       << ModuleName;
617718292f2SDouglas Gregor     Diags.Report(ExistingModule->getValue()->DefinitionLoc,
618718292f2SDouglas Gregor                  diag::note_mmap_prev_definition);
619718292f2SDouglas Gregor 
620718292f2SDouglas Gregor     // Skip the module definition.
621718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
622718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
623718292f2SDouglas Gregor       consumeToken();
624718292f2SDouglas Gregor 
625718292f2SDouglas Gregor     HadError = true;
626718292f2SDouglas Gregor     return;
627718292f2SDouglas Gregor   }
628718292f2SDouglas Gregor 
629718292f2SDouglas Gregor   // Start defining this module.
630755b2055SDouglas Gregor   ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Framework,
631755b2055SDouglas Gregor                             Explicit);
632718292f2SDouglas Gregor   ModuleSpace[ModuleName] = ActiveModule;
633718292f2SDouglas Gregor 
634718292f2SDouglas Gregor   bool Done = false;
635718292f2SDouglas Gregor   do {
636718292f2SDouglas Gregor     switch (Tok.Kind) {
637718292f2SDouglas Gregor     case MMToken::EndOfFile:
638718292f2SDouglas Gregor     case MMToken::RBrace:
639718292f2SDouglas Gregor       Done = true;
640718292f2SDouglas Gregor       break;
641718292f2SDouglas Gregor 
642718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
643718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
644718292f2SDouglas Gregor       parseModuleDecl();
645718292f2SDouglas Gregor       break;
646718292f2SDouglas Gregor 
6472b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
6482b82c2a5SDouglas Gregor       parseExportDecl();
6492b82c2a5SDouglas Gregor       break;
6502b82c2a5SDouglas Gregor 
651718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
652718292f2SDouglas Gregor       parseHeaderDecl();
653718292f2SDouglas Gregor       break;
654718292f2SDouglas Gregor 
655718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
656718292f2SDouglas Gregor       parseUmbrellaDecl();
657718292f2SDouglas Gregor       break;
658718292f2SDouglas Gregor 
659718292f2SDouglas Gregor     default:
660718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
661718292f2SDouglas Gregor       consumeToken();
662718292f2SDouglas Gregor       break;
663718292f2SDouglas Gregor     }
664718292f2SDouglas Gregor   } while (!Done);
665718292f2SDouglas Gregor 
666718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
667718292f2SDouglas Gregor     consumeToken();
668718292f2SDouglas Gregor   else {
669718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
670718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
671718292f2SDouglas Gregor     HadError = true;
672718292f2SDouglas Gregor   }
673718292f2SDouglas Gregor 
674718292f2SDouglas Gregor   // We're done parsing this module. Pop back to our parent scope.
675718292f2SDouglas Gregor   ActiveModule = ActiveModule->Parent;
676718292f2SDouglas Gregor }
677718292f2SDouglas Gregor 
678718292f2SDouglas Gregor /// \brief Parse an umbrella header declaration.
679718292f2SDouglas Gregor ///
680718292f2SDouglas Gregor ///   umbrella-declaration:
681718292f2SDouglas Gregor ///     'umbrella' string-literal
682718292f2SDouglas Gregor void ModuleMapParser::parseUmbrellaDecl() {
683718292f2SDouglas Gregor   assert(Tok.is(MMToken::UmbrellaKeyword));
684718292f2SDouglas Gregor   SourceLocation UmbrellaLoc = consumeToken();
685718292f2SDouglas Gregor 
686718292f2SDouglas Gregor   // Parse the header name.
687718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
688718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
689718292f2SDouglas Gregor       << "umbrella";
690718292f2SDouglas Gregor     HadError = true;
691718292f2SDouglas Gregor     return;
692718292f2SDouglas Gregor   }
693718292f2SDouglas Gregor   StringRef FileName = Tok.getString();
694718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
695718292f2SDouglas Gregor 
6965257fc63SDouglas Gregor   // Check whether we already have an umbrella header.
6975257fc63SDouglas Gregor   if (ActiveModule->UmbrellaHeader) {
6985257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_header_conflict)
6995257fc63SDouglas Gregor       << ActiveModule->getFullModuleName()
7005257fc63SDouglas Gregor       << ActiveModule->UmbrellaHeader->getName();
7015257fc63SDouglas Gregor     HadError = true;
7025257fc63SDouglas Gregor     return;
7035257fc63SDouglas Gregor   }
7045257fc63SDouglas Gregor 
7055257fc63SDouglas Gregor   // Only top-level modules can have umbrella headers.
7065257fc63SDouglas Gregor   if (ActiveModule->Parent) {
7075257fc63SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_header_submodule)
7085257fc63SDouglas Gregor       << ActiveModule->getFullModuleName();
7095257fc63SDouglas Gregor     HadError = true;
7105257fc63SDouglas Gregor     return;
7115257fc63SDouglas Gregor   }
7125257fc63SDouglas Gregor 
7135257fc63SDouglas Gregor   // Look for this file.
7145257fc63SDouglas Gregor   llvm::SmallString<128> PathName;
715f545f67dSDouglas Gregor   const FileEntry *File = 0;
716f545f67dSDouglas Gregor 
717f545f67dSDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
718f545f67dSDouglas Gregor     PathName = FileName;
719f545f67dSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
720f545f67dSDouglas Gregor   } else {
721f545f67dSDouglas Gregor     // Search for the header file within the search directory.
7225257fc63SDouglas Gregor     PathName += Directory->getName();
723755b2055SDouglas Gregor     unsigned PathLength = PathName.size();
724755b2055SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
725755b2055SDouglas Gregor       // Check whether this file is in the public headers.
726755b2055SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
7275257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
728755b2055SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
729755b2055SDouglas Gregor 
730755b2055SDouglas Gregor       if (!File) {
731755b2055SDouglas Gregor         // Check whether this file is in the private headers.
732755b2055SDouglas Gregor         PathName.resize(PathLength);
733755b2055SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
734755b2055SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
735755b2055SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
736755b2055SDouglas Gregor       }
737755b2055SDouglas Gregor 
738755b2055SDouglas Gregor       // FIXME: Deal with subframeworks.
739755b2055SDouglas Gregor     } else {
740755b2055SDouglas Gregor       // Lookup for normal headers.
741755b2055SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
742755b2055SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
743755b2055SDouglas Gregor     }
744f545f67dSDouglas Gregor   }
7455257fc63SDouglas Gregor 
7465257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
7475257fc63SDouglas Gregor   // Come up with a lazy way to do this.
748755b2055SDouglas Gregor   if (File) {
749a89c5ac4SDouglas Gregor     const DirectoryEntry *UmbrellaDir = File->getDir();
750a89c5ac4SDouglas Gregor     if (ActiveModule->IsFramework) {
751a89c5ac4SDouglas Gregor       // For framework modules, use the framework directory as the umbrella
752a89c5ac4SDouglas Gregor       // directory.
753a89c5ac4SDouglas Gregor       UmbrellaDir = SourceMgr.getFileManager().getDirectory(
754a89c5ac4SDouglas Gregor                       llvm::sys::path::parent_path(UmbrellaDir->getName()));
755a89c5ac4SDouglas Gregor     }
756a89c5ac4SDouglas Gregor 
7575257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
7585257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
7595257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
7605257fc63SDouglas Gregor       HadError = true;
761a89c5ac4SDouglas Gregor     } else if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) {
762b65dbfffSDouglas Gregor       Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
763b65dbfffSDouglas Gregor         << OwningModule->getFullModuleName();
764b65dbfffSDouglas Gregor       HadError = true;
7655257fc63SDouglas Gregor     } else {
7665257fc63SDouglas Gregor       // Record this umbrella header.
767a89c5ac4SDouglas Gregor       Map.setUmbrellaHeader(ActiveModule, File);
7685257fc63SDouglas Gregor     }
7695257fc63SDouglas Gregor   } else {
7705257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
7715257fc63SDouglas Gregor       << true << FileName;
7725257fc63SDouglas Gregor     HadError = true;
7735257fc63SDouglas Gregor   }
774718292f2SDouglas Gregor }
775718292f2SDouglas Gregor 
776718292f2SDouglas Gregor /// \brief Parse a header declaration.
777718292f2SDouglas Gregor ///
778718292f2SDouglas Gregor ///   header-declaration:
779718292f2SDouglas Gregor ///     'header' string-literal
780718292f2SDouglas Gregor void ModuleMapParser::parseHeaderDecl() {
781718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
7821871ed3dSBenjamin Kramer   consumeToken();
783718292f2SDouglas Gregor 
784718292f2SDouglas Gregor   // Parse the header name.
785718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
786718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
787718292f2SDouglas Gregor       << "header";
788718292f2SDouglas Gregor     HadError = true;
789718292f2SDouglas Gregor     return;
790718292f2SDouglas Gregor   }
791718292f2SDouglas Gregor   StringRef FileName = Tok.getString();
792718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
793718292f2SDouglas Gregor 
7945257fc63SDouglas Gregor   // Look for this file.
7955257fc63SDouglas Gregor   llvm::SmallString<128> PathName;
796f545f67dSDouglas Gregor   if (llvm::sys::path::is_relative(FileName)) {
797f545f67dSDouglas Gregor     // FIXME: Change this search to also look for private headers!
7985257fc63SDouglas Gregor     PathName += Directory->getName();
799755b2055SDouglas Gregor 
800755b2055SDouglas Gregor     if (ActiveModule->isPartOfFramework())
801755b2055SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
802f545f67dSDouglas Gregor   }
803755b2055SDouglas Gregor 
8045257fc63SDouglas Gregor   llvm::sys::path::append(PathName, FileName);
8055257fc63SDouglas Gregor 
8065257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
8075257fc63SDouglas Gregor   // Come up with a lazy way to do this.
8085257fc63SDouglas Gregor   if (const FileEntry *File = SourceMgr.getFileManager().getFile(PathName)) {
8095257fc63SDouglas Gregor     if (const Module *OwningModule = Map.Headers[File]) {
8105257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
8115257fc63SDouglas Gregor         << FileName << OwningModule->getFullModuleName();
8125257fc63SDouglas Gregor       HadError = true;
8135257fc63SDouglas Gregor     } else {
8145257fc63SDouglas Gregor       // Record this file.
815a89c5ac4SDouglas Gregor       Map.addHeader(ActiveModule, File);
8165257fc63SDouglas Gregor     }
8175257fc63SDouglas Gregor   } else {
8185257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
8195257fc63SDouglas Gregor       << false << FileName;
8205257fc63SDouglas Gregor     HadError = true;
8215257fc63SDouglas Gregor   }
822718292f2SDouglas Gregor }
823718292f2SDouglas Gregor 
8242b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
8252b82c2a5SDouglas Gregor ///
8262b82c2a5SDouglas Gregor ///   export-declaration:
8272b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
8282b82c2a5SDouglas Gregor ///
8292b82c2a5SDouglas Gregor ///   wildcard-module-id:
8302b82c2a5SDouglas Gregor ///     identifier
8312b82c2a5SDouglas Gregor ///     '*'
8322b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
8332b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
8342b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
8352b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
8362b82c2a5SDouglas Gregor 
8372b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
8382b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
8392b82c2a5SDouglas Gregor   bool Wildcard = false;
8402b82c2a5SDouglas Gregor   do {
8412b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
8422b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
8432b82c2a5SDouglas Gregor                                               Tok.getLocation()));
8442b82c2a5SDouglas Gregor       consumeToken();
8452b82c2a5SDouglas Gregor 
8462b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
8472b82c2a5SDouglas Gregor         consumeToken();
8482b82c2a5SDouglas Gregor         continue;
8492b82c2a5SDouglas Gregor       }
8502b82c2a5SDouglas Gregor 
8512b82c2a5SDouglas Gregor       break;
8522b82c2a5SDouglas Gregor     }
8532b82c2a5SDouglas Gregor 
8542b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
8552b82c2a5SDouglas Gregor       Wildcard = true;
856f5eedd05SDouglas Gregor       consumeToken();
8572b82c2a5SDouglas Gregor       break;
8582b82c2a5SDouglas Gregor     }
8592b82c2a5SDouglas Gregor 
8602b82c2a5SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
8612b82c2a5SDouglas Gregor     HadError = true;
8622b82c2a5SDouglas Gregor     return;
8632b82c2a5SDouglas Gregor   } while (true);
8642b82c2a5SDouglas Gregor 
8652b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
8662b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
8672b82c2a5SDouglas Gregor   };
8682b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
8692b82c2a5SDouglas Gregor }
8702b82c2a5SDouglas Gregor 
87173441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) {
87273441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
87373441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
87473441091SDouglas Gregor   bool Failed = false;
87573441091SDouglas Gregor 
87673441091SDouglas Gregor   // Inferred modules must be submodules.
87773441091SDouglas Gregor   if (!ActiveModule) {
87873441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
87973441091SDouglas Gregor     Failed = true;
88073441091SDouglas Gregor   }
88173441091SDouglas Gregor 
88273441091SDouglas Gregor   // Inferred modules must have umbrella headers.
88373441091SDouglas Gregor   if (!Failed && !ActiveModule->getTopLevelModule()->UmbrellaHeader) {
88473441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
88573441091SDouglas Gregor     Failed = true;
88673441091SDouglas Gregor   }
88773441091SDouglas Gregor 
88873441091SDouglas Gregor   // Check for redefinition of an inferred module.
88973441091SDouglas Gregor   if (!Failed && ActiveModule->getTopLevelModule()->InferSubmodules) {
89073441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
89173441091SDouglas Gregor     if (ActiveModule->getTopLevelModule()->InferredSubmoduleLoc.isValid())
89273441091SDouglas Gregor       Diags.Report(ActiveModule->getTopLevelModule()->InferredSubmoduleLoc,
89373441091SDouglas Gregor                    diag::note_mmap_prev_definition);
89473441091SDouglas Gregor     Failed = true;
89573441091SDouglas Gregor   }
89673441091SDouglas Gregor 
89773441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
89873441091SDouglas Gregor   if (Failed) {
89973441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
90073441091SDouglas Gregor       consumeToken();
90173441091SDouglas Gregor       skipUntil(MMToken::RBrace);
90273441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
90373441091SDouglas Gregor         consumeToken();
90473441091SDouglas Gregor     }
90573441091SDouglas Gregor     HadError = true;
90673441091SDouglas Gregor     return;
90773441091SDouglas Gregor   }
90873441091SDouglas Gregor 
90973441091SDouglas Gregor   // Note that we have an inferred submodule.
91073441091SDouglas Gregor   Module *TopModule = ActiveModule->getTopLevelModule();
91173441091SDouglas Gregor   TopModule->InferSubmodules = true;
91273441091SDouglas Gregor   TopModule->InferredSubmoduleLoc = StarLoc;
91373441091SDouglas Gregor   TopModule->InferExplicitSubmodules = Explicit;
91473441091SDouglas Gregor 
91573441091SDouglas Gregor   // Parse the opening brace.
91673441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
91773441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
91873441091SDouglas Gregor     HadError = true;
91973441091SDouglas Gregor     return;
92073441091SDouglas Gregor   }
92173441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
92273441091SDouglas Gregor 
92373441091SDouglas Gregor   // Parse the body of the inferred submodule.
92473441091SDouglas Gregor   bool Done = false;
92573441091SDouglas Gregor   do {
92673441091SDouglas Gregor     switch (Tok.Kind) {
92773441091SDouglas Gregor     case MMToken::EndOfFile:
92873441091SDouglas Gregor     case MMToken::RBrace:
92973441091SDouglas Gregor       Done = true;
93073441091SDouglas Gregor       break;
93173441091SDouglas Gregor 
93273441091SDouglas Gregor     case MMToken::ExportKeyword: {
93373441091SDouglas Gregor       consumeToken();
93473441091SDouglas Gregor       if (Tok.is(MMToken::Star))
93573441091SDouglas Gregor         TopModule->InferExportWildcard = true;
93673441091SDouglas Gregor       else
93773441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
93873441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
93973441091SDouglas Gregor       consumeToken();
94073441091SDouglas Gregor       break;
94173441091SDouglas Gregor     }
94273441091SDouglas Gregor 
94373441091SDouglas Gregor     case MMToken::ExplicitKeyword:
94473441091SDouglas Gregor     case MMToken::ModuleKeyword:
94573441091SDouglas Gregor     case MMToken::HeaderKeyword:
94673441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
94773441091SDouglas Gregor     default:
94873441091SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member);
94973441091SDouglas Gregor       consumeToken();
95073441091SDouglas Gregor       break;
95173441091SDouglas Gregor     }
95273441091SDouglas Gregor   } while (!Done);
95373441091SDouglas Gregor 
95473441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
95573441091SDouglas Gregor     consumeToken();
95673441091SDouglas Gregor   else {
95773441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
95873441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
95973441091SDouglas Gregor     HadError = true;
96073441091SDouglas Gregor   }
96173441091SDouglas Gregor }
96273441091SDouglas Gregor 
963718292f2SDouglas Gregor /// \brief Parse a module map file.
964718292f2SDouglas Gregor ///
965718292f2SDouglas Gregor ///   module-map-file:
966718292f2SDouglas Gregor ///     module-declaration*
967718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
968718292f2SDouglas Gregor   do {
969718292f2SDouglas Gregor     switch (Tok.Kind) {
970718292f2SDouglas Gregor     case MMToken::EndOfFile:
971718292f2SDouglas Gregor       return HadError;
972718292f2SDouglas Gregor 
973718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
974755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
975718292f2SDouglas Gregor       parseModuleDecl();
976718292f2SDouglas Gregor       break;
977718292f2SDouglas Gregor 
978718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
9792b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
980718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
981718292f2SDouglas Gregor     case MMToken::Identifier:
982718292f2SDouglas Gregor     case MMToken::LBrace:
9832b82c2a5SDouglas Gregor     case MMToken::Period:
984718292f2SDouglas Gregor     case MMToken::RBrace:
9852b82c2a5SDouglas Gregor     case MMToken::Star:
986718292f2SDouglas Gregor     case MMToken::StringLiteral:
987718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
988718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
989718292f2SDouglas Gregor       HadError = true;
990718292f2SDouglas Gregor       consumeToken();
991718292f2SDouglas Gregor       break;
992718292f2SDouglas Gregor     }
993718292f2SDouglas Gregor   } while (true);
994718292f2SDouglas Gregor 
995718292f2SDouglas Gregor   return HadError;
996718292f2SDouglas Gregor }
997718292f2SDouglas Gregor 
998718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
999718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1000718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1001718292f2SDouglas Gregor   if (!Buffer)
1002718292f2SDouglas Gregor     return true;
1003718292f2SDouglas Gregor 
1004718292f2SDouglas Gregor   // Parse this module map file.
1005718292f2SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts);
1006718292f2SDouglas Gregor   Diags->getClient()->BeginSourceFile(LangOpts);
10075257fc63SDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir());
1008718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
1009718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
1010718292f2SDouglas Gregor 
1011718292f2SDouglas Gregor   return Result;
1012718292f2SDouglas Gregor }
1013