Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 1 | //===--- Module.cpp - Describe a module -----------------------------------===// |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 14 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Module.h" |
| 16 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | 0070c0b | 2012-01-30 06:38:25 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/ArrayRef.h" |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/ADT/StringSwitch.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 24 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Richard Smith | 7794486 | 2014-03-02 05:58:18 +0000 | [diff] [blame] | 27 | Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 28 | bool IsFramework, bool IsExplicit, unsigned VisibilityID) |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 29 | : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), Directory(), |
Adrian Prantl | 15bcf70 | 2015-06-30 17:39:43 +0000 | [diff] [blame] | 30 | Umbrella(), Signature(0), ASTFile(nullptr), VisibilityID(VisibilityID), |
Richard Smith | 8a308ec | 2015-11-05 00:54:55 +0000 | [diff] [blame] | 31 | IsMissingRequirement(false), HasIncompatibleModuleFile(false), |
| 32 | IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework), |
| 33 | IsExplicit(IsExplicit), IsSystem(false), IsExternC(false), |
| 34 | IsInferred(false), InferSubmodules(false), InferExplicitSubmodules(false), |
| 35 | InferExportWildcard(false), ConfigMacrosExhaustive(false), |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 36 | NoUndeclaredIncludes(false), NameVisibility(Hidden) { |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 37 | if (Parent) { |
| 38 | if (!Parent->isAvailable()) |
| 39 | IsAvailable = false; |
Douglas Gregor | 3ec6663 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 40 | if (Parent->IsSystem) |
| 41 | IsSystem = true; |
Richard Smith | 9bca298 | 2014-03-08 00:03:56 +0000 | [diff] [blame] | 42 | if (Parent->IsExternC) |
| 43 | IsExternC = true; |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 44 | if (Parent->NoUndeclaredIncludes) |
| 45 | NoUndeclaredIncludes = true; |
Ben Langmuir | 993055f | 2014-04-18 23:51:00 +0000 | [diff] [blame] | 46 | IsMissingRequirement = Parent->IsMissingRequirement; |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 47 | |
| 48 | Parent->SubModuleIndex[Name] = Parent->SubModules.size(); |
| 49 | Parent->SubModules.push_back(this); |
| 50 | } |
| 51 | } |
| 52 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 53 | Module::~Module() { |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 54 | for (submodule_iterator I = submodule_begin(), IEnd = submodule_end(); |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 55 | I != IEnd; ++I) { |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 56 | delete *I; |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 57 | } |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 60 | /// \brief Determine whether a translation unit built using the current |
| 61 | /// language options has the given feature. |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 62 | static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, |
| 63 | const TargetInfo &Target) { |
Ben Langmuir | 532d210 | 2015-02-02 21:56:15 +0000 | [diff] [blame] | 64 | bool HasFeature = llvm::StringSwitch<bool>(Feature) |
| 65 | .Case("altivec", LangOpts.AltiVec) |
| 66 | .Case("blocks", LangOpts.Blocks) |
| 67 | .Case("cplusplus", LangOpts.CPlusPlus) |
| 68 | .Case("cplusplus11", LangOpts.CPlusPlus11) |
Elad Cohen | fb6358d | 2016-09-04 06:00:42 +0000 | [diff] [blame] | 69 | .Case("freestanding", LangOpts.Freestanding) |
Bruno Cardoso Lopes | 6736e19 | 2016-08-30 21:25:42 +0000 | [diff] [blame] | 70 | .Case("gnuinlineasm", LangOpts.GNUAsm) |
Ben Langmuir | 532d210 | 2015-02-02 21:56:15 +0000 | [diff] [blame] | 71 | .Case("objc", LangOpts.ObjC1) |
| 72 | .Case("objc_arc", LangOpts.ObjCAutoRefCount) |
| 73 | .Case("opencl", LangOpts.OpenCL) |
| 74 | .Case("tls", Target.isTLSSupported()) |
Ulrich Weigand | 3c5038a | 2015-07-30 14:08:36 +0000 | [diff] [blame] | 75 | .Case("zvector", LangOpts.ZVector) |
Ben Langmuir | 532d210 | 2015-02-02 21:56:15 +0000 | [diff] [blame] | 76 | .Default(Target.hasFeature(Feature)); |
| 77 | if (!HasFeature) |
| 78 | HasFeature = std::find(LangOpts.ModuleFeatures.begin(), |
| 79 | LangOpts.ModuleFeatures.end(), |
| 80 | Feature) != LangOpts.ModuleFeatures.end(); |
| 81 | return HasFeature; |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 84 | bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target, |
| 85 | Requirement &Req, |
| 86 | UnresolvedHeaderDirective &MissingHeader) const { |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 87 | if (IsAvailable) |
| 88 | return true; |
| 89 | |
| 90 | for (const Module *Current = this; Current; Current = Current->Parent) { |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 91 | for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) { |
| 92 | if (hasFeature(Current->Requirements[I].first, LangOpts, Target) != |
| 93 | Current->Requirements[I].second) { |
| 94 | Req = Current->Requirements[I]; |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | } |
Ben Langmuir | 75a7e43 | 2015-07-13 19:48:52 +0000 | [diff] [blame] | 98 | if (!Current->MissingHeaders.empty()) { |
| 99 | MissingHeader = Current->MissingHeaders.front(); |
| 100 | return false; |
| 101 | } |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | llvm_unreachable("could not find a reason why module is unavailable"); |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Dmitri Gribenko | 62bcd92 | 2014-04-18 14:36:51 +0000 | [diff] [blame] | 107 | bool Module::isSubModuleOf(const Module *Other) const { |
Douglas Gregor | f5eedd0 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 108 | const Module *This = this; |
| 109 | do { |
| 110 | if (This == Other) |
| 111 | return true; |
| 112 | |
| 113 | This = This->Parent; |
| 114 | } while (This); |
| 115 | |
| 116 | return false; |
| 117 | } |
| 118 | |
Douglas Gregor | 7344109 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 119 | const Module *Module::getTopLevelModule() const { |
| 120 | const Module *Result = this; |
| 121 | while (Result->Parent) |
| 122 | Result = Result->Parent; |
| 123 | |
| 124 | return Result; |
| 125 | } |
| 126 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 127 | std::string Module::getFullModuleName() const { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 128 | SmallVector<StringRef, 2> Names; |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 129 | |
| 130 | // Build up the set of module names (from innermost to outermost). |
| 131 | for (const Module *M = this; M; M = M->Parent) |
| 132 | Names.push_back(M->Name); |
| 133 | |
| 134 | std::string Result; |
Craig Topper | 61ac906 | 2013-07-08 03:55:09 +0000 | [diff] [blame] | 135 | for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(), |
| 136 | IEnd = Names.rend(); |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 137 | I != IEnd; ++I) { |
| 138 | if (!Result.empty()) |
| 139 | Result += '.'; |
| 140 | |
| 141 | Result += *I; |
| 142 | } |
| 143 | |
| 144 | return Result; |
| 145 | } |
| 146 | |
Ben Langmuir | 7ff2914 | 2015-08-13 17:13:33 +0000 | [diff] [blame] | 147 | bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const { |
| 148 | for (const Module *M = this; M; M = M->Parent) { |
| 149 | if (nameParts.empty() || M->Name != nameParts.back()) |
| 150 | return false; |
| 151 | nameParts = nameParts.drop_back(); |
| 152 | } |
| 153 | return nameParts.empty(); |
| 154 | } |
| 155 | |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 156 | Module::DirectoryName Module::getUmbrellaDir() const { |
| 157 | if (Header U = getUmbrellaHeader()) |
| 158 | return {"", U.Entry->getDir()}; |
Douglas Gregor | 73141fa | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 159 | |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 160 | return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()}; |
Douglas Gregor | 73141fa | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Argyrios Kyrtzidis | 3c5305c | 2013-03-13 21:13:43 +0000 | [diff] [blame] | 163 | ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) { |
| 164 | if (!TopHeaderNames.empty()) { |
| 165 | for (std::vector<std::string>::iterator |
| 166 | I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) { |
| 167 | if (const FileEntry *FE = FileMgr.getFile(*I)) |
| 168 | TopHeaders.insert(FE); |
| 169 | } |
| 170 | TopHeaderNames.clear(); |
| 171 | } |
| 172 | |
| 173 | return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end()); |
| 174 | } |
| 175 | |
Richard Smith | 8f4d3ff | 2015-03-26 22:10:01 +0000 | [diff] [blame] | 176 | bool Module::directlyUses(const Module *Requested) const { |
| 177 | auto *Top = getTopLevelModule(); |
| 178 | |
| 179 | // A top-level module implicitly uses itself. |
| 180 | if (Requested->isSubModuleOf(Top)) |
| 181 | return true; |
| 182 | |
| 183 | for (auto *Use : Top->DirectUses) |
| 184 | if (Requested->isSubModuleOf(Use)) |
| 185 | return true; |
Bruno Cardoso Lopes | ed84df0 | 2016-10-21 01:41:56 +0000 | [diff] [blame] | 186 | |
| 187 | // Anyone is allowed to use our builtin stddef.h and its accompanying module. |
| 188 | if (!Requested->Parent && Requested->Name == "_Builtin_stddef_max_align_t") |
| 189 | return true; |
| 190 | |
Richard Smith | 8f4d3ff | 2015-03-26 22:10:01 +0000 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 194 | void Module::addRequirement(StringRef Feature, bool RequiredState, |
| 195 | const LangOptions &LangOpts, |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 196 | const TargetInfo &Target) { |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 197 | Requirements.push_back(Requirement(Feature, RequiredState)); |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 198 | |
| 199 | // If this feature is currently available, we're done. |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 200 | if (hasFeature(Feature, LangOpts, Target) == RequiredState) |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 201 | return; |
| 202 | |
Ben Langmuir | 993055f | 2014-04-18 23:51:00 +0000 | [diff] [blame] | 203 | markUnavailable(/*MissingRequirement*/true); |
Ben Langmuir | ec8c975 | 2014-04-18 22:07:31 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Ben Langmuir | 993055f | 2014-04-18 23:51:00 +0000 | [diff] [blame] | 206 | void Module::markUnavailable(bool MissingRequirement) { |
Ben Langmuir | 75a7e43 | 2015-07-13 19:48:52 +0000 | [diff] [blame] | 207 | auto needUpdate = [MissingRequirement](Module *M) { |
| 208 | return M->IsAvailable || (!M->IsMissingRequirement && MissingRequirement); |
| 209 | }; |
| 210 | |
| 211 | if (!needUpdate(this)) |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 212 | return; |
| 213 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 214 | SmallVector<Module *, 2> Stack; |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 215 | Stack.push_back(this); |
| 216 | while (!Stack.empty()) { |
| 217 | Module *Current = Stack.back(); |
| 218 | Stack.pop_back(); |
| 219 | |
Ben Langmuir | 75a7e43 | 2015-07-13 19:48:52 +0000 | [diff] [blame] | 220 | if (!needUpdate(Current)) |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 221 | continue; |
| 222 | |
| 223 | Current->IsAvailable = false; |
Ben Langmuir | 993055f | 2014-04-18 23:51:00 +0000 | [diff] [blame] | 224 | Current->IsMissingRequirement |= MissingRequirement; |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 225 | for (submodule_iterator Sub = Current->submodule_begin(), |
| 226 | SubEnd = Current->submodule_end(); |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 227 | Sub != SubEnd; ++Sub) { |
Ben Langmuir | 75a7e43 | 2015-07-13 19:48:52 +0000 | [diff] [blame] | 228 | if (needUpdate(*Sub)) |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 229 | Stack.push_back(*Sub); |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 234 | Module *Module::findSubmodule(StringRef Name) const { |
| 235 | llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); |
| 236 | if (Pos == SubModuleIndex.end()) |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 237 | return nullptr; |
| 238 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 239 | return SubModules[Pos->getValue()]; |
| 240 | } |
| 241 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 242 | static void printModuleId(raw_ostream &OS, const ModuleId &Id) { |
Douglas Gregor | 24bb923 | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 243 | for (unsigned I = 0, N = Id.size(); I != N; ++I) { |
| 244 | if (I) |
| 245 | OS << "."; |
| 246 | OS << Id[I].first; |
| 247 | } |
| 248 | } |
| 249 | |
Argyrios Kyrtzidis | 8739f7b | 2013-02-19 19:34:40 +0000 | [diff] [blame] | 250 | void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const { |
Dmitri Gribenko | e9bcf5b | 2013-11-04 21:51:33 +0000 | [diff] [blame] | 251 | // All non-explicit submodules are exported. |
| 252 | for (std::vector<Module *>::const_iterator I = SubModules.begin(), |
| 253 | E = SubModules.end(); |
| 254 | I != E; ++I) { |
| 255 | Module *Mod = *I; |
| 256 | if (!Mod->IsExplicit) |
| 257 | Exported.push_back(Mod); |
| 258 | } |
| 259 | |
| 260 | // Find re-exported modules by filtering the list of imported modules. |
Argyrios Kyrtzidis | 8739f7b | 2013-02-19 19:34:40 +0000 | [diff] [blame] | 261 | bool AnyWildcard = false; |
| 262 | bool UnrestrictedWildcard = false; |
| 263 | SmallVector<Module *, 4> WildcardRestrictions; |
| 264 | for (unsigned I = 0, N = Exports.size(); I != N; ++I) { |
| 265 | Module *Mod = Exports[I].getPointer(); |
| 266 | if (!Exports[I].getInt()) { |
| 267 | // Export a named module directly; no wildcards involved. |
| 268 | Exported.push_back(Mod); |
| 269 | |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | // Wildcard export: export all of the imported modules that match |
| 274 | // the given pattern. |
| 275 | AnyWildcard = true; |
| 276 | if (UnrestrictedWildcard) |
| 277 | continue; |
| 278 | |
| 279 | if (Module *Restriction = Exports[I].getPointer()) |
| 280 | WildcardRestrictions.push_back(Restriction); |
| 281 | else { |
| 282 | WildcardRestrictions.clear(); |
| 283 | UnrestrictedWildcard = true; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // If there were any wildcards, push any imported modules that were |
| 288 | // re-exported by the wildcard restriction. |
| 289 | if (!AnyWildcard) |
| 290 | return; |
| 291 | |
| 292 | for (unsigned I = 0, N = Imports.size(); I != N; ++I) { |
| 293 | Module *Mod = Imports[I]; |
| 294 | bool Acceptable = UnrestrictedWildcard; |
| 295 | if (!Acceptable) { |
| 296 | // Check whether this module meets one of the restrictions. |
| 297 | for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) { |
| 298 | Module *Restriction = WildcardRestrictions[R]; |
| 299 | if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) { |
| 300 | Acceptable = true; |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (!Acceptable) |
| 307 | continue; |
| 308 | |
| 309 | Exported.push_back(Mod); |
| 310 | } |
| 311 | } |
| 312 | |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 313 | void Module::buildVisibleModulesCache() const { |
| 314 | assert(VisibleModulesCache.empty() && "cache does not need building"); |
| 315 | |
| 316 | // This module is visible to itself. |
| 317 | VisibleModulesCache.insert(this); |
| 318 | |
Dmitri Gribenko | dc360d5 | 2013-10-31 22:24:10 +0000 | [diff] [blame] | 319 | // Every imported module is visible. |
Richard Smith | dde17e7 | 2013-11-01 02:19:14 +0000 | [diff] [blame] | 320 | SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end()); |
Dmitri Gribenko | dc360d5 | 2013-10-31 22:24:10 +0000 | [diff] [blame] | 321 | while (!Stack.empty()) { |
| 322 | Module *CurrModule = Stack.pop_back_val(); |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 323 | |
Richard Smith | dde17e7 | 2013-11-01 02:19:14 +0000 | [diff] [blame] | 324 | // Every module transitively exported by an imported module is visible. |
| 325 | if (VisibleModulesCache.insert(CurrModule).second) |
| 326 | CurrModule->getExportedModules(Stack); |
Richard Smith | 0e5d7b8 | 2013-07-25 23:08:39 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 330 | void Module::print(raw_ostream &OS, unsigned Indent) const { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 331 | OS.indent(Indent); |
| 332 | if (IsFramework) |
| 333 | OS << "framework "; |
| 334 | if (IsExplicit) |
| 335 | OS << "explicit "; |
Douglas Gregor | a686e1b | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 336 | OS << "module " << Name; |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 337 | |
Ben Langmuir | 7615f00 | 2015-01-13 17:47:38 +0000 | [diff] [blame] | 338 | if (IsSystem || IsExternC) { |
Douglas Gregor | a686e1b | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 339 | OS.indent(Indent + 2); |
Ben Langmuir | 7615f00 | 2015-01-13 17:47:38 +0000 | [diff] [blame] | 340 | if (IsSystem) |
| 341 | OS << " [system]"; |
| 342 | if (IsExternC) |
| 343 | OS << " [extern_c]"; |
Douglas Gregor | a686e1b | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | OS << " {\n"; |
| 347 | |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 348 | if (!Requirements.empty()) { |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 349 | OS.indent(Indent + 2); |
| 350 | OS << "requires "; |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 351 | for (unsigned I = 0, N = Requirements.size(); I != N; ++I) { |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 352 | if (I) |
| 353 | OS << ", "; |
Richard Smith | a3feee2 | 2013-10-28 22:18:19 +0000 | [diff] [blame] | 354 | if (!Requirements[I].second) |
| 355 | OS << "!"; |
| 356 | OS << Requirements[I].first; |
Douglas Gregor | 1fb5c3a | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 357 | } |
| 358 | OS << "\n"; |
| 359 | } |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 360 | |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 361 | if (Header H = getUmbrellaHeader()) { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 362 | OS.indent(Indent + 2); |
Douglas Gregor | 322f633 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 363 | OS << "umbrella header \""; |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 364 | OS.write_escaped(H.NameAsWritten); |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 365 | OS << "\"\n"; |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 366 | } else if (DirectoryName D = getUmbrellaDir()) { |
Douglas Gregor | 322f633 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 367 | OS.indent(Indent + 2); |
| 368 | OS << "umbrella \""; |
Richard Smith | 2b63d15 | 2015-05-16 02:28:53 +0000 | [diff] [blame] | 369 | OS.write_escaped(D.NameAsWritten); |
Douglas Gregor | 322f633 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 370 | OS << "\"\n"; |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 371 | } |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 372 | |
| 373 | if (!ConfigMacros.empty() || ConfigMacrosExhaustive) { |
| 374 | OS.indent(Indent + 2); |
| 375 | OS << "config_macros "; |
| 376 | if (ConfigMacrosExhaustive) |
Douglas Gregor | 8d93242 | 2013-03-20 03:59:18 +0000 | [diff] [blame] | 377 | OS << "[exhaustive]"; |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 378 | for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) { |
| 379 | if (I) |
| 380 | OS << ", "; |
| 381 | OS << ConfigMacros[I]; |
| 382 | } |
Douglas Gregor | 8d93242 | 2013-03-20 03:59:18 +0000 | [diff] [blame] | 383 | OS << "\n"; |
Douglas Gregor | 35b13ec | 2013-03-20 00:22:05 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 386 | struct { |
Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 387 | StringRef Prefix; |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 388 | HeaderKind Kind; |
| 389 | } Kinds[] = {{"", HK_Normal}, |
| 390 | {"textual ", HK_Textual}, |
| 391 | {"private ", HK_Private}, |
| 392 | {"private textual ", HK_PrivateTextual}, |
| 393 | {"exclude ", HK_Excluded}}; |
Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 394 | |
| 395 | for (auto &K : Kinds) { |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 396 | for (auto &H : Headers[K.Kind]) { |
Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 397 | OS.indent(Indent + 2); |
| 398 | OS << K.Prefix << "header \""; |
Richard Smith | 3c1a41a | 2014-12-02 00:08:08 +0000 | [diff] [blame] | 399 | OS.write_escaped(H.NameAsWritten); |
Richard Smith | 306d892 | 2014-10-22 23:50:56 +0000 | [diff] [blame] | 400 | OS << "\"\n"; |
| 401 | } |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 402 | } |
Douglas Gregor | 5952766 | 2012-10-15 06:28:11 +0000 | [diff] [blame] | 403 | |
Douglas Gregor | eb90e83 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 404 | for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 405 | MI != MIEnd; ++MI) |
Ben Langmuir | 9d6448b | 2014-08-09 00:57:23 +0000 | [diff] [blame] | 406 | // Print inferred subframework modules so that we don't need to re-infer |
| 407 | // them (requires expensive directory iteration + stat calls) when we build |
| 408 | // the module. Regular inferred submodules are OK, as we need to look at all |
| 409 | // those header files anyway. |
| 410 | if (!(*MI)->IsInferred || (*MI)->IsFramework) |
Ben Langmuir | ffbafa2 | 2014-04-23 21:10:46 +0000 | [diff] [blame] | 411 | (*MI)->print(OS, Indent + 2); |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 412 | |
Douglas Gregor | 24bb923 | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 413 | for (unsigned I = 0, N = Exports.size(); I != N; ++I) { |
| 414 | OS.indent(Indent + 2); |
Douglas Gregor | 8c7c835 | 2011-12-05 17:34:59 +0000 | [diff] [blame] | 415 | OS << "export "; |
| 416 | if (Module *Restriction = Exports[I].getPointer()) { |
| 417 | OS << Restriction->getFullModuleName(); |
| 418 | if (Exports[I].getInt()) |
| 419 | OS << ".*"; |
| 420 | } else { |
| 421 | OS << "*"; |
| 422 | } |
Douglas Gregor | 24bb923 | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 423 | OS << "\n"; |
| 424 | } |
| 425 | |
| 426 | for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { |
| 427 | OS.indent(Indent + 2); |
| 428 | OS << "export "; |
| 429 | printModuleId(OS, UnresolvedExports[I].Id); |
Davide Italiano | 7f96b39 | 2016-03-09 21:09:51 +0000 | [diff] [blame] | 430 | if (UnresolvedExports[I].Wildcard) |
| 431 | OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*"); |
Douglas Gregor | 24bb923 | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 432 | OS << "\n"; |
| 433 | } |
| 434 | |
Daniel Jasper | ba7f2f7 | 2013-09-24 09:14:14 +0000 | [diff] [blame] | 435 | for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) { |
| 436 | OS.indent(Indent + 2); |
| 437 | OS << "use "; |
| 438 | OS << DirectUses[I]->getFullModuleName(); |
| 439 | OS << "\n"; |
| 440 | } |
| 441 | |
| 442 | for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) { |
| 443 | OS.indent(Indent + 2); |
| 444 | OS << "use "; |
| 445 | printModuleId(OS, UnresolvedDirectUses[I]); |
| 446 | OS << "\n"; |
| 447 | } |
| 448 | |
Douglas Gregor | 6ddfca9 | 2013-01-14 17:21:00 +0000 | [diff] [blame] | 449 | for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) { |
| 450 | OS.indent(Indent + 2); |
| 451 | OS << "link "; |
| 452 | if (LinkLibraries[I].IsFramework) |
| 453 | OS << "framework "; |
| 454 | OS << "\""; |
| 455 | OS.write_escaped(LinkLibraries[I].Library); |
| 456 | OS << "\""; |
| 457 | } |
| 458 | |
Douglas Gregor | fb91265 | 2013-03-20 21:10:35 +0000 | [diff] [blame] | 459 | for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) { |
| 460 | OS.indent(Indent + 2); |
| 461 | OS << "conflict "; |
| 462 | printModuleId(OS, UnresolvedConflicts[I].Id); |
| 463 | OS << ", \""; |
| 464 | OS.write_escaped(UnresolvedConflicts[I].Message); |
| 465 | OS << "\"\n"; |
| 466 | } |
| 467 | |
| 468 | for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) { |
| 469 | OS.indent(Indent + 2); |
| 470 | OS << "conflict "; |
| 471 | OS << Conflicts[I].Other->getFullModuleName(); |
| 472 | OS << ", \""; |
| 473 | OS.write_escaped(Conflicts[I].Message); |
| 474 | OS << "\"\n"; |
| 475 | } |
| 476 | |
Douglas Gregor | 7344109 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 477 | if (InferSubmodules) { |
| 478 | OS.indent(Indent + 2); |
| 479 | if (InferExplicitSubmodules) |
| 480 | OS << "explicit "; |
| 481 | OS << "module * {\n"; |
| 482 | if (InferExportWildcard) { |
| 483 | OS.indent(Indent + 4); |
| 484 | OS << "export *\n"; |
| 485 | } |
| 486 | OS.indent(Indent + 2); |
| 487 | OS << "}\n"; |
| 488 | } |
| 489 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 490 | OS.indent(Indent); |
| 491 | OS << "}\n"; |
| 492 | } |
| 493 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 494 | LLVM_DUMP_METHOD void Module::dump() const { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 495 | print(llvm::errs()); |
| 496 | } |
| 497 | |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 498 | void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc, |
| 499 | VisibleCallback Vis, ConflictCallback Cb) { |
Ben Langmuir | 6d25fdc | 2016-02-11 17:04:42 +0000 | [diff] [blame] | 500 | assert(Loc.isValid() && "setVisible expects a valid import location"); |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 501 | if (isVisible(M)) |
| 502 | return; |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 503 | |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 504 | ++Generation; |
| 505 | |
| 506 | struct Visiting { |
| 507 | Module *M; |
| 508 | Visiting *ExportedBy; |
| 509 | }; |
| 510 | |
| 511 | std::function<void(Visiting)> VisitModule = [&](Visiting V) { |
| 512 | // Modules that aren't available cannot be made visible. |
| 513 | if (!V.M->isAvailable()) |
| 514 | return; |
| 515 | |
| 516 | // Nothing to do for a module that's already visible. |
| 517 | unsigned ID = V.M->getVisibilityID(); |
| 518 | if (ImportLocs.size() <= ID) |
| 519 | ImportLocs.resize(ID + 1); |
| 520 | else if (ImportLocs[ID].isValid()) |
| 521 | return; |
| 522 | |
| 523 | ImportLocs[ID] = Loc; |
| 524 | Vis(M); |
| 525 | |
| 526 | // Make any exported modules visible. |
| 527 | SmallVector<Module *, 16> Exports; |
| 528 | V.M->getExportedModules(Exports); |
| 529 | for (Module *E : Exports) |
| 530 | VisitModule({E, &V}); |
| 531 | |
| 532 | for (auto &C : V.M->Conflicts) { |
| 533 | if (isVisible(C.Other)) { |
| 534 | llvm::SmallVector<Module*, 8> Path; |
| 535 | for (Visiting *I = &V; I; I = I->ExportedBy) |
| 536 | Path.push_back(I->M); |
| 537 | Cb(Path, C.Other, C.Message); |
| 538 | } |
| 539 | } |
| 540 | }; |
| 541 | VisitModule({M, nullptr}); |
| 542 | } |