Spicy
signed-integer.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <algorithm>
6 #include <vector>
7 
8 #include <hilti/ast/operators/common.h>
9 #include <hilti/ast/types/bool.h>
10 #include <hilti/ast/types/integer.h>
11 #include <hilti/ast/types/real.h>
12 #include <hilti/ast/types/string.h>
13 #include <hilti/ast/types/type.h>
14 #include <hilti/base/logger.h>
15 
16 namespace hilti {
17 namespace operator_ {
18 
19 namespace detail {
20 inline static auto widestTypeSigned() {
21  return [=](const hilti::node::Range<Expression>& orig_ops,
22  const hilti::node::Range<Expression>& resolved_ops) -> std::optional<Type> {
23  if ( orig_ops.empty() && resolved_ops.empty() )
24  return type::DocOnly("int<*>");
25 
26  int w1 = 0;
27  int w2 = 0;
28 
29  if ( auto t = orig_ops[0].type().tryAs<type::SignedInteger>() )
30  w1 = t->width();
31  else if ( auto t = orig_ops[0].type().tryAs<type::UnsignedInteger>() )
32  w1 = t->width();
33 
34  if ( auto t = orig_ops[1].type().tryAs<type::SignedInteger>() )
35  w2 = t->width();
36  else if ( auto t = orig_ops[1].type().tryAs<type::UnsignedInteger>() )
37  w2 = t->width();
38 
39  if ( ! (w1 && w2) )
40  return {};
41 
42  const bool is_ctor1 = orig_ops[0].isA<expression::Ctor>();
43  const bool is_ctor2 = orig_ops[1].isA<expression::Ctor>();
44 
45  if ( is_ctor1 && ! is_ctor2 )
46  return type::SignedInteger(w2);
47 
48  if ( is_ctor2 && ! is_ctor1 )
49  return type::SignedInteger(w1);
50 
51  return type::SignedInteger(std::max(w1, w2));
52  };
53 }
54 } // namespace detail
55 
56 STANDARD_OPERATOR_1(signed_integer, DecrPostfix, operator_::sameTypeAs(0, "int"), type::SignedInteger(type::Wildcard()),
57  "Decrements the value, returning the old value.");
58 STANDARD_OPERATOR_1(signed_integer, DecrPrefix, operator_::sameTypeAs(0, "int"), type::SignedInteger(type::Wildcard()),
59  "Increments the value, returning the new value.");
60 STANDARD_OPERATOR_1(signed_integer, IncrPostfix, operator_::sameTypeAs(0, "int"), type::SignedInteger(type::Wildcard()),
61  "Increments the value, returning the old value.");
62 STANDARD_OPERATOR_1(signed_integer, IncrPrefix, operator_::sameTypeAs(0, "int"), type::SignedInteger(type::Wildcard()),
63  "Increments the value, returning the new value.");
64 STANDARD_OPERATOR_1(signed_integer, SignNeg, operator_::sameTypeAs(0, "int"), type::SignedInteger(type::Wildcard()),
65  "Inverts the sign of the integer.");
66 STANDARD_OPERATOR_2(signed_integer, Difference, detail::widestTypeSigned(), detail::widestTypeSigned(),
67  detail::widestTypeSigned(), "Computes the difference between the two integers.");
68 STANDARD_OPERATOR_2(signed_integer, DifferenceAssign, operator_::sameTypeAs(0, "int"),
69  type::SignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "int"),
70  "Decrements the first value by the second, assigning the new value.");
71 STANDARD_OPERATOR_2(signed_integer, Division, detail::widestTypeSigned(), detail::widestTypeSigned(),
72  detail::widestTypeSigned(), "Divides the first integer by the second.");
73 STANDARD_OPERATOR_2(signed_integer, DivisionAssign, operator_::sameTypeAs(0, "int"),
74  type::SignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "int"),
75  "Divides the first value by the second, assigning the new value.");
76 STANDARD_OPERATOR_2(signed_integer, Equal, type::Bool(), detail::widestTypeSigned(), detail::widestTypeSigned(),
77  "Compares the two integers.");
78 STANDARD_OPERATOR_2(signed_integer, Greater, type::Bool(), detail::widestTypeSigned(), detail::widestTypeSigned(),
79  "Compares the two integers.");
80 STANDARD_OPERATOR_2(signed_integer, GreaterEqual, type::Bool(), detail::widestTypeSigned(), detail::widestTypeSigned(),
81  "Compares the two integers.");
82 STANDARD_OPERATOR_2(signed_integer, Lower, type::Bool(), detail::widestTypeSigned(), detail::widestTypeSigned(),
83  "Compares the two integers.");
84 STANDARD_OPERATOR_2(signed_integer, LowerEqual, type::Bool(), detail::widestTypeSigned(), detail::widestTypeSigned(),
85  "Compares the two integers.");
86 STANDARD_OPERATOR_2(signed_integer, Modulo, detail::widestTypeSigned(), detail::widestTypeSigned(),
87  detail::widestTypeSigned(), "Computes the modulus of the first integer divided by the second.");
88 STANDARD_OPERATOR_2(signed_integer, Multiple, detail::widestTypeSigned(), detail::widestTypeSigned(),
89  detail::widestTypeSigned(), "Multiplies the first integer by the second.");
90 STANDARD_OPERATOR_2(signed_integer, MultipleAssign, operator_::sameTypeAs(0, "int"),
91  type::SignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "int"),
92  "Multiplies the first value by the second, assigning the new value.");
93 STANDARD_OPERATOR_2(signed_integer, Power, detail::widestTypeSigned(), detail::widestTypeSigned(),
94  detail::widestTypeSigned(), "Computes the first integer raised to the power of the second.");
95 STANDARD_OPERATOR_2(signed_integer, Sum, detail::widestTypeSigned(), detail::widestTypeSigned(),
96  detail::widestTypeSigned(), "Computes the sum of the integers.");
97 STANDARD_OPERATOR_2(signed_integer, SumAssign, operator_::sameTypeAs(0, "int"), type::SignedInteger(type::Wildcard()),
98  operator_::sameTypeAs(0, "int"), "Increments the first integer by the second.");
99 STANDARD_OPERATOR_2(signed_integer, Unequal, type::Bool(), detail::widestTypeSigned(), detail::widestTypeSigned(),
100  "Compares the two integers.");
101 STANDARD_OPERATOR_2x(signed_integer, CastToSigned, Cast, operator_::typedType(1, "int<*>"),
102  type::SignedInteger(type::Wildcard()), type::Type_(type::SignedInteger(type::Wildcard())),
103  "Converts the value into another signed integer type, accepting any loss of information.");
104 STANDARD_OPERATOR_2x(signed_integer, CastToUnsigned, Cast, operator_::typedType(1, "uint<*>"),
105  type::SignedInteger(type::Wildcard()), type::Type_(type::UnsignedInteger(type::Wildcard())),
106  "Converts the value into an unsigned integer type, accepting any loss of information.");
107 STANDARD_OPERATOR_2x(signed_integer, CastToReal, Cast, type::Real(), type::SignedInteger(type::Wildcard()),
108  type::Type_(type::Real()), "Converts the value into a real, accepting any loss of information.");
109 STANDARD_OPERATOR_2x(signed_integer, CastToEnum, Cast, operator_::typedType(1, "enum<*>"),
110  type::SignedInteger(type::Wildcard()), type::Type_(type::Enum(type::Wildcard())),
111  "Converts the value into an enum instance. The value does *not* need to correspond to "
112  "any of the target type's enumerator labels.");
113 STANDARD_OPERATOR_2x(signed_integer, CastToInterval, Cast, type::Interval(), type::SignedInteger(type::Wildcard()),
114  type::Type_(type::Interval()), "Interprets the value as number of seconds.");
115 
116 } // namespace operator_
117 } // namespace hilti
Definition: node.h:39