|
| 1 | +//===--- TypeLookupError.h - Type lookup error value. -----------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 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 | +// Provides the TypeLookupError class, which represents errors when demangling |
| 14 | +// or looking up types. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef SWIFT_DEMANGLING_TypeLookupError_H |
| 19 | +#define SWIFT_DEMANGLING_TypeLookupError_H |
| 20 | + |
| 21 | +#include "swift/Basic/TaggedUnion.h" |
| 22 | +#include "swift/Runtime/Portability.h" |
| 23 | + |
| 24 | +namespace swift { |
| 25 | + |
| 26 | +/// An error that occurred while looking up a type at runtime from a mangled |
| 27 | +/// name. |
| 28 | +/// /// |
| 29 | +/// This ultimately just provides a string, but is built to take up minimal |
| 30 | +/// space when passed around, and perform as much work lazily as possible. We |
| 31 | +/// don't want to spend a lot of time building strings when the caller is going |
| 32 | +/// to handle the error gracefully and try a fallback. We only want to waste |
| 33 | +/// time/space on that when the error message is actually relevant, so build it |
| 34 | +/// as late as possible. |
| 35 | +/// /// |
| 36 | +/// To be as compact as possible, this type holds a context pointer and a |
| 37 | +/// callback function. The callback function uses the context pointer to return |
| 38 | +/// an error string when requested. The callback function also does quadruple |
| 39 | +/// duty to copy/destroy the context as needed, and free the returned error |
| 40 | +/// string if needed. Commands are passed to the callback to request the |
| 41 | +/// different operations, which means we only have to store one function pointer |
| 42 | +/// instead of four. |
| 43 | +class TypeLookupError { |
| 44 | +public: |
| 45 | + /// The commands that can be passed to the callback function. |
| 46 | + enum class Command { |
| 47 | + /// Return the error string to the caller, as a char *. |
| 48 | + CopyErrorString, |
| 49 | + |
| 50 | + /// Destroy the error string returned from CopyErrorString, if necessary. |
| 51 | + /// The return value is ignored. |
| 52 | + DestroyErrorString, |
| 53 | + |
| 54 | + /// Return a copy of the context pointer (used for copying TypeLookupError |
| 55 | + /// objects.) |
| 56 | + CopyContext, |
| 57 | + |
| 58 | + /// Destroy the context pointer. The return value is ignored. |
| 59 | + DestroyContext, |
| 60 | + }; |
| 61 | + |
| 62 | + /// The callback used to respond to the commands. The parameters are: |
| 63 | + /// - `context`: the context that the value was initialized with, or the |
| 64 | + /// context returned from a CopyContext call |
| 65 | + /// - `command`: the command to respond to |
| 66 | + /// - `param`: when `command` is `DestroyErrorString`, the string pointer to |
| 67 | + /// destroy, otherwise NULL |
| 68 | + using Callback = void *(*)(void *context, Command command, void *param); |
| 69 | + |
| 70 | +private: |
| 71 | + void *Context; |
| 72 | + Callback Fn; |
| 73 | + |
| 74 | + /// A no-op callback used to avoid a bunch of `if (Fn)` checks. |
| 75 | + static void *nop(void *context, Command command, void *param) { |
| 76 | + return nullptr; |
| 77 | + } |
| 78 | + |
| 79 | + /// Helper functions for getting a C string from a lambda. These allow us to |
| 80 | + /// wrap lambdas returning `char *` or `std::string` and standardize them on |
| 81 | + /// `char *`. |
| 82 | + static char *getCString(char *str) { return str; } |
| 83 | + |
| 84 | + static char *getCString(const std::string &str) { |
| 85 | + return strdup(str.c_str()); |
| 86 | + } |
| 87 | + |
| 88 | +public: |
| 89 | + TypeLookupError(const TypeLookupError &other) { |
| 90 | + Fn = other.Fn; |
| 91 | + Context = other.Fn(other.Context, Command::CopyContext, nullptr); |
| 92 | + } |
| 93 | + |
| 94 | + TypeLookupError(TypeLookupError &&other) { |
| 95 | + Fn = other.Fn; |
| 96 | + Context = other.Context; |
| 97 | + |
| 98 | + other.Fn = nop; |
| 99 | + other.Context = nullptr; |
| 100 | + } |
| 101 | + |
| 102 | + ~TypeLookupError() { Fn(Context, Command::DestroyContext, nullptr); } |
| 103 | + |
| 104 | + TypeLookupError(void *context, Callback fn) : Context(context), Fn(fn ? fn : nop) {} |
| 105 | + |
| 106 | + TypeLookupError &operator=(const TypeLookupError &other) { |
| 107 | + if (this == &other) |
| 108 | + return *this; |
| 109 | + |
| 110 | + Fn(Context, Command::DestroyContext, nullptr); |
| 111 | + Fn = other.Fn; |
| 112 | + Context = Fn(Context, Command::CopyContext, nullptr); |
| 113 | + |
| 114 | + return *this; |
| 115 | + } |
| 116 | + |
| 117 | + /// Construct a TypeLookupError that just returns a constant C string. |
| 118 | + TypeLookupError(const char *str) |
| 119 | + : TypeLookupError([=] { return const_cast<char *>(str); }) {} |
| 120 | + |
| 121 | + /// Construct a TypeLookupError that creates a string using asprintf. The passed-in |
| 122 | + /// format string and arguments are passed directly to swift_asprintf when |
| 123 | + /// the string is requested. The arguments are captured and the string is only |
| 124 | + /// formatted when needed. |
| 125 | + template <typename... Args> |
| 126 | + TypeLookupError(const char *fmt, Args... args) |
| 127 | + : TypeLookupError([=] { |
| 128 | + char *str; |
| 129 | + swift_asprintf(&str, fmt, args...); |
| 130 | + return str; |
| 131 | + }) {} |
| 132 | + |
| 133 | + /// Construct a TypeLookupError that wraps a function returning a string. The |
| 134 | + /// passed-in function can return either a `std::string` or `char *`. If it |
| 135 | + /// returns `char *` then the string will be destroyed with `free()`. |
| 136 | + template <typename F> TypeLookupError(const F &fn) { |
| 137 | + Context = new F(fn); |
| 138 | + Fn = [](void *context, Command command, void *param) -> void * { |
| 139 | + auto castContext = reinterpret_cast<F *>(context); |
| 140 | + switch (command) { |
| 141 | + case Command::CopyErrorString: { |
| 142 | + return TypeLookupError::getCString((*castContext)()); |
| 143 | + } |
| 144 | + case Command::DestroyErrorString: |
| 145 | + free(param); |
| 146 | + return nullptr; |
| 147 | + case Command::CopyContext: |
| 148 | + return new F(*castContext); |
| 149 | + case Command::DestroyContext: |
| 150 | + delete castContext; |
| 151 | + return nullptr; |
| 152 | + } |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + /// Get the error string from the error value. The value must be passed to |
| 157 | + /// `freeErrorString` when done. (Unless you're just calling a `fatalError` |
| 158 | + /// in which case there's no point.) |
| 159 | + char *copyErrorString() { |
| 160 | + return reinterpret_cast<char *>( |
| 161 | + Fn(Context, Command::CopyErrorString, nullptr)); |
| 162 | + } |
| 163 | + |
| 164 | + /// Free an error string previously obtained from `copyErrorString`. |
| 165 | + void freeErrorString(char *str) { |
| 166 | + Fn(Context, Command::DestroyErrorString, str); |
| 167 | + } |
| 168 | +}; |
| 169 | + |
| 170 | +/// A value that's either a `TypeLookupError` or some parameterized type value `T`. A |
| 171 | +/// convenience wrapper around `TaggedUnion<T, TypeLookupError>`. |
| 172 | +template <typename T> class TypeLookupErrorOr { |
| 173 | + TaggedUnion<T, TypeLookupError> Value; |
| 174 | + |
| 175 | +public: |
| 176 | + TypeLookupErrorOr(const T &t) : Value(t) { |
| 177 | + if (!t) |
| 178 | + Value = TypeLookupError("unknown error"); |
| 179 | + } |
| 180 | + |
| 181 | + TypeLookupErrorOr(const TypeLookupError &te) : Value(te) {} |
| 182 | + |
| 183 | + T getType() { |
| 184 | + if (auto *ptr = Value.template dyn_cast<T>()) |
| 185 | + return *ptr; |
| 186 | + return T(); |
| 187 | + } |
| 188 | + |
| 189 | + TypeLookupError *getError() { |
| 190 | + return Value.template dyn_cast<TypeLookupError>(); |
| 191 | + } |
| 192 | + |
| 193 | + bool isError() { return getError() != nullptr; } |
| 194 | +}; |
| 195 | + |
| 196 | +} // namespace swift |
| 197 | + |
| 198 | +#endif // SWIFT_DEMANGLING_TypeLookupError_H |
0 commit comments