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" 16de3ef502SDouglas Gregor #include "llvm/Support/raw_ostream.h" 17de3ef502SDouglas Gregor using namespace clang; 18de3ef502SDouglas Gregor 19de3ef502SDouglas Gregor Module::~Module() { 20de3ef502SDouglas Gregor for (llvm::StringMap<Module *>::iterator I = SubModules.begin(), 21de3ef502SDouglas Gregor IEnd = SubModules.end(); 22de3ef502SDouglas Gregor I != IEnd; ++I) { 23de3ef502SDouglas Gregor delete I->getValue(); 24de3ef502SDouglas Gregor } 25de3ef502SDouglas Gregor 26de3ef502SDouglas Gregor } 27de3ef502SDouglas Gregor 28f5eedd05SDouglas Gregor bool Module::isSubModuleOf(Module *Other) const { 29f5eedd05SDouglas Gregor const Module *This = this; 30f5eedd05SDouglas Gregor do { 31f5eedd05SDouglas Gregor if (This == Other) 32f5eedd05SDouglas Gregor return true; 33f5eedd05SDouglas Gregor 34f5eedd05SDouglas Gregor This = This->Parent; 35f5eedd05SDouglas Gregor } while (This); 36f5eedd05SDouglas Gregor 37f5eedd05SDouglas Gregor return false; 38f5eedd05SDouglas Gregor } 39f5eedd05SDouglas Gregor 40de3ef502SDouglas Gregor std::string Module::getFullModuleName() const { 41de3ef502SDouglas Gregor llvm::SmallVector<StringRef, 2> Names; 42de3ef502SDouglas Gregor 43de3ef502SDouglas Gregor // Build up the set of module names (from innermost to outermost). 44de3ef502SDouglas Gregor for (const Module *M = this; M; M = M->Parent) 45de3ef502SDouglas Gregor Names.push_back(M->Name); 46de3ef502SDouglas Gregor 47de3ef502SDouglas Gregor std::string Result; 48de3ef502SDouglas Gregor for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(), 49de3ef502SDouglas Gregor IEnd = Names.rend(); 50de3ef502SDouglas Gregor I != IEnd; ++I) { 51de3ef502SDouglas Gregor if (!Result.empty()) 52de3ef502SDouglas Gregor Result += '.'; 53de3ef502SDouglas Gregor 54de3ef502SDouglas Gregor Result += *I; 55de3ef502SDouglas Gregor } 56de3ef502SDouglas Gregor 57de3ef502SDouglas Gregor return Result; 58de3ef502SDouglas Gregor } 59de3ef502SDouglas Gregor 60de3ef502SDouglas Gregor StringRef Module::getTopLevelModuleName() const { 61de3ef502SDouglas Gregor const Module *Top = this; 62de3ef502SDouglas Gregor while (Top->Parent) 63de3ef502SDouglas Gregor Top = Top->Parent; 64de3ef502SDouglas Gregor 65de3ef502SDouglas Gregor return Top->Name; 66de3ef502SDouglas Gregor } 67de3ef502SDouglas Gregor 6824bb923aSDouglas Gregor static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) { 6924bb923aSDouglas Gregor for (unsigned I = 0, N = Id.size(); I != N; ++I) { 7024bb923aSDouglas Gregor if (I) 7124bb923aSDouglas Gregor OS << "."; 7224bb923aSDouglas Gregor OS << Id[I].first; 7324bb923aSDouglas Gregor } 7424bb923aSDouglas Gregor } 7524bb923aSDouglas Gregor 76de3ef502SDouglas Gregor void Module::print(llvm::raw_ostream &OS, unsigned Indent) const { 77de3ef502SDouglas Gregor OS.indent(Indent); 78de3ef502SDouglas Gregor if (IsFramework) 79de3ef502SDouglas Gregor OS << "framework "; 80de3ef502SDouglas Gregor if (IsExplicit) 81de3ef502SDouglas Gregor OS << "explicit "; 82de3ef502SDouglas Gregor OS << "module " << Name << " {\n"; 83de3ef502SDouglas Gregor 84de3ef502SDouglas Gregor if (UmbrellaHeader) { 85de3ef502SDouglas Gregor OS.indent(Indent + 2); 86de3ef502SDouglas Gregor OS << "umbrella \""; 87de3ef502SDouglas Gregor OS.write_escaped(UmbrellaHeader->getName()); 88de3ef502SDouglas Gregor OS << "\"\n"; 89de3ef502SDouglas Gregor } 90de3ef502SDouglas Gregor 91de3ef502SDouglas Gregor for (unsigned I = 0, N = Headers.size(); I != N; ++I) { 92de3ef502SDouglas Gregor OS.indent(Indent + 2); 93de3ef502SDouglas Gregor OS << "header \""; 94de3ef502SDouglas Gregor OS.write_escaped(Headers[I]->getName()); 95de3ef502SDouglas Gregor OS << "\"\n"; 96de3ef502SDouglas Gregor } 97de3ef502SDouglas Gregor 98de3ef502SDouglas Gregor for (llvm::StringMap<Module *>::const_iterator MI = SubModules.begin(), 99de3ef502SDouglas Gregor MIEnd = SubModules.end(); 100de3ef502SDouglas Gregor MI != MIEnd; ++MI) 101de3ef502SDouglas Gregor MI->getValue()->print(OS, Indent + 2); 102de3ef502SDouglas Gregor 10324bb923aSDouglas Gregor for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 10424bb923aSDouglas Gregor OS.indent(Indent + 2); 105*8c7c8352SDouglas Gregor OS << "export "; 106*8c7c8352SDouglas Gregor if (Module *Restriction = Exports[I].getPointer()) { 107*8c7c8352SDouglas Gregor OS << Restriction->getFullModuleName(); 10824bb923aSDouglas Gregor if (Exports[I].getInt()) 10924bb923aSDouglas Gregor OS << ".*"; 110*8c7c8352SDouglas Gregor } else { 111*8c7c8352SDouglas Gregor OS << "*"; 112*8c7c8352SDouglas Gregor } 11324bb923aSDouglas Gregor OS << "\n"; 11424bb923aSDouglas Gregor } 11524bb923aSDouglas Gregor 11624bb923aSDouglas Gregor for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { 11724bb923aSDouglas Gregor OS.indent(Indent + 2); 11824bb923aSDouglas Gregor OS << "export "; 11924bb923aSDouglas Gregor printModuleId(OS, UnresolvedExports[I].Id); 120*8c7c8352SDouglas Gregor if (UnresolvedExports[I].Wildcard) { 121*8c7c8352SDouglas Gregor if (UnresolvedExports[I].Id.empty()) 122*8c7c8352SDouglas Gregor OS << "*"; 123*8c7c8352SDouglas Gregor else 12424bb923aSDouglas Gregor OS << ".*"; 125*8c7c8352SDouglas Gregor } 12624bb923aSDouglas Gregor OS << "\n"; 12724bb923aSDouglas Gregor } 12824bb923aSDouglas Gregor 129de3ef502SDouglas Gregor OS.indent(Indent); 130de3ef502SDouglas Gregor OS << "}\n"; 131de3ef502SDouglas Gregor } 132de3ef502SDouglas Gregor 133de3ef502SDouglas Gregor void Module::dump() const { 134de3ef502SDouglas Gregor print(llvm::errs()); 135de3ef502SDouglas Gregor } 136de3ef502SDouglas Gregor 137de3ef502SDouglas Gregor 138