2004-06-03 03:38:44 +00:00
|
|
|
#include "asn1c_internal.h"
|
2004-08-20 13:37:01 +00:00
|
|
|
#include "asn1c_misc.h"
|
|
|
|
|
2005-08-14 02:18:27 +00:00
|
|
|
#include <asn1fix_crange.h> /* constraint groker from libasn1fix */
|
|
|
|
#include <asn1fix_export.h> /* other exportable stuff from libasn1fix */
|
2004-06-03 03:38:44 +00:00
|
|
|
|
2005-01-28 12:18:50 +00:00
|
|
|
/*
|
|
|
|
* Checks that the given string is not a reserved C/C++ keyword.
|
2005-03-03 21:29:27 +00:00
|
|
|
* ISO/IEC 9899:1999 (C99), A.1.2
|
2005-01-28 12:18:50 +00:00
|
|
|
*/
|
|
|
|
static char *res_kwd[] = {
|
2005-03-03 21:29:27 +00:00
|
|
|
"const", "auto", "register", "volatile", "restrict", "extern",
|
|
|
|
"static", "inline",
|
|
|
|
"signed", "unsigned",
|
|
|
|
"void", "char", "short", "int", "long", "float", "double",
|
|
|
|
"enum", "union", "struct", "typedef", "class",
|
|
|
|
"switch", "case", "default", "break", "continue",
|
|
|
|
"if", "else", "do", "for", "while", "goto",
|
|
|
|
"sizeof", "return"
|
|
|
|
};
|
2005-01-28 12:18:50 +00:00
|
|
|
static int
|
|
|
|
reserved_keyword(const char *str) {
|
2006-08-18 02:27:55 +00:00
|
|
|
size_t i;
|
2005-01-28 12:18:50 +00:00
|
|
|
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 *
|
2006-03-21 03:40:38 +00:00
|
|
|
asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) {
|
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;
|
2006-03-21 03:40:38 +00:00
|
|
|
char *first = 0;
|
|
|
|
char *second = 0;
|
2006-08-18 02:27:55 +00:00
|
|
|
ssize_t size;
|
2004-06-03 03:38:44 +00:00
|
|
|
char *p;
|
|
|
|
|
2006-03-21 03:40:38 +00:00
|
|
|
if(expr) {
|
|
|
|
/*
|
|
|
|
* Estimate the necessary storage size
|
|
|
|
*/
|
|
|
|
if(expr->Identifier == NULL)
|
|
|
|
return "Member";
|
|
|
|
size = strlen(expr->Identifier);
|
|
|
|
if(expr->spec_index != -1) {
|
|
|
|
static char buf[32];
|
|
|
|
second = buf;
|
|
|
|
size += 1 + snprintf(buf, sizeof buf, "%dP%d",
|
|
|
|
expr->_lineno, expr->spec_index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size = -1;
|
|
|
|
}
|
2004-06-03 03:38:44 +00:00
|
|
|
|
2006-03-21 03:40:38 +00:00
|
|
|
va_start(ap, expr);
|
2004-06-03 03:38:44 +00:00
|
|
|
while((str = va_arg(ap, char *)))
|
|
|
|
size += 1 + strlen(str);
|
|
|
|
va_end(ap);
|
2006-03-21 03:40:38 +00:00
|
|
|
if(size == -1) return NULL;
|
2004-06-03 03:38:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-03-21 03:40:38 +00:00
|
|
|
va_start(ap, expr);
|
2004-06-03 03:38:44 +00:00
|
|
|
p = storage;
|
2006-03-21 03:40:38 +00:00
|
|
|
nextstr = "";
|
|
|
|
for(p = storage, str = 0; str || nextstr; str = nextstr) {
|
2004-06-03 03:38:44 +00:00
|
|
|
int subst_made = 0;
|
2006-03-21 03:40:38 +00:00
|
|
|
nextstr = second ? second : va_arg(ap, char *);
|
|
|
|
|
|
|
|
if(str == 0) {
|
|
|
|
if(expr) {
|
|
|
|
str = expr->Identifier;
|
|
|
|
first = str;
|
|
|
|
second = 0;
|
|
|
|
} else {
|
|
|
|
first = nextstr;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2004-06-03 03:38:44 +00:00
|
|
|
|
|
|
|
if(str[0] == ' ' && str[1] == '\0') {
|
|
|
|
*p++ = ' ';
|
|
|
|
nodelimiter = 1; /* No delimiter */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-09-13 02:51:20 +00:00
|
|
|
if(str != first && !nodelimiter && !(flags & AMI_NODELIMITER))
|
2004-06-03 03:38:44 +00:00
|
|
|
*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)
|
2006-03-21 03:40:38 +00:00
|
|
|
&& str == first && !nextstr && reserved_keyword(str)) {
|
2005-01-28 12:18:50 +00:00
|
|
|
*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) {
|
2006-03-21 03:40:38 +00:00
|
|
|
asn1p_expr_t *exprid = 0;
|
2004-09-08 00:28:57 +00:00
|
|
|
asn1p_expr_t *top_parent;
|
2006-03-21 03:40:38 +00:00
|
|
|
asn1p_expr_t *terminal;
|
2006-09-13 02:51:20 +00:00
|
|
|
int stdname = 0;
|
2004-06-03 03:38:44 +00:00
|
|
|
char *typename;
|
|
|
|
|
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;
|
|
|
|
|
2006-03-14 11:52:12 +00:00
|
|
|
DEBUG("asn1c_type_name(%s: 0x%x)", expr->Identifier, expr->expr_type);
|
|
|
|
|
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.
|
|
|
|
*/
|
2005-08-18 13:38:19 +00:00
|
|
|
tmp.expr = asn1f_class_access_ex(arg->asn,
|
2006-03-21 03:40:38 +00:00
|
|
|
arg->expr->module, arg->expr, expr->rhs_pspecs, expr->reference);
|
2006-03-14 11:52:12 +00:00
|
|
|
if(!tmp.expr) return NULL;
|
2004-06-03 03:38:44 +00:00
|
|
|
|
|
|
|
return asn1c_type_name(&tmp, tmp.expr, _format);
|
2004-09-08 00:28:57 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 03:40:38 +00:00
|
|
|
terminal = asn1f_find_terminal_type_ex(arg->asn, expr);
|
|
|
|
|
2005-03-04 22:18:20 +00:00
|
|
|
if(_format == TNF_RSAFE) {
|
|
|
|
if(terminal && terminal->expr_type & ASN_CONSTR_MASK) {
|
|
|
|
typename = terminal->Identifier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2005-03-04 22:18:20 +00:00
|
|
|
* switch to a recursion-safe type naming
|
2004-09-17 06:32:12 +00:00
|
|
|
* ("struct foo" instead of "foo_t").
|
2004-06-03 03:38:44 +00:00
|
|
|
*/
|
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
|
|
|
}
|
2006-03-21 03:40:38 +00:00
|
|
|
|
|
|
|
if(terminal && terminal->spec_index != -1) {
|
|
|
|
exprid = terminal;
|
|
|
|
typename = 0;
|
2004-06-03 03:38:44 +00:00
|
|
|
}
|
2006-03-21 03:40:38 +00:00
|
|
|
|
2004-06-03 03:38:44 +00:00
|
|
|
break;
|
|
|
|
case ASN_BASIC_INTEGER:
|
|
|
|
case ASN_BASIC_ENUMERATED:
|
2004-09-14 12:47:45 +00:00
|
|
|
case ASN_BASIC_REAL:
|
2005-08-14 02:18:27 +00:00
|
|
|
if((expr->expr_type == ASN_BASIC_REAL
|
|
|
|
&& (arg->flags & A1C_USE_NATIVE_TYPES))
|
|
|
|
|| asn1c_type_fits_long(arg, expr)) {
|
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
|
2005-02-25 12:09:55 +00:00
|
|
|
return "long";
|
2006-09-13 02:51:20 +00:00
|
|
|
default:
|
|
|
|
typename = 0;
|
|
|
|
switch(expr->expr_type) {
|
|
|
|
case ASN_BASIC_INTEGER:
|
|
|
|
typename = "NativeInteger"; break;
|
|
|
|
case ASN_BASIC_ENUMERATED:
|
|
|
|
typename = "NativeEnumerated"; break;
|
|
|
|
case ASN_BASIC_REAL:
|
|
|
|
typename = "NativeReal"; break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2004-09-14 12:47:45 +00:00
|
|
|
}
|
2006-09-13 02:51:20 +00:00
|
|
|
if(typename) {
|
|
|
|
if(_format != TNF_INCLUDE)
|
|
|
|
return typename;
|
|
|
|
stdname = 1;
|
|
|
|
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;
|
2006-09-13 02:51:20 +00:00
|
|
|
stdname = 1;
|
2004-06-03 03:38:44 +00:00
|
|
|
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:
|
2006-03-21 03:40:38 +00:00
|
|
|
return asn1c_make_identifier(AMI_MASK_ONLY_SPACES,
|
|
|
|
0, exprid ? exprid->Identifier : typename, 0);
|
2006-09-13 02:51:20 +00:00
|
|
|
case TNF_INCLUDE:
|
|
|
|
return asn1c_make_identifier(
|
|
|
|
AMI_MASK_ONLY_SPACES | AMI_NODELIMITER,
|
|
|
|
0, stdname ? "<" : "\"",
|
|
|
|
exprid ? exprid->Identifier : typename,
|
|
|
|
stdname ? ".h>" : ".h\"", 0);
|
2004-06-03 03:38:44 +00:00
|
|
|
case TNF_SAFE:
|
2006-03-21 03:40:38 +00:00
|
|
|
return asn1c_make_identifier(0, exprid, typename, 0);
|
2005-03-04 22:18:20 +00:00
|
|
|
case TNF_CTYPE: /* C type */
|
2006-03-21 03:40:38 +00:00
|
|
|
return asn1c_make_identifier(0, exprid,
|
|
|
|
exprid?"t":typename, exprid?0:"t", 0);
|
2005-03-04 22:18:20 +00:00
|
|
|
case TNF_RSAFE: /* Recursion-safe type */
|
2006-03-21 03:40:38 +00:00
|
|
|
return asn1c_make_identifier(AMI_CHECK_RESERVED, 0,
|
2005-03-04 22:18:20 +00:00
|
|
|
"struct", " ", typename, 0);
|
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;
|
|
|
|
}
|
|
|
|
|
2005-08-14 02:18:27 +00:00
|
|
|
/*
|
|
|
|
* Check whether the specified INTEGER or ENUMERATED type can be represented
|
|
|
|
* using the generic 'long' type.
|
|
|
|
*/
|
|
|
|
enum asn1c_fitslong_e
|
|
|
|
asn1c_type_fits_long(arg_t *arg, asn1p_expr_t *expr) {
|
|
|
|
asn1cnst_range_t *range = 0;
|
|
|
|
asn1cnst_edge_t left;
|
|
|
|
asn1cnst_edge_t right;
|
|
|
|
asn1p_expr_t *v;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since we don't know the sizeof(long) on the possible target platform
|
|
|
|
* which will be compiling the code generated by asn1c, let's play it
|
|
|
|
* simple: long's range is equal to or greater than int32_t.
|
2005-08-15 01:01:24 +00:00
|
|
|
* NOTE: the most negative integer cannot be written in C, as the C99
|
|
|
|
* standard will give it an unsigned type.
|
|
|
|
* It is defined here as a constant expression.
|
2005-08-14 02:18:27 +00:00
|
|
|
*/
|
2005-08-15 01:04:48 +00:00
|
|
|
#define RIGHTMAX 2147483647 /* of 32-bit integer type */
|
|
|
|
#define LEFTMIN (-RIGHTMAX-1) /* of 32-bit integer type */
|
2005-08-14 02:18:27 +00:00
|
|
|
|
|
|
|
/* Descend to the terminal type */
|
|
|
|
expr = asn1f_find_terminal_type_ex(arg->asn, expr);
|
|
|
|
if(expr == 0) return FL_NOTFIT;
|
|
|
|
|
|
|
|
/* The "fits into long" operation is relevant only for integer types */
|
|
|
|
switch(expr->expr_type) {
|
|
|
|
case ASN_BASIC_INTEGER:
|
|
|
|
case ASN_BASIC_ENUMERATED:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FL_NOTFIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, evaluate the range of explicitly given identifiers.
|
|
|
|
*/
|
|
|
|
TQ_FOR(v, &(expr->members), next) {
|
|
|
|
if(v->expr_type != A1TC_UNIVERVAL)
|
|
|
|
continue;
|
|
|
|
if(v->value->value.v_integer < LEFTMIN
|
|
|
|
|| v->value->value.v_integer > RIGHTMAX)
|
|
|
|
return FL_NOTFIT;
|
|
|
|
}
|
|
|
|
|
2005-08-14 14:45:44 +00:00
|
|
|
if(!expr->combined_constraints)
|
|
|
|
return (arg->flags & A1C_USE_NATIVE_TYPES)
|
|
|
|
? FL_FORCED : FL_NOTFIT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Second, if -fbless-SIZE is given, the (SIZE()) constraint may be
|
|
|
|
* applied (non-standard! but we can deal with this) to the type.
|
|
|
|
* Check the range.
|
|
|
|
*/
|
|
|
|
range = asn1constraint_compute_PER_range(expr->expr_type,
|
|
|
|
expr->combined_constraints, ACT_CT_SIZE, 0, 0,
|
|
|
|
CPR_simulate_fbless_SIZE);
|
|
|
|
if(range) {
|
|
|
|
if(!range->incompatible) {
|
|
|
|
right = range->right;
|
|
|
|
/* Use 4 instead of sizeof(long) is justified! */
|
|
|
|
if(right.type == ARE_VALUE && right.value <= 4)
|
|
|
|
return FL_FITSOK;
|
|
|
|
}
|
|
|
|
asn1constraint_range_free(range);
|
|
|
|
}
|
|
|
|
|
2005-08-14 02:18:27 +00:00
|
|
|
/*
|
2005-08-14 14:45:44 +00:00
|
|
|
* Third, pull up the PER visible range of the INTEGER.
|
2005-08-14 02:18:27 +00:00
|
|
|
*/
|
2005-08-14 14:45:44 +00:00
|
|
|
range = asn1constraint_compute_PER_range(expr->expr_type,
|
|
|
|
expr->combined_constraints, ACT_EL_RANGE, 0, 0, 0);
|
2005-08-14 02:18:27 +00:00
|
|
|
if(!range
|
|
|
|
|| range->empty_constraint
|
|
|
|
|| range->extensible
|
|
|
|
|| range->incompatible
|
|
|
|
|| range->not_PER_visible
|
|
|
|
) {
|
|
|
|
asn1constraint_range_free(range);
|
|
|
|
return (arg->flags & A1C_USE_NATIVE_TYPES)
|
|
|
|
? FL_FORCED : FL_NOTFIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
left = range->left;
|
|
|
|
right = range->right;
|
|
|
|
asn1constraint_range_free(range);
|
|
|
|
|
|
|
|
/* If some fixed value is outside of target range, not fit */
|
|
|
|
if(left.type == ARE_VALUE
|
|
|
|
&& (left.value < LEFTMIN || left.value > RIGHTMAX))
|
|
|
|
return FL_NOTFIT;
|
|
|
|
if(right.type == ARE_VALUE
|
|
|
|
&& (right.value > RIGHTMAX || right.value < LEFTMIN))
|
|
|
|
return FL_NOTFIT;
|
|
|
|
|
|
|
|
/* If the range is open, fits only if -fnative-types is given */
|
|
|
|
if(left.type != ARE_VALUE || right.type != ARE_VALUE) {
|
|
|
|
return (arg->flags & A1C_USE_NATIVE_TYPES)
|
|
|
|
? FL_FORCED : FL_NOTFIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FL_FITSOK;
|
|
|
|
}
|
|
|
|
|