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
|
|
|
/*
|
2016-10-24 13:22:57 +00:00
|
|
|
* Checks that the given string is not a reserved C/C++ keyword [1],[2].
|
|
|
|
* _* keywords not included, since asn1 identifiers cannot begin with hyphen [3]
|
|
|
|
* [1] ISO/IEC 9899:2011 (C11), 6.4.1
|
|
|
|
* [2] ISO/IEC 14882:2014 (C++14), 2.12
|
|
|
|
* [3] ISO/IEC 8824-1:2003 (asn1) 11.3
|
2005-01-28 12:18:50 +00:00
|
|
|
*/
|
|
|
|
static char *res_kwd[] = {
|
2016-10-24 13:22:57 +00:00
|
|
|
/* C */
|
|
|
|
"auto", "break", "case", "char", "const", "continue", "default", "do",
|
|
|
|
"double", "else", "enum", "extern", "float", "for", "goto", "if",
|
|
|
|
"inline", "int", "long", "register", "restrict", "return", "short",
|
|
|
|
"signed", "sizeof", "static", "struct", "switch", "typedef", "union",
|
|
|
|
"unsigned", "void", "volatile", "while",
|
|
|
|
/* C++ */
|
|
|
|
"alignas", "alignof", "and", "and_eq", "asm", "bitand", "bitor", "bool",
|
|
|
|
"catch", "char16_t", "char32_t", "class", "compl", "const_cast",
|
|
|
|
"constexpr", "decltype", "delete", "delete", "dynamic_cast",
|
|
|
|
"explicit", "export", "false", "friend", "mutable", "namespace", "new",
|
|
|
|
"noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq",
|
|
|
|
"private", "protected", "public", "reinterpret_cast", "static_assert",
|
|
|
|
"static_cast", "template", "this", "thread_local", "throw", "true", "try",
|
|
|
|
"typeid", "typename", "using", "virtual", "wchar_t", "xor", "xor_eq"
|
2005-03-03 21:29:27 +00:00
|
|
|
};
|
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;
|
2017-05-07 13:00:51 +00:00
|
|
|
ssize_t size = 0;
|
2004-06-03 03:38:44 +00:00
|
|
|
char *p;
|
2017-05-07 13:00:51 +00:00
|
|
|
char *prefix = NULL;
|
|
|
|
char *sptr[4], **psptr = &sptr[0];
|
|
|
|
int sptr_cnt = 0;
|
2004-06-03 03:38:44 +00:00
|
|
|
|
2006-03-21 03:40:38 +00:00
|
|
|
if(expr) {
|
|
|
|
/*
|
|
|
|
* Estimate the necessary storage size
|
|
|
|
*/
|
|
|
|
if(expr->Identifier == NULL)
|
|
|
|
return "Member";
|
2017-05-07 13:00:51 +00:00
|
|
|
/*
|
|
|
|
* Add MODULE name to resolve clash
|
|
|
|
*/
|
|
|
|
if(expr->_mark & TM_NAMECLASH) {
|
|
|
|
size += strlen(expr->module->ModuleName) + 2;
|
|
|
|
sptr[sptr_cnt++] = expr->module->ModuleName;
|
|
|
|
}
|
|
|
|
sptr[sptr_cnt++] = expr->Identifier;
|
|
|
|
|
|
|
|
size += strlen(expr->Identifier);
|
2006-03-21 03:40:38 +00:00
|
|
|
if(expr->spec_index != -1) {
|
|
|
|
static char buf[32];
|
|
|
|
size += 1 + snprintf(buf, sizeof buf, "%dP%d",
|
|
|
|
expr->_lineno, expr->spec_index);
|
2017-05-07 13:00:51 +00:00
|
|
|
sptr[sptr_cnt++] = (char *)&buf;
|
2006-03-21 03:40:38 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size = -1;
|
|
|
|
}
|
2017-05-07 13:00:51 +00:00
|
|
|
sptr[sptr_cnt++] = (char *)0;
|
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
|
|
|
|
2017-05-07 13:00:51 +00:00
|
|
|
if(prefix)
|
|
|
|
size += 1 + strlen(prefix);
|
2004-06-03 03:38:44 +00:00
|
|
|
/*
|
2017-06-27 15:19:11 +00:00
|
|
|
* Make sure we have the required amount of storage.
|
2004-06-03 03:38:44 +00:00
|
|
|
*/
|
|
|
|
if(storage_size <= size) {
|
2017-06-27 15:19:11 +00:00
|
|
|
char *tmp = malloc(size + 1);
|
|
|
|
if(tmp) {
|
|
|
|
free(storage);
|
|
|
|
storage = tmp;
|
|
|
|
storage_size = size + 1;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-06-03 03:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2017-05-07 13:00:51 +00:00
|
|
|
if(prefix) {
|
|
|
|
strcpy(storage, prefix);
|
|
|
|
p += strlen(prefix);
|
|
|
|
}
|
2006-03-21 03:40:38 +00:00
|
|
|
nextstr = "";
|
2017-05-07 13:00:51 +00:00
|
|
|
for(str = 0; str || nextstr; str = nextstr) {
|
2004-06-03 03:38:44 +00:00
|
|
|
int subst_made = 0;
|
2017-05-07 13:00:51 +00:00
|
|
|
nextstr = *(psptr) ? *(psptr++) : va_arg(ap, char *);
|
2006-03-21 03:40:38 +00:00
|
|
|
|
|
|
|
if(str == 0) {
|
2017-05-07 13:00:51 +00:00
|
|
|
str = first = nextstr;
|
|
|
|
nextstr = *(psptr) ? *(psptr++) : va_arg(ap, char *);
|
|
|
|
if (!first) continue;
|
2006-03-21 03:40:38 +00:00
|
|
|
}
|
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;
|
2017-05-07 13:00:51 +00:00
|
|
|
asn1p_expr_t *terminal = 0;
|
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-10-09 12:07:58 +00:00
|
|
|
if(0) DEBUG("asn1c_type_name(%s: 0x%x)",
|
|
|
|
expr->Identifier, expr->expr_type);
|
2006-03-14 11:52:12 +00:00
|
|
|
|
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
|
|
|
|
2017-06-28 17:34:23 +00:00
|
|
|
if(_format != TNF_RSAFE && terminal && ((terminal->spec_index != -1) || (terminal->_mark & TM_NAMECLASH))) {
|
2006-03-21 03:40:38 +00:00
|
|
|
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
|
2013-03-27 08:56:23 +00:00
|
|
|
&& !(arg->flags & A1C_USE_WIDE_TYPES))
|
2005-08-14 02:18:27 +00:00
|
|
|
|| 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";
|
2007-12-03 13:41:36 +00:00
|
|
|
else if(asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN)
|
|
|
|
return "unsigned long";
|
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:
|
2017-05-07 13:00:51 +00:00
|
|
|
return asn1c_make_identifier(AMI_MASK_ONLY_SPACES | AMI_NODELIMITER,
|
|
|
|
0, MODULE_NAME_OF(exprid), exprid ? exprid->Identifier : typename, (char*)0);
|
2006-09-13 02:51:20 +00:00
|
|
|
case TNF_INCLUDE:
|
|
|
|
return asn1c_make_identifier(
|
|
|
|
AMI_MASK_ONLY_SPACES | AMI_NODELIMITER,
|
2010-10-16 08:26:57 +00:00
|
|
|
0, ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED))
|
|
|
|
? "\"" : "<"),
|
2017-05-07 13:00:51 +00:00
|
|
|
MODULE_NAME_OF(exprid),
|
2006-09-13 02:51:20 +00:00
|
|
|
exprid ? exprid->Identifier : typename,
|
2010-10-16 08:26:57 +00:00
|
|
|
((!stdname || (arg->flags & A1C_INCLUDES_QUOTED))
|
Fix segmentation fault in asn1c_make_identifier
See also http://stackoverflow.com/questions/25390720/va-arg-gives-something-strange-in-cygwin-x64
Program received signal SIGSEGV, Segmentation fault.
(gdb) bt
#0 0x00007ffff7b7c8f1 in __strlen_sse2_pminub () from /lib64/libc.so.6
#1 0x0000000000425d29 in asn1c_make_identifier (flags=flags@entry=(AMI_MASK_ONLY_SPACES | AMI_NODELIMITER), expr=expr@entry=0x0) at asn1c_misc.c:73
#2 0x00000000004263ec in asn1c_type_name (arg=arg@entry=0x7fffffffe000, expr=expr@entry=0x6757c0, _format=<optimized out>,
_format@entry=TNF_INCLUDE) at asn1c_misc.c:268
#3 0x00000000004226fc in asn1c_lang_C_type_SIMPLE_TYPE (arg=arg@entry=0x7fffffffe000) at asn1c_C.c:1119
#4 0x00000000004232c1 in asn1c_lang_C_type_common_INTEGER (arg=0x7fffffffe000) at asn1c_C.c:251
#5 0x0000000000419c75 in asn1c_compile_expr (arg=arg@entry=0x7fffffffe000) at asn1compiler.c:100
#6 0x0000000000419f33 in asn1_compile (asn=asn@entry=0x671e10, datadir=datadir@entry=0x426730 "/home/vasko/usr/share/asn1c",
flags=flags@entry=A1C_NO_C99, argc=2, optc=0, argv=0x7fffffffe268) at asn1compiler.c:44
#7 0x0000000000401f13 in main (ac=1, av=0x7fffffffe270) at asn1c.c:328
2016-07-18 17:24:49 +00:00
|
|
|
? ".h\"" : ".h>"), (char*)0);
|
2004-06-03 03:38:44 +00:00
|
|
|
case TNF_SAFE:
|
Fix segmentation fault in asn1c_make_identifier
See also http://stackoverflow.com/questions/25390720/va-arg-gives-something-strange-in-cygwin-x64
Program received signal SIGSEGV, Segmentation fault.
(gdb) bt
#0 0x00007ffff7b7c8f1 in __strlen_sse2_pminub () from /lib64/libc.so.6
#1 0x0000000000425d29 in asn1c_make_identifier (flags=flags@entry=(AMI_MASK_ONLY_SPACES | AMI_NODELIMITER), expr=expr@entry=0x0) at asn1c_misc.c:73
#2 0x00000000004263ec in asn1c_type_name (arg=arg@entry=0x7fffffffe000, expr=expr@entry=0x6757c0, _format=<optimized out>,
_format@entry=TNF_INCLUDE) at asn1c_misc.c:268
#3 0x00000000004226fc in asn1c_lang_C_type_SIMPLE_TYPE (arg=arg@entry=0x7fffffffe000) at asn1c_C.c:1119
#4 0x00000000004232c1 in asn1c_lang_C_type_common_INTEGER (arg=0x7fffffffe000) at asn1c_C.c:251
#5 0x0000000000419c75 in asn1c_compile_expr (arg=arg@entry=0x7fffffffe000) at asn1compiler.c:100
#6 0x0000000000419f33 in asn1_compile (asn=asn@entry=0x671e10, datadir=datadir@entry=0x426730 "/home/vasko/usr/share/asn1c",
flags=flags@entry=A1C_NO_C99, argc=2, optc=0, argv=0x7fffffffe268) at asn1compiler.c:44
#7 0x0000000000401f13 in main (ac=1, av=0x7fffffffe270) at asn1c.c:328
2016-07-18 17:24:49 +00:00
|
|
|
return asn1c_make_identifier(0, exprid, typename, (char*)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,
|
Fix segmentation fault in asn1c_make_identifier
See also http://stackoverflow.com/questions/25390720/va-arg-gives-something-strange-in-cygwin-x64
Program received signal SIGSEGV, Segmentation fault.
(gdb) bt
#0 0x00007ffff7b7c8f1 in __strlen_sse2_pminub () from /lib64/libc.so.6
#1 0x0000000000425d29 in asn1c_make_identifier (flags=flags@entry=(AMI_MASK_ONLY_SPACES | AMI_NODELIMITER), expr=expr@entry=0x0) at asn1c_misc.c:73
#2 0x00000000004263ec in asn1c_type_name (arg=arg@entry=0x7fffffffe000, expr=expr@entry=0x6757c0, _format=<optimized out>,
_format@entry=TNF_INCLUDE) at asn1c_misc.c:268
#3 0x00000000004226fc in asn1c_lang_C_type_SIMPLE_TYPE (arg=arg@entry=0x7fffffffe000) at asn1c_C.c:1119
#4 0x00000000004232c1 in asn1c_lang_C_type_common_INTEGER (arg=0x7fffffffe000) at asn1c_C.c:251
#5 0x0000000000419c75 in asn1c_compile_expr (arg=arg@entry=0x7fffffffe000) at asn1compiler.c:100
#6 0x0000000000419f33 in asn1_compile (asn=asn@entry=0x671e10, datadir=datadir@entry=0x426730 "/home/vasko/usr/share/asn1c",
flags=flags@entry=A1C_NO_C99, argc=2, optc=0, argv=0x7fffffffe268) at asn1compiler.c:44
#7 0x0000000000401f13 in main (ac=1, av=0x7fffffffe270) at asn1c.c:328
2016-07-18 17:24:49 +00:00
|
|
|
exprid?"t":typename, exprid?0:"t", (char*)0);
|
2005-03-04 22:18:20 +00:00
|
|
|
case TNF_RSAFE: /* Recursion-safe type */
|
2017-05-07 13:00:51 +00:00
|
|
|
return asn1c_make_identifier(AMI_CHECK_RESERVED | AMI_NODELIMITER, 0,
|
|
|
|
"struct", " ", MODULE_NAME_OF(exprid), typename, (char*)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
|
2007-12-03 13:41:36 +00:00
|
|
|
* using the generic 'long' or 'unsigned long' type.
|
2005-08-14 02:18:27 +00:00
|
|
|
*/
|
|
|
|
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)
|
2013-03-27 08:56:23 +00:00
|
|
|
return (arg->flags & A1C_USE_WIDE_TYPES)
|
|
|
|
? FL_NOTFIT : FL_PRESUMED;
|
2005-08-14 14:45:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2017-08-01 03:20:17 +00:00
|
|
|
range = asn1constraint_compute_constraint_range(expr->Identifier,
|
|
|
|
expr->expr_type,
|
2005-08-14 14:45:44 +00:00
|
|
|
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)
|
2007-12-03 13:41:36 +00:00
|
|
|
return FL_FITS_SIGNED;
|
2005-08-14 14:45:44 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
*/
|
2017-08-01 03:20:17 +00:00
|
|
|
range = asn1constraint_compute_PER_range(expr->Identifier, expr->expr_type,
|
2005-08-14 14:45:44 +00:00
|
|
|
expr->combined_constraints, ACT_EL_RANGE, 0, 0, 0);
|
2014-02-10 19:00:51 +00:00
|
|
|
|
2005-08-14 02:18:27 +00:00
|
|
|
if(!range
|
2014-02-10 19:00:51 +00:00
|
|
|
/* Commenting out
|
|
|
|
|| range->extensible
|
|
|
|
* because this may or may not indicate wide type.
|
|
|
|
*/
|
2014-02-10 19:06:56 +00:00
|
|
|
|| (range->extensible && (arg->flags & A1C_USE_WIDE_TYPES))
|
2005-08-14 02:18:27 +00:00
|
|
|
|| range->empty_constraint
|
|
|
|
|| range->incompatible
|
|
|
|
|| range->not_PER_visible
|
|
|
|
) {
|
|
|
|
asn1constraint_range_free(range);
|
2013-03-27 08:56:23 +00:00
|
|
|
return (arg->flags & A1C_USE_WIDE_TYPES)
|
|
|
|
? FL_NOTFIT : FL_PRESUMED;
|
2005-08-14 02:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
left = range->left;
|
|
|
|
right = range->right;
|
|
|
|
asn1constraint_range_free(range);
|
|
|
|
|
2007-12-03 13:41:36 +00:00
|
|
|
/* Special case for unsigned */
|
|
|
|
if(left.type == ARE_VALUE
|
|
|
|
&& left.value >= 0
|
|
|
|
&& right.type == ARE_VALUE
|
|
|
|
&& right.value > 2147483647
|
2016-01-10 21:15:02 +00:00
|
|
|
&& right.value <= (asn1c_integer_t)(4294967295UL))
|
2007-12-03 13:41:36 +00:00
|
|
|
return FL_FITS_UNSIGN;
|
|
|
|
|
|
|
|
|
2005-08-14 02:18:27 +00:00
|
|
|
/* 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;
|
|
|
|
|
2013-03-27 08:56:23 +00:00
|
|
|
/* If the range is open, fits only unless -fwide-types is given */
|
2005-08-14 02:18:27 +00:00
|
|
|
if(left.type != ARE_VALUE || right.type != ARE_VALUE) {
|
2013-03-27 08:56:23 +00:00
|
|
|
return (arg->flags & A1C_USE_WIDE_TYPES)
|
|
|
|
? FL_NOTFIT : FL_PRESUMED;
|
2005-08-14 02:18:27 +00:00
|
|
|
}
|
|
|
|
|
2007-12-03 13:41:36 +00:00
|
|
|
return FL_FITS_SIGNED;
|
2005-08-14 02:18:27 +00:00
|
|
|
}
|
|
|
|
|