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; |
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, |
| 210 | asn1f_find_terminal_type_ex(arg->asn, expr_ns, expr)); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 211 | |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 212 | if(_format == TNF_RSAFE) { |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 213 | if(terminal && terminal->expr_type & ASN_CONSTR_MASK) { |
| 214 | typename = terminal->Identifier; |
| 215 | } |
| 216 | } |
| 217 | |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 218 | if(_format == TNF_CTYPE) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 219 | /* |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 220 | * If the component references the type itself, |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 221 | * switch to a recursion-safe type naming |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 222 | * ("struct foo" instead of "foo_t"). |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 223 | */ |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 224 | if(terminal && terminal == top_parent) { |
| 225 | _format = TNF_RSAFE; |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 226 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 227 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 228 | |
Bi-Ruei, Chiu | 94f0b64 | 2017-06-29 01:34:23 +0800 | [diff] [blame] | 229 | 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] | 230 | exprid = terminal; |
| 231 | typename = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 232 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 233 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 234 | break; |
| 235 | case ASN_BASIC_INTEGER: |
| 236 | case ASN_BASIC_ENUMERATED: |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 237 | case ASN_BASIC_REAL: |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 238 | if((expr->expr_type == ASN_BASIC_REAL |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 239 | && !(arg->flags & A1C_USE_WIDE_TYPES)) |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 240 | || asn1c_type_fits_long(arg, expr)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 241 | switch(_format) { |
| 242 | case TNF_CTYPE: |
| 243 | case TNF_RSAFE: |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 244 | if(expr->expr_type == ASN_BASIC_REAL) |
| 245 | return "double"; |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 246 | else if(asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN) |
| 247 | return "unsigned long"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 248 | else |
Lev Walkin | 475d91f | 2005-02-25 12:09:55 +0000 | [diff] [blame] | 249 | return "long"; |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 250 | default: |
| 251 | typename = 0; |
| 252 | switch(expr->expr_type) { |
| 253 | case ASN_BASIC_INTEGER: |
| 254 | typename = "NativeInteger"; break; |
| 255 | case ASN_BASIC_ENUMERATED: |
| 256 | typename = "NativeEnumerated"; break; |
| 257 | case ASN_BASIC_REAL: |
| 258 | typename = "NativeReal"; break; |
| 259 | default: |
| 260 | break; |
| 261 | } |
| 262 | break; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 263 | } |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 264 | if(typename) { |
| 265 | if(_format != TNF_INCLUDE) |
| 266 | return typename; |
| 267 | stdname = 1; |
| 268 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | /* Fall through */ |
| 272 | default: |
Lev Walkin | 0e22b98 | 2004-08-25 02:03:26 +0000 | [diff] [blame] | 273 | if(expr->expr_type |
| 274 | & (ASN_CONSTR_MASK | ASN_BASIC_MASK | ASN_STRING_MASK)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 275 | if(_format == TNF_RSAFE) |
| 276 | _format = TNF_CTYPE; |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 277 | stdname = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 278 | typename = ASN_EXPR_TYPE2STR(expr->expr_type); |
| 279 | } else { |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 280 | _format = TNF_RSAFE; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 281 | typename = expr->Identifier; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | switch(_format) { |
| 286 | case TNF_UNMODIFIED: |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 287 | return asn1c_make_identifier(AMI_MASK_ONLY_SPACES | AMI_NODELIMITER, |
| 288 | 0, MODULE_NAME_OF(exprid), exprid ? exprid->Identifier : typename, (char*)0); |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 289 | case TNF_INCLUDE: |
| 290 | return asn1c_make_identifier( |
| 291 | AMI_MASK_ONLY_SPACES | AMI_NODELIMITER, |
Lev Walkin | ffd035e | 2010-10-16 01:26:57 -0700 | [diff] [blame] | 292 | 0, ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED)) |
| 293 | ? "\"" : "<"), |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 294 | MODULE_NAME_OF(exprid), |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 295 | exprid ? exprid->Identifier : typename, |
Lev Walkin | ffd035e | 2010-10-16 01:26:57 -0700 | [diff] [blame] | 296 | ((!stdname || (arg->flags & A1C_INCLUDES_QUOTED)) |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 297 | ? ".h\"" : ".h>"), (char*)0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 298 | case TNF_SAFE: |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 299 | return asn1c_make_identifier(0, exprid, typename, (char*)0); |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 300 | case TNF_CTYPE: /* C type */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 301 | return asn1c_make_identifier(0, exprid, |
Vasil Velichkov | febd3d5 | 2016-07-18 20:24:49 +0300 | [diff] [blame] | 302 | exprid?"t":typename, exprid?0:"t", (char*)0); |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 303 | case TNF_RSAFE: /* Recursion-safe type */ |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 304 | return asn1c_make_identifier(AMI_CHECK_RESERVED | AMI_NODELIMITER, 0, |
| 305 | "struct", " ", MODULE_NAME_OF(exprid), typename, (char*)0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Lev Walkin | 8dd0eed | 2004-09-17 06:32:12 +0000 | [diff] [blame] | 308 | assert(!"unreachable"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 309 | return typename; |
| 310 | } |
| 311 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 312 | /* |
| 313 | * Check whether the specified INTEGER or ENUMERATED type can be represented |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 314 | * using the generic 'long' or 'unsigned long' type. |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 315 | */ |
| 316 | enum asn1c_fitslong_e |
| 317 | asn1c_type_fits_long(arg_t *arg, asn1p_expr_t *expr) { |
| 318 | asn1cnst_range_t *range = 0; |
| 319 | asn1cnst_edge_t left; |
| 320 | asn1cnst_edge_t right; |
| 321 | asn1p_expr_t *v; |
| 322 | |
| 323 | /* |
| 324 | * Since we don't know the sizeof(long) on the possible target platform |
| 325 | * which will be compiling the code generated by asn1c, let's play it |
| 326 | * simple: long's range is equal to or greater than int32_t. |
Lev Walkin | 4c97bd6 | 2005-08-15 01:01:24 +0000 | [diff] [blame] | 327 | * NOTE: the most negative integer cannot be written in C, as the C99 |
| 328 | * standard will give it an unsigned type. |
| 329 | * It is defined here as a constant expression. |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 330 | */ |
Lev Walkin | c08c3cf | 2005-08-15 01:04:48 +0000 | [diff] [blame] | 331 | #define RIGHTMAX 2147483647 /* of 32-bit integer type */ |
| 332 | #define LEFTMIN (-RIGHTMAX-1) /* of 32-bit integer type */ |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 333 | |
| 334 | /* Descend to the terminal type */ |
Lev Walkin | c0e03b9 | 2017-08-22 01:48:23 -0700 | [diff] [blame] | 335 | expr = WITH_MODULE_NAMESPACE( |
| 336 | expr->module, expr_ns, |
| 337 | asn1f_find_terminal_type_ex(arg->asn, expr_ns, expr)); |
| 338 | if(expr == 0) return FL_NOTFIT; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 339 | |
| 340 | /* The "fits into long" operation is relevant only for integer types */ |
| 341 | switch(expr->expr_type) { |
| 342 | case ASN_BASIC_INTEGER: |
| 343 | case ASN_BASIC_ENUMERATED: |
| 344 | break; |
| 345 | default: |
| 346 | return FL_NOTFIT; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * First, evaluate the range of explicitly given identifiers. |
| 351 | */ |
| 352 | TQ_FOR(v, &(expr->members), next) { |
| 353 | if(v->expr_type != A1TC_UNIVERVAL) |
| 354 | continue; |
| 355 | if(v->value->value.v_integer < LEFTMIN |
| 356 | || v->value->value.v_integer > RIGHTMAX) |
| 357 | return FL_NOTFIT; |
| 358 | } |
| 359 | |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 360 | if(!expr->combined_constraints) |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 361 | return (arg->flags & A1C_USE_WIDE_TYPES) |
| 362 | ? FL_NOTFIT : FL_PRESUMED; |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 363 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 364 | /* |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 365 | * Second, if -fbless-SIZE is given, the (SIZE()) constraint may be |
| 366 | * applied (non-standard! but we can deal with this) to the type. |
| 367 | * Check the range. |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 368 | */ |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 369 | range = asn1constraint_compute_constraint_range(expr->Identifier, |
| 370 | expr->expr_type, |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 371 | expr->combined_constraints, ACT_CT_SIZE, 0, 0, |
| 372 | CPR_simulate_fbless_SIZE); |
| 373 | if(range) { |
| 374 | if(!range->incompatible) { |
| 375 | right = range->right; |
| 376 | /* Use 4 instead of sizeof(long) is justified! */ |
| 377 | if(right.type == ARE_VALUE && right.value <= 4) |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 378 | return FL_FITS_SIGNED; |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 379 | } |
| 380 | asn1constraint_range_free(range); |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Third, pull up the PER visible range of the INTEGER. |
| 385 | */ |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 386 | range = asn1constraint_compute_PER_range(expr->Identifier, expr->expr_type, |
Lev Walkin | 4b55341 | 2005-08-14 14:45:44 +0000 | [diff] [blame] | 387 | expr->combined_constraints, ACT_EL_RANGE, 0, 0, 0); |
Lev Walkin | e5086e3 | 2014-02-10 11:00:51 -0800 | [diff] [blame] | 388 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 389 | if(!range |
Lev Walkin | e5086e3 | 2014-02-10 11:00:51 -0800 | [diff] [blame] | 390 | /* Commenting out |
| 391 | || range->extensible |
| 392 | * because this may or may not indicate wide type. |
| 393 | */ |
Lev Walkin | 334d5cc | 2014-02-10 11:06:56 -0800 | [diff] [blame] | 394 | || (range->extensible && (arg->flags & A1C_USE_WIDE_TYPES)) |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 395 | || range->empty_constraint |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 396 | || range->incompatible |
| 397 | || range->not_PER_visible |
| 398 | ) { |
| 399 | asn1constraint_range_free(range); |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 400 | return (arg->flags & A1C_USE_WIDE_TYPES) |
| 401 | ? FL_NOTFIT : FL_PRESUMED; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | left = range->left; |
| 405 | right = range->right; |
| 406 | asn1constraint_range_free(range); |
| 407 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 408 | /* Special case for unsigned */ |
| 409 | if(left.type == ARE_VALUE |
| 410 | && left.value >= 0 |
| 411 | && right.type == ARE_VALUE |
| 412 | && right.value > 2147483647 |
Lev Walkin | 1b3a135 | 2016-01-10 13:15:02 -0800 | [diff] [blame] | 413 | && right.value <= (asn1c_integer_t)(4294967295UL)) |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 414 | return FL_FITS_UNSIGN; |
| 415 | |
| 416 | |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 417 | /* If some fixed value is outside of target range, not fit */ |
| 418 | if(left.type == ARE_VALUE |
| 419 | && (left.value < LEFTMIN || left.value > RIGHTMAX)) |
| 420 | return FL_NOTFIT; |
| 421 | if(right.type == ARE_VALUE |
| 422 | && (right.value > RIGHTMAX || right.value < LEFTMIN)) |
| 423 | return FL_NOTFIT; |
| 424 | |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 425 | /* If the range is open, fits only unless -fwide-types is given */ |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 426 | if(left.type != ARE_VALUE || right.type != ARE_VALUE) { |
Lev Walkin | 2a744a7 | 2013-03-27 01:56:23 -0700 | [diff] [blame] | 427 | return (arg->flags & A1C_USE_WIDE_TYPES) |
| 428 | ? FL_NOTFIT : FL_PRESUMED; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 431 | return FL_FITS_SIGNED; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 432 | } |
| 433 | |