1 //===--- Module.cpp - Describe a module -----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Module class, which describes a module in the source
13 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/Module.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/LangOptions.h"
18 #include "clang/Basic/TargetInfo.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
25 using namespace clang;
27 Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
28 bool IsFramework, bool IsExplicit)
29 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
30 Umbrella(), ASTFile(0), IsAvailable(true), IsFromModuleFile(false),
31 IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
32 InferSubmodules(false), InferExplicitSubmodules(false),
33 InferExportWildcard(false), ConfigMacrosExhaustive(false),
34 NameVisibility(Hidden)
37 if (!Parent->isAvailable())
42 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
43 Parent->SubModules.push_back(this);
48 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
54 /// \brief Determine whether a translation unit built using the current
55 /// language options has the given feature.
56 static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
57 const TargetInfo &Target) {
58 return llvm::StringSwitch<bool>(Feature)
59 .Case("altivec", LangOpts.AltiVec)
60 .Case("blocks", LangOpts.Blocks)
61 .Case("cplusplus", LangOpts.CPlusPlus)
62 .Case("cplusplus11", LangOpts.CPlusPlus11)
63 .Case("objc", LangOpts.ObjC1)
64 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
65 .Case("opencl", LangOpts.OpenCL)
66 .Case("tls", Target.isTLSSupported())
67 .Default(Target.hasFeature(Feature));
71 Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
72 Requirement &Req) const {
76 for (const Module *Current = this; Current; Current = Current->Parent) {
77 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
78 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
79 Current->Requirements[I].second) {
80 Req = Current->Requirements[I];
86 llvm_unreachable("could not find a reason why module is unavailable");
89 bool Module::isSubModuleOf(Module *Other) const {
90 const Module *This = this;
101 const Module *Module::getTopLevelModule() const {
102 const Module *Result = this;
103 while (Result->Parent)
104 Result = Result->Parent;
109 std::string Module::getFullModuleName() const {
110 SmallVector<StringRef, 2> Names;
112 // Build up the set of module names (from innermost to outermost).
113 for (const Module *M = this; M; M = M->Parent)
114 Names.push_back(M->Name);
117 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
129 const DirectoryEntry *Module::getUmbrellaDir() const {
130 if (const FileEntry *Header = getUmbrellaHeader())
131 return Header->getDir();
133 return Umbrella.dyn_cast<const DirectoryEntry *>();
136 ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
137 if (!TopHeaderNames.empty()) {
138 for (std::vector<std::string>::iterator
139 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
140 if (const FileEntry *FE = FileMgr.getFile(*I))
141 TopHeaders.insert(FE);
143 TopHeaderNames.clear();
146 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
149 void Module::addRequirement(StringRef Feature, bool RequiredState,
150 const LangOptions &LangOpts,
151 const TargetInfo &Target) {
152 Requirements.push_back(Requirement(Feature, RequiredState));
154 // If this feature is currently available, we're done.
155 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
161 SmallVector<Module *, 2> Stack;
162 Stack.push_back(this);
163 while (!Stack.empty()) {
164 Module *Current = Stack.back();
167 if (!Current->IsAvailable)
170 Current->IsAvailable = false;
171 for (submodule_iterator Sub = Current->submodule_begin(),
172 SubEnd = Current->submodule_end();
173 Sub != SubEnd; ++Sub) {
174 if ((*Sub)->IsAvailable)
175 Stack.push_back(*Sub);
180 Module *Module::findSubmodule(StringRef Name) const {
181 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
182 if (Pos == SubModuleIndex.end())
185 return SubModules[Pos->getValue()];
188 static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
189 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
196 void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
197 // All non-explicit submodules are exported.
198 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
199 E = SubModules.end();
202 if (!Mod->IsExplicit)
203 Exported.push_back(Mod);
206 // Find re-exported modules by filtering the list of imported modules.
207 bool AnyWildcard = false;
208 bool UnrestrictedWildcard = false;
209 SmallVector<Module *, 4> WildcardRestrictions;
210 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
211 Module *Mod = Exports[I].getPointer();
212 if (!Exports[I].getInt()) {
213 // Export a named module directly; no wildcards involved.
214 Exported.push_back(Mod);
219 // Wildcard export: export all of the imported modules that match
220 // the given pattern.
222 if (UnrestrictedWildcard)
225 if (Module *Restriction = Exports[I].getPointer())
226 WildcardRestrictions.push_back(Restriction);
228 WildcardRestrictions.clear();
229 UnrestrictedWildcard = true;
233 // If there were any wildcards, push any imported modules that were
234 // re-exported by the wildcard restriction.
238 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
239 Module *Mod = Imports[I];
240 bool Acceptable = UnrestrictedWildcard;
242 // Check whether this module meets one of the restrictions.
243 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
244 Module *Restriction = WildcardRestrictions[R];
245 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
255 Exported.push_back(Mod);
259 void Module::buildVisibleModulesCache() const {
260 assert(VisibleModulesCache.empty() && "cache does not need building");
262 // This module is visible to itself.
263 VisibleModulesCache.insert(this);
265 // Every imported module is visible.
266 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
267 while (!Stack.empty()) {
268 Module *CurrModule = Stack.pop_back_val();
270 // Every module transitively exported by an imported module is visible.
271 if (VisibleModulesCache.insert(CurrModule).second)
272 CurrModule->getExportedModules(Stack);
276 void Module::print(raw_ostream &OS, unsigned Indent) const {
282 OS << "module " << Name;
285 OS.indent(Indent + 2);
291 if (!Requirements.empty()) {
292 OS.indent(Indent + 2);
294 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
297 if (!Requirements[I].second)
299 OS << Requirements[I].first;
304 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
305 OS.indent(Indent + 2);
306 OS << "umbrella header \"";
307 OS.write_escaped(UmbrellaHeader->getName());
309 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
310 OS.indent(Indent + 2);
312 OS.write_escaped(UmbrellaDir->getName());
316 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
317 OS.indent(Indent + 2);
318 OS << "config_macros ";
319 if (ConfigMacrosExhaustive)
320 OS << "[exhaustive]";
321 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
324 OS << ConfigMacros[I];
329 for (unsigned I = 0, N = NormalHeaders.size(); I != N; ++I) {
330 OS.indent(Indent + 2);
332 OS.write_escaped(NormalHeaders[I]->getName());
336 for (unsigned I = 0, N = ExcludedHeaders.size(); I != N; ++I) {
337 OS.indent(Indent + 2);
338 OS << "exclude header \"";
339 OS.write_escaped(ExcludedHeaders[I]->getName());
343 for (unsigned I = 0, N = PrivateHeaders.size(); I != N; ++I) {
344 OS.indent(Indent + 2);
345 OS << "private header \"";
346 OS.write_escaped(PrivateHeaders[I]->getName());
350 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
352 (*MI)->print(OS, Indent + 2);
354 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
355 OS.indent(Indent + 2);
357 if (Module *Restriction = Exports[I].getPointer()) {
358 OS << Restriction->getFullModuleName();
359 if (Exports[I].getInt())
367 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
368 OS.indent(Indent + 2);
370 printModuleId(OS, UnresolvedExports[I].Id);
371 if (UnresolvedExports[I].Wildcard) {
372 if (UnresolvedExports[I].Id.empty())
380 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
381 OS.indent(Indent + 2);
383 OS << DirectUses[I]->getFullModuleName();
387 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
388 OS.indent(Indent + 2);
390 printModuleId(OS, UnresolvedDirectUses[I]);
394 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
395 OS.indent(Indent + 2);
397 if (LinkLibraries[I].IsFramework)
400 OS.write_escaped(LinkLibraries[I].Library);
404 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
405 OS.indent(Indent + 2);
407 printModuleId(OS, UnresolvedConflicts[I].Id);
409 OS.write_escaped(UnresolvedConflicts[I].Message);
413 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
414 OS.indent(Indent + 2);
416 OS << Conflicts[I].Other->getFullModuleName();
418 OS.write_escaped(Conflicts[I].Message);
422 if (InferSubmodules) {
423 OS.indent(Indent + 2);
424 if (InferExplicitSubmodules)
426 OS << "module * {\n";
427 if (InferExportWildcard) {
428 OS.indent(Indent + 4);
431 OS.indent(Indent + 2);
439 void Module::dump() const {