File: | tools/clang/lib/Basic/Module.cpp |
Warning: | line 607, column 1 Potential memory leak |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
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/CharInfo.h" | |||
17 | #include "clang/Basic/FileManager.h" | |||
18 | #include "clang/Basic/LangOptions.h" | |||
19 | #include "clang/Basic/SourceLocation.h" | |||
20 | #include "clang/Basic/TargetInfo.h" | |||
21 | #include "llvm/ADT/ArrayRef.h" | |||
22 | #include "llvm/ADT/SmallVector.h" | |||
23 | #include "llvm/ADT/StringMap.h" | |||
24 | #include "llvm/ADT/StringRef.h" | |||
25 | #include "llvm/ADT/StringSwitch.h" | |||
26 | #include "llvm/Support/Compiler.h" | |||
27 | #include "llvm/Support/ErrorHandling.h" | |||
28 | #include "llvm/Support/raw_ostream.h" | |||
29 | #include <algorithm> | |||
30 | #include <cassert> | |||
31 | #include <functional> | |||
32 | #include <string> | |||
33 | #include <utility> | |||
34 | #include <vector> | |||
35 | ||||
36 | using namespace clang; | |||
37 | ||||
38 | Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, | |||
39 | bool IsFramework, bool IsExplicit, unsigned VisibilityID) | |||
40 | : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent), | |||
41 | VisibilityID(VisibilityID), IsMissingRequirement(false), | |||
42 | HasIncompatibleModuleFile(false), IsAvailable(true), | |||
43 | IsFromModuleFile(false), IsFramework(IsFramework), IsExplicit(IsExplicit), | |||
44 | IsSystem(false), IsExternC(false), IsInferred(false), | |||
45 | InferSubmodules(false), InferExplicitSubmodules(false), | |||
46 | InferExportWildcard(false), ConfigMacrosExhaustive(false), | |||
47 | NoUndeclaredIncludes(false), NameVisibility(Hidden) { | |||
48 | if (Parent) { | |||
49 | if (!Parent->isAvailable()) | |||
50 | IsAvailable = false; | |||
51 | if (Parent->IsSystem) | |||
52 | IsSystem = true; | |||
53 | if (Parent->IsExternC) | |||
54 | IsExternC = true; | |||
55 | if (Parent->NoUndeclaredIncludes) | |||
56 | NoUndeclaredIncludes = true; | |||
57 | IsMissingRequirement = Parent->IsMissingRequirement; | |||
58 | ||||
59 | Parent->SubModuleIndex[Name] = Parent->SubModules.size(); | |||
60 | Parent->SubModules.push_back(this); | |||
61 | } | |||
62 | } | |||
63 | ||||
64 | Module::~Module() { | |||
65 | for (submodule_iterator I = submodule_begin(), IEnd = submodule_end(); | |||
66 | I != IEnd; ++I) { | |||
67 | delete *I; | |||
68 | } | |||
69 | } | |||
70 | ||||
71 | /// \brief Determine whether a translation unit built using the current | |||
72 | /// language options has the given feature. | |||
73 | static bool hasFeature(StringRef Feature, const LangOptions &LangOpts, | |||
74 | const TargetInfo &Target) { | |||
75 | bool HasFeature = llvm::StringSwitch<bool>(Feature) | |||
76 | .Case("altivec", LangOpts.AltiVec) | |||
77 | .Case("blocks", LangOpts.Blocks) | |||
78 | .Case("coroutines", LangOpts.CoroutinesTS) | |||
79 | .Case("cplusplus", LangOpts.CPlusPlus) | |||
80 | .Case("cplusplus11", LangOpts.CPlusPlus11) | |||
81 | .Case("cplusplus14", LangOpts.CPlusPlus14) | |||
82 | .Case("cplusplus17", LangOpts.CPlusPlus17) | |||
83 | .Case("c99", LangOpts.C99) | |||
84 | .Case("c11", LangOpts.C11) | |||
85 | .Case("c17", LangOpts.C17) | |||
86 | .Case("freestanding", LangOpts.Freestanding) | |||
87 | .Case("gnuinlineasm", LangOpts.GNUAsm) | |||
88 | .Case("objc", LangOpts.ObjC1) | |||
89 | .Case("objc_arc", LangOpts.ObjCAutoRefCount) | |||
90 | .Case("opencl", LangOpts.OpenCL) | |||
91 | .Case("tls", Target.isTLSSupported()) | |||
92 | .Case("zvector", LangOpts.ZVector) | |||
93 | .Default(Target.hasFeature(Feature)); | |||
94 | if (!HasFeature) | |||
95 | HasFeature = std::find(LangOpts.ModuleFeatures.begin(), | |||
96 | LangOpts.ModuleFeatures.end(), | |||
97 | Feature) != LangOpts.ModuleFeatures.end(); | |||
98 | return HasFeature; | |||
99 | } | |||
100 | ||||
101 | bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target, | |||
102 | Requirement &Req, | |||
103 | UnresolvedHeaderDirective &MissingHeader, | |||
104 | Module *&ShadowingModule) const { | |||
105 | if (IsAvailable) | |||
106 | return true; | |||
107 | ||||
108 | for (const Module *Current = this; Current; Current = Current->Parent) { | |||
109 | if (Current->ShadowingModule) { | |||
110 | ShadowingModule = Current->ShadowingModule; | |||
111 | return false; | |||
112 | } | |||
113 | for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) { | |||
114 | if (hasFeature(Current->Requirements[I].first, LangOpts, Target) != | |||
115 | Current->Requirements[I].second) { | |||
116 | Req = Current->Requirements[I]; | |||
117 | return false; | |||
118 | } | |||
119 | } | |||
120 | if (!Current->MissingHeaders.empty()) { | |||
121 | MissingHeader = Current->MissingHeaders.front(); | |||
122 | return false; | |||
123 | } | |||
124 | } | |||
125 | ||||
126 | llvm_unreachable("could not find a reason why module is unavailable")::llvm::llvm_unreachable_internal("could not find a reason why module is unavailable" , "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Basic/Module.cpp" , 126); | |||
127 | } | |||
128 | ||||
129 | bool Module::isSubModuleOf(const Module *Other) const { | |||
130 | const Module *This = this; | |||
131 | do { | |||
132 | if (This == Other) | |||
133 | return true; | |||
134 | ||||
135 | This = This->Parent; | |||
136 | } while (This); | |||
137 | ||||
138 | return false; | |||
139 | } | |||
140 | ||||
141 | const Module *Module::getTopLevelModule() const { | |||
142 | const Module *Result = this; | |||
143 | while (Result->Parent) | |||
144 | Result = Result->Parent; | |||
145 | ||||
146 | return Result; | |||
147 | } | |||
148 | ||||
149 | static StringRef getModuleNameFromComponent( | |||
150 | const std::pair<std::string, SourceLocation> &IdComponent) { | |||
151 | return IdComponent.first; | |||
152 | } | |||
153 | ||||
154 | static StringRef getModuleNameFromComponent(StringRef R) { return R; } | |||
155 | ||||
156 | template<typename InputIter> | |||
157 | static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End, | |||
158 | bool AllowStringLiterals = true) { | |||
159 | for (InputIter It = Begin; It != End; ++It) { | |||
160 | if (It != Begin) | |||
161 | OS << "."; | |||
162 | ||||
163 | StringRef Name = getModuleNameFromComponent(*It); | |||
164 | if (!AllowStringLiterals || isValidIdentifier(Name)) | |||
165 | OS << Name; | |||
166 | else { | |||
167 | OS << '"'; | |||
168 | OS.write_escaped(Name); | |||
169 | OS << '"'; | |||
170 | } | |||
171 | } | |||
172 | } | |||
173 | ||||
174 | template<typename Container> | |||
175 | static void printModuleId(raw_ostream &OS, const Container &C) { | |||
176 | return printModuleId(OS, C.begin(), C.end()); | |||
177 | } | |||
178 | ||||
179 | std::string Module::getFullModuleName(bool AllowStringLiterals) const { | |||
180 | SmallVector<StringRef, 2> Names; | |||
181 | ||||
182 | // Build up the set of module names (from innermost to outermost). | |||
183 | for (const Module *M = this; M; M = M->Parent) | |||
184 | Names.push_back(M->Name); | |||
185 | ||||
186 | std::string Result; | |||
187 | ||||
188 | llvm::raw_string_ostream Out(Result); | |||
189 | printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals); | |||
190 | Out.flush(); | |||
191 | ||||
192 | return Result; | |||
193 | } | |||
194 | ||||
195 | bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const { | |||
196 | for (const Module *M = this; M; M = M->Parent) { | |||
197 | if (nameParts.empty() || M->Name != nameParts.back()) | |||
198 | return false; | |||
199 | nameParts = nameParts.drop_back(); | |||
200 | } | |||
201 | return nameParts.empty(); | |||
202 | } | |||
203 | ||||
204 | Module::DirectoryName Module::getUmbrellaDir() const { | |||
205 | if (Header U = getUmbrellaHeader()) | |||
206 | return {"", U.Entry->getDir()}; | |||
207 | ||||
208 | return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()}; | |||
209 | } | |||
210 | ||||
211 | ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) { | |||
212 | if (!TopHeaderNames.empty()) { | |||
213 | for (std::vector<std::string>::iterator | |||
214 | I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) { | |||
215 | if (const FileEntry *FE = FileMgr.getFile(*I)) | |||
216 | TopHeaders.insert(FE); | |||
217 | } | |||
218 | TopHeaderNames.clear(); | |||
219 | } | |||
220 | ||||
221 | return llvm::makeArrayRef(TopHeaders.begin(), TopHeaders.end()); | |||
222 | } | |||
223 | ||||
224 | bool Module::directlyUses(const Module *Requested) const { | |||
225 | auto *Top = getTopLevelModule(); | |||
226 | ||||
227 | // A top-level module implicitly uses itself. | |||
228 | if (Requested->isSubModuleOf(Top)) | |||
229 | return true; | |||
230 | ||||
231 | for (auto *Use : Top->DirectUses) | |||
232 | if (Requested->isSubModuleOf(Use)) | |||
233 | return true; | |||
234 | ||||
235 | // Anyone is allowed to use our builtin stddef.h and its accompanying module. | |||
236 | if (!Requested->Parent && Requested->Name == "_Builtin_stddef_max_align_t") | |||
237 | return true; | |||
238 | ||||
239 | return false; | |||
240 | } | |||
241 | ||||
242 | void Module::addRequirement(StringRef Feature, bool RequiredState, | |||
243 | const LangOptions &LangOpts, | |||
244 | const TargetInfo &Target) { | |||
245 | Requirements.push_back(Requirement(Feature, RequiredState)); | |||
246 | ||||
247 | // If this feature is currently available, we're done. | |||
248 | if (hasFeature(Feature, LangOpts, Target) == RequiredState) | |||
249 | return; | |||
250 | ||||
251 | markUnavailable(/*MissingRequirement*/true); | |||
252 | } | |||
253 | ||||
254 | void Module::markUnavailable(bool MissingRequirement) { | |||
255 | auto needUpdate = [MissingRequirement](Module *M) { | |||
256 | return M->IsAvailable || (!M->IsMissingRequirement && MissingRequirement); | |||
257 | }; | |||
258 | ||||
259 | if (!needUpdate(this)) | |||
260 | return; | |||
261 | ||||
262 | SmallVector<Module *, 2> Stack; | |||
263 | Stack.push_back(this); | |||
264 | while (!Stack.empty()) { | |||
265 | Module *Current = Stack.back(); | |||
266 | Stack.pop_back(); | |||
267 | ||||
268 | if (!needUpdate(Current)) | |||
269 | continue; | |||
270 | ||||
271 | Current->IsAvailable = false; | |||
272 | Current->IsMissingRequirement |= MissingRequirement; | |||
273 | for (submodule_iterator Sub = Current->submodule_begin(), | |||
274 | SubEnd = Current->submodule_end(); | |||
275 | Sub != SubEnd; ++Sub) { | |||
276 | if (needUpdate(*Sub)) | |||
277 | Stack.push_back(*Sub); | |||
278 | } | |||
279 | } | |||
280 | } | |||
281 | ||||
282 | Module *Module::findSubmodule(StringRef Name) const { | |||
283 | llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name); | |||
284 | if (Pos == SubModuleIndex.end()) | |||
285 | return nullptr; | |||
286 | ||||
287 | return SubModules[Pos->getValue()]; | |||
288 | } | |||
289 | ||||
290 | void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const { | |||
291 | // All non-explicit submodules are exported. | |||
292 | for (std::vector<Module *>::const_iterator I = SubModules.begin(), | |||
293 | E = SubModules.end(); | |||
294 | I != E; ++I) { | |||
295 | Module *Mod = *I; | |||
296 | if (!Mod->IsExplicit) | |||
297 | Exported.push_back(Mod); | |||
298 | } | |||
299 | ||||
300 | // Find re-exported modules by filtering the list of imported modules. | |||
301 | bool AnyWildcard = false; | |||
302 | bool UnrestrictedWildcard = false; | |||
303 | SmallVector<Module *, 4> WildcardRestrictions; | |||
304 | for (unsigned I = 0, N = Exports.size(); I != N; ++I) { | |||
305 | Module *Mod = Exports[I].getPointer(); | |||
306 | if (!Exports[I].getInt()) { | |||
307 | // Export a named module directly; no wildcards involved. | |||
308 | Exported.push_back(Mod); | |||
309 | ||||
310 | continue; | |||
311 | } | |||
312 | ||||
313 | // Wildcard export: export all of the imported modules that match | |||
314 | // the given pattern. | |||
315 | AnyWildcard = true; | |||
316 | if (UnrestrictedWildcard) | |||
317 | continue; | |||
318 | ||||
319 | if (Module *Restriction = Exports[I].getPointer()) | |||
320 | WildcardRestrictions.push_back(Restriction); | |||
321 | else { | |||
322 | WildcardRestrictions.clear(); | |||
323 | UnrestrictedWildcard = true; | |||
324 | } | |||
325 | } | |||
326 | ||||
327 | // If there were any wildcards, push any imported modules that were | |||
328 | // re-exported by the wildcard restriction. | |||
329 | if (!AnyWildcard) | |||
330 | return; | |||
331 | ||||
332 | for (unsigned I = 0, N = Imports.size(); I != N; ++I) { | |||
333 | Module *Mod = Imports[I]; | |||
334 | bool Acceptable = UnrestrictedWildcard; | |||
335 | if (!Acceptable) { | |||
336 | // Check whether this module meets one of the restrictions. | |||
337 | for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) { | |||
338 | Module *Restriction = WildcardRestrictions[R]; | |||
339 | if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) { | |||
340 | Acceptable = true; | |||
341 | break; | |||
342 | } | |||
343 | } | |||
344 | } | |||
345 | ||||
346 | if (!Acceptable) | |||
347 | continue; | |||
348 | ||||
349 | Exported.push_back(Mod); | |||
350 | } | |||
351 | } | |||
352 | ||||
353 | void Module::buildVisibleModulesCache() const { | |||
354 | assert(VisibleModulesCache.empty() && "cache does not need building")(static_cast <bool> (VisibleModulesCache.empty() && "cache does not need building") ? void (0) : __assert_fail ( "VisibleModulesCache.empty() && \"cache does not need building\"" , "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Basic/Module.cpp" , 354, __extension__ __PRETTY_FUNCTION__)); | |||
355 | ||||
356 | // This module is visible to itself. | |||
357 | VisibleModulesCache.insert(this); | |||
358 | ||||
359 | // Every imported module is visible. | |||
360 | SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end()); | |||
361 | while (!Stack.empty()) { | |||
362 | Module *CurrModule = Stack.pop_back_val(); | |||
363 | ||||
364 | // Every module transitively exported by an imported module is visible. | |||
365 | if (VisibleModulesCache.insert(CurrModule).second) | |||
366 | CurrModule->getExportedModules(Stack); | |||
367 | } | |||
368 | } | |||
369 | ||||
370 | void Module::print(raw_ostream &OS, unsigned Indent) const { | |||
371 | OS.indent(Indent); | |||
372 | if (IsFramework) | |||
373 | OS << "framework "; | |||
374 | if (IsExplicit) | |||
375 | OS << "explicit "; | |||
376 | OS << "module "; | |||
377 | printModuleId(OS, &Name, &Name + 1); | |||
378 | ||||
379 | if (IsSystem || IsExternC) { | |||
380 | OS.indent(Indent + 2); | |||
381 | if (IsSystem) | |||
382 | OS << " [system]"; | |||
383 | if (IsExternC) | |||
384 | OS << " [extern_c]"; | |||
385 | } | |||
386 | ||||
387 | OS << " {\n"; | |||
388 | ||||
389 | if (!Requirements.empty()) { | |||
390 | OS.indent(Indent + 2); | |||
391 | OS << "requires "; | |||
392 | for (unsigned I = 0, N = Requirements.size(); I != N; ++I) { | |||
393 | if (I) | |||
394 | OS << ", "; | |||
395 | if (!Requirements[I].second) | |||
396 | OS << "!"; | |||
397 | OS << Requirements[I].first; | |||
398 | } | |||
399 | OS << "\n"; | |||
400 | } | |||
401 | ||||
402 | if (Header H = getUmbrellaHeader()) { | |||
403 | OS.indent(Indent + 2); | |||
404 | OS << "umbrella header \""; | |||
405 | OS.write_escaped(H.NameAsWritten); | |||
406 | OS << "\"\n"; | |||
407 | } else if (DirectoryName D = getUmbrellaDir()) { | |||
408 | OS.indent(Indent + 2); | |||
409 | OS << "umbrella \""; | |||
410 | OS.write_escaped(D.NameAsWritten); | |||
411 | OS << "\"\n"; | |||
412 | } | |||
413 | ||||
414 | if (!ConfigMacros.empty() || ConfigMacrosExhaustive) { | |||
415 | OS.indent(Indent + 2); | |||
416 | OS << "config_macros "; | |||
417 | if (ConfigMacrosExhaustive) | |||
418 | OS << "[exhaustive]"; | |||
419 | for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) { | |||
420 | if (I) | |||
421 | OS << ", "; | |||
422 | OS << ConfigMacros[I]; | |||
423 | } | |||
424 | OS << "\n"; | |||
425 | } | |||
426 | ||||
427 | struct { | |||
428 | StringRef Prefix; | |||
429 | HeaderKind Kind; | |||
430 | } Kinds[] = {{"", HK_Normal}, | |||
431 | {"textual ", HK_Textual}, | |||
432 | {"private ", HK_Private}, | |||
433 | {"private textual ", HK_PrivateTextual}, | |||
434 | {"exclude ", HK_Excluded}}; | |||
435 | ||||
436 | for (auto &K : Kinds) { | |||
437 | assert(&K == &Kinds[K.Kind] && "kinds in wrong order")(static_cast <bool> (&K == &Kinds[K.Kind] && "kinds in wrong order") ? void (0) : __assert_fail ("&K == &Kinds[K.Kind] && \"kinds in wrong order\"" , "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Basic/Module.cpp" , 437, __extension__ __PRETTY_FUNCTION__)); | |||
438 | for (auto &H : Headers[K.Kind]) { | |||
439 | OS.indent(Indent + 2); | |||
440 | OS << K.Prefix << "header \""; | |||
441 | OS.write_escaped(H.NameAsWritten); | |||
442 | OS << "\" { size " << H.Entry->getSize() | |||
443 | << " mtime " << H.Entry->getModificationTime() << " }\n"; | |||
444 | } | |||
445 | } | |||
446 | for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) { | |||
447 | for (auto &U : *Unresolved) { | |||
448 | OS.indent(Indent + 2); | |||
449 | OS << Kinds[U.Kind].Prefix << "header \""; | |||
450 | OS.write_escaped(U.FileName); | |||
451 | OS << "\""; | |||
452 | if (U.Size || U.ModTime) { | |||
453 | OS << " {"; | |||
454 | if (U.Size) | |||
455 | OS << " size " << *U.Size; | |||
456 | if (U.ModTime) | |||
457 | OS << " mtime " << *U.ModTime; | |||
458 | OS << " }"; | |||
459 | } | |||
460 | OS << "\n"; | |||
461 | } | |||
462 | } | |||
463 | ||||
464 | if (!ExportAsModule.empty()) { | |||
465 | OS.indent(Indent + 2); | |||
466 | OS << "export_as" << ExportAsModule << "\n"; | |||
467 | } | |||
468 | ||||
469 | for (submodule_const_iterator MI = submodule_begin(), MIEnd = submodule_end(); | |||
470 | MI != MIEnd; ++MI) | |||
471 | // Print inferred subframework modules so that we don't need to re-infer | |||
472 | // them (requires expensive directory iteration + stat calls) when we build | |||
473 | // the module. Regular inferred submodules are OK, as we need to look at all | |||
474 | // those header files anyway. | |||
475 | if (!(*MI)->IsInferred || (*MI)->IsFramework) | |||
476 | (*MI)->print(OS, Indent + 2); | |||
477 | ||||
478 | for (unsigned I = 0, N = Exports.size(); I != N; ++I) { | |||
479 | OS.indent(Indent + 2); | |||
480 | OS << "export "; | |||
481 | if (Module *Restriction = Exports[I].getPointer()) { | |||
482 | OS << Restriction->getFullModuleName(true); | |||
483 | if (Exports[I].getInt()) | |||
484 | OS << ".*"; | |||
485 | } else { | |||
486 | OS << "*"; | |||
487 | } | |||
488 | OS << "\n"; | |||
489 | } | |||
490 | ||||
491 | for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) { | |||
492 | OS.indent(Indent + 2); | |||
493 | OS << "export "; | |||
494 | printModuleId(OS, UnresolvedExports[I].Id); | |||
495 | if (UnresolvedExports[I].Wildcard) | |||
496 | OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*"); | |||
497 | OS << "\n"; | |||
498 | } | |||
499 | ||||
500 | for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) { | |||
501 | OS.indent(Indent + 2); | |||
502 | OS << "use "; | |||
503 | OS << DirectUses[I]->getFullModuleName(true); | |||
504 | OS << "\n"; | |||
505 | } | |||
506 | ||||
507 | for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) { | |||
508 | OS.indent(Indent + 2); | |||
509 | OS << "use "; | |||
510 | printModuleId(OS, UnresolvedDirectUses[I]); | |||
511 | OS << "\n"; | |||
512 | } | |||
513 | ||||
514 | for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) { | |||
515 | OS.indent(Indent + 2); | |||
516 | OS << "link "; | |||
517 | if (LinkLibraries[I].IsFramework) | |||
518 | OS << "framework "; | |||
519 | OS << "\""; | |||
520 | OS.write_escaped(LinkLibraries[I].Library); | |||
521 | OS << "\""; | |||
522 | } | |||
523 | ||||
524 | for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) { | |||
525 | OS.indent(Indent + 2); | |||
526 | OS << "conflict "; | |||
527 | printModuleId(OS, UnresolvedConflicts[I].Id); | |||
528 | OS << ", \""; | |||
529 | OS.write_escaped(UnresolvedConflicts[I].Message); | |||
530 | OS << "\"\n"; | |||
531 | } | |||
532 | ||||
533 | for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) { | |||
534 | OS.indent(Indent + 2); | |||
535 | OS << "conflict "; | |||
536 | OS << Conflicts[I].Other->getFullModuleName(true); | |||
537 | OS << ", \""; | |||
538 | OS.write_escaped(Conflicts[I].Message); | |||
539 | OS << "\"\n"; | |||
540 | } | |||
541 | ||||
542 | if (InferSubmodules) { | |||
543 | OS.indent(Indent + 2); | |||
544 | if (InferExplicitSubmodules) | |||
545 | OS << "explicit "; | |||
546 | OS << "module * {\n"; | |||
547 | if (InferExportWildcard) { | |||
548 | OS.indent(Indent + 4); | |||
549 | OS << "export *\n"; | |||
550 | } | |||
551 | OS.indent(Indent + 2); | |||
552 | OS << "}\n"; | |||
553 | } | |||
554 | ||||
555 | OS.indent(Indent); | |||
556 | OS << "}\n"; | |||
557 | } | |||
558 | ||||
559 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void Module::dump() const { | |||
560 | print(llvm::errs()); | |||
561 | } | |||
562 | ||||
563 | void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc, | |||
564 | VisibleCallback Vis, ConflictCallback Cb) { | |||
565 | assert(Loc.isValid() && "setVisible expects a valid import location")(static_cast <bool> (Loc.isValid() && "setVisible expects a valid import location" ) ? void (0) : __assert_fail ("Loc.isValid() && \"setVisible expects a valid import location\"" , "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/lib/Basic/Module.cpp" , 565, __extension__ __PRETTY_FUNCTION__)); | |||
566 | if (isVisible(M)) | |||
| ||||
567 | return; | |||
568 | ||||
569 | ++Generation; | |||
570 | ||||
571 | struct Visiting { | |||
572 | Module *M; | |||
573 | Visiting *ExportedBy; | |||
574 | }; | |||
575 | ||||
576 | std::function<void(Visiting)> VisitModule = [&](Visiting V) { | |||
577 | // Modules that aren't available cannot be made visible. | |||
578 | if (!V.M->isAvailable()) | |||
579 | return; | |||
580 | ||||
581 | // Nothing to do for a module that's already visible. | |||
582 | unsigned ID = V.M->getVisibilityID(); | |||
583 | if (ImportLocs.size() <= ID) | |||
584 | ImportLocs.resize(ID + 1); | |||
585 | else if (ImportLocs[ID].isValid()) | |||
586 | return; | |||
587 | ||||
588 | ImportLocs[ID] = Loc; | |||
589 | Vis(M); | |||
590 | ||||
591 | // Make any exported modules visible. | |||
592 | SmallVector<Module *, 16> Exports; | |||
593 | V.M->getExportedModules(Exports); | |||
594 | for (Module *E : Exports) | |||
595 | VisitModule({E, &V}); | |||
596 | ||||
597 | for (auto &C : V.M->Conflicts) { | |||
598 | if (isVisible(C.Other)) { | |||
599 | llvm::SmallVector<Module*, 8> Path; | |||
600 | for (Visiting *I = &V; I; I = I->ExportedBy) | |||
601 | Path.push_back(I->M); | |||
602 | Cb(Path, C.Other, C.Message); | |||
603 | } | |||
604 | } | |||
605 | }; | |||
606 | VisitModule({M, nullptr}); | |||
607 | } | |||
|
1 | // Implementation of std::function -*- C++ -*- |
2 | |
3 | // Copyright (C) 2004-2017 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /** @file include/bits/function.h |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{functional} |
28 | */ |
29 | |
30 | #ifndef _GLIBCXX_STD_FUNCTION_H1 |
31 | #define _GLIBCXX_STD_FUNCTION_H1 1 |
32 | |
33 | #pragma GCC system_header |
34 | |
35 | #if __cplusplus201103L < 201103L |
36 | # include <bits/c++0x_warning.h> |
37 | #else |
38 | |
39 | #if __cpp_rtti199711 |
40 | # include <typeinfo> |
41 | #endif |
42 | #include <bits/stl_function.h> |
43 | #include <bits/invoke.h> |
44 | #include <bits/refwrap.h> |
45 | #include <bits/functexcept.h> |
46 | |
47 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
48 | { |
49 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
50 | |
51 | /** |
52 | * Derives from @c unary_function or @c binary_function, or perhaps |
53 | * nothing, depending on the number of arguments provided. The |
54 | * primary template is the basis case, which derives nothing. |
55 | */ |
56 | template<typename _Res, typename... _ArgTypes> |
57 | struct _Maybe_unary_or_binary_function { }; |
58 | |
59 | /// Derives from @c unary_function, as appropriate. |
60 | template<typename _Res, typename _T1> |
61 | struct _Maybe_unary_or_binary_function<_Res, _T1> |
62 | : std::unary_function<_T1, _Res> { }; |
63 | |
64 | /// Derives from @c binary_function, as appropriate. |
65 | template<typename _Res, typename _T1, typename _T2> |
66 | struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> |
67 | : std::binary_function<_T1, _T2, _Res> { }; |
68 | |
69 | |
70 | /** |
71 | * @brief Exception class thrown when class template function's |
72 | * operator() is called with an empty target. |
73 | * @ingroup exceptions |
74 | */ |
75 | class bad_function_call : public std::exception |
76 | { |
77 | public: |
78 | virtual ~bad_function_call() noexcept; |
79 | |
80 | const char* what() const noexcept; |
81 | }; |
82 | |
83 | /** |
84 | * Trait identifying "location-invariant" types, meaning that the |
85 | * address of the object (or any of its members) will not escape. |
86 | * Trivially copyable types are location-invariant and users can |
87 | * specialize this trait for other types. |
88 | */ |
89 | template<typename _Tp> |
90 | struct __is_location_invariant |
91 | : is_trivially_copyable<_Tp>::type |
92 | { }; |
93 | |
94 | class _Undefined_class; |
95 | |
96 | union _Nocopy_types |
97 | { |
98 | void* _M_object; |
99 | const void* _M_const_object; |
100 | void (*_M_function_pointer)(); |
101 | void (_Undefined_class::*_M_member_pointer)(); |
102 | }; |
103 | |
104 | union [[gnu::may_alias]] _Any_data |
105 | { |
106 | void* _M_access() { return &_M_pod_data[0]; } |
107 | const void* _M_access() const { return &_M_pod_data[0]; } |
108 | |
109 | template<typename _Tp> |
110 | _Tp& |
111 | _M_access() |
112 | { return *static_cast<_Tp*>(_M_access()); } |
113 | |
114 | template<typename _Tp> |
115 | const _Tp& |
116 | _M_access() const |
117 | { return *static_cast<const _Tp*>(_M_access()); } |
118 | |
119 | _Nocopy_types _M_unused; |
120 | char _M_pod_data[sizeof(_Nocopy_types)]; |
121 | }; |
122 | |
123 | enum _Manager_operation |
124 | { |
125 | __get_type_info, |
126 | __get_functor_ptr, |
127 | __clone_functor, |
128 | __destroy_functor |
129 | }; |
130 | |
131 | // Simple type wrapper that helps avoid annoying const problems |
132 | // when casting between void pointers and pointers-to-pointers. |
133 | template<typename _Tp> |
134 | struct _Simple_type_wrapper |
135 | { |
136 | _Simple_type_wrapper(_Tp __value) : __value(__value) { } |
137 | |
138 | _Tp __value; |
139 | }; |
140 | |
141 | template<typename _Tp> |
142 | struct __is_location_invariant<_Simple_type_wrapper<_Tp> > |
143 | : __is_location_invariant<_Tp> |
144 | { }; |
145 | |
146 | template<typename _Signature> |
147 | class function; |
148 | |
149 | /// Base class of all polymorphic function object wrappers. |
150 | class _Function_base |
151 | { |
152 | public: |
153 | static const std::size_t _M_max_size = sizeof(_Nocopy_types); |
154 | static const std::size_t _M_max_align = __alignof__(_Nocopy_types); |
155 | |
156 | template<typename _Functor> |
157 | class _Base_manager |
158 | { |
159 | protected: |
160 | static const bool __stored_locally = |
161 | (__is_location_invariant<_Functor>::value |
162 | && sizeof(_Functor) <= _M_max_size |
163 | && __alignof__(_Functor) <= _M_max_align |
164 | && (_M_max_align % __alignof__(_Functor) == 0)); |
165 | |
166 | typedef integral_constant<bool, __stored_locally> _Local_storage; |
167 | |
168 | // Retrieve a pointer to the function object |
169 | static _Functor* |
170 | _M_get_pointer(const _Any_data& __source) |
171 | { |
172 | const _Functor* __ptr = |
173 | __stored_locally? std::__addressof(__source._M_access<_Functor>()) |
174 | /* have stored a pointer */ : __source._M_access<_Functor*>(); |
175 | return const_cast<_Functor*>(__ptr); |
176 | } |
177 | |
178 | // Clone a location-invariant function object that fits within |
179 | // an _Any_data structure. |
180 | static void |
181 | _M_clone(_Any_data& __dest, const _Any_data& __source, true_type) |
182 | { |
183 | ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>()); |
184 | } |
185 | |
186 | // Clone a function object that is not location-invariant or |
187 | // that cannot fit into an _Any_data structure. |
188 | static void |
189 | _M_clone(_Any_data& __dest, const _Any_data& __source, false_type) |
190 | { |
191 | __dest._M_access<_Functor*>() = |
192 | new _Functor(*__source._M_access<_Functor*>()); |
193 | } |
194 | |
195 | // Destroying a location-invariant object may still require |
196 | // destruction. |
197 | static void |
198 | _M_destroy(_Any_data& __victim, true_type) |
199 | { |
200 | __victim._M_access<_Functor>().~_Functor(); |
201 | } |
202 | |
203 | // Destroying an object located on the heap. |
204 | static void |
205 | _M_destroy(_Any_data& __victim, false_type) |
206 | { |
207 | delete __victim._M_access<_Functor*>(); |
208 | } |
209 | |
210 | public: |
211 | static bool |
212 | _M_manager(_Any_data& __dest, const _Any_data& __source, |
213 | _Manager_operation __op) |
214 | { |
215 | switch (__op) |
216 | { |
217 | #if __cpp_rtti199711 |
218 | case __get_type_info: |
219 | __dest._M_access<const type_info*>() = &typeid(_Functor); |
220 | break; |
221 | #endif |
222 | case __get_functor_ptr: |
223 | __dest._M_access<_Functor*>() = _M_get_pointer(__source); |
224 | break; |
225 | |
226 | case __clone_functor: |
227 | _M_clone(__dest, __source, _Local_storage()); |
228 | break; |
229 | |
230 | case __destroy_functor: |
231 | _M_destroy(__dest, _Local_storage()); |
232 | break; |
233 | } |
234 | return false; |
235 | } |
236 | |
237 | static void |
238 | _M_init_functor(_Any_data& __functor, _Functor&& __f) |
239 | { _M_init_functor(__functor, std::move(__f), _Local_storage()); } |
240 | |
241 | template<typename _Signature> |
242 | static bool |
243 | _M_not_empty_function(const function<_Signature>& __f) |
244 | { return static_cast<bool>(__f); } |
245 | |
246 | template<typename _Tp> |
247 | static bool |
248 | _M_not_empty_function(_Tp* __fp) |
249 | { return __fp != nullptr; } |
250 | |
251 | template<typename _Class, typename _Tp> |
252 | static bool |
253 | _M_not_empty_function(_Tp _Class::* __mp) |
254 | { return __mp != nullptr; } |
255 | |
256 | template<typename _Tp> |
257 | static bool |
258 | _M_not_empty_function(const _Tp&) |
259 | { return true; } |
260 | |
261 | private: |
262 | static void |
263 | _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type) |
264 | { ::new (__functor._M_access()) _Functor(std::move(__f)); } |
265 | |
266 | static void |
267 | _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type) |
268 | { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); } |
269 | }; |
270 | |
271 | _Function_base() : _M_manager(nullptr) { } |
272 | |
273 | ~_Function_base() |
274 | { |
275 | if (_M_manager) |
276 | _M_manager(_M_functor, _M_functor, __destroy_functor); |
277 | } |
278 | |
279 | bool _M_empty() const { return !_M_manager; } |
280 | |
281 | typedef bool (*_Manager_type)(_Any_data&, const _Any_data&, |
282 | _Manager_operation); |
283 | |
284 | _Any_data _M_functor; |
285 | _Manager_type _M_manager; |
286 | }; |
287 | |
288 | template<typename _Signature, typename _Functor> |
289 | class _Function_handler; |
290 | |
291 | template<typename _Res, typename _Functor, typename... _ArgTypes> |
292 | class _Function_handler<_Res(_ArgTypes...), _Functor> |
293 | : public _Function_base::_Base_manager<_Functor> |
294 | { |
295 | typedef _Function_base::_Base_manager<_Functor> _Base; |
296 | |
297 | public: |
298 | static _Res |
299 | _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) |
300 | { |
301 | return (*_Base::_M_get_pointer(__functor))( |
302 | std::forward<_ArgTypes>(__args)...); |
303 | } |
304 | }; |
305 | |
306 | template<typename _Functor, typename... _ArgTypes> |
307 | class _Function_handler<void(_ArgTypes...), _Functor> |
308 | : public _Function_base::_Base_manager<_Functor> |
309 | { |
310 | typedef _Function_base::_Base_manager<_Functor> _Base; |
311 | |
312 | public: |
313 | static void |
314 | _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) |
315 | { |
316 | (*_Base::_M_get_pointer(__functor))( |
317 | std::forward<_ArgTypes>(__args)...); |
318 | } |
319 | }; |
320 | |
321 | template<typename _Class, typename _Member, typename _Res, |
322 | typename... _ArgTypes> |
323 | class _Function_handler<_Res(_ArgTypes...), _Member _Class::*> |
324 | : public _Function_handler<void(_ArgTypes...), _Member _Class::*> |
325 | { |
326 | typedef _Function_handler<void(_ArgTypes...), _Member _Class::*> |
327 | _Base; |
328 | |
329 | public: |
330 | static _Res |
331 | _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) |
332 | { |
333 | return std::__invoke(_Base::_M_get_pointer(__functor)->__value, |
334 | std::forward<_ArgTypes>(__args)...); |
335 | } |
336 | }; |
337 | |
338 | template<typename _Class, typename _Member, typename... _ArgTypes> |
339 | class _Function_handler<void(_ArgTypes...), _Member _Class::*> |
340 | : public _Function_base::_Base_manager< |
341 | _Simple_type_wrapper< _Member _Class::* > > |
342 | { |
343 | typedef _Member _Class::* _Functor; |
344 | typedef _Simple_type_wrapper<_Functor> _Wrapper; |
345 | typedef _Function_base::_Base_manager<_Wrapper> _Base; |
346 | |
347 | public: |
348 | static bool |
349 | _M_manager(_Any_data& __dest, const _Any_data& __source, |
350 | _Manager_operation __op) |
351 | { |
352 | switch (__op) |
353 | { |
354 | #if __cpp_rtti199711 |
355 | case __get_type_info: |
356 | __dest._M_access<const type_info*>() = &typeid(_Functor); |
357 | break; |
358 | #endif |
359 | case __get_functor_ptr: |
360 | __dest._M_access<_Functor*>() = |
361 | &_Base::_M_get_pointer(__source)->__value; |
362 | break; |
363 | |
364 | default: |
365 | _Base::_M_manager(__dest, __source, __op); |
366 | } |
367 | return false; |
368 | } |
369 | |
370 | static void |
371 | _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) |
372 | { |
373 | std::__invoke(_Base::_M_get_pointer(__functor)->__value, |
374 | std::forward<_ArgTypes>(__args)...); |
375 | } |
376 | }; |
377 | |
378 | template<typename _From, typename _To> |
379 | using __check_func_return_type |
380 | = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>; |
381 | |
382 | /** |
383 | * @brief Primary class template for std::function. |
384 | * @ingroup functors |
385 | * |
386 | * Polymorphic function wrapper. |
387 | */ |
388 | template<typename _Res, typename... _ArgTypes> |
389 | class function<_Res(_ArgTypes...)> |
390 | : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>, |
391 | private _Function_base |
392 | { |
393 | template<typename _Func, |
394 | typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type> |
395 | struct _Callable : __check_func_return_type<_Res2, _Res> { }; |
396 | |
397 | // Used so the return type convertibility checks aren't done when |
398 | // performing overload resolution for copy construction/assignment. |
399 | template<typename _Tp> |
400 | struct _Callable<function, _Tp> : false_type { }; |
401 | |
402 | template<typename _Cond, typename _Tp> |
403 | using _Requires = typename enable_if<_Cond::value, _Tp>::type; |
404 | |
405 | public: |
406 | typedef _Res result_type; |
407 | |
408 | // [3.7.2.1] construct/copy/destroy |
409 | |
410 | /** |
411 | * @brief Default construct creates an empty function call wrapper. |
412 | * @post @c !(bool)*this |
413 | */ |
414 | function() noexcept |
415 | : _Function_base() { } |
416 | |
417 | /** |
418 | * @brief Creates an empty function call wrapper. |
419 | * @post @c !(bool)*this |
420 | */ |
421 | function(nullptr_t) noexcept |
422 | : _Function_base() { } |
423 | |
424 | /** |
425 | * @brief %Function copy constructor. |
426 | * @param __x A %function object with identical call signature. |
427 | * @post @c bool(*this) == bool(__x) |
428 | * |
429 | * The newly-created %function contains a copy of the target of @a |
430 | * __x (if it has one). |
431 | */ |
432 | function(const function& __x); |
433 | |
434 | /** |
435 | * @brief %Function move constructor. |
436 | * @param __x A %function object rvalue with identical call signature. |
437 | * |
438 | * The newly-created %function contains the target of @a __x |
439 | * (if it has one). |
440 | */ |
441 | function(function&& __x) noexcept : _Function_base() |
442 | { |
443 | __x.swap(*this); |
444 | } |
445 | |
446 | /** |
447 | * @brief Builds a %function that targets a copy of the incoming |
448 | * function object. |
449 | * @param __f A %function object that is callable with parameters of |
450 | * type @c T1, @c T2, ..., @c TN and returns a value convertible |
451 | * to @c Res. |
452 | * |
453 | * The newly-created %function object will target a copy of |
454 | * @a __f. If @a __f is @c reference_wrapper<F>, then this function |
455 | * object will contain a reference to the function object @c |
456 | * __f.get(). If @a __f is a NULL function pointer or NULL |
457 | * pointer-to-member, the newly-created object will be empty. |
458 | * |
459 | * If @a __f is a non-NULL function pointer or an object of type @c |
460 | * reference_wrapper<F>, this function will not throw. |
461 | */ |
462 | template<typename _Functor, |
463 | typename = _Requires<__not_<is_same<_Functor, function>>, void>, |
464 | typename = _Requires<_Callable<_Functor>, void>> |
465 | function(_Functor); |
466 | |
467 | /** |
468 | * @brief %Function assignment operator. |
469 | * @param __x A %function with identical call signature. |
470 | * @post @c (bool)*this == (bool)x |
471 | * @returns @c *this |
472 | * |
473 | * The target of @a __x is copied to @c *this. If @a __x has no |
474 | * target, then @c *this will be empty. |
475 | * |
476 | * If @a __x targets a function pointer or a reference to a function |
477 | * object, then this operation will not throw an %exception. |
478 | */ |
479 | function& |
480 | operator=(const function& __x) |
481 | { |
482 | function(__x).swap(*this); |
483 | return *this; |
484 | } |
485 | |
486 | /** |
487 | * @brief %Function move-assignment operator. |
488 | * @param __x A %function rvalue with identical call signature. |
489 | * @returns @c *this |
490 | * |
491 | * The target of @a __x is moved to @c *this. If @a __x has no |
492 | * target, then @c *this will be empty. |
493 | * |
494 | * If @a __x targets a function pointer or a reference to a function |
495 | * object, then this operation will not throw an %exception. |
496 | */ |
497 | function& |
498 | operator=(function&& __x) noexcept |
499 | { |
500 | function(std::move(__x)).swap(*this); |
501 | return *this; |
502 | } |
503 | |
504 | /** |
505 | * @brief %Function assignment to zero. |
506 | * @post @c !(bool)*this |
507 | * @returns @c *this |
508 | * |
509 | * The target of @c *this is deallocated, leaving it empty. |
510 | */ |
511 | function& |
512 | operator=(nullptr_t) noexcept |
513 | { |
514 | if (_M_manager) |
515 | { |
516 | _M_manager(_M_functor, _M_functor, __destroy_functor); |
517 | _M_manager = nullptr; |
518 | _M_invoker = nullptr; |
519 | } |
520 | return *this; |
521 | } |
522 | |
523 | /** |
524 | * @brief %Function assignment to a new target. |
525 | * @param __f A %function object that is callable with parameters of |
526 | * type @c T1, @c T2, ..., @c TN and returns a value convertible |
527 | * to @c Res. |
528 | * @return @c *this |
529 | * |
530 | * This %function object wrapper will target a copy of @a |
531 | * __f. If @a __f is @c reference_wrapper<F>, then this function |
532 | * object will contain a reference to the function object @c |
533 | * __f.get(). If @a __f is a NULL function pointer or NULL |
534 | * pointer-to-member, @c this object will be empty. |
535 | * |
536 | * If @a __f is a non-NULL function pointer or an object of type @c |
537 | * reference_wrapper<F>, this function will not throw. |
538 | */ |
539 | template<typename _Functor> |
540 | _Requires<_Callable<typename decay<_Functor>::type>, function&> |
541 | operator=(_Functor&& __f) |
542 | { |
543 | function(std::forward<_Functor>(__f)).swap(*this); |
544 | return *this; |
545 | } |
546 | |
547 | /// @overload |
548 | template<typename _Functor> |
549 | function& |
550 | operator=(reference_wrapper<_Functor> __f) noexcept |
551 | { |
552 | function(__f).swap(*this); |
553 | return *this; |
554 | } |
555 | |
556 | // [3.7.2.2] function modifiers |
557 | |
558 | /** |
559 | * @brief Swap the targets of two %function objects. |
560 | * @param __x A %function with identical call signature. |
561 | * |
562 | * Swap the targets of @c this function object and @a __f. This |
563 | * function will not throw an %exception. |
564 | */ |
565 | void swap(function& __x) noexcept |
566 | { |
567 | std::swap(_M_functor, __x._M_functor); |
568 | std::swap(_M_manager, __x._M_manager); |
569 | std::swap(_M_invoker, __x._M_invoker); |
570 | } |
571 | |
572 | // [3.7.2.3] function capacity |
573 | |
574 | /** |
575 | * @brief Determine if the %function wrapper has a target. |
576 | * |
577 | * @return @c true when this %function object contains a target, |
578 | * or @c false when it is empty. |
579 | * |
580 | * This function will not throw an %exception. |
581 | */ |
582 | explicit operator bool() const noexcept |
583 | { return !_M_empty(); } |
584 | |
585 | // [3.7.2.4] function invocation |
586 | |
587 | /** |
588 | * @brief Invokes the function targeted by @c *this. |
589 | * @returns the result of the target. |
590 | * @throws bad_function_call when @c !(bool)*this |
591 | * |
592 | * The function call operator invokes the target function object |
593 | * stored by @c this. |
594 | */ |
595 | _Res operator()(_ArgTypes... __args) const; |
596 | |
597 | #if __cpp_rtti199711 |
598 | // [3.7.2.5] function target access |
599 | /** |
600 | * @brief Determine the type of the target of this function object |
601 | * wrapper. |
602 | * |
603 | * @returns the type identifier of the target function object, or |
604 | * @c typeid(void) if @c !(bool)*this. |
605 | * |
606 | * This function will not throw an %exception. |
607 | */ |
608 | const type_info& target_type() const noexcept; |
609 | |
610 | /** |
611 | * @brief Access the stored target function object. |
612 | * |
613 | * @return Returns a pointer to the stored target function object, |
614 | * if @c typeid(_Functor).equals(target_type()); otherwise, a NULL |
615 | * pointer. |
616 | * |
617 | * This function does not throw exceptions. |
618 | * |
619 | * @{ |
620 | */ |
621 | template<typename _Functor> _Functor* target() noexcept; |
622 | |
623 | template<typename _Functor> const _Functor* target() const noexcept; |
624 | // @} |
625 | #endif |
626 | |
627 | private: |
628 | using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...); |
629 | _Invoker_type _M_invoker; |
630 | }; |
631 | |
632 | #if __cpp_deduction_guides >= 201606 |
633 | template<typename> |
634 | struct __function_guide_helper |
635 | { }; |
636 | |
637 | template<typename _Res, typename _Tp, bool _Nx, typename... _Args> |
638 | struct __function_guide_helper< |
639 | _Res (_Tp::*) (_Args...) noexcept(_Nx) |
640 | > |
641 | { using type = _Res(_Args...); }; |
642 | |
643 | template<typename _Res, typename _Tp, bool _Nx, typename... _Args> |
644 | struct __function_guide_helper< |
645 | _Res (_Tp::*) (_Args...) & noexcept(_Nx) |
646 | > |
647 | { using type = _Res(_Args...); }; |
648 | |
649 | template<typename _Res, typename _Tp, bool _Nx, typename... _Args> |
650 | struct __function_guide_helper< |
651 | _Res (_Tp::*) (_Args...) const noexcept(_Nx) |
652 | > |
653 | { using type = _Res(_Args...); }; |
654 | |
655 | template<typename _Res, typename _Tp, bool _Nx, typename... _Args> |
656 | struct __function_guide_helper< |
657 | _Res (_Tp::*) (_Args...) const & noexcept(_Nx) |
658 | > |
659 | { using type = _Res(_Args...); }; |
660 | |
661 | template<typename _Res, typename... _ArgTypes> |
662 | function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>; |
663 | |
664 | template<typename _Functor, typename _Signature = typename |
665 | __function_guide_helper<decltype(&_Functor::operator())>::type> |
666 | function(_Functor) -> function<_Signature>; |
667 | #endif |
668 | |
669 | // Out-of-line member definitions. |
670 | template<typename _Res, typename... _ArgTypes> |
671 | function<_Res(_ArgTypes...)>:: |
672 | function(const function& __x) |
673 | : _Function_base() |
674 | { |
675 | if (static_cast<bool>(__x)) |
676 | { |
677 | __x._M_manager(_M_functor, __x._M_functor, __clone_functor); |
678 | _M_invoker = __x._M_invoker; |
679 | _M_manager = __x._M_manager; |
680 | } |
681 | } |
682 | |
683 | template<typename _Res, typename... _ArgTypes> |
684 | template<typename _Functor, typename, typename> |
685 | function<_Res(_ArgTypes...)>:: |
686 | function(_Functor __f) |
687 | : _Function_base() |
688 | { |
689 | typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler; |
690 | |
691 | if (_My_handler::_M_not_empty_function(__f)) |
692 | { |
693 | _My_handler::_M_init_functor(_M_functor, std::move(__f)); |
694 | _M_invoker = &_My_handler::_M_invoke; |
695 | _M_manager = &_My_handler::_M_manager; |
696 | } |
697 | } |
698 | |
699 | template<typename _Res, typename... _ArgTypes> |
700 | _Res |
701 | function<_Res(_ArgTypes...)>:: |
702 | operator()(_ArgTypes... __args) const |
703 | { |
704 | if (_M_empty()) |
705 | __throw_bad_function_call(); |
706 | return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...); |
707 | } |
708 | |
709 | #if __cpp_rtti199711 |
710 | template<typename _Res, typename... _ArgTypes> |
711 | const type_info& |
712 | function<_Res(_ArgTypes...)>:: |
713 | target_type() const noexcept |
714 | { |
715 | if (_M_manager) |
716 | { |
717 | _Any_data __typeinfo_result; |
718 | _M_manager(__typeinfo_result, _M_functor, __get_type_info); |
719 | return *__typeinfo_result._M_access<const type_info*>(); |
720 | } |
721 | else |
722 | return typeid(void); |
723 | } |
724 | |
725 | template<typename _Res, typename... _ArgTypes> |
726 | template<typename _Functor> |
727 | _Functor* |
728 | function<_Res(_ArgTypes...)>:: |
729 | target() noexcept |
730 | { |
731 | const function* __const_this = this; |
732 | const _Functor* __func = __const_this->template target<_Functor>(); |
733 | return const_cast<_Functor*>(__func); |
734 | } |
735 | |
736 | template<typename _Res, typename... _ArgTypes> |
737 | template<typename _Functor> |
738 | const _Functor* |
739 | function<_Res(_ArgTypes...)>:: |
740 | target() const noexcept |
741 | { |
742 | if (typeid(_Functor) == target_type() && _M_manager) |
743 | { |
744 | _Any_data __ptr; |
745 | _M_manager(__ptr, _M_functor, __get_functor_ptr); |
746 | return __ptr._M_access<const _Functor*>(); |
747 | } |
748 | else |
749 | return nullptr; |
750 | } |
751 | #endif |
752 | |
753 | // [20.7.15.2.6] null pointer comparisons |
754 | |
755 | /** |
756 | * @brief Compares a polymorphic function object wrapper against 0 |
757 | * (the NULL pointer). |
758 | * @returns @c true if the wrapper has no target, @c false otherwise |
759 | * |
760 | * This function will not throw an %exception. |
761 | */ |
762 | template<typename _Res, typename... _Args> |
763 | inline bool |
764 | operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept |
765 | { return !static_cast<bool>(__f); } |
766 | |
767 | /// @overload |
768 | template<typename _Res, typename... _Args> |
769 | inline bool |
770 | operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept |
771 | { return !static_cast<bool>(__f); } |
772 | |
773 | /** |
774 | * @brief Compares a polymorphic function object wrapper against 0 |
775 | * (the NULL pointer). |
776 | * @returns @c false if the wrapper has no target, @c true otherwise |
777 | * |
778 | * This function will not throw an %exception. |
779 | */ |
780 | template<typename _Res, typename... _Args> |
781 | inline bool |
782 | operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept |
783 | { return static_cast<bool>(__f); } |
784 | |
785 | /// @overload |
786 | template<typename _Res, typename... _Args> |
787 | inline bool |
788 | operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept |
789 | { return static_cast<bool>(__f); } |
790 | |
791 | |
792 | // [20.7.15.2.7] specialized algorithms |
793 | |
794 | /** |
795 | * @brief Swap the targets of two polymorphic function object wrappers. |
796 | * |
797 | * This function will not throw an %exception. |
798 | */ |
799 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
800 | // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps |
801 | template<typename _Res, typename... _Args> |
802 | inline void |
803 | swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept |
804 | { __x.swap(__y); } |
805 | |
806 | _GLIBCXX_END_NAMESPACE_VERSION |
807 | } // namespace std |
808 | |
809 | #endif // C++11 |
810 | |
811 | #endif // _GLIBCXX_STD_FUNCTION_H |