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 25using namespace clang; 26 27Module::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 50Module::~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. 59static 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 78bool 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 101bool 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 113const Module *Module::getTopLevelModule() const { 114 const Module *Result = this; 115 while (Result->Parent) 116 Result = Result->Parent; 117 118 return Result; 119} 120 121std::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 141const DirectoryEntry *Module::getUmbrellaDir() const { 142 if (const FileEntry *Header = getUmbrellaHeader()) 143 return Header->getDir(); 144 145 return Umbrella.dyn_cast<const DirectoryEntry *>(); 146} 147 148ArrayRef<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 161bool 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 174void Module::addRequirement(StringRef Feature, bool RequiredState, 175 const LangOptions &LangOpts, 176 const TargetInfo &Target) { 177 Requirements.push_back(Requirement(Feature, RequiredState)); 178 179 // If this feature is currently available, we're done. 180 if (hasFeature(Feature, LangOpts, Target) == RequiredState) 181 return; 182 183 markUnavailable(/*MissingRequirement*/true); 184} 185 186void Module::markUnavailable(bool MissingRequirement) { 187 if (!IsAvailable) 188 return; 189 190 SmallVector<Module *, 2> Stack; 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; 200 Current->IsMissingRequirement |= MissingRequirement; 201 for (submodule_iterator Sub = Current->submodule_begin(), 202 SubEnd = Current->submodule_end(); 203 Sub != SubEnd; ++Sub) { 204 if ((*Sub)->IsAvailable) 205 Stack.push_back(*Sub); 206 } 207 } 208} 209 210Module *Module::findSubmodule(StringRef Name) const { 211 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); 212 if (Pos == SubModuleIndex.end()) 213 return nullptr; 214 215 return SubModules[Pos->getValue()]; 216} 217 218static void printModuleId(raw_ostream &OS, const ModuleId &Id) { 219 for (unsigned I = 0, N = Id.size(); I != N; ++I) { 220 if (I) 221 OS << "."; 222 OS << Id[I].first; 223 } 224} 225 226void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const { 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. 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 289void 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 295 // Every imported module is visible. 296 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end()); 297 while (!Stack.empty()) { 298 Module *CurrModule = Stack.pop_back_val(); 299 300 // Every module transitively exported by an imported module is visible. 301 if (VisibleModulesCache.insert(CurrModule).second) 302 CurrModule->getExportedModules(Stack); 303 } 304} 305 306void Module::print(raw_ostream &OS, unsigned Indent) const { 307 OS.indent(Indent); 308 if (IsFramework) 309 OS << "framework "; 310 if (IsExplicit) 311 OS << "explicit "; 312 OS << "module " << Name; 313 314 if (IsSystem || IsExternC) { 315 OS.indent(Indent + 2); 316 if (IsSystem) 317 OS << " [system]"; 318 if (IsExternC) 319 OS << " [extern_c]"; 320 } 321 322 OS << " {\n"; 323 324 if (!Requirements.empty()) { 325 OS.indent(Indent + 2); 326 OS << "requires "; 327 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) { 328 if (I) 329 OS << ", "; 330 if (!Requirements[I].second) 331 OS << "!"; 332 OS << Requirements[I].first; 333 } 334 OS << "\n"; 335 } 336 337 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) { 338 OS.indent(Indent + 2); 339 OS << "umbrella header \""; 340 OS.write_escaped(UmbrellaHeader->getName()); 341 OS << "\"\n"; 342 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) { 343 OS.indent(Indent + 2); 344 OS << "umbrella \""; 345 OS.write_escaped(UmbrellaDir->getName()); 346 OS << "\"\n"; 347 } 348 349 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) { 350 OS.indent(Indent + 2); 351 OS << "config_macros "; 352 if (ConfigMacrosExhaustive) 353 OS << "[exhaustive]"; 354 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) { 355 if (I) 356 OS << ", "; 357 OS << ConfigMacros[I]; 358 } 359 OS << "\n"; 360 } 361 362 struct { 363 StringRef Prefix; 364 HeaderKind Kind; 365 } Kinds[] = {{"", HK_Normal}, 366 {"textual ", HK_Textual}, 367 {"private ", HK_Private}, 368 {"private textual ", HK_PrivateTextual}, 369 {"exclude ", HK_Excluded}}; 370 371 for (auto &K : Kinds) { 372 for (auto &H : Headers[K.Kind]) { 373 OS.indent(Indent + 2); 374 OS << K.Prefix << "header \""; 375 OS.write_escaped(H.NameAsWritten); 376 OS << "\"\n"; 377 } 378 } 379 380 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); 381 MI != MIEnd; ++MI) 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) 387 (*MI)->print(OS, Indent + 2); 388 389 for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 390 OS.indent(Indent + 2); 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 } 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); 406 if (UnresolvedExports[I].Wildcard) { 407 if (UnresolvedExports[I].Id.empty()) 408 OS << "*"; 409 else 410 OS << ".*"; 411 } 412 OS << "\n"; 413 } 414 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 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 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 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 470 OS.indent(Indent); 471 OS << "}\n"; 472} 473 474void Module::dump() const { 475 print(llvm::errs()); 476} 477 478 479