Module.cpp 12 KB

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