Skip to content

Commit

Permalink
Use comprehensible names for instances of parameterized types
Browse files Browse the repository at this point in the history
With previous commit, the following data types are generated in ProtocolIE-Field.h
for S1AP's ASN.1 :

```
typedef enum ProtocolIE_Field_6564P0__value_PR {
    ProtocolIE_Field_6564P0__value_PR_NOTHING, /* No components present */
    ProtocolIE_Field_6564P0__value_PR_E_RABToBeSetupItemBearerSUReq
} ProtocolIE_Field_6564P0__value_PR;

typedef struct ProtocolIE_Field_6564P0 {
    ProtocolIE_ID_t      id;
    Criticality_t        criticality;
    struct ProtocolIE_Field_6564P0__value {
        ProtocolIE_Field_6564P0__value_PR        present;
        union ProtocolIE_Field_6564P0__value_u {
            E_RABToBeSetupItemBearerSUReq_t      E_RABToBeSetupItemBearerSUReq;
        } choice;

        /* Context for parsing across buffer boundaries */
        asn_struct_ctx_t _asn_ctx;
    } value;

    /* Context for parsing across buffer boundaries */
    asn_struct_ctx_t _asn_ctx;
} ProtocolIE_Field_6564P0_t;

```

It's difficult for developer to recognize to which ASN.1 type they are linked.
They all start with 'ProtocolIE_Field_'. It will be error-prone.

With this commit, more human comprehensible data types are generated :

```

typedef enum E_RABToBeSetupItemBearerSUReqIEs_6564P0__value_PR {
    E_RABToBeSetupItemBearerSUReqIEs_6564P0__value_PR_NOTHING, /* No components present */
    E_RABToBeSetupItemBearerSUReqIEs_6564P0__value_PR_E_RABToBeSetupItemBearerSUReq
} E_RABToBeSetupItemBearerSUReqIEs_6564P0__value_PR;

typedef struct E_RABToBeSetupItemBearerSUReqIEs_6564P0 {
    ProtocolIE_ID_t      id;
    Criticality_t        criticality;
    struct E_RABToBeSetupItemBearerSUReqIEs_6564P0__value {
        E_RABToBeSetupItemBearerSUReqIEs_6564P0__value_PR present;
        union E_RABToBeSetupItemBearerSUReqIEs_6564P0__value_u {
            E_RABToBeSetupItemBearerSUReq_t               E_RABToBeSetupItemBearerSUReq;
        } choice;

        /* Context for parsing across buffer boundaries */
        asn_struct_ctx_t _asn_ctx;
    } value;

    /* Context for parsing across buffer boundaries */
    asn_struct_ctx_t _asn_ctx;
} E_RABToBeSetupItemBearerSUReqIEs_6564P0_t;

```

Developers can understand they are generated from :

```

E-RABToBeSetupItemBearerSUReqIEs S1AP-PROTOCOL-IES ::= {
    { ID id-E-RABToBeSetupItemBearerSUReq CRITICALITY reject TYPE E-RABToBeSetupItemBearerSUReq PRESENCE mandatory },
    ...
}

```
  • Loading branch information
brchiu committed Nov 14, 2017
1 parent 1e81b32 commit d5cfad7
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 146 deletions.
11 changes: 3 additions & 8 deletions libasn1compiler/asn1c_C.c
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
}

} else {
GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE));
GEN_POS_INCLUDE_BASE(OT_INCLUDES, expr);

REDIR(OT_TYPE_DECLS);

Expand Down Expand Up @@ -3236,13 +3236,8 @@ emit_include_dependencies(arg_t *arg) {
if((!(memb->expr_type & ASN_CONSTR_MASK)
&& memb->expr_type > ASN_CONSTR_MASK)
|| memb->meta_type == AMT_TYPEREF) {
if(memb->marker.flags & EM_UNRECURSE) {
GEN_POSTINCLUDE(asn1c_type_name(arg,
memb, TNF_INCLUDE));
} else {
GEN_INCLUDE(asn1c_type_name(arg,
memb, TNF_INCLUDE));
}
GEN_POS_INCLUDE_BASE((memb->marker.flags & EM_UNRECURSE) ?
OT_POST_INCLUDE : OT_INCLUDES, memb);
}
}

