blob: c5838fb65f2b45024c185415e0f974c54aef7235 [file] [log] [blame]
Douglas Gregor1a4761e2011-11-30 23:21:26 +00001//===--- Module.h - Describe a module ---------------------------*- C++ -*-===//
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"
Douglas Gregor51f564f2011-12-31 04:05:44 +000016#include "clang/Basic/LangOptions.h"
Douglas Gregore727d212012-01-30 06:38:25 +000017#include "clang/Basic/TargetInfo.h"
Douglas Gregor51f564f2011-12-31 04:05:44 +000018#include "llvm/Support/ErrorHandling.h"
Douglas Gregor1a4761e2011-11-30 23:21:26 +000019#include "llvm/Support/raw_ostream.h"
Douglas Gregor51f564f2011-12-31 04:05:44 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringSwitch.h"
Douglas Gregor1a4761e2011-11-30 23:21:26 +000022using namespace clang;
23
Douglas Gregorb7a78192012-01-04 23:32:19 +000024Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
25 bool IsFramework, bool IsExplicit)
26 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
27 Umbrella(), IsAvailable(true), IsFromModuleFile(false),
Douglas Gregora1f1fad2012-01-27 19:52:33 +000028 IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false),
29 InferSubmodules(false), InferExplicitSubmodules(false),
30 InferExportWildcard(false), NameVisibility(Hidden)
Douglas Gregorb7a78192012-01-04 23:32:19 +000031{
32 if (Parent) {
33 if (!Parent->isAvailable())
34 IsAvailable = false;
35
36 Parent->SubModuleIndex[Name] = Parent->SubModules.size();
37 Parent->SubModules.push_back(this);
38 }
39}
40
Douglas Gregor1a4761e2011-11-30 23:21:26 +000041Module::~Module() {
Douglas Gregorb7a78192012-01-04 23:32:19 +000042 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +000043 I != IEnd; ++I) {
Douglas Gregorb7a78192012-01-04 23:32:19 +000044 delete *I;
Douglas Gregor1a4761e2011-11-30 23:21:26 +000045 }
46
47}
48
Douglas Gregor51f564f2011-12-31 04:05:44 +000049/// \brief Determine whether a translation unit built using the current
50/// language options has the given feature.
Douglas Gregordc58aa72012-01-30 06:01:29 +000051static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
52 const TargetInfo &Target) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000053 return llvm::StringSwitch<bool>(Feature)
Douglas Gregore727d212012-01-30 06:38:25 +000054 .Case("altivec", LangOpts.AltiVec)
Douglas Gregor51f564f2011-12-31 04:05:44 +000055 .Case("blocks", LangOpts.Blocks)
56 .Case("cplusplus", LangOpts.CPlusPlus)
57 .Case("cplusplus11", LangOpts.CPlusPlus0x)
58 .Case("objc", LangOpts.ObjC1)
59 .Case("objc_arc", LangOpts.ObjCAutoRefCount)
Douglas Gregore727d212012-01-30 06:38:25 +000060 .Case("opencl", LangOpts.OpenCL)
61 .Case("tls", Target.isTLSSupported())
62 .Default(Target.hasFeature(Feature));
Douglas Gregor51f564f2011-12-31 04:05:44 +000063}
64
65bool
Douglas Gregordc58aa72012-01-30 06:01:29 +000066Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
67 StringRef &Feature) const {
Douglas Gregor51f564f2011-12-31 04:05:44 +000068 if (IsAvailable)
69 return true;
70
71 for (const Module *Current = this; Current; Current = Current->Parent) {
72 for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) {
Douglas Gregordc58aa72012-01-30 06:01:29 +000073 if (!hasFeature(Current->Requires[I], LangOpts, Target)) {
Douglas Gregor51f564f2011-12-31 04:05:44 +000074 Feature = Current->Requires[I];
75 return false;
76 }
77 }
78 }
79
80 llvm_unreachable("could not find a reason why module is unavailable");
Douglas Gregor51f564f2011-12-31 04:05:44 +000081}
82
Douglas Gregor0adaa882011-12-05 17:28:06 +000083bool Module::isSubModuleOf(Module *Other) const {
84 const Module *This = this;
85 do {
86 if (This == Other)
87 return true;
88
89 This = This->Parent;
90 } while (This);
91
92 return false;
93}
94
Douglas Gregor1e123682011-12-05 22:27:44 +000095const Module *Module::getTopLevelModule() const {
96 const Module *Result = this;
97 while (Result->Parent)
98 Result = Result->Parent;
99
100 return Result;
101}
102
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000103std::string Module::getFullModuleName() const {
104 llvm::SmallVector<StringRef, 2> Names;
105
106 // Build up the set of module names (from innermost to outermost).
107 for (const Module *M = this; M; M = M->Parent)
108 Names.push_back(M->Name);
109
110 std::string Result;
111 for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(),
112 IEnd = Names.rend();
113 I != IEnd; ++I) {
114 if (!Result.empty())
115 Result += '.';
116
117 Result += *I;
118 }
119
120 return Result;
121}
122
Douglas Gregor10694ce2011-12-08 17:39:04 +0000123const DirectoryEntry *Module::getUmbrellaDir() const {
124 if (const FileEntry *Header = getUmbrellaHeader())
125 return Header->getDir();
126
127 return Umbrella.dyn_cast<const DirectoryEntry *>();
128}
129
Douglas Gregordc58aa72012-01-30 06:01:29 +0000130void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts,
131 const TargetInfo &Target) {
Douglas Gregor51f564f2011-12-31 04:05:44 +0000132 Requires.push_back(Feature);
133
134 // If this feature is currently available, we're done.
Douglas Gregordc58aa72012-01-30 06:01:29 +0000135 if (hasFeature(Feature, LangOpts, Target))
Douglas Gregor51f564f2011-12-31 04:05:44 +0000136 return;
137
138 if (!IsAvailable)
139 return;
140
141 llvm::SmallVector<Module *, 2> Stack;
142 Stack.push_back(this);
143 while (!Stack.empty()) {
144 Module *Current = Stack.back();
145 Stack.pop_back();
146
147 if (!Current->IsAvailable)
148 continue;
149
150 Current->IsAvailable = false;
Douglas Gregorb7a78192012-01-04 23:32:19 +0000151 for (submodule_iterator Sub = Current->submodule_begin(),
152 SubEnd = Current->submodule_end();
Douglas Gregor51f564f2011-12-31 04:05:44 +0000153 Sub != SubEnd; ++Sub) {
Douglas Gregorb7a78192012-01-04 23:32:19 +0000154 if ((*Sub)->IsAvailable)
155 Stack.push_back(*Sub);
Douglas Gregor51f564f2011-12-31 04:05:44 +0000156 }
157 }
158}
159
Douglas Gregorb7a78192012-01-04 23:32:19 +0000160Module *Module::findSubmodule(StringRef Name) const {
161 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
162 if (Pos == SubModuleIndex.end())
163 return 0;
164
165 return SubModules[Pos->getValue()];
166}
167
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000168static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) {
169 for (unsigned I = 0, N = Id.size(); I != N; ++I) {
170 if (I)
171 OS << ".";
172 OS << Id[I].first;
173 }
174}
175
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000176void Module::print(llvm::raw_ostream &OS, unsigned Indent) const {
177 OS.indent(Indent);
178 if (IsFramework)
179 OS << "framework ";
180 if (IsExplicit)
181 OS << "explicit ";
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000182 OS << "module " << Name;
Douglas Gregor51f564f2011-12-31 04:05:44 +0000183
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000184 if (IsSystem) {
185 OS.indent(Indent + 2);
186 OS << " [system]";
187 }
188
189 OS << " {\n";
190
Douglas Gregor51f564f2011-12-31 04:05:44 +0000191 if (!Requires.empty()) {
192 OS.indent(Indent + 2);
193 OS << "requires ";
194 for (unsigned I = 0, N = Requires.size(); I != N; ++I) {
195 if (I)
196 OS << ", ";
197 OS << Requires[I];
198 }
199 OS << "\n";
200 }
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000201
Douglas Gregor10694ce2011-12-08 17:39:04 +0000202 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000203 OS.indent(Indent + 2);
Douglas Gregor489ad432011-12-08 18:00:48 +0000204 OS << "umbrella header \"";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000205 OS.write_escaped(UmbrellaHeader->getName());
206 OS << "\"\n";
Douglas Gregor489ad432011-12-08 18:00:48 +0000207 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
208 OS.indent(Indent + 2);
209 OS << "umbrella \"";
210 OS.write_escaped(UmbrellaDir->getName());
211 OS << "\"\n";
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000212 }
213
214 for (unsigned I = 0, N = Headers.size(); I != N; ++I) {
215 OS.indent(Indent + 2);
216 OS << "header \"";
217 OS.write_escaped(Headers[I]->getName());
218 OS << "\"\n";
219 }
220
Douglas Gregorb7a78192012-01-04 23:32:19 +0000221 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000222 MI != MIEnd; ++MI)
Douglas Gregorb7a78192012-01-04 23:32:19 +0000223 (*MI)->print(OS, Indent + 2);
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000224
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000225 for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
226 OS.indent(Indent + 2);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000227 OS << "export ";
228 if (Module *Restriction = Exports[I].getPointer()) {
229 OS << Restriction->getFullModuleName();
230 if (Exports[I].getInt())
231 OS << ".*";
232 } else {
233 OS << "*";
234 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000235 OS << "\n";
236 }
237
238 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
239 OS.indent(Indent + 2);
240 OS << "export ";
241 printModuleId(OS, UnresolvedExports[I].Id);
Douglas Gregorf4ac17e2011-12-05 17:34:59 +0000242 if (UnresolvedExports[I].Wildcard) {
243 if (UnresolvedExports[I].Id.empty())
244 OS << "*";
245 else
246 OS << ".*";
247 }
Douglas Gregoraf13bfc2011-12-02 18:58:38 +0000248 OS << "\n";
249 }
250
Douglas Gregor1e123682011-12-05 22:27:44 +0000251 if (InferSubmodules) {
252 OS.indent(Indent + 2);
253 if (InferExplicitSubmodules)
254 OS << "explicit ";
255 OS << "module * {\n";
256 if (InferExportWildcard) {
257 OS.indent(Indent + 4);
258 OS << "export *\n";
259 }
260 OS.indent(Indent + 2);
261 OS << "}\n";
262 }
263
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000264 OS.indent(Indent);
265 OS << "}\n";
266}
267
268void Module::dump() const {
269 print(llvm::errs());
270}
271
272