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; 106930a85ccSDouglas Gregor 107930a85ccSDouglas Gregor // Search up the module stack until we find a module with an umbrella 108930a85ccSDouglas Gregor // header. 109930a85ccSDouglas Gregor Module *UmbrellaModule = Result; 110930a85ccSDouglas Gregor while (!UmbrellaModule->UmbrellaHeader && UmbrellaModule->Parent) 111930a85ccSDouglas Gregor UmbrellaModule = UmbrellaModule->Parent; 112930a85ccSDouglas Gregor 113930a85ccSDouglas 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(); 123930a85ccSDouglas 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, 130930a85ccSDouglas 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. 137930a85ccSDouglas 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, 144930a85ccSDouglas 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. 148930a85ccSDouglas 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 254f2161a70SDouglas Gregor // FIXME: Look for subframeworks. 255f2161a70SDouglas Gregor 25656c64013SDouglas Gregor Modules[ModuleName] = Result; 25756c64013SDouglas Gregor return Result; 25856c64013SDouglas Gregor } 25956c64013SDouglas Gregor 260a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){ 261a89c5ac4SDouglas Gregor Headers[UmbrellaHeader] = Mod; 262a89c5ac4SDouglas Gregor Mod->UmbrellaHeader = UmbrellaHeader; 263a89c5ac4SDouglas Gregor 264a89c5ac4SDouglas Gregor const DirectoryEntry *UmbrellaDir = UmbrellaHeader->getDir(); 265a89c5ac4SDouglas Gregor if (Mod->IsFramework) 266a89c5ac4SDouglas Gregor UmbrellaDir = SourceMgr->getFileManager().getDirectory( 267a89c5ac4SDouglas Gregor llvm::sys::path::parent_path(UmbrellaDir->getName())); 268a89c5ac4SDouglas Gregor 269a89c5ac4SDouglas Gregor UmbrellaDirs[UmbrellaDir] = Mod; 270a89c5ac4SDouglas Gregor } 271a89c5ac4SDouglas Gregor 272a89c5ac4SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header) { 273a89c5ac4SDouglas Gregor Mod->Headers.push_back(Header); 274a89c5ac4SDouglas Gregor Headers[Header] = Mod; 275a89c5ac4SDouglas Gregor } 276a89c5ac4SDouglas Gregor 277514b636aSDouglas Gregor const FileEntry * 278de3ef502SDouglas Gregor ModuleMap::getContainingModuleMapFile(Module *Module) { 279514b636aSDouglas Gregor if (Module->DefinitionLoc.isInvalid() || !SourceMgr) 280514b636aSDouglas Gregor return 0; 281514b636aSDouglas Gregor 282514b636aSDouglas Gregor return SourceMgr->getFileEntryForID( 283514b636aSDouglas Gregor SourceMgr->getFileID(Module->DefinitionLoc)); 284514b636aSDouglas Gregor } 285514b636aSDouglas Gregor 286718292f2SDouglas Gregor void ModuleMap::dump() { 287718292f2SDouglas Gregor llvm::errs() << "Modules:"; 288718292f2SDouglas Gregor for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 289718292f2SDouglas Gregor MEnd = Modules.end(); 290718292f2SDouglas Gregor M != MEnd; ++M) 291d28d1b8dSDouglas Gregor M->getValue()->print(llvm::errs(), 2); 292718292f2SDouglas Gregor 293718292f2SDouglas Gregor llvm::errs() << "Headers:"; 294718292f2SDouglas Gregor for (llvm::DenseMap<const FileEntry *, Module *>::iterator 295718292f2SDouglas Gregor H = Headers.begin(), 296718292f2SDouglas Gregor HEnd = Headers.end(); 297718292f2SDouglas Gregor H != HEnd; ++H) { 298718292f2SDouglas Gregor llvm::errs() << " \"" << H->first->getName() << "\" -> " 299718292f2SDouglas Gregor << H->second->getFullModuleName() << "\n"; 300718292f2SDouglas Gregor } 301718292f2SDouglas Gregor } 302718292f2SDouglas Gregor 3032b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) { 3042b82c2a5SDouglas Gregor bool HadError = false; 3052b82c2a5SDouglas Gregor for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) { 3062b82c2a5SDouglas Gregor Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 3072b82c2a5SDouglas Gregor Complain); 308f5eedd05SDouglas Gregor if (Export.getPointer() || Export.getInt()) 3092b82c2a5SDouglas Gregor Mod->Exports.push_back(Export); 3102b82c2a5SDouglas Gregor else 3112b82c2a5SDouglas Gregor HadError = true; 3122b82c2a5SDouglas Gregor } 3132b82c2a5SDouglas Gregor Mod->UnresolvedExports.clear(); 3142b82c2a5SDouglas Gregor return HadError; 3152b82c2a5SDouglas Gregor } 3162b82c2a5SDouglas Gregor 3170093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) { 3180093b3c7SDouglas Gregor if (Loc.isInvalid()) 3190093b3c7SDouglas Gregor return 0; 3200093b3c7SDouglas Gregor 3210093b3c7SDouglas Gregor // Use the expansion location to determine which module we're in. 3220093b3c7SDouglas Gregor FullSourceLoc ExpansionLoc = Loc.getExpansionLoc(); 3230093b3c7SDouglas Gregor if (!ExpansionLoc.isFileID()) 3240093b3c7SDouglas Gregor return 0; 3250093b3c7SDouglas Gregor 3260093b3c7SDouglas Gregor 3270093b3c7SDouglas Gregor const SourceManager &SrcMgr = Loc.getManager(); 3280093b3c7SDouglas Gregor FileID ExpansionFileID = ExpansionLoc.getFileID(); 3290093b3c7SDouglas Gregor const FileEntry *ExpansionFile = SrcMgr.getFileEntryForID(ExpansionFileID); 3300093b3c7SDouglas Gregor if (!ExpansionFile) 3310093b3c7SDouglas Gregor return 0; 3320093b3c7SDouglas Gregor 3330093b3c7SDouglas Gregor // Find the module that owns this header. 3340093b3c7SDouglas Gregor return findModuleForHeader(ExpansionFile); 3350093b3c7SDouglas Gregor } 3360093b3c7SDouglas Gregor 337718292f2SDouglas Gregor //----------------------------------------------------------------------------// 338718292f2SDouglas Gregor // Module map file parser 339718292f2SDouglas Gregor //----------------------------------------------------------------------------// 340718292f2SDouglas Gregor 341718292f2SDouglas Gregor namespace clang { 342718292f2SDouglas Gregor /// \brief A token in a module map file. 343718292f2SDouglas Gregor struct MMToken { 344718292f2SDouglas Gregor enum TokenKind { 345718292f2SDouglas Gregor EndOfFile, 346718292f2SDouglas Gregor HeaderKeyword, 347718292f2SDouglas Gregor Identifier, 348718292f2SDouglas Gregor ExplicitKeyword, 3492b82c2a5SDouglas Gregor ExportKeyword, 350755b2055SDouglas Gregor FrameworkKeyword, 351718292f2SDouglas Gregor ModuleKeyword, 3522b82c2a5SDouglas Gregor Period, 353718292f2SDouglas Gregor UmbrellaKeyword, 3542b82c2a5SDouglas Gregor Star, 355718292f2SDouglas Gregor StringLiteral, 356718292f2SDouglas Gregor LBrace, 357718292f2SDouglas Gregor RBrace 358718292f2SDouglas Gregor } Kind; 359718292f2SDouglas Gregor 360718292f2SDouglas Gregor unsigned Location; 361718292f2SDouglas Gregor unsigned StringLength; 362718292f2SDouglas Gregor const char *StringData; 363718292f2SDouglas Gregor 364718292f2SDouglas Gregor void clear() { 365718292f2SDouglas Gregor Kind = EndOfFile; 366718292f2SDouglas Gregor Location = 0; 367718292f2SDouglas Gregor StringLength = 0; 368718292f2SDouglas Gregor StringData = 0; 369718292f2SDouglas Gregor } 370718292f2SDouglas Gregor 371718292f2SDouglas Gregor bool is(TokenKind K) const { return Kind == K; } 372718292f2SDouglas Gregor 373718292f2SDouglas Gregor SourceLocation getLocation() const { 374718292f2SDouglas Gregor return SourceLocation::getFromRawEncoding(Location); 375718292f2SDouglas Gregor } 376718292f2SDouglas Gregor 377718292f2SDouglas Gregor StringRef getString() const { 378718292f2SDouglas Gregor return StringRef(StringData, StringLength); 379718292f2SDouglas Gregor } 380718292f2SDouglas Gregor }; 381718292f2SDouglas Gregor 382718292f2SDouglas Gregor class ModuleMapParser { 383718292f2SDouglas Gregor Lexer &L; 384718292f2SDouglas Gregor SourceManager &SourceMgr; 385718292f2SDouglas Gregor DiagnosticsEngine &Diags; 386718292f2SDouglas Gregor ModuleMap ⤅ 387718292f2SDouglas Gregor 3885257fc63SDouglas Gregor /// \brief The directory that this module map resides in. 3895257fc63SDouglas Gregor const DirectoryEntry *Directory; 3905257fc63SDouglas Gregor 391718292f2SDouglas Gregor /// \brief Whether an error occurred. 392718292f2SDouglas Gregor bool HadError; 393718292f2SDouglas Gregor 394718292f2SDouglas Gregor /// \brief Default target information, used only for string literal 395718292f2SDouglas Gregor /// parsing. 396718292f2SDouglas Gregor TargetInfo *Target; 397718292f2SDouglas Gregor 398718292f2SDouglas Gregor /// \brief Stores string data for the various string literals referenced 399718292f2SDouglas Gregor /// during parsing. 400718292f2SDouglas Gregor llvm::BumpPtrAllocator StringData; 401718292f2SDouglas Gregor 402718292f2SDouglas Gregor /// \brief The current token. 403718292f2SDouglas Gregor MMToken Tok; 404718292f2SDouglas Gregor 405718292f2SDouglas Gregor /// \brief The active module. 406de3ef502SDouglas Gregor Module *ActiveModule; 407718292f2SDouglas Gregor 408718292f2SDouglas Gregor /// \brief Consume the current token and return its location. 409718292f2SDouglas Gregor SourceLocation consumeToken(); 410718292f2SDouglas Gregor 411718292f2SDouglas Gregor /// \brief Skip tokens until we reach the a token with the given kind 412718292f2SDouglas Gregor /// (or the end of the file). 413718292f2SDouglas Gregor void skipUntil(MMToken::TokenKind K); 414718292f2SDouglas Gregor 415718292f2SDouglas Gregor void parseModuleDecl(); 416718292f2SDouglas Gregor void parseUmbrellaDecl(); 417718292f2SDouglas Gregor void parseHeaderDecl(); 4182b82c2a5SDouglas Gregor void parseExportDecl(); 41973441091SDouglas Gregor void parseInferredSubmoduleDecl(bool Explicit); 420718292f2SDouglas Gregor 421718292f2SDouglas Gregor public: 422718292f2SDouglas Gregor explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 423718292f2SDouglas Gregor DiagnosticsEngine &Diags, 4245257fc63SDouglas Gregor ModuleMap &Map, 4255257fc63SDouglas Gregor const DirectoryEntry *Directory) 4265257fc63SDouglas Gregor : L(L), SourceMgr(SourceMgr), Diags(Diags), Map(Map), 4275257fc63SDouglas Gregor Directory(Directory), HadError(false), ActiveModule(0) 428718292f2SDouglas Gregor { 429718292f2SDouglas Gregor TargetOptions TargetOpts; 430718292f2SDouglas Gregor TargetOpts.Triple = llvm::sys::getDefaultTargetTriple(); 431718292f2SDouglas Gregor Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); 432718292f2SDouglas Gregor 433718292f2SDouglas Gregor Tok.clear(); 434718292f2SDouglas Gregor consumeToken(); 435718292f2SDouglas Gregor } 436718292f2SDouglas Gregor 437718292f2SDouglas Gregor bool parseModuleMapFile(); 438718292f2SDouglas Gregor }; 439718292f2SDouglas Gregor } 440718292f2SDouglas Gregor 441718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() { 442718292f2SDouglas Gregor retry: 443718292f2SDouglas Gregor SourceLocation Result = Tok.getLocation(); 444718292f2SDouglas Gregor Tok.clear(); 445718292f2SDouglas Gregor 446718292f2SDouglas Gregor Token LToken; 447718292f2SDouglas Gregor L.LexFromRawLexer(LToken); 448718292f2SDouglas Gregor Tok.Location = LToken.getLocation().getRawEncoding(); 449718292f2SDouglas Gregor switch (LToken.getKind()) { 450718292f2SDouglas Gregor case tok::raw_identifier: 451718292f2SDouglas Gregor Tok.StringData = LToken.getRawIdentifierData(); 452718292f2SDouglas Gregor Tok.StringLength = LToken.getLength(); 453718292f2SDouglas Gregor Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString()) 454718292f2SDouglas Gregor .Case("header", MMToken::HeaderKeyword) 455718292f2SDouglas Gregor .Case("explicit", MMToken::ExplicitKeyword) 4562b82c2a5SDouglas Gregor .Case("export", MMToken::ExportKeyword) 457755b2055SDouglas Gregor .Case("framework", MMToken::FrameworkKeyword) 458718292f2SDouglas Gregor .Case("module", MMToken::ModuleKeyword) 459718292f2SDouglas Gregor .Case("umbrella", MMToken::UmbrellaKeyword) 460718292f2SDouglas Gregor .Default(MMToken::Identifier); 461718292f2SDouglas Gregor break; 462718292f2SDouglas Gregor 463718292f2SDouglas Gregor case tok::eof: 464718292f2SDouglas Gregor Tok.Kind = MMToken::EndOfFile; 465718292f2SDouglas Gregor break; 466718292f2SDouglas Gregor 467718292f2SDouglas Gregor case tok::l_brace: 468718292f2SDouglas Gregor Tok.Kind = MMToken::LBrace; 469718292f2SDouglas Gregor break; 470718292f2SDouglas Gregor 4712b82c2a5SDouglas Gregor case tok::period: 4722b82c2a5SDouglas Gregor Tok.Kind = MMToken::Period; 4732b82c2a5SDouglas Gregor break; 4742b82c2a5SDouglas Gregor 475718292f2SDouglas Gregor case tok::r_brace: 476718292f2SDouglas Gregor Tok.Kind = MMToken::RBrace; 477718292f2SDouglas Gregor break; 478718292f2SDouglas Gregor 4792b82c2a5SDouglas Gregor case tok::star: 4802b82c2a5SDouglas Gregor Tok.Kind = MMToken::Star; 4812b82c2a5SDouglas Gregor break; 4822b82c2a5SDouglas Gregor 483718292f2SDouglas Gregor case tok::string_literal: { 484718292f2SDouglas Gregor // Parse the string literal. 485718292f2SDouglas Gregor LangOptions LangOpts; 486718292f2SDouglas Gregor StringLiteralParser StringLiteral(<oken, 1, SourceMgr, LangOpts, *Target); 487718292f2SDouglas Gregor if (StringLiteral.hadError) 488718292f2SDouglas Gregor goto retry; 489718292f2SDouglas Gregor 490718292f2SDouglas Gregor // Copy the string literal into our string data allocator. 491718292f2SDouglas Gregor unsigned Length = StringLiteral.GetStringLength(); 492718292f2SDouglas Gregor char *Saved = StringData.Allocate<char>(Length + 1); 493718292f2SDouglas Gregor memcpy(Saved, StringLiteral.GetString().data(), Length); 494718292f2SDouglas Gregor Saved[Length] = 0; 495718292f2SDouglas Gregor 496718292f2SDouglas Gregor // Form the token. 497718292f2SDouglas Gregor Tok.Kind = MMToken::StringLiteral; 498718292f2SDouglas Gregor Tok.StringData = Saved; 499718292f2SDouglas Gregor Tok.StringLength = Length; 500718292f2SDouglas Gregor break; 501718292f2SDouglas Gregor } 502718292f2SDouglas Gregor 503718292f2SDouglas Gregor case tok::comment: 504718292f2SDouglas Gregor goto retry; 505718292f2SDouglas Gregor 506718292f2SDouglas Gregor default: 507718292f2SDouglas Gregor Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token); 508718292f2SDouglas Gregor HadError = true; 509718292f2SDouglas Gregor goto retry; 510718292f2SDouglas Gregor } 511718292f2SDouglas Gregor 512718292f2SDouglas Gregor return Result; 513718292f2SDouglas Gregor } 514718292f2SDouglas Gregor 515718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) { 516718292f2SDouglas Gregor unsigned braceDepth = 0; 517718292f2SDouglas Gregor do { 518718292f2SDouglas Gregor switch (Tok.Kind) { 519718292f2SDouglas Gregor case MMToken::EndOfFile: 520718292f2SDouglas Gregor return; 521718292f2SDouglas Gregor 522718292f2SDouglas Gregor case MMToken::LBrace: 523718292f2SDouglas Gregor if (Tok.is(K) && braceDepth == 0) 524718292f2SDouglas Gregor return; 525718292f2SDouglas Gregor 526718292f2SDouglas Gregor ++braceDepth; 527718292f2SDouglas Gregor break; 528718292f2SDouglas Gregor 529718292f2SDouglas Gregor case MMToken::RBrace: 530718292f2SDouglas Gregor if (braceDepth > 0) 531718292f2SDouglas Gregor --braceDepth; 532718292f2SDouglas Gregor else if (Tok.is(K)) 533718292f2SDouglas Gregor return; 534718292f2SDouglas Gregor break; 535718292f2SDouglas Gregor 536718292f2SDouglas Gregor default: 537718292f2SDouglas Gregor if (braceDepth == 0 && Tok.is(K)) 538718292f2SDouglas Gregor return; 539718292f2SDouglas Gregor break; 540718292f2SDouglas Gregor } 541718292f2SDouglas Gregor 542718292f2SDouglas Gregor consumeToken(); 543718292f2SDouglas Gregor } while (true); 544718292f2SDouglas Gregor } 545718292f2SDouglas Gregor 546718292f2SDouglas Gregor /// \brief Parse a module declaration. 547718292f2SDouglas Gregor /// 548718292f2SDouglas Gregor /// module-declaration: 549755b2055SDouglas Gregor /// 'framework'[opt] 'module' identifier { module-member* } 550718292f2SDouglas Gregor /// 551718292f2SDouglas Gregor /// module-member: 552718292f2SDouglas Gregor /// umbrella-declaration 553718292f2SDouglas Gregor /// header-declaration 55473441091SDouglas Gregor /// 'explicit'[opt] submodule-declaration 5552b82c2a5SDouglas Gregor /// export-declaration 55673441091SDouglas Gregor /// 55773441091SDouglas Gregor /// submodule-declaration: 55873441091SDouglas Gregor /// module-declaration 55973441091SDouglas Gregor /// inferred-submodule-declaration 560718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() { 561755b2055SDouglas Gregor assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) || 562755b2055SDouglas Gregor Tok.is(MMToken::FrameworkKeyword)); 563718292f2SDouglas Gregor 564f2161a70SDouglas Gregor // Parse 'explicit' or 'framework' keyword, if present. 565718292f2SDouglas Gregor bool Explicit = false; 566f2161a70SDouglas Gregor bool Framework = false; 567755b2055SDouglas Gregor 568f2161a70SDouglas Gregor // Parse 'explicit' keyword, if present. 569f2161a70SDouglas Gregor if (Tok.is(MMToken::ExplicitKeyword)) { 570f2161a70SDouglas Gregor consumeToken(); 571f2161a70SDouglas Gregor Explicit = true; 572f2161a70SDouglas Gregor } 573f2161a70SDouglas Gregor 574f2161a70SDouglas Gregor // Parse 'framework' keyword, if present. 575755b2055SDouglas Gregor if (Tok.is(MMToken::FrameworkKeyword)) { 576755b2055SDouglas Gregor consumeToken(); 577755b2055SDouglas Gregor Framework = true; 578755b2055SDouglas Gregor } 579718292f2SDouglas Gregor 580718292f2SDouglas Gregor // Parse 'module' keyword. 581718292f2SDouglas Gregor if (!Tok.is(MMToken::ModuleKeyword)) { 582718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), 583718292f2SDouglas Gregor diag::err_mmap_expected_module_after_explicit); 584718292f2SDouglas Gregor consumeToken(); 585718292f2SDouglas Gregor HadError = true; 586718292f2SDouglas Gregor return; 587718292f2SDouglas Gregor } 588718292f2SDouglas Gregor consumeToken(); // 'module' keyword 589718292f2SDouglas Gregor 59073441091SDouglas Gregor // If we have a wildcard for the module name, this is an inferred submodule. 59173441091SDouglas Gregor // Parse it. 59273441091SDouglas Gregor if (Tok.is(MMToken::Star)) 59373441091SDouglas Gregor return parseInferredSubmoduleDecl(Explicit); 59473441091SDouglas Gregor 595718292f2SDouglas Gregor // Parse the module name. 596718292f2SDouglas Gregor if (!Tok.is(MMToken::Identifier)) { 597718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name); 598718292f2SDouglas Gregor HadError = true; 599718292f2SDouglas Gregor return; 600718292f2SDouglas Gregor } 601718292f2SDouglas Gregor StringRef ModuleName = Tok.getString(); 602718292f2SDouglas Gregor SourceLocation ModuleNameLoc = consumeToken(); 603718292f2SDouglas Gregor 604718292f2SDouglas Gregor // Parse the opening brace. 605718292f2SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 606718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace) 607718292f2SDouglas Gregor << ModuleName; 608718292f2SDouglas Gregor HadError = true; 609718292f2SDouglas Gregor return; 610718292f2SDouglas Gregor } 611718292f2SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 612718292f2SDouglas Gregor 613718292f2SDouglas Gregor // Determine whether this (sub)module has already been defined. 614718292f2SDouglas Gregor llvm::StringMap<Module *> &ModuleSpace 615718292f2SDouglas Gregor = ActiveModule? ActiveModule->SubModules : Map.Modules; 616718292f2SDouglas Gregor llvm::StringMap<Module *>::iterator ExistingModule 617718292f2SDouglas Gregor = ModuleSpace.find(ModuleName); 618718292f2SDouglas Gregor if (ExistingModule != ModuleSpace.end()) { 619718292f2SDouglas Gregor Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition) 620718292f2SDouglas Gregor << ModuleName; 621718292f2SDouglas Gregor Diags.Report(ExistingModule->getValue()->DefinitionLoc, 622718292f2SDouglas Gregor diag::note_mmap_prev_definition); 623718292f2SDouglas Gregor 624718292f2SDouglas Gregor // Skip the module definition. 625718292f2SDouglas Gregor skipUntil(MMToken::RBrace); 626718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 627718292f2SDouglas Gregor consumeToken(); 628718292f2SDouglas Gregor 629718292f2SDouglas Gregor HadError = true; 630718292f2SDouglas Gregor return; 631718292f2SDouglas Gregor } 632718292f2SDouglas Gregor 633718292f2SDouglas Gregor // Start defining this module. 634755b2055SDouglas Gregor ActiveModule = new Module(ModuleName, ModuleNameLoc, ActiveModule, Framework, 635755b2055SDouglas Gregor Explicit); 636718292f2SDouglas Gregor ModuleSpace[ModuleName] = ActiveModule; 637718292f2SDouglas Gregor 638718292f2SDouglas Gregor bool Done = false; 639718292f2SDouglas Gregor do { 640718292f2SDouglas Gregor switch (Tok.Kind) { 641718292f2SDouglas Gregor case MMToken::EndOfFile: 642718292f2SDouglas Gregor case MMToken::RBrace: 643718292f2SDouglas Gregor Done = true; 644718292f2SDouglas Gregor break; 645718292f2SDouglas Gregor 646718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 647f2161a70SDouglas Gregor case MMToken::FrameworkKeyword: 648718292f2SDouglas Gregor case MMToken::ModuleKeyword: 649718292f2SDouglas Gregor parseModuleDecl(); 650718292f2SDouglas Gregor break; 651718292f2SDouglas Gregor 6522b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 6532b82c2a5SDouglas Gregor parseExportDecl(); 6542b82c2a5SDouglas Gregor break; 6552b82c2a5SDouglas Gregor 656718292f2SDouglas Gregor case MMToken::HeaderKeyword: 657718292f2SDouglas Gregor parseHeaderDecl(); 658718292f2SDouglas Gregor break; 659718292f2SDouglas Gregor 660718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 661718292f2SDouglas Gregor parseUmbrellaDecl(); 662718292f2SDouglas Gregor break; 663718292f2SDouglas Gregor 664718292f2SDouglas Gregor default: 665718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member); 666718292f2SDouglas Gregor consumeToken(); 667718292f2SDouglas Gregor break; 668718292f2SDouglas Gregor } 669718292f2SDouglas Gregor } while (!Done); 670718292f2SDouglas Gregor 671718292f2SDouglas Gregor if (Tok.is(MMToken::RBrace)) 672718292f2SDouglas Gregor consumeToken(); 673718292f2SDouglas Gregor else { 674718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 675718292f2SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 676718292f2SDouglas Gregor HadError = true; 677718292f2SDouglas Gregor } 678718292f2SDouglas Gregor 679718292f2SDouglas Gregor // We're done parsing this module. Pop back to our parent scope. 680718292f2SDouglas Gregor ActiveModule = ActiveModule->Parent; 681718292f2SDouglas Gregor } 682718292f2SDouglas Gregor 683f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the 684f2161a70SDouglas Gregor /// subframework in which the given module lives. 685f2161a70SDouglas Gregor void appendSubframeworkPaths(Module *Mod, llvm::SmallVectorImpl<char> &Path) { 686f2161a70SDouglas Gregor // Collect the framework names from the given module to the top-level module. 687f2161a70SDouglas Gregor llvm::SmallVector<StringRef, 2> Paths; 688f2161a70SDouglas Gregor for (; Mod; Mod = Mod->Parent) { 689f2161a70SDouglas Gregor if (Mod->IsFramework) 690f2161a70SDouglas Gregor Paths.push_back(Mod->Name); 691f2161a70SDouglas Gregor } 692f2161a70SDouglas Gregor 693f2161a70SDouglas Gregor if (Paths.empty()) 694f2161a70SDouglas Gregor return; 695f2161a70SDouglas Gregor 696f2161a70SDouglas Gregor // Add Frameworks/Name.framework for each subframework. 697f2161a70SDouglas Gregor for (unsigned I = Paths.size() - 1; I != 0; --I) { 698f2161a70SDouglas Gregor llvm::sys::path::append(Path, "Frameworks"); 699f2161a70SDouglas Gregor llvm::sys::path::append(Path, Paths[I-1] + ".framework"); 700f2161a70SDouglas Gregor } 701f2161a70SDouglas Gregor } 702f2161a70SDouglas Gregor 703718292f2SDouglas Gregor /// \brief Parse an umbrella header declaration. 704718292f2SDouglas Gregor /// 705718292f2SDouglas Gregor /// umbrella-declaration: 706718292f2SDouglas Gregor /// 'umbrella' string-literal 707718292f2SDouglas Gregor void ModuleMapParser::parseUmbrellaDecl() { 708718292f2SDouglas Gregor assert(Tok.is(MMToken::UmbrellaKeyword)); 709718292f2SDouglas Gregor SourceLocation UmbrellaLoc = consumeToken(); 710718292f2SDouglas Gregor 711718292f2SDouglas Gregor // Parse the header name. 712718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 713718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 714718292f2SDouglas Gregor << "umbrella"; 715718292f2SDouglas Gregor HadError = true; 716718292f2SDouglas Gregor return; 717718292f2SDouglas Gregor } 718718292f2SDouglas Gregor StringRef FileName = Tok.getString(); 719718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 720718292f2SDouglas Gregor 7215257fc63SDouglas Gregor // Check whether we already have an umbrella header. 7225257fc63SDouglas Gregor if (ActiveModule->UmbrellaHeader) { 7235257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_umbrella_header_conflict) 7245257fc63SDouglas Gregor << ActiveModule->getFullModuleName() 7255257fc63SDouglas Gregor << ActiveModule->UmbrellaHeader->getName(); 7265257fc63SDouglas Gregor HadError = true; 7275257fc63SDouglas Gregor return; 7285257fc63SDouglas Gregor } 7295257fc63SDouglas Gregor 7305257fc63SDouglas Gregor // Look for this file. 7315257fc63SDouglas Gregor llvm::SmallString<128> PathName; 732f545f67dSDouglas Gregor const FileEntry *File = 0; 733f545f67dSDouglas Gregor 734f545f67dSDouglas Gregor if (llvm::sys::path::is_absolute(FileName)) { 735f545f67dSDouglas Gregor PathName = FileName; 736f545f67dSDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 737f545f67dSDouglas Gregor } else { 738f545f67dSDouglas Gregor // Search for the header file within the search directory. 7395257fc63SDouglas Gregor PathName += Directory->getName(); 740755b2055SDouglas Gregor unsigned PathLength = PathName.size(); 741f2161a70SDouglas Gregor 742755b2055SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 743f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 744f2161a70SDouglas Gregor 745755b2055SDouglas Gregor // Check whether this file is in the public headers. 746755b2055SDouglas Gregor llvm::sys::path::append(PathName, "Headers"); 7475257fc63SDouglas Gregor llvm::sys::path::append(PathName, FileName); 748755b2055SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 749755b2055SDouglas Gregor 750755b2055SDouglas Gregor if (!File) { 751755b2055SDouglas Gregor // Check whether this file is in the private headers. 752755b2055SDouglas Gregor PathName.resize(PathLength); 753755b2055SDouglas Gregor llvm::sys::path::append(PathName, "PrivateHeaders"); 754755b2055SDouglas Gregor llvm::sys::path::append(PathName, FileName); 755755b2055SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 756755b2055SDouglas Gregor } 757755b2055SDouglas Gregor } else { 758755b2055SDouglas Gregor // Lookup for normal headers. 759755b2055SDouglas Gregor llvm::sys::path::append(PathName, FileName); 760755b2055SDouglas Gregor File = SourceMgr.getFileManager().getFile(PathName); 761755b2055SDouglas Gregor } 762f545f67dSDouglas Gregor } 7635257fc63SDouglas Gregor 7645257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 7655257fc63SDouglas Gregor // Come up with a lazy way to do this. 766755b2055SDouglas Gregor if (File) { 767a89c5ac4SDouglas Gregor const DirectoryEntry *UmbrellaDir = File->getDir(); 768a89c5ac4SDouglas Gregor if (ActiveModule->IsFramework) { 769a89c5ac4SDouglas Gregor // For framework modules, use the framework directory as the umbrella 770a89c5ac4SDouglas Gregor // directory. 771a89c5ac4SDouglas Gregor UmbrellaDir = SourceMgr.getFileManager().getDirectory( 772a89c5ac4SDouglas Gregor llvm::sys::path::parent_path(UmbrellaDir->getName())); 773a89c5ac4SDouglas Gregor } 774a89c5ac4SDouglas Gregor 7755257fc63SDouglas Gregor if (const Module *OwningModule = Map.Headers[File]) { 7765257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 7775257fc63SDouglas Gregor << FileName << OwningModule->getFullModuleName(); 7785257fc63SDouglas Gregor HadError = true; 779a89c5ac4SDouglas Gregor } else if ((OwningModule = Map.UmbrellaDirs[UmbrellaDir])) { 780b65dbfffSDouglas Gregor Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash) 781b65dbfffSDouglas Gregor << OwningModule->getFullModuleName(); 782b65dbfffSDouglas Gregor HadError = true; 7835257fc63SDouglas Gregor } else { 7845257fc63SDouglas Gregor // Record this umbrella header. 785a89c5ac4SDouglas Gregor Map.setUmbrellaHeader(ActiveModule, File); 7865257fc63SDouglas Gregor } 7875257fc63SDouglas Gregor } else { 7885257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 7895257fc63SDouglas Gregor << true << FileName; 7905257fc63SDouglas Gregor HadError = true; 7915257fc63SDouglas Gregor } 792718292f2SDouglas Gregor } 793718292f2SDouglas Gregor 794718292f2SDouglas Gregor /// \brief Parse a header declaration. 795718292f2SDouglas Gregor /// 796718292f2SDouglas Gregor /// header-declaration: 797718292f2SDouglas Gregor /// 'header' string-literal 798718292f2SDouglas Gregor void ModuleMapParser::parseHeaderDecl() { 799718292f2SDouglas Gregor assert(Tok.is(MMToken::HeaderKeyword)); 8001871ed3dSBenjamin Kramer consumeToken(); 801718292f2SDouglas Gregor 802718292f2SDouglas Gregor // Parse the header name. 803718292f2SDouglas Gregor if (!Tok.is(MMToken::StringLiteral)) { 804718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 805718292f2SDouglas Gregor << "header"; 806718292f2SDouglas Gregor HadError = true; 807718292f2SDouglas Gregor return; 808718292f2SDouglas Gregor } 809718292f2SDouglas Gregor StringRef FileName = Tok.getString(); 810718292f2SDouglas Gregor SourceLocation FileNameLoc = consumeToken(); 811718292f2SDouglas Gregor 8125257fc63SDouglas Gregor // Look for this file. 8135257fc63SDouglas Gregor llvm::SmallString<128> PathName; 814f545f67dSDouglas Gregor if (llvm::sys::path::is_relative(FileName)) { 815f545f67dSDouglas Gregor // FIXME: Change this search to also look for private headers! 8165257fc63SDouglas Gregor PathName += Directory->getName(); 817755b2055SDouglas Gregor 818f2161a70SDouglas Gregor if (ActiveModule->isPartOfFramework()) { 819f2161a70SDouglas Gregor appendSubframeworkPaths(ActiveModule, PathName); 820755b2055SDouglas Gregor llvm::sys::path::append(PathName, "Headers"); 821f545f67dSDouglas Gregor } 822f2161a70SDouglas Gregor } 823755b2055SDouglas Gregor 8245257fc63SDouglas Gregor llvm::sys::path::append(PathName, FileName); 8255257fc63SDouglas Gregor 8265257fc63SDouglas Gregor // FIXME: We shouldn't be eagerly stat'ing every file named in a module map. 8275257fc63SDouglas Gregor // Come up with a lazy way to do this. 8285257fc63SDouglas Gregor if (const FileEntry *File = SourceMgr.getFileManager().getFile(PathName)) { 8295257fc63SDouglas Gregor if (const Module *OwningModule = Map.Headers[File]) { 8305257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_conflict) 8315257fc63SDouglas Gregor << FileName << OwningModule->getFullModuleName(); 8325257fc63SDouglas Gregor HadError = true; 8335257fc63SDouglas Gregor } else { 8345257fc63SDouglas Gregor // Record this file. 835a89c5ac4SDouglas Gregor Map.addHeader(ActiveModule, File); 8365257fc63SDouglas Gregor } 8375257fc63SDouglas Gregor } else { 8385257fc63SDouglas Gregor Diags.Report(FileNameLoc, diag::err_mmap_header_not_found) 8395257fc63SDouglas Gregor << false << FileName; 8405257fc63SDouglas Gregor HadError = true; 8415257fc63SDouglas Gregor } 842718292f2SDouglas Gregor } 843718292f2SDouglas Gregor 8442b82c2a5SDouglas Gregor /// \brief Parse a module export declaration. 8452b82c2a5SDouglas Gregor /// 8462b82c2a5SDouglas Gregor /// export-declaration: 8472b82c2a5SDouglas Gregor /// 'export' wildcard-module-id 8482b82c2a5SDouglas Gregor /// 8492b82c2a5SDouglas Gregor /// wildcard-module-id: 8502b82c2a5SDouglas Gregor /// identifier 8512b82c2a5SDouglas Gregor /// '*' 8522b82c2a5SDouglas Gregor /// identifier '.' wildcard-module-id 8532b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() { 8542b82c2a5SDouglas Gregor assert(Tok.is(MMToken::ExportKeyword)); 8552b82c2a5SDouglas Gregor SourceLocation ExportLoc = consumeToken(); 8562b82c2a5SDouglas Gregor 8572b82c2a5SDouglas Gregor // Parse the module-id with an optional wildcard at the end. 8582b82c2a5SDouglas Gregor ModuleId ParsedModuleId; 8592b82c2a5SDouglas Gregor bool Wildcard = false; 8602b82c2a5SDouglas Gregor do { 8612b82c2a5SDouglas Gregor if (Tok.is(MMToken::Identifier)) { 8622b82c2a5SDouglas Gregor ParsedModuleId.push_back(std::make_pair(Tok.getString(), 8632b82c2a5SDouglas Gregor Tok.getLocation())); 8642b82c2a5SDouglas Gregor consumeToken(); 8652b82c2a5SDouglas Gregor 8662b82c2a5SDouglas Gregor if (Tok.is(MMToken::Period)) { 8672b82c2a5SDouglas Gregor consumeToken(); 8682b82c2a5SDouglas Gregor continue; 8692b82c2a5SDouglas Gregor } 8702b82c2a5SDouglas Gregor 8712b82c2a5SDouglas Gregor break; 8722b82c2a5SDouglas Gregor } 8732b82c2a5SDouglas Gregor 8742b82c2a5SDouglas Gregor if(Tok.is(MMToken::Star)) { 8752b82c2a5SDouglas Gregor Wildcard = true; 876f5eedd05SDouglas Gregor consumeToken(); 8772b82c2a5SDouglas Gregor break; 8782b82c2a5SDouglas Gregor } 8792b82c2a5SDouglas Gregor 8802b82c2a5SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id); 8812b82c2a5SDouglas Gregor HadError = true; 8822b82c2a5SDouglas Gregor return; 8832b82c2a5SDouglas Gregor } while (true); 8842b82c2a5SDouglas Gregor 8852b82c2a5SDouglas Gregor Module::UnresolvedExportDecl Unresolved = { 8862b82c2a5SDouglas Gregor ExportLoc, ParsedModuleId, Wildcard 8872b82c2a5SDouglas Gregor }; 8882b82c2a5SDouglas Gregor ActiveModule->UnresolvedExports.push_back(Unresolved); 8892b82c2a5SDouglas Gregor } 8902b82c2a5SDouglas Gregor 89173441091SDouglas Gregor void ModuleMapParser::parseInferredSubmoduleDecl(bool Explicit) { 89273441091SDouglas Gregor assert(Tok.is(MMToken::Star)); 89373441091SDouglas Gregor SourceLocation StarLoc = consumeToken(); 89473441091SDouglas Gregor bool Failed = false; 89573441091SDouglas Gregor 89673441091SDouglas Gregor // Inferred modules must be submodules. 89773441091SDouglas Gregor if (!ActiveModule) { 89873441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule); 89973441091SDouglas Gregor Failed = true; 90073441091SDouglas Gregor } 90173441091SDouglas Gregor 90273441091SDouglas Gregor // Inferred modules must have umbrella headers. 903*dd005f69SDouglas Gregor if (!Failed && !ActiveModule->UmbrellaHeader) { 90473441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella); 90573441091SDouglas Gregor Failed = true; 90673441091SDouglas Gregor } 90773441091SDouglas Gregor 90873441091SDouglas Gregor // Check for redefinition of an inferred module. 909*dd005f69SDouglas Gregor if (!Failed && ActiveModule->InferSubmodules) { 91073441091SDouglas Gregor Diags.Report(StarLoc, diag::err_mmap_inferred_redef); 911*dd005f69SDouglas Gregor if (ActiveModule->InferredSubmoduleLoc.isValid()) 912*dd005f69SDouglas Gregor Diags.Report(ActiveModule->InferredSubmoduleLoc, 91373441091SDouglas Gregor diag::note_mmap_prev_definition); 91473441091SDouglas Gregor Failed = true; 91573441091SDouglas Gregor } 91673441091SDouglas Gregor 91773441091SDouglas Gregor // If there were any problems with this inferred submodule, skip its body. 91873441091SDouglas Gregor if (Failed) { 91973441091SDouglas Gregor if (Tok.is(MMToken::LBrace)) { 92073441091SDouglas Gregor consumeToken(); 92173441091SDouglas Gregor skipUntil(MMToken::RBrace); 92273441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 92373441091SDouglas Gregor consumeToken(); 92473441091SDouglas Gregor } 92573441091SDouglas Gregor HadError = true; 92673441091SDouglas Gregor return; 92773441091SDouglas Gregor } 92873441091SDouglas Gregor 92973441091SDouglas Gregor // Note that we have an inferred submodule. 930*dd005f69SDouglas Gregor ActiveModule->InferSubmodules = true; 931*dd005f69SDouglas Gregor ActiveModule->InferredSubmoduleLoc = StarLoc; 932*dd005f69SDouglas Gregor ActiveModule->InferExplicitSubmodules = Explicit; 93373441091SDouglas Gregor 93473441091SDouglas Gregor // Parse the opening brace. 93573441091SDouglas Gregor if (!Tok.is(MMToken::LBrace)) { 93673441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard); 93773441091SDouglas Gregor HadError = true; 93873441091SDouglas Gregor return; 93973441091SDouglas Gregor } 94073441091SDouglas Gregor SourceLocation LBraceLoc = consumeToken(); 94173441091SDouglas Gregor 94273441091SDouglas Gregor // Parse the body of the inferred submodule. 94373441091SDouglas Gregor bool Done = false; 94473441091SDouglas Gregor do { 94573441091SDouglas Gregor switch (Tok.Kind) { 94673441091SDouglas Gregor case MMToken::EndOfFile: 94773441091SDouglas Gregor case MMToken::RBrace: 94873441091SDouglas Gregor Done = true; 94973441091SDouglas Gregor break; 95073441091SDouglas Gregor 95173441091SDouglas Gregor case MMToken::ExportKeyword: { 95273441091SDouglas Gregor consumeToken(); 95373441091SDouglas Gregor if (Tok.is(MMToken::Star)) 954*dd005f69SDouglas Gregor ActiveModule->InferExportWildcard = true; 95573441091SDouglas Gregor else 95673441091SDouglas Gregor Diags.Report(Tok.getLocation(), 95773441091SDouglas Gregor diag::err_mmap_expected_export_wildcard); 95873441091SDouglas Gregor consumeToken(); 95973441091SDouglas Gregor break; 96073441091SDouglas Gregor } 96173441091SDouglas Gregor 96273441091SDouglas Gregor case MMToken::ExplicitKeyword: 96373441091SDouglas Gregor case MMToken::ModuleKeyword: 96473441091SDouglas Gregor case MMToken::HeaderKeyword: 96573441091SDouglas Gregor case MMToken::UmbrellaKeyword: 96673441091SDouglas Gregor default: 96773441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_wildcard_member); 96873441091SDouglas Gregor consumeToken(); 96973441091SDouglas Gregor break; 97073441091SDouglas Gregor } 97173441091SDouglas Gregor } while (!Done); 97273441091SDouglas Gregor 97373441091SDouglas Gregor if (Tok.is(MMToken::RBrace)) 97473441091SDouglas Gregor consumeToken(); 97573441091SDouglas Gregor else { 97673441091SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace); 97773441091SDouglas Gregor Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match); 97873441091SDouglas Gregor HadError = true; 97973441091SDouglas Gregor } 98073441091SDouglas Gregor } 98173441091SDouglas Gregor 982718292f2SDouglas Gregor /// \brief Parse a module map file. 983718292f2SDouglas Gregor /// 984718292f2SDouglas Gregor /// module-map-file: 985718292f2SDouglas Gregor /// module-declaration* 986718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() { 987718292f2SDouglas Gregor do { 988718292f2SDouglas Gregor switch (Tok.Kind) { 989718292f2SDouglas Gregor case MMToken::EndOfFile: 990718292f2SDouglas Gregor return HadError; 991718292f2SDouglas Gregor 992718292f2SDouglas Gregor case MMToken::ModuleKeyword: 993755b2055SDouglas Gregor case MMToken::FrameworkKeyword: 994718292f2SDouglas Gregor parseModuleDecl(); 995718292f2SDouglas Gregor break; 996718292f2SDouglas Gregor 997718292f2SDouglas Gregor case MMToken::ExplicitKeyword: 9982b82c2a5SDouglas Gregor case MMToken::ExportKeyword: 999718292f2SDouglas Gregor case MMToken::HeaderKeyword: 1000718292f2SDouglas Gregor case MMToken::Identifier: 1001718292f2SDouglas Gregor case MMToken::LBrace: 10022b82c2a5SDouglas Gregor case MMToken::Period: 1003718292f2SDouglas Gregor case MMToken::RBrace: 10042b82c2a5SDouglas Gregor case MMToken::Star: 1005718292f2SDouglas Gregor case MMToken::StringLiteral: 1006718292f2SDouglas Gregor case MMToken::UmbrellaKeyword: 1007718292f2SDouglas Gregor Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module); 1008718292f2SDouglas Gregor HadError = true; 1009718292f2SDouglas Gregor consumeToken(); 1010718292f2SDouglas Gregor break; 1011718292f2SDouglas Gregor } 1012718292f2SDouglas Gregor } while (true); 1013718292f2SDouglas Gregor 1014718292f2SDouglas Gregor return HadError; 1015718292f2SDouglas Gregor } 1016718292f2SDouglas Gregor 1017718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) { 1018718292f2SDouglas Gregor FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User); 1019718292f2SDouglas Gregor const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID); 1020718292f2SDouglas Gregor if (!Buffer) 1021718292f2SDouglas Gregor return true; 1022718292f2SDouglas Gregor 1023718292f2SDouglas Gregor // Parse this module map file. 1024718292f2SDouglas Gregor Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, LangOpts); 1025718292f2SDouglas Gregor Diags->getClient()->BeginSourceFile(LangOpts); 10265257fc63SDouglas Gregor ModuleMapParser Parser(L, *SourceMgr, *Diags, *this, File->getDir()); 1027718292f2SDouglas Gregor bool Result = Parser.parseModuleMapFile(); 1028718292f2SDouglas Gregor Diags->getClient()->EndSourceFile(); 1029718292f2SDouglas Gregor 1030718292f2SDouglas Gregor return Result; 1031718292f2SDouglas Gregor } 1032