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 
62  PreferOriginalType = (1U << 6U),
63 
65  DisallowTypeChanges = (1U << 7U),
66 
73  ContextualConversion = (1U << 8U),
74 
76  _Recursing = (1U << 10U),
77 
82  TryAllForAssignment = (1U << 0U) | (1U << 3U) | (1U << 4U) | (1U << 5U) | (1U << 6U),
83 
88  TryAllForMatching = (1U << 1U) | (1U << 3U) | (1U << 4U) | (1U << 5U) | (1U << 6U),
89 
94  TryDirectMatchForFunctionCall = (1U << 2U) | (1U << 3U) | (1U << 4U) | (1U << 6U),
95 
100  TryAllForFunctionCall = (1U << 2U) | (1U << 3U) | (1U << 4U) | (1U << 5U) | (1U << 6U),
101 
106  TryDirectForMatching = (1U << 1U) | (1U << 3U) | (1U << 4U) | (1U << 6U)
107 };
108 
113 extern std::string to_string(bitmask<CoercionStyle> style);
114 
115 } // namespace hilti
116 
117 enableEnumClassBitmask(hilti::CoercionStyle); // Must be in global scope
118 
119 namespace hilti {
120 
124  operator bool() const { return coerced.hasValue(); }
125 
131  Result<Expression> coerced = {};
132 
137  std::optional<Expression> nexpr = {};
138 
144  bool consider_type_changed = false;
145 
159  CoercedExpression(const Expression& src) : coerced(src) {}
160 
168  CoercedExpression(Type src, Expression coerced)
169  : coerced(coerced),
170  nexpr(coerced),
171  consider_type_changed(type::effectiveType(std::move(src)).typename_() !=
172  type::effectiveType(coerced.type()).typename_()) {}
173 
175  CoercedExpression() = default;
176 
181  CoercedExpression(const result::Error& error) : coerced(error) {}
182 };
183 
202 CoercedExpression coerceExpression(const Expression& e, const Type& dst,
203  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
204 
225 CoercedExpression coerceExpression(const Expression& e, const Type& src_, const Type& dst_,
226  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
227 
245 Result<std::pair<bool, std::vector<Expression>>> coerceOperands(const std::vector<Expression>& exprs,
246  const std::vector<operator_::Operand>& operands,
247  bitmask<CoercionStyle> style);
248 
259 Result<Ctor> coerceCtor(Ctor c, const Type& dst, bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
260 
271 Result<Type> coerceType(const Type& src_, const Type& dst_,
272  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
273 
274 
275 } // namespace hilti
Definition: result.h:18
CoercedExpression(Type src, Expression coerced)
Definition: coercion.h:168
Definition: optional.h:79
CoercedExpression(const Expression &src)
Definition: coercion.h:159
Definition: coercion.h:122
CoercedExpression(const result::Error &error)
Definition: coercion.h:181
Definition: result.h:67