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