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  auto signature() const {
31  return Signature{.args = {{.id = "value", .type = 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  }
36 END_CTOR
37 
38 BEGIN_CTOR(enum_, CtorUnsigned)
39  auto ctorType() const { return type::Enum(type::Wildcard()); }
40 
41  auto signature() const {
42  return Signature{.args = {{.id = "value", .type = type::UnsignedInteger(type::Wildcard())}}, .doc = R"(
43 Instantiates an enum instance initialized from an unsigned integer
44 value. The value does *not* need to correspond to any of the type's
45 enumerator labels. It must not be larger than the maximum that a
46 *signed* 64-bit integer value can represent.
47 )"};
48  }
49 END_CTOR
50 
51 BEGIN_METHOD(enum_, HasLabel)
52  auto signature() const {
53  return Signature{.self = type::constant(type::Enum(type::Wildcard())),
54  .result = type::Bool(),
55  .id = "has_label",
56  .args = {},
57  .doc = R"(
58 Returns *true* if the value of *op1* corresponds to a known enum label (other
59 than ``Undef``), as defined by it's type.
60 )"};
61  }
62 END_METHOD
63 
64 } // namespace hilti::operator_
Definition: operator-registry.h:16