Expand Down
14 changes: 14 additions & 0 deletions libasn1compiler/asn1c_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,20 @@ asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
_format = TNF_CTYPE;
stdname = 1;
typename = ASN_EXPR_TYPE2STR(expr->expr_type);
if(_format == TNF_INCLUDE) {
if(expr->expr_type == ASN_CONSTR_SEQUENCE)
typename = "constr_SEQUENCE";
else if(expr->expr_type == ASN_CONSTR_CHOICE)
typename = "constr_CHOICE";
else if(expr->expr_type == ASN_CONSTR_SET)
typename = "constr_SET";
else if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF)
typename = "constr_SEQUENCE_OF";
else if(expr->expr_type == ASN_CONSTR_SET_OF)
typename = "constr_SET_OF";
else if(expr->expr_type == ASN_CONSTR_OPEN_TYPE)
typename = "OPEN_TYPE";
}
} else {
_format = TNF_RSAFE;
typename = expr->Identifier;
Expand Down
23 changes: 15 additions & 8 deletions libasn1compiler/asn1c_out.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,31 @@ int asn1c_compiled_output(arg_t *arg, const char *fmt, ...);
} while(0)

/* Generate #include line */
#define GEN_INCLUDE_STD(typename) do { \
if((arg->flags & A1C_INCLUDES_QUOTED)) { \
#define GEN_INCLUDE_STD(typename) do { \
if((arg->flags & A1C_INCLUDES_QUOTED)) { \
GEN_INCLUDE("\"" typename ".h\""); \
} else { \
GEN_INCLUDE("<" typename ".h>"); \
} } while(0)
#define GEN_INCLUDE(filename) do { \
#define GEN_INCLUDE(filename) \
GEN_POS_INCLUDE(OT_INCLUDES, filename)
#define GEN_POSTINCLUDE(filename) \
GEN_POS_INCLUDE(OT_POST_INCLUDE, filename)
#define GEN_POS_INCLUDE(pos, filename) do { \
int saved_target = arg->target->target; \
if(!filename) break; \
REDIR(OT_INCLUDES); \
REDIR(pos); \
OUT_NOINDENT("#include %s\n", filename); \
REDIR(saved_target); \
} while(0)
#define GEN_POSTINCLUDE(filename) do { \
#define GEN_POS_INCLUDE_BASE(pos, expr) do { \
asn1p_expr_t *rhs_pspecs = expr->rhs_pspecs; \
expr->rhs_pspecs = (asn1p_expr_t *)0; \
int saved_target = arg->target->target; \
if(!filename) break; \
REDIR(OT_POST_INCLUDE); \
OUT_NOINDENT("#include %s\n", filename); \
REDIR(pos); \
OUT_NOINDENT("#include %s\n", \
asn1c_type_name(arg, expr, TNF_INCLUDE)); \
expr->rhs_pspecs = rhs_pspecs; \
REDIR(saved_target); \
} while(0)

Expand Down
16 changes: 16 additions & 0 deletions libasn1fix/asn1fix_param.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ typedef struct resolver_arg {
asn1p_expr_t *original_expr;
asn1p_paramlist_t *lhs_params;
asn1p_expr_t *rhs_pspecs;
char *resolved_name;
} resolver_arg_t;

