blob: f689c733d9c03738f4c83fceb2bb0118ac29600a [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 Hines6bcf27b2014-05-29 04:14:42 -070028 const FileEntry *File, bool IsFramework, bool IsExplicit)
29 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), ModuleMap(File),
30 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) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000061 return llvm::StringSwitch<bool>(Feature)
Douglas Gregore727d212012-01-30 06:38:25 +000062 .Case("altivec", LangOpts.AltiVec)
Douglas Gregor51f564f2011-12-31 04:05:44 +000063 .Case("blocks", LangOpts.Blocks)
64 .Case("cplusplus", LangOpts.CPlusPlus)
Richard Smith80ad52f2013-01-02 11:42:31 +000065 .Case("cplusplus11", LangOpts.CPlusPlus11)
Douglas Gregor51f564f2011-12-31 04:05:44 +000066 .Case("objc", LangOpts.ObjC1)
67 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
Douglas Gregore727d212012-01-30 06:38:25 +000068 .Case("opencl", LangOpts.OpenCL)
69 .Case("tls", Target.isTLSSupported())
70 .Default(Target.hasFeature(Feature));
Douglas Gregor51f564f2011-12-31 04:05:44 +000071}
72
Richard Smith5794b532013-10-28 22:18:19 +000073bool
Douglas Gregordc58aa72012-01-30 06:01:29 +000074Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
Stephen Hines651f13c2014-04-23 16:59:28 -070075 Requirement &Req, HeaderDirective &MissingHeader) const {
Douglas Gregor51f564f2011-12-31 04:05:44 +000076 if (IsAvailable)
77 return true;
78
79 for (const Module *Current = this; Current; Current = Current->Parent) {
Stephen Hines651f13c2014-04-23 16:59:28 -070080 if (!Current->MissingHeaders.empty()) {
81 MissingHeader = Current->MissingHeaders.front();
82 return false;
83 }
Richard Smith5794b532013-10-28 22:18:19 +000084 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
85 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
86 Current->Requirements[I].second) {
87 Req = Current->Requirements[I];
Douglas Gregor51f564f2011-12-31 04:05:44 +000088 return false;
89 }
90 }
91 }
92
93 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor51f564f2011-12-31 04:05:44 +000094}
95
Stephen Hines6bcf27b2014-05-29 04:14:42 -070096bool Module::isSubModuleOf(const Module *Other) const {
Douglas Gregor0adaa882011-12-05 17:28:06 +000097 const Module *This = this;
98 do {
99 if (This == Other)
100 return true;
101
102 This = This->Parent;
103 } while (This);
104
105 return false;
106}
107
Douglas Gregor1e123682011-12-05 22:27:44 +0000108const Module *Module::getTopLevelModule() const {
109 const Module *Result = this;
110 while (Result->Parent)
111 Result = Result->Parent;
112
113 return Result;
114}
115
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000116std::string Module::getFullModuleName() const {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000117 SmallVector<StringRef, 2> Names;
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000118
119 // Build up the set of module names (from innermost to outermost).
120 for (const Module *M = this; M; M = M->Parent)
121 Names.push_back(M->Name);
122
123 std::string Result;
Craig Topper163fbf82013-07-08 03:55:09 +0000124 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
125 IEnd = Names.rend();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000126 I != IEnd; ++I) {
127 if (!Result.empty())
128 Result += '.';
129
130 Result += *I;
131 }
132
133 return Result;
134}
135
Douglas Gregor10694ce2011-12-08 17:39:04 +0000136const DirectoryEntry *Module::getUmbrellaDir() const {
137 if (const FileEntry *Header = getUmbrellaHeader())
138 return Header->getDir();
139
140 return Umbrella.dyn_cast<const DirectoryEntry *>();
141}
142
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +0000143ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
144 if (!TopHeaderNames.empty()) {
145 for (std::vector<std::string>::iterator
146 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
147 if (const FileEntry *FE = FileMgr.getFile(*I))
148 TopHeaders.insert(FE);
149 }
150 TopHeaderNames.clear();
151 }
152
153 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
154}
155
Richard Smith5794b532013-10-28 22:18:19 +0000156void Module::addRequirement(StringRef Feature, bool RequiredState,
157 const LangOptions &LangOpts,
Douglas Gregordc58aa72012-01-30 06:01:29 +0000158 const TargetInfo &Target) {
Richard Smith5794b532013-10-28 22:18:19 +0000159 Requirements.push_back(Requirement(Feature, RequiredState));
Douglas Gregor51f564f2011-12-31 04:05:44 +0000160
161 // If this feature is currently available, we're done.
Richard Smith5794b532013-10-28 22:18:19 +0000162 if (hasFeature(Feature, LangOpts, Target) == RequiredState)
Douglas Gregor51f564f2011-12-31 04:05:44 +0000163 return;
164
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700165 markUnavailable(/*MissingRequirement*/true);
166}
167
168void Module::markUnavailable(bool MissingRequirement) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000169 if (!IsAvailable)
170 return;
171
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000172 SmallVector<Module *, 2> Stack;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000173 Stack.push_back(this);
174 while (!Stack.empty()) {
175 Module *Current = Stack.back();
176 Stack.pop_back();
177
178 if (!Current->IsAvailable)
179 continue;
180
181 Current->IsAvailable = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700182 Current->IsMissingRequirement |= MissingRequirement;
Douglas Gregorb7a78192012-01-04 23:32:19 +0000183 for (submodule_iterator Sub = Current->submodule_begin(),
184 SubEnd = Current->submodule_end();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000185 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +0000186 if ((*Sub)->IsAvailable)
187 Stack.push_back(*Sub);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000188 }
189 }
190}
191
Douglas Gregorb7a78192012-01-04 23:32:19 +0000192Module *Module::findSubmodule(StringRef Name) const {
193 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
194 if (Pos == SubModuleIndex.end())
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700195 return nullptr;
196
Douglas Gregorb7a78192012-01-04 23:32:19 +0000197 return SubModules[Pos->getValue()];
198}
199
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000200static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000201 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
202 if (I)
203 OS << ".";
204 OS << Id[I].first;
205 }
206}
207
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +0000208void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
Dmitri Gribenko86250892013-11-04 21:51:33 +0000209 // All non-explicit submodules are exported.
210 for (std::vector<Module *>::const_iterator I = SubModules.begin(),
211 E = SubModules.end();
212 I != E; ++I) {
213 Module *Mod = *I;
214 if (!Mod->IsExplicit)
215 Exported.push_back(Mod);
216 }
217
218 // Find re-exported modules by filtering the list of imported modules.
Argyrios Kyrtzidis21a00042013-02-19 19:34:40 +0000219 bool AnyWildcard = false;
220 bool UnrestrictedWildcard = false;
221 SmallVector<Module *, 4> WildcardRestrictions;
222 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
223 Module *Mod = Exports[I].getPointer();
224 if (!Exports[I].getInt()) {
225 // Export a named module directly; no wildcards involved.
226 Exported.push_back(Mod);
227
228 continue;
229 }
230
231 // Wildcard export: export all of the imported modules that match
232 // the given pattern.
233 AnyWildcard = true;
234 if (UnrestrictedWildcard)
235 continue;
236
237 if (Module *Restriction = Exports[I].getPointer())
238 WildcardRestrictions.push_back(Restriction);
239 else {
240 WildcardRestrictions.clear();
241 UnrestrictedWildcard = true;
242 }
243 }
244
245 // If there were any wildcards, push any imported modules that were
246 // re-exported by the wildcard restriction.
247 if (!AnyWildcard)
248 return;
249
250 for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
251 Module *Mod = Imports[I];
252 bool Acceptable = UnrestrictedWildcard;
253 if (!Acceptable) {
254 // Check whether this module meets one of the restrictions.
255 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
256 Module *Restriction = WildcardRestrictions[R];
257 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
258 Acceptable = true;
259 break;
260 }
261 }
262 }
263
264 if (!Acceptable)
265 continue;
266
267 Exported.push_back(Mod);
268 }
269}
270
Richard Smithb7751002013-07-25 23:08:39 +0000271void Module::buildVisibleModulesCache() const {
272 assert(VisibleModulesCache.empty() && "cache does not need building");
273
274 // This module is visible to itself.
275 VisibleModulesCache.insert(this);
276
Dmitri Gribenkobc64d352013-10-31 22:24:10 +0000277 // Every imported module is visible.
Richard Smith8b1ab402013-11-01 02:19:14 +0000278 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
Dmitri Gribenkobc64d352013-10-31 22:24:10 +0000279 while (!Stack.empty()) {
280 Module *CurrModule = Stack.pop_back_val();
Richard Smithb7751002013-07-25 23:08:39 +0000281
Richard Smith8b1ab402013-11-01 02:19:14 +0000282 // Every module transitively exported by an imported module is visible.
283 if (VisibleModulesCache.insert(CurrModule).second)
284 CurrModule->getExportedModules(Stack);
Richard Smithb7751002013-07-25 23:08:39 +0000285 }
286}
287
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000288void Module::print(raw_ostream &OS, unsigned Indent) const {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000289 OS.indent(Indent);
290 if (IsFramework)
291 OS << "framework ";
292 if (IsExplicit)
293 OS << "explicit ";
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000294 OS << "module " << Name;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000295
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000296 if (IsSystem) {
297 OS.indent(Indent + 2);
298 OS << " [system]";
299 }
300
301 OS << " {\n";
302
Richard Smith5794b532013-10-28 22:18:19 +0000303 if (!Requirements.empty()) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000304 OS.indent(Indent + 2);
305 OS << "requires ";
Richard Smith5794b532013-10-28 22:18:19 +0000306 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000307 if (I)
308 OS << ", ";
Richard Smith5794b532013-10-28 22:18:19 +0000309 if (!Requirements[I].second)
310 OS << "!";
311 OS << Requirements[I].first;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000312 }
313 OS << "\n";
314 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000315
Douglas Gregor10694ce2011-12-08 17:39:04 +0000316 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000317 OS.indent(Indent + 2);
Douglas Gregor489ad432011-12-08 18:00:48 +0000318 OS << "umbrella header \"";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000319 OS.write_escaped(UmbrellaHeader->getName());
320 OS << "\"\n";
Douglas Gregor489ad432011-12-08 18:00:48 +0000321 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
322 OS.indent(Indent + 2);
323 OS << "umbrella \"";
324 OS.write_escaped(UmbrellaDir->getName());
325 OS << "\"\n";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000326 }
Douglas Gregor63a72682013-03-20 00:22:05 +0000327
328 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
329 OS.indent(Indent + 2);
330 OS << "config_macros ";
331 if (ConfigMacrosExhaustive)
Douglas Gregor970e4412013-03-20 03:59:18 +0000332 OS << "[exhaustive]";
Douglas Gregor63a72682013-03-20 00:22:05 +0000333 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
334 if (I)
335 OS << ", ";
336 OS << ConfigMacros[I];
337 }
Douglas Gregor970e4412013-03-20 03:59:18 +0000338 OS << "\n";
Douglas Gregor63a72682013-03-20 00:22:05 +0000339 }
340
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000341 for (unsigned I = 0, N = NormalHeaders.size(); I != N; ++I) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000342 OS.indent(Indent + 2);
343 OS << "header \"";
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000344 OS.write_escaped(NormalHeaders[I]->getName());
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000345 OS << "\"\n";
346 }
Douglas Gregor2b49d1f2012-10-15 06:28:11 +0000347
348 for (unsigned I = 0, N = ExcludedHeaders.size(); I != N; ++I) {
349 OS.indent(Indent + 2);
350 OS << "exclude header \"";
351 OS.write_escaped(ExcludedHeaders[I]->getName());
352 OS << "\"\n";
353 }
Lawrence Crowlbc3f6282013-06-20 21:14:14 +0000354
355 for (unsigned I = 0, N = PrivateHeaders.size(); I != N; ++I) {
356 OS.indent(Indent + 2);
357 OS << "private header \"";
358 OS.write_escaped(PrivateHeaders[I]->getName());
359 OS << "\"\n";
360 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000361
Douglas Gregorb7a78192012-01-04 23:32:19 +0000362 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000363 MI != MIEnd; ++MI)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700364 if (!(*MI)->IsInferred)
365 (*MI)->print(OS, Indent + 2);
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000366
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000367 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
368 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000369 OS << "export ";
370 if (Module *Restriction = Exports[I].getPointer()) {
371 OS << Restriction->getFullModuleName();
372 if (Exports[I].getInt())
373 OS << ".*";
374 } else {
375 OS << "*";
376 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000377 OS << "\n";
378 }
379
380 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
381 OS.indent(Indent + 2);
382 OS << "export ";
383 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000384 if (UnresolvedExports[I].Wildcard) {
385 if (UnresolvedExports[I].Id.empty())
386 OS << "*";
387 else
388 OS << ".*";
389 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000390 OS << "\n";
391 }
392
Daniel Jasperddd2dfc2013-09-24 09:14:14 +0000393 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
394 OS.indent(Indent + 2);
395 OS << "use ";
396 OS << DirectUses[I]->getFullModuleName();
397 OS << "\n";
398 }
399
400 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
401 OS.indent(Indent + 2);
402 OS << "use ";
403 printModuleId(OS, UnresolvedDirectUses[I]);
404 OS << "\n";
405 }
406
Douglas Gregorb6cbe512013-01-14 17:21:00 +0000407 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
408 OS.indent(Indent + 2);
409 OS << "link ";
410 if (LinkLibraries[I].IsFramework)
411 OS << "framework ";
412 OS << "\"";
413 OS.write_escaped(LinkLibraries[I].Library);
414 OS << "\"";
415 }
416
Douglas Gregor906d66a2013-03-20 21:10:35 +0000417 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
418 OS.indent(Indent + 2);
419 OS << "conflict ";
420 printModuleId(OS, UnresolvedConflicts[I].Id);
421 OS << ", \"";
422 OS.write_escaped(UnresolvedConflicts[I].Message);
423 OS << "\"\n";
424 }
425
426 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
427 OS.indent(Indent + 2);
428 OS << "conflict ";
429 OS << Conflicts[I].Other->getFullModuleName();
430 OS << ", \"";
431 OS.write_escaped(Conflicts[I].Message);
432 OS << "\"\n";
433 }
434
Douglas Gregor1e123682011-12-05 22:27:44 +0000435 if (InferSubmodules) {
436 OS.indent(Indent + 2);
437 if (InferExplicitSubmodules)
438 OS << "explicit ";
439 OS << "module * {\n";
440 if (InferExportWildcard) {
441 OS.indent(Indent + 4);
442 OS << "export *\n";
443 }
444 OS.indent(Indent + 2);
445 OS << "}\n";
446 }
447
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000448 OS.indent(Indent);
449 OS << "}\n";
450}
451
452void Module::dump() const {
453 print(llvm::errs());
454}
455
456