9 #include <hilti/ast/operators/common.h> 10 #include <hilti/ast/types/tuple.h> 15 STANDARD_OPERATOR_2(tuple, Equal, type::Bool(), type::constant(type::Tuple(type::Wildcard())),
16 operator_::sameTypeAs(0,
"tuple<*>"),
"Compares two tuples element-wise.");
17 STANDARD_OPERATOR_2(tuple, Unequal, type::Bool(), type::constant(type::Tuple(type::Wildcard())),
18 operator_::sameTypeAs(0,
"tuple<*>"),
"Compares two tuples element-wise.");
20 BEGIN_OPERATOR_CUSTOM(tuple, Index)
21 Type result(
const std::vector<Expression>& ops)
const {
23 return type::DocOnly(
"<type of element>");
28 auto ctor = ops[1].tryAs<expression::Ctor>();
32 auto i = ctor->ctor().tryAs<ctor::UnsignedInteger>();
36 const auto& types = ops[0].type().as<type::Tuple>().types();
38 if ( types.size() <= i->value() )
41 return types[i->value()];
44 bool isLhs()
const {
return true; }
46 std::vector<Operand> operands()
const {
47 return {{.type = type::Tuple(type::Wildcard())}, {.type = type::UnsignedInteger(64)}};
50 void validate(
const expression::ResolvedOperator& i, operator_::position_t p)
const {
51 if (
auto ec = i.op1().tryAs<expression::Ctor>() )
52 if (
auto c = ec->ctor().tryAs<ctor::UnsignedInteger>() ) {
53 if ( c->value() < 0 || c->value() >= i.op0().type().as<type::Tuple>().types().size() )
54 p.node.addError(
"tuple index out of range");
59 p.node.addError(
"tuple index must be an integer constant");
62 std::string doc()
const {
63 return "Extracts the tuple element at the given index. The index must be a constant unsigned integer.";
67 BEGIN_OPERATOR_CUSTOM(tuple, Member)
68 Type result(
const std::vector<Expression>& ops)
const {
70 return type::DocOnly(
"<type of element>");
72 auto id = ops[1].as<expression::Member>().
id();
73 auto elem = ops[0].type().as<type::Tuple>().elementByID(
id);
80 bool isLhs()
const {
return true; }
82 std::vector<Operand> operands()
const {
83 return {{.type = type::Tuple(type::Wildcard())}, {.type = type::Member(type::Wildcard()), .doc =
"<id>"}};
86 void validate(
const expression::ResolvedOperator& i, operator_::position_t p)
const {
87 auto id = i.operands()[1].as<expression::Member>().
id();
88 auto elem = i.operands()[0].type().as<type::Tuple>().elementByID(
id);
91 p.node.addError(
"unknown tuple element");
94 std::string doc()
const {
return "Extracts the tuple element corresponding to the given ID."; }