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