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), 30 Umbrella(), ASTFile(0), IsAvailable(true), IsFromModuleFile(false), 31 IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false), 32 IsExternC(false), InferSubmodules(false), InferExplicitSubmodules(false), 33 InferExportWildcard(false), ConfigMacrosExhaustive(false), 34 NameVisibility(Hidden) { 35 if (Parent) { 36 if (!Parent->isAvailable()) 37 IsAvailable = false; 38 if (Parent->IsSystem) 39 IsSystem = true; 40 if (Parent->IsExternC) 41 IsExternC = true; 42 43 Parent->SubModuleIndex[Name] = Parent->SubModules.size(); 44 Parent->SubModules.push_back(this); 45 } 46 } 47 48 Module::~Module() { 49 for (submodule_iterator I = submodule_begin(), IEnd = submodule_end(); 50 I != IEnd; ++I) { 51 delete *I; 52 } 53 } 54 55 /// \brief Determine whether a translation unit built using the current 56 /// language options has the given feature. 57 static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, 58 const TargetInfo &Target) { 59 return llvm::StringSwitch<bool>(Feature) 60 .Case("altivec", LangOpts.AltiVec) 61 .Case("blocks", LangOpts.Blocks) 62 .Case("cplusplus", LangOpts.CPlusPlus) 63 .Case("cplusplus11", LangOpts.CPlusPlus11) 64 .Case("objc", LangOpts.ObjC1) 65 .Case("objc_arc", LangOpts.ObjCAutoRefCount) 66 .Case("opencl", LangOpts.OpenCL) 67 .Case("tls", Target.isTLSSupported()) 68 .Default(Target.hasFeature(Feature)); 69 } 70 71 bool 72 Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target, 73 Requirement &Req, HeaderDirective &MissingHeader) const { 74 if (IsAvailable) 75 return true; 76 77 for (const Module *Current = this; Current; Current = Current->Parent) { 78 if (!Current->MissingHeaders.empty()) { 79 MissingHeader = Current->MissingHeaders.front(); 80 return false; 81 } 82 for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) { 83 if (hasFeature(Current->Requirements[I].first, LangOpts, Target) != 84 Current->Requirements[I].second) { 85 Req = Current->Requirements[I]; 86 return false; 87 } 88 } 89 } 90 91 llvm_unreachable("could not find a reason why module is unavailable"); 92 } 93 94 bool Module::isSubModuleOf(Module *Other) const { 95 const Module *This = this; 96 do { 97 if (This == Other) 98 return true; 99 100 This = This->Parent; 101 } while (This); 102 103 return false; 104 } 105 106 const Module *Module::getTopLevelModule() const { 107 const Module *Result = this; 108 while (Result->Parent) 109 Result = Result->Parent; 110 111 return Result; 112 } 113 114 std::string Module::getFullModuleName() const { 115 SmallVector<StringRef, 2> Names; 116 117 // Build up the set of module names (from innermost to outermost). 118 for (const Module *M = this; M; M = M->Parent) 119 Names.push_back(M->Name); 120 121 std::string Result; 122 for (SmallVectorImpl<StringRef>::reverse_iterator I = Names.rbegin(), 123 IEnd = Names.rend(); 124 I != IEnd; ++I) { 125 if (!Result.empty()) 126 Result += '.'; 127 128 Result += *I; 129 } 130 131 return Result; 132 } 133 134 const DirectoryEntry *Module::getUmbrellaDir() const { 135 if (const FileEntry *Header = getUmbrellaHeader()) 136 return Header->getDir(); 137 138 return Umbrella.dyn_cast<const DirectoryEntry *>(); 139 } 140 141 ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) { 142 if (!TopHeaderNames.empty()) { 143 for (std::vector<std::string>::iterator 144 I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) { 145 if (const FileEntry *FE = FileMgr.getFile(*I)) 146 TopHeaders.insert(FE); 147 } 148 TopHeaderNames.clear(); 149 } 150 151 return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end()); 152 } 153 154 void Module::addRequirement(StringRef Feature, bool RequiredState, 155 const LangOptions &LangOpts, 156 const TargetInfo &Target) { 157 Requirements.push_back(Requirement(Feature, RequiredState)); 158 159 // If this feature is currently available, we're done. 160 if (hasFeature(Feature, LangOpts, Target) == RequiredState) 161 return; 162 163 if (!IsAvailable) 164 return; 165 166 SmallVector<Module *, 2> Stack; 167 Stack.push_back(this); 168 while (!Stack.empty()) { 169 Module *Current = Stack.back(); 170 Stack.pop_back(); 171 172 if (!Current->IsAvailable) 173 continue; 174 175 Current->IsAvailable = false; 176 for (submodule_iterator Sub = Current->submodule_begin(), 177 SubEnd = Current->submodule_end(); 178 Sub != SubEnd; ++Sub) { 179 if ((*Sub)->IsAvailable) 180 Stack.push_back(*Sub); 181 } 182 } 183 } 184 185 Module *Module::findSubmodule(StringRef Name) const { 186 llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); 187 if (Pos == SubModuleIndex.end()) 188 return 0; 189 190 return SubModules[Pos->getValue()]; 191 } 192 193 static void printModuleId(raw_ostream &OS, const ModuleId &Id) { 194 for (unsigned I = 0, N = Id.size(); I != N; ++I) { 195 if (I) 196 OS << "."; 197 OS << Id[I].first; 198 } 199 } 200 201 void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const { 202 // All non-explicit submodules are exported. 203 for (std::vector<Module *>::const_iterator I = SubModules.begin(), 204 E = SubModules.end(); 205 I != E; ++I) { 206 Module *Mod = *I; 207 if (!Mod->IsExplicit) 208 Exported.push_back(Mod); 209 } 210 211 // Find re-exported modules by filtering the list of imported modules. 212 bool AnyWildcard = false; 213 bool UnrestrictedWildcard = false; 214 SmallVector<Module *, 4> WildcardRestrictions; 215 for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 216 Module *Mod = Exports[I].getPointer(); 217 if (!Exports[I].getInt()) { 218 // Export a named module directly; no wildcards involved. 219 Exported.push_back(Mod); 220 221 continue; 222 } 223 224 // Wildcard export: export all of the imported modules that match 225 // the given pattern. 226 AnyWildcard = true; 227 if (UnrestrictedWildcard) 228 continue; 229 230 if (Module *Restriction = Exports[I].getPointer()) 231 WildcardRestrictions.push_back(Restriction); 232 else { 233 WildcardRestrictions.clear(); 234 UnrestrictedWildcard = true; 235 } 236 } 237 238 // If there were any wildcards, push any imported modules that were 239 // re-exported by the wildcard restriction. 240 if (!AnyWildcard) 241 return; 242 243 for (unsigned I = 0, N = Imports.size(); I != N; ++I) { 244 Module *Mod = Imports[I]; 245 bool Acceptable = UnrestrictedWildcard; 246 if (!Acceptable) { 247 // Check whether this module meets one of the restrictions. 248 for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) { 249 Module *Restriction = WildcardRestrictions[R]; 250 if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) { 251 Acceptable = true; 252 break; 253 } 254 } 255 } 256 257 if (!Acceptable) 258 continue; 259 260 Exported.push_back(Mod); 261 } 262 } 263 264 void Module::buildVisibleModulesCache() const { 265 assert(VisibleModulesCache.empty() && "cache does not need building"); 266 267 // This module is visible to itself. 268 VisibleModulesCache.insert(this); 269 270 // Every imported module is visible. 271 SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end()); 272 while (!Stack.empty()) { 273 Module *CurrModule = Stack.pop_back_val(); 274 275 // Every module transitively exported by an imported module is visible. 276 if (VisibleModulesCache.insert(CurrModule).second) 277 CurrModule->getExportedModules(Stack); 278 } 279 } 280 281 void Module::print(raw_ostream &OS, unsigned Indent) const { 282 OS.indent(Indent); 283 if (IsFramework) 284 OS << "framework "; 285 if (IsExplicit) 286 OS << "explicit "; 287 OS << "module " << Name; 288 289 if (IsSystem) { 290 OS.indent(Indent + 2); 291 OS << " [system]"; 292 } 293 294 OS << " {\n"; 295 296 if (!Requirements.empty()) { 297 OS.indent(Indent + 2); 298 OS << "requires "; 299 for (unsigned I = 0, N = Requirements.size(); I != N; ++I) { 300 if (I) 301 OS << ", "; 302 if (!Requirements[I].second) 303 OS << "!"; 304 OS << Requirements[I].first; 305 } 306 OS << "\n"; 307 } 308 309 if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) { 310 OS.indent(Indent + 2); 311 OS << "umbrella header \""; 312 OS.write_escaped(UmbrellaHeader->getName()); 313 OS << "\"\n"; 314 } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) { 315 OS.indent(Indent + 2); 316 OS << "umbrella \""; 317 OS.write_escaped(UmbrellaDir->getName()); 318 OS << "\"\n"; 319 } 320 321 if (!ConfigMacros.empty() || ConfigMacrosExhaustive) { 322 OS.indent(Indent + 2); 323 OS << "config_macros "; 324 if (ConfigMacrosExhaustive) 325 OS << "[exhaustive]"; 326 for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) { 327 if (I) 328 OS << ", "; 329 OS << ConfigMacros[I]; 330 } 331 OS << "\n"; 332 } 333 334 for (unsigned I = 0, N = NormalHeaders.size(); I != N; ++I) { 335 OS.indent(Indent + 2); 336 OS << "header \""; 337 OS.write_escaped(NormalHeaders[I]->getName()); 338 OS << "\"\n"; 339 } 340 341 for (unsigned I = 0, N = ExcludedHeaders.size(); I != N; ++I) { 342 OS.indent(Indent + 2); 343 OS << "exclude header \""; 344 OS.write_escaped(ExcludedHeaders[I]->getName()); 345 OS << "\"\n"; 346 } 347 348 for (unsigned I = 0, N = PrivateHeaders.size(); I != N; ++I) { 349 OS.indent(Indent + 2); 350 OS << "private header \""; 351 OS.write_escaped(PrivateHeaders[I]->getName()); 352 OS << "\"\n"; 353 } 354 355 for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); 356 MI != MIEnd; ++MI) 357 (*MI)->print(OS, Indent + 2); 358 359 for (unsigned I = 0, N = Exports.size(); I != N; ++I) { 360 OS.indent(Indent + 2); 361 OS << "export "; 362 if (Module *Restriction = Exports[I].getPointer()) { 363 OS << Restriction->getFullModuleName(); 364 if (Exports[I].getInt()) 365 OS << ".*"; 366 } else { 367 OS << "*"; 368 } 369 OS << "\n"; 370 } 371 372 for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { 373 OS.indent(Indent + 2); 374 OS << "export "; 375 printModuleId(OS, UnresolvedExports[I].Id); 376 if (UnresolvedExports[I].Wildcard) { 377 if (UnresolvedExports[I].Id.empty()) 378 OS << "*"; 379 else 380 OS << ".*"; 381 } 382 OS << "\n"; 383 } 384 385 for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) { 386 OS.indent(Indent + 2); 387 OS << "use "; 388 OS << DirectUses[I]->getFullModuleName(); 389 OS << "\n"; 390 } 391 392 for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) { 393 OS.indent(Indent + 2); 394 OS << "use "; 395 printModuleId(OS, UnresolvedDirectUses[I]); 396 OS << "\n"; 397 } 398 399 for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) { 400 OS.indent(Indent + 2); 401 OS << "link "; 402 if (LinkLibraries[I].IsFramework) 403 OS << "framework "; 404 OS << "\""; 405 OS.write_escaped(LinkLibraries[I].Library); 406 OS << "\""; 407 } 408 409 for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) { 410 OS.indent(Indent + 2); 411 OS << "conflict "; 412 printModuleId(OS, UnresolvedConflicts[I].Id); 413 OS << ", \""; 414 OS.write_escaped(UnresolvedConflicts[I].Message); 415 OS << "\"\n"; 416 } 417 418 for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) { 419 OS.indent(Indent + 2); 420 OS << "conflict "; 421 OS << Conflicts[I].Other->getFullModuleName(); 422 OS << ", \""; 423 OS.write_escaped(Conflicts[I].Message); 424 OS << "\"\n"; 425 } 426 427 if (InferSubmodules) { 428 OS.indent(Indent + 2); 429 if (InferExplicitSubmodules) 430 OS << "explicit "; 431 OS << "module * {\n"; 432 if (InferExportWildcard) { 433 OS.indent(Indent + 4); 434 OS << "export *\n"; 435 } 436 OS.indent(Indent + 2); 437 OS << "}\n"; 438 } 439 440 OS.indent(Indent); 441 OS << "}\n"; 442 } 443 444 void Module::dump() const { 445 print(llvm::errs()); 446 } 447 448 449