Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 1 | //===--- Module.h - Describe a module ---------------------------*- C++ -*-===// |
| 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 | #include "clang/Basic/Module.h" |
| 15 | #include "clang/Basic/FileManager.h" |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 16 | #include "clang/Basic/LangOptions.h" |
Douglas Gregor | e727d21 | 2012-01-30 06:38:25 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/ADT/StringSwitch.h" |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 24 | Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, |
| 25 | bool IsFramework, bool IsExplicit) |
| 26 | : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), |
| 27 | Umbrella(), IsAvailable(true), IsFromModuleFile(false), |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 28 | IsFramework(IsFramework), IsExplicit(IsExplicit), IsSystem(false), |
| 29 | InferSubmodules(false), InferExplicitSubmodules(false), |
| 30 | InferExportWildcard(false), NameVisibility(Hidden) |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 31 | { |
| 32 | if (Parent) { |
| 33 | if (!Parent->isAvailable()) |
| 34 | IsAvailable = false; |
Douglas Gregor | 2f04f18 | 2012-02-02 18:42:48 +0000 | [diff] [blame] | 35 | if (Parent->IsSystem) |
| 36 | IsSystem = true; |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 37 | |
| 38 | Parent->SubModuleIndex[Name] = Parent->SubModules.size(); |
| 39 | Parent->SubModules.push_back(this); |
| 40 | } |
| 41 | } |
| 42 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 43 | Module::~Module() { |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 44 | for (submodule_iterator I = submodule_begin(), IEnd = submodule_end(); |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 45 | I != IEnd; ++I) { |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 46 | delete *I; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | } |
| 50 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 51 | /// \brief Determine whether a translation unit built using the current |
| 52 | /// language options has the given feature. |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 53 | static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, |
| 54 | const TargetInfo &Target) { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 55 | return llvm::StringSwitch<bool>(Feature) |
Douglas Gregor | e727d21 | 2012-01-30 06:38:25 +0000 | [diff] [blame] | 56 | .Case("altivec", LangOpts.AltiVec) |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 57 | .Case("blocks", LangOpts.Blocks) |
| 58 | .Case("cplusplus", LangOpts.CPlusPlus) |
| 59 | .Case("cplusplus11", LangOpts.CPlusPlus0x) |
| 60 | .Case("objc", LangOpts.ObjC1) |
| 61 | .Case("objc_arc", LangOpts.ObjCAutoRefCount) |
Douglas Gregor | e727d21 | 2012-01-30 06:38:25 +0000 | [diff] [blame] | 62 | .Case("opencl", LangOpts.OpenCL) |
| 63 | .Case("tls", Target.isTLSSupported()) |
| 64 | .Default(Target.hasFeature(Feature)); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 68 | Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target, |
| 69 | StringRef &Feature) const { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 70 | if (IsAvailable) |
| 71 | return true; |
| 72 | |
| 73 | for (const Module *Current = this; Current; Current = Current->Parent) { |
| 74 | for (unsigned I = 0, N = Current->Requires.size(); I != N; ++I) { |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 75 | if (!hasFeature(Current->Requires[I], LangOpts, Target)) { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 76 | Feature = Current->Requires[I]; |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | llvm_unreachable("could not find a reason why module is unavailable"); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Douglas Gregor | 0adaa88 | 2011-12-05 17:28:06 +0000 | [diff] [blame] | 85 | bool Module::isSubModuleOf(Module *Other) const { |
| 86 | const Module *This = this; |
| 87 | do { |
| 88 | if (This == Other) |
| 89 | return true; |
| 90 | |
| 91 | This = This->Parent; |
| 92 | } while (This); |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 97 | const Module *Module::getTopLevelModule() const { |
| 98 | const Module *Result = this; |
| 99 | while (Result->Parent) |
| 100 | Result = Result->Parent; |
| 101 | |
| 102 | return Result; |
| 103 | } |
| 104 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 105 | std::string Module::getFullModuleName() const { |
| 106 | llvm::SmallVector<StringRef, 2> Names; |
| 107 | |
| 108 | // Build up the set of module names (from innermost to outermost). |
| 109 | for (const Module *M = this; M; M = M->Parent) |
| 110 | Names.push_back(M->Name); |
| 111 | |
| 112 | std::string Result; |
| 113 | for (llvm::SmallVector<StringRef, 2>::reverse_iterator I = Names.rbegin(), |
| 114 | IEnd = Names.rend(); |
| 115 | I != IEnd; ++I) { |
| 116 | if (!Result.empty()) |
| 117 | Result += '.'; |
| 118 | |
| 119 | Result += *I; |
| 120 | } |
| 121 | |
| 122 | return Result; |
| 123 | } |
| 124 | |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 125 | const DirectoryEntry *Module::getUmbrellaDir() const { |
| 126 | if (const FileEntry *Header = getUmbrellaHeader()) |
| 127 | return Header->getDir(); |
| 128 | |
| 129 | return Umbrella.dyn_cast<const DirectoryEntry *>(); |
| 130 | } |
| 131 | |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 132 | void Module::addRequirement(StringRef Feature, const LangOptions &LangOpts, |
| 133 | const TargetInfo &Target) { |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 134 | Requires.push_back(Feature); |
| 135 | |
| 136 | // If this feature is currently available, we're done. |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 137 | if (hasFeature(Feature, LangOpts, Target)) |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 138 | return; |
| 139 | |
| 140 | if (!IsAvailable) |
| 141 | return; |
| 142 | |
| 143 | llvm::SmallVector<Module *, 2> Stack; |
| 144 | Stack.push_back(this); |
| 145 | while (!Stack.empty()) { |
| 146 | Module *Current = Stack.back(); |
| 147 | Stack.pop_back(); |
| 148 | |
| 149 | if (!Current->IsAvailable) |
| 150 | continue; |
| 151 | |
| 152 | Current->IsAvailable = false; |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 153 | for (submodule_iterator Sub = Current->submodule_begin(), |
| 154 | SubEnd = Current->submodule_end(); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 155 | Sub != SubEnd; ++Sub) { |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 156 | if ((*Sub)->IsAvailable) |
| 157 | Stack.push_back(*Sub); |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 162 | Module *Module::findSubmodule(StringRef Name) const { |
| 163 | llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); |
| 164 | if (Pos == SubModuleIndex.end()) |
| 165 | return 0; |
| 166 | |
| 167 | return SubModules[Pos->getValue()]; |
| 168 | } |
| 169 | |
Douglas Gregor | af13bfc | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 170 | static void printModuleId(llvm::raw_ostream &OS, const ModuleId &Id) { |
| 171 | for (unsigned I = 0, N = Id.size(); I != N; ++I) { |
| 172 | if (I) |
| 173 | OS << "."; |
| 174 | OS << Id[I].first; |
| 175 | } |
| 176 | } |
| 177 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 178 | void Module::print(llvm::raw_ostream &OS, unsigned Indent) const { |
| 179 | OS.indent(Indent); |
| 180 | if (IsFramework) |
| 181 | OS << "framework "; |
| 182 | if (IsExplicit) |
| 183 | OS << "explicit "; |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 184 | OS << "module " << Name; |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 185 | |
Douglas Gregor | a1f1fad | 2012-01-27 19:52:33 +0000 | [diff] [blame] | 186 | if (IsSystem) { |
| 187 | OS.indent(Indent + 2); |
| 188 | OS << " [system]"; |
| 189 | } |
| 190 | |
| 191 | OS << " {\n"; |
| 192 | |
Douglas Gregor | 51f564f | 2011-12-31 04:05:44 +0000 | [diff] [blame] | 193 | if (!Requires.empty()) { |
| 194 | OS.indent(Indent + 2); |
| 195 | OS << "requires "; |
| 196 | for (unsigned I = 0, N = Requires.size(); I != N; ++I) { |
| 197 | if (I) |
| 198 | OS << ", "; |
| 199 | OS << Requires[I]; |
| 200 | } |
| 201 | OS << "\n"; |
| 202 | } |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | 10694ce | 2011-12-08 17:39:04 +0000 | [diff] [blame] | 204 | if (const FileEntry *UmbrellaHeader = getUmbrellaHeader()) { |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 205 | OS.indent(Indent + 2); |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 206 | OS << "umbrella header \""; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 207 | OS.write_escaped(UmbrellaHeader->getName()); |
| 208 | OS << "\"\n"; |
Douglas Gregor | 489ad43 | 2011-12-08 18:00:48 +0000 | [diff] [blame] | 209 | } else if (const DirectoryEntry *UmbrellaDir = getUmbrellaDir()) { |
| 210 | OS.indent(Indent + 2); |
| 211 | OS << "umbrella \""; |
| 212 | OS.write_escaped(UmbrellaDir->getName()); |
| 213 | OS << "\"\n"; |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | for (unsigned I = 0, N = Headers.size(); I != N; ++I) { |
| 217 | OS.indent(Indent + 2); |
| 218 | OS << "header \""; |
| 219 | OS.write_escaped(Headers[I]->getName()); |
| 220 | OS << "\"\n"; |
| 221 | } |
| 222 | |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 223 | for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 224 | MI != MIEnd; ++MI) |
Douglas Gregor | b7a7819 | 2012-01-04 23:32:19 +0000 | [diff] [blame] | 225 | (*MI)->print(OS, Indent + 2); |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 226 | |
Douglas Gregor | af13bfc | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 227 | for (unsigned I = 0, N = Exports.size(); I != N; ++I) { |
| 228 | OS.indent(Indent + 2); |
Douglas Gregor | f4ac17e | 2011-12-05 17:34:59 +0000 | [diff] [blame] | 229 | OS << "export "; |
| 230 | if (Module *Restriction = Exports[I].getPointer()) { |
| 231 | OS << Restriction->getFullModuleName(); |
| 232 | if (Exports[I].getInt()) |
| 233 | OS << ".*"; |
| 234 | } else { |
| 235 | OS << "*"; |
| 236 | } |
Douglas Gregor | af13bfc | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 237 | OS << "\n"; |
| 238 | } |
| 239 | |
| 240 | for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { |
| 241 | OS.indent(Indent + 2); |
| 242 | OS << "export "; |
| 243 | printModuleId(OS, UnresolvedExports[I].Id); |
Douglas Gregor | f4ac17e | 2011-12-05 17:34:59 +0000 | [diff] [blame] | 244 | if (UnresolvedExports[I].Wildcard) { |
| 245 | if (UnresolvedExports[I].Id.empty()) |
| 246 | OS << "*"; |
| 247 | else |
| 248 | OS << ".*"; |
| 249 | } |
Douglas Gregor | af13bfc | 2011-12-02 18:58:38 +0000 | [diff] [blame] | 250 | OS << "\n"; |
| 251 | } |
| 252 | |
Douglas Gregor | 1e12368 | 2011-12-05 22:27:44 +0000 | [diff] [blame] | 253 | if (InferSubmodules) { |
| 254 | OS.indent(Indent + 2); |
| 255 | if (InferExplicitSubmodules) |
| 256 | OS << "explicit "; |
| 257 | OS << "module * {\n"; |
| 258 | if (InferExportWildcard) { |
| 259 | OS.indent(Indent + 4); |
| 260 | OS << "export *\n"; |
| 261 | } |
| 262 | OS.indent(Indent + 2); |
| 263 | OS << "}\n"; |
| 264 | } |
| 265 | |
Douglas Gregor | 1a4761e | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 266 | OS.indent(Indent); |
| 267 | OS << "}\n"; |
| 268 | } |
| 269 | |
| 270 | void Module::dump() const { |
| 271 | print(llvm::errs()); |
| 272 | } |
| 273 | |
| 274 | |