Spicy
enum.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <hilti/ast/operators/common.h>
6 #include <hilti/ast/types/bool.h>
7 #include <hilti/ast/types/enum.h>
8 #include <hilti/ast/types/integer.h>
9 #include <hilti/ast/types/type.h>
10 #include <hilti/base/logger.h>
11 
12 namespace hilti::operator_ {
13 
14 STANDARD_OPERATOR_2(enum_, Equal, type::Bool(), type::constant(type::Enum(type::Wildcard())),
15  operator_::sameTypeAs(0, "enum<*>"), "Compares two enum values.");
16 STANDARD_OPERATOR_2(enum_, Unequal, type::Bool(), type::constant(type::Enum(type::Wildcard())),
17  operator_::sameTypeAs(0, "enum<*>"), "Compares two enum values.");
18 STANDARD_OPERATOR_2x(
19  enum_, CastToSignedInteger, Cast, operator_::typedType(1, "int"), type::Enum(type::Wildcard()),
20  type::Type_(type::SignedInteger(type::Wildcard())),
21  "Casts an enum value into a signed integer. If the enum value is ``Undef``, this will return ``-1``.");
22 STANDARD_OPERATOR_2x(
23  enum_, CastToUnsignedInteger, Cast, operator_::typedType(1, "uint"), type::Enum(type::Wildcard()),
24  type::Type_(type::UnsignedInteger(type::Wildcard())),
25  "Casts an enum value into a unsigned integer. This will throw an exception if the enum value is ``Undef``.");
26 
27 BEGIN_CTOR(enum_, CtorSigned)
28  auto ctorType() const { return type::Enum(type::Wildcard()); }
29 
30  const auto& signature() const {
31  static auto _signature = Signature{.args = {{"value", type::SignedInteger(type::Wildcard())}}, .doc = R"(
32 Instantiates an enum instance initialized from a signed integer value. The value does
33 *not* need to correspond to any of the type's enumerator labels.
34 )"};
35  return _signature;
36  }
37 END_CTOR
38 
39 BEGIN_CTOR(enum_, CtorUnsigned)
40  auto ctorType() const { return type::Enum(type::Wildcard()); }
41 
42  const auto& signature() const {
43  static auto _signature = Signature{.args = {{"value", type::UnsignedInteger(type::Wildcard())}}, .doc = R"(
44 Instantiates an enum instance initialized from an unsigned integer
45 value. The value does *not* need to correspond to any of the type's
46 enumerator labels. It must not be larger than the maximum that a
47 *signed* 64-bit integer value can represent.
48 )"};
49  return _signature;
50  }
51 END_CTOR
52 
53 BEGIN_METHOD(enum_, HasLabel)
54  const auto& signature() const {
55  static auto _signature = Signature{.self = type::constant(type::Enum(type::Wildcard())),
56  .result = type::Bool(),
57  .id = "has_label",
58  .args = {},
59  .doc = R"(
60 Returns *true* if the value of *op1* corresponds to a known enum label (other
61 than ``Undef``), as defined by it's type.
62 )"};
63  return _signature;
64  }
65 END_METHOD
66 
67 } // namespace hilti::operator_
Definition: operator-registry.h:15