Module.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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), Directory(),
  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 Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
  67. Requirement &Req,
  68. UnresolvedHeaderDirective &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 || IsExternC) {
  249. OS.indent(Indent + 2);
  250. if (IsSystem)
  251. OS << " [system]";
  252. if (IsExternC)
  253. OS << " [extern_c]";
  254. }
  255. OS << " {\n";
  256. if (!Requirements.empty()) {
  257. OS.indent(Indent + 2);
  258. OS << "requires ";
  259. for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
  260. if (I)
  261. OS << ", ";
  262. if (!Requirements[I].second)
  263. OS << "!";
  264. OS << Requirements[I].first;
  265. }
  266. OS << "\n";
  267. }
  268. if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
  269. OS.indent(Indent + 2);
  270. OS << "umbrella header \"";
  271. OS.write_escaped(UmbrellaHeader->getName());
  272. OS << "\"\n";
  273. } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
  274. OS.indent(Indent + 2);
  275. OS << "umbrella \"";
  276. OS.write_escaped(UmbrellaDir->getName());
  277. OS << "\"\n";
  278. }
  279. if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
  280. OS.indent(Indent + 2);
  281. OS << "config_macros ";
  282. if (ConfigMacrosExhaustive)
  283. OS << "[exhaustive]";
  284. for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
  285. if (I)
  286. OS << ", ";
  287. OS << ConfigMacros[I];
  288. }
  289. OS << "\n";
  290. }
  291. struct {
  292. StringRef Prefix;
  293. HeaderKind Kind;
  294. } Kinds[] = {{"", HK_Normal},
  295. {"textual ", HK_Textual},
  296. {"private ", HK_Private},
  297. {"private textual ", HK_PrivateTextual},
  298. {"exclude ", HK_Excluded}};
  299. for (auto &K : Kinds) {
  300. for (auto &H : Headers[K.Kind]) {
  301. OS.indent(Indent + 2);
  302. OS << K.Prefix << "header \"";
  303. OS.write_escaped(H.NameAsWritten);
  304. OS << "\"\n";
  305. }
  306. }
  307. for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
  308. MI != MIEnd; ++MI)
  309. // Print inferred subframework modules so that we don't need to re-infer
  310. // them (requires expensive directory iteration + stat calls) when we build
  311. // the module. Regular inferred submodules are OK, as we need to look at all
  312. // those header files anyway.
  313. if (!(*MI)->IsInferred || (*MI)->IsFramework)
  314. (*MI)->print(OS, Indent + 2);
  315. for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
  316. OS.indent(Indent + 2);
  317. OS << "export ";
  318. if (Module *Restriction = Exports[I].getPointer()) {
  319. OS << Restriction->getFullModuleName();
  320. if (Exports[I].getInt())
  321. OS << ".*";
  322. } else {
  323. OS << "*";
  324. }
  325. OS << "\n";
  326. }
  327. for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
  328. OS.indent(Indent + 2);
  329. OS << "export ";
  330. printModuleId(OS, UnresolvedExports[I].Id);
  331. if (UnresolvedExports[I].Wildcard) {
  332. if (UnresolvedExports[I].Id.empty())
  333. OS << "*";
  334. else
  335. OS << ".*";
  336. }
  337. OS << "\n";
  338. }
  339. for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
  340. OS.indent(Indent + 2);
  341. OS << "use ";
  342. OS << DirectUses[I]->getFullModuleName();
  343. OS << "\n";
  344. }
  345. for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
  346. OS.indent(Indent + 2);
  347. OS << "use ";
  348. printModuleId(OS, UnresolvedDirectUses[I]);
  349. OS << "\n";
  350. }
  351. for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
  352. OS.indent(Indent + 2);
  353. OS << "link ";
  354. if (LinkLibraries[I].IsFramework)
  355. OS << "framework ";
  356. OS << "\"";
  357. OS.write_escaped(LinkLibraries[I].Library);
  358. OS << "\"";
  359. }
  360. for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
  361. OS.indent(Indent + 2);
  362. OS << "conflict ";
  363. printModuleId(OS, UnresolvedConflicts[I].Id);
  364. OS << ", \"";
  365. OS.write_escaped(UnresolvedConflicts[I].Message);
  366. OS << "\"\n";
  367. }
  368. for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
  369. OS.indent(Indent + 2);
  370. OS << "conflict ";
  371. OS << Conflicts[I].Other->getFullModuleName();
  372. OS << ", \"";
  373. OS.write_escaped(Conflicts[I].Message);
  374. OS << "\"\n";
  375. }
  376. if (InferSubmodules) {
  377. OS.indent(Indent + 2);
  378. if (InferExplicitSubmodules)
  379. OS << "explicit ";
  380. OS << "module * {\n";
  381. if (InferExportWildcard) {
  382. OS.indent(Indent + 4);
  383. OS << "export *\n";
  384. }
  385. OS.indent(Indent + 2);
  386. OS << "}\n";
  387. }
  388. OS.indent(Indent);
  389. OS << "}\n";
  390. }
  391. void Module::dump() const {
  392. print(llvm::errs());
  393. }