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,
198  Expression* e,
199  QualifiedType* dst,
200  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment,
201  bool lhs = false);
202 
224 CoercedExpression coerceExpression(Builder* builder,
225  Expression* e,
226  QualifiedType* src_,
227  QualifiedType* dst_,
228  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment,
229  bool lhs = false);
230 
250 Result<std::pair<bool, Expressions>> coerceOperands(Builder* builder,
251  operator_::Kind kind,
252  const Expressions& exprs,
253  const operator_::Operands& operands,
254  bitmask<CoercionStyle> style);
255 
267 Result<Ctor*> coerceCtor(Builder* builder,
268  Ctor* c,
269  QualifiedType* dst,
270  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
271 
283 Result<QualifiedType*> coerceType(Builder* builder,
284  QualifiedType* src_,
285  QualifiedType* dst_,
286  bitmask<CoercionStyle> style = CoercionStyle::TryAllForAssignment);
287 
288 namespace coercer::detail {
289 
291 Ctor* coerceCtor(Builder* builder, Ctor* c, QualifiedType* dst, bitmask<CoercionStyle> style);
292 
294 QualifiedType* coerceType(Builder* builder, QualifiedType* t, QualifiedType* dst, bitmask<CoercionStyle> style);
295 
296 } // namespace coercer::detail
297 
298 } // namespace hilti
Definition: expression.h:15
Definition: type.h:365
Definition: result.h:73
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