Spicy
coercer.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <utility>
7 
8 #include <hilti/rt/3rdparty/ArticleEnumClass-v2/EnumClass.h>
9 
10 #include <hilti/ast/ctor.h>
11 #include <hilti/ast/expression.h>
12 #include <hilti/ast/operator.h>
13 #include <hilti/ast/type.h>
14 #include <hilti/base/util.h>
15 
16 namespace hilti {
17 
19 enum class CoercionStyle {
24  Assignment = (1U << 0U),
25 
30  FunctionCall = (1U << 2U),
31 
36  TryExactMatch = (1U << 3U),
37 
42  TryConstPromotion = (1U << 4U),
43 
49  TryCoercion = (1U << 5U),
50 
57  TryCoercionWithinSameType = (1U << 6U),
58 
60  DisallowTypeChanges = (1U << 7U),
61 
68  ContextualConversion = (1U << 8U),
69 
71  TryDeref = (1U << 9U),
72 
77  TryAllForAssignment = Assignment | TryExactMatch | TryConstPromotion | TryCoercion | TryDeref,
78 
83  TryDirectForMatching = TryExactMatch | TryConstPromotion,
84 
89  TryAllForMatching = TryDirectForMatching | TryCoercion,
90 
95  TryDirectMatchForFunctionCall = FunctionCall | TryExactMatch | TryConstPromotion,
96 
101  TryAllForFunctionCall = TryDirectMatchForFunctionCall | TryCoercion | TryDeref,
102 };
103 
108 extern std::string to_string(bitmask<CoercionStyle> style);
109 
110 } // namespace hilti
111 
112 enableEnumClassBitmask(hilti::CoercionStyle); // Must be in global scope
113 
114 namespace hilti {
115 
119  operator bool() const { return coerced.hasValue(); }
120 
127 
133 
139  bool consider_type_changed = false;
140 
155 
164  : coerced(coerced),
165  nexpr(coerced),
166  consider_type_changed(src->type()->typeClass() != coerced->type()->type()->typeClass()) {}
167 
169  CoercedExpression() = default;
170 
175  CoercedExpression(const result::Error& error) : coerced(error) {}
176 };
177 
197 CoercedExpression coerceExpression(Builder* builder, Expression* e, QualifiedType* dst,
198  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment, bool lhs = false);
199 
221 CoercedExpression coerceExpression(Builder* builder, Expression* e, QualifiedType* src_, QualifiedType* dst_,
222  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment, bool lhs = false);
223 
243 Result<std::pair<bool, Expressions>> coerceOperands(Builder* builder, operator_::Kind kind, const Expressions& exprs,
244  const operator_::Operands& operands, bitmask<CoercionStyle> style);
245 
257 Result<Ctor*> coerceCtor(Builder* builder, Ctor* c, QualifiedType* dst,
258  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
259 
271 Result<QualifiedType*> coerceType(Builder* builder, QualifiedType* src_, QualifiedType* dst_,
272  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
273 
274 
275 namespace coercer::detail {
276 
278 Ctor* coerceCtor(Builder* builder, Ctor* c, QualifiedType* dst, bitmask<CoercionStyle> style);
279 
281 QualifiedType* coerceType(Builder* builder, QualifiedType* t, QualifiedType* dst, bitmask<CoercionStyle> style);
282 
283 } // namespace coercer::detail
284 
285 } // namespace hilti
Definition: expression.h:15
Definition: type.h:362
Definition: result.h:71
Definition: result.h:18
Definition: coercer.h:117
CoercedExpression(QualifiedType *src, Expression *coerced)
Definition: coercer.h:163
Result< Expression * > coerced
Definition: coercer.h:126
CoercedExpression(const result::Error &error)
Definition: coercer.h:175
bool consider_type_changed
Definition: coercer.h:139
CoercedExpression(Expression *src)
Definition: coercer.h:154
Expression * nexpr
Definition: coercer.h:132