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 
23eb90e830SDouglas Gregor Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
24eb90e830SDouglas Gregor                bool IsFramework, bool IsExplicit)
25eb90e830SDouglas Gregor   : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
26eb90e830SDouglas Gregor     Umbrella(), IsAvailable(true), IsFromModuleFile(false),
27a686e1b0SDouglas Gregor     IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
28a686e1b0SDouglas Gregor     InferSubmodules(false), InferExplicitSubmodules(false),
29a686e1b0SDouglas Gregor     InferExportWildcard(false), NameVisibility(Hidden)
30eb90e830SDouglas Gregor {
31eb90e830SDouglas Gregor   if (Parent) {
32eb90e830SDouglas Gregor     if (!Parent->isAvailable())
33eb90e830SDouglas Gregor       IsAvailable = false;
34eb90e830SDouglas Gregor 
35eb90e830SDouglas Gregor     Parent->SubModuleIndex[Name] = Parent->SubModules.size();
36eb90e830SDouglas Gregor     Parent->SubModules.push_back(this);
37eb90e830SDouglas Gregor   }
38eb90e830SDouglas Gregor }
39eb90e830SDouglas Gregor 
40de3ef502SDouglas Gregor Module::~Module() {
41eb90e830SDouglas Gregor   for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
42de3ef502SDouglas Gregor        I != IEnd; ++I) {
43eb90e830SDouglas 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.
50*89929282SDouglas Gregor static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
51*89929282SDouglas Gregor                        const TargetInfo &Target) {
521fb5c3a6SDouglas Gregor   return llvm::StringSwitch<bool>(Feature)
531fb5c3a6SDouglas Gregor            .Case("blocks", LangOpts.Blocks)
541fb5c3a6SDouglas Gregor            .Case("cplusplus", LangOpts.CPlusPlus)
551fb5c3a6SDouglas Gregor            .Case("cplusplus11", LangOpts.CPlusPlus0x)
561fb5c3a6SDouglas Gregor            .Case("objc", LangOpts.ObjC1)
571fb5c3a6SDouglas Gregor            .Case("objc_arc", LangOpts.ObjCAutoRefCount)
581fb5c3a6SDouglas Gregor            .Default(false);
591fb5c3a6SDouglas Gregor }
601fb5c3a6SDouglas Gregor 
611fb5c3a6SDouglas Gregor bool
62*89929282SDouglas Gregor Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
63*89929282SDouglas Gregor                     StringRef &Feature) const {
641fb5c3a6SDouglas Gregor   if (IsAvailable)
651fb5c3a6SDouglas Gregor     return true;
661fb5c3a6SDouglas Gregor 
671fb5c3a6SDouglas Gregor   for (const Module *Current = this; Current; Current = Current->Parent) {
681fb5c3a6SDouglas Gregor     for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
69*89929282SDouglas Gregor       if (!hasFeature(Current->Requires[I], LangOpts, Target)) {
701fb5c3a6SDouglas Gregor         Feature = Current->Requires[I];
711fb5c3a6SDouglas Gregor         return false;
721fb5c3a6SDouglas Gregor       }
731fb5c3a6SDouglas Gregor     }
741fb5c3a6SDouglas Gregor   }
751fb5c3a6SDouglas Gregor 
761fb5c3a6SDouglas Gregor   llvm_unreachable("could not find a reason why module is unavailable");
771fb5c3a6SDouglas Gregor }
781fb5c3a6SDouglas Gregor 
79f5eedd05SDouglas Gregor bool Module::isSubModuleOf(Module *Other) const {
80f5eedd05SDouglas Gregor   const Module *This = this;
81f5eedd05SDouglas Gregor   do {
82f5eedd05SDouglas Gregor     if (This == Other)
83f5eedd05SDouglas Gregor       return true;
84f5eedd05SDouglas Gregor 
85f5eedd05SDouglas Gregor     This = This->Parent;
86f5eedd05SDouglas Gregor   } while (This);
87f5eedd05SDouglas Gregor 
88f5eedd05SDouglas Gregor   return false;
89f5eedd05SDouglas Gregor }
90f5eedd05SDouglas Gregor 
9173441091SDouglas Gregor const Module *Module::getTopLevelModule() const {
9273441091SDouglas Gregor   const Module *Result = this;
9373441091SDouglas Gregor   while (Result->Parent)
9473441091SDouglas Gregor     Result = Result->Parent;
9573441091SDouglas Gregor 
9673441091SDouglas Gregor   return Result;
9773441091SDouglas Gregor }
9873441091SDouglas Gregor 
99de3ef502SDouglas Gregor std::string Module::getFullModuleName() const {
100de3ef502SDouglas Gregor   llvm::SmallVector<StringRef, 2> Names;
101de3ef502SDouglas Gregor 
102de3ef502SDouglas Gregor   // Build up the set of module names (from innermost to outermost).
103de3ef502SDouglas Gregor   for (const Module *M = this; M; M = M->Parent)
104de3ef502SDouglas Gregor     Names.push_back(M->Name);
105de3ef502SDouglas Gregor 
106de3ef502SDouglas Gregor   std::string Result;
107de3ef502SDouglas Gregor   for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
108de3ef502SDouglas Gregor                                                       IEnd = Names.rend();
109de3ef502SDouglas Gregor        I != IEnd; ++I) {
110de3ef502SDouglas Gregor     if (!Result.empty())
111de3ef502SDouglas Gregor       Result += '.';
112de3ef502SDouglas Gregor 
113de3ef502SDouglas Gregor     Result += *I;
114de3ef502SDouglas Gregor   }
115de3ef502SDouglas Gregor 
116de3ef502SDouglas Gregor   return Result;
117de3ef502SDouglas Gregor }
118de3ef502SDouglas Gregor 
11973141fa9SDouglas Gregor const DirectoryEntry *Module::getUmbrellaDir() const {
12073141fa9SDouglas Gregor   if (const FileEntry *Header = getUmbrellaHeader())
12173141fa9SDouglas Gregor     return Header->getDir();
12273141fa9SDouglas Gregor 
12373141fa9SDouglas Gregor   return Umbrella.dyn_cast<const DirectoryEntry *>();
12473141fa9SDouglas Gregor }
12573141fa9SDouglas Gregor 
126*89929282SDouglas Gregor void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts,
127*89929282SDouglas Gregor                             const TargetInfo &Target) {
1281fb5c3a6SDouglas Gregor   Requires.push_back(Feature);
1291fb5c3a6SDouglas Gregor 
1301fb5c3a6SDouglas Gregor   // If this feature is currently available, we're done.
131*89929282SDouglas Gregor   if (hasFeature(Feature, LangOpts, Target))
1321fb5c3a6SDouglas Gregor     return;
1331fb5c3a6SDouglas Gregor 
1341fb5c3a6SDouglas Gregor   if (!IsAvailable)
1351fb5c3a6SDouglas Gregor     return;
1361fb5c3a6SDouglas Gregor 
1371fb5c3a6SDouglas Gregor   llvm::SmallVector<Module *, 2> Stack;
1381fb5c3a6SDouglas Gregor   Stack.push_back(this);
1391fb5c3a6SDouglas Gregor   while (!Stack.empty()) {
1401fb5c3a6SDouglas Gregor     Module *Current = Stack.back();
1411fb5c3a6SDouglas Gregor     Stack.pop_back();
1421fb5c3a6SDouglas Gregor 
1431fb5c3a6SDouglas Gregor     if (!Current->IsAvailable)
1441fb5c3a6SDouglas Gregor       continue;
1451fb5c3a6SDouglas Gregor 
1461fb5c3a6SDouglas Gregor     Current->IsAvailable = false;
147eb90e830SDouglas Gregor     for (submodule_iterator Sub = Current->submodule_begin(),
148eb90e830SDouglas Gregor                          SubEnd = Current->submodule_end();
1491fb5c3a6SDouglas Gregor          Sub != SubEnd; ++Sub) {
150eb90e830SDouglas Gregor       if ((*Sub)->IsAvailable)
151eb90e830SDouglas Gregor         Stack.push_back(*Sub);
1521fb5c3a6SDouglas Gregor     }
1531fb5c3a6SDouglas Gregor   }
1541fb5c3a6SDouglas Gregor }
1551fb5c3a6SDouglas Gregor 
156eb90e830SDouglas Gregor Module *Module::findSubmodule(StringRef Name) const {
157eb90e830SDouglas Gregor   llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
158eb90e830SDouglas Gregor   if (Pos == SubModuleIndex.end())
159eb90e830SDouglas Gregor     return 0;
160eb90e830SDouglas Gregor 
161eb90e830SDouglas Gregor   return SubModules[Pos->getValue()];
162eb90e830SDouglas Gregor }
163eb90e830SDouglas Gregor 
16424bb923aSDouglas Gregor static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
16524bb923aSDouglas Gregor   for (unsigned I = 0, N = Id.size(); I != N; ++I) {
16624bb923aSDouglas Gregor     if (I)
16724bb923aSDouglas Gregor       OS << ".";
16824bb923aSDouglas Gregor     OS << Id[I].first;
16924bb923aSDouglas Gregor   }
17024bb923aSDouglas Gregor }
17124bb923aSDouglas Gregor 
172de3ef502SDouglas Gregor void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
173de3ef502SDouglas Gregor   OS.indent(Indent);
174de3ef502SDouglas Gregor   if (IsFramework)
175de3ef502SDouglas Gregor     OS << "framework ";
176de3ef502SDouglas Gregor   if (IsExplicit)
177de3ef502SDouglas Gregor     OS << "explicit ";
178a686e1b0SDouglas Gregor   OS << "module " << Name;
179a686e1b0SDouglas Gregor 
180a686e1b0SDouglas Gregor   if (IsSystem) {
181a686e1b0SDouglas Gregor     OS.indent(Indent + 2);
182a686e1b0SDouglas Gregor     OS << " [system]";
183a686e1b0SDouglas Gregor   }
184a686e1b0SDouglas Gregor 
185a686e1b0SDouglas Gregor   OS << " {\n";
186de3ef502SDouglas Gregor 
1871fb5c3a6SDouglas Gregor   if (!Requires.empty()) {
1881fb5c3a6SDouglas Gregor     OS.indent(Indent + 2);
1891fb5c3a6SDouglas Gregor     OS << "requires ";
1901fb5c3a6SDouglas Gregor     for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
1911fb5c3a6SDouglas Gregor       if (I)
1921fb5c3a6SDouglas Gregor         OS << ", ";
1931fb5c3a6SDouglas Gregor       OS << Requires[I];
1941fb5c3a6SDouglas Gregor     }
1951fb5c3a6SDouglas Gregor     OS << "\n";
1961fb5c3a6SDouglas Gregor   }
1971fb5c3a6SDouglas Gregor 
19873141fa9SDouglas Gregor   if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
199de3ef502SDouglas Gregor     OS.indent(Indent + 2);
200322f633cSDouglas Gregor     OS << "umbrella header \"";
201de3ef502SDouglas Gregor     OS.write_escaped(UmbrellaHeader->getName());
202de3ef502SDouglas Gregor     OS << "\"\n";
203322f633cSDouglas Gregor   } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
204322f633cSDouglas Gregor     OS.indent(Indent + 2);
205322f633cSDouglas Gregor     OS << "umbrella \"";
206322f633cSDouglas Gregor     OS.write_escaped(UmbrellaDir->getName());
207322f633cSDouglas Gregor     OS << "\"\n";
208de3ef502SDouglas Gregor   }
209de3ef502SDouglas Gregor 
210de3ef502SDouglas Gregor   for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
211de3ef502SDouglas Gregor     OS.indent(Indent + 2);
212de3ef502SDouglas Gregor     OS << "header \"";
213de3ef502SDouglas Gregor     OS.write_escaped(Headers[I]->getName());
214de3ef502SDouglas Gregor     OS << "\"\n";
215de3ef502SDouglas Gregor   }
216de3ef502SDouglas Gregor 
217eb90e830SDouglas Gregor   for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
218de3ef502SDouglas Gregor        MI != MIEnd; ++MI)
219eb90e830SDouglas Gregor     (*MI)->print(OS, Indent + 2);
220de3ef502SDouglas Gregor 
22124bb923aSDouglas Gregor   for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
22224bb923aSDouglas Gregor     OS.indent(Indent + 2);
2238c7c8352SDouglas Gregor     OS << "export ";
2248c7c8352SDouglas Gregor     if (Module *Restriction = Exports[I].getPointer()) {
2258c7c8352SDouglas Gregor       OS << Restriction->getFullModuleName();
22624bb923aSDouglas Gregor       if (Exports[I].getInt())
22724bb923aSDouglas Gregor         OS << ".*";
2288c7c8352SDouglas Gregor     } else {
2298c7c8352SDouglas Gregor       OS << "*";
2308c7c8352SDouglas Gregor     }
23124bb923aSDouglas Gregor     OS << "\n";
23224bb923aSDouglas Gregor   }
23324bb923aSDouglas Gregor 
23424bb923aSDouglas Gregor   for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
23524bb923aSDouglas Gregor     OS.indent(Indent + 2);
23624bb923aSDouglas Gregor     OS << "export ";
23724bb923aSDouglas Gregor     printModuleId(OS, UnresolvedExports[I].Id);
2388c7c8352SDouglas Gregor     if (UnresolvedExports[I].Wildcard) {
2398c7c8352SDouglas Gregor       if (UnresolvedExports[I].Id.empty())
2408c7c8352SDouglas Gregor         OS << "*";
2418c7c8352SDouglas Gregor       else
24224bb923aSDouglas Gregor         OS << ".*";
2438c7c8352SDouglas Gregor     }
24424bb923aSDouglas Gregor     OS << "\n";
24524bb923aSDouglas Gregor   }
24624bb923aSDouglas Gregor 
24773441091SDouglas Gregor   if (InferSubmodules) {
24873441091SDouglas Gregor     OS.indent(Indent + 2);
24973441091SDouglas Gregor     if (InferExplicitSubmodules)
25073441091SDouglas Gregor       OS << "explicit ";
25173441091SDouglas Gregor     OS << "module * {\n";
25273441091SDouglas Gregor     if (InferExportWildcard) {
25373441091SDouglas Gregor       OS.indent(Indent + 4);
25473441091SDouglas Gregor       OS << "export *\n";
25573441091SDouglas Gregor     }
25673441091SDouglas Gregor     OS.indent(Indent + 2);
25773441091SDouglas Gregor     OS << "}\n";
25873441091SDouglas Gregor   }
25973441091SDouglas Gregor 
260de3ef502SDouglas Gregor   OS.indent(Indent);
261de3ef502SDouglas Gregor   OS << "}\n";
262de3ef502SDouglas Gregor }
263de3ef502SDouglas Gregor 
264de3ef502SDouglas Gregor void Module::dump() const {
265de3ef502SDouglas Gregor   print(llvm::errs());
266de3ef502SDouglas Gregor }
267de3ef502SDouglas Gregor 
268de3ef502SDouglas Gregor 
269