1de3ef502SDouglas Gregor //===--- Module.h - Describe a module ---------------------------*- C++ -*-===// 2de3ef502SDouglas Gregor // 3de3ef502SDouglas Gregor // The LLVM Compiler Infrastructure 4de3ef502SDouglas Gregor // 5de3ef502SDouglas Gregor // This file is distributed under the University of Illinois Open Source 6de3ef502SDouglas Gregor // License. See LICENSE.TXT for details. 7de3ef502SDouglas Gregor // 8de3ef502SDouglas Gregor //===----------------------------------------------------------------------===// 9de3ef502SDouglas Gregor // 10de3ef502SDouglas Gregor // This file defines the Module class, which describes a module in the source 11de3ef502SDouglas Gregor // code. 12de3ef502SDouglas Gregor // 13de3ef502SDouglas Gregor //===----------------------------------------------------------------------===// 14de3ef502SDouglas Gregor #include "clang/Basic/Module.h" 15de3ef502SDouglas Gregor #include "clang/Basic/FileManager.h" 161fb5c3a6SDouglas Gregor #include "clang/Basic/LangOptions.h" 171fb5c3a6SDouglas Gregor #include "llvm/Support/ErrorHandling.h" 18de3ef502SDouglas Gregor #include "llvm/Support/raw_ostream.h" 191fb5c3a6SDouglas Gregor #include "llvm/ADT/SmallVector.h" 201fb5c3a6SDouglas Gregor #include "llvm/ADT/StringSwitch.h" 21de3ef502SDouglas Gregor using namespace clang; 22de3ef502SDouglas Gregor 23*eb90e830SDouglas Gregor Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 24*eb90e830SDouglas Gregor bool IsFramework, bool IsExplicit) 25*eb90e830SDouglas Gregor : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), 26*eb90e830SDouglas Gregor Umbrella(), IsAvailable(true), IsFromModuleFile(false), 27*eb90e830SDouglas Gregor IsFramework(IsFramework), IsExplicit(IsExplicit), InferSubmodules(false), 28*eb90e830SDouglas Gregor InferExplicitSubmodules(false), InferExportWildcard(false), 29*eb90e830SDouglas Gregor NameVisibility(Hidden) 30*eb90e830SDouglas Gregor { 31*eb90e830SDouglas Gregor if (Parent) { 32*eb90e830SDouglas Gregor if (!Parent->isAvailable()) 33*eb90e830SDouglas Gregor IsAvailable = false; 34*eb90e830SDouglas Gregor 35*eb90e830SDouglas Gregor Parent->SubModuleIndex[Name] = Parent->SubModules.size(); 36*eb90e830SDouglas Gregor Parent->SubModules.push_back(this); 37*eb90e830SDouglas Gregor } 38*eb90e830SDouglas Gregor } 39*eb90e830SDouglas Gregor 40de3ef502SDouglas Gregor Module::~Module() { 41*eb90e830SDouglas Gregor for (submodule_iterator I = submodule_begin(), IEnd = submodule_end(); 42de3ef502SDouglas Gregor I != IEnd; ++I) { 43*eb90e830SDouglas Gregor delete *I; 44de3ef502SDouglas Gregor } 45de3ef502SDouglas Gregor 46de3ef502SDouglas Gregor } 47de3ef502SDouglas Gregor 481fb5c3a6SDouglas Gregor /// \brief Determine whether a translation unit built using the current 491fb5c3a6SDouglas Gregor /// language options has the given feature. 501fb5c3a6SDouglas Gregor static bool hasFeature(StringRef Feature, const LangOptions &LangOpts) { 511fb5c3a6SDouglas Gregor return llvm::StringSwitch<bool>(Feature) 521fb5c3a6SDouglas Gregor .Case("blocks", LangOpts.Blocks) 531fb5c3a6SDouglas Gregor .Case("cplusplus", LangOpts.CPlusPlus) 541fb5c3a6SDouglas Gregor .Case("cplusplus11", LangOpts.CPlusPlus0x) 551fb5c3a6SDouglas Gregor .Case("objc", LangOpts.ObjC1) 561fb5c3a6SDouglas Gregor .Case("objc_arc", LangOpts.ObjCAutoRefCount) 571fb5c3a6SDouglas Gregor .Default(false); 581fb5c3a6SDouglas Gregor } 591fb5c3a6SDouglas Gregor 601fb5c3a6SDouglas Gregor bool 611fb5c3a6SDouglas Gregor Module::isAvailable(const LangOptions &LangOpts, StringRef &Feature) const { 621fb5c3a6SDouglas Gregor if (IsAvailable) 631fb5c3a6SDouglas Gregor return true; 641fb5c3a6SDouglas Gregor 651fb5c3a6SDouglas Gregor for (const Module *Current = this; Current; Current = Current->Parent) { 661fb5c3a6SDouglas Gregor for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) { 671fb5c3a6SDouglas Gregor if (!hasFeature(Current->Requires[I], LangOpts)) { 681fb5c3a6SDouglas Gregor Feature = Current->Requires[I]; 691fb5c3a6SDouglas Gregor return false; 701fb5c3a6SDouglas Gregor } 711fb5c3a6SDouglas Gregor } 721fb5c3a6SDouglas Gregor } 731fb5c3a6SDouglas Gregor 741fb5c3a6SDouglas Gregor llvm_unreachable("could not find a reason why module is unavailable"); 751fb5c3a6SDouglas Gregor return false; 761fb5c3a6SDouglas Gregor } 771fb5c3a6SDouglas Gregor 78f5eedd05SDouglas Gregor bool Module::isSubModuleOf(Module *Other) const { 79f5eedd05SDouglas Gregor const Module *This = this; 80f5eedd05SDouglas Gregor do { 81f5eedd05SDouglas Gregor if (This == Other) 82f5eedd05SDouglas Gregor return true; 83f5eedd05SDouglas Gregor 84f5eedd05SDouglas Gregor This = This->Parent; 85f5eedd05SDouglas Gregor } while (This); 86f5eedd05SDouglas Gregor 87f5eedd05SDouglas Gregor return false; 88f5eedd05SDouglas Gregor } 89f5eedd05SDouglas Gregor 9073441091SDouglas Gregor const Module *Module::getTopLevelModule() const { 9173441091SDouglas Gregor const Module *Result = this; 9273441091SDouglas Gregor while (Result->Parent) 9373441091SDouglas Gregor Result = Result->Parent; 9473441091SDouglas Gregor 9573441091SDouglas Gregor return Result; 9673441091SDouglas Gregor } 9773441091SDouglas Gregor 98de3ef502SDouglas Gregor std::string Module::getFullModuleName() const { 99de3ef502SDouglas Gregor llvm::SmallVector<StringRef, 2> Names; 100de3ef502SDouglas Gregor 101de3ef502SDouglas Gregor // Build up the set of module names (from innermost to outermost). 102de3ef502SDouglas Gregor for (const Module *M = this; M; M = M->Parent) 103de3ef502SDouglas Gregor Names.push_back(M->Name); 104de3ef502SDouglas Gregor 105de3ef502SDouglas Gregor std::string Result; 106de3ef502SDouglas Gregor for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(), 107de3ef502SDouglas Gregor IEnd = Names.rend(); 108de3ef502SDouglas Gregor I != IEnd; ++I) { 109de3ef502SDouglas Gregor if (!Result.empty()) 110de3ef502SDouglas Gregor Result += '.'; 111de3ef502SDouglas Gregor 112de3ef502SDouglas Gregor Result += *I; 113de3ef502SDouglas Gregor } 114de3ef502SDouglas Gregor 115de3ef502SDouglas Gregor return Result; 116de3ef502SDouglas Gregor } 117de3ef502SDouglas Gregor 11873141fa9SDouglas Gregor const DirectoryEntry *Module::getUmbrellaDir() const { 11973141fa9SDouglas Gregor if (const FileEntry *Header = getUmbrellaHeader()) 12073141fa9SDouglas Gregor return Header->getDir(); 12173141fa9SDouglas Gregor 12273141fa9SDouglas Gregor return Umbrella.dyn_cast<const DirectoryEntry *>(); 12373141fa9SDouglas Gregor } 12473141fa9SDouglas Gregor 1251fb5c3a6SDouglas Gregor void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts) { 1261fb5c3a6SDouglas Gregor Requires.push_back(Feature); 1271fb5c3a6SDouglas Gregor 1281fb5c3a6SDouglas Gregor // If this feature is currently available, we're done. 1291fb5c3a6SDouglas Gregor if (hasFeature(Feature, LangOpts)) 1301fb5c3a6SDouglas Gregor return; 1311fb5c3a6SDouglas Gregor 1321fb5c3a6SDouglas Gregor if (!IsAvailable) 1331fb5c3a6SDouglas Gregor return; 1341fb5c3a6SDouglas Gregor 1351fb5c3a6SDouglas Gregor llvm::SmallVector<Module *, 2> Stack; 1361fb5c3a6SDouglas Gregor Stack.push_back(this); 1371fb5c3a6SDouglas Gregor while (!Stack.empty()) { 1381fb5c3a6SDouglas Gregor Module *Current = Stack.back(); 1391fb5c3a6SDouglas Gregor Stack.pop_back(); 1401fb5c3a6SDouglas Gregor 1411fb5c3a6SDouglas Gregor if (!Current->IsAvailable) 1421fb5c3a6SDouglas Gregor continue; 1431fb5c3a6SDouglas Gregor 1441fb5c3a6SDouglas Gregor Current->IsAvailable = false; 145*eb90e830SDouglas Gregor for (submodule_iterator Sub = Current->submodule_begin(), 146*eb90e830SDouglas Gregor SubEnd = Current->submodule_end(); 1471fb5c3a6SDouglas Gregor Sub != SubEnd; ++Sub) { 148*eb90e830SDouglas Gregor if ((*Sub)->IsAvailable) 149*eb90e830SDouglas Gregor Stack.push_back(*Sub); 1501fb5c3a6SDouglas Gregor } 1511fb5c3a6SDouglas Gregor } 1521fb5c3a6SDouglas Gregor } 1531fb5c3a6SDouglas Gregor 154*eb90e830SDouglas Gregor Module *Module::findSubmodule(StringRef Name) const { 155*eb90e830SDouglas Gregor llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); 156*eb90e830SDouglas Gregor if (Pos == SubModuleIndex.end()) 157*eb90e830SDouglas Gregor return 0; 158*eb90e830SDouglas Gregor 159*eb90e830SDouglas Gregor return SubModules[Pos->getValue()]; 160*eb90e830SDouglas Gregor } 161*eb90e830SDouglas Gregor 16224bb923aSDouglas Gregor static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) { 16324bb923aSDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 16424bb923aSDouglas Gregor if (I) 16524bb923aSDouglas Gregor OS << "."; 16624bb923aSDouglas Gregor OS << Id[I].first; 16724bb923aSDouglas Gregor } 16824bb923aSDouglas Gregor } 16924bb923aSDouglas Gregor 170de3ef502SDouglas Gregor void Module::print(llvm::raw_ostream &OS, unsigned Indent) const { 171de3ef502SDouglas Gregor OS.indent(Indent); 172de3ef502SDouglas Gregor if (IsFramework) 173de3ef502SDouglas Gregor OS << "framework "; 174de3ef502SDouglas Gregor if (IsExplicit) 175de3ef502SDouglas Gregor OS << "explicit "; 176de3ef502SDouglas Gregor OS << "module " << Name << " {\n"; 177de3ef502SDouglas Gregor 1781fb5c3a6SDouglas Gregor if (!Requires.empty()) { 1791fb5c3a6SDouglas Gregor OS.indent(Indent + 2); 1801fb5c3a6SDouglas Gregor OS << "requires "; 1811fb5c3a6SDouglas Gregor for (unsigned I = 0, N = Requires.size(); I != N; ++I) { 1821fb5c3a6SDouglas Gregor if (I) 1831fb5c3a6SDouglas Gregor OS << ", "; 1841fb5c3a6SDouglas Gregor OS << Requires[I]; 1851fb5c3a6SDouglas Gregor } 1861fb5c3a6SDouglas Gregor OS << "\n"; 1871fb5c3a6SDouglas Gregor } 1881fb5c3a6SDouglas Gregor 18973141fa9SDouglas Gregor if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) { 190de3ef502SDouglas Gregor OS.indent(Indent + 2); 191322f633cSDouglas Gregor OS << "umbrella header \""; 192de3ef502SDouglas Gregor OS.write_escaped(UmbrellaHeader->getName()); 193de3ef502SDouglas Gregor OS << "\"\n"; 194322f633cSDouglas Gregor } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) { 195322f633cSDouglas Gregor OS.indent(Indent + 2); 196322f633cSDouglas Gregor OS << "umbrella \""; 197322f633cSDouglas Gregor OS.write_escaped(UmbrellaDir->getName()); 198322f633cSDouglas Gregor OS << "\"\n"; 199de3ef502SDouglas Gregor } 200de3ef502SDouglas Gregor 201de3ef502SDouglas Gregor for (unsigned I = 0, N = Headers.size(); I != N; ++I) { 202de3ef502SDouglas Gregor OS.indent(Indent + 2); 203de3ef502SDouglas Gregor OS << "header \""; 204de3ef502SDouglas Gregor OS.write_escaped(Headers[I]->getName()); 205de3ef502SDouglas Gregor OS << "\"\n"; 206de3ef502SDouglas Gregor } 207de3ef502SDouglas Gregor 208*eb90e830SDouglas Gregor for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); 209de3ef502SDouglas Gregor MI != MIEnd; ++MI) 210*eb90e830SDouglas Gregor (*MI)->print(OS, Indent + 2); 211de3ef502SDouglas Gregor 21224bb923aSDouglas Gregor for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 21324bb923aSDouglas Gregor OS.indent(Indent + 2); 2148c7c8352SDouglas Gregor OS << "export "; 2158c7c8352SDouglas Gregor if (Module *Restriction = Exports[I].getPointer()) { 2168c7c8352SDouglas Gregor OS << Restriction->getFullModuleName(); 21724bb923aSDouglas Gregor if (Exports[I].getInt()) 21824bb923aSDouglas Gregor OS << ".*"; 2198c7c8352SDouglas Gregor } else { 2208c7c8352SDouglas Gregor OS << "*"; 2218c7c8352SDouglas Gregor } 22224bb923aSDouglas Gregor OS << "\n"; 22324bb923aSDouglas Gregor } 22424bb923aSDouglas Gregor 22524bb923aSDouglas Gregor for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { 22624bb923aSDouglas Gregor OS.indent(Indent + 2); 22724bb923aSDouglas Gregor OS << "export "; 22824bb923aSDouglas Gregor printModuleId(OS, UnresolvedExports[I].Id); 2298c7c8352SDouglas Gregor if (UnresolvedExports[I].Wildcard) { 2308c7c8352SDouglas Gregor if (UnresolvedExports[I].Id.empty()) 2318c7c8352SDouglas Gregor OS << "*"; 2328c7c8352SDouglas Gregor else 23324bb923aSDouglas Gregor OS << ".*"; 2348c7c8352SDouglas Gregor } 23524bb923aSDouglas Gregor OS << "\n"; 23624bb923aSDouglas Gregor } 23724bb923aSDouglas Gregor 23873441091SDouglas Gregor if (InferSubmodules) { 23973441091SDouglas Gregor OS.indent(Indent + 2); 24073441091SDouglas Gregor if (InferExplicitSubmodules) 24173441091SDouglas Gregor OS << "explicit "; 24273441091SDouglas Gregor OS << "module * {\n"; 24373441091SDouglas Gregor if (InferExportWildcard) { 24473441091SDouglas Gregor OS.indent(Indent + 4); 24573441091SDouglas Gregor OS << "export *\n"; 24673441091SDouglas Gregor } 24773441091SDouglas Gregor OS.indent(Indent + 2); 24873441091SDouglas Gregor OS << "}\n"; 24973441091SDouglas Gregor } 25073441091SDouglas Gregor 251de3ef502SDouglas Gregor OS.indent(Indent); 252de3ef502SDouglas Gregor OS << "}\n"; 253de3ef502SDouglas Gregor } 254de3ef502SDouglas Gregor 255de3ef502SDouglas Gregor void Module::dump() const { 256de3ef502SDouglas Gregor print(llvm::errs()); 257de3ef502SDouglas Gregor } 258de3ef502SDouglas Gregor 259de3ef502SDouglas Gregor 260