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" 17*0070c0bfSDouglas Gregor #include "clang/Basic/TargetInfo.h" 181fb5c3a6SDouglas Gregor #include "llvm/Support/ErrorHandling.h" 19de3ef502SDouglas Gregor #include "llvm/Support/raw_ostream.h" 201fb5c3a6SDouglas Gregor #include "llvm/ADT/SmallVector.h" 211fb5c3a6SDouglas Gregor #include "llvm/ADT/StringSwitch.h" 22de3ef502SDouglas Gregor using namespace clang; 23de3ef502SDouglas Gregor 24eb90e830SDouglas Gregor Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 25eb90e830SDouglas Gregor bool IsFramework, bool IsExplicit) 26eb90e830SDouglas Gregor : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), 27eb90e830SDouglas Gregor Umbrella(), IsAvailable(true), IsFromModuleFile(false), 28a686e1b0SDouglas Gregor IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false), 29a686e1b0SDouglas Gregor InferSubmodules(false), InferExplicitSubmodules(false), 30a686e1b0SDouglas Gregor InferExportWildcard(false), NameVisibility(Hidden) 31eb90e830SDouglas Gregor { 32eb90e830SDouglas Gregor if (Parent) { 33eb90e830SDouglas Gregor if (!Parent->isAvailable()) 34eb90e830SDouglas Gregor IsAvailable = false; 35eb90e830SDouglas Gregor 36eb90e830SDouglas Gregor Parent->SubModuleIndex[Name] = Parent->SubModules.size(); 37eb90e830SDouglas Gregor Parent->SubModules.push_back(this); 38eb90e830SDouglas Gregor } 39eb90e830SDouglas Gregor } 40eb90e830SDouglas Gregor 41de3ef502SDouglas Gregor Module::~Module() { 42eb90e830SDouglas Gregor for (submodule_iterator I = submodule_begin(), IEnd = submodule_end(); 43de3ef502SDouglas Gregor I != IEnd; ++I) { 44eb90e830SDouglas Gregor delete *I; 45de3ef502SDouglas Gregor } 46de3ef502SDouglas Gregor 47de3ef502SDouglas Gregor } 48de3ef502SDouglas Gregor 491fb5c3a6SDouglas Gregor /// \brief Determine whether a translation unit built using the current 501fb5c3a6SDouglas Gregor /// language options has the given feature. 5189929282SDouglas Gregor static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, 5289929282SDouglas Gregor const TargetInfo &Target) { 531fb5c3a6SDouglas Gregor return llvm::StringSwitch<bool>(Feature) 54*0070c0bfSDouglas Gregor .Case("altivec", LangOpts.AltiVec) 551fb5c3a6SDouglas Gregor .Case("blocks", LangOpts.Blocks) 561fb5c3a6SDouglas Gregor .Case("cplusplus", LangOpts.CPlusPlus) 571fb5c3a6SDouglas Gregor .Case("cplusplus11", LangOpts.CPlusPlus0x) 581fb5c3a6SDouglas Gregor .Case("objc", LangOpts.ObjC1) 591fb5c3a6SDouglas Gregor .Case("objc_arc", LangOpts.ObjCAutoRefCount) 60*0070c0bfSDouglas Gregor .Case("opencl", LangOpts.OpenCL) 61*0070c0bfSDouglas Gregor .Case("tls", Target.isTLSSupported()) 62*0070c0bfSDouglas Gregor .Default(Target.hasFeature(Feature)); 631fb5c3a6SDouglas Gregor } 641fb5c3a6SDouglas Gregor 651fb5c3a6SDouglas Gregor bool 6689929282SDouglas Gregor Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target, 6789929282SDouglas Gregor StringRef &Feature) const { 681fb5c3a6SDouglas Gregor if (IsAvailable) 691fb5c3a6SDouglas Gregor return true; 701fb5c3a6SDouglas Gregor 711fb5c3a6SDouglas Gregor for (const Module *Current = this; Current; Current = Current->Parent) { 721fb5c3a6SDouglas Gregor for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) { 7389929282SDouglas Gregor if (!hasFeature(Current->Requires[I], LangOpts, Target)) { 741fb5c3a6SDouglas Gregor Feature = Current->Requires[I]; 751fb5c3a6SDouglas Gregor return false; 761fb5c3a6SDouglas Gregor } 771fb5c3a6SDouglas Gregor } 781fb5c3a6SDouglas Gregor } 791fb5c3a6SDouglas Gregor 801fb5c3a6SDouglas Gregor llvm_unreachable("could not find a reason why module is unavailable"); 811fb5c3a6SDouglas Gregor } 821fb5c3a6SDouglas Gregor 83f5eedd05SDouglas Gregor bool Module::isSubModuleOf(Module *Other) const { 84f5eedd05SDouglas Gregor const Module *This = this; 85f5eedd05SDouglas Gregor do { 86f5eedd05SDouglas Gregor if (This == Other) 87f5eedd05SDouglas Gregor return true; 88f5eedd05SDouglas Gregor 89f5eedd05SDouglas Gregor This = This->Parent; 90f5eedd05SDouglas Gregor } while (This); 91f5eedd05SDouglas Gregor 92f5eedd05SDouglas Gregor return false; 93f5eedd05SDouglas Gregor } 94f5eedd05SDouglas Gregor 9573441091SDouglas Gregor const Module *Module::getTopLevelModule() const { 9673441091SDouglas Gregor const Module *Result = this; 9773441091SDouglas Gregor while (Result->Parent) 9873441091SDouglas Gregor Result = Result->Parent; 9973441091SDouglas Gregor 10073441091SDouglas Gregor return Result; 10173441091SDouglas Gregor } 10273441091SDouglas Gregor 103de3ef502SDouglas Gregor std::string Module::getFullModuleName() const { 104de3ef502SDouglas Gregor llvm::SmallVector<StringRef, 2> Names; 105de3ef502SDouglas Gregor 106de3ef502SDouglas Gregor // Build up the set of module names (from innermost to outermost). 107de3ef502SDouglas Gregor for (const Module *M = this; M; M = M->Parent) 108de3ef502SDouglas Gregor Names.push_back(M->Name); 109de3ef502SDouglas Gregor 110de3ef502SDouglas Gregor std::string Result; 111de3ef502SDouglas Gregor for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(), 112de3ef502SDouglas Gregor IEnd = Names.rend(); 113de3ef502SDouglas Gregor I != IEnd; ++I) { 114de3ef502SDouglas Gregor if (!Result.empty()) 115de3ef502SDouglas Gregor Result += '.'; 116de3ef502SDouglas Gregor 117de3ef502SDouglas Gregor Result += *I; 118de3ef502SDouglas Gregor } 119de3ef502SDouglas Gregor 120de3ef502SDouglas Gregor return Result; 121de3ef502SDouglas Gregor } 122de3ef502SDouglas Gregor 12373141fa9SDouglas Gregor const DirectoryEntry *Module::getUmbrellaDir() const { 12473141fa9SDouglas Gregor if (const FileEntry *Header = getUmbrellaHeader()) 12573141fa9SDouglas Gregor return Header->getDir(); 12673141fa9SDouglas Gregor 12773141fa9SDouglas Gregor return Umbrella.dyn_cast<const DirectoryEntry *>(); 12873141fa9SDouglas Gregor } 12973141fa9SDouglas Gregor 13089929282SDouglas Gregor void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts, 13189929282SDouglas Gregor const TargetInfo &Target) { 1321fb5c3a6SDouglas Gregor Requires.push_back(Feature); 1331fb5c3a6SDouglas Gregor 1341fb5c3a6SDouglas Gregor // If this feature is currently available, we're done. 13589929282SDouglas Gregor if (hasFeature(Feature, LangOpts, Target)) 1361fb5c3a6SDouglas Gregor return; 1371fb5c3a6SDouglas Gregor 1381fb5c3a6SDouglas Gregor if (!IsAvailable) 1391fb5c3a6SDouglas Gregor return; 1401fb5c3a6SDouglas Gregor 1411fb5c3a6SDouglas Gregor llvm::SmallVector<Module *, 2> Stack; 1421fb5c3a6SDouglas Gregor Stack.push_back(this); 1431fb5c3a6SDouglas Gregor while (!Stack.empty()) { 1441fb5c3a6SDouglas Gregor Module *Current = Stack.back(); 1451fb5c3a6SDouglas Gregor Stack.pop_back(); 1461fb5c3a6SDouglas Gregor 1471fb5c3a6SDouglas Gregor if (!Current->IsAvailable) 1481fb5c3a6SDouglas Gregor continue; 1491fb5c3a6SDouglas Gregor 1501fb5c3a6SDouglas Gregor Current->IsAvailable = false; 151eb90e830SDouglas Gregor for (submodule_iterator Sub = Current->submodule_begin(), 152eb90e830SDouglas Gregor SubEnd = Current->submodule_end(); 1531fb5c3a6SDouglas Gregor Sub != SubEnd; ++Sub) { 154eb90e830SDouglas Gregor if ((*Sub)->IsAvailable) 155eb90e830SDouglas Gregor Stack.push_back(*Sub); 1561fb5c3a6SDouglas Gregor } 1571fb5c3a6SDouglas Gregor } 1581fb5c3a6SDouglas Gregor } 1591fb5c3a6SDouglas Gregor 160eb90e830SDouglas Gregor Module *Module::findSubmodule(StringRef Name) const { 161eb90e830SDouglas Gregor llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); 162eb90e830SDouglas Gregor if (Pos == SubModuleIndex.end()) 163eb90e830SDouglas Gregor return 0; 164eb90e830SDouglas Gregor 165eb90e830SDouglas Gregor return SubModules[Pos->getValue()]; 166eb90e830SDouglas Gregor } 167eb90e830SDouglas Gregor 16824bb923aSDouglas Gregor static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) { 16924bb923aSDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 17024bb923aSDouglas Gregor if (I) 17124bb923aSDouglas Gregor OS << "."; 17224bb923aSDouglas Gregor OS << Id[I].first; 17324bb923aSDouglas Gregor } 17424bb923aSDouglas Gregor } 17524bb923aSDouglas Gregor 176de3ef502SDouglas Gregor void Module::print(llvm::raw_ostream &OS, unsigned Indent) const { 177de3ef502SDouglas Gregor OS.indent(Indent); 178de3ef502SDouglas Gregor if (IsFramework) 179de3ef502SDouglas Gregor OS << "framework "; 180de3ef502SDouglas Gregor if (IsExplicit) 181de3ef502SDouglas Gregor OS << "explicit "; 182a686e1b0SDouglas Gregor OS << "module " << Name; 183a686e1b0SDouglas Gregor 184a686e1b0SDouglas Gregor if (IsSystem) { 185a686e1b0SDouglas Gregor OS.indent(Indent + 2); 186a686e1b0SDouglas Gregor OS << " [system]"; 187a686e1b0SDouglas Gregor } 188a686e1b0SDouglas Gregor 189a686e1b0SDouglas Gregor OS << " {\n"; 190de3ef502SDouglas Gregor 1911fb5c3a6SDouglas Gregor if (!Requires.empty()) { 1921fb5c3a6SDouglas Gregor OS.indent(Indent + 2); 1931fb5c3a6SDouglas Gregor OS << "requires "; 1941fb5c3a6SDouglas Gregor for (unsigned I = 0, N = Requires.size(); I != N; ++I) { 1951fb5c3a6SDouglas Gregor if (I) 1961fb5c3a6SDouglas Gregor OS << ", "; 1971fb5c3a6SDouglas Gregor OS << Requires[I]; 1981fb5c3a6SDouglas Gregor } 1991fb5c3a6SDouglas Gregor OS << "\n"; 2001fb5c3a6SDouglas Gregor } 2011fb5c3a6SDouglas Gregor 20273141fa9SDouglas Gregor if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) { 203de3ef502SDouglas Gregor OS.indent(Indent + 2); 204322f633cSDouglas Gregor OS << "umbrella header \""; 205de3ef502SDouglas Gregor OS.write_escaped(UmbrellaHeader->getName()); 206de3ef502SDouglas Gregor OS << "\"\n"; 207322f633cSDouglas Gregor } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) { 208322f633cSDouglas Gregor OS.indent(Indent + 2); 209322f633cSDouglas Gregor OS << "umbrella \""; 210322f633cSDouglas Gregor OS.write_escaped(UmbrellaDir->getName()); 211322f633cSDouglas Gregor OS << "\"\n"; 212de3ef502SDouglas Gregor } 213de3ef502SDouglas Gregor 214de3ef502SDouglas Gregor for (unsigned I = 0, N = Headers.size(); I != N; ++I) { 215de3ef502SDouglas Gregor OS.indent(Indent + 2); 216de3ef502SDouglas Gregor OS << "header \""; 217de3ef502SDouglas Gregor OS.write_escaped(Headers[I]->getName()); 218de3ef502SDouglas Gregor OS << "\"\n"; 219de3ef502SDouglas Gregor } 220de3ef502SDouglas Gregor 221eb90e830SDouglas Gregor for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); 222de3ef502SDouglas Gregor MI != MIEnd; ++MI) 223eb90e830SDouglas Gregor (*MI)->print(OS, Indent + 2); 224de3ef502SDouglas Gregor 22524bb923aSDouglas Gregor for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 22624bb923aSDouglas Gregor OS.indent(Indent + 2); 2278c7c8352SDouglas Gregor OS << "export "; 2288c7c8352SDouglas Gregor if (Module *Restriction = Exports[I].getPointer()) { 2298c7c8352SDouglas Gregor OS << Restriction->getFullModuleName(); 23024bb923aSDouglas Gregor if (Exports[I].getInt()) 23124bb923aSDouglas Gregor OS << ".*"; 2328c7c8352SDouglas Gregor } else { 2338c7c8352SDouglas Gregor OS << "*"; 2348c7c8352SDouglas Gregor } 23524bb923aSDouglas Gregor OS << "\n"; 23624bb923aSDouglas Gregor } 23724bb923aSDouglas Gregor 23824bb923aSDouglas Gregor for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { 23924bb923aSDouglas Gregor OS.indent(Indent + 2); 24024bb923aSDouglas Gregor OS << "export "; 24124bb923aSDouglas Gregor printModuleId(OS, UnresolvedExports[I].Id); 2428c7c8352SDouglas Gregor if (UnresolvedExports[I].Wildcard) { 2438c7c8352SDouglas Gregor if (UnresolvedExports[I].Id.empty()) 2448c7c8352SDouglas Gregor OS << "*"; 2458c7c8352SDouglas Gregor else 24624bb923aSDouglas Gregor OS << ".*"; 2478c7c8352SDouglas Gregor } 24824bb923aSDouglas Gregor OS << "\n"; 24924bb923aSDouglas Gregor } 25024bb923aSDouglas Gregor 25173441091SDouglas Gregor if (InferSubmodules) { 25273441091SDouglas Gregor OS.indent(Indent + 2); 25373441091SDouglas Gregor if (InferExplicitSubmodules) 25473441091SDouglas Gregor OS << "explicit "; 25573441091SDouglas Gregor OS << "module * {\n"; 25673441091SDouglas Gregor if (InferExportWildcard) { 25773441091SDouglas Gregor OS.indent(Indent + 4); 25873441091SDouglas Gregor OS << "export *\n"; 25973441091SDouglas Gregor } 26073441091SDouglas Gregor OS.indent(Indent + 2); 26173441091SDouglas Gregor OS << "}\n"; 26273441091SDouglas Gregor } 26373441091SDouglas Gregor 264de3ef502SDouglas Gregor OS.indent(Indent); 265de3ef502SDouglas Gregor OS << "}\n"; 266de3ef502SDouglas Gregor } 267de3ef502SDouglas Gregor 268de3ef502SDouglas Gregor void Module::dump() const { 269de3ef502SDouglas Gregor print(llvm::errs()); 270de3ef502SDouglas Gregor } 271de3ef502SDouglas Gregor 272de3ef502SDouglas Gregor 273