Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #include "asn1c_internal.h" |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2 | #include "asn1c_misc.h" |
| 3 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 4 | #include <asn1fix_crange.h> /* constraint groker from libasn1fix */ |
| 5 | #include <asn1fix_export.h> /* other exportable stuff from libasn1fix */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 7 | /* |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 8 | * Checks that the given string is not a reserved C/C++ keyword. |
Lev Walkin | 3fe2e12 | 2005-03-03 21:29:27 +0000 | [diff] [blame] | 9 | * ISO/IEC 9899:1999 (C99), A.1.2 |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 10 | */ |
| 11 | static char *res_kwd[] = { |
Lev Walkin | 30ae27b | 2013-03-19 17:15:28 -0700 | [diff] [blame] | 12 | "auto", "break", "case", "char", "const", "continue", "default", |
| 13 | "do", "double", "else", "enum", "extern", "float", "for", "goto", |
| 14 | "if", "inline", "int", "long", "register", "restrict", "return", |
| 15 | "short", "signed", "sizeof", "static", "struct", "switch", "typedef", |
| 16 | "union", "unsigned", "void", "volatile", "while", |
| 17 | "_Bool", "_Complex", "_Imaginary", |
| 18 | /* C++ */ |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 19 | "class", "explicit", "bool", "mutable", |
Lev Walkin | 30ae27b | 2013-03-19 17:15:28 -0700 | [diff] [blame] | 20 | "template", "typeid", "typename", "and", "and_eq", |
| 21 | "or", "or_eq", "xor", "xor_eq", "not", "not_eq", |
| 22 | "bitor", "compl", "bitand", |
| 23 | "const_cast", "dynamic_cast", "reinterpret_cast", |
| 24 | "static_cast", "true", "false", "namespace", "using", |
| 25 | "throw", "try", "catch" |
Lev Walkin | 3fe2e12 | 2005-03-03 21:29:27 +0000 | [diff] [blame] | 26 | }; |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 27 | static int |
| 28 | reserved_keyword(const char *str) { |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 29 | size_t i; |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 30 | for(i = 0 ; i < sizeof(res_kwd)/sizeof(res_kwd[0]); i++) { |
| 31 | if(strcmp(str, res_kwd[i]) == 0) |
| 32 | return 1; |
| 33 | } |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 38 | * Construct identifier from multiple parts. |
| 39 | * Convert unsafe characters to underscores. |
| 40 | */ |
| 41 | char * |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 42 | asn1c_make_identifier(enum ami_flags_e flags, asn1p_expr_t *expr, ...) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | static char *storage; |
| 44 | static int storage_size; |
| 45 | int nodelimiter = 0; |
| 46 | va_list ap; |
| 47 | char *str; |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 48 | char *nextstr; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 49 | char *first = 0; |
| 50 | char *second = 0; |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 51 | ssize_t size; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 52 | char *p; |
| 53 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 54 | if(expr) { |
| 55 | /* |
| 56 | * Estimate the necessary storage size |
| 57 | */ |
| 58 | if(expr->Identifier == NULL) |
| 59 | return "Member"; |
| 60 | size = strlen(expr->Identifier); |
| 61 | if(expr->spec_index != -1) { |
| 62 | static char buf[32]; |
| 63 | second = buf; |
| 64 | size += 1 + snprintf(buf, sizeof buf, "%dP%d", |
| 65 | expr->_lineno, expr->spec_index); |
| 66 | } |
| 67 | } else { |
| 68 | size = -1; |
| 69 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 71 | va_start(ap, expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | while((str = va_arg(ap, char *))) |
| 73 | size += 1 + strlen(str); |
| 74 | va_end(ap); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 75 | if(size == -1) return NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Make sure we have this amount of storage. |
| 79 | */ |
| 80 | if(storage_size <= size) { |
Lev Walkin | d8b8364 | 2016-03-14 02:00:27 -0700 | [diff] [blame] | 81 | free(storage); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | storage = malloc(size + 1); |
| 83 | if(storage) { |
| 84 | storage_size = size; |
| 85 | } else { |
| 86 | storage_size = 0; |
| 87 | return NULL; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Fill-in the storage. |
| 93 | */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 94 | va_start(ap, expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 95 | p = storage; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 96 | nextstr = ""; |
| 97 | for(p = storage, str = 0; str || nextstr; str = nextstr) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 98 | int subst_made = 0; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 99 | nextstr = second ? second : va_arg(ap, char *); |
| 100 | |
| 101 | if(str == 0) { |
| 102 | if(expr) { |
| 103 | str = expr->Identifier; |
| 104 | first = str; |
| 105 | second = 0; |
| 106 | } else { |
| 107 | first = nextstr; |
| 108 | continue; |
| 109 | } |
| 110 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | |
| 112 | if(str[0] == ' ' && str[1] == '\0') { |
| 113 | *p++ = ' '; |
| 114 | nodelimiter = 1; /* No delimiter */ |
| 115 | continue; |
| 116 | } |
| 117 | |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 118 | if(str != first && !nodelimiter && !(flags & AMI_NODELIMITER)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | *p++ = '_'; /* Delimiter between tokens */ |
| 120 | nodelimiter = 0; |
| 121 | |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 122 | /* |
| 123 | * If it is a single argument, check that it does not clash |
| 124 | * with C/C++ language keywords. |
| 125 | */ |
| 126 | if((flags & AMI_CHECK_RESERVED) |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 127 | && str == first && !nextstr && reserved_keyword(str)) { |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 128 | *p++ = toupper(*str++); |
| 129 | /* Fall through */ |
| 130 | } |
| 131 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | for(; *str; str++) { |
| 133 | if(isalnum(*str)) { |
| 134 | *p++ = *str; |
| 135 | subst_made = 0; |
| 136 | } else if(!subst_made++) { |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 137 | if((flags & AMI_MASK_ONLY_SPACES) |
| 138 | && !isspace(*str)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 139 | *p ++ = *str; |
| 140 | } else { |
| 141 | *p++ = '_'; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | va_end(ap); |
| 147 | *p = '\0'; |
| 148 | |
| 149 | assert((p - storage) <= storage_size); |
| 150 | |
| 151 | return storage; |
| 152 | } |
| 153 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 154 | char * |
| 155 | asn1c_type_name(arg_t *arg, asn1p_expr_t *expr, enum tnfmt _format) { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 156 | asn1p_expr_t *exprid = 0; |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 157 | asn1p_expr_t *top_parent; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 158 | asn1p_expr_t *terminal; |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 159 | int stdname = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 160 | char *typename; |
| 161 | |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 162 | /* Rewind to the topmost parent expression */ |
| 163 | if((top_parent = expr->parent_expr)) |
| 164 | while(top_parent->parent_expr) |
| 165 | top_parent = top_parent->parent_expr; |
| 166 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 167 | if(0) DEBUG("asn1c_type_name(%s: 0x%x)", |
| 168 | expr->Identifier, expr->expr_type); |
Lev Walkin | c2a7509 | 2006-03-14 11:52:12 +0000 | [diff] [blame] | 169 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 170 | switch(expr->expr_type) { |
| 171 | case A1TC_REFERENCE: |
| 172 | typename = expr->reference->components[ |
| 173 | expr->reference->comp_count-1].name; |
| 174 | if(typename[0] == '&') { |
| 175 | arg_t tmp = *arg; |
| 176 | |
| 177 | /* |
| 178 | * This is a reference to a type defined in a class. |
| 179 | * Resolve it and use instead. |
| 180 | */ |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 181 | tmp.expr = asn1f_class_access_ex(arg->asn, |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 182 | arg->expr->module, arg->expr, expr->rhs_pspecs, expr->reference); |
Lev Walkin | c2a7509 | 2006-03-14 11:52:12 +0000 | [diff] [blame] | 183 | if(!tmp.expr) return NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 184 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 185 | return asn1c_type_name(&tmp, tmp.expr, _format); |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 188 | terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 189 | |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 190 | if(_format == TNF_RSAFE) { |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 191 | if(terminal && terminal->expr_type & ASN_CONSTR_MASK) { |
| 192 | typename = terminal->Identifier; |
| 193 | } |
| 194 | } |
| 195 | |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 196 | if(_format == TNF_CTYPE) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 197 | /* |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 198 | * If the component references the type itself, |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 199 | * switch to a recursion-safe type naming |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 200 | * ("struct foo" instead of "foo_t"). |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 201 | */ |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 202 | if(terminal && terminal == top_parent) { |
| 203 | _format = TNF_RSAFE; |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 204 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 205 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 206 | |
| 207 | if(terminal && terminal->spec_index != -1) { |
| 208 | exprid = terminal; |
| 209 | typename = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 211 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 212 | break; |
| 213 | case ASN_BASIC_INTEGER: |
| 214 | case ASN_BASIC_ENUMERATED: |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 215 | case ASN_BASIC_REAL: |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 216 | if((expr->expr_type == ASN_BASIC_REAL |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 217 | && !(arg->flags & A1C_USE_WIDE_TYPES)) |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 218 | || asn1c_type_fits_long(arg, expr)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 219 | switch(_format) { |
| 220 | case TNF_CTYPE: |
| 221 | case TNF_RSAFE: |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 222 | if(expr->expr_type == ASN_BASIC_REAL) |
| 223 | return "double"; |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 224 | else if(asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN) |
| 225 | return "unsigned long"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 226 | else |
Lev Walkin | 475d91f | 2005-02-25 12:09:55 +0000 | [diff] [blame] | 227 | return "long"; |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 228 | default: |
| 229 | typename = 0; |
| 230 | switch(expr->expr_type) { |
| 231 | case ASN_BASIC_INTEGER: |
| 232 | typename = "NativeInteger"; break; |
| 233 | case ASN_BASIC_ENUMERATED: |
| 234 | typename = "NativeEnumerated"; break; |
| 235 | case ASN_BASIC_REAL: |
| 236 | typename = "NativeReal"; break; |
| 237 | default: |
| 238 | break; |
| 239 | } |
| 240 | break; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 241 | } |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 242 | if(typename) { |
| 243 | if(_format != TNF_INCLUDE) |
| 244 | return typename; |
| 245 | stdname = 1; |
| 246 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | /* Fall through */ |
| 250 | default: |
Lev Walkin | 0e22b98 | 2004-08-25 02:03:26 +0000 | [diff] [blame] | 251 | if(expr->expr_type |
| 252 | & (ASN_CONSTR_MASK | ASN_BASIC_MASK | ASN_STRING_MASK)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 253 | if(_format == TNF_RSAFE) |
| 254 | _format = TNF_CTYPE; |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 255 | stdname = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 256 | typename = ASN_EXPR_TYPE2STR(expr->expr_type); |
| 257 | } else { |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 258 | _format = TNF_RSAFE; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 259 | typename = expr->Identifier; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | switch(_format) { |
| 264 | case TNF_UNMODIFIED: |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 265 | return asn1c_make_identifier(AMI_MASK_ONLY_SPACES, |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 266 | 0, exprid ? exprid->Identifier : typename, (char*)0); |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 267 | case TNF_INCLUDE: |
| 268 | return asn1c_make_identifier( |
| 269 | AMI_MASK_ONLY_SPACES | AMI_NODELIMITER, |
Lev Walkin | ffd035e | 2010-10-16 01:26:57 -0700 | [diff] [blame] | 270 | 0, ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED)) |
| 271 | ? "\"" : "<"), |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 272 | exprid ? exprid->Identifier : typename, |
Lev Walkin | ffd035e | 2010-10-16 01:26:57 -0700 | [diff] [blame] | 273 | ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED)) |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 274 | ? ".h\"" : ".h>"), (char*)0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 275 | case TNF_SAFE: |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 276 | return asn1c_make_identifier(0, exprid, typename, (char*)0); |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 277 | case TNF_CTYPE: /* C type */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 278 | return asn1c_make_identifier(0, exprid, |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 279 | exprid?"t":typename, exprid?0:"t", (char*)0); |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 280 | case TNF_RSAFE: /* Recursion-safe type */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 281 | return asn1c_make_identifier(AMI_CHECK_RESERVED, 0, |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 282 | "struct", " ", typename, (char*)0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 285 | assert(!"unreachable"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 286 | return typename; |
| 287 | } |
| 288 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 289 | /* |
| 290 | * Check whether the specified INTEGER or ENUMERATED type can be represented |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 291 | * using the generic 'long' or 'unsigned long' type. |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 292 | */ |
| 293 | enum asn1c_fitslong_e |
| 294 | asn1c_type_fits_long(arg_t *arg, asn1p_expr_t *expr) { |
| 295 | asn1cnst_range_t *range = 0; |
| 296 | asn1cnst_edge_t left; |
| 297 | asn1cnst_edge_t right; |
| 298 | asn1p_expr_t *v; |
| 299 | |
| 300 | /* |
| 301 | * Since we don't know the sizeof(long) on the possible target platform |
| 302 | * which will be compiling the code generated by asn1c, let's play it |
| 303 | * simple: long's range is equal to or greater than int32_t. |
Lev Walkin | 4c97bd6 | 2005-08-15 01:01:24 +0000 | [diff] [blame] | 304 | * NOTE: the most negative integer cannot be written in C, as the C99 |
| 305 | * standard will give it an unsigned type. |
| 306 | * It is defined here as a constant expression. |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 307 | */ |
Lev Walkin | c08c3cf | 2005-08-15 01:04:48 +0000 | [diff] [blame] | 308 | #define RIGHTMAX 2147483647 /* of 32-bit integer type */ |
| 309 | #define LEFTMIN (-RIGHTMAX-1) /* of 32-bit integer type */ |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 310 | |
| 311 | /* Descend to the terminal type */ |
| 312 | expr = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 313 | if(expr == 0) return FL_NOTFIT; |
| 314 | |
| 315 | /* The "fits into long" operation is relevant only for integer types */ |
| 316 | switch(expr->expr_type) { |
| 317 | case ASN_BASIC_INTEGER: |
| 318 | case ASN_BASIC_ENUMERATED: |
| 319 | break; |
| 320 | default: |
| 321 | return FL_NOTFIT; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * First, evaluate the range of explicitly given identifiers. |
| 326 | */ |
| 327 | TQ_FOR(v, &(expr->members), next) { |
| 328 | if(v->expr_type != A1TC_UNIVERVAL) |
| 329 | continue; |
| 330 | if(v->value->value.v_integer < LEFTMIN |
| 331 | || v->value->value.v_integer > RIGHTMAX) |
| 332 | return FL_NOTFIT; |
| 333 | } |
| 334 | |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 335 | if(!expr->combined_constraints) |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 336 | return (arg->flags & A1C_USE_WIDE_TYPES) |
| 337 | ? FL_NOTFIT : FL_PRESUMED; |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 338 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 339 | /* |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 340 | * Second, if -fbless-SIZE is given, the (SIZE()) constraint may be |
| 341 | * applied (non-standard! but we can deal with this) to the type. |
| 342 | * Check the range. |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 343 | */ |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 344 | range = asn1constraint_compute_PER_range(expr->expr_type, |
| 345 | expr->combined_constraints, ACT_CT_SIZE, 0, 0, |
| 346 | CPR_simulate_fbless_SIZE); |
| 347 | if(range) { |
| 348 | if(!range->incompatible) { |
| 349 | right = range->right; |
| 350 | /* Use 4 instead of sizeof(long) is justified! */ |
| 351 | if(right.type == ARE_VALUE && right.value <= 4) |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 352 | return FL_FITS_SIGNED; |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 353 | } |
| 354 | asn1constraint_range_free(range); |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Third, pull up the PER visible range of the INTEGER. |
| 359 | */ |
| 360 | range = asn1constraint_compute_PER_range(expr->expr_type, |
| 361 | expr->combined_constraints, ACT_EL_RANGE, 0, 0, 0); |
Lev Walkin | e5086e3 | 2014-02-10 11:00:51 -0800 | [diff] [blame] | 362 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 363 | if(!range |
Lev Walkin | e5086e3 | 2014-02-10 11:00:51 -0800 | [diff] [blame] | 364 | /* Commenting out |
| 365 | || range->extensible |
| 366 | * because this may or may not indicate wide type. |
| 367 | */ |
Lev Walkin | 334d5cc | 2014-02-10 11:06:56 -0800 | [diff] [blame] | 368 | || (range->extensible && (arg->flags & A1C_USE_WIDE_TYPES)) |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 369 | || range->empty_constraint |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 370 | || range->incompatible |
| 371 | || range->not_PER_visible |
| 372 | ) { |
| 373 | asn1constraint_range_free(range); |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 374 | return (arg->flags & A1C_USE_WIDE_TYPES) |
| 375 | ? FL_NOTFIT : FL_PRESUMED; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | left = range->left; |
| 379 | right = range->right; |
| 380 | asn1constraint_range_free(range); |
| 381 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 382 | /* Special case for unsigned */ |
| 383 | if(left.type == ARE_VALUE |
| 384 | && left.value >= 0 |
| 385 | && right.type == ARE_VALUE |
| 386 | && right.value > 2147483647 |
Lev Walkin | 1b3a135 | 2016-01-10 13:15:02 -0800 | [diff] [blame] | 387 | && right.value <= (asn1c_integer_t)(4294967295UL)) |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 388 | return FL_FITS_UNSIGN; |
| 389 | |
| 390 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 391 | /* If some fixed value is outside of target range, not fit */ |
| 392 | if(left.type == ARE_VALUE |
| 393 | && (left.value < LEFTMIN || left.value > RIGHTMAX)) |
| 394 | return FL_NOTFIT; |
| 395 | if(right.type == ARE_VALUE |
| 396 | && (right.value > RIGHTMAX || right.value < LEFTMIN)) |
| 397 | return FL_NOTFIT; |
| 398 | |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 399 | /* If the range is open, fits only unless -fwide-types is given */ |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 400 | if(left.type != ARE_VALUE || right.type != ARE_VALUE) { |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 401 | return (arg->flags & A1C_USE_WIDE_TYPES) |
| 402 | ? FL_NOTFIT : FL_PRESUMED; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 405 | return FL_FITS_SIGNED; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 406 | } |
| 407 | |