Spicy
unsigned-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 
14 namespace hilti {
15 namespace operator_ {
16 
17 namespace detail {
18 inline static auto widestTypeUnsigned() {
19  return [=](const hilti::node::Range<Expression>& orig_ops,
20  const hilti::node::Range<Expression>& resolved_ops) -> std::optional<Type> {
21  if ( orig_ops.empty() && resolved_ops.empty() )
22  return type::DocOnly("uint<*>");
23 
24  int w1 = 0;
25  int w2 = 0;
26 
27  if ( auto t = orig_ops[0].type().tryAs<type::SignedInteger>() )
28  w1 = t->width();
29  else if ( auto t = orig_ops[0].type().tryAs<type::UnsignedInteger>() )
30  w1 = t->width();
31 
32  if ( auto t = orig_ops[1].type().tryAs<type::SignedInteger>() )
33  w2 = t->width();
34  else if ( auto t = orig_ops[1].type().tryAs<type::UnsignedInteger>() )
35  w2 = t->width();
36 
37  if ( ! (w1 && w2) )
38  return {};
39 
40  const bool is_ctor1 = orig_ops[0].isA<expression::Ctor>();
41  const bool is_ctor2 = orig_ops[1].isA<expression::Ctor>();
42 
43  if ( is_ctor1 && ! is_ctor2 )
44  return type::UnsignedInteger(w2);
45 
46  if ( is_ctor2 && ! is_ctor1 )
47  return type::UnsignedInteger(w1);
48 
49  return type::UnsignedInteger(std::max(w1, w2));
50  };
51 }
52 } // namespace detail
53 
54 STANDARD_OPERATOR_1(unsigned_integer, DecrPostfix, operator_::sameTypeAs(0, "uint"),
55  type::UnsignedInteger(type::Wildcard()), "Decrements the value, returning the old value.");
56 STANDARD_OPERATOR_1(unsigned_integer, DecrPrefix, operator_::sameTypeAs(0, "uint"),
57  type::UnsignedInteger(type::Wildcard()), "Increments the value, returning the new value.");
58 STANDARD_OPERATOR_1(unsigned_integer, IncrPostfix, operator_::sameTypeAs(0, "uint"),
59  type::UnsignedInteger(type::Wildcard()), "Increments the value, returning the old value.");
60 STANDARD_OPERATOR_1(unsigned_integer, IncrPrefix, operator_::sameTypeAs(0, "uint"),
61  type::UnsignedInteger(type::Wildcard()), "Increments the value, returning the new value.");
62 STANDARD_OPERATOR_1(unsigned_integer, Negate, operator_::sameTypeAs(0, "uint"), type::UnsignedInteger(type::Wildcard()),
63  "Computes the bit-wise negation of the integer.");
64 STANDARD_OPERATOR_2(unsigned_integer, BitAnd, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
65  detail::widestTypeUnsigned(), "Computes the bit-wise 'and' of the two integers.");
66 STANDARD_OPERATOR_2(unsigned_integer, BitOr, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
67  detail::widestTypeUnsigned(), "Computes the bit-wise 'or' of the two integers.");
68 STANDARD_OPERATOR_2(unsigned_integer, BitXor, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
69  detail::widestTypeUnsigned(), "Computes the bit-wise 'xor' of the two integers.");
70 STANDARD_OPERATOR_2(unsigned_integer, Difference, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
71  detail::widestTypeUnsigned(), "Computes the difference between the two integers.");
72 STANDARD_OPERATOR_2(unsigned_integer, DifferenceAssign, operator_::sameTypeAs(0, "uint"),
73  type::UnsignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "uint"),
74  "Decrements the first value by the second.");
75 STANDARD_OPERATOR_2(unsigned_integer, Division, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
76  detail::widestTypeUnsigned(), "Divides the first integer by the second.");
77 STANDARD_OPERATOR_2(unsigned_integer, DivisionAssign, operator_::sameTypeAs(0, "uint"),
78  type::UnsignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "uint"),
79  "Divides the first value by the second, assigning the new value.");
80 STANDARD_OPERATOR_2(unsigned_integer, Equal, type::Bool(), detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
81  "Compares the two integers.");
82 STANDARD_OPERATOR_2(unsigned_integer, Greater, type::Bool(), detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
83  "Compares the two integers.");
84 STANDARD_OPERATOR_2(unsigned_integer, GreaterEqual, type::Bool(), detail::widestTypeUnsigned(),
85  detail::widestTypeUnsigned(), "Compares the two integers.");
86 STANDARD_OPERATOR_2(unsigned_integer, Lower, type::Bool(), detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
87  "Compares the two integers.");
88 STANDARD_OPERATOR_2(unsigned_integer, LowerEqual, type::Bool(), detail::widestTypeUnsigned(),
89  detail::widestTypeUnsigned(), "Compares the two integers.");
90 STANDARD_OPERATOR_2(unsigned_integer, Modulo, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
91  detail::widestTypeUnsigned(), "Computes the modulus of the first integer divided by the second.");
92 STANDARD_OPERATOR_2(unsigned_integer, Multiple, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
93  detail::widestTypeUnsigned(), "Multiplies the first integer by the second.");
94 STANDARD_OPERATOR_2(unsigned_integer, MultipleAssign, operator_::sameTypeAs(0, "uint"),
95  type::UnsignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "uint"),
96  "Multiplies the first value by the second, assigning the new value.");
97 STANDARD_OPERATOR_2(unsigned_integer, Power, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
98  detail::widestTypeUnsigned(), "Computes the first integer raised to the power of the second.");
99 STANDARD_OPERATOR_2(unsigned_integer, ShiftLeft, operator_::sameTypeAs(0, "uint"),
100  type::UnsignedInteger(type::Wildcard()), type::UnsignedInteger(type::Wildcard()),
101  "Shifts the integer to the left by the given number of bits.");
102 STANDARD_OPERATOR_2(unsigned_integer, ShiftRight, operator_::sameTypeAs(0, "uint"),
103  type::UnsignedInteger(type::Wildcard()), type::UnsignedInteger(type::Wildcard()),
104  "Shifts the integer to the right by the given number of bits.");
105 STANDARD_OPERATOR_2(unsigned_integer, Sum, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
106  detail::widestTypeUnsigned(), "Computes the sum of the integers.");
107 STANDARD_OPERATOR_2(unsigned_integer, SumAssign, operator_::sameTypeAs(0, "uint"),
108  type::UnsignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "uint"),
109  "Increments the first integer by the second.");
110 STANDARD_OPERATOR_2(unsigned_integer, Unequal, type::Bool(), detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
111  "Compares the two integers.");
112 STANDARD_OPERATOR_2x(unsigned_integer, CastToUnsigned, Cast, operator_::typedType(1, "uint<*>"),
113  type::UnsignedInteger(type::Wildcard()), type::Type_(type::UnsignedInteger(type::Wildcard())),
114  "Converts the value into another unsigned integer type, accepting any loss of information.");
115 STANDARD_OPERATOR_2x(unsigned_integer, CastToSigned, Cast, operator_::typedType(1, "int<*>"),
116  type::UnsignedInteger(type::Wildcard()), type::Type_(type::SignedInteger(type::Wildcard())),
117  "Converts the value into a signed integer type, accepting any loss of information.");
118 STANDARD_OPERATOR_2x(unsigned_integer, CastToReal, Cast, type::Real(), type::UnsignedInteger(type::Wildcard()),
119  type::Type_(type::Real()), "Converts the value into a real, accepting any loss of information.");
120 STANDARD_OPERATOR_2x(unsigned_integer, CastToEnum, Cast, operator_::typedType(1, "enum<*>"),
121  type::UnsignedInteger(type::Wildcard()), type::Type_(type::Enum(type::Wildcard())),
122  "Converts the value into an enum instance. The value does *not* need to correspond to "
123  "any of the target type's enumerator labels. It must not be larger than the maximum that a "
124  "*signed* 64-bit integer value can represent.");
125 STANDARD_OPERATOR_2x(unsigned_integer, CastToTime, Cast, type::Time(), type::UnsignedInteger(type::Wildcard()),
126  type::Type_(type::Time()), "Interprets the value as number of seconds since the UNIX epoch.");
127 STANDARD_OPERATOR_2x(unsigned_integer, CastToInterval, Cast, type::Interval(), type::UnsignedInteger(type::Wildcard()),
128  type::Type_(type::Interval()), "Interprets the value as number of seconds.");
129 } // namespace operator_
130 } // namespace hilti
Definition: node.h:39