2004-06-03 03:38:44 +00:00
|
|
|
#include "asn1c_internal.h"
|
2004-08-20 13:37:01 +00:00
|
|
|
#include "asn1c_misc.h"
|
|
|
|
|
2004-06-03 03:38:44 +00:00
|
|
|
#include <asn1fix_export.h>
|
|
|
|
|
2005-01-28 12:18:50 +00:00
|
|
|
/*
|
|
|
|
* Checks that the given string is not a reserved C/C++ keyword.
|
|
|
|
*/
|
|
|
|
static char *res_kwd[] = {
|
|
|
|
"char", "int", "long",
|
|
|
|
"float", "double",
|
|
|
|
"struct", "typedef", "class" };
|
|
|
|
static int
|
|
|
|
reserved_keyword(const char *str) {
|
|
|
|
int i;
|
|
|
|
for(i = 0 ; i < sizeof(res_kwd)/sizeof(res_kwd[0]); i++) {
|
|
|
|
if(strcmp(str, res_kwd[i]) == 0)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-06-03 03:38:44 +00:00
|
|
|
/*
|
|
|
|
* Construct identifier from multiple parts.
|
|
|
|
* Convert unsafe characters to underscores.
|
|
|
|
*/
|
|
|
|
char *
|
2005-01-28 12:18:50 +00:00
|
|
|
asn1c_make_identifier(enum ami_flags_e flags, char *arg1, ...) {
|
2004-06-03 03:38:44 +00:00
|
|
|
static char *storage;
|
|
|
|
static int storage_size;
|
|
|
|
int nodelimiter = 0;
|
|
|
|
va_list ap;
|
|
|
|
char *str;
|
2005-01-28 12:18:50 +00:00
|
|
|
char *nextstr;
|
2004-06-03 03:38:44 +00:00
|
|
|
int size;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if(arg1 == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Estimate the necessary storage size
|
|
|
|
*/
|
|
|
|
size = strlen(arg1);
|
|
|
|
va_start(ap, arg1);
|
|
|
|
while((str = va_arg(ap, char *)))
|
|
|
|
size += 1 + strlen(str);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure we have this amount of storage.
|
|
|
|
*/
|
|
|
|
if(storage_size <= size) {
|
|
|
|
if(storage) free(storage);
|
|
|
|
storage = malloc(size + 1);
|
|
|
|
if(storage) {
|
|
|
|
storage_size = size;
|
|
|
|
} else {
|
|
|
|
storage_size = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fill-in the storage.
|
|
|
|
*/
|
|
|
|
va_start(ap, arg1);
|
|
|
|
str = arg1;
|
|
|
|
p = storage;
|
2005-01-28 12:18:50 +00:00
|
|
|
for(str = arg1; str; str = nextstr) {
|
2004-06-03 03:38:44 +00:00
|
|
|
int subst_made = 0;
|
2005-01-28 12:18:50 +00:00
|
|
|
nextstr = va_arg(ap, char *);
|
2004-06-03 03:38:44 +00:00
|
|
|
|
|
|
|
if(str[0] == ' ' && str[1] == '\0') {
|
|
|
|
*p++ = ' ';
|
|
|
|
nodelimiter = 1; /* No delimiter */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(str != arg1 && !nodelimiter)
|
|
|
|
*p++ = '_'; /* Delimiter between tokens */
|
|
|
|
nodelimiter = 0;
|
|
|
|
|
2005-01-28 12:18:50 +00:00
|
|
|
/*
|
|
|
|
* If it is a single argument, check that it does not clash
|
|
|
|
* with C/C++ language keywords.
|
|
|
|
*/
|
|
|
|
if((flags & AMI_CHECK_RESERVED)
|
|
|
|
&& str == arg1 && !nextstr && reserved_keyword(str)) {
|
|
|
|
*p++ = toupper(*str++);
|
|
|
|
/* Fall through */
|
|
|
|
}
|
|
|
|
|
2004-06-03 03:38:44 +00:00
|
|
|
for(; *str; str++) {
|
|
|
|
if(isalnum(*str)) {
|
|
|
|
*p++ = *str;
|
|
|
|
subst_made = 0;
|
|
|
|
} else if(!subst_made++) {
|
2005-01-28 12:18:50 +00:00
|
|
|
if((flags & AMI_MASK_ONLY_SPACES)
|
|
|
|
&& !isspace(*str)) {
|
2004-06-03 03:38:44 +00:00
|
|
|
*p ++ = *str;
|
|
|
|
} else {
|
|
|
|
*p++ = '_';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
assert((p - storage) <= storage_size);
|
|
|
|
|
|
|
|
return storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) {
|
2004-09-08 00:28:57 +00:00
|
|
|
asn1p_expr_t *top_parent;
|
2004-06-03 03:38:44 +00:00
|
|
|
char *typename;
|
2005-01-28 12:18:50 +00:00
|
|
|
enum ami_flags_e ami_flags = (_format & TNF_CHECK)
|
|
|
|
? AMI_CHECK_RESERVED : 0;
|
|
|
|
_format &= ~TNF_CHECK;
|
2004-06-03 03:38:44 +00:00
|
|
|
|
2004-09-08 00:28:57 +00:00
|
|
|
/* Rewind to the topmost parent expression */
|
|
|
|
if((top_parent = expr->parent_expr))
|
|
|
|
while(top_parent->parent_expr)
|
|
|
|
top_parent = top_parent->parent_expr;
|
|
|
|
|
2004-06-03 03:38:44 +00:00
|
|
|
switch(expr->expr_type) {
|
|
|
|
case A1TC_REFERENCE:
|
|
|
|
typename = expr->reference->components[
|
|
|
|
expr->reference->comp_count-1].name;
|
|
|
|
if(typename[0] == '&') {
|
|
|
|
arg_t tmp = *arg;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a reference to a type defined in a class.
|
|
|
|
* Resolve it and use instead.
|
|
|
|
*/
|
|
|
|
tmp.expr = asn1f_class_access_ex(arg->asn, arg->mod,
|
2004-08-22 03:23:15 +00:00
|
|
|
arg->expr, expr->reference);
|
2004-06-03 03:38:44 +00:00
|
|
|
if(tmp.expr) return NULL;
|
|
|
|
|
2004-08-22 03:23:15 +00:00
|
|
|
tmp.mod = tmp.expr->module;
|
2004-06-03 03:38:44 +00:00
|
|
|
return asn1c_type_name(&tmp, tmp.expr, _format);
|
2004-09-08 00:28:57 +00:00
|
|
|
}
|
|
|
|
|
2004-09-17 06:32:12 +00:00
|
|
|
if(_format == TNF_CTYPE) {
|
2004-06-03 03:38:44 +00:00
|
|
|
/*
|
2004-09-17 06:32:12 +00:00
|
|
|
* If the component references the type itself,
|
|
|
|
* switch to a recursion safe type representation
|
|
|
|
* ("struct foo" instead of "foo_t").
|
2004-06-03 03:38:44 +00:00
|
|
|
*/
|
|
|
|
asn1p_expr_t *terminal;
|
2004-09-22 16:05:13 +00:00
|
|
|
terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
|
2004-09-17 06:32:12 +00:00
|
|
|
if(terminal && terminal == top_parent) {
|
|
|
|
_format = TNF_RSAFE;
|
2004-09-08 00:28:57 +00:00
|
|
|
}
|
2004-06-03 03:38:44 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-08-25 02:03:26 +00:00
|
|
|
#if 0
|
2004-06-03 03:38:44 +00:00
|
|
|
case ASN_CONSTR_SEQUENCE_OF:
|
|
|
|
case ASN_CONSTR_SET_OF:
|
|
|
|
if(expr->Identifier) {
|
|
|
|
typename = expr->Identifier;
|
|
|
|
} else {
|
|
|
|
asn1p_expr_t *child;
|
|
|
|
child = TQ_FIRST(&(expr->members));
|
|
|
|
typename = asn1c_type_name(arg, child, _format);
|
|
|
|
if(typename)
|
|
|
|
return typename;
|
|
|
|
_format = TNF_SAFE;
|
|
|
|
typename = child->Identifier;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-25 02:03:26 +00:00
|
|
|
#endif
|
2004-06-03 03:38:44 +00:00
|
|
|
case ASN_BASIC_INTEGER:
|
|
|
|
case ASN_BASIC_ENUMERATED:
|
2004-09-14 12:47:45 +00:00
|
|
|
case ASN_BASIC_REAL:
|
|
|
|
if((arg->flags & A1C_USE_NATIVE_TYPES)) {
|
2004-06-03 03:38:44 +00:00
|
|
|
switch(_format) {
|
|
|
|
case TNF_CTYPE:
|
|
|
|
case TNF_RSAFE:
|
2004-09-14 12:47:45 +00:00
|
|
|
if(expr->expr_type == ASN_BASIC_REAL)
|
|
|
|
return "double";
|
2004-06-03 03:38:44 +00:00
|
|
|
else
|
2004-09-14 12:47:45 +00:00
|
|
|
return "int";
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
switch(expr->expr_type) {
|
|
|
|
case ASN_BASIC_INTEGER:
|
|
|
|
return "NativeInteger";
|
|
|
|
case ASN_BASIC_ENUMERATED:
|
|
|
|
return "NativeEnumerated";
|
|
|
|
case ASN_BASIC_REAL:
|
|
|
|
return "NativeReal";
|
|
|
|
default: break;
|
2004-06-03 03:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Fall through */
|
|
|
|
default:
|
2004-08-25 02:03:26 +00:00
|
|
|
if(expr->expr_type
|
|
|
|
& (ASN_CONSTR_MASK | ASN_BASIC_MASK | ASN_STRING_MASK)) {
|
2004-06-03 03:38:44 +00:00
|
|
|
if(_format == TNF_RSAFE)
|
|
|
|
_format = TNF_CTYPE;
|
|
|
|
typename = ASN_EXPR_TYPE2STR(expr->expr_type);
|
|
|
|
} else {
|
2004-09-17 06:32:12 +00:00
|
|
|
_format = TNF_RSAFE;
|
2004-06-03 03:38:44 +00:00
|
|
|
typename = expr->Identifier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(_format) {
|
|
|
|
case TNF_UNMODIFIED:
|
|
|
|
case TNF_INCLUDE:
|
2005-01-28 12:18:50 +00:00
|
|
|
assert(ami_flags == 0); /* (TNF_INCLUDE | TNF_CHECK)?! */
|
|
|
|
ami_flags |= AMI_MASK_ONLY_SPACES;
|
|
|
|
return asn1c_make_identifier(ami_flags, typename, 0);
|
2004-06-03 03:38:44 +00:00
|
|
|
case TNF_SAFE:
|
2005-01-28 12:18:50 +00:00
|
|
|
return asn1c_make_identifier(ami_flags, typename, 0);
|
2004-06-03 03:38:44 +00:00
|
|
|
case TNF_CTYPE:
|
2005-01-28 12:18:50 +00:00
|
|
|
return asn1c_make_identifier(ami_flags, typename, "t", 0);
|
2004-06-03 03:38:44 +00:00
|
|
|
case TNF_RSAFE:
|
2005-01-28 12:18:50 +00:00
|
|
|
return asn1c_make_identifier(ami_flags, "struct", " ", typename, 0);
|
|
|
|
case TNF_NORCHECK:
|
|
|
|
case TNF_CHECK:
|
|
|
|
assert(_format != TNF_NORCHECK);
|
|
|
|
assert(_format != TNF_CHECK);
|
2004-06-03 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
2004-09-17 06:32:12 +00:00
|
|
|
assert(!"unreachable");
|
2004-06-03 03:38:44 +00:00
|
|
|
return typename;
|
|
|
|
}
|
|
|
|
|