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 } // namespace detail
52 
53 STANDARD_OPERATOR_1(unsigned_integer, DecrPostfix, operator_::sameTypeAs(0, "uint"),
54  type::UnsignedInteger(type::Wildcard()), "Decrements the value, returning the old value.");
55 STANDARD_OPERATOR_1(unsigned_integer, DecrPrefix, operator_::sameTypeAs(0, "uint"),
56  type::UnsignedInteger(type::Wildcard()), "Increments the value, returning the new value.");
57 STANDARD_OPERATOR_1(unsigned_integer, IncrPostfix, operator_::sameTypeAs(0, "uint"),
58  type::UnsignedInteger(type::Wildcard()), "Increments the value, returning the old value.");
59 STANDARD_OPERATOR_1(unsigned_integer, IncrPrefix, operator_::sameTypeAs(0, "uint"),
60  type::UnsignedInteger(type::Wildcard()), "Increments the value, returning the new value.");
61 STANDARD_OPERATOR_1(unsigned_integer, Negate, operator_::sameTypeAs(0, "uint"), type::UnsignedInteger(type::Wildcard()),
62  "Computes the bit-wise negation of the integer.");
63 STANDARD_OPERATOR_2(unsigned_integer, BitAnd, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
64  detail::widestTypeUnsigned(), "Computes the bit-wise 'and' of the two integers.");
65 STANDARD_OPERATOR_2(unsigned_integer, BitOr, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
66  detail::widestTypeUnsigned(), "Computes the bit-wise 'or' of the two integers.");
67 STANDARD_OPERATOR_2(unsigned_integer, BitXor, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
68  detail::widestTypeUnsigned(), "Computes the bit-wise 'xor' of the two integers.");
69 STANDARD_OPERATOR_2(unsigned_integer, Difference, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
70  detail::widestTypeUnsigned(), "Computes the difference between the two integers.");
71 STANDARD_OPERATOR_2(unsigned_integer, DifferenceAssign, operator_::sameTypeAs(0, "uint"),
72  type::UnsignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "uint"),
73  "Decrements the first value by the second.");
74 STANDARD_OPERATOR_2(unsigned_integer, Division, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
75  detail::widestTypeUnsigned(), "Divides the first integer by the second.");
76 STANDARD_OPERATOR_2(unsigned_integer, DivisionAssign, operator_::sameTypeAs(0, "uint"),
77  type::UnsignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "uint"),
78  "Divides the first value by the second, assigning the new value.");
79 STANDARD_OPERATOR_2(unsigned_integer, Equal, type::Bool(), detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
80  "Compares the two integers.");
81 STANDARD_OPERATOR_2(unsigned_integer, Greater, type::Bool(), detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
82  "Compares the two integers.");
83 STANDARD_OPERATOR_2(unsigned_integer, GreaterEqual, type::Bool(), detail::widestTypeUnsigned(),
84  detail::widestTypeUnsigned(), "Compares the two integers.");
85 STANDARD_OPERATOR_2(unsigned_integer, Lower, type::Bool(), detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
86  "Compares the two integers.");
87 STANDARD_OPERATOR_2(unsigned_integer, LowerEqual, type::Bool(), detail::widestTypeUnsigned(),
88  detail::widestTypeUnsigned(), "Compares the two integers.");
89 STANDARD_OPERATOR_2(unsigned_integer, Modulo, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
90  detail::widestTypeUnsigned(), "Computes the modulus of the first integer divided by the second.");
91 STANDARD_OPERATOR_2(unsigned_integer, Multiple, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
92  detail::widestTypeUnsigned(), "Multiplies the first integer by the second.");
93 STANDARD_OPERATOR_2(unsigned_integer, MultipleAssign, operator_::sameTypeAs(0, "uint"),
94  type::UnsignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "uint"),
95  "Multiplies the first value by the second, assigning the new value.");
96 STANDARD_OPERATOR_2(unsigned_integer, Power, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
97  detail::widestTypeUnsigned(), "Computes the first integer raised to the power of the second.");
98 STANDARD_OPERATOR_2(unsigned_integer, ShiftLeft, operator_::sameTypeAs(0, "uint"),
99  type::UnsignedInteger(type::Wildcard()), type::UnsignedInteger(type::Wildcard()),
100  "Shifts the integer to the left by the given number of bits.");
101 STANDARD_OPERATOR_2(unsigned_integer, ShiftRight, operator_::sameTypeAs(0, "uint"),
102  type::UnsignedInteger(type::Wildcard()), type::UnsignedInteger(type::Wildcard()),
103  "Shifts the integer to the right by the given number of bits.");
104 STANDARD_OPERATOR_2(unsigned_integer, Sum, detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
105  detail::widestTypeUnsigned(), "Computes the sum of the integers.");
106 STANDARD_OPERATOR_2(unsigned_integer, SumAssign, operator_::sameTypeAs(0, "uint"),
107  type::UnsignedInteger(type::Wildcard()), operator_::sameTypeAs(0, "uint"),
108  "Increments the first integer by the second.");
109 STANDARD_OPERATOR_2(unsigned_integer, Unequal, type::Bool(), detail::widestTypeUnsigned(), detail::widestTypeUnsigned(),
110  "Compares the two integers.");
111 STANDARD_OPERATOR_2x(unsigned_integer, CastToUnsigned, Cast, operator_::typedType(1, "uint<*>"),
112  type::UnsignedInteger(type::Wildcard()), type::Type_(type::UnsignedInteger(type::Wildcard())),
113  "Converts the value into another unsigned integer type, accepting any loss of information.");
114 STANDARD_OPERATOR_2x(unsigned_integer, CastToSigned, Cast, operator_::typedType(1, "int<*>"),
115  type::UnsignedInteger(type::Wildcard()), type::Type_(type::SignedInteger(type::Wildcard())),
116  "Converts the value into a signed integer type, accepting any loss of information.");
117 STANDARD_OPERATOR_2x(unsigned_integer, CastToReal, Cast, type::Real(), type::UnsignedInteger(type::Wildcard()),
118  type::Type_(type::Real()), "Converts the value into a real, accepting any loss of information.");
119 STANDARD_OPERATOR_2x(unsigned_integer, CastToEnum, Cast, operator_::typedType(1, "enum<*>"),
120  type::UnsignedInteger(type::Wildcard()), type::Type_(type::Enum(type::Wildcard())),
121  "Converts the value into an enum instance. The value does *not* need to correspond to "
122  "any of the target type's enumerator labels. It must not be larger than the maximum that a "
123  "*signed* 64-bit integer value can represent.");
124 STANDARD_OPERATOR_2x(unsigned_integer, CastToTime, Cast, type::Time(), type::UnsignedInteger(type::Wildcard()),
125  type::Type_(type::Time()), "Interprets the value as number of seconds since the UNIX epoch.");
126 STANDARD_OPERATOR_2x(unsigned_integer, CastToInterval, Cast, type::Interval(), type::UnsignedInteger(type::Wildcard()),
127  type::Type_(type::Interval()), "Interprets the value as number of seconds.");
128 } // namespace hilti::operator_
Definition: node.h:37
Definition: operator-registry.h:15