Commit 6d692046 authored by Lev Walkin's avatar Lev Walkin

extend naming API

parent 14e75ed7
......@@ -36,26 +36,26 @@ c_name_clash_finder_destroy() {
}
static void
register_global_name(arg_t *arg, const char *name) {
register_global_name(asn1p_expr_t *expr, const char *name) {
struct intl_name *n;
TQ_FOR(n, &used_names, next) {
if(strcmp(n->name, name) == 0) {
if(!(arg->expr->_mark & TM_NAMEGIVEN) && arg->expr != n->expr) {
n->clashes_with = arg->expr;
arg->expr->ref_cnt++;
if(!(expr->_mark & TM_NAMEGIVEN) && expr != n->expr) {
n->clashes_with = expr;
expr->ref_cnt++;
return;
}
}
}
if(arg->expr->_mark & TM_NAMEGIVEN)
if(expr->_mark & TM_NAMEGIVEN)
return;
n = calloc(1, sizeof(*n));
assert(n);
n->expr = arg->expr;
arg->expr->ref_cnt++;
n->expr = expr;
expr->ref_cnt++;
n->name = strdup(name);
TQ_ADD(&used_names, n, next);
}
......@@ -90,24 +90,21 @@ c_name_clash(arg_t *arg) {
static abuf *
construct_base_name(abuf *buf, arg_t *arg, int compound_names,
construct_base_name(abuf *buf, asn1p_expr_t *expr, int compound_names,
int avoid_keywords) {
const char *id;
if(!buf) buf = abuf_new();
assert(buf);
if(compound_names && arg->expr->parent_expr) {
arg_t tmparg = *arg;
tmparg.expr = arg->expr->parent_expr;
construct_base_name(buf, &tmparg, compound_names, 0);
if(compound_names && expr->parent_expr) {
construct_base_name(buf, expr->parent_expr, compound_names, 0);
if(buf->length) {
abuf_str(buf, "__"); /* component separator */
}
}
id = asn1c_make_identifier(
((avoid_keywords && !buf->length) ? AMI_CHECK_RESERVED : 0), arg->expr,
0);
((avoid_keywords && !buf->length) ? AMI_CHECK_RESERVED : 0), expr, 0);
abuf_str(buf, id);
......@@ -115,11 +112,17 @@ construct_base_name(abuf *buf, arg_t *arg, int compound_names,
}
static struct c_names
c_name_impl(arg_t *arg, int avoid_keywords) {
asn1p_expr_type_e expr_type = arg->expr->expr_type;
c_name_impl(arg_t *arg, asn1p_expr_t *expr, int avoid_keywords) {
asn1p_expr_type_e expr_type = expr->expr_type;
struct c_names names;
int compound_names = 0;
static abuf b_type_asn_name;
static abuf b_type_part_name;
static abuf b_type_base_name;
static abuf b_type_c_name;
static abuf b_asn_name;
static abuf b_part_name;
static abuf b_base_name;
static abuf b_short_name;
static abuf b_full_name;
......@@ -129,7 +132,13 @@ c_name_impl(arg_t *arg, int avoid_keywords) {
static abuf b_members_enum;
static abuf b_members_name;
abuf_clear(&b_type_asn_name);
abuf_clear(&b_type_part_name);
abuf_clear(&b_type_base_name);
abuf_clear(&b_type_c_name);
abuf_clear(&b_asn_name);
abuf_clear(&b_base_name);
abuf_clear(&b_part_name);
abuf_clear(&b_short_name);
abuf_clear(&b_full_name);
abuf_clear(&b_as_member);
......@@ -138,6 +147,13 @@ c_name_impl(arg_t *arg, int avoid_keywords) {
abuf_clear(&b_members_enum);
abuf_clear(&b_members_name);
abuf_str(&b_type_asn_name, asn1c_type_name(arg, expr, TNF_UNMODIFIED));
abuf_str(&b_type_part_name, asn1c_type_name(arg, expr, TNF_SAFE));
abuf_str(&b_type_base_name, asn1c_type_name(arg, expr, TNF_SAFE));
abuf_str(&b_type_c_name, asn1c_type_name(arg, expr, TNF_CTYPE));
if((arg->flags & A1C_COMPOUND_NAMES)) {
if((expr_type & ASN_CONSTR_MASK)
|| expr_type == ASN_BASIC_ENUMERATED
......@@ -147,28 +163,34 @@ c_name_impl(arg_t *arg, int avoid_keywords) {
}
}
abuf *base_name =
construct_base_name(NULL, arg, compound_names, avoid_keywords);
abuf *part_name =
construct_base_name(NULL, arg, compound_names, 0);
abuf *member_name =
construct_base_name(NULL, arg, 0, 1);
construct_base_name(&b_asn_name, expr, 0, 0);
construct_base_name(&b_part_name, expr, 0, 0);
construct_base_name(&b_base_name, expr, compound_names, avoid_keywords);
construct_base_name(&b_as_member, expr, 0, 1);
static abuf tmp_compoundable_part_name;
abuf_clear(&tmp_compoundable_part_name);
construct_base_name(&tmp_compoundable_part_name, expr, compound_names, 0);
abuf_printf(&b_base_name, "%s", base_name->buffer);
if(!arg->expr->_anonymous_type) {
if(!expr->_anonymous_type) {
if(arg->embed) {
abuf_printf(&b_short_name, "%s", member_name->buffer);
abuf_printf(&b_short_name, "%s", b_as_member.buffer);
} else {
abuf_printf(&b_short_name, "%s_t", member_name->buffer);
abuf_printf(&b_short_name, "%s_t", b_as_member.buffer);
}
}
abuf_printf(&b_full_name, "struct %s", base_name->buffer);
abuf_printf(&b_as_member, "%s", member_name->buffer);
abuf_printf(&b_presence_enum, "enum %s_PR", part_name->buffer);
abuf_printf(&b_presence_name, "%s_PR", part_name->buffer);
abuf_printf(&b_members_enum, "enum %s", base_name->buffer);
abuf_printf(&b_members_name, "e_%s", part_name->buffer);
abuf_printf(&b_full_name, "struct %s", b_base_name.buffer);
abuf_printf(&b_presence_enum, "enum %s_PR", tmp_compoundable_part_name.buffer);
abuf_printf(&b_presence_name, "%s_PR", tmp_compoundable_part_name.buffer);
abuf_printf(&b_members_enum, "enum %s", b_base_name.buffer);
abuf_printf(&b_members_name, "e_%s", tmp_compoundable_part_name.buffer);
names.type.asn_name = b_type_asn_name.buffer;
names.type.base_name = b_type_base_name.buffer;
names.type.part_name = b_type_part_name.buffer;
names.type.c_name = b_type_c_name.buffer;
names.asn_name = b_asn_name.buffer;
names.part_name = b_part_name.buffer;
names.base_name = b_base_name.buffer;
names.short_name = b_short_name.buffer;
names.full_name = b_full_name.buffer;
......@@ -178,26 +200,27 @@ c_name_impl(arg_t *arg, int avoid_keywords) {
names.members_enum = b_members_enum.buffer;
names.members_name = b_members_name.buffer;
abuf_free(base_name);
abuf_free(part_name);
abuf_free(member_name);
/* A _subset_ of names is checked against being globally unique */
register_global_name(arg, names.base_name);
register_global_name(arg, names.full_name);
register_global_name(arg, names.presence_enum);
register_global_name(arg, names.presence_name);
register_global_name(arg, names.members_enum);
register_global_name(arg, names.members_name);
register_global_name(expr, names.base_name);
register_global_name(expr, names.full_name);
register_global_name(expr, names.presence_enum);
register_global_name(expr, names.presence_name);
register_global_name(expr, names.members_enum);
register_global_name(expr, names.members_name);
arg->expr->_mark |= TM_NAMEGIVEN;
expr->_mark |= TM_NAMEGIVEN;
return names;
}
struct c_names
c_name(arg_t *arg) {
return c_name_impl(arg, 1);
return c_name_impl(arg, arg->expr, 1);
}
struct c_names
c_expr_name(arg_t *arg, asn1p_expr_t *expr) {
return c_name_impl(arg, expr, 1);
}
const char *
......@@ -206,7 +229,8 @@ c_member_name(arg_t *arg, asn1p_expr_t *expr) {
abuf_clear(&ab);
abuf_str(&ab, c_name_impl(arg, 0).base_name);
/* NB: do not use part_name, doesn't work for -fcompound-names */
abuf_str(&ab, c_name_impl(arg, arg->expr, 0).base_name);
abuf_str(&ab, "_");
abuf_str(&ab, asn1c_make_identifier(0, expr, 0));
......@@ -221,12 +245,42 @@ c_presence_name(arg_t *arg, asn1p_expr_t *expr) {
abuf_clear(&ab);
if(expr) {
abuf_str(&ab, c_name_impl(arg, 0).base_name);
/* NB: do not use part_name, doesn't work for -fcompound-names */
abuf_str(&ab, c_name_impl(arg, arg->expr, 0).base_name);
abuf_str(&ab, "_PR_");
abuf_str(&ab, asn1c_make_identifier(0, expr, 0));
} else {
abuf_printf(&ab, "%s_PR_NOTHING", c_name_impl(arg, 0).base_name);
abuf_printf(&ab, "%s_PR_NOTHING",
c_name_impl(arg, arg->expr, 0).base_name);
}
return ab.buffer;
}
const char *
c_names_format(struct c_names ns) {
static abuf nbuf;
abuf_clear(&nbuf);
#define FMT_COMPONENT(x) abuf_printf(&nbuf, " ." #x "=\"%s\",", ns.x);
abuf_str(&nbuf, "{");
FMT_COMPONENT(type.asn_name);
FMT_COMPONENT(type.part_name);
FMT_COMPONENT(type.base_name);
FMT_COMPONENT(type.c_name);
FMT_COMPONENT(asn_name);
FMT_COMPONENT(part_name);
FMT_COMPONENT(base_name);
FMT_COMPONENT(full_name);
FMT_COMPONENT(short_name);
FMT_COMPONENT(full_name);
FMT_COMPONENT(as_member);
FMT_COMPONENT(presence_enum);
FMT_COMPONENT(presence_name);
FMT_COMPONENT(members_enum);
FMT_COMPONENT(members_name);
abuf_printf(&nbuf, " .members_name=\"%s\" }", ns.members_name);
return nbuf.buffer;
}
#ifndef ASN1_COMPILER_NAMING_H
#define ASN1_COMPILER_NAMING_H
/*
* A member or a field name.
*/
struct c_names {
const char *base_name; /* "foo" */
struct {
const char *asn_name; /* "T-Rex" */
const char *part_name; /* "T_Rex" */
const char *base_name; /* "T_Rex" */
const char *c_name; /* "T_Rex_t" */
} type;
const char *asn_name; /* "foo" */
const char *part_name; /* "foo", "signed" */
const char *base_name; /* "foo" if not, "parent_foo" if compound */
const char *short_name; /* "foo_t", "e_foo" */
const char *full_name; /* "struct foo", "enum foo" */
const char *as_member; /* "foo" (not compounded) */
......@@ -13,9 +25,12 @@ struct c_names {
};
struct c_names c_name(arg_t *);
struct c_names c_expr_name(arg_t *, asn1p_expr_t *);
const char *c_member_name(arg_t *, asn1p_expr_t *); /* %s_%s */
const char *c_presence_name(arg_t *, asn1p_expr_t *); /* %s_PR_%s */
const char *c_names_format(struct c_names); /* For debugging */
/*
* Returns 0 if no C name clashes have been encountered.
* Returns 1 if C name clashes have been encountered.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment