Spicy
coercion.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <utility>
7 #include <vector>
8 
9 #include <hilti/rt/3rdparty/ArticleEnumClass-v2/EnumClass.h>
10 
11 #include <hilti/ast/ctor.h>
12 #include <hilti/ast/expression.h>
13 #include <hilti/ast/operator.h>
14 #include <hilti/ast/type.h>
15 #include <hilti/base/util.h>
16 
17 namespace hilti {
18 
20 enum class CoercionStyle {
25  Assignment = (1U << 0U),
26 
31  OperandMatching = (1U << 1U),
32 
37  FunctionCall = (1U << 2U),
38 
43  TryExactMatch = (1U << 3U),
44 
49  TryConstPromotion = (1U << 4U),
50 
56  TryCoercion = (1U << 5U),
57 
59  DisallowTypeChanges = (1U << 7U),
60 
67  ContextualConversion = (1U << 8U),
68 
70  _Recursing = (1U << 10U),
71 
76  TryAllForAssignment = (1U << 0U) | (1U << 3U) | (1U << 4U) | (1U << 5U) | (1U << 6U),
77 
82  TryAllForMatching = (1U << 1U) | (1U << 3U) | (1U << 4U) | (1U << 5U) | (1U << 6U),
83 
88  TryDirectMatchForFunctionCall = (1U << 2U) | (1U << 3U) | (1U << 4U) | (1U << 6U),
89 
94  TryAllForFunctionCall = (1U << 2U) | (1U << 3U) | (1U << 4U) | (1U << 5U) | (1U << 6U),
95 
100  TryDirectForMatching = (1U << 1U) | (1U << 3U) | (1U << 4U) | (1U << 6U)
101 };
102 
107 extern std::string to_string(bitmask<CoercionStyle> style);
108 
109 } // namespace hilti
110 
111 enableEnumClassBitmask(hilti::CoercionStyle); // Must be in global scope
112 
113 namespace hilti {
114 
118  operator bool() const { return coerced.hasValue(); }
119 
125  Result<Expression> coerced = {};
126 
131  std::optional<Expression> nexpr = {};
132 
138  bool consider_type_changed = false;
139 
153  CoercedExpression(const Expression& src) : coerced(src) {}
154 
162  CoercedExpression(Type src, Expression coerced)
163  : coerced(coerced),
164  nexpr(coerced),
165  consider_type_changed(std::move(src).typename_() != coerced.type().typename_()) {}
166 
168  CoercedExpression() = default;
169 
174  CoercedExpression(const result::Error& error) : coerced(error) {}
175 };
176 
195 CoercedExpression coerceExpression(const Expression& e, const Type& dst,
196  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
197 
218 CoercedExpression coerceExpression(const Expression& e, const Type& src_, const Type& dst_,
219  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
220 
239  const std::vector<operator_::Operand>& operands,
240  bitmask<CoercionStyle> style);
241 
252 Result<Ctor> coerceCtor(Ctor c, const Type& dst, bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
253 
264 Result<Type> coerceType(const Type& src_, const Type& dst_,
265  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
266 
267 namespace detail {
269 std::optional<Ctor> coerceCtor(Ctor c, const Type& dst, bitmask<CoercionStyle> style);
271 std::optional<Type> coerceType(Type t, const Type& dst, bitmask<CoercionStyle> style);
272 } // namespace detail
273 
274 } // namespace hilti
Definition: result.h:18
Definition: node.h:39
CoercedExpression(Type src, Expression coerced)
Definition: coercion.h:162
Definition: optional.h:79
CoercedExpression(const Expression &src)
Definition: coercion.h:153
Definition: coercion.h:116
Definition: type.h:159
CoercedExpression(const result::Error &error)
Definition: coercion.h:174
Definition: result.h:67