|
| 1 | +//===--- CompatibiltyOverride.h - Back-deploying compatibility fixes --*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// Support back-deploying compatibility fixes for newer apps running on older runtimes. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#ifndef COMPATIBILITY_OVERRIDE_H |
| 18 | +#define COMPATIBILITY_OVERRIDE_H |
| 19 | + |
| 20 | +#include "public/runtime/Private.h" |
| 21 | + |
| 22 | +#include "Runtime/Concurrency.h" |
| 23 | +#include "swift/Runtime/Metadata.h" |
| 24 | +#include "swift/Runtime/Once.h" |
| 25 | +#include <type_traits> |
| 26 | + |
| 27 | +namespace swift { |
| 28 | + |
| 29 | +// Macro utilities. |
| 30 | +#define COMPATIBILITY_UNPAREN(...) __VA_ARGS__ |
| 31 | +#define COMPATIBILITY_CONCAT2(x, y) x##y |
| 32 | +#define COMPATIBILITY_CONCAT(x, y) COMPATIBILITY_CONCAT2(x, y) |
| 33 | + |
| 34 | +// This ridiculous construct will remove the parentheses from the argument and |
| 35 | +// add a trailing comma, or will produce nothing when passed no argument. For |
| 36 | +// example: |
| 37 | +// COMPATIBILITY_UNPAREN_WITH_COMMA((1, 2, 3)) -> 1, 2, 3, |
| 38 | +// COMPATIBILITY_UNPAREN_WITH_COMMA((4)) -> 4, |
| 39 | +// COMPATIBILITY_UNPAREN_WITH_COMMA() -> |
| 40 | +#define COMPATIBILITY_UNPAREN_WITH_COMMA(x) \ |
| 41 | + COMPATIBILITY_CONCAT(COMPATIBILITY_UNPAREN_ADD_TRAILING_COMMA_, \ |
| 42 | + COMPATIBILITY_UNPAREN_WITH_COMMA2 x) |
| 43 | +#define COMPATIBILITY_UNPAREN_WITH_COMMA2(...) PARAMS(__VA_ARGS__) |
| 44 | +#define COMPATIBILITY_UNPAREN_ADD_TRAILING_COMMA_PARAMS(...) __VA_ARGS__, |
| 45 | +#define COMPATIBILITY_UNPAREN_ADD_TRAILING_COMMA_COMPATIBILITY_UNPAREN_WITH_COMMA2 |
| 46 | + |
| 47 | +// This ridiculous construct will preserve the parentheses around the argument, |
| 48 | +// or will produce an empty pair of parentheses when passed no argument. For |
| 49 | +// example: |
| 50 | +// COMPATIBILITY_PAREN((1, 2, 3)) -> (1, 2, 3) |
| 51 | +// COMPATIBILITY_PAREN((4)) -> (4) |
| 52 | +// COMPATIBILITY_PAREN() -> () |
| 53 | +#define COMPATIBILITY_PAREN(x) \ |
| 54 | + COMPATIBILITY_CONCAT(COMPATIBILITY_PAREN_, COMPATIBILITY_PAREN2 x) |
| 55 | +#define COMPATIBILITY_PAREN2(...) PARAMS(__VA_ARGS__) |
| 56 | +#define COMPATIBILITY_PAREN_PARAMS(...) (__VA_ARGS__) |
| 57 | +#define COMPATIBILITY_PAREN_COMPATIBILITY_PAREN2 () |
| 58 | + |
| 59 | +// Include path computation. Code that includes this file can write |
| 60 | +// `#include COMPATIBILITY_OVERRIDE_INCLUDE_PATH` to include the appropriate |
| 61 | +// .def file for the current library. |
| 62 | +#define COMPATIBILITY_OVERRIDE_INCLUDE_PATH_swiftRuntime \ |
| 63 | + "CompatibilityOverrideRuntime.def" |
| 64 | +#define COMPATIBILITY_OVERRIDE_INCLUDE_PATH_swift_Concurrency \ |
| 65 | + "CompatibilityOverrideConcurrency.def" |
| 66 | + |
| 67 | +#define COMPATIBILITY_OVERRIDE_INCLUDE_PATH \ |
| 68 | + COMPATIBILITY_CONCAT(COMPATIBILITY_OVERRIDE_INCLUDE_PATH_, \ |
| 69 | + SWIFT_TARGET_LIBRARY_NAME) |
| 70 | + |
| 71 | +// Compatibility overrides are only supported on Darwin. |
| 72 | +#ifndef SWIFT_RUNTIME_NO_COMPATIBILITY_OVERRIDES |
| 73 | +#if !(defined(__APPLE__) && defined(__MACH__)) |
| 74 | +#define SWIFT_RUNTIME_NO_COMPATIBILITY_OVERRIDES |
| 75 | +#endif |
| 76 | +#endif |
| 77 | + |
| 78 | +#ifdef SWIFT_RUNTIME_NO_COMPATIBILITY_OVERRIDES |
| 79 | + |
| 80 | +# error Back-deployment library must always be built with compatibilty overrides |
| 81 | + |
| 82 | +#else // #ifdef SWIFT_RUNTIME_NO_COMPATIBILITY_OVERRIDES |
| 83 | + |
| 84 | +// Override section name computation. `COMPATIBILITY_OVERRIDE_SECTION_NAME` will |
| 85 | +// resolve to string literal containing the appropriate section name for the |
| 86 | +// current library. |
| 87 | +#define COMPATIBILITY_OVERRIDE_SECTION_NAME_swift_Concurrency "__s_async_hook" |
| 88 | + |
| 89 | +#define COMPATIBILITY_OVERRIDE_SECTION_NAME \ |
| 90 | + COMPATIBILITY_CONCAT(COMPATIBILITY_OVERRIDE_SECTION_NAME_, \ |
| 91 | + SWIFT_TARGET_LIBRARY_NAME) |
| 92 | + |
| 93 | +// Create typedefs for function pointers to call the original implementation. |
| 94 | +#define OVERRIDE(name, ret, attrs, ccAttrs, namespace, typedArgs, namedArgs) \ |
| 95 | + ccAttrs typedef ret(*Original_##name) COMPATIBILITY_PAREN(typedArgs); |
| 96 | +#include "CompatibilityOverrideRuntime.def" |
| 97 | +#include "CompatibilityOverrideConcurrency.def" |
| 98 | +#undef OVERRIDE |
| 99 | + |
| 100 | + |
| 101 | +// Create typedefs for override function pointers. |
| 102 | +#define OVERRIDE(name, ret, attrs, ccAttrs, namespace, typedArgs, namedArgs) \ |
| 103 | + ccAttrs typedef ret (*Override_##name)(COMPATIBILITY_UNPAREN_WITH_COMMA( \ |
| 104 | + typedArgs) Original_##name originalImpl); |
| 105 | +#include "CompatibilityOverrideRuntime.def" |
| 106 | +#include "CompatibilityOverrideConcurrency.def" |
| 107 | +#undef OVERRIDE |
| 108 | + |
| 109 | +// Create declarations for getOverride functions. |
| 110 | +#define OVERRIDE(name, ret, attrs, ccAttrs, namespace, typedArgs, namedArgs) \ |
| 111 | + Override_ ## name getOverride_ ## name(); |
| 112 | +#include "CompatibilityOverrideRuntime.def" |
| 113 | +#include "CompatibilityOverrideConcurrency.def" |
| 114 | +#undef OVERRIDE |
| 115 | + |
| 116 | +/// Used to define an override point. The override point #defines the appropriate |
| 117 | +/// OVERRIDE macro from CompatibilityOverride.def to this macro, then includes |
| 118 | +/// the file to generate the override points. The original implementation of the |
| 119 | +/// functionality must be available as swift_funcNameHereImpl. |
| 120 | +#define COMPATIBILITY_OVERRIDE(name, ret, attrs, ccAttrs, namespace, \ |
| 121 | + typedArgs, namedArgs) \ |
| 122 | + attrs ccAttrs ret namespace swift_##name COMPATIBILITY_PAREN(typedArgs) { \ |
| 123 | + static Override_##name Override; \ |
| 124 | + static swift_once_t Predicate; \ |
| 125 | + swift_once( \ |
| 126 | + &Predicate, [](void *) { Override = getOverride_##name(); }, nullptr); \ |
| 127 | + if (Override != nullptr) \ |
| 128 | + return Override(COMPATIBILITY_UNPAREN_WITH_COMMA(namedArgs) \ |
| 129 | + swift_##name##Impl); \ |
| 130 | + return swift_##name##Impl COMPATIBILITY_PAREN(namedArgs); \ |
| 131 | + } |
| 132 | + |
| 133 | +#endif // #else SWIFT_RUNTIME_NO_COMPATIBILITY_OVERRIDES |
| 134 | + |
| 135 | +} /* end namespace swift */ |
| 136 | + |
| 137 | +#endif /* COMPATIBILITY_OVERRIDE_H */ |
0 commit comments