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 | */ |
Lev Walkin | b46156d | 2017-09-05 02:53:05 -0700 | [diff] [blame] | 45 | const 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; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 54 | ssize_t size = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 55 | char *p; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 56 | char *prefix = NULL; |
| 57 | char *sptr[4], **psptr = &sptr[0]; |
| 58 | int sptr_cnt = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 60 | if(expr) { |
| 61 | /* |
| 62 | * Estimate the necessary storage size |
| 63 | */ |
| 64 | if(expr->Identifier == NULL) |
| 65 | return "Member"; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 66 | /* |
| 67 | * Add MODULE name to resolve clash |
| 68 | */ |
| 69 | if(expr->_mark & TM_NAMECLASH) { |
| 70 | size += strlen(expr->module->ModuleName) + 2; |
| 71 | sptr[sptr_cnt++] = expr->module->ModuleName; |
| 72 | } |
| 73 | sptr[sptr_cnt++] = expr->Identifier; |
| 74 | |
| 75 | size += strlen(expr->Identifier); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 76 | if(expr->spec_index != -1) { |
| 77 | static char buf[32]; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 78 | size += 1 + snprintf(buf, sizeof buf, "%dP%d", |
| 79 | expr->_lineno, expr->spec_index); |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 80 | sptr[sptr_cnt++] = (char *)&buf; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 81 | } |
| 82 | } else { |
| 83 | size = -1; |
| 84 | } |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 85 | sptr[sptr_cnt++] = (char *)0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 87 | va_start(ap, expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 88 | while((str = va_arg(ap, char *))) |
| 89 | size += 1 + strlen(str); |
| 90 | va_end(ap); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 91 | if(size == -1) return NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 92 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 93 | if(prefix) |
| 94 | size += 1 + strlen(prefix); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 95 | /* |
Lev Walkin | f4cbbe7 | 2017-06-27 08:19:11 -0700 | [diff] [blame] | 96 | * Make sure we have the required amount of storage. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 97 | */ |
| 98 | if(storage_size <= size) { |
Lev Walkin | f4cbbe7 | 2017-06-27 08:19:11 -0700 | [diff] [blame] | 99 | char *tmp = malloc(size + 1); |
| 100 | if(tmp) { |
| 101 | free(storage); |
| 102 | storage = tmp; |
| 103 | storage_size = size + 1; |
| 104 | } else { |
| 105 | return NULL; |
| 106 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Fill-in the storage. |
| 111 | */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 112 | va_start(ap, expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 113 | p = storage; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 114 | if(prefix) { |
| 115 | strcpy(storage, prefix); |
| 116 | p += strlen(prefix); |
| 117 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 118 | nextstr = ""; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 119 | for(str = 0; str || nextstr; str = nextstr) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 120 | int subst_made = 0; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 121 | nextstr = *(psptr) ? *(psptr++) : va_arg(ap, char *); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 122 | |
| 123 | if(str == 0) { |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 124 | str = first = nextstr; |
| 125 | nextstr = *(psptr) ? *(psptr++) : va_arg(ap, char *); |
| 126 | if (!first) continue; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 127 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 128 | |
| 129 | if(str[0] == ' ' && str[1] == '\0') { |
| 130 | *p++ = ' '; |
| 131 | nodelimiter = 1; /* No delimiter */ |
| 132 | continue; |
| 133 | } |
| 134 | |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 135 | if(str != first && !nodelimiter && !(flags & AMI_NODELIMITER)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 136 | *p++ = '_'; /* Delimiter between tokens */ |
| 137 | nodelimiter = 0; |
| 138 | |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 139 | /* |
| 140 | * If it is a single argument, check that it does not clash |
| 141 | * with C/C++ language keywords. |
| 142 | */ |
| 143 | if((flags & AMI_CHECK_RESERVED) |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 144 | && str == first && !nextstr && reserved_keyword(str)) { |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 145 | *p++ = toupper(*str++); |
| 146 | /* Fall through */ |
| 147 | } |
| 148 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 149 | for(; *str; str++) { |
| 150 | if(isalnum(*str)) { |
| 151 | *p++ = *str; |
| 152 | subst_made = 0; |
| 153 | } else if(!subst_made++) { |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 154 | if((flags & AMI_MASK_ONLY_SPACES) |
| 155 | && !isspace(*str)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 156 | *p ++ = *str; |
| 157 | } else { |
| 158 | *p++ = '_'; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | va_end(ap); |
| 164 | *p = '\0'; |
| 165 | |
| 166 | assert((p - storage) <= storage_size); |
| 167 | |
| 168 | return storage; |
| 169 | } |
| 170 | |
Lev Walkin | cf3f6eb | 2017-08-23 04:34:15 -0700 | [diff] [blame] | 171 | const char * |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 172 | 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] | 173 | asn1p_expr_t *exprid = 0; |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 174 | asn1p_expr_t *top_parent; |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 175 | asn1p_expr_t *terminal = 0; |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 176 | int stdname = 0; |
Lev Walkin | cf3f6eb | 2017-08-23 04:34:15 -0700 | [diff] [blame] | 177 | const char *typename; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 178 | |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 179 | /* Rewind to the topmost parent expression */ |
| 180 | if((top_parent = expr->parent_expr)) |
| 181 | while(top_parent->parent_expr) |
| 182 | top_parent = top_parent->parent_expr; |
| 183 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 184 | if(0) DEBUG("asn1c_type_name(%s: 0x%x)", |
| 185 | expr->Identifier, expr->expr_type); |
Lev Walkin | c2a7509 | 2006-03-14 11:52:12 +0000 | [diff] [blame] | 186 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 187 | switch(expr->expr_type) { |
| 188 | case A1TC_REFERENCE: |
| 189 | typename = expr->reference->components[ |
| 190 | expr->reference->comp_count-1].name; |
| 191 | if(typename[0] == '&') { |
| 192 | arg_t tmp = *arg; |
| 193 | |
| 194 | /* |
| 195 | * This is a reference to a type defined in a class. |
| 196 | * Resolve it and use instead. |
| 197 | */ |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 198 | tmp.expr = WITH_MODULE_NAMESPACE( |
| 199 | arg->expr->module, expr_ns, |
| 200 | asn1f_class_access_ex(arg->asn, arg->expr->module, expr_ns, |
| 201 | arg->expr, expr->rhs_pspecs, |
| 202 | expr->reference)); |
| 203 | if(!tmp.expr) return NULL; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 204 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 205 | return asn1c_type_name(&tmp, tmp.expr, _format); |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 208 | terminal = WITH_MODULE_NAMESPACE( |
| 209 | expr->module, expr_ns, |
Bi-Ruei, Chiu | 3ed43b2 | 2017-10-06 18:46:25 +0800 | [diff] [blame] | 210 | (expr->meta_type == AMT_TYPEREF) ? |
| 211 | asn1f_lookup_symbol_ex(arg->asn, expr_ns, expr, expr->reference) : |
| 212 | asn1f_find_terminal_type_ex(arg->asn, expr_ns, expr)); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 213 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 214 | if(_format == TNF_RSAFE) { |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 215 | if(terminal && terminal->expr_type & ASN_CONSTR_MASK) { |
| 216 | typename = terminal->Identifier; |
| 217 | } |
| 218 | } |
| 219 | |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 220 | if(_format == TNF_CTYPE || _format == TNF_CONSTYPE) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 221 | /* |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 222 | * If the component references the type itself, |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 223 | * switch to a recursion-safe type naming |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 224 | * ("struct foo" instead of "foo_t"). |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 225 | */ |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 226 | if(terminal && terminal == top_parent) { |
| 227 | _format = TNF_RSAFE; |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 228 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 229 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 230 | |
Bi-Ruei, Chiu | 94f0b64 | 2017-06-29 01:34:23 +0800 | [diff] [blame] | 231 | if(_format != TNF_RSAFE && terminal && ((terminal->spec_index != -1) || (terminal->_mark & TM_NAMECLASH))) { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 232 | exprid = terminal; |
| 233 | typename = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 234 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 235 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 236 | break; |
| 237 | case ASN_BASIC_INTEGER: |
| 238 | case ASN_BASIC_ENUMERATED: |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 239 | case ASN_BASIC_REAL: |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 240 | if((expr->expr_type == ASN_BASIC_REAL |
| 241 | && (_format == TNF_CONSTYPE || !(arg->flags & A1C_USE_WIDE_TYPES) |
| 242 | || asn1c_REAL_fits(arg, expr) != RL_NOTFIT)) |
| 243 | || asn1c_type_fits_long(arg, expr)) { |
| 244 | switch(_format) { |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 245 | case TNF_CONSTYPE: |
| 246 | if(expr->expr_type == ASN_BASIC_REAL) { |
| 247 | return "double"; |
| 248 | } else if(asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN) { |
| 249 | return "unsigned long"; |
| 250 | } else { |
| 251 | return "long"; |
| 252 | } |
| 253 | case TNF_CTYPE: |
| 254 | case TNF_RSAFE: |
| 255 | if(expr->expr_type == ASN_BASIC_REAL) { |
| 256 | asn1cnst_range_t *range = asn1constraint_compute_OER_range( |
| 257 | expr->Identifier, ASN_BASIC_REAL, |
| 258 | expr->combined_constraints, ACT_EL_RANGE, 0, 0, 0); |
| 259 | if(range->narrowing == NARROW_FLOAT32) { |
| 260 | asn1constraint_range_free(range); |
| 261 | return "float"; |
| 262 | } else { |
| 263 | asn1constraint_range_free(range); |
| 264 | return "double"; |
| 265 | } |
| 266 | } else if(asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN) { |
| 267 | return "unsigned long"; |
| 268 | } else { |
| 269 | return "long"; |
| 270 | } |
| 271 | default: |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 272 | typename = 0; |
| 273 | switch(expr->expr_type) { |
| 274 | case ASN_BASIC_INTEGER: |
| 275 | typename = "NativeInteger"; break; |
| 276 | case ASN_BASIC_ENUMERATED: |
| 277 | typename = "NativeEnumerated"; break; |
| 278 | case ASN_BASIC_REAL: |
| 279 | typename = "NativeReal"; break; |
| 280 | default: |
| 281 | break; |
| 282 | } |
| 283 | break; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 284 | } |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 285 | if(typename) { |
| 286 | if(_format != TNF_INCLUDE) |
| 287 | return typename; |
| 288 | stdname = 1; |
| 289 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | /* Fall through */ |
| 293 | default: |
Lev Walkin | 0e22b98 | 2004-08-25 02:03:26 +0000 | [diff] [blame] | 294 | if(expr->expr_type |
| 295 | & (ASN_CONSTR_MASK | ASN_BASIC_MASK | ASN_STRING_MASK)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 296 | if(_format == TNF_RSAFE) |
| 297 | _format = TNF_CTYPE; |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 298 | stdname = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 299 | typename = ASN_EXPR_TYPE2STR(expr->expr_type); |
| 300 | } else { |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 301 | _format = TNF_RSAFE; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 302 | typename = expr->Identifier; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | switch(_format) { |
| 307 | case TNF_UNMODIFIED: |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 308 | return asn1c_make_identifier(AMI_MASK_ONLY_SPACES | AMI_NODELIMITER, |
| 309 | 0, MODULE_NAME_OF(exprid), exprid ? exprid->Identifier : typename, (char*)0); |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 310 | case TNF_INCLUDE: |
| 311 | return asn1c_make_identifier( |
| 312 | AMI_MASK_ONLY_SPACES | AMI_NODELIMITER, |
Lev Walkin | ffd035e | 2010-10-16 01:26:57 -0700 | [diff] [blame] | 313 | 0, ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED)) |
| 314 | ? "\"" : "<"), |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 315 | MODULE_NAME_OF(exprid), |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 316 | exprid ? exprid->Identifier : typename, |
Lev Walkin | ffd035e | 2010-10-16 01:26:57 -0700 | [diff] [blame] | 317 | ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED)) |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 318 | ? ".h\"" : ".h>"), (char*)0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 319 | case TNF_SAFE: |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 320 | return asn1c_make_identifier(0, exprid, typename, (char*)0); |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 321 | case TNF_CTYPE: /* C type */ |
Lev Walkin | b545070 | 2017-10-04 02:52:57 -0700 | [diff] [blame] | 322 | case TNF_CONSTYPE: /* C type */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 323 | return asn1c_make_identifier(0, exprid, |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 324 | exprid?"t":typename, exprid?0:"t", (char*)0); |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 325 | case TNF_RSAFE: /* Recursion-safe type */ |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 326 | return asn1c_make_identifier(AMI_CHECK_RESERVED | AMI_NODELIMITER, 0, |
| 327 | "struct", " ", MODULE_NAME_OF(exprid), typename, (char*)0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 330 | assert(!"unreachable"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 331 | return typename; |
| 332 | } |
| 333 | |
Lev Walkin | 4329272 | 2017-10-05 00:33:32 -0700 | [diff] [blame] | 334 | static asn1p_expr_type_e |
| 335 | expr_get_type(arg_t *arg, asn1p_expr_t *expr) { |
| 336 | asn1p_expr_t *terminal; |
| 337 | terminal = asn1f_find_terminal_type_ex(arg->asn, arg->ns, expr); |
| 338 | if(terminal) return terminal->expr_type; |
| 339 | return A1TC_INVALID; |
| 340 | } |
| 341 | |
| 342 | enum asn1c_fitsfloat_e |
| 343 | asn1c_REAL_fits(arg_t *arg, asn1p_expr_t *expr) { |
| 344 | asn1p_expr_type_e etype = expr_get_type(arg, arg->expr); |
| 345 | if(etype == ASN_BASIC_REAL) { |
| 346 | asn1cnst_range_t *range = asn1constraint_compute_OER_range( |
| 347 | expr->Identifier, etype, expr->combined_constraints, ACT_EL_RANGE, |
| 348 | 0, 0, 0); |
| 349 | enum asn1c_fitsfloat_e fits; |
| 350 | switch(range->narrowing) { |
| 351 | case NARROW_FLOAT32: |
| 352 | fits = RL_FITS_FLOAT32; |
| 353 | break; |
| 354 | case NARROW_DOUBLE64: |
| 355 | fits = RL_FITS_DOUBLE64; |
| 356 | break; |
| 357 | default: |
| 358 | fits = RL_NOTFIT; |
| 359 | break; |
| 360 | } |
| 361 | asn1constraint_range_free(range); |
| 362 | return fits; |
| 363 | } else { |
| 364 | return 0; |
| 365 | } |
| 366 | } |
| 367 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 368 | /* |
| 369 | * Check whether the specified INTEGER or ENUMERATED type can be represented |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 370 | * using the generic 'long' or 'unsigned long' type. |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 371 | */ |
| 372 | enum asn1c_fitslong_e |
| 373 | asn1c_type_fits_long(arg_t *arg, asn1p_expr_t *expr) { |
| 374 | asn1cnst_range_t *range = 0; |
| 375 | asn1cnst_edge_t left; |
| 376 | asn1cnst_edge_t right; |
| 377 | asn1p_expr_t *v; |
| 378 | |
| 379 | /* |
| 380 | * Since we don't know the sizeof(long) on the possible target platform |
| 381 | * which will be compiling the code generated by asn1c, let's play it |
| 382 | * simple: long's range is equal to or greater than int32_t. |
Lev Walkin | 4c97bd6 | 2005-08-15 01:01:24 +0000 | [diff] [blame] | 383 | * NOTE: the most negative integer cannot be written in C, as the C99 |
| 384 | * standard will give it an unsigned type. |
| 385 | * It is defined here as a constant expression. |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 386 | */ |
Lev Walkin | c08c3cf | 2005-08-15 01:04:48 +0000 | [diff] [blame] | 387 | #define RIGHTMAX 2147483647 /* of 32-bit integer type */ |
| 388 | #define LEFTMIN (-RIGHTMAX-1) /* of 32-bit integer type */ |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 389 | |
| 390 | /* Descend to the terminal type */ |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 391 | expr = WITH_MODULE_NAMESPACE( |
| 392 | expr->module, expr_ns, |
| 393 | asn1f_find_terminal_type_ex(arg->asn, expr_ns, expr)); |
| 394 | if(expr == 0) return FL_NOTFIT; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 395 | |
| 396 | /* The "fits into long" operation is relevant only for integer types */ |
| 397 | switch(expr->expr_type) { |
| 398 | case ASN_BASIC_INTEGER: |
| 399 | case ASN_BASIC_ENUMERATED: |
| 400 | break; |
| 401 | default: |
| 402 | return FL_NOTFIT; |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * First, evaluate the range of explicitly given identifiers. |
| 407 | */ |
| 408 | TQ_FOR(v, &(expr->members), next) { |
| 409 | if(v->expr_type != A1TC_UNIVERVAL) |
| 410 | continue; |
| 411 | if(v->value->value.v_integer < LEFTMIN |
| 412 | || v->value->value.v_integer > RIGHTMAX) |
| 413 | return FL_NOTFIT; |
| 414 | } |
| 415 | |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 416 | if(!expr->combined_constraints) |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 417 | return (arg->flags & A1C_USE_WIDE_TYPES) |
| 418 | ? FL_NOTFIT : FL_PRESUMED; |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 419 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 420 | /* |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 421 | * Second, if -fbless-SIZE is given, the (SIZE()) constraint may be |
| 422 | * applied (non-standard! but we can deal with this) to the type. |
| 423 | * Check the range. |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 424 | */ |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 425 | range = asn1constraint_compute_constraint_range(expr->Identifier, |
| 426 | expr->expr_type, |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 427 | expr->combined_constraints, ACT_CT_SIZE, 0, 0, |
| 428 | CPR_simulate_fbless_SIZE); |
| 429 | if(range) { |
| 430 | if(!range->incompatible) { |
| 431 | right = range->right; |
| 432 | /* Use 4 instead of sizeof(long) is justified! */ |
| 433 | if(right.type == ARE_VALUE && right.value <= 4) |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 434 | return FL_FITS_SIGNED; |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 435 | } |
| 436 | asn1constraint_range_free(range); |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Third, pull up the PER visible range of the INTEGER. |
| 441 | */ |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 442 | range = asn1constraint_compute_PER_range(expr->Identifier, expr->expr_type, |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 443 | expr->combined_constraints, ACT_EL_RANGE, 0, 0, 0); |
Lev Walkin | e5086e3 | 2014-02-10 11:00:51 -0800 | [diff] [blame] | 444 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 445 | if(!range |
Lev Walkin | e5086e3 | 2014-02-10 11:00:51 -0800 | [diff] [blame] | 446 | /* Commenting out |
| 447 | || range->extensible |
| 448 | * because this may or may not indicate wide type. |
| 449 | */ |
Lev Walkin | 334d5cc | 2014-02-10 11:06:56 -0800 | [diff] [blame] | 450 | || (range->extensible && (arg->flags & A1C_USE_WIDE_TYPES)) |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 451 | || range->empty_constraint |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 452 | || range->incompatible |
| 453 | || range->not_PER_visible |
| 454 | ) { |
| 455 | asn1constraint_range_free(range); |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 456 | return (arg->flags & A1C_USE_WIDE_TYPES) |
| 457 | ? FL_NOTFIT : FL_PRESUMED; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | left = range->left; |
| 461 | right = range->right; |
| 462 | asn1constraint_range_free(range); |
| 463 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 464 | /* Special case for unsigned */ |
Lev Walkin | e0f2a5b | 2017-08-30 20:02:29 -0700 | [diff] [blame] | 465 | if(!(arg->flags & A1C_USE_WIDE_TYPES) && left.type == ARE_VALUE |
| 466 | && left.value >= 0 && left.value <= 2147483647 && right.type == ARE_MAX) |
| 467 | return FL_FITS_UNSIGN; |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 468 | if(left.type == ARE_VALUE |
| 469 | && left.value >= 0 |
| 470 | && right.type == ARE_VALUE |
| 471 | && right.value > 2147483647 |
Lev Walkin | 1b3a135 | 2016-01-10 13:15:02 -0800 | [diff] [blame] | 472 | && right.value <= (asn1c_integer_t)(4294967295UL)) |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 473 | return FL_FITS_UNSIGN; |
| 474 | |
| 475 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 476 | /* If some fixed value is outside of target range, not fit */ |
| 477 | if(left.type == ARE_VALUE |
| 478 | && (left.value < LEFTMIN || left.value > RIGHTMAX)) |
| 479 | return FL_NOTFIT; |
| 480 | if(right.type == ARE_VALUE |
| 481 | && (right.value > RIGHTMAX || right.value < LEFTMIN)) |
| 482 | return FL_NOTFIT; |
| 483 | |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 484 | /* If the range is open, fits only unless -fwide-types is given */ |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 485 | if(left.type != ARE_VALUE || right.type != ARE_VALUE) { |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 486 | return (arg->flags & A1C_USE_WIDE_TYPES) |
| 487 | ? FL_NOTFIT : FL_PRESUMED; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 490 | return FL_FITS_SIGNED; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 491 | } |
| 492 | |