13a02247dSChandler Carruth //===--- Module.cpp - Describe a module -----------------------------------===//
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"
170070c0bfSDouglas Gregor #include "clang/Basic/TargetInfo.h"
181fb5c3a6SDouglas Gregor #include "llvm/ADT/SmallVector.h"
191fb5c3a6SDouglas Gregor #include "llvm/ADT/StringSwitch.h"
203a02247dSChandler Carruth #include "llvm/Support/ErrorHandling.h"
213a02247dSChandler Carruth #include "llvm/Support/raw_ostream.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),
2743af5132SArgyrios Kyrtzidis     Umbrella(), ASTFile(0), 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;
353ec6663bSDouglas Gregor     if (Parent->IsSystem)
363ec6663bSDouglas Gregor       IsSystem = true;
37eb90e830SDouglas Gregor 
38eb90e830SDouglas Gregor     Parent->SubModuleIndex[Name] = Parent->SubModules.size();
39eb90e830SDouglas Gregor     Parent->SubModules.push_back(this);
40eb90e830SDouglas Gregor   }
41eb90e830SDouglas Gregor }
42eb90e830SDouglas Gregor 
43de3ef502SDouglas Gregor Module::~Module() {
44eb90e830SDouglas Gregor   for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
45de3ef502SDouglas Gregor        I != IEnd; ++I) {
46eb90e830SDouglas Gregor     delete *I;
47de3ef502SDouglas Gregor   }
48de3ef502SDouglas Gregor 
49de3ef502SDouglas Gregor }
50de3ef502SDouglas Gregor 
511fb5c3a6SDouglas Gregor /// \brief Determine whether a translation unit built using the current
521fb5c3a6SDouglas Gregor /// language options has the given feature.
5389929282SDouglas Gregor static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
5489929282SDouglas Gregor                        const TargetInfo &Target) {
551fb5c3a6SDouglas Gregor   return llvm::StringSwitch<bool>(Feature)
560070c0bfSDouglas Gregor            .Case("altivec", LangOpts.AltiVec)
571fb5c3a6SDouglas Gregor            .Case("blocks", LangOpts.Blocks)
581fb5c3a6SDouglas Gregor            .Case("cplusplus", LangOpts.CPlusPlus)
59*2bf7fdb7SRichard Smith            .Case("cplusplus11", LangOpts.CPlusPlus11)
601fb5c3a6SDouglas Gregor            .Case("objc", LangOpts.ObjC1)
611fb5c3a6SDouglas Gregor            .Case("objc_arc", LangOpts.ObjCAutoRefCount)
620070c0bfSDouglas Gregor            .Case("opencl", LangOpts.OpenCL)
630070c0bfSDouglas Gregor            .Case("tls", Target.isTLSSupported())
640070c0bfSDouglas Gregor            .Default(Target.hasFeature(Feature));
651fb5c3a6SDouglas Gregor }
661fb5c3a6SDouglas Gregor 
671fb5c3a6SDouglas Gregor bool
6889929282SDouglas Gregor Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
6989929282SDouglas Gregor                     StringRef &Feature) const {
701fb5c3a6SDouglas Gregor   if (IsAvailable)
711fb5c3a6SDouglas Gregor     return true;
721fb5c3a6SDouglas Gregor 
731fb5c3a6SDouglas Gregor   for (const Module *Current = this; Current; Current = Current->Parent) {
741fb5c3a6SDouglas Gregor     for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
7589929282SDouglas Gregor       if (!hasFeature(Current->Requires[I], LangOpts, Target)) {
761fb5c3a6SDouglas Gregor         Feature = Current->Requires[I];
771fb5c3a6SDouglas Gregor         return false;
781fb5c3a6SDouglas Gregor       }
791fb5c3a6SDouglas Gregor     }
801fb5c3a6SDouglas Gregor   }
811fb5c3a6SDouglas Gregor 
821fb5c3a6SDouglas Gregor   llvm_unreachable("could not find a reason why module is unavailable");
831fb5c3a6SDouglas Gregor }
841fb5c3a6SDouglas Gregor 
85f5eedd05SDouglas Gregor bool Module::isSubModuleOf(Module *Other) const {
86f5eedd05SDouglas Gregor   const Module *This = this;
87f5eedd05SDouglas Gregor   do {
88f5eedd05SDouglas Gregor     if (This == Other)
89f5eedd05SDouglas Gregor       return true;
90f5eedd05SDouglas Gregor 
91f5eedd05SDouglas Gregor     This = This->Parent;
92f5eedd05SDouglas Gregor   } while (This);
93f5eedd05SDouglas Gregor 
94f5eedd05SDouglas Gregor   return false;
95f5eedd05SDouglas Gregor }
96f5eedd05SDouglas Gregor 
9773441091SDouglas Gregor const Module *Module::getTopLevelModule() const {
9873441091SDouglas Gregor   const Module *Result = this;
9973441091SDouglas Gregor   while (Result->Parent)
10073441091SDouglas Gregor     Result = Result->Parent;
10173441091SDouglas Gregor 
10273441091SDouglas Gregor   return Result;
10373441091SDouglas Gregor }
10473441091SDouglas Gregor 
105de3ef502SDouglas Gregor std::string Module::getFullModuleName() const {
106de3ef502SDouglas Gregor   llvm::SmallVector<StringRef, 2> Names;
107de3ef502SDouglas Gregor 
108de3ef502SDouglas Gregor   // Build up the set of module names (from innermost to outermost).
109de3ef502SDouglas Gregor   for (const Module *M = this; M; M = M->Parent)
110de3ef502SDouglas Gregor     Names.push_back(M->Name);
111de3ef502SDouglas Gregor 
112de3ef502SDouglas Gregor   std::string Result;
113de3ef502SDouglas Gregor   for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
114de3ef502SDouglas Gregor                                                       IEnd = Names.rend();
115de3ef502SDouglas Gregor        I != IEnd; ++I) {
116de3ef502SDouglas Gregor     if (!Result.empty())
117de3ef502SDouglas Gregor       Result += '.';
118de3ef502SDouglas Gregor 
119de3ef502SDouglas Gregor     Result += *I;
120de3ef502SDouglas Gregor   }
121de3ef502SDouglas Gregor 
122de3ef502SDouglas Gregor   return Result;
123de3ef502SDouglas Gregor }
124de3ef502SDouglas Gregor 
12573141fa9SDouglas Gregor const DirectoryEntry *Module::getUmbrellaDir() const {
12673141fa9SDouglas Gregor   if (const FileEntry *Header = getUmbrellaHeader())
12773141fa9SDouglas Gregor     return Header->getDir();
12873141fa9SDouglas Gregor 
12973141fa9SDouglas Gregor   return Umbrella.dyn_cast<const DirectoryEntry *>();
13073141fa9SDouglas Gregor }
13173141fa9SDouglas Gregor 
13289929282SDouglas Gregor void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts,
13389929282SDouglas Gregor                             const TargetInfo &Target) {
1341fb5c3a6SDouglas Gregor   Requires.push_back(Feature);
1351fb5c3a6SDouglas Gregor 
1361fb5c3a6SDouglas Gregor   // If this feature is currently available, we're done.
13789929282SDouglas Gregor   if (hasFeature(Feature, LangOpts, Target))
1381fb5c3a6SDouglas Gregor     return;
1391fb5c3a6SDouglas Gregor 
1401fb5c3a6SDouglas Gregor   if (!IsAvailable)
1411fb5c3a6SDouglas Gregor     return;
1421fb5c3a6SDouglas Gregor 
1431fb5c3a6SDouglas Gregor   llvm::SmallVector<Module *, 2> Stack;
1441fb5c3a6SDouglas Gregor   Stack.push_back(this);
1451fb5c3a6SDouglas Gregor   while (!Stack.empty()) {
1461fb5c3a6SDouglas Gregor     Module *Current = Stack.back();
1471fb5c3a6SDouglas Gregor     Stack.pop_back();
1481fb5c3a6SDouglas Gregor 
1491fb5c3a6SDouglas Gregor     if (!Current->IsAvailable)
1501fb5c3a6SDouglas Gregor       continue;
1511fb5c3a6SDouglas Gregor 
1521fb5c3a6SDouglas Gregor     Current->IsAvailable = false;
153eb90e830SDouglas Gregor     for (submodule_iterator Sub = Current->submodule_begin(),
154eb90e830SDouglas Gregor                          SubEnd = Current->submodule_end();
1551fb5c3a6SDouglas Gregor          Sub != SubEnd; ++Sub) {
156eb90e830SDouglas Gregor       if ((*Sub)->IsAvailable)
157eb90e830SDouglas Gregor         Stack.push_back(*Sub);
1581fb5c3a6SDouglas Gregor     }
1591fb5c3a6SDouglas Gregor   }
1601fb5c3a6SDouglas Gregor }
1611fb5c3a6SDouglas Gregor 
162eb90e830SDouglas Gregor Module *Module::findSubmodule(StringRef Name) const {
163eb90e830SDouglas Gregor   llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
164eb90e830SDouglas Gregor   if (Pos == SubModuleIndex.end())
165eb90e830SDouglas Gregor     return 0;
166eb90e830SDouglas Gregor 
167eb90e830SDouglas Gregor   return SubModules[Pos->getValue()];
168eb90e830SDouglas Gregor }
169eb90e830SDouglas Gregor 
17024bb923aSDouglas Gregor static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
17124bb923aSDouglas Gregor   for (unsigned I = 0, N = Id.size(); I != N; ++I) {
17224bb923aSDouglas Gregor     if (I)
17324bb923aSDouglas Gregor       OS << ".";
17424bb923aSDouglas Gregor     OS << Id[I].first;
17524bb923aSDouglas Gregor   }
17624bb923aSDouglas Gregor }
17724bb923aSDouglas Gregor 
178de3ef502SDouglas Gregor void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
179de3ef502SDouglas Gregor   OS.indent(Indent);
180de3ef502SDouglas Gregor   if (IsFramework)
181de3ef502SDouglas Gregor     OS << "framework ";
182de3ef502SDouglas Gregor   if (IsExplicit)
183de3ef502SDouglas Gregor     OS << "explicit ";
184a686e1b0SDouglas Gregor   OS << "module " << Name;
185a686e1b0SDouglas Gregor 
186a686e1b0SDouglas Gregor   if (IsSystem) {
187a686e1b0SDouglas Gregor     OS.indent(Indent + 2);
188a686e1b0SDouglas Gregor     OS << " [system]";
189a686e1b0SDouglas Gregor   }
190a686e1b0SDouglas Gregor 
191a686e1b0SDouglas Gregor   OS << " {\n";
192de3ef502SDouglas Gregor 
1931fb5c3a6SDouglas Gregor   if (!Requires.empty()) {
1941fb5c3a6SDouglas Gregor     OS.indent(Indent + 2);
1951fb5c3a6SDouglas Gregor     OS << "requires ";
1961fb5c3a6SDouglas Gregor     for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
1971fb5c3a6SDouglas Gregor       if (I)
1981fb5c3a6SDouglas Gregor         OS << ", ";
1991fb5c3a6SDouglas Gregor       OS << Requires[I];
2001fb5c3a6SDouglas Gregor     }
2011fb5c3a6SDouglas Gregor     OS << "\n";
2021fb5c3a6SDouglas Gregor   }
2031fb5c3a6SDouglas Gregor 
20473141fa9SDouglas Gregor   if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
205de3ef502SDouglas Gregor     OS.indent(Indent + 2);
206322f633cSDouglas Gregor     OS << "umbrella header \"";
207de3ef502SDouglas Gregor     OS.write_escaped(UmbrellaHeader->getName());
208de3ef502SDouglas Gregor     OS << "\"\n";
209322f633cSDouglas Gregor   } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
210322f633cSDouglas Gregor     OS.indent(Indent + 2);
211322f633cSDouglas Gregor     OS << "umbrella \"";
212322f633cSDouglas Gregor     OS.write_escaped(UmbrellaDir->getName());
213322f633cSDouglas Gregor     OS << "\"\n";
214de3ef502SDouglas Gregor   }
215de3ef502SDouglas Gregor 
216de3ef502SDouglas Gregor   for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
217de3ef502SDouglas Gregor     OS.indent(Indent + 2);
218de3ef502SDouglas Gregor     OS << "header \"";
219de3ef502SDouglas Gregor     OS.write_escaped(Headers[I]->getName());
220de3ef502SDouglas Gregor     OS << "\"\n";
221de3ef502SDouglas Gregor   }
22259527666SDouglas Gregor 
22359527666SDouglas Gregor   for (unsigned I = 0, N = ExcludedHeaders.size(); I != N; ++I) {
22459527666SDouglas Gregor     OS.indent(Indent + 2);
22559527666SDouglas Gregor     OS << "exclude header \"";
22659527666SDouglas Gregor     OS.write_escaped(ExcludedHeaders[I]->getName());
22759527666SDouglas Gregor     OS << "\"\n";
22859527666SDouglas Gregor   }
229de3ef502SDouglas Gregor 
230eb90e830SDouglas Gregor   for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
231de3ef502SDouglas Gregor        MI != MIEnd; ++MI)
232eb90e830SDouglas Gregor     (*MI)->print(OS, Indent + 2);
233de3ef502SDouglas Gregor 
23424bb923aSDouglas Gregor   for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
23524bb923aSDouglas Gregor     OS.indent(Indent + 2);
2368c7c8352SDouglas Gregor     OS << "export ";
2378c7c8352SDouglas Gregor     if (Module *Restriction = Exports[I].getPointer()) {
2388c7c8352SDouglas Gregor       OS << Restriction->getFullModuleName();
23924bb923aSDouglas Gregor       if (Exports[I].getInt())
24024bb923aSDouglas Gregor         OS << ".*";
2418c7c8352SDouglas Gregor     } else {
2428c7c8352SDouglas Gregor       OS << "*";
2438c7c8352SDouglas Gregor     }
24424bb923aSDouglas Gregor     OS << "\n";
24524bb923aSDouglas Gregor   }
24624bb923aSDouglas Gregor 
24724bb923aSDouglas Gregor   for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
24824bb923aSDouglas Gregor     OS.indent(Indent + 2);
24924bb923aSDouglas Gregor     OS << "export ";
25024bb923aSDouglas Gregor     printModuleId(OS, UnresolvedExports[I].Id);
2518c7c8352SDouglas Gregor     if (UnresolvedExports[I].Wildcard) {
2528c7c8352SDouglas Gregor       if (UnresolvedExports[I].Id.empty())
2538c7c8352SDouglas Gregor         OS << "*";
2548c7c8352SDouglas Gregor       else
25524bb923aSDouglas Gregor         OS << ".*";
2568c7c8352SDouglas Gregor     }
25724bb923aSDouglas Gregor     OS << "\n";
25824bb923aSDouglas Gregor   }
25924bb923aSDouglas Gregor 
26073441091SDouglas Gregor   if (InferSubmodules) {
26173441091SDouglas Gregor     OS.indent(Indent + 2);
26273441091SDouglas Gregor     if (InferExplicitSubmodules)
26373441091SDouglas Gregor       OS << "explicit ";
26473441091SDouglas Gregor     OS << "module * {\n";
26573441091SDouglas Gregor     if (InferExportWildcard) {
26673441091SDouglas Gregor       OS.indent(Indent + 4);
26773441091SDouglas Gregor       OS << "export *\n";
26873441091SDouglas Gregor     }
26973441091SDouglas Gregor     OS.indent(Indent + 2);
27073441091SDouglas Gregor     OS << "}\n";
27173441091SDouglas Gregor   }
27273441091SDouglas Gregor 
273de3ef502SDouglas Gregor   OS.indent(Indent);
274de3ef502SDouglas Gregor   OS << "}\n";
275de3ef502SDouglas Gregor }
276de3ef502SDouglas Gregor 
277de3ef502SDouglas Gregor void Module::dump() const {
278de3ef502SDouglas Gregor   print(llvm::errs());
279de3ef502SDouglas Gregor }
280de3ef502SDouglas Gregor 
281de3ef502SDouglas Gregor 
282