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