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