blob: e7e37ced80909988c84c796a93527e8b7965e2f9 [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
Stephen Hines651f13c2014-04-23 16:59:28 -070027Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
Stephen Hines176edba2014-12-01 14:53:08 -080028 bool IsFramework, bool IsExplicit)
Stephen Hines0e2c34f2015-03-23 12:09:02 -070029 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), Directory(),
Stephen Hines6bcf27b2014-05-29 04:14:42 -070030 Umbrella(), ASTFile(nullptr), IsMissingRequirement(false),
31 IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework),
32 IsExplicit(IsExplicit), IsSystem(false), IsExternC(false),
33 IsInferred(false), InferSubmodules(false), InferExplicitSubmodules(false),
34 InferExportWildcard(false), ConfigMacrosExhaustive(false),
35 NameVisibility(Hidden) {
Douglas Gregorb7a78192012-01-04 23:32:19 +000036 if (Parent) {
37 if (!Parent->isAvailable())
38 IsAvailable = false;
Douglas Gregor2f04f182012-02-02 18:42:48 +000039 if (Parent->IsSystem)
40 IsSystem = true;
Stephen Hines651f13c2014-04-23 16:59:28 -070041 if (Parent->IsExternC)
42 IsExternC = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070043 IsMissingRequirement = Parent->IsMissingRequirement;
Douglas Gregorb7a78192012-01-04 23:32:19 +000044
45 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
46 Parent->SubModules.push_back(this);
47 }
48}
49
Douglas Gregor1a4761e2011-11-30 23:21:26 +000050Module::~Module() {
Douglas Gregorb7a78192012-01-04 23:32:19 +000051 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +000052 I != IEnd; ++I) {
Douglas Gregorb7a78192012-01-04 23:32:19 +000053 delete *I;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000054 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +000055}
56
Douglas Gregor51f564f2011-12-31 04:05:44 +000057/// \brief Determine whether a translation unit built using the current
58/// language options has the given feature.
Douglas Gregordc58aa72012-01-30 06:01:29 +000059static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
60 const TargetInfo &Target) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -070061 bool HasFeature = llvm::StringSwitch<bool>(Feature)
62 .Case("altivec", LangOpts.AltiVec)
63 .Case("blocks", LangOpts.Blocks)
64 .Case("cplusplus", LangOpts.CPlusPlus)
65 .Case("cplusplus11", LangOpts.CPlusPlus11)
66 .Case("objc", LangOpts.ObjC1)
67 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
68 .Case("opencl", LangOpts.OpenCL)
69 .Case("tls", Target.isTLSSupported())
70 .Default(Target.hasFeature(Feature));
71 if (!HasFeature)
72 HasFeature = std::find(LangOpts.ModuleFeatures.begin(),
73 LangOpts.ModuleFeatures.end(),
74 Feature) != LangOpts.ModuleFeatures.end();
75 return HasFeature;
Douglas Gregor51f564f2011-12-31 04:05:44 +000076}
77
Stephen Hines0e2c34f2015-03-23 12:09:02 -070078bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
79 Requirement &Req,
80 UnresolvedHeaderDirective &MissingHeader) const {
Douglas Gregor51f564f2011-12-31 04:05:44 +000081 if (IsAvailable)
82 return true;
83
84 for (const Module *Current = this; Current; Current = Current->Parent) {
Stephen Hines651f13c2014-04-23 16:59:28 -070085 if (!Current->MissingHeaders.empty()) {
86 MissingHeader = Current->MissingHeaders.front();
87 return false;
88 }
Richard Smith5794b532013-10-28 22:18:19 +000089 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
90 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
91 Current->Requirements[I].second) {
92 Req = Current->Requirements[I];
Douglas Gregor51f564f2011-12-31 04:05:44 +000093 return false;
94 }
95 }
96 }
97
98 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor51f564f2011-12-31 04:05:44 +000099}
100
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700101bool Module::isSubModuleOf(const Module *Other) const {
Douglas Gregor0adaa882011-12-05 17:28:06 +0000102 const Module *This = this;
103 do {
104 if (This == Other)
105 return true;
106
107 This = This->Parent;
108 } while (This);
109
110 return false;
111}
112
Douglas Gregor1e123682011-12-05 22:27:44 +0000113const Module *Module::getTopLevelModule() const {
114 const Module *Result = this;
115 while (Result->Parent)
116 Result = Result->Parent;
117
118 return Result;
119}
120
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000121std::string Module::getFullModuleName() const {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000122 SmallVector<StringRef, 2> Names;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000123
124 // Build up the set of module names (from innermost to outermost).
125 for (const Module *M = this; M; M = M->Parent)
126 Names.push_back(M->Name);
127
128 std::string Result;
Craig Topper163fbf82013-07-08 03:55:09 +0000129 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
130 IEnd = Names.rend();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000131 I != IEnd; ++I) {
132 if (!Result.empty())
133 Result += '.';
134
135 Result += *I;
136 }
137
138 return Result;
139}
140
Douglas Gregor10694ce2011-12-08 17:39:04 +0000141const DirectoryEntry *Module::getUmbrellaDir() const {
142 if (const FileEntry *Header = getUmbrellaHeader())
143 return Header->getDir();
144
145 return Umbrella.dyn_cast<const DirectoryEntry *>();
146}
147
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +0000148ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
149 if (!TopHeaderNames.empty()) {
150 for (std::vector<std::string>::iterator
151 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
152 if (const FileEntry *FE = FileMgr.getFile(*I))
153 TopHeaders.insert(FE);
154 }
155 TopHeaderNames.clear();
156 }
157
158 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
159}
160
Richard Smith5794b532013-10-28 22:18:19 +0000161void Module::addRequirement(StringRef Feature, bool RequiredState,
162 const LangOptions &LangOpts,
Douglas Gregordc58aa72012-01-30 06:01:29 +0000163 const TargetInfo &Target) {
Richard Smith5794b532013-10-28 22:18:19 +0000164 Requirements.push_back(Requirement(Feature, RequiredState));
Douglas Gregor51f564f2011-12-31 04:05:44 +0000165
166 // If this feature is currently available, we're done.
Richard Smith5794b532013-10-28 22:18:19 +0000167 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
Douglas Gregor51f564f2011-12-31 04:05:44 +0000168 return;
169
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700170 markUnavailable(/*MissingRequirement*/true);
171}
172
173void Module::markUnavailable(bool MissingRequirement) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000174 if (!IsAvailable)
175 return;
176
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000177 SmallVector<Module *, 2> Stack;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000178 Stack.push_back(this);
179 while (!Stack.empty()) {
180 Module *Current = Stack.back();
181 Stack.pop_back();
182
183 if (!Current->IsAvailable)
184 continue;
185
186 Current->IsAvailable = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700187 Current->IsMissingRequirement |= MissingRequirement;
Douglas Gregorb7a78192012-01-04 23:32:19 +0000188 for (submodule_iterator Sub = Current->submodule_begin(),
189 SubEnd = Current->submodule_end();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000190 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +0000191 if ((*Sub)->IsAvailable)
192 Stack.push_back(*Sub);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000193 }
194 }
195}
196
Douglas Gregorb7a78192012-01-04 23:32:19 +0000197Module *Module::findSubmodule(StringRef Name) const {
198 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
199 if (Pos == SubModuleIndex.end())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700200 return nullptr;
201
Douglas Gregorb7a78192012-01-04 23:32:19 +0000202 return SubModules[Pos->getValue()];
203}
204
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000205static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000206 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
207 if (I)
208 OS << ".";
209 OS << Id[I].first;
210 }
211}
212
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +0000213void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
Dmitri Gribenko86250892013-11-04 21:51:33 +0000214 // All non-explicit submodules are exported.
215 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
216 E = SubModules.end();
217 I != E; ++I) {
218 Module *Mod = *I;
219 if (!Mod->IsExplicit)
220 Exported.push_back(Mod);
221 }
222
223 // Find re-exported modules by filtering the list of imported modules.
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +0000224 bool AnyWildcard = false;
225 bool UnrestrictedWildcard = false;
226 SmallVector<Module *, 4> WildcardRestrictions;
227 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
228 Module *Mod = Exports[I].getPointer();
229 if (!Exports[I].getInt()) {
230 // Export a named module directly; no wildcards involved.
231 Exported.push_back(Mod);
232
233 continue;
234 }
235
236 // Wildcard export: export all of the imported modules that match
237 // the given pattern.
238 AnyWildcard = true;
239 if (UnrestrictedWildcard)
240 continue;
241
242 if (Module *Restriction = Exports[I].getPointer())
243 WildcardRestrictions.push_back(Restriction);
244 else {
245 WildcardRestrictions.clear();
246 UnrestrictedWildcard = true;
247 }
248 }
249
250 // If there were any wildcards, push any imported modules that were
251 // re-exported by the wildcard restriction.
252 if (!AnyWildcard)
253 return;
254
255 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
256 Module *Mod = Imports[I];
257 bool Acceptable = UnrestrictedWildcard;
258 if (!Acceptable) {
259 // Check whether this module meets one of the restrictions.
260 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
261 Module *Restriction = WildcardRestrictions[R];
262 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
263 Acceptable = true;
264 break;
265 }
266 }
267 }
268
269 if (!Acceptable)
270 continue;
271
272 Exported.push_back(Mod);
273 }
274}
275
Richard Smithb7751002013-07-25 23:08:39 +0000276void Module::buildVisibleModulesCache() const {
277 assert(VisibleModulesCache.empty() && "cache does not need building");
278
279 // This module is visible to itself.
280 VisibleModulesCache.insert(this);
281
Dmitri Gribenkobc64d352013-10-31 22:24:10 +0000282 // Every imported module is visible.
Richard Smith8b1ab402013-11-01 02:19:14 +0000283 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
Dmitri Gribenkobc64d352013-10-31 22:24:10 +0000284 while (!Stack.empty()) {
285 Module *CurrModule = Stack.pop_back_val();
Richard Smithb7751002013-07-25 23:08:39 +0000286
Richard Smith8b1ab402013-11-01 02:19:14 +0000287 // Every module transitively exported by an imported module is visible.
288 if (VisibleModulesCache.insert(CurrModule).second)
289 CurrModule->getExportedModules(Stack);
Richard Smithb7751002013-07-25 23:08:39 +0000290 }
291}
292
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000293void Module::print(raw_ostream &OS, unsigned Indent) const {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000294 OS.indent(Indent);
295 if (IsFramework)
296 OS << "framework ";
297 if (IsExplicit)
298 OS << "explicit ";
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000299 OS << "module " << Name;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000300
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700301 if (IsSystem || IsExternC) {
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000302 OS.indent(Indent + 2);
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700303 if (IsSystem)
304 OS << " [system]";
305 if (IsExternC)
306 OS << " [extern_c]";
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000307 }
308
309 OS << " {\n";
310
Richard Smith5794b532013-10-28 22:18:19 +0000311 if (!Requirements.empty()) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000312 OS.indent(Indent + 2);
313 OS << "requires ";
Richard Smith5794b532013-10-28 22:18:19 +0000314 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000315 if (I)
316 OS << ", ";
Richard Smith5794b532013-10-28 22:18:19 +0000317 if (!Requirements[I].second)
318 OS << "!";
319 OS << Requirements[I].first;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000320 }
321 OS << "\n";
322 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000323
Douglas Gregor10694ce2011-12-08 17:39:04 +0000324 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000325 OS.indent(Indent + 2);
Douglas Gregor489ad432011-12-08 18:00:48 +0000326 OS << "umbrella header \"";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000327 OS.write_escaped(UmbrellaHeader->getName());
328 OS << "\"\n";
Douglas Gregor489ad432011-12-08 18:00:48 +0000329 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
330 OS.indent(Indent + 2);
331 OS << "umbrella \"";
332 OS.write_escaped(UmbrellaDir->getName());
333 OS << "\"\n";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000334 }
Douglas Gregor63a72682013-03-20 00:22:05 +0000335
336 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
337 OS.indent(Indent + 2);
338 OS << "config_macros ";
339 if (ConfigMacrosExhaustive)
Douglas Gregor970e4412013-03-20 03:59:18 +0000340 OS << "[exhaustive]";
Douglas Gregor63a72682013-03-20 00:22:05 +0000341 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
342 if (I)
343 OS << ", ";
344 OS << ConfigMacros[I];
345 }
Douglas Gregor970e4412013-03-20 03:59:18 +0000346 OS << "\n";
Douglas Gregor63a72682013-03-20 00:22:05 +0000347 }
348
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700349 struct {
Stephen Hines176edba2014-12-01 14:53:08 -0800350 StringRef Prefix;
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700351 HeaderKind Kind;
352 } Kinds[] = {{"", HK_Normal},
353 {"textual ", HK_Textual},
354 {"private ", HK_Private},
355 {"private textual ", HK_PrivateTextual},
356 {"exclude ", HK_Excluded}};
Stephen Hines176edba2014-12-01 14:53:08 -0800357
358 for (auto &K : Kinds) {
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700359 for (auto &H : Headers[K.Kind]) {
Stephen Hines176edba2014-12-01 14:53:08 -0800360 OS.indent(Indent + 2);
361 OS << K.Prefix << "header \"";
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700362 OS.write_escaped(H.NameAsWritten);
Stephen Hines176edba2014-12-01 14:53:08 -0800363 OS << "\"\n";
364 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000365 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000366
Douglas Gregorb7a78192012-01-04 23:32:19 +0000367 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000368 MI != MIEnd; ++MI)
Stephen Hines176edba2014-12-01 14:53:08 -0800369 // Print inferred subframework modules so that we don't need to re-infer
370 // them (requires expensive directory iteration + stat calls) when we build
371 // the module. Regular inferred submodules are OK, as we need to look at all
372 // those header files anyway.
373 if (!(*MI)->IsInferred || (*MI)->IsFramework)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700374 (*MI)->print(OS, Indent + 2);
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000375
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000376 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
377 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000378 OS << "export ";
379 if (Module *Restriction = Exports[I].getPointer()) {
380 OS << Restriction->getFullModuleName();
381 if (Exports[I].getInt())
382 OS << ".*";
383 } else {
384 OS << "*";
385 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000386 OS << "\n";
387 }
388
389 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
390 OS.indent(Indent + 2);
391 OS << "export ";
392 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000393 if (UnresolvedExports[I].Wildcard) {
394 if (UnresolvedExports[I].Id.empty())
395 OS << "*";
396 else
397 OS << ".*";
398 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000399 OS << "\n";
400 }
401
Daniel Jasperddd2dfc2013-09-24 09:14:14 +0000402 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
403 OS.indent(Indent + 2);
404 OS << "use ";
405 OS << DirectUses[I]->getFullModuleName();
406 OS << "\n";
407 }
408
409 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
410 OS.indent(Indent + 2);
411 OS << "use ";
412 printModuleId(OS, UnresolvedDirectUses[I]);
413 OS << "\n";
414 }
415
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000416 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
417 OS.indent(Indent + 2);
418 OS << "link ";
419 if (LinkLibraries[I].IsFramework)
420 OS << "framework ";
421 OS << "\"";
422 OS.write_escaped(LinkLibraries[I].Library);
423 OS << "\"";
424 }
425
Douglas Gregor906d66a2013-03-20 21:10:35 +0000426 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
427 OS.indent(Indent + 2);
428 OS << "conflict ";
429 printModuleId(OS, UnresolvedConflicts[I].Id);
430 OS << ", \"";
431 OS.write_escaped(UnresolvedConflicts[I].Message);
432 OS << "\"\n";
433 }
434
435 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
436 OS.indent(Indent + 2);
437 OS << "conflict ";
438 OS << Conflicts[I].Other->getFullModuleName();
439 OS << ", \"";
440 OS.write_escaped(Conflicts[I].Message);
441 OS << "\"\n";
442 }
443
Douglas Gregor1e123682011-12-05 22:27:44 +0000444 if (InferSubmodules) {
445 OS.indent(Indent + 2);
446 if (InferExplicitSubmodules)
447 OS << "explicit ";
448 OS << "module * {\n";
449 if (InferExportWildcard) {
450 OS.indent(Indent + 4);
451 OS << "export *\n";
452 }
453 OS.indent(Indent + 2);
454 OS << "}\n";
455 }
456
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000457 OS.indent(Indent);
458 OS << "}\n";
459}
460
461void Module::dump() const {
462 print(llvm::errs());
463}
464
465