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> 20 inline static auto widestTypeSigned() {
23 if ( orig_ops.empty() && resolved_ops.empty() )
24 return type::DocOnly(
"int<*>");
29 if (
auto t = orig_ops[0].type().tryAs<type::SignedInteger>() )
31 else if (
auto t = orig_ops[0].type().tryAs<type::UnsignedInteger>() )
34 if (
auto t = orig_ops[1].type().tryAs<type::SignedInteger>() )
36 else if (
auto t = orig_ops[1].type().tryAs<type::UnsignedInteger>() )
42 const bool is_ctor1 = orig_ops[0].isA<expression::Ctor>();
43 const bool is_ctor2 = orig_ops[1].isA<expression::Ctor>();
45 if ( is_ctor1 && ! is_ctor2 )
46 return type::SignedInteger(w2);
48 if ( is_ctor2 && ! is_ctor1 )
49 return type::SignedInteger(w1);
51 return type::SignedInteger(std::max(w1, w2));
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.");