static asn1p_expr_t *resolve_expr(asn1p_expr_t *, void *resolver_arg);
Expand Down Expand Up @@ -52,8 +53,13 @@ asn1f_parameterization_fork(arg_t *arg, asn1p_expr_t *expr, asn1p_expr_t *rhs_ps
rarg.original_expr = expr;
rarg.lhs_params = expr->lhs_params;
rarg.rhs_pspecs = rhs_pspecs;
rarg.resolved_name = NULL;
exc = asn1p_expr_clone_with_resolver(expr, resolve_expr, &rarg);
if(!exc) return NULL;
if(rarg.resolved_name) {
free(exc->Identifier);
exc->Identifier = strdup(rarg.resolved_name);
}
rpc = asn1p_expr_clone(rhs_pspecs, 0);
assert(rpc);

Expand Down Expand Up @@ -138,6 +144,16 @@ resolve_expr(asn1p_expr_t *expr_to_resolve, void *resolver_arg) {
free(nex->Identifier);
nex->Identifier = expr_to_resolve->Identifier
? strdup(expr_to_resolve->Identifier) : 0;
if(expr->meta_type == AMT_TYPEREF) {
asn1p_ref_t *ref = expr->reference;
rarg->resolved_name = ref->components[ref->comp_count - 1].name;
} else if(expr->meta_type == AMT_VALUESET) {
asn1p_constraint_t *ct = expr->constraints;
if(ct->type == ACT_EL_TYPE) {
asn1p_ref_t *ref = ct->containedSubtype->value.v_type->reference;
rarg->resolved_name = ref->components[ref->comp_count - 1].name;
}
}
return nex;
} else {
FATAL("Feature not implemented for %s (%d/%x), "
Expand Down
62 changes: 31 additions & 31 deletions tests/tests-asn1c-compiler/144-ios-parameterization-OK.asn1.-P
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/*** <<< TYPE-DECLS [Message] >>> ***/

typedef struct Message {
SpecializedContent_30P0_t content;
RegionalExtension_30P0_t content;

/* Context for parsing across buffer boundaries */
asn_struct_ctx_t _asn_ctx;
Expand All @@ -23,7 +23,7 @@ static asn_TYPE_member_t asn_MBR_Message_1[] = {
{ ATF_NOFLAGS, 0, offsetof(struct Message, content),
.tag = (ASN_TAG_CLASS_UNIVERSAL | (16 << 2)),
.tag_mode = 0,
.type = &asn_DEF_SpecializedContent_30P0,
.type = &asn_DEF_RegionalExtension_30P0,
.type_selector = 0,
{ .oer_constraints = 0, .per_constraints = 0, .general_constraints = 0 },
0, 0, /* No default value */
Expand Down Expand Up @@ -81,11 +81,11 @@ typedef enum value_PR {

/*** <<< TYPE-DECLS [SpecializedContent] >>> ***/

typedef struct SpecializedContent_30P0 {
typedef struct RegionalExtension_30P0 {
long id;
struct value {
value_PR present;
union SpecializedContent_30P0__value_u {
union RegionalExtension_30P0__value_u {
long INTEGER;
BOOLEAN_t BOOLEAN;
} choice;
Expand All @@ -96,13 +96,13 @@ typedef struct SpecializedContent_30P0 {

/* Context for parsing across buffer boundaries */
asn_struct_ctx_t _asn_ctx;
} SpecializedContent_30P0_t;
} RegionalExtension_30P0_t;

/*** <<< FUNC-DECLS [SpecializedContent] >>> ***/

extern asn_TYPE_descriptor_t asn_DEF_SpecializedContent_30P0;
extern asn_SEQUENCE_specifics_t asn_SPC_SpecializedContent_30P0_specs_1;
extern asn_TYPE_member_t asn_MBR_SpecializedContent_30P0_1[2];
extern asn_TYPE_descriptor_t asn_DEF_RegionalExtension_30P0;
extern asn_SEQUENCE_specifics_t asn_SPC_RegionalExtension_30P0_specs_1;
extern asn_TYPE_member_t asn_MBR_RegionalExtension_30P0_1[2];

/*** <<< IOC-TABLES [SpecializedContent] >>> ***/

Expand Down Expand Up @@ -140,13 +140,13 @@ memb_id_constraint_1(const asn_TYPE_descriptor_t *td, const void *sptr,
}

static asn_type_selector_result_t
select_SpecializedContent_30P0_value_type(const asn_TYPE_descriptor_t *parent_type, const void *parent_sptr) {
select_RegionalExtension_30P0_value_type(const asn_TYPE_descriptor_t *parent_type, const void *parent_sptr) {
asn_type_selector_result_t result = {0, 0};
const asn_ioc_set_t *itable = asn_IOS_RegionalExtension_1;
size_t constraining_column = 0; /* &id */
size_t for_column = 1; /* &Type */
size_t row;
const long *constraining_value = (const long *)((const char *)parent_sptr + offsetof(struct SpecializedContent_30P0, id));
const long *constraining_value = (const long *)((const char *)parent_sptr + offsetof(struct RegionalExtension_30P0, id));

for(row=0; row < itable->rows_count; row++) {
const asn_ioc_cell_t *constraining_cell = &itable->rows[row * itable->columns_count + constraining_column];
Expand Down Expand Up @@ -233,8 +233,8 @@ asn_TYPE_descriptor_t asn_DEF_value_3 = {
&asn_SPC_value_specs_3 /* Additional specs */
};

asn_TYPE_member_t asn_MBR_SpecializedContent_30P0_1[] = {
{ ATF_NOFLAGS, 0, offsetof(struct SpecializedContent_30P0, id),
asn_TYPE_member_t asn_MBR_RegionalExtension_30P0_1[] = {
{ ATF_NOFLAGS, 0, offsetof(struct RegionalExtension_30P0, id),
.tag = (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)),
.tag_mode = 0,
.type = &asn_DEF_NativeInteger,
Expand All @@ -243,43 +243,43 @@ asn_TYPE_member_t asn_MBR_SpecializedContent_30P0_1[] = {
0, 0, /* No default value */
.name = "id"
},
{ ATF_OPEN_TYPE | ATF_NOFLAGS, 0, offsetof(struct SpecializedContent_30P0, value),
{ ATF_OPEN_TYPE | ATF_NOFLAGS, 0, offsetof(struct RegionalExtension_30P0, value),
.tag = -1 /* Ambiguous tag (ANY?) */,
.tag_mode = 0,
.type = &asn_DEF_value_3,
.type_selector = select_SpecializedContent_30P0_value_type,
.type_selector = select_RegionalExtension_30P0_value_type,
{ .oer_constraints = 0, .per_constraints = 0, .general_constraints = memb_value_constraint_1 },
0, 0, /* No default value */
.name = "value"
},
};
static const ber_tlv_tag_t asn_DEF_SpecializedContent_30P0_tags_1[] = {
static const ber_tlv_tag_t asn_DEF_RegionalExtension_30P0_tags_1[] = {
(ASN_TAG_CLASS_UNIVERSAL | (16 << 2))
};
static const asn_TYPE_tag2member_t asn_MAP_SpecializedContent_30P0_tag2el_1[] = {
static const asn_TYPE_tag2member_t asn_MAP_RegionalExtension_30P0_tag2el_1[] = {
{ (ASN_TAG_CLASS_UNIVERSAL | (2 << 2)), 0, 0, 0 } /* id */
};
asn_SEQUENCE_specifics_t asn_SPC_SpecializedContent_30P0_specs_1 = {
sizeof(struct SpecializedContent_30P0),
offsetof(struct SpecializedContent_30P0, _asn_ctx),
.tag2el = asn_MAP_SpecializedContent_30P0_tag2el_1,
asn_SEQUENCE_specifics_t asn_SPC_RegionalExtension_30P0_specs_1 = {
sizeof(struct RegionalExtension_30P0),
offsetof(struct RegionalExtension_30P0, _asn_ctx),
.tag2el = asn_MAP_RegionalExtension_30P0_tag2el_1,
.tag2el_count = 1, /* Count of tags in the map */
0, 0, 0, /* Optional elements (not needed) */
-1, /* First extension addition */
};
asn_TYPE_descriptor_t asn_DEF_SpecializedContent_30P0 = {
"SpecializedContent",
"SpecializedContent",
asn_TYPE_descriptor_t asn_DEF_RegionalExtension_30P0 = {
"RegionalExtension",
"RegionalExtension",
&asn_OP_SEQUENCE,
asn_DEF_SpecializedContent_30P0_tags_1,
sizeof(asn_DEF_SpecializedContent_30P0_tags_1)
/sizeof(asn_DEF_SpecializedContent_30P0_tags_1[0]), /* 1 */
asn_DEF_SpecializedContent_30P0_tags_1, /* Same as above */
sizeof(asn_DEF_SpecializedContent_30P0_tags_1)
/sizeof(asn_DEF_SpecializedContent_30P0_tags_1[0]), /* 1 */
asn_DEF_RegionalExtension_30P0_tags_1,
sizeof(asn_DEF_RegionalExtension_30P0_tags_1)
/sizeof(asn_DEF_RegionalExtension_30P0_tags_1[0]), /* 1 */
asn_DEF_RegionalExtension_30P0_tags_1, /* Same as above */
sizeof(asn_DEF_RegionalExtension_30P0_tags_1)
/sizeof(asn_DEF_RegionalExtension_30P0_tags_1[0]), /* 1 */
{ 0, 0, SEQUENCE_constraint },
asn_MBR_SpecializedContent_30P0_1,
asn_MBR_RegionalExtension_30P0_1,
2, /* Elements count */
&asn_SPC_SpecializedContent_30P0_specs_1 /* Additional specs */
&asn_SPC_RegionalExtension_30P0_specs_1 /* Additional specs */
};

Loading

0 comments on commit d5cfad7

Please sign in to comment.