• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- Module.cpp - Describe a module -----------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the Module class, which describes a module in the source
11 // code.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/Module.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/LangOptions.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace clang;
24 
Module(StringRef Name,SourceLocation DefinitionLoc,Module * Parent,bool IsFramework,bool IsExplicit)25 Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
26                bool IsFramework, bool IsExplicit)
27   : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
28     Umbrella(), ASTFile(0), IsAvailable(true), IsFromModuleFile(false),
29     IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
30     InferSubmodules(false), InferExplicitSubmodules(false),
31     InferExportWildcard(false), ConfigMacrosExhaustive(false),
32     NameVisibility(Hidden)
33 {
34   if (Parent) {
35     if (!Parent->isAvailable())
36       IsAvailable = false;
37     if (Parent->IsSystem)
38       IsSystem = true;
39 
40     Parent->SubModuleIndex[Name] = Parent->SubModules.size();
41     Parent->SubModules.push_back(this);
42   }
43 }
44 
~Module()45 Module::~Module() {
46   for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
47        I != IEnd; ++I) {
48     delete *I;
49   }
50 }
51 
52 /// \brief Determine whether a translation unit built using the current
53 /// language options has the given feature.
hasFeature(StringRef Feature,const LangOptions & LangOpts,const TargetInfo & Target)54 static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
55                        const TargetInfo &Target) {
56   return llvm::StringSwitch<bool>(Feature)
57            .Case("altivec", LangOpts.AltiVec)
58            .Case("blocks", LangOpts.Blocks)
59            .Case("cplusplus", LangOpts.CPlusPlus)
60            .Case("cplusplus11", LangOpts.CPlusPlus11)
61            .Case("objc", LangOpts.ObjC1)
62            .Case("objc_arc", LangOpts.ObjCAutoRefCount)
63            .Case("opencl", LangOpts.OpenCL)
64            .Case("tls", Target.isTLSSupported())
65            .Default(Target.hasFeature(Feature));
66 }
67 
68 bool
isAvailable(const LangOptions & LangOpts,const TargetInfo & Target,StringRef & Feature) const69 Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
70                     StringRef &Feature) const {
71   if (IsAvailable)
72     return true;
73 
74   for (const Module *Current = this; Current; Current = Current->Parent) {
75     for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
76       if (!hasFeature(Current->Requires[I], LangOpts, Target)) {
77         Feature = Current->Requires[I];
78         return false;
79       }
80     }
81   }
82 
83   llvm_unreachable("could not find a reason why module is unavailable");
84 }
85 
isSubModuleOf(Module * Other) const86 bool Module::isSubModuleOf(Module *Other) const {
87   const Module *This = this;
88   do {
89     if (This == Other)
90       return true;
91 
92     This = This->Parent;
93   } while (This);
94 
95   return false;
96 }
97 
getTopLevelModule() const98 const Module *Module::getTopLevelModule() const {
99   const Module *Result = this;
100   while (Result->Parent)
101     Result = Result->Parent;
102 
103   return Result;
104 }
105 
getFullModuleName() const106 std::string Module::getFullModuleName() const {
107   SmallVector<StringRef, 2> Names;
108 
109   // Build up the set of module names (from innermost to outermost).
110   for (const Module *M = this; M; M = M->Parent)
111     Names.push_back(M->Name);
112 
113   std::string Result;
114   for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
115                                                  IEnd = Names.rend();
116        I != IEnd; ++I) {
117     if (!Result.empty())
118       Result += '.';
119 
120     Result += *I;
121   }
122 
123   return Result;
124 }
125 
getUmbrellaDir() const126 const DirectoryEntry *Module::getUmbrellaDir() const {
127   if (const FileEntry *Header = getUmbrellaHeader())
128     return Header->getDir();
129 
130   return Umbrella.dyn_cast<const DirectoryEntry *>();
131 }
132 
getTopHeaders(FileManager & FileMgr)133 ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
134   if (!TopHeaderNames.empty()) {
135     for (std::vector<std::string>::iterator
136            I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
137       if (const FileEntry *FE = FileMgr.getFile(*I))
138         TopHeaders.insert(FE);
139     }
140     TopHeaderNames.clear();
141   }
142 
143   return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
144 }
145 
addRequirement(StringRef Feature,const LangOptions & LangOpts,const TargetInfo & Target)146 void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts,
147                             const TargetInfo &Target) {
148   Requires.push_back(Feature);
149 
150   // If this feature is currently available, we're done.
151   if (hasFeature(Feature, LangOpts, Target))
152     return;
153 
154   if (!IsAvailable)
155     return;
156 
157   SmallVector<Module *, 2> Stack;
158   Stack.push_back(this);
159   while (!Stack.empty()) {
160     Module *Current = Stack.back();
161     Stack.pop_back();
162 
163     if (!Current->IsAvailable)
164       continue;
165 
166     Current->IsAvailable = false;
167     for (submodule_iterator Sub = Current->submodule_begin(),
168                          SubEnd = Current->submodule_end();
169          Sub != SubEnd; ++Sub) {
170       if ((*Sub)->IsAvailable)
171         Stack.push_back(*Sub);
172     }
173   }
174 }
175 
findSubmodule(StringRef Name) const176 Module *Module::findSubmodule(StringRef Name) const {
177   llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
178   if (Pos == SubModuleIndex.end())
179     return 0;
180 
181   return SubModules[Pos->getValue()];
182 }
183 
printModuleId(raw_ostream & OS,const ModuleId & Id)184 static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
185   for (unsigned I = 0, N = Id.size(); I != N; ++I) {
186     if (I)
187       OS << ".";
188     OS << Id[I].first;
189   }
190 }
191 
getExportedModules(SmallVectorImpl<Module * > & Exported) const192 void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
193   bool AnyWildcard = false;
194   bool UnrestrictedWildcard = false;
195   SmallVector<Module *, 4> WildcardRestrictions;
196   for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
197     Module *Mod = Exports[I].getPointer();
198     if (!Exports[I].getInt()) {
199       // Export a named module directly; no wildcards involved.
200       Exported.push_back(Mod);
201 
202       continue;
203     }
204 
205     // Wildcard export: export all of the imported modules that match
206     // the given pattern.
207     AnyWildcard = true;
208     if (UnrestrictedWildcard)
209       continue;
210 
211     if (Module *Restriction = Exports[I].getPointer())
212       WildcardRestrictions.push_back(Restriction);
213     else {
214       WildcardRestrictions.clear();
215       UnrestrictedWildcard = true;
216     }
217   }
218 
219   // If there were any wildcards, push any imported modules that were
220   // re-exported by the wildcard restriction.
221   if (!AnyWildcard)
222     return;
223 
224   for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
225     Module *Mod = Imports[I];
226     bool Acceptable = UnrestrictedWildcard;
227     if (!Acceptable) {
228       // Check whether this module meets one of the restrictions.
229       for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
230         Module *Restriction = WildcardRestrictions[R];
231         if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
232           Acceptable = true;
233           break;
234         }
235       }
236     }
237 
238     if (!Acceptable)
239       continue;
240 
241     Exported.push_back(Mod);
242   }
243 }
244 
buildVisibleModulesCache() const245 void Module::buildVisibleModulesCache() const {
246   assert(VisibleModulesCache.empty() && "cache does not need building");
247 
248   // This module is visible to itself.
249   VisibleModulesCache.insert(this);
250 
251   llvm::SmallVector<Module*, 4> Exported;
252   for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
253     // Every imported module is visible.
254     VisibleModulesCache.insert(Imports[I]);
255 
256     // Every module exported by an imported module is visible.
257     Imports[I]->getExportedModules(Exported);
258     VisibleModulesCache.insert(Exported.begin(), Exported.end());
259     Exported.clear();
260   }
261 }
262 
print(raw_ostream & OS,unsigned Indent) const263 void Module::print(raw_ostream &OS, unsigned Indent) const {
264   OS.indent(Indent);
265   if (IsFramework)
266     OS << "framework ";
267   if (IsExplicit)
268     OS << "explicit ";
269   OS << "module " << Name;
270 
271   if (IsSystem) {
272     OS.indent(Indent + 2);
273     OS << " [system]";
274   }
275 
276   OS << " {\n";
277 
278   if (!Requires.empty()) {
279     OS.indent(Indent + 2);
280     OS << "requires ";
281     for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
282       if (I)
283         OS << ", ";
284       OS << Requires[I];
285     }
286     OS << "\n";
287   }
288 
289   if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
290     OS.indent(Indent + 2);
291     OS << "umbrella header \"";
292     OS.write_escaped(UmbrellaHeader->getName());
293     OS << "\"\n";
294   } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
295     OS.indent(Indent + 2);
296     OS << "umbrella \"";
297     OS.write_escaped(UmbrellaDir->getName());
298     OS << "\"\n";
299   }
300 
301   if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
302     OS.indent(Indent + 2);
303     OS << "config_macros ";
304     if (ConfigMacrosExhaustive)
305       OS << "[exhaustive]";
306     for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
307       if (I)
308         OS << ", ";
309       OS << ConfigMacros[I];
310     }
311     OS << "\n";
312   }
313 
314   for (unsigned I = 0, N = NormalHeaders.size(); I != N; ++I) {
315     OS.indent(Indent + 2);
316     OS << "header \"";
317     OS.write_escaped(NormalHeaders[I]->getName());
318     OS << "\"\n";
319   }
320 
321   for (unsigned I = 0, N = ExcludedHeaders.size(); I != N; ++I) {
322     OS.indent(Indent + 2);
323     OS << "exclude header \"";
324     OS.write_escaped(ExcludedHeaders[I]->getName());
325     OS << "\"\n";
326   }
327 
328   for (unsigned I = 0, N = PrivateHeaders.size(); I != N; ++I) {
329     OS.indent(Indent + 2);
330     OS << "private header \"";
331     OS.write_escaped(PrivateHeaders[I]->getName());
332     OS << "\"\n";
333   }
334 
335   for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
336        MI != MIEnd; ++MI)
337     (*MI)->print(OS, Indent + 2);
338 
339   for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
340     OS.indent(Indent + 2);
341     OS << "export ";
342     if (Module *Restriction = Exports[I].getPointer()) {
343       OS << Restriction->getFullModuleName();
344       if (Exports[I].getInt())
345         OS << ".*";
346     } else {
347       OS << "*";
348     }
349     OS << "\n";
350   }
351 
352   for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
353     OS.indent(Indent + 2);
354     OS << "export ";
355     printModuleId(OS, UnresolvedExports[I].Id);
356     if (UnresolvedExports[I].Wildcard) {
357       if (UnresolvedExports[I].Id.empty())
358         OS << "*";
359       else
360         OS << ".*";
361     }
362     OS << "\n";
363   }
364 
365   for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
366     OS.indent(Indent + 2);
367     OS << "link ";
368     if (LinkLibraries[I].IsFramework)
369       OS << "framework ";
370     OS << "\"";
371     OS.write_escaped(LinkLibraries[I].Library);
372     OS << "\"";
373   }
374 
375   for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
376     OS.indent(Indent + 2);
377     OS << "conflict ";
378     printModuleId(OS, UnresolvedConflicts[I].Id);
379     OS << ", \"";
380     OS.write_escaped(UnresolvedConflicts[I].Message);
381     OS << "\"\n";
382   }
383 
384   for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
385     OS.indent(Indent + 2);
386     OS << "conflict ";
387     OS << Conflicts[I].Other->getFullModuleName();
388     OS << ", \"";
389     OS.write_escaped(Conflicts[I].Message);
390     OS << "\"\n";
391   }
392 
393   if (InferSubmodules) {
394     OS.indent(Indent + 2);
395     if (InferExplicitSubmodules)
396       OS << "explicit ";
397     OS << "module * {\n";
398     if (InferExportWildcard) {
399       OS.indent(Indent + 4);
400       OS << "export *\n";
401     }
402     OS.indent(Indent + 2);
403     OS << "}\n";
404   }
405 
406   OS.indent(Indent);
407   OS << "}\n";
408 }
409 
dump() const410 void Module::dump() const {
411   print(llvm::errs());
412 }
413 
414 
415