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