Module.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
  25. bool IsFramework, bool IsExplicit)
  26. : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
  27. Umbrella(), ASTFile(nullptr), IsMissingRequirement(false),
  28. IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework),
  29. IsExplicit(IsExplicit), IsSystem(false), IsExternC(false),
  30. IsInferred(false), InferSubmodules(false), InferExplicitSubmodules(false),
  31. InferExportWildcard(false), ConfigMacrosExhaustive(false),
  32. NameVisibility(Hidden) {
  33. if (Parent) {
  34. if (!Parent->isAvailable())
  35. IsAvailable = false;
  36. if (Parent->IsSystem)
  37. IsSystem = true;
  38. if (Parent->IsExternC)
  39. IsExternC = true;
  40. IsMissingRequirement = Parent->IsMissingRequirement;
  41. Parent->SubModuleIndex[Name] = Parent->SubModules.size();
  42. Parent->SubModules.push_back(this);
  43. }
  44. }
  45. Module::~Module() {
  46. for (submodule_iterator I = submodule_begin(), IEnd = submodule_end();
  47. I != IEnd; ++I) {
  48. delete *I;
  49. }
  50. }
  51. /// \brief Determine whether a translation unit built using the current
  52. /// language options has the given feature.
  53. static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
  54. const TargetInfo &Target) {
  55. return llvm::StringSwitch<bool>(Feature)
  56. .Case("altivec", LangOpts.AltiVec)
  57. .Case("blocks", LangOpts.Blocks)
  58. .Case("cplusplus", LangOpts.CPlusPlus)
  59. .Case("cplusplus11", LangOpts.CPlusPlus11)
  60. .Case("objc", LangOpts.ObjC1)
  61. .Case("objc_arc", LangOpts.ObjCAutoRefCount)
  62. .Case("opencl", LangOpts.OpenCL)
  63. .Case("tls", Target.isTLSSupported())
  64. .Default(Target.hasFeature(Feature));
  65. }
  66. bool
  67. Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
  68. Requirement &Req, HeaderDirective &MissingHeader) const {
  69. if (IsAvailable)
  70. return true;
  71. for (const Module *Current = this; Current; Current = Current->Parent) {
  72. if (!Current->MissingHeaders.empty()) {
  73. MissingHeader = Current->MissingHeaders.front();
  74. return false;
  75. }
  76. for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
  77. if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
  78. Current->Requirements[I].second) {
  79. Req = Current->Requirements[I];
  80. return false;
  81. }
  82. }
  83. }
  84. llvm_unreachable("could not find a reason why module is unavailable");
  85. }
  86. bool Module::isSubModuleOf(const Module *Other) const {
  87. const Module *This = this;
  88. do {
  89. if (This == Other)
  90. return true;
  91. This = This->Parent;
  92. } while (This);
  93. return false;
  94. }
  95. const Module *Module::getTopLevelModule() const {
  96. const Module *Result = this;
  97. while (Result->Parent)
  98. Result = Result->Parent;
  99. return Result;
  100. }
  101. std::string Module::getFullModuleName() const {
  102. SmallVector<StringRef, 2> Names;
  103. // Build up the set of module names (from innermost to outermost).
  104. for (const Module *M = this; M; M = M->Parent)
  105. Names.push_back(M->Name);
  106. std::string Result;
  107. for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(),
  108. IEnd = Names.rend();
  109. I != IEnd; ++I) {
  110. if (!Result.empty())
  111. Result += '.';
  112. Result += *I;
  113. }
  114. return Result;
  115. }
  116. const DirectoryEntry *Module::getUmbrellaDir() const {
  117. if (const FileEntry *Header = getUmbrellaHeader())
  118. return Header->getDir();
  119. return Umbrella.dyn_cast<const DirectoryEntry *>();
  120. }
  121. ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
  122. if (!TopHeaderNames.empty()) {
  123. for (std::vector<std::string>::iterator
  124. I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
  125. if (const FileEntry *FE = FileMgr.getFile(*I))
  126. TopHeaders.insert(FE);
  127. }
  128. TopHeaderNames.clear();
  129. }
  130. return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end());
  131. }
  132. void Module::addRequirement(StringRef Feature, bool RequiredState,
  133. const LangOptions &LangOpts,
  134. const TargetInfo &Target) {
  135. Requirements.push_back(Requirement(Feature, RequiredState));
  136. // If this feature is currently available, we're done.
  137. if (hasFeature(Feature, LangOpts, Target) == RequiredState)
  138. return;
  139. markUnavailable(/*MissingRequirement*/true);
  140. }
  141. void Module::markUnavailable(bool MissingRequirement) {
  142. if (!IsAvailable)
  143. return;
  144. SmallVector<Module *, 2> Stack;
  145. Stack.push_back(this);
  146. while (!Stack.empty()) {
  147. Module *Current = Stack.back();
  148. Stack.pop_back();
  149. if (!Current->IsAvailable)
  150. continue;
  151. Current->IsAvailable = false;
  152. Current->IsMissingRequirement |= MissingRequirement;
  153. for (submodule_iterator Sub = Current->submodule_begin(),
  154. SubEnd = Current->submodule_end();
  155. Sub != SubEnd; ++Sub) {
  156. if ((*Sub)->IsAvailable)
  157. Stack.push_back(*Sub);
  158. }
  159. }
  160. }
  161. Module *Module::findSubmodule(StringRef Name) const {
  162. llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
  163. if (Pos == SubModuleIndex.end())
  164. return nullptr;
  165. return SubModules[Pos->getValue()];
  166. }
  167. static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
  168. for (unsigned I = 0, N = Id.size(); I != N; ++I) {
  169. if (I)
  170. OS << ".";
  171. OS << Id[I].first;
  172. }
  173. }
  174. void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
  175. // All non-explicit submodules are exported.
  176. for (std::vector<Module *>::const_iterator I = SubModules.begin(),
  177. E = SubModules.end();
  178. I != E; ++I) {
  179. Module *Mod = *I;
  180. if (!Mod->IsExplicit)
  181. Exported.push_back(Mod);
  182. }
  183. // Find re-exported modules by filtering the list of imported modules.
  184. bool AnyWildcard = false;
  185. bool UnrestrictedWildcard = false;
  186. SmallVector<Module *, 4> WildcardRestrictions;
  187. for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
  188. Module *Mod = Exports[I].getPointer();
  189. if (!Exports[I].getInt()) {
  190. // Export a named module directly; no wildcards involved.
  191. Exported.push_back(Mod);
  192. continue;
  193. }
  194. // Wildcard export: export all of the imported modules that match
  195. // the given pattern.
  196. AnyWildcard = true;
  197. if (UnrestrictedWildcard)
  198. continue;
  199. if (Module *Restriction = Exports[I].getPointer())
  200. WildcardRestrictions.push_back(Restriction);
  201. else {
  202. WildcardRestrictions.clear();
  203. UnrestrictedWildcard = true;
  204. }
  205. }
  206. // If there were any wildcards, push any imported modules that were
  207. // re-exported by the wildcard restriction.
  208. if (!AnyWildcard)
  209. return;
  210. for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
  211. Module *Mod = Imports[I];
  212. bool Acceptable = UnrestrictedWildcard;
  213. if (!Acceptable) {
  214. // Check whether this module meets one of the restrictions.
  215. for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
  216. Module *Restriction = WildcardRestrictions[R];
  217. if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
  218. Acceptable = true;
  219. break;
  220. }
  221. }
  222. }
  223. if (!Acceptable)
  224. continue;
  225. Exported.push_back(Mod);
  226. }
  227. }
  228. void Module::buildVisibleModulesCache() const {
  229. assert(VisibleModulesCache.empty() && "cache does not need building");
  230. // This module is visible to itself.
  231. VisibleModulesCache.insert(this);
  232. // Every imported module is visible.
  233. SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
  234. while (!Stack.empty()) {
  235. Module *CurrModule = Stack.pop_back_val();
  236. // Every module transitively exported by an imported module is visible.
  237. if (VisibleModulesCache.insert(CurrModule).second)
  238. CurrModule->getExportedModules(Stack);
  239. }
  240. }
  241. void Module::print(raw_ostream &OS, unsigned Indent) const {
  242. OS.indent(Indent);
  243. if (IsFramework)
  244. OS << "framework ";
  245. if (IsExplicit)
  246. OS << "explicit ";
  247. OS << "module " << Name;
  248. if (IsSystem) {
  249. OS.indent(Indent + 2);
  250. OS << " [system]";
  251. }
  252. OS << " {\n";
  253. if (!Requirements.empty()) {
  254. OS.indent(Indent + 2);
  255. OS << "requires ";
  256. for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
  257. if (I)
  258. OS << ", ";
  259. if (!Requirements[I].second)
  260. OS << "!";
  261. OS << Requirements[I].first;
  262. }
  263. OS << "\n";
  264. }
  265. if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
  266. OS.indent(Indent + 2);
  267. OS << "umbrella header \"";
  268. OS.write_escaped(UmbrellaHeader->getName());
  269. OS << "\"\n";
  270. } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
  271. OS.indent(Indent + 2);
  272. OS << "umbrella \"";
  273. OS.write_escaped(UmbrellaDir->getName());
  274. OS << "\"\n";
  275. }
  276. if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
  277. OS.indent(Indent + 2);
  278. OS << "config_macros ";
  279. if (ConfigMacrosExhaustive)
  280. OS << "[exhaustive]";
  281. for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
  282. if (I)
  283. OS << ", ";
  284. OS << ConfigMacros[I];
  285. }
  286. OS << "\n";
  287. }
  288. struct HeaderKind {
  289. StringRef Prefix;
  290. const SmallVectorImpl<const FileEntry *> &Headers;
  291. } Kinds[] = {{"", NormalHeaders},
  292. {"exclude ", ExcludedHeaders},
  293. {"textual ", TextualHeaders},
  294. {"private ", PrivateHeaders}};
  295. for (auto &K : Kinds) {
  296. for (auto *H : K.Headers) {
  297. OS.indent(Indent + 2);
  298. OS << K.Prefix << "header \"";
  299. OS.write_escaped(H->getName());
  300. OS << "\"\n";
  301. }
  302. }
  303. for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
  304. MI != MIEnd; ++MI)
  305. // Print inferred subframework modules so that we don't need to re-infer
  306. // them (requires expensive directory iteration + stat calls) when we build
  307. // the module. Regular inferred submodules are OK, as we need to look at all
  308. // those header files anyway.
  309. if (!(*MI)->IsInferred || (*MI)->IsFramework)
  310. (*MI)->print(OS, Indent + 2);
  311. for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
  312. OS.indent(Indent + 2);
  313. OS << "export ";
  314. if (Module *Restriction = Exports[I].getPointer()) {
  315. OS << Restriction->getFullModuleName();
  316. if (Exports[I].getInt())
  317. OS << ".*";
  318. } else {
  319. OS << "*";
  320. }
  321. OS << "\n";
  322. }
  323. for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
  324. OS.indent(Indent + 2);
  325. OS << "export ";
  326. printModuleId(OS, UnresolvedExports[I].Id);
  327. if (UnresolvedExports[I].Wildcard) {
  328. if (UnresolvedExports[I].Id.empty())
  329. OS << "*";
  330. else
  331. OS << ".*";
  332. }
  333. OS << "\n";
  334. }
  335. for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
  336. OS.indent(Indent + 2);
  337. OS << "use ";
  338. OS << DirectUses[I]->getFullModuleName();
  339. OS << "\n";
  340. }
  341. for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
  342. OS.indent(Indent + 2);
  343. OS << "use ";
  344. printModuleId(OS, UnresolvedDirectUses[I]);
  345. OS << "\n";
  346. }
  347. for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
  348. OS.indent(Indent + 2);
  349. OS << "link ";
  350. if (LinkLibraries[I].IsFramework)
  351. OS << "framework ";
  352. OS << "\"";
  353. OS.write_escaped(LinkLibraries[I].Library);
  354. OS << "\"";
  355. }
  356. for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
  357. OS.indent(Indent + 2);
  358. OS << "conflict ";
  359. printModuleId(OS, UnresolvedConflicts[I].Id);
  360. OS << ", \"";
  361. OS.write_escaped(UnresolvedConflicts[I].Message);
  362. OS << "\"\n";
  363. }
  364. for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
  365. OS.indent(Indent + 2);
  366. OS << "conflict ";
  367. OS << Conflicts[I].Other->getFullModuleName();
  368. OS << ", \"";
  369. OS.write_escaped(Conflicts[I].Message);
  370. OS << "\"\n";
  371. }
  372. if (InferSubmodules) {
  373. OS.indent(Indent + 2);
  374. if (InferExplicitSubmodules)
  375. OS << "explicit ";
  376. OS << "module * {\n";
  377. if (InferExportWildcard) {
  378. OS.indent(Indent + 4);
  379. OS << "export *\n";
  380. }
  381. OS.indent(Indent + 2);
  382. OS << "}\n";
  383. }
  384. OS.indent(Indent);
  385. OS << "}\n";
  386. }
  387. void Module::dump() const {
  388. print(llvm::errs());
  389. }