blob: ccf7077d6fb88e60068718ef4892b592f46e566f [file] [log] [blame]
Chandler Carruth55fc8732012-12-04 09:13:33 +00001//===--- Module.cpp - Describe a module -----------------------------------===//
Douglas Gregor1a4761e2011-11-30 23:21:26 +00002//
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//===----------------------------------------------------------------------===//
Richard Smith5794b532013-10-28 22:18:19 +000014
Douglas Gregor1a4761e2011-11-30 23:21:26 +000015#include "clang/Basic/Module.h"
16#include "clang/Basic/FileManager.h"
Douglas Gregor51f564f2011-12-31 04:05:44 +000017#include "clang/Basic/LangOptions.h"
Douglas Gregore727d212012-01-30 06:38:25 +000018#include "clang/Basic/TargetInfo.h"
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +000019#include "llvm/ADT/ArrayRef.h"
Douglas Gregor51f564f2011-12-31 04:05:44 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringSwitch.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
Richard Smith5794b532013-10-28 22:18:19 +000024
Douglas Gregor1a4761e2011-11-30 23:21:26 +000025using namespace clang;
26
Douglas Gregorb7a78192012-01-04 23:32:19 +000027Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
28 bool IsFramework, bool IsExplicit)
Argyrios Kyrtzidise2ac16b2012-09-29 01:06:04 +000029 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
30 Umbrella(), ASTFile(0), IsAvailable(true), IsFromModuleFile(false),
Douglas Gregora1f1fad2012-01-27 19:52:33 +000031 IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
32 InferSubmodules(false), InferExplicitSubmodules(false),
Douglas Gregor970e4412013-03-20 03:59:18 +000033 InferExportWildcard(false), ConfigMacrosExhaustive(false),
34 NameVisibility(Hidden)
Douglas Gregorb7a78192012-01-04 23:32:19 +000035{
36 if (Parent) {
37 if (!Parent->isAvailable())
38 IsAvailable = false;
Douglas Gregor2f04f182012-02-02 18:42:48 +000039 if (Parent->IsSystem)
40 IsSystem = true;
Douglas Gregorb7a78192012-01-04 23:32:19 +000041
42 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
43 Parent->SubModules.push_back(this);
44 }
45}
46
Douglas Gregor1a4761e2011-11-30 23:21:26 +000047Module::~Module() {
Douglas Gregorb7a78192012-01-04 23:32:19 +000048 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +000049 I != IEnd; ++I) {
Douglas Gregorb7a78192012-01-04 23:32:19 +000050 delete *I;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000051 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +000052}
53
Douglas Gregor51f564f2011-12-31 04:05:44 +000054/// \brief Determine whether a translation unit built using the current
55/// language options has the given feature.
Douglas Gregordc58aa72012-01-30 06:01:29 +000056static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
57 const TargetInfo &Target) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000058 return llvm::StringSwitch<bool>(Feature)
Douglas Gregore727d212012-01-30 06:38:25 +000059 .Case("altivec", LangOpts.AltiVec)
Douglas Gregor51f564f2011-12-31 04:05:44 +000060 .Case("blocks", LangOpts.Blocks)
61 .Case("cplusplus", LangOpts.CPlusPlus)
Richard Smith80ad52f2013-01-02 11:42:31 +000062 .Case("cplusplus11", LangOpts.CPlusPlus11)
Douglas Gregor51f564f2011-12-31 04:05:44 +000063 .Case("objc", LangOpts.ObjC1)
64 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
Douglas Gregore727d212012-01-30 06:38:25 +000065 .Case("opencl", LangOpts.OpenCL)
66 .Case("tls", Target.isTLSSupported())
67 .Default(Target.hasFeature(Feature));
Douglas Gregor51f564f2011-12-31 04:05:44 +000068}
69
Richard Smith5794b532013-10-28 22:18:19 +000070bool
Douglas Gregordc58aa72012-01-30 06:01:29 +000071Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
Richard Smith5794b532013-10-28 22:18:19 +000072 Requirement &Req) const {
Douglas Gregor51f564f2011-12-31 04:05:44 +000073 if (IsAvailable)
74 return true;
75
76 for (const Module *Current = this; Current; Current = Current->Parent) {
Richard Smith5794b532013-10-28 22:18:19 +000077 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];
Douglas Gregor51f564f2011-12-31 04:05:44 +000081 return false;
82 }
83 }
84 }
85
86 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor51f564f2011-12-31 04:05:44 +000087}
88
Douglas Gregor0adaa882011-12-05 17:28:06 +000089bool Module::isSubModuleOf(Module *Other) const {
90 const Module *This = this;
91 do {
92 if (This == Other)
93 return true;
94
95 This = This->Parent;
96 } while (This);
97
98 return false;
99}
100
Douglas Gregor1e123682011-12-05 22:27:44 +0000101const Module *Module::getTopLevelModule() const {
102 const Module *Result = this;
103 while (Result->Parent)
104 Result = Result->Parent;
105
106 return Result;
107}
108
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000109std::string Module::getFullModuleName() const {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000110 SmallVector<StringRef, 2> Names;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000111
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);
115
116 std::string Result;
Craig Topper163fbf82013-07-08 03:55:09 +0000117 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
118 IEnd = Names.rend();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000119 I != IEnd; ++I) {
120 if (!Result.empty())
121 Result += '.';
122
123 Result += *I;
124 }
125
126 return Result;
127}
128
Douglas Gregor10694ce2011-12-08 17:39:04 +0000129const DirectoryEntry *Module::getUmbrellaDir() const {
130 if (const FileEntry *Header = getUmbrellaHeader())
131 return Header->getDir();
132
133 return Umbrella.dyn_cast<const DirectoryEntry *>();
134}
135
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +0000136ArrayRef<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);
142 }
143 TopHeaderNames.clear();
144 }
145
146 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
147}
148
Richard Smith5794b532013-10-28 22:18:19 +0000149void Module::addRequirement(StringRef Feature, bool RequiredState,
150 const LangOptions &LangOpts,
Douglas Gregordc58aa72012-01-30 06:01:29 +0000151 const TargetInfo &Target) {
Richard Smith5794b532013-10-28 22:18:19 +0000152 Requirements.push_back(Requirement(Feature, RequiredState));
Douglas Gregor51f564f2011-12-31 04:05:44 +0000153
154 // If this feature is currently available, we're done.
Richard Smith5794b532013-10-28 22:18:19 +0000155 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
Douglas Gregor51f564f2011-12-31 04:05:44 +0000156 return;
157
158 if (!IsAvailable)
159 return;
160
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000161 SmallVector<Module *, 2> Stack;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000162 Stack.push_back(this);
163 while (!Stack.empty()) {
164 Module *Current = Stack.back();
165 Stack.pop_back();
166
167 if (!Current->IsAvailable)
168 continue;
169
170 Current->IsAvailable = false;
Douglas Gregorb7a78192012-01-04 23:32:19 +0000171 for (submodule_iterator Sub = Current->submodule_begin(),
172 SubEnd = Current->submodule_end();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000173 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +0000174 if ((*Sub)->IsAvailable)
175 Stack.push_back(*Sub);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000176 }
177 }
178}
179
Douglas Gregorb7a78192012-01-04 23:32:19 +0000180Module *Module::findSubmodule(StringRef Name) const {
181 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
182 if (Pos == SubModuleIndex.end())
183 return 0;
184
185 return SubModules[Pos->getValue()];
186}
187
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000188static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000189 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
190 if (I)
191 OS << ".";
192 OS << Id[I].first;
193 }
194}
195
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +0000196void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
197 bool AnyWildcard = false;
198 bool UnrestrictedWildcard = false;
199 SmallVector<Module *, 4> WildcardRestrictions;
200 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
201 Module *Mod = Exports[I].getPointer();
202 if (!Exports[I].getInt()) {
203 // Export a named module directly; no wildcards involved.
204 Exported.push_back(Mod);
205
206 continue;
207 }
208
209 // Wildcard export: export all of the imported modules that match
210 // the given pattern.
211 AnyWildcard = true;
212 if (UnrestrictedWildcard)
213 continue;
214
215 if (Module *Restriction = Exports[I].getPointer())
216 WildcardRestrictions.push_back(Restriction);
217 else {
218 WildcardRestrictions.clear();
219 UnrestrictedWildcard = true;
220 }
221 }
222
223 // If there were any wildcards, push any imported modules that were
224 // re-exported by the wildcard restriction.
225 if (!AnyWildcard)
226 return;
227
228 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
229 Module *Mod = Imports[I];
230 bool Acceptable = UnrestrictedWildcard;
231 if (!Acceptable) {
232 // Check whether this module meets one of the restrictions.
233 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
234 Module *Restriction = WildcardRestrictions[R];
235 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
236 Acceptable = true;
237 break;
238 }
239 }
240 }
241
242 if (!Acceptable)
243 continue;
244
245 Exported.push_back(Mod);
246 }
247}
248
Richard Smithb7751002013-07-25 23:08:39 +0000249void Module::buildVisibleModulesCache() const {
250 assert(VisibleModulesCache.empty() && "cache does not need building");
251
252 // This module is visible to itself.
253 VisibleModulesCache.insert(this);
254
Dmitri Gribenkobc64d352013-10-31 22:24:10 +0000255 // Every imported module is visible.
256 // Every module exported by an imported module is visible.
257 llvm::SmallPtrSet<Module *, 4> Visited;
258 llvm::SmallVector<Module *, 4> Exports;
259 SmallVector<Module *, 4> Stack(Imports.begin(), Imports.end());
260 while (!Stack.empty()) {
261 Module *CurrModule = Stack.pop_back_val();
262 VisibleModulesCache.insert(CurrModule);
Richard Smithb7751002013-07-25 23:08:39 +0000263
Dmitri Gribenkobc64d352013-10-31 22:24:10 +0000264 CurrModule->getExportedModules(Exports);
265 for (SmallVectorImpl<Module *>::iterator I = Exports.begin(),
266 E = Exports.end();
267 I != E; ++I) {
268 Module *Exported = *I;
269 if (Visited.insert(Exported))
270 Stack.push_back(Exported);
271 }
Richard Smithb7751002013-07-25 23:08:39 +0000272 }
273}
274
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000275void Module::print(raw_ostream &OS, unsigned Indent) const {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000276 OS.indent(Indent);
277 if (IsFramework)
278 OS << "framework ";
279 if (IsExplicit)
280 OS << "explicit ";
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000281 OS << "module " << Name;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000282
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000283 if (IsSystem) {
284 OS.indent(Indent + 2);
285 OS << " [system]";
286 }
287
288 OS << " {\n";
289
Richard Smith5794b532013-10-28 22:18:19 +0000290 if (!Requirements.empty()) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000291 OS.indent(Indent + 2);
292 OS << "requires ";
Richard Smith5794b532013-10-28 22:18:19 +0000293 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000294 if (I)
295 OS << ", ";
Richard Smith5794b532013-10-28 22:18:19 +0000296 if (!Requirements[I].second)
297 OS << "!";
298 OS << Requirements[I].first;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000299 }
300 OS << "\n";
301 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000302
Douglas Gregor10694ce2011-12-08 17:39:04 +0000303 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000304 OS.indent(Indent + 2);
Douglas Gregor489ad432011-12-08 18:00:48 +0000305 OS << "umbrella header \"";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000306 OS.write_escaped(UmbrellaHeader->getName());
307 OS << "\"\n";
Douglas Gregor489ad432011-12-08 18:00:48 +0000308 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
309 OS.indent(Indent + 2);
310 OS << "umbrella \"";
311 OS.write_escaped(UmbrellaDir->getName());
312 OS << "\"\n";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000313 }
Douglas Gregor63a72682013-03-20 00:22:05 +0000314
315 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
316 OS.indent(Indent + 2);
317 OS << "config_macros ";
318 if (ConfigMacrosExhaustive)
Douglas Gregor970e4412013-03-20 03:59:18 +0000319 OS << "[exhaustive]";
Douglas Gregor63a72682013-03-20 00:22:05 +0000320 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
321 if (I)
322 OS << ", ";
323 OS << ConfigMacros[I];
324 }
Douglas Gregor970e4412013-03-20 03:59:18 +0000325 OS << "\n";
Douglas Gregor63a72682013-03-20 00:22:05 +0000326 }
327
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000328 for (unsigned I = 0, N = NormalHeaders.size(); I != N; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000329 OS.indent(Indent + 2);
330 OS << "header \"";
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000331 OS.write_escaped(NormalHeaders[I]->getName());
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000332 OS << "\"\n";
333 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000334
335 for (unsigned I = 0, N = ExcludedHeaders.size(); I != N; ++I) {
336 OS.indent(Indent + 2);
337 OS << "exclude header \"";
338 OS.write_escaped(ExcludedHeaders[I]->getName());
339 OS << "\"\n";
340 }
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000341
342 for (unsigned I = 0, N = PrivateHeaders.size(); I != N; ++I) {
343 OS.indent(Indent + 2);
344 OS << "private header \"";
345 OS.write_escaped(PrivateHeaders[I]->getName());
346 OS << "\"\n";
347 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000348
Douglas Gregorb7a78192012-01-04 23:32:19 +0000349 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000350 MI != MIEnd; ++MI)
Douglas Gregorb7a78192012-01-04 23:32:19 +0000351 (*MI)->print(OS, Indent + 2);
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000352
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000353 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
354 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000355 OS << "export ";
356 if (Module *Restriction = Exports[I].getPointer()) {
357 OS << Restriction->getFullModuleName();
358 if (Exports[I].getInt())
359 OS << ".*";
360 } else {
361 OS << "*";
362 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000363 OS << "\n";
364 }
365
366 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
367 OS.indent(Indent + 2);
368 OS << "export ";
369 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000370 if (UnresolvedExports[I].Wildcard) {
371 if (UnresolvedExports[I].Id.empty())
372 OS << "*";
373 else
374 OS << ".*";
375 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000376 OS << "\n";
377 }
378
Daniel Jasperddd2dfc2013-09-24 09:14:14 +0000379 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
380 OS.indent(Indent + 2);
381 OS << "use ";
382 OS << DirectUses[I]->getFullModuleName();
383 OS << "\n";
384 }
385
386 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
387 OS.indent(Indent + 2);
388 OS << "use ";
389 printModuleId(OS, UnresolvedDirectUses[I]);
390 OS << "\n";
391 }
392
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000393 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
394 OS.indent(Indent + 2);
395 OS << "link ";
396 if (LinkLibraries[I].IsFramework)
397 OS << "framework ";
398 OS << "\"";
399 OS.write_escaped(LinkLibraries[I].Library);
400 OS << "\"";
401 }
402
Douglas Gregor906d66a2013-03-20 21:10:35 +0000403 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
404 OS.indent(Indent + 2);
405 OS << "conflict ";
406 printModuleId(OS, UnresolvedConflicts[I].Id);
407 OS << ", \"";
408 OS.write_escaped(UnresolvedConflicts[I].Message);
409 OS << "\"\n";
410 }
411
412 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
413 OS.indent(Indent + 2);
414 OS << "conflict ";
415 OS << Conflicts[I].Other->getFullModuleName();
416 OS << ", \"";
417 OS.write_escaped(Conflicts[I].Message);
418 OS << "\"\n";
419 }
420
Douglas Gregor1e123682011-12-05 22:27:44 +0000421 if (InferSubmodules) {
422 OS.indent(Indent + 2);
423 if (InferExplicitSubmodules)
424 OS << "explicit ";
425 OS << "module * {\n";
426 if (InferExportWildcard) {
427 OS.indent(Indent + 4);
428 OS << "export *\n";
429 }
430 OS.indent(Indent + 2);
431 OS << "}\n";
432 }
433
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000434 OS.indent(Indent);
435 OS << "}\n";
436}
437
438void Module::dump() const {
439 print(llvm::errs());
440}
441
442