Skip to content

Commit

Permalink
Set discriminant for default union case to 0
Browse files Browse the repository at this point in the history
Sets the discriminant value for default union case in the serializer
ops to 0 in idlc and type builder. The value is not used, so there
is no need to set it to the first available integer or enum value.

Signed-off-by: Dennis Potman <[email protected]>
  • Loading branch information
dpotman authored and eboasson committed Oct 10, 2022
1 parent 8b7cb30 commit 53cbc51
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
40 changes: 37 additions & 3 deletions src/core/ddsc/tests/TypeBuilderTypes.idl
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,10 @@ module TypeBuilderTypes {
};

/* enum with non-default bit-bound as switch type */
union t17 switch(en8) {
case EN8_1: t14 u1;
case EN8_2: t15 u2;
@bit_bound(12) enum en16 { EN16_1, EN16_2 };
union t17 switch(en16) {
case EN16_1: t14 u1;
case EN16_2: t15 u2;
};

/* hash member id */
Expand Down Expand Up @@ -367,4 +368,37 @@ module TypeBuilderTypes {
sequence<t24_1> u99;
};

union t25_1 switch(long) {
default:
string<9> u1;
};
struct t25 {
t25_1 f1;
};

/* default case with non-zero value */
@nested union t26_1 switch(octet) {
case 0:
long u1;
default:
long u2;
};
struct t26 {
t26_1 f1;
};

/* default case with non-zero enum value */
enum en27 { @value(4) E27_3, E27_1, @value(3) E27_2 };
@nested union t27_1 switch(en27) {
case E27_1:
long u1;
case E27_3:
long u3;
default:
long ud;
};
struct t27 {
t27_1 f1;
};

};
4 changes: 3 additions & 1 deletion src/core/ddsc/tests/typebuilder.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ static bool tmap_equal (ddsi_typemap_t *a, ddsi_typemap_t *b)
CU_TheoryDataPoints (ddsc_typebuilder, topic_desc) = {
CU_DataPoints (const dds_topic_descriptor_t *, &D(t1), &D(t2), &D(t3), &D(t4), &D(t5), &D(t6), &D(t7), &D(t8),
&D(t9), &D(t10), &D(t11), &D(t12), &D(t13), &D(t14), &D(t15), &D(t16),
&D(t17), &D(t18), &D(t19), &D(t20), &D(t21), &D(t22), &D(t23), &D(t24) ),
&D(t17), &D(t18), &D(t19), &D(t20), &D(t21), &D(t22), &D(t23), &D(t24),
&D(t25), &D(t26), &D(t27) ),
};

#undef D

CU_Theory((const dds_topic_descriptor_t *desc), ddsc_typebuilder, topic_desc, .init = typebuilder_init, .fini = typebuilder_fini)
Expand Down
6 changes: 6 additions & 0 deletions src/core/ddsi/src/ddsi_typebuilder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,12 @@ static dds_return_t get_ops_union (const struct typebuilder_union *tb_union, uin
if (tb_union->disc_type.args.prim_args.is_signed)
flags |= DDS_OP_FLAG_SGN;
break;
case DDS_OP_VAL_ENU:
flags |= get_bitbound_flags (tb_union->disc_type.args.enum_args.bit_bound);
break;
case DDS_OP_VAL_BMK:
flags |= get_bitbound_flags (tb_union->disc_type.args.bitmask_args.bit_bound);
break;
default:
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/idl/include/idl/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,8 @@ IDL_EXPORT bool idl_is_case(const void *node);
IDL_EXPORT bool idl_is_default_case(const void *node);
IDL_EXPORT bool idl_is_implicit_default_case(const void *ptr);
IDL_EXPORT bool idl_is_case_label(const void *node);
IDL_EXPORT bool idl_is_default_case_label(const void *node);
IDL_EXPORT bool idl_is_implicit_default_case_label(const void *node);
IDL_EXPORT bool idl_is_enum(const void *node);
IDL_EXPORT bool idl_is_enumerator(const void *node);
IDL_EXPORT bool idl_is_bitmask(const void *node);
Expand Down
20 changes: 20 additions & 0 deletions src/idl/src/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,26 @@ bool idl_is_case_label(const void *ptr)
return true;
}

static bool idl_is_case_label_mask_impl(const void *ptr, const idl_mask_t mask)
{
const idl_case_label_t *node = ptr;
if (!(idl_mask(node) & IDL_CASE_LABEL))
return false;
if ((idl_mask(node) & mask) == mask)
return true;
return false;
}

bool idl_is_default_case_label(const void *ptr)
{
return idl_is_case_label_mask_impl (ptr, IDL_DEFAULT_CASE_LABEL);
}

bool idl_is_implicit_default_case_label(const void *ptr)
{
return idl_is_case_label_mask_impl (ptr, IDL_IMPLICIT_DEFAULT_CASE_LABEL);
}

static void delete_case_label(void *ptr)
{
idl_case_label_t *node = ptr;
Expand Down
4 changes: 2 additions & 2 deletions src/tools/idlc/src/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,8 @@ emit_case(
descriptor->n_opcodes++; /* this stashes an opcode, but is using stash_jeq_offset so we'll increase the opcode count here */
off++;
}
/* generate union case discriminator */
if ((ret = stash_constant(pstate, &ctype->instructions, off++, label->const_expr)))
/* generate union case discriminator, use 0 for default case */
if ((ret = stash_constant(pstate, &ctype->instructions, off++, idl_is_default_case_label(label) || idl_is_implicit_default_case_label(label) ? 0 : label->const_expr)))
return ret;
/* generate union case member (address) offset; use offset 0 for empty types,
as these members are not generated and no offset can be calculated */
Expand Down

0 comments on commit 53cbc51

Please sign in to comment.