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 15 #include "clang/Basic/Module.h" 16 #include "clang/Basic/FileManager.h" 17 #include "clang/Basic/LangOptions.h" 18 #include "clang/Basic/TargetInfo.h" 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringSwitch.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/raw_ostream.h" 24 25 using namespace clang; 26 27 Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 28 bool IsFramework, bool IsExplicit) 29 : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), Directory(), 30 Umbrella(), ASTFile(nullptr), IsMissingRequirement(false), 31 IsAvailable(true), IsFromModuleFile(false), IsFramework(IsFramework), 32 IsExplicit(IsExplicit), IsSystem(false), IsExternC(false), 33 IsInferred(false), InferSubmodules(false), InferExplicitSubmodules(false), 34 InferExportWildcard(false), ConfigMacrosExhaustive(false), 35 NameVisibility(Hidden) { 36 if (Parent) { 37 if (!Parent->isAvailable()) 38 IsAvailable = false; 39 if (Parent->IsSystem) 40 IsSystem = true; 41 if (Parent->IsExternC) 42 IsExternC = true; 43 IsMissingRequirement = Parent->IsMissingRequirement; 44 45 Parent->SubModuleIndex[Name] = Parent->SubModules.size(); 46 Parent->SubModules.push_back(this); 47 } 48 } 49 50 Module::~Module() { 51 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end(); 52 I != IEnd; ++I) { 53 delete *I; 54 } 55 } 56 57 /// \brief Determine whether a translation unit built using the current 58 /// language options has the given feature. 59 static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, 60 const TargetInfo &Target) { 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; 76 } 77 78 bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target, 79 Requirement &Req, 80 UnresolvedHeaderDirective &MissingHeader) const { 81 if (IsAvailable) 82 return true; 83 84 for (const Module *Current = this; Current; Current = Current->Parent) { 85 if (!Current->MissingHeaders.empty()) { 86 MissingHeader = Current->MissingHeaders.front(); 87 return false; 88 } 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]; 93 return false; 94 } 95 } 96 } 97 98 llvm_unreachable("could not find a reason why module is unavailable"); 99 } 100 101 bool Module::isSubModuleOf(const Module *Other) const { 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 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 121 std::string Module::getFullModuleName() const { 122 SmallVector<StringRef, 2> Names; 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; 129 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(), 130 IEnd = Names.rend(); 131 I != IEnd; ++I) { 132 if (!Result.empty()) 133 Result += '.'; 134 135 Result += *I; 136 } 137 138 return Result; 139 } 140 141 const DirectoryEntry *Module::getUmbrellaDir() const { 142 if (const FileEntry *Header = getUmbrellaHeader()) 143 return Header->getDir(); 144 145 return Umbrella.dyn_cast<const DirectoryEntry *>(); 146 } 147 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 161 void Module::addRequirement(StringRef Feature, bool RequiredState, 162 const LangOptions &LangOpts, 163 const TargetInfo &Target) { 164 Requirements.push_back(Requirement(Feature, RequiredState)); 165 166 // If this feature is currently available, we're done. 167 if (hasFeature(Feature, LangOpts, Target) == RequiredState) 168 return; 169 170 markUnavailable(/*MissingRequirement*/true); 171 } 172 173 void Module::markUnavailable(bool MissingRequirement) { 174 if (!IsAvailable) 175 return; 176 177 SmallVector<Module *, 2> Stack; 178 Stack.push_back(this); 179 while (!Stack.empty()) { 180 Module *Current = Stack.back(); 181 Stack.pop_back(); 182 183 if (!Current->IsAvailable) 184 continue; 185 186 Current->IsAvailable = false; 187 Current->IsMissingRequirement |= MissingRequirement; 188 for (submodule_iterator Sub = Current->submodule_begin(), 189 SubEnd = Current->submodule_end(); 190 Sub != SubEnd; ++Sub) { 191 if ((*Sub)->IsAvailable) 192 Stack.push_back(*Sub); 193 } 194 } 195 } 196 197 Module *Module::findSubmodule(StringRef Name) const { 198 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); 199 if (Pos == SubModuleIndex.end()) 200 return nullptr; 201 202 return SubModules[Pos->getValue()]; 203 } 204 205 static void printModuleId(raw_ostream &OS, const ModuleId &Id) { 206 for (unsigned I = 0, N = Id.size(); I != N; ++I) { 207 if (I) 208 OS << "."; 209 OS << Id[I].first; 210 } 211 } 212 213 void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const { 214 // All non-explicit submodules are exported. 215 for (std::vector<Module *>::const_iterator I = SubModules.begin(), 216 E = SubModules.end(); 217 I != E; ++I) { 218 Module *Mod = *I; 219 if (!Mod->IsExplicit) 220 Exported.push_back(Mod); 221 } 222 223 // Find re-exported modules by filtering the list of imported modules. 224 bool AnyWildcard = false; 225 bool UnrestrictedWildcard = false; 226 SmallVector<Module *, 4> WildcardRestrictions; 227 for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 228 Module *Mod = Exports[I].getPointer(); 229 if (!Exports[I].getInt()) { 230 // Export a named module directly; no wildcards involved. 231 Exported.push_back(Mod); 232 233 continue; 234 } 235 236 // Wildcard export: export all of the imported modules that match 237 // the given pattern. 238 AnyWildcard = true; 239 if (UnrestrictedWildcard) 240 continue; 241 242 if (Module *Restriction = Exports[I].getPointer()) 243 WildcardRestrictions.push_back(Restriction); 244 else { 245 WildcardRestrictions.clear(); 246 UnrestrictedWildcard = true; 247 } 248 } 249 250 // If there were any wildcards, push any imported modules that were 251 // re-exported by the wildcard restriction. 252 if (!AnyWildcard) 253 return; 254 255 for (unsigned I = 0, N = Imports.size(); I != N; ++I) { 256 Module *Mod = Imports[I]; 257 bool Acceptable = UnrestrictedWildcard; 258 if (!Acceptable) { 259 // Check whether this module meets one of the restrictions. 260 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) { 261 Module *Restriction = WildcardRestrictions[R]; 262 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) { 263 Acceptable = true; 264 break; 265 } 266 } 267 } 268 269 if (!Acceptable) 270 continue; 271 272 Exported.push_back(Mod); 273 } 274 } 275 276 void Module::buildVisibleModulesCache() const { 277 assert(VisibleModulesCache.empty() && "cache does not need building"); 278 279 // This module is visible to itself. 280 VisibleModulesCache.insert(this); 281 282 // Every imported module is visible. 283 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end()); 284 while (!Stack.empty()) { 285 Module *CurrModule = Stack.pop_back_val(); 286 287 // Every module transitively exported by an imported module is visible. 288 if (VisibleModulesCache.insert(CurrModule).second) 289 CurrModule->getExportedModules(Stack); 290 } 291 } 292 293 void Module::print(raw_ostream &OS, unsigned Indent) const { 294 OS.indent(Indent); 295 if (IsFramework) 296 OS << "framework "; 297 if (IsExplicit) 298 OS << "explicit "; 299 OS << "module " << Name; 300 301 if (IsSystem || IsExternC) { 302 OS.indent(Indent + 2); 303 if (IsSystem) 304 OS << " [system]"; 305 if (IsExternC) 306 OS << " [extern_c]"; 307 } 308 309 OS << " {\n"; 310 311 if (!Requirements.empty()) { 312 OS.indent(Indent + 2); 313 OS << "requires "; 314 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) { 315 if (I) 316 OS << ", "; 317 if (!Requirements[I].second) 318 OS << "!"; 319 OS << Requirements[I].first; 320 } 321 OS << "\n"; 322 } 323 324 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) { 325 OS.indent(Indent + 2); 326 OS << "umbrella header \""; 327 OS.write_escaped(UmbrellaHeader->getName()); 328 OS << "\"\n"; 329 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) { 330 OS.indent(Indent + 2); 331 OS << "umbrella \""; 332 OS.write_escaped(UmbrellaDir->getName()); 333 OS << "\"\n"; 334 } 335 336 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) { 337 OS.indent(Indent + 2); 338 OS << "config_macros "; 339 if (ConfigMacrosExhaustive) 340 OS << "[exhaustive]"; 341 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) { 342 if (I) 343 OS << ", "; 344 OS << ConfigMacros[I]; 345 } 346 OS << "\n"; 347 } 348 349 struct { 350 StringRef Prefix; 351 HeaderKind Kind; 352 } Kinds[] = {{"", HK_Normal}, 353 {"textual ", HK_Textual}, 354 {"private ", HK_Private}, 355 {"private textual ", HK_PrivateTextual}, 356 {"exclude ", HK_Excluded}}; 357 358 for (auto &K : Kinds) { 359 for (auto &H : Headers[K.Kind]) { 360 OS.indent(Indent + 2); 361 OS << K.Prefix << "header \""; 362 OS.write_escaped(H.NameAsWritten); 363 OS << "\"\n"; 364 } 365 } 366 367 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); 368 MI != MIEnd; ++MI) 369 // Print inferred subframework modules so that we don't need to re-infer 370 // them (requires expensive directory iteration + stat calls) when we build 371 // the module. Regular inferred submodules are OK, as we need to look at all 372 // those header files anyway. 373 if (!(*MI)->IsInferred || (*MI)->IsFramework) 374 (*MI)->print(OS, Indent + 2); 375 376 for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 377 OS.indent(Indent + 2); 378 OS << "export "; 379 if (Module *Restriction = Exports[I].getPointer()) { 380 OS << Restriction->getFullModuleName(); 381 if (Exports[I].getInt()) 382 OS << ".*"; 383 } else { 384 OS << "*"; 385 } 386 OS << "\n"; 387 } 388 389 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { 390 OS.indent(Indent + 2); 391 OS << "export "; 392 printModuleId(OS, UnresolvedExports[I].Id); 393 if (UnresolvedExports[I].Wildcard) { 394 if (UnresolvedExports[I].Id.empty()) 395 OS << "*"; 396 else 397 OS << ".*"; 398 } 399 OS << "\n"; 400 } 401 402 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) { 403 OS.indent(Indent + 2); 404 OS << "use "; 405 OS << DirectUses[I]->getFullModuleName(); 406 OS << "\n"; 407 } 408 409 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) { 410 OS.indent(Indent + 2); 411 OS << "use "; 412 printModuleId(OS, UnresolvedDirectUses[I]); 413 OS << "\n"; 414 } 415 416 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) { 417 OS.indent(Indent + 2); 418 OS << "link "; 419 if (LinkLibraries[I].IsFramework) 420 OS << "framework "; 421 OS << "\""; 422 OS.write_escaped(LinkLibraries[I].Library); 423 OS << "\""; 424 } 425 426 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) { 427 OS.indent(Indent + 2); 428 OS << "conflict "; 429 printModuleId(OS, UnresolvedConflicts[I].Id); 430 OS << ", \""; 431 OS.write_escaped(UnresolvedConflicts[I].Message); 432 OS << "\"\n"; 433 } 434 435 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) { 436 OS.indent(Indent + 2); 437 OS << "conflict "; 438 OS << Conflicts[I].Other->getFullModuleName(); 439 OS << ", \""; 440 OS.write_escaped(Conflicts[I].Message); 441 OS << "\"\n"; 442 } 443 444 if (InferSubmodules) { 445 OS.indent(Indent + 2); 446 if (InferExplicitSubmodules) 447 OS << "explicit "; 448 OS << "module * {\n"; 449 if (InferExportWildcard) { 450 OS.indent(Indent + 4); 451 OS << "export *\n"; 452 } 453 OS.indent(Indent + 2); 454 OS << "}\n"; 455 } 456 457 OS.indent(Indent); 458 OS << "}\n"; 459 } 460 461 void Module::dump() const { 462 print(llvm::errs()); 463 } 464 465 466