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