Module.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. const FileEntry *File, bool IsFramework, bool IsExplicit)
  26. : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), ModuleMap(File),
  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(const 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. IsMissingRequirement = true;
  138. markUnavailable();
  139. }
  140. void Module::markUnavailable() {
  141. if (!IsAvailable)
  142. return;
  143. SmallVector<Module *, 2> Stack;
  144. Stack.push_back(this);
  145. while (!Stack.empty()) {
  146. Module *Current = Stack.back();
  147. Stack.pop_back();
  148. if (!Current->IsAvailable)
  149. continue;
  150. Current->IsAvailable = false;
  151. for (submodule_iterator Sub = Current->submodule_begin(),
  152. SubEnd = Current->submodule_end();
  153. Sub != SubEnd; ++Sub) {
  154. if ((*Sub)->IsAvailable)
  155. Stack.push_back(*Sub);
  156. }
  157. }
  158. }
  159. Module *Module::findSubmodule(StringRef Name) const {
  160. llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
  161. if (Pos == SubModuleIndex.end())
  162. return 0;
  163. return SubModules[Pos->getValue()];
  164. }
  165. static void printModuleId(raw_ostream &OS, const ModuleId &Id) {
  166. for (unsigned I = 0, N = Id.size(); I != N; ++I) {
  167. if (I)
  168. OS << ".";
  169. OS << Id[I].first;
  170. }
  171. }
  172. void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
  173. // All non-explicit submodules are exported.
  174. for (std::vector<Module *>::const_iterator I = SubModules.begin(),
  175. E = SubModules.end();
  176. I != E; ++I) {
  177. Module *Mod = *I;
  178. if (!Mod->IsExplicit)
  179. Exported.push_back(Mod);
  180. }
  181. // Find re-exported modules by filtering the list of imported modules.
  182. bool AnyWildcard = false;
  183. bool UnrestrictedWildcard = false;
  184. SmallVector<Module *, 4> WildcardRestrictions;
  185. for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
  186. Module *Mod = Exports[I].getPointer();
  187. if (!Exports[I].getInt()) {
  188. // Export a named module directly; no wildcards involved.
  189. Exported.push_back(Mod);
  190. continue;
  191. }
  192. // Wildcard export: export all of the imported modules that match
  193. // the given pattern.
  194. AnyWildcard = true;
  195. if (UnrestrictedWildcard)
  196. continue;
  197. if (Module *Restriction = Exports[I].getPointer())
  198. WildcardRestrictions.push_back(Restriction);
  199. else {
  200. WildcardRestrictions.clear();
  201. UnrestrictedWildcard = true;
  202. }
  203. }
  204. // If there were any wildcards, push any imported modules that were
  205. // re-exported by the wildcard restriction.
  206. if (!AnyWildcard)
  207. return;
  208. for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
  209. Module *Mod = Imports[I];
  210. bool Acceptable = UnrestrictedWildcard;
  211. if (!Acceptable) {
  212. // Check whether this module meets one of the restrictions.
  213. for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
  214. Module *Restriction = WildcardRestrictions[R];
  215. if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
  216. Acceptable = true;
  217. break;
  218. }
  219. }
  220. }
  221. if (!Acceptable)
  222. continue;
  223. Exported.push_back(Mod);
  224. }
  225. }
  226. void Module::buildVisibleModulesCache() const {
  227. assert(VisibleModulesCache.empty() && "cache does not need building");
  228. // This module is visible to itself.
  229. VisibleModulesCache.insert(this);
  230. // Every imported module is visible.
  231. SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
  232. while (!Stack.empty()) {
  233. Module *CurrModule = Stack.pop_back_val();
  234. // Every module transitively exported by an imported module is visible.
  235. if (VisibleModulesCache.insert(CurrModule).second)
  236. CurrModule->getExportedModules(Stack);
  237. }
  238. }
  239. void Module::print(raw_ostream &OS, unsigned Indent) const {
  240. OS.indent(Indent);
  241. if (IsFramework)
  242. OS << "framework ";
  243. if (IsExplicit)
  244. OS << "explicit ";
  245. OS << "module " << Name;
  246. if (IsSystem) {
  247. OS.indent(Indent + 2);
  248. OS << " [system]";
  249. }
  250. OS << " {\n";
  251. if (!Requirements.empty()) {
  252. OS.indent(Indent + 2);
  253. OS << "requires ";
  254. for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
  255. if (I)
  256. OS << ", ";
  257. if (!Requirements[I].second)
  258. OS << "!";
  259. OS << Requirements[I].first;
  260. }
  261. OS << "\n";
  262. }
  263. if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) {
  264. OS.indent(Indent + 2);
  265. OS << "umbrella header \"";
  266. OS.write_escaped(UmbrellaHeader->getName());
  267. OS << "\"\n";
  268. } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) {
  269. OS.indent(Indent + 2);
  270. OS << "umbrella \"";
  271. OS.write_escaped(UmbrellaDir->getName());
  272. OS << "\"\n";
  273. }
  274. if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
  275. OS.indent(Indent + 2);
  276. OS << "config_macros ";
  277. if (ConfigMacrosExhaustive)
  278. OS << "[exhaustive]";
  279. for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
  280. if (I)
  281. OS << ", ";
  282. OS << ConfigMacros[I];
  283. }
  284. OS << "\n";
  285. }
  286. for (unsigned I = 0, N = NormalHeaders.size(); I != N; ++I) {
  287. OS.indent(Indent + 2);
  288. OS << "header \"";
  289. OS.write_escaped(NormalHeaders[I]->getName());
  290. OS << "\"\n";
  291. }
  292. for (unsigned I = 0, N = ExcludedHeaders.size(); I != N; ++I) {
  293. OS.indent(Indent + 2);
  294. OS << "exclude header \"";
  295. OS.write_escaped(ExcludedHeaders[I]->getName());
  296. OS << "\"\n";
  297. }
  298. for (unsigned I = 0, N = PrivateHeaders.size(); I != N; ++I) {
  299. OS.indent(Indent + 2);
  300. OS << "private header \"";
  301. OS.write_escaped(PrivateHeaders[I]->getName());
  302. OS << "\"\n";
  303. }
  304. for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end();
  305. MI != MIEnd; ++MI)
  306. (*MI)->print(OS, Indent + 2);
  307. for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
  308. OS.indent(Indent + 2);
  309. OS << "export ";
  310. if (Module *Restriction = Exports[I].getPointer()) {
  311. OS << Restriction->getFullModuleName();
  312. if (Exports[I].getInt())
  313. OS << ".*";
  314. } else {
  315. OS << "*";
  316. }
  317. OS << "\n";
  318. }
  319. for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
  320. OS.indent(Indent + 2);
  321. OS << "export ";
  322. printModuleId(OS, UnresolvedExports[I].Id);
  323. if (UnresolvedExports[I].Wildcard) {
  324. if (UnresolvedExports[I].Id.empty())
  325. OS << "*";
  326. else
  327. OS << ".*";
  328. }
  329. OS << "\n";
  330. }
  331. for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
  332. OS.indent(Indent + 2);
  333. OS << "use ";
  334. OS << DirectUses[I]->getFullModuleName();
  335. OS << "\n";
  336. }
  337. for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
  338. OS.indent(Indent + 2);
  339. OS << "use ";
  340. printModuleId(OS, UnresolvedDirectUses[I]);
  341. OS << "\n";
  342. }
  343. for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
  344. OS.indent(Indent + 2);
  345. OS << "link ";
  346. if (LinkLibraries[I].IsFramework)
  347. OS << "framework ";
  348. OS << "\"";
  349. OS.write_escaped(LinkLibraries[I].Library);
  350. OS << "\"";
  351. }
  352. for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
  353. OS.indent(Indent + 2);
  354. OS << "conflict ";
  355. printModuleId(OS, UnresolvedConflicts[I].Id);
  356. OS << ", \"";
  357. OS.write_escaped(UnresolvedConflicts[I].Message);
  358. OS << "\"\n";
  359. }
  360. for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
  361. OS.indent(Indent + 2);
  362. OS << "conflict ";
  363. OS << Conflicts[I].Other->getFullModuleName();
  364. OS << ", \"";
  365. OS.write_escaped(Conflicts[I].Message);
  366. OS << "\"\n";
  367. }
  368. if (InferSubmodules) {
  369. OS.indent(Indent + 2);
  370. if (InferExplicitSubmodules)
  371. OS << "explicit ";
  372. OS << "module * {\n";
  373. if (InferExportWildcard) {
  374. OS.indent(Indent + 4);
  375. OS << "export *\n";
  376. }
  377. OS.indent(Indent + 2);
  378. OS << "}\n";
  379. }
  380. OS.indent(Indent);
  381. OS << "}\n";
  382. }
  383. void Module::dump() const {
  384. print(llvm::errs());
  385. }