value_transform.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_EXPLORER_AST_VALUE_TRANSFORM_H_
  5. #define CARBON_EXPLORER_AST_VALUE_TRANSFORM_H_
  6. #include "common/error.h"
  7. #include "explorer/ast/value.h"
  8. namespace Carbon {
  9. template <typename T, typename, typename... Args>
  10. constexpr bool is_list_constructible_impl = false;
  11. template <typename T, typename... Args>
  12. constexpr bool is_list_constructible_impl<
  13. T, decltype(T{std::declval<Args>()...}), Args...> = true;
  14. // A no-op visitor used to implement `IsRecursivelyTransformable`. The
  15. // `operator()` function returns `true_type` if it's called with arguments that
  16. // can be used to direct-list-initialize `T`, and `false_type` otherwise.
  17. template <typename T>
  18. struct IsRecursivelyTransformableVisitor {
  19. template <typename... Args>
  20. auto operator()(Args&&... args)
  21. -> std::integral_constant<bool,
  22. is_list_constructible_impl<T, T, Args...>>;
  23. };
  24. // A type trait that indicates whether `T` is transformable. A transformable
  25. // type provides a function
  26. //
  27. // template<typename F> void Decompose(F f) const;
  28. //
  29. // that takes a callable `f` and passes it an argument list that can be passed
  30. // to the constructor of `T` to create an equivalent value.
  31. template <typename T, typename = std::true_type>
  32. constexpr bool IsRecursivelyTransformable = false;
  33. template <typename T>
  34. // NOLINTNEXTLINE(misc-definitions-in-headers)
  35. constexpr bool IsRecursivelyTransformable<
  36. T, decltype(std::declval<const T>().Decompose(
  37. IsRecursivelyTransformableVisitor<T>{}))> = true;
  38. // Unwrapper for the case where there's nothing to unwrap.
  39. class NoOpUnwrapper {
  40. public:
  41. template <typename T, typename U>
  42. auto UnwrapOr(T&& value, const U&) -> T {
  43. return std::forward<T>(value);
  44. }
  45. template <typename T>
  46. auto Wrap(T&& value) -> T&& {
  47. return std::forward<T>(value);
  48. }
  49. constexpr bool failed() const { return false; }
  50. };
  51. // Helper to temporarily unwrap the ErrorOr around a value, and then put it
  52. // back when we're done with the overall computation.
  53. class ErrorUnwrapper {
  54. public:
  55. // Unwrap the `ErrorOr` from the given value, or collect the error and return
  56. // the given fallback value on failure.
  57. template <typename T, typename U>
  58. auto UnwrapOr(ErrorOr<T> value, const U& fallback) -> T {
  59. if (!value.ok()) {
  60. status_ = std::move(value).error();
  61. return fallback;
  62. }
  63. return std::move(*value);
  64. }
  65. template <typename T, typename U>
  66. auto UnwrapOr(T&& value, const U&) -> T {
  67. return std::forward<T>(value);
  68. }
  69. // Wrap the given value into `ErrorOr`, returning our collected error if any,
  70. // or the given value if we succeeded.
  71. template <typename T>
  72. auto Wrap(T&& value) -> ErrorOr<T> {
  73. if (!status_.ok()) {
  74. Error error = std::move(status_).error();
  75. status_ = Success();
  76. return error;
  77. }
  78. return std::forward<T>(value);
  79. }
  80. bool failed() const { return !status_.ok(); }
  81. private:
  82. ErrorOr<Success> status_ = Success();
  83. };
  84. // Base class for transforms of visitable data types.
  85. template <typename Derived, typename ResultUnwrapper>
  86. class TransformBase {
  87. public:
  88. explicit TransformBase(Nonnull<Arena*> arena) : arena_(arena) {}
  89. // Transform the given value, and produce either the transformed value or an
  90. // error.
  91. template <typename T>
  92. auto Transform(const T& v) -> decltype(auto) {
  93. return unwrapper_.Wrap(TransformOrOriginal(v));
  94. }
  95. protected:
  96. // Transform the given value, or return the original if transformation fails.
  97. template <typename T>
  98. auto TransformOrOriginal(const T& v)
  99. -> decltype(std::declval<ResultUnwrapper>().UnwrapOr(
  100. std::declval<Derived>()(v), v)) {
  101. // If we've already failed, don't do any more transformations.
  102. if (unwrapper_.failed()) {
  103. return v;
  104. }
  105. return unwrapper_.UnwrapOr(static_cast<Derived&>(*this)(v), v);
  106. }
  107. // Transformable values are recursively transformed by default.
  108. template <typename T,
  109. std::enable_if_t<IsRecursivelyTransformable<T>, void*> = nullptr>
  110. auto operator()(const T& value) -> T {
  111. return value.Decompose([&](const auto&... elements) {
  112. return [&](auto&&... transformed_elements) {
  113. if (unwrapper_.failed()) {
  114. return value;
  115. }
  116. return T{decltype(transformed_elements)(transformed_elements)...};
  117. }(TransformOrOriginal(elements)...);
  118. });
  119. }
  120. // Transformable pointers are recursively transformed and reallocated by
  121. // default.
  122. template <typename T,
  123. std::enable_if_t<IsRecursivelyTransformable<T>, void*> = nullptr>
  124. auto operator()(Nonnull<const T*> value) -> auto {
  125. return value->Decompose([&](const auto&... elements) {
  126. return [&](auto&&... transformed_elements)
  127. -> decltype(AllocateTrait<T>::New(
  128. arena_,
  129. decltype(transformed_elements)(transformed_elements)...)) {
  130. if (unwrapper_.failed()) {
  131. return value;
  132. }
  133. return AllocateTrait<T>::New(
  134. arena_, decltype(transformed_elements)(transformed_elements)...);
  135. }(TransformOrOriginal(elements)...);
  136. });
  137. }
  138. // Fundamental types like `int` are assumed to not need transformation.
  139. template <typename T>
  140. auto operator()(const T& v) -> std::enable_if_t<std::is_fundamental_v<T>, T> {
  141. return v;
  142. }
  143. auto operator()(const std::string& str) -> const std::string& { return str; }
  144. auto operator()(llvm::StringRef str) -> llvm::StringRef { return str; }
  145. // Transform `optional<T>` by transforming the `T` if it's present.
  146. template <typename T>
  147. auto operator()(const std::optional<T>& v) -> std::optional<T> {
  148. if (!v) {
  149. return std::nullopt;
  150. }
  151. return TransformOrOriginal(*v);
  152. }
  153. // Transform `pair<T, U>` by transforming T and U.
  154. template <typename T, typename U>
  155. auto operator()(const std::pair<T, U>& pair) -> std::pair<T, U> {
  156. return std::pair<T, U>{TransformOrOriginal(pair.first),
  157. TransformOrOriginal(pair.second)};
  158. }
  159. // Transform `vector<T>` by transforming its elements.
  160. template <typename T>
  161. auto operator()(const std::vector<T>& vec) -> std::vector<T> {
  162. std::vector<T> result;
  163. result.reserve(vec.size());
  164. for (auto& value : vec) {
  165. result.push_back(TransformOrOriginal(value));
  166. }
  167. return result;
  168. }
  169. // Transform `map<T, U>` by transforming its keys and values.
  170. template <typename T, typename U>
  171. auto operator()(const std::map<T, U>& map) -> std::map<T, U> {
  172. std::map<T, U> result;
  173. for (auto& [key, value] : map) {
  174. result.insert({TransformOrOriginal(key), TransformOrOriginal(value)});
  175. }
  176. return result;
  177. }
  178. // Transform `llvm::StringMap<T>` by transforming its keys and values.
  179. template <typename T>
  180. auto operator()(const llvm::StringMap<T>& map) -> llvm::StringMap<T> {
  181. llvm::StringMap<T> result;
  182. for (const auto& it : map) {
  183. result.insert(
  184. {TransformOrOriginal(it.first()), TransformOrOriginal(it.second)});
  185. }
  186. return result;
  187. }
  188. private:
  189. Nonnull<Arena*> arena_;
  190. // Unwrapper for results. Used to remove an ErrorOr<...> wrapper temporarily
  191. // during recursive transformations and re-apply it when we're done.
  192. ResultUnwrapper unwrapper_;
  193. };
  194. // Base class for transforms of `Value`s.
  195. template <typename Derived, typename ResultUnwrapper>
  196. class ValueTransform : public TransformBase<Derived, ResultUnwrapper> {
  197. public:
  198. using TransformBase<Derived, ResultUnwrapper>::TransformBase;
  199. using TransformBase<Derived, ResultUnwrapper>::operator();
  200. // Leave references to AST nodes alone by default.
  201. // The 'int = 0' parameter avoids this function hiding the `operator()(const
  202. // T*)` in the base class. We can remove this once we start using a compiler
  203. // that implements P1787R6.
  204. template <typename NodeT>
  205. auto operator()(Nonnull<const NodeT*> node, int /*unused*/ = 0)
  206. -> std::enable_if_t<std::is_base_of_v<AstNode, NodeT>,
  207. Nonnull<const NodeT*>> {
  208. return node;
  209. }
  210. auto operator()(Address addr) -> Address { return addr; }
  211. auto operator()(ValueNodeView value_node) -> ValueNodeView {
  212. return value_node;
  213. }
  214. // For a type that provides a `Visit` function to visit the most-derived
  215. // object, visit and transform that most-derived object.
  216. template <typename R, typename T>
  217. auto TransformDerived(Nonnull<const T*> value) -> R {
  218. return value->template Visit<R>([&](const auto* derived_value) {
  219. using DerivedType = std::remove_pointer_t<decltype(derived_value)>;
  220. static_assert(IsRecursivelyTransformable<DerivedType>);
  221. return this->TransformOrOriginal(derived_value);
  222. });
  223. }
  224. // For values, dispatch on the value kind and recursively transform.
  225. auto operator()(Nonnull<const Value*> value) -> Nonnull<const Value*> {
  226. return TransformDerived<Nonnull<const Value*>>(value);
  227. }
  228. // Provide a more precise type from transforming a `Witness`.
  229. auto operator()(Nonnull<const Witness*> value) -> Nonnull<const Witness*> {
  230. return llvm::cast<Witness>(
  231. this->TransformOrOriginal(llvm::cast<Value>(value)));
  232. }
  233. // For elements, dispatch on the element kind and recursively transform.
  234. auto operator()(Nonnull<const Element*> elem) -> Nonnull<const Element*> {
  235. return TransformDerived<Nonnull<const Element*>>(elem);
  236. }
  237. // Preserve vtable during transformation.
  238. auto operator()(Nonnull<const VTable*> vtable) -> Nonnull<const VTable*> {
  239. return vtable;
  240. }
  241. // Preserve class value ptr during transformation.
  242. auto operator()(Nonnull<const NominalClassValue**> value_ptr)
  243. -> Nonnull<const NominalClassValue**> {
  244. return value_ptr;
  245. }
  246. // Preserve constraint kind for intrinsic constraints.
  247. auto operator()(IntrinsicConstraint::Kind kind) -> IntrinsicConstraint::Kind {
  248. return kind;
  249. }
  250. };
  251. } // namespace Carbon
  252. #endif // CARBON_EXPLORER_AST_VALUE_TRANSFORM_H_