Commit b46156d9 authored by Lev Walkin's avatar Lev Walkin

naming abstraction

parent e4ea1750
......@@ -14,6 +14,7 @@ libasn1compiler_la_SOURCES = \
asn1c_misc.c asn1c_misc.h \
asn1c_out.c asn1c_out.h \
asn1c_lang.c asn1c_lang.h \
asn1c_naming.c asn1c_naming.h \
asn1c_save.c asn1c_save.h \
asn1c_C.c asn1c_C.h \
asn1c_constraint.c asn1c_constraint.h \
......
This diff is collapsed.
......@@ -42,7 +42,7 @@ reserved_keyword(const char *str) {
* Construct identifier from multiple parts.
* Convert unsafe characters to underscores.
*/
char *
const char *
asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) {
static char *storage;
static int storage_size;
......
......@@ -12,7 +12,7 @@ enum ami_flags_e {
AMI_NODELIMITER = 4, /* Do not put delimiter, just concatenate */
AMI_USE_PREFIX = 8, /* Use Prefix when generating identifier */
};
char *asn1c_make_identifier(enum ami_flags_e, asn1p_expr_t *expr, ...);
const char *asn1c_make_identifier(enum ami_flags_e, asn1p_expr_t *expr, ...);
/*
* Return the type name of the specified expression.
......
#include "asn1c_internal.h"
#include "asn1c_naming.h"
#include "asn1c_misc.h"
#include "asn1c_misc.h"
#include <asn1_buffer.h>
static abuf *
construct_base_name(abuf *buf, arg_t *arg, int compound_names,
int avoid_keywords) {
const char *id;
if(!buf) buf = abuf_new();
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(buf->length) {
abuf_str(buf, "__"); /* component separator */
}
}
id = asn1c_make_identifier(
((avoid_keywords && !buf->length) ? AMI_CHECK_RESERVED : 0), arg->expr,
0);
abuf_str(buf, id);
return buf;
}
static struct c_names
c_name_impl(arg_t *arg, int avoid_keywords) {
asn1p_expr_type_e expr_type = arg->expr->expr_type;
struct c_names names;
int compound_names = 0;
static abuf b_base_name;
static abuf b_short_name;
static abuf b_full_name;
static abuf b_as_member;
static abuf b_presence_enum;
static abuf b_presence_name;
static abuf b_members_enum;
static abuf b_members_name;
abuf_clear(&b_base_name);
abuf_clear(&b_short_name);
abuf_clear(&b_full_name);
abuf_clear(&b_as_member);
abuf_clear(&b_presence_enum);
abuf_clear(&b_presence_name);
abuf_clear(&b_members_enum);
abuf_clear(&b_members_name);
if((arg->flags & A1C_COMPOUND_NAMES)) {
if((expr_type & ASN_CONSTR_MASK)
|| expr_type == ASN_BASIC_ENUMERATED
|| ((expr_type == ASN_BASIC_INTEGER
|| expr_type == ASN_BASIC_BIT_STRING))) {
compound_names = 1;
}
}
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);
abuf_printf(&b_base_name, "%s", base_name->buffer);
if(!arg->expr->_anonymous_type) {
if(arg->embed) {
abuf_printf(&b_short_name, "%s", member_name->buffer);
} else {
abuf_printf(&b_short_name, "%s_t", member_name->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);
names.base_name = b_base_name.buffer;
names.short_name = b_short_name.buffer;
names.full_name = b_full_name.buffer;
names.as_member = b_as_member.buffer;
names.presence_enum = b_presence_enum.buffer;
names.presence_name = b_presence_name.buffer;
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);
return names;
}
struct c_names
c_name(arg_t *arg) {
return c_name_impl(arg, 1);
}
const char *
c_member_name(arg_t *arg, asn1p_expr_t *expr) {
static abuf ab;
abuf_clear(&ab);
abuf_printf(&ab, "%s_%s", c_name_impl(arg, 0).base_name,
asn1c_make_identifier(0, expr, 0));
return ab.buffer;
}
const char *
c_presence_name(arg_t *arg, asn1p_expr_t *expr) {
static abuf ab;
abuf_clear(&ab);
if(expr) {
abuf_printf(&ab, "%s_PR_%s", c_name_impl(arg, 0).base_name,
asn1c_make_identifier(0, expr, 0));
} else {
abuf_printf(&ab, "%s_PR_NOTHING", c_name_impl(arg, 0).base_name);
}
return ab.buffer;
}
#ifndef ASN1_COMPILER_NAMING_H
#define ASN1_COMPILER_NAMING_H
struct c_names {
const char *base_name; /* "foo" */
const char *short_name; /* "foo_t", "e_foo" */
const char *full_name; /* "struct foo", "enum foo" */
const char *as_member; /* "foo" (not compounded) */
const char *presence_enum; /* "enum foo_PR" */
const char *presence_name; /* "foo_PR" */
const char *members_enum; /* "enum foo" */
const char *members_name; /* "e_foo" */
};
struct c_names c_name(arg_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 */
#endif /* ASN1_COMPILER_NAMING_H */
......@@ -243,7 +243,7 @@ asn1c_save_streams(arg_t *arg, asn1c_fdeps_t *deps, int optc, char **argv) {
FILE *fp_c, *fp_h;
char *tmpname_c, *tmpname_h;
char *name_buf;
char *header_id;
const char *header_id;
const char *c_retained = "";
const char *h_retained = "";
char *filename;
......
This diff is collapsed.
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