Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Don't look into this file. First, because it's a mess, and second, because |
| 3 | * it's a brain of the compiler, and you don't wanna mess with brains do you? ;) |
| 4 | */ |
| 5 | #include "asn1c_internal.h" |
| 6 | #include "asn1c_C.h" |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 7 | #include "asn1c_constraint.h" |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 8 | #include "asn1c_out.h" |
| 9 | #include "asn1c_misc.h" |
Lev Walkin | ad0d637 | 2017-08-08 02:02:42 -0700 | [diff] [blame^] | 10 | #include "asn1c_ioc.h" |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 11 | #include <asn1fix_crange.h> /* constraint groker from libasn1fix */ |
| 12 | #include <asn1fix_export.h> /* other exportables from libasn1fix */ |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 13 | #include <asn1parser.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 15 | typedef struct tag2el_s { |
| 16 | struct asn1p_type_tag_s el_tag; |
| 17 | int el_no; |
Lev Walkin | 38abe79 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 18 | int toff_first; |
| 19 | int toff_last; |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 20 | asn1p_expr_t *from_expr; |
| 21 | } tag2el_t; |
| 22 | |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 23 | typedef enum fte { |
| 24 | FTE_ALLTAGS, |
| 25 | FTE_CANONICAL_XER, |
| 26 | } fte_e; |
| 27 | static int _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags); |
| 28 | static int _add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags); |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 29 | |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 30 | enum onc_flags { |
| 31 | ONC_noflags = 0x00, |
| 32 | ONC_avoid_keywords = 0x01, |
| 33 | ONC_force_compound_name = 0x02, |
| 34 | }; |
| 35 | static int out_name_chain(arg_t *arg, enum onc_flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 36 | static int asn1c_lang_C_type_SEQUENCE_def(arg_t *arg); |
| 37 | static int asn1c_lang_C_type_SET_def(arg_t *arg); |
| 38 | static int asn1c_lang_C_type_CHOICE_def(arg_t *arg); |
| 39 | static int asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of); |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 40 | static int _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag_p); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 41 | static int compute_extensions_start(asn1p_expr_t *expr); |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 42 | static int expr_break_recursion(arg_t *arg, asn1p_expr_t *expr); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 43 | static int expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 44 | static int expr_elements_count(arg_t *arg, asn1p_expr_t *expr); |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 45 | static int emit_single_member_OER_constraint_value(arg_t *arg, asn1cnst_range_t *range); |
| 46 | static int emit_single_member_OER_constraint_size(arg_t *arg, asn1cnst_range_t *range); |
| 47 | static int emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int juscountvalues, const char *type); |
Lev Walkin | 98eabc1 | 2017-07-19 08:51:11 +0400 | [diff] [blame] | 48 | static int emit_member_OER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx); |
Lev Walkin | 4b5dae3 | 2006-10-09 12:27:44 +0000 | [diff] [blame] | 49 | static int emit_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 50 | static int emit_member_table(arg_t *arg, asn1p_expr_t *expr); |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 51 | static int emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier); |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 52 | static int emit_include_dependencies(arg_t *arg); |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 53 | static asn1p_expr_t *terminal_structable(arg_t *arg, asn1p_expr_t *expr); |
| 54 | static int expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr); |
| 55 | static int asn1c_recurse(arg_t *arg, asn1p_expr_t *expr, int (*callback)(arg_t *arg, void *key), void *key); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 56 | static asn1p_expr_type_e expr_get_type(arg_t *arg, asn1p_expr_t *expr); |
| 57 | static int try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out); |
| 58 | static int *compute_canonical_members_order(arg_t *arg, int el_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 60 | enum tvm_compat { |
| 61 | _TVM_SAME = 0, /* tags and all_tags are same */ |
| 62 | _TVM_SUBSET = 1, /* tags are subset of all_tags */ |
| 63 | _TVM_DIFFERENT = 2, /* tags and all_tags are different */ |
| 64 | }; |
| 65 | static enum tvm_compat emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tc, int *atc); |
| 66 | |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 67 | enum etd_spec { |
| 68 | ETD_NO_SPECIFICS, |
| 69 | ETD_HAS_SPECIFICS |
| 70 | }; |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 71 | static int emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_count, int all_tags_count, int elements_count, enum etd_spec); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 72 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 73 | #define C99_MODE (!(arg->flags & A1C_NO_C99)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 74 | #define UNNAMED_UNIONS (arg->flags & A1C_UNNAMED_UNIONS) |
Lev Walkin | dd32b59 | 2004-09-06 08:07:29 +0000 | [diff] [blame] | 75 | #define HIDE_INNER_DEFS (arg->embed && !(arg->flags & A1C_ALL_DEFS_GLOBAL)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | |
| 77 | #define PCTX_DEF INDENTED( \ |
| 78 | OUT("\n"); \ |
| 79 | OUT("/* Context for parsing across buffer boundaries */\n"); \ |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 80 | OUT("asn_struct_ctx_t _asn_ctx;\n")); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 81 | |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 82 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 83 | #define DEPENDENCIES do { \ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 84 | emit_include_dependencies(arg); \ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 85 | if(expr->expr_type == ASN_CONSTR_SET_OF) \ |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 86 | GEN_INCLUDE_STD("asn_SET_OF"); \ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 87 | if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF) \ |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 88 | GEN_INCLUDE_STD("asn_SEQUENCE_OF"); \ |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 89 | } while(0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 91 | /* MKID_safe() without checking for reserved keywords */ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 92 | #define MKID(expr) (asn1c_make_identifier(0, expr, 0)) |
| 93 | #define MKID_safe(expr) (asn1c_make_identifier(AMI_CHECK_RESERVED, expr, 0)) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | |
| 95 | int |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 96 | asn1c_lang_C_type_REAL(arg_t *arg) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 97 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 98 | } |
| 99 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 100 | struct value2enum { |
| 101 | asn1c_integer_t value; |
| 102 | const char *name; |
| 103 | int idx; |
| 104 | }; |
| 105 | static int compar_enumMap_byName(const void *ap, const void *bp) { |
| 106 | const struct value2enum *a = (const struct value2enum *)ap; |
| 107 | const struct value2enum *b = (const struct value2enum *)bp; |
| 108 | return strcmp(a->name, b->name); |
| 109 | } |
| 110 | static int compar_enumMap_byValue(const void *ap, const void *bp) { |
| 111 | const struct value2enum *a = (const struct value2enum *)ap; |
| 112 | const struct value2enum *b = (const struct value2enum *)bp; |
| 113 | if(a->value < b->value) |
| 114 | return -1; |
| 115 | else if(a->value == b->value) |
| 116 | return 0; |
| 117 | return 1; |
| 118 | } |
| 119 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 120 | int |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 121 | asn1c_lang_C_type_common_INTEGER(arg_t *arg) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 122 | asn1p_expr_t *expr = arg->expr; |
| 123 | asn1p_expr_t *v; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 124 | int el_count = expr_elements_count(arg, expr); |
| 125 | struct value2enum *v2e; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 126 | int map_extensions = (expr->expr_type == ASN_BASIC_INTEGER); |
Lev Walkin | 414b078 | 2005-08-13 23:30:24 +0000 | [diff] [blame] | 127 | int eidx; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 128 | int saved_target = arg->target->target; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 130 | v2e = alloca((el_count + 1) * sizeof(*v2e)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 131 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 132 | /* |
Lev Walkin | 414b078 | 2005-08-13 23:30:24 +0000 | [diff] [blame] | 133 | * For all ENUMERATED types and for those INTEGER types which |
| 134 | * have identifiers, print out an enumeration table. |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 135 | */ |
Lev Walkin | 414b078 | 2005-08-13 23:30:24 +0000 | [diff] [blame] | 136 | if(expr->expr_type == ASN_BASIC_ENUMERATED || el_count) { |
| 137 | eidx = 0; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 138 | REDIR(OT_DEPS); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 139 | OUT("typedef enum "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 140 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 141 | OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 142 | TQ_FOR(v, &(expr->members), next) { |
| 143 | switch(v->expr_type) { |
| 144 | case A1TC_UNIVERVAL: |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 145 | OUT("\t"); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 146 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 147 | OUT("_%s", MKID(v)); |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 148 | OUT("\t= %s%s\n", |
| 149 | asn1p_itoa(v->value->value.v_integer), |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 150 | (eidx+1 < el_count) ? "," : ""); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 151 | v2e[eidx].name = v->Identifier; |
| 152 | v2e[eidx].value = v->value->value.v_integer; |
| 153 | eidx++; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 154 | break; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 155 | case A1TC_EXTENSIBLE: |
| 156 | OUT("\t/*\n"); |
| 157 | OUT("\t * Enumeration is extensible\n"); |
| 158 | OUT("\t */\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 159 | if(!map_extensions) |
| 160 | map_extensions = eidx + 1; |
Lev Walkin | c78cbfb | 2004-09-14 12:47:45 +0000 | [diff] [blame] | 161 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 162 | default: |
| 163 | return -1; |
| 164 | } |
| 165 | } |
Lev Walkin | 171487e | 2006-03-21 07:25:18 +0000 | [diff] [blame] | 166 | OUT("} e_"); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 167 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | 171487e | 2006-03-21 07:25:18 +0000 | [diff] [blame] | 168 | OUT(";\n"); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 169 | assert(eidx == el_count); |
Lev Walkin | 414b078 | 2005-08-13 23:30:24 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* |
| 173 | * For all ENUMERATED types print out a mapping table |
| 174 | * between identifiers and associated values. |
| 175 | * This is prohibited for INTEGER types by by X.693:8.3.4. |
| 176 | */ |
| 177 | if(expr->expr_type == ASN_BASIC_ENUMERATED) { |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Generate a enumerationName<->value map for XER codec. |
| 181 | */ |
| 182 | REDIR(OT_STAT_DEFS); |
| 183 | |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 184 | OUT("static const asn_INTEGER_enum_map_t asn_MAP_%s_value2enum_%d[] = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 185 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 186 | qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byValue); |
| 187 | for(eidx = 0; eidx < el_count; eidx++) { |
| 188 | v2e[eidx].idx = eidx; |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 189 | OUT("\t{ %s,\t%ld,\t\"%s\" }%s\n", |
| 190 | asn1p_itoa(v2e[eidx].value), |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 191 | (long)strlen(v2e[eidx].name), v2e[eidx].name, |
| 192 | (eidx + 1 < el_count) ? "," : ""); |
| 193 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 194 | if(map_extensions) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 195 | OUT("\t/* This list is extensible */\n"); |
| 196 | OUT("};\n"); |
| 197 | |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 198 | OUT("static const unsigned int asn_MAP_%s_enum2value_%d[] = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 199 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 200 | qsort(v2e, el_count, sizeof(v2e[0]), compar_enumMap_byName); |
| 201 | for(eidx = 0; eidx < el_count; eidx++) { |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 202 | OUT("\t%d%s\t/* %s(%s) */\n", |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 203 | v2e[eidx].idx, |
| 204 | (eidx + 1 < el_count) ? "," : "", |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 205 | v2e[eidx].name, asn1p_itoa(v2e[eidx].value)); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 206 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 207 | if(map_extensions) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 208 | OUT("\t/* This list is extensible */\n"); |
| 209 | OUT("};\n"); |
| 210 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 211 | if(!(expr->_type_referenced)) OUT("static "); |
| 212 | OUT("const asn_INTEGER_specifics_t asn_SPC_%s_specs_%d = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 213 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 214 | INDENT(+1); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 215 | OUT("asn_MAP_%s_value2enum_%d,\t" |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 216 | "/* \"tag\" => N; sorted by tag */\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 217 | MKID(expr), |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 218 | expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 219 | OUT("asn_MAP_%s_enum2value_%d,\t" |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 220 | "/* N => \"tag\"; sorted by N */\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 221 | MKID(expr), |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 222 | expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 223 | OUT("%d,\t/* Number of elements in the maps */\n", |
| 224 | el_count); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 225 | if(map_extensions) { |
| 226 | OUT("%d,\t/* Extensions before this member */\n", |
| 227 | map_extensions); |
| 228 | } else { |
| 229 | OUT("0,\t/* Enumeration is not extensible */\n"); |
| 230 | } |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 231 | if(expr->expr_type == ASN_BASIC_ENUMERATED) |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 232 | OUT("1,\t/* Strict enumeration */\n"); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 233 | else |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 234 | OUT("0,\n"); |
| 235 | OUT("0,\t/* Native long size */\n"); |
| 236 | OUT("0\n"); |
| 237 | INDENT(-1); |
| 238 | OUT("};\n"); |
| 239 | } |
| 240 | |
| 241 | if(expr->expr_type == ASN_BASIC_INTEGER |
| 242 | && asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN) { |
| 243 | REDIR(OT_STAT_DEFS); |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 244 | if(!(expr->_type_referenced)) OUT("static "); |
| 245 | OUT("const asn_INTEGER_specifics_t asn_SPC_%s_specs_%d = {\n", |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 246 | MKID(expr), expr->_type_unique_index); |
| 247 | INDENT(+1); |
| 248 | OUT("0,\t"); |
| 249 | OUT("0,\t"); |
| 250 | OUT("0,\t"); |
| 251 | OUT("0,\t"); |
| 252 | OUT("0,\n"); |
| 253 | OUT("0,\t/* Native long size */\n"); |
| 254 | OUT("1\t/* Unsigned representation */\n"); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 255 | INDENT(-1); |
| 256 | OUT("};\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 259 | REDIR(saved_target); |
| 260 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 261 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 262 | } |
| 263 | |
| 264 | int |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 265 | asn1c_lang_C_type_BIT_STRING(arg_t *arg) { |
| 266 | asn1p_expr_t *expr = arg->expr; |
| 267 | asn1p_expr_t *v; |
| 268 | int el_count = expr_elements_count(arg, expr); |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 269 | int saved_target = arg->target->target; |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 270 | |
| 271 | if(el_count) { |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 272 | int eidx = 0; |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 273 | REDIR(OT_DEPS); |
| 274 | OUT("typedef enum "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 275 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 276 | OUT(" {\n"); |
| 277 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 278 | if(v->expr_type != A1TC_UNIVERVAL) { |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 279 | OUT("/* Unexpected BIT STRING element: %s */\n", |
| 280 | v->Identifier); |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 281 | continue; |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 282 | } |
Lev Walkin | 0f8d4a4 | 2006-08-26 03:29:15 +0000 | [diff] [blame] | 283 | eidx++; |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 284 | OUT("\t"); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 285 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 286 | OUT("_%s", MKID(v)); |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 287 | OUT("\t= %s%s\n", |
| 288 | asn1p_itoa(v->value->value.v_integer), |
Lev Walkin | 082cadc | 2005-08-14 02:18:27 +0000 | [diff] [blame] | 289 | (eidx < el_count) ? "," : ""); |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 290 | } |
Lev Walkin | 171487e | 2006-03-21 07:25:18 +0000 | [diff] [blame] | 291 | OUT("} e_"); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 292 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | 171487e | 2006-03-21 07:25:18 +0000 | [diff] [blame] | 293 | OUT(";\n"); |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 294 | assert(eidx == el_count); |
| 295 | } |
| 296 | |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 297 | REDIR(saved_target); |
| 298 | |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 299 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 300 | } |
| 301 | |
| 302 | int |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 303 | asn1c_lang_C_type_SEQUENCE(arg_t *arg) { |
| 304 | asn1p_expr_t *expr = arg->expr; |
| 305 | asn1p_expr_t *v; |
| 306 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 307 | int saved_target = arg->target->target; |
Lev Walkin | ad0d637 | 2017-08-08 02:02:42 -0700 | [diff] [blame^] | 308 | asn1c_ioc_table_and_objset_t ioc_tao; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 309 | |
| 310 | DEPENDENCIES; |
| 311 | |
Lev Walkin | ad0d637 | 2017-08-08 02:02:42 -0700 | [diff] [blame^] | 312 | ioc_tao = asn1c_get_ioc_table(arg); |
| 313 | if(ioc_tao.ioct) { |
| 314 | if(emit_ioc_table(arg, expr, ioc_tao)) { |
| 315 | return -1; |
| 316 | } |
| 317 | } else if(ioc_tao.fatal_error) { |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 318 | return -1; |
| 319 | } |
| 320 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 321 | if(arg->embed) { |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 322 | |
| 323 | /* Use _anonymous_type field to indicate it's called from |
| 324 | * asn1c_lang_C_type_SEx_OF() */ |
| 325 | if (expr->_anonymous_type) { |
| 326 | REDIR(OT_FWD_DEFS); |
| 327 | OUT("typedef "); |
| 328 | } |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 329 | OUT("struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 330 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 331 | OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 332 | } else { |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 333 | REDIR(OT_TYPE_DECLS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 334 | OUT("typedef struct %s {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 335 | MKID_safe(expr)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 339 | if(v->expr_type == A1TC_EXTENSIBLE) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 340 | if(comp_mode < 3) comp_mode++; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 341 | if(comp_mode == 1) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 342 | v->marker.flags |= EM_OMITABLE | EM_INDIRECT; |
| 343 | try_inline_default(arg, v, 1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 344 | EMBED(v); |
| 345 | } |
| 346 | |
| 347 | PCTX_DEF; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 348 | |
| 349 | if (arg->embed && expr->_anonymous_type) { |
| 350 | OUT("} %s", (expr->marker.flags & EM_INDIRECT)?"*":""); |
| 351 | out_name_chain(arg, ONC_avoid_keywords); |
| 352 | OUT("%s;\n", arg->embed ? "" : "_t"); |
| 353 | |
| 354 | REDIR(saved_target); |
| 355 | |
| 356 | OUT("%s", (expr->marker.flags & EM_INDIRECT)?"*":""); |
| 357 | out_name_chain(arg, ONC_avoid_keywords); |
| 358 | } else { |
| 359 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
Lev Walkin | caf0d5a | 2005-03-04 23:48:19 +0000 | [diff] [blame] | 360 | expr->_anonymous_type ? "" : |
| 361 | arg->embed |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 362 | ? MKID_safe(expr) |
| 363 | : MKID(expr), |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 364 | arg->embed ? "" : "_t"); |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 365 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 366 | |
| 367 | return asn1c_lang_C_type_SEQUENCE_def(arg); |
| 368 | } |
| 369 | |
| 370 | static int |
| 371 | asn1c_lang_C_type_SEQUENCE_def(arg_t *arg) { |
| 372 | asn1p_expr_t *expr = arg->expr; |
| 373 | asn1p_expr_t *v; |
| 374 | int elements; /* Number of elements */ |
Lev Walkin | 33af5c3 | 2007-06-23 20:05:49 +0000 | [diff] [blame] | 375 | int ext_start = -2; |
| 376 | int ext_stop = -2; |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 377 | tag2el_t *tag2el = NULL; |
| 378 | int tag2el_count = 0; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 379 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 380 | int all_tags_count; |
| 381 | enum tvm_compat tv_mode; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 382 | int roms_count; /* Root optional members */ |
| 383 | int aoms_count; /* Additions optional members */ |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 384 | int saved_target = arg->target->target; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 385 | |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 386 | /* |
| 387 | * Fetch every inner tag from the tag to elements map. |
| 388 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 389 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) { |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 390 | if(tag2el) free(tag2el); |
| 391 | return -1; |
| 392 | } |
| 393 | |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 394 | GEN_INCLUDE_STD("constr_SEQUENCE"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 395 | if(!arg->embed) |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 396 | GEN_DECLARE("SEQUENCE", expr); /* asn_DEF_xxx */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 397 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 398 | REDIR(OT_STAT_DEFS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 399 | |
| 400 | /* |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 401 | * Print out the table according to which parsing is performed. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 402 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 403 | if(expr_elements_count(arg, expr)) { |
| 404 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 405 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 406 | if(!(expr->_type_referenced)) OUT("static "); |
| 407 | OUT("asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 408 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 409 | |
| 410 | elements = 0; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 411 | roms_count = 0; |
| 412 | aoms_count = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 413 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
| 414 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 415 | if((++comp_mode) == 1) |
| 416 | ext_start = elements - 1; |
| 417 | else |
| 418 | ext_stop = elements - 1; |
| 419 | continue; |
| 420 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 421 | if(v->marker.flags & EM_OMITABLE) |
| 422 | comp_mode == 1 ? ++aoms_count : ++roms_count; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 423 | emit_member_table(arg, v); |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 424 | elements++; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 425 | }); |
| 426 | OUT("};\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 427 | |
Lev Walkin | 3c5d941 | 2017-07-24 01:49:24 +0400 | [diff] [blame] | 428 | if((roms_count + aoms_count) && (arg->flags & (A1C_GEN_OER|A1C_GEN_PER))) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 429 | int elm = 0; |
| 430 | int comma = 0; |
| 431 | comp_mode = 0; |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 432 | OUT("static const int asn_MAP_%s_oms_%d[] = {", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 433 | MKID(expr), |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 434 | expr->_type_unique_index); |
| 435 | TQ_FOR(v, &(expr->members), next) { |
| 436 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 437 | ++comp_mode; |
| 438 | continue; |
| 439 | } |
| 440 | if((v->marker.flags & EM_OMITABLE) |
| 441 | && comp_mode != 1) { |
| 442 | if(!comma) comma++; |
| 443 | else OUT(","); |
| 444 | OUT(" %d", elm); |
| 445 | } |
| 446 | ++elm; |
| 447 | } |
| 448 | elm = 0; |
| 449 | comp_mode = 0; |
| 450 | TQ_FOR(v, &(expr->members), next) { |
| 451 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 452 | ++comp_mode; |
| 453 | continue; |
| 454 | } |
| 455 | if((v->marker.flags & EM_OMITABLE) |
| 456 | && comp_mode == 1) { |
| 457 | if(!comma) comma++; |
| 458 | else OUT(","); |
| 459 | OUT(" %d", elm); |
| 460 | } |
| 461 | ++elm; |
| 462 | } |
| 463 | OUT(" };\n"); |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 464 | if(roms_count > 65536) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 465 | FATAL("Too many optional elements in %s " |
| 466 | "at line %d!", |
| 467 | arg->expr->Identifier, |
| 468 | arg->expr->_lineno); |
Lev Walkin | ea6635b | 2017-08-06 23:23:04 -0700 | [diff] [blame] | 469 | return -1; |
| 470 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 471 | } else { |
| 472 | roms_count = 0; |
| 473 | aoms_count = 0; |
| 474 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 475 | } else { |
| 476 | elements = 0; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 477 | roms_count = 0; |
| 478 | aoms_count = 0; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 479 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 480 | |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 481 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 482 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 483 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 484 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 485 | |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 486 | /* |
| 487 | * Tags to elements map. |
| 488 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 489 | emit_tag2member_map(arg, tag2el, tag2el_count, 0); |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 490 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 491 | if(!(expr->_type_referenced)) OUT("static "); |
| 492 | OUT("asn_SEQUENCE_specifics_t asn_SPC_%s_specs_%d = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 493 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 494 | INDENT(+1); |
| 495 | OUT("sizeof(struct "); |
| 496 | out_name_chain(arg, ONC_avoid_keywords); OUT("),\n"); |
| 497 | OUT("offsetof(struct "); |
| 498 | out_name_chain(arg, ONC_avoid_keywords); OUT(", _asn_ctx),\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 499 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 500 | if(tag2el_count) { |
| 501 | OUT("asn_MAP_%s_tag2el_%d,\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 502 | MKID(expr), |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 503 | expr->_type_unique_index); |
| 504 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
| 505 | } else { |
| 506 | OUT("0,\t/* No top level tags */\n"); |
| 507 | OUT("0,\t/* No tags in the map */\n"); |
| 508 | } |
| 509 | if(roms_count + aoms_count) { |
| 510 | OUT("asn_MAP_%s_oms_%d,\t/* Optional members */\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 511 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 512 | OUT("%d, %d,\t/* Root/Additions */\n", roms_count, aoms_count); |
| 513 | } else { |
| 514 | OUT("0, 0, 0,\t/* Optional elements (not needed) */\n"); |
| 515 | } |
| 516 | OUT("%d,\t/* Start extensions */\n", |
Lev Walkin | 33af5c3 | 2007-06-23 20:05:49 +0000 | [diff] [blame] | 517 | ext_start<0 ? -1 : ext_start); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 518 | OUT("%d\t/* Stop extensions */\n", |
Lev Walkin | 33af5c3 | 2007-06-23 20:05:49 +0000 | [diff] [blame] | 519 | (ext_stop<ext_start)?elements+1:(ext_stop<0?-1:ext_stop)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 520 | INDENT(-1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 521 | OUT("};\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 522 | |
| 523 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 524 | * Emit asn_DEF_xxx table. |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 525 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 526 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements, |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 527 | ETD_HAS_SPECIFICS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 528 | |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 529 | REDIR(saved_target); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 530 | |
Bi-Ruei, Chiu | 12021b5 | 2016-11-04 11:59:45 +0800 | [diff] [blame] | 531 | if(tag2el) free(tag2el); |
| 532 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 533 | return 0; |
Lev Walkin | b0b3341 | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 534 | } /* _SEQUENCE_def() */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 535 | |
| 536 | int |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 537 | asn1c_lang_C_type_SET(arg_t *arg) { |
| 538 | asn1p_expr_t *expr = arg->expr; |
| 539 | asn1p_expr_t *v; |
| 540 | long mcount; |
| 541 | char *id; |
| 542 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 543 | int saved_target = arg->target->target; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 544 | |
| 545 | DEPENDENCIES; |
| 546 | |
| 547 | REDIR(OT_DEPS); |
| 548 | |
| 549 | OUT("\n"); |
| 550 | OUT("/*\n"); |
| 551 | OUT(" * Method of determining the components presence\n"); |
| 552 | OUT(" */\n"); |
| 553 | mcount = 0; |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 554 | OUT("typedef enum "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 555 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 556 | OUT("_PR {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 557 | TQ_FOR(v, &(expr->members), next) { |
| 558 | if(v->expr_type == A1TC_EXTENSIBLE) continue; |
| 559 | INDENTED( |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 560 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 561 | OUT("_PR_"); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 562 | id = MKID(v); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 563 | OUT("%s,\t/* Member %s is present */\n", |
| 564 | id, id) |
| 565 | ); |
| 566 | mcount++; |
| 567 | } |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 568 | OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 569 | |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 570 | REDIR(saved_target); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 571 | |
| 572 | if(arg->embed) { |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 573 | if (expr->_anonymous_type) { |
| 574 | REDIR(OT_FWD_DEFS); |
| 575 | OUT("typedef "); |
| 576 | } |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 577 | OUT("struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 578 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 579 | OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 580 | } else { |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 581 | REDIR(OT_TYPE_DECLS); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 582 | OUT("typedef struct %s {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 583 | MKID_safe(expr)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 587 | if(v->expr_type == A1TC_EXTENSIBLE) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 588 | if(comp_mode < 3) comp_mode++; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 589 | if(comp_mode == 1) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 590 | v->marker.flags |= EM_OMITABLE | EM_INDIRECT; |
| 591 | try_inline_default(arg, v, 1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 592 | EMBED(v); |
| 593 | } |
| 594 | |
| 595 | INDENTED( |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 596 | id = MKID(expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 597 | OUT("\n"); |
| 598 | OUT("/* Presence bitmask: ASN_SET_ISPRESENT(p%s, %s_PR_x) */\n", |
| 599 | id, id); |
| 600 | OUT("unsigned int _presence_map\n"); |
| 601 | OUT("\t[((%ld+(8*sizeof(unsigned int))-1)/(8*sizeof(unsigned int)))];\n", mcount); |
| 602 | ); |
| 603 | |
| 604 | PCTX_DEF; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 605 | |
| 606 | if (arg->embed && expr->_anonymous_type) { |
| 607 | OUT("} %s", (expr->marker.flags & EM_INDIRECT)?"*":""); |
| 608 | out_name_chain(arg, ONC_avoid_keywords); |
| 609 | OUT("%s;\n", arg->embed ? "" : "_t"); |
| 610 | |
| 611 | REDIR(saved_target); |
| 612 | |
| 613 | OUT("%s", (expr->marker.flags & EM_INDIRECT)?"*":""); |
| 614 | out_name_chain(arg, ONC_avoid_keywords); |
| 615 | } else { |
| 616 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
| 617 | expr->_anonymous_type ? "" : MKID_safe(expr), |
| 618 | arg->embed ? "" : "_t"); |
| 619 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 620 | |
| 621 | return asn1c_lang_C_type_SET_def(arg); |
| 622 | } |
| 623 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 624 | static int |
| 625 | asn1c_lang_C_type_SET_def(arg_t *arg) { |
| 626 | asn1p_expr_t *expr = arg->expr; |
| 627 | asn1p_expr_t *v; |
| 628 | int elements; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 629 | tag2el_t *tag2el = NULL; |
| 630 | int tag2el_count = 0; |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 631 | tag2el_t *tag2el_cxer = NULL; |
| 632 | int tag2el_cxer_count = 0; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 633 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 634 | int all_tags_count; |
| 635 | enum tvm_compat tv_mode; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 636 | char *p; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 637 | int saved_target = arg->target->target; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 638 | |
| 639 | /* |
| 640 | * Fetch every inner tag from the tag to elements map. |
| 641 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 642 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 643 | if(tag2el) free(tag2el); |
| 644 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 645 | } |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 646 | if(_fill_tag2el_map(arg, &tag2el_cxer, &tag2el_cxer_count, -1, FTE_CANONICAL_XER)) { |
| 647 | if(tag2el) free(tag2el); |
| 648 | if(tag2el_cxer) free(tag2el_cxer); |
| 649 | return -1; |
| 650 | } |
| 651 | if(tag2el_cxer_count == tag2el_count |
| 652 | && memcmp(tag2el, tag2el_cxer, tag2el_count) == 0) { |
| 653 | free(tag2el_cxer); |
| 654 | tag2el_cxer = 0; |
| 655 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 656 | |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 657 | GEN_INCLUDE_STD("constr_SET"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 658 | if(!arg->embed) |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 659 | GEN_DECLARE("SET", expr); /* asn_DEF_xxx */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 660 | |
| 661 | REDIR(OT_STAT_DEFS); |
| 662 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 663 | /* |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 664 | * Print out the table according to which parsing is performed. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 665 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 666 | if(expr_elements_count(arg, expr)) { |
| 667 | int comp_mode = 0; /* {root,ext=1,root,root,...} */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 668 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 669 | if(!(expr->_type_referenced)) OUT("static "); |
| 670 | OUT("asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 671 | MKID(expr), expr->_type_unique_index); |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 672 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 673 | elements = 0; |
| 674 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
| 675 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 676 | if(comp_mode < 3) comp_mode++; |
| 677 | } else { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 678 | emit_member_table(arg, v); |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 679 | elements++; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 680 | } |
| 681 | }); |
| 682 | OUT("};\n"); |
| 683 | } else { |
| 684 | elements = 0; |
| 685 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 686 | |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 687 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 688 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 689 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 690 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | * Tags to elements map. |
| 694 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 695 | emit_tag2member_map(arg, tag2el, tag2el_count, 0); |
| 696 | if(tag2el_cxer) |
| 697 | emit_tag2member_map(arg, tag2el_cxer, tag2el_cxer_count, "_cxer"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 698 | |
| 699 | /* |
| 700 | * Emit a map of mandatory elements. |
| 701 | */ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 702 | OUT("static const uint8_t asn_MAP_%s_mmap_%d", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 703 | MKID(expr), expr->_type_unique_index); |
| 704 | p = MKID_safe(expr); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 705 | OUT("[(%d + (8 * sizeof(unsigned int)) - 1) / 8]", elements); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 706 | OUT(" = {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 707 | INDENTED( |
| 708 | if(elements) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 709 | int el = 0; |
| 710 | TQ_FOR(v, &(expr->members), next) { |
| 711 | if(v->expr_type == A1TC_EXTENSIBLE) continue; |
Lev Walkin | ac760bf | 2005-10-05 08:59:03 +0000 | [diff] [blame] | 712 | if(el) { |
| 713 | if((el % 8) == 0) |
| 714 | OUT(",\n"); |
| 715 | else |
| 716 | OUT(" | "); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 717 | } |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 718 | OUT("(%d << %d)", |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 719 | (v->marker.flags & EM_OMITABLE) != EM_OMITABLE, |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 720 | 7 - (el % 8)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 721 | el++; |
| 722 | } |
| 723 | } else { |
| 724 | OUT("0"); |
| 725 | } |
| 726 | ); |
| 727 | OUT("\n"); |
| 728 | OUT("};\n"); |
| 729 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 730 | if(!(expr->_type_referenced)) OUT("static \n"); |
| 731 | OUT("asn_SET_specifics_t asn_SPC_%s_specs_%d = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 732 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 733 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 734 | OUT("sizeof(struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 735 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 736 | OUT("),\n"); |
| 737 | OUT("offsetof(struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 738 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 739 | OUT(", _asn_ctx),\n"); |
| 740 | OUT("offsetof(struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 741 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 742 | OUT(", _presence_map),\n"); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 743 | p = MKID(expr); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 744 | OUT("asn_MAP_%s_tag2el_%d,\n", p, expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 745 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 746 | if(tag2el_cxer) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 747 | OUT("asn_MAP_%s_tag2el_cxer_%d,\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 748 | p, expr->_type_unique_index); |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 749 | else |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 750 | OUT("asn_MAP_%s_tag2el_%d,\t/* Same as above */\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 751 | p, expr->_type_unique_index); |
| 752 | OUT("%d,\t/* Count of tags in the CXER map */\n", |
| 753 | tag2el_cxer_count); |
Lev Walkin | 3563148 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 754 | OUT("%d,\t/* Whether extensible */\n", |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 755 | compute_extensions_start(expr) == -1 ? 0 : 1); |
Lev Walkin | 46ef3f3 | 2017-07-06 08:47:53 -0700 | [diff] [blame] | 756 | OUT("(const unsigned int *)asn_MAP_%s_mmap_%d\t/* Mandatory elements map */\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 757 | p, expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 758 | ); |
| 759 | OUT("};\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 760 | |
| 761 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 762 | * Emit asn_DEF_xxx table. |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 763 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 764 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements, |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 765 | ETD_HAS_SPECIFICS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 766 | |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 767 | REDIR(saved_target); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 768 | |
Bi-Ruei, Chiu | 12021b5 | 2016-11-04 11:59:45 +0800 | [diff] [blame] | 769 | if (tag2el) free(tag2el); |
| 770 | if (tag2el_cxer) free(tag2el_cxer); |
| 771 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 772 | return 0; |
Lev Walkin | b0b3341 | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 773 | } /* _SET_def() */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 774 | |
| 775 | int |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 776 | asn1c_lang_C_type_SEx_OF(arg_t *arg) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 777 | asn1p_expr_t *expr = arg->expr; |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 778 | asn1p_expr_t *memb = TQ_FIRST(&expr->members); |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 779 | int saved_target = arg->target->target; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 780 | |
| 781 | DEPENDENCIES; |
| 782 | |
| 783 | if(arg->embed) { |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 784 | if (expr->_anonymous_type) { |
| 785 | REDIR(OT_FWD_DEFS); |
| 786 | OUT("typedef "); |
| 787 | } |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 788 | OUT("struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 789 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 790 | OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 791 | } else { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 792 | OUT("typedef struct %s {\n", MKID_safe(expr)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 795 | INDENT(+1); |
| 796 | OUT("A_%s_OF(", |
| 797 | (arg->expr->expr_type == ASN_CONSTR_SET_OF) |
| 798 | ? "SET" : "SEQUENCE"); |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 799 | |
| 800 | /* |
| 801 | * README README |
| 802 | * The implementation of the A_SET_OF() macro is already indirect. |
| 803 | */ |
| 804 | memb->marker.flags |= EM_INDIRECT; |
| 805 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 806 | if(memb->expr_type & ASN_CONSTR_MASK |
| 807 | || ((memb->expr_type == ASN_BASIC_ENUMERATED |
Lev Walkin | 8ecf9db | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 808 | || (0 /* -- prohibited by X.693:8.3.4 */ |
| 809 | && memb->expr_type == ASN_BASIC_INTEGER)) |
| 810 | && expr_elements_count(arg, memb))) { |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 811 | arg_t tmp; |
| 812 | asn1p_expr_t tmp_memb; |
| 813 | arg->embed++; |
| 814 | tmp = *arg; |
| 815 | tmp.expr = &tmp_memb; |
| 816 | tmp_memb = *memb; |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 817 | tmp_memb.marker.flags &= ~EM_INDIRECT; |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 818 | tmp_memb._anonymous_type = 1; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 819 | if(tmp_memb.Identifier == 0) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 820 | tmp_memb.Identifier = "Member"; |
| 821 | if(0) |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 822 | tmp_memb.Identifier = strdup( |
| 823 | asn1c_make_identifier(0, |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 824 | expr, "Member", 0)); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 825 | assert(tmp_memb.Identifier); |
| 826 | } |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 827 | tmp.default_cb(&tmp); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 828 | if(tmp_memb.Identifier != memb->Identifier) |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 829 | if(0) free(tmp_memb.Identifier); |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 830 | arg->embed--; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 831 | assert(arg->target->target == OT_TYPE_DECLS || |
| 832 | arg->target->target == OT_FWD_DEFS); |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 833 | } else { |
Lev Walkin | fe7f6ec | 2005-03-04 22:38:22 +0000 | [diff] [blame] | 834 | OUT("%s", asn1c_type_name(arg, memb, |
| 835 | (memb->marker.flags & EM_UNRECURSE) |
| 836 | ? TNF_RSAFE : TNF_CTYPE)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 837 | } |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 838 | /* README README (above) */ |
| 839 | if(0 && (memb->marker.flags & EM_INDIRECT)) |
| 840 | OUT(" *"); |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 841 | OUT(") list;\n"); |
| 842 | INDENT(-1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 843 | |
| 844 | PCTX_DEF; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 845 | |
| 846 | if (arg->embed && expr->_anonymous_type) { |
| 847 | OUT("} %s", (expr->marker.flags & EM_INDIRECT)?"*":""); |
| 848 | out_name_chain(arg, ONC_avoid_keywords); |
| 849 | OUT("%s;\n", arg->embed ? "" : "_t"); |
| 850 | |
| 851 | REDIR(saved_target); |
| 852 | |
| 853 | OUT("%s", (expr->marker.flags & EM_INDIRECT)?"*":""); |
| 854 | out_name_chain(arg, ONC_avoid_keywords); |
| 855 | } else { |
| 856 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
| 857 | expr->_anonymous_type ? "" : MKID_safe(expr), |
| 858 | arg->embed ? "" : "_t"); |
| 859 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 860 | |
| 861 | /* |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 862 | * SET OF/SEQUENCE OF definition |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 863 | */ |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 864 | return asn1c_lang_C_type_SEx_OF_def(arg, |
| 865 | (arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | static int |
| 869 | asn1c_lang_C_type_SEx_OF_def(arg_t *arg, int seq_of) { |
| 870 | asn1p_expr_t *expr = arg->expr; |
| 871 | asn1p_expr_t *v; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 872 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 873 | int all_tags_count; |
| 874 | enum tvm_compat tv_mode; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 875 | int saved_target = arg->target->target; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 876 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 877 | /* |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 878 | * Print out the table according to which parsing is performed. |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 879 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 880 | if(seq_of) { |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 881 | GEN_INCLUDE_STD("constr_SEQUENCE_OF"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 882 | } else { |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 883 | GEN_INCLUDE_STD("constr_SET_OF"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 884 | } |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 885 | if(!arg->embed) |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 886 | GEN_DECLARE("SET_OF", expr); /* asn_DEF_xxx */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 887 | |
| 888 | REDIR(OT_STAT_DEFS); |
| 889 | |
| 890 | /* |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 891 | * Print out the table according to which parsing is performed. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 892 | */ |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 893 | if(!(expr->_type_referenced)) OUT("static "); |
| 894 | OUT("asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 895 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 896 | INDENT(+1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 897 | v = TQ_FIRST(&(expr->members)); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 898 | if(!v->Identifier) { |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 899 | v->Identifier = strdup("Member"); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 900 | assert(v->Identifier); |
| 901 | } |
| 902 | v->_anonymous_type = 1; |
| 903 | arg->embed++; |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 904 | emit_member_table(arg, v); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 905 | arg->embed--; |
Bi-Ruei, Chiu | 9b78083 | 2016-11-07 22:19:10 +0800 | [diff] [blame] | 906 | free(v->Identifier); |
| 907 | v->Identifier = (char *)NULL; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 908 | INDENT(-1); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 909 | OUT("};\n"); |
| 910 | |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 911 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 912 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 913 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 914 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 915 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 916 | if(!(expr->_type_referenced)) OUT("static "); |
| 917 | OUT("asn_SET_OF_specifics_t asn_SPC_%s_specs_%d = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 918 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 919 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 920 | OUT("sizeof(struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 921 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 922 | OUT("),\n"); |
| 923 | OUT("offsetof(struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 924 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 925 | OUT(", _asn_ctx),\n"); |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 926 | { |
| 927 | int as_xvl = expr_as_xmlvaluelist(arg, v); |
| 928 | OUT("%d,\t/* XER encoding is %s */\n", |
| 929 | as_xvl, |
| 930 | as_xvl ? "XMLValueList" : "XMLDelimitedItemList"); |
| 931 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 932 | ); |
| 933 | OUT("};\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 934 | |
| 935 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 936 | * Emit asn_DEF_xxx table. |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 937 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 938 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, 1, |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 939 | ETD_HAS_SPECIFICS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 940 | |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 941 | REDIR(saved_target); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 942 | |
| 943 | return 0; |
Lev Walkin | b0b3341 | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 944 | } /* _SEx_OF_def() */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 945 | |
| 946 | int |
| 947 | asn1c_lang_C_type_CHOICE(arg_t *arg) { |
| 948 | asn1p_expr_t *expr = arg->expr; |
| 949 | asn1p_expr_t *v; |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 950 | char *id; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 951 | int saved_target = arg->target->target; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 952 | |
| 953 | DEPENDENCIES; |
| 954 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 955 | REDIR(OT_DEPS); |
| 956 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 957 | OUT("typedef enum "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 958 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 959 | OUT("_PR {\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 960 | INDENTED( |
Lev Walkin | 11380fb | 2006-08-18 01:38:40 +0000 | [diff] [blame] | 961 | int skipComma = 1; |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 962 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 963 | OUT("_PR_NOTHING,\t/* No components present */\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 964 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | 11380fb | 2006-08-18 01:38:40 +0000 | [diff] [blame] | 965 | if(skipComma) skipComma = 0; |
johvik | 792216e | 2017-04-11 10:42:21 +0200 | [diff] [blame] | 966 | else if (v->expr_type == A1TC_EXTENSIBLE && !TQ_NEXT(v, next)) OUT("\n"); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 967 | else OUT(",\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 968 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 969 | OUT("/* Extensions may appear below */\n"); |
Lev Walkin | 11380fb | 2006-08-18 01:38:40 +0000 | [diff] [blame] | 970 | skipComma = 1; |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 971 | continue; |
| 972 | } |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 973 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 974 | id = MKID(v); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 975 | OUT("_PR_%s", id); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 976 | } |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 977 | OUT("\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 978 | ); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 979 | OUT("} "); out_name_chain(arg, ONC_noflags); OUT("_PR;\n"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 980 | |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 981 | REDIR(saved_target); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 982 | |
| 983 | if(arg->embed) { |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 984 | if (expr->_anonymous_type) { |
| 985 | REDIR(OT_FWD_DEFS); |
| 986 | OUT("typedef "); |
| 987 | } |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 988 | OUT("struct "); out_name_chain(arg, ONC_avoid_keywords); OUT(" {\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 989 | } else { |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 990 | REDIR(OT_TYPE_DECLS); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 991 | OUT("typedef struct %s {\n", MKID_safe(expr)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | INDENTED( |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 995 | out_name_chain(arg, ONC_noflags); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 996 | OUT("_PR present;\n"); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 997 | OUT("union "); |
| 998 | if(UNNAMED_UNIONS == 0) { |
| 999 | out_name_chain(arg, ONC_force_compound_name); |
| 1000 | OUT("_u "); |
| 1001 | } |
| 1002 | OUT("{\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1003 | TQ_FOR(v, &(expr->members), next) { |
| 1004 | EMBED(v); |
| 1005 | } |
| 1006 | if(UNNAMED_UNIONS) OUT("};\n"); |
| 1007 | else OUT("} choice;\n"); |
| 1008 | ); |
| 1009 | |
| 1010 | PCTX_DEF; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 1011 | |
| 1012 | if (arg->embed && expr->_anonymous_type) { |
| 1013 | OUT("} %s", (expr->marker.flags & EM_INDIRECT)?"*":""); |
| 1014 | out_name_chain(arg, ONC_avoid_keywords); |
| 1015 | OUT("%s;\n", arg->embed ? "" : "_t"); |
| 1016 | |
| 1017 | REDIR(saved_target); |
| 1018 | |
| 1019 | OUT("%s", (expr->marker.flags & EM_INDIRECT)?"*":""); |
| 1020 | out_name_chain(arg, ONC_avoid_keywords); |
| 1021 | } else { |
| 1022 | OUT("} %s%s%s", (expr->marker.flags & EM_INDIRECT)?"*":"", |
| 1023 | expr->_anonymous_type ? "" : |
| 1024 | arg->embed |
| 1025 | ? MKID_safe(expr) |
| 1026 | : MKID(expr), |
| 1027 | arg->embed ? "" : "_t"); |
| 1028 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1029 | |
| 1030 | return asn1c_lang_C_type_CHOICE_def(arg); |
| 1031 | } |
| 1032 | |
| 1033 | static int |
| 1034 | asn1c_lang_C_type_CHOICE_def(arg_t *arg) { |
| 1035 | asn1p_expr_t *expr = arg->expr; |
| 1036 | asn1p_expr_t *v; |
| 1037 | int elements; /* Number of elements */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1038 | tag2el_t *tag2el = NULL; |
| 1039 | int tag2el_count = 0; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1040 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1041 | int all_tags_count; |
| 1042 | enum tvm_compat tv_mode; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1043 | int *cmap = 0; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 1044 | int saved_target = arg->target->target; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1045 | |
| 1046 | /* |
| 1047 | * Fetch every inner tag from the tag to elements map. |
| 1048 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1049 | if(_fill_tag2el_map(arg, &tag2el, &tag2el_count, -1, FTE_ALLTAGS)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1050 | if(tag2el) free(tag2el); |
| 1051 | return -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Lev Walkin | 22b5ed4 | 2006-09-13 02:51:20 +0000 | [diff] [blame] | 1054 | GEN_INCLUDE_STD("constr_CHOICE"); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 1055 | if(!arg->embed) |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 1056 | GEN_DECLARE("CHOICE", expr); /* asn_DEF_xxx */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1057 | |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 1058 | REDIR(OT_STAT_DEFS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1059 | |
| 1060 | /* |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1061 | * Print out the table according to which parsing is performed. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1062 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1063 | if(expr_elements_count(arg, expr)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1064 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 1065 | if(!(expr->_type_referenced)) OUT("static "); |
| 1066 | OUT("asn_TYPE_member_t asn_MBR_%s_%d[] = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1067 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1068 | |
| 1069 | elements = 0; |
| 1070 | INDENTED(TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | 659c63b | 2005-01-17 11:40:49 +0000 | [diff] [blame] | 1071 | if(v->expr_type == A1TC_EXTENSIBLE) |
| 1072 | continue; |
Lev Walkin | 659c63b | 2005-01-17 11:40:49 +0000 | [diff] [blame] | 1073 | emit_member_table(arg, v); |
Lev Walkin | da9a3b8 | 2005-08-16 17:00:21 +0000 | [diff] [blame] | 1074 | elements++; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1075 | }); |
| 1076 | OUT("};\n"); |
| 1077 | } else { |
| 1078 | elements = 0; |
| 1079 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1080 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1081 | /* Create a canonical elements map */ |
| 1082 | if(elements && (arg->flags & A1C_GEN_PER)) { |
| 1083 | int i; |
| 1084 | cmap = compute_canonical_members_order(arg, elements); |
| 1085 | if(cmap) { |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 1086 | OUT("static const int asn_MAP_%s_cmap_%d[] = {", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1087 | MKID(expr), |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1088 | expr->_type_unique_index); |
| 1089 | for(i = 0; i < elements; i++) { |
| 1090 | if(i) OUT(","); |
| 1091 | OUT(" %d", cmap[i]); |
| 1092 | } |
| 1093 | OUT(" };\n"); |
| 1094 | free(cmap); |
| 1095 | } |
| 1096 | } |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1097 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1098 | if(arg->embed) { |
| 1099 | /* |
| 1100 | * Our parent structure has already taken this into account. |
| 1101 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1102 | tv_mode = _TVM_SAME; |
| 1103 | tags_count = all_tags_count = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1104 | } else { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1105 | tv_mode = emit_tags_vectors(arg, expr, |
| 1106 | &tags_count, &all_tags_count); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1107 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1108 | |
| 1109 | /* |
| 1110 | * Tags to elements map. |
| 1111 | */ |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1112 | emit_tag2member_map(arg, tag2el, tag2el_count, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1113 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 1114 | if(!(expr->_type_referenced)) OUT("static "); |
| 1115 | OUT("asn_CHOICE_specifics_t asn_SPC_%s_specs_%d = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1116 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1117 | INDENTED( |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1118 | OUT("sizeof(struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 1119 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1120 | OUT("),\n"); |
| 1121 | OUT("offsetof(struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 1122 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1123 | OUT(", _asn_ctx),\n"); |
| 1124 | OUT("offsetof(struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 1125 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1126 | OUT(", present),\n"); |
| 1127 | OUT("sizeof(((struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 1128 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1129 | OUT(" *)0)->present),\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1130 | OUT("asn_MAP_%s_tag2el_%d,\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1131 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1132 | OUT("%d,\t/* Count of tags in the map */\n", tag2el_count); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1133 | if(C99_MODE) OUT(".canonical_order = "); |
| 1134 | if(cmap) OUT("asn_MAP_%s_cmap_%d,\t/* Canonically sorted */\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1135 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1136 | else OUT("0,\n"); |
| 1137 | if(C99_MODE) OUT(".ext_start = "); |
| 1138 | OUT("%d\t/* Extensions start */\n", |
| 1139 | compute_extensions_start(expr)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1140 | ); |
| 1141 | OUT("};\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1142 | |
| 1143 | /* |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1144 | * Emit asn_DEF_xxx table. |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1145 | */ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1146 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, elements, |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1147 | ETD_HAS_SPECIFICS); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1148 | |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 1149 | REDIR(saved_target); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1150 | |
Bi-Ruei, Chiu | 12021b5 | 2016-11-04 11:59:45 +0800 | [diff] [blame] | 1151 | if (tag2el) free(tag2el); |
| 1152 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1153 | return 0; |
Lev Walkin | b0b3341 | 2005-01-17 11:57:48 +0000 | [diff] [blame] | 1154 | } /* _CHOICE_def() */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1155 | |
| 1156 | int |
| 1157 | asn1c_lang_C_type_REFERENCE(arg_t *arg) { |
| 1158 | asn1p_ref_t *ref; |
| 1159 | |
| 1160 | ref = arg->expr->reference; |
| 1161 | if(ref->components[ref->comp_count-1].name[0] == '&') { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1162 | asn1p_expr_t *extract; |
| 1163 | arg_t tmp; |
| 1164 | int ret; |
| 1165 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1166 | extract = asn1f_class_access_ex(arg->asn, arg->expr->module, |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1167 | arg->expr, arg->expr->rhs_pspecs, ref); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1168 | if(extract == NULL) |
| 1169 | return -1; |
| 1170 | |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 1171 | extract = asn1p_expr_clone(extract, 0); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1172 | if(extract) { |
Lev Walkin | d8b8364 | 2016-03-14 02:00:27 -0700 | [diff] [blame] | 1173 | free(extract->Identifier); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1174 | extract->Identifier = strdup(arg->expr->Identifier); |
| 1175 | if(extract->Identifier == NULL) { |
| 1176 | asn1p_expr_free(extract); |
| 1177 | return -1; |
| 1178 | } |
| 1179 | } else { |
| 1180 | return -1; |
| 1181 | } |
| 1182 | |
| 1183 | tmp = *arg; |
| 1184 | tmp.asn = arg->asn; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1185 | tmp.expr = extract; |
| 1186 | |
| 1187 | ret = arg->default_cb(&tmp); |
| 1188 | |
| 1189 | asn1p_expr_free(extract); |
| 1190 | |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | return asn1c_lang_C_type_SIMPLE_TYPE(arg); |
| 1196 | } |
| 1197 | |
| 1198 | int |
| 1199 | asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) { |
| 1200 | asn1p_expr_t *expr = arg->expr; |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1201 | int tags_count; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1202 | int all_tags_count; |
| 1203 | enum tvm_compat tv_mode; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1204 | enum etd_spec etd_spec; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1205 | char *p; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 1206 | int saved_target = arg->target->target; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1207 | |
| 1208 | if(arg->embed) { |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1209 | enum tnfmt tnfmt = TNF_CTYPE; |
| 1210 | |
| 1211 | /* |
| 1212 | * If this is an optional compound type, |
| 1213 | * refer it using "struct X" convention, |
| 1214 | * as it may recursively include the current structure. |
| 1215 | */ |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1216 | if(expr->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) { |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 1217 | if(terminal_structable(arg, expr)) { |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1218 | tnfmt = TNF_RSAFE; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 1219 | if(saved_target != OT_FWD_DECLS) { |
| 1220 | REDIR(OT_FWD_DECLS); |
| 1221 | OUT("%s;\n", |
| 1222 | asn1c_type_name(arg, arg->expr, tnfmt)); |
| 1223 | } |
| 1224 | REDIR(saved_target); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1225 | } |
| 1226 | } |
| 1227 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1228 | |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1229 | OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt)); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1230 | if(!expr->_anonymous_type) { |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1231 | OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t "); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1232 | OUT("%s", MKID_safe(expr)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1233 | if((expr->marker.flags & (EM_DEFAULT & ~EM_INDIRECT)) |
| 1234 | == (EM_DEFAULT & ~EM_INDIRECT)) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1235 | OUT("\t/* DEFAULT %s */", |
| 1236 | asn1f_printable_value( |
| 1237 | expr->marker.default_value)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1238 | else if((expr->marker.flags & EM_OPTIONAL) |
| 1239 | == EM_OPTIONAL) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1240 | OUT("\t/* OPTIONAL */"); |
| 1241 | } |
| 1242 | |
| 1243 | } else { |
| 1244 | GEN_INCLUDE(asn1c_type_name(arg, expr, TNF_INCLUDE)); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1245 | |
| 1246 | REDIR(OT_TYPE_DECLS); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1247 | |
| 1248 | OUT("typedef %s\t", |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1249 | asn1c_type_name(arg, arg->expr, TNF_CTYPE)); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1250 | OUT("%s%s_t", |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 1251 | (expr->marker.flags & EM_INDIRECT)?"*":" ", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1252 | MKID(expr)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1255 | if((expr->expr_type == ASN_BASIC_ENUMERATED) |
Lev Walkin | 8ecf9db | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 1256 | || (0 /* -- prohibited by X.693:8.3.4 */ |
| 1257 | && expr->expr_type == ASN_BASIC_INTEGER |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 1258 | && expr_elements_count(arg, expr)) |
| 1259 | || (expr->expr_type == ASN_BASIC_INTEGER |
| 1260 | && asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN) |
| 1261 | ) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1262 | etd_spec = ETD_HAS_SPECIFICS; |
| 1263 | else |
| 1264 | etd_spec = ETD_NO_SPECIFICS; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1265 | |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1266 | /* |
| 1267 | * If this type just blindly refers the other type, alias it. |
| 1268 | * Type1 ::= Type2 |
| 1269 | */ |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1270 | if(arg->embed && etd_spec == ETD_NO_SPECIFICS) { |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 1271 | REDIR(saved_target); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1272 | return 0; |
| 1273 | } |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1274 | if((!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS)) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1275 | && (arg->embed || expr->tag.tag_class == TC_NOCLASS) |
Lev Walkin | 6938d04 | 2005-03-04 23:23:50 +0000 | [diff] [blame] | 1276 | && etd_spec == ETD_NO_SPECIFICS |
| 1277 | && 0 /* This shortcut is incompatible with XER */ |
| 1278 | ) { |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1279 | char *type_name; |
| 1280 | REDIR(OT_FUNC_DECLS); |
| 1281 | type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
| 1282 | OUT("/* This type is equivalent to %s */\n", type_name); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1283 | if(HIDE_INNER_DEFS) OUT("/* "); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1284 | OUT("#define\tasn_DEF_%s\t", MKID(expr)); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1285 | type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1286 | OUT("asn_DEF_%s", type_name); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1287 | if(HIDE_INNER_DEFS) |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1288 | OUT("\t// (Use -fall-defs-global to expose) */"); |
| 1289 | OUT("\n"); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1290 | REDIR(OT_CODE); |
| 1291 | OUT("/* This type is equivalent to %s */\n", type_name); |
| 1292 | OUT("\n"); |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 1293 | REDIR(saved_target); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1294 | return 0; |
| 1295 | } |
| 1296 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1297 | REDIR(OT_CODE); |
| 1298 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1299 | /* |
| 1300 | * Constraint checking. |
| 1301 | */ |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 1302 | if(!(arg->flags & A1C_NO_CONSTRAINTS) && expr->combined_constraints) { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1303 | p = MKID(expr); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1304 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1305 | OUT("int\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1306 | OUT("%s", p); |
| 1307 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1308 | OUT("_constraint(asn_TYPE_descriptor_t *td, const void *sptr,\n"); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1309 | INDENT(+1); |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 1310 | OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {"); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1311 | OUT("\n"); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 1312 | DEBUG("expr constraint checking code for %s", p); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1313 | if(asn1c_emit_constraint_checking_code(arg) == 1) { |
| 1314 | OUT("/* Replace with underlying type checker */\n"); |
| 1315 | OUT("td->check_constraints " |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1316 | "= asn_DEF_%s.check_constraints;\n", |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1317 | asn1c_type_name(arg, expr, TNF_SAFE)); |
| 1318 | OUT("return td->check_constraints" |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 1319 | "(td, sptr, ctfailcb, app_key);\n"); |
Lev Walkin | 84cd58e | 2004-08-19 13:29:46 +0000 | [diff] [blame] | 1320 | } |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 1321 | INDENT(-1); |
| 1322 | OUT("}\n"); |
| 1323 | OUT("\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1324 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1325 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 1326 | REDIR(OT_STAT_DEFS); |
| 1327 | |
| 1328 | /* |
| 1329 | * Print out asn_DEF_<type>_[all_]tags[] vectors. |
| 1330 | */ |
| 1331 | tv_mode = emit_tags_vectors(arg, expr, &tags_count, &all_tags_count); |
| 1332 | DEBUG("emit tag vectors for %s %d, %d, %d", expr->Identifier, |
| 1333 | tv_mode, tags_count, all_tags_count); |
| 1334 | |
| 1335 | emit_type_DEF(arg, expr, tv_mode, tags_count, all_tags_count, |
| 1336 | 0, etd_spec); |
| 1337 | |
| 1338 | REDIR(OT_CODE); |
| 1339 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1340 | /* |
| 1341 | * Emit suicidal functions. |
| 1342 | */ |
| 1343 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1344 | /* |
| 1345 | * This function replaces certain fields from the definition |
| 1346 | * of a type with the corresponding fields from the basic type |
| 1347 | * (from which the current type is inherited). |
| 1348 | */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1349 | OUT("/*\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1350 | OUT(" * This type is implemented using %s,\n", |
| 1351 | asn1c_type_name(arg, expr, TNF_SAFE)); |
Lev Walkin | b65b300 | 2004-09-23 22:20:47 +0000 | [diff] [blame] | 1352 | OUT(" * so here we adjust the DEF accordingly.\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1353 | OUT(" */\n"); |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 1354 | |
| 1355 | #if 0 /* remove unnecessary functions */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1356 | OUT("static void\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1357 | OUT("%s_%d_inherit_TYPE_descriptor(asn_TYPE_descriptor_t *td) {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1358 | MKID(expr), expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1359 | INDENT(+1); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1360 | { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1361 | asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1362 | char *type_name = asn1c_type_name(arg, expr, TNF_SAFE); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1363 | OUT("td->free_struct = asn_DEF_%s.free_struct;\n", type_name); |
| 1364 | OUT("td->print_struct = asn_DEF_%s.print_struct;\n", type_name); |
Lev Walkin | 6169b8d | 2013-12-07 11:02:44 -0800 | [diff] [blame] | 1365 | OUT("td->check_constraints = asn_DEF_%s.check_constraints;\n", type_name); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1366 | OUT("td->ber_decoder = asn_DEF_%s.ber_decoder;\n", type_name); |
| 1367 | OUT("td->der_encoder = asn_DEF_%s.der_encoder;\n", type_name); |
| 1368 | OUT("td->xer_decoder = asn_DEF_%s.xer_decoder;\n", type_name); |
| 1369 | OUT("td->xer_encoder = asn_DEF_%s.xer_encoder;\n", type_name); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1370 | OUT("td->uper_decoder = asn_DEF_%s.uper_decoder;\n", type_name); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 1371 | OUT("td->uper_encoder = asn_DEF_%s.uper_encoder;\n", type_name); |
Lev Walkin | ba68c91 | 2017-07-06 07:52:39 -0700 | [diff] [blame] | 1372 | OUT("td->oer_decoder = asn_DEF_%s.oer_decoder;\n", type_name); |
| 1373 | OUT("td->oer_encoder = asn_DEF_%s.oer_encoder;\n", type_name); |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1374 | if(!terminal && !tags_count) { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1375 | OUT("/* The next four lines are here because of -fknown-extern-type */\n"); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1376 | OUT("td->tags = asn_DEF_%s.tags;\n", type_name); |
| 1377 | OUT("td->tags_count = asn_DEF_%s.tags_count;\n", type_name); |
| 1378 | OUT("td->all_tags = asn_DEF_%s.all_tags;\n", type_name); |
| 1379 | OUT("td->all_tags_count = asn_DEF_%s.all_tags_count;\n",type_name); |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1380 | OUT("/* End of these lines */\n"); |
| 1381 | } |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 1382 | OUT("if(!td->oer_constraints)\n"); |
| 1383 | OUT("\ttd->oer_constraints = asn_DEF_%s.oer_constraints;\n", |
| 1384 | type_name); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1385 | OUT("if(!td->per_constraints)\n"); |
| 1386 | OUT("\ttd->per_constraints = asn_DEF_%s.per_constraints;\n", |
| 1387 | type_name); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1388 | OUT("td->elements = asn_DEF_%s.elements;\n", type_name); |
| 1389 | OUT("td->elements_count = asn_DEF_%s.elements_count;\n", type_name); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1390 | if(etd_spec != ETD_NO_SPECIFICS) { |
| 1391 | INDENT(-1); |
Lev Walkin | 7ef83a4 | 2005-03-29 19:04:24 +0000 | [diff] [blame] | 1392 | OUT(" /* "); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1393 | } |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1394 | OUT("td->specifics = asn_DEF_%s.specifics;", type_name); |
| 1395 | if(etd_spec == ETD_NO_SPECIFICS) { |
| 1396 | INDENT(-1); |
| 1397 | OUT("\n"); |
| 1398 | } else { |
| 1399 | OUT("\t// Defined explicitly */\n"); |
| 1400 | } |
| 1401 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1402 | OUT("}\n"); |
| 1403 | OUT("\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1404 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1405 | p = MKID(expr); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1406 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1407 | OUT("void\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1408 | OUT("%s", p); |
| 1409 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1410 | OUT("_free(asn_TYPE_descriptor_t *td,\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1411 | INDENTED( |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1412 | OUT("\tvoid *struct_ptr, int contents_only) {\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1413 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1414 | p, expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1415 | OUT("td->free_struct(td, struct_ptr, contents_only);\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1416 | ); |
| 1417 | OUT("}\n"); |
| 1418 | OUT("\n"); |
| 1419 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1420 | p = MKID(expr); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1421 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1422 | OUT("int\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1423 | OUT("%s", p); |
| 1424 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1425 | OUT("_print(asn_TYPE_descriptor_t *td, const void *struct_ptr,\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1426 | INDENTED( |
| 1427 | OUT("\tint ilevel, asn_app_consume_bytes_f *cb, void *app_key) {\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1428 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1429 | p, expr->_type_unique_index); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1430 | OUT("return td->print_struct(td, struct_ptr, ilevel, cb, app_key);\n"); |
| 1431 | ); |
| 1432 | OUT("}\n"); |
| 1433 | OUT("\n"); |
| 1434 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1435 | p = MKID(expr); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1436 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 1437 | OUT("asn_dec_rval_t\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1438 | OUT("%s", p); |
| 1439 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1440 | OUT("_decode_ber(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1441 | INDENTED( |
Lev Walkin | dd1512e | 2005-03-17 21:56:00 +0000 | [diff] [blame] | 1442 | OUT("\tvoid **structure, const void *bufptr, size_t size, int tag_mode) {\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1443 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1444 | p, expr->_type_unique_index); |
Lev Walkin | 05363a7 | 2004-09-29 13:16:40 +0000 | [diff] [blame] | 1445 | OUT("return td->ber_decoder(opt_codec_ctx, td, structure, bufptr, size, tag_mode);\n"); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1446 | ); |
| 1447 | OUT("}\n"); |
| 1448 | OUT("\n"); |
| 1449 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1450 | p = MKID(expr); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1451 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1452 | OUT("asn_enc_rval_t\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1453 | OUT("%s", p); |
| 1454 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1455 | OUT("_encode_der(asn_TYPE_descriptor_t *td,\n"); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1456 | INDENTED( |
| 1457 | OUT("\tvoid *structure, int tag_mode, ber_tlv_tag_t tag,\n"); |
| 1458 | OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1459 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1460 | p, expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1461 | OUT("return td->der_encoder(td, structure, tag_mode, tag, cb, app_key);\n"); |
| 1462 | ); |
| 1463 | OUT("}\n"); |
| 1464 | OUT("\n"); |
| 1465 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1466 | p = MKID(expr); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1467 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | ca47c29 | 2004-10-23 13:34:00 +0000 | [diff] [blame] | 1468 | OUT("asn_dec_rval_t\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1469 | OUT("%s", p); |
| 1470 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1471 | OUT("_decode_xer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n"); |
Lev Walkin | ca47c29 | 2004-10-23 13:34:00 +0000 | [diff] [blame] | 1472 | INDENTED( |
Lev Walkin | dd1512e | 2005-03-17 21:56:00 +0000 | [diff] [blame] | 1473 | OUT("\tvoid **structure, const char *opt_mname, const void *bufptr, size_t size) {\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1474 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1475 | p, expr->_type_unique_index); |
Lev Walkin | ca47c29 | 2004-10-23 13:34:00 +0000 | [diff] [blame] | 1476 | OUT("return td->xer_decoder(opt_codec_ctx, td, structure, opt_mname, bufptr, size);\n"); |
| 1477 | ); |
| 1478 | OUT("}\n"); |
| 1479 | OUT("\n"); |
| 1480 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1481 | p = MKID(expr); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1482 | if(HIDE_INNER_DEFS) OUT("static "); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1483 | OUT("asn_enc_rval_t\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1484 | OUT("%s", p); |
| 1485 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1486 | OUT("_encode_xer(asn_TYPE_descriptor_t *td, void *structure,\n"); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1487 | INDENTED( |
| 1488 | OUT("\tint ilevel, enum xer_encoder_flags_e flags,\n"); |
| 1489 | OUT("\tasn_app_consume_bytes_f *cb, void *app_key) {\n"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1490 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1491 | p, expr->_type_unique_index); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1492 | OUT("return td->xer_encoder(td, structure, ilevel, flags, cb, app_key);\n"); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1493 | ); |
| 1494 | OUT("}\n"); |
| 1495 | OUT("\n"); |
| 1496 | |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 1497 | if(arg->flags & A1C_GEN_OER) { |
| 1498 | p = MKID(expr); |
| 1499 | |
| 1500 | if(HIDE_INNER_DEFS) OUT("static "); |
| 1501 | OUT("asn_dec_rval_t\n"); |
| 1502 | OUT("%s", p); |
| 1503 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1504 | OUT("_decode_oer(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n"); |
| 1505 | INDENTED( |
| 1506 | OUT("\tasn_oer_constraints_t *constraints, void **structure, const void *buffer, size_t size) {\n"); |
| 1507 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1508 | p, expr->_type_unique_index); |
| 1509 | OUT("return td->oer_decoder(opt_codec_ctx, td, constraints, structure, buffer, size);\n"); |
| 1510 | ); |
| 1511 | OUT("}\n"); |
| 1512 | OUT("\n"); |
| 1513 | |
| 1514 | p = MKID(expr); |
| 1515 | if(HIDE_INNER_DEFS) OUT("static "); |
| 1516 | OUT("asn_enc_rval_t\n"); |
| 1517 | OUT("%s", p); |
| 1518 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1519 | OUT("_encode_oer(asn_TYPE_descriptor_t *td,\n"); |
| 1520 | INDENTED( |
| 1521 | OUT("\tasn_oer_constraints_t *constraints,\n"); |
| 1522 | OUT("\tvoid *structure, asn_app_consume_bytes_f *cb, void *app_key) {\n"); |
| 1523 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1524 | p, expr->_type_unique_index); |
| 1525 | OUT("return td->uper_encoder(td, constraints, structure, cb, app_key);\n"); |
| 1526 | ); |
| 1527 | OUT("}\n"); |
| 1528 | OUT("\n"); |
| 1529 | } |
| 1530 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1531 | if(arg->flags & A1C_GEN_PER) { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1532 | p = MKID(expr); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 1533 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1534 | if(HIDE_INNER_DEFS) OUT("static "); |
| 1535 | OUT("asn_dec_rval_t\n"); |
| 1536 | OUT("%s", p); |
| 1537 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1538 | OUT("_decode_uper(asn_codec_ctx_t *opt_codec_ctx, asn_TYPE_descriptor_t *td,\n"); |
| 1539 | INDENTED( |
| 1540 | OUT("\tasn_per_constraints_t *constraints, void **structure, asn_per_data_t *per_data) {\n"); |
| 1541 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1542 | p, expr->_type_unique_index); |
| 1543 | OUT("return td->uper_decoder(opt_codec_ctx, td, constraints, structure, per_data);\n"); |
| 1544 | ); |
| 1545 | OUT("}\n"); |
| 1546 | OUT("\n"); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 1547 | |
| 1548 | p = MKID(expr); |
| 1549 | if(HIDE_INNER_DEFS) OUT("static "); |
| 1550 | OUT("asn_enc_rval_t\n"); |
| 1551 | OUT("%s", p); |
| 1552 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 1553 | OUT("_encode_uper(asn_TYPE_descriptor_t *td,\n"); |
| 1554 | INDENTED( |
| 1555 | OUT("\tasn_per_constraints_t *constraints,\n"); |
| 1556 | OUT("\tvoid *structure, asn_per_outp_t *per_out) {\n"); |
| 1557 | OUT("%s_%d_inherit_TYPE_descriptor(td);\n", |
| 1558 | p, expr->_type_unique_index); |
| 1559 | OUT("return td->uper_encoder(td, constraints, structure, per_out);\n"); |
| 1560 | ); |
| 1561 | OUT("}\n"); |
| 1562 | OUT("\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1563 | } |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 1564 | #endif |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1565 | REDIR(OT_FUNC_DECLS); |
| 1566 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1567 | p = MKID(expr); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1568 | if(HIDE_INNER_DEFS) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 1569 | OUT("/* extern asn_TYPE_descriptor_t asn_DEF_%s_%d;" |
| 1570 | "\t// (Use -fall-defs-global to expose) */\n", |
| 1571 | p, expr->_type_unique_index); |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1572 | } else { |
| 1573 | OUT("extern asn_TYPE_descriptor_t asn_DEF_%s;\n", p); |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 1574 | if (etd_spec == ETD_HAS_SPECIFICS) { |
| 1575 | if((expr->expr_type == ASN_BASIC_ENUMERATED) || |
| 1576 | (expr->expr_type == ASN_BASIC_INTEGER)) { |
| 1577 | if(expr->_type_referenced) { |
| 1578 | OUT("extern asn_INTEGER_specifics_t " |
| 1579 | "asn_SPC_%s_specs_%d;\n", p, expr->_type_unique_index); |
| 1580 | } |
| 1581 | } else { |
| 1582 | asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 1583 | |
| 1584 | OUT("extern asn_%s_specifics_t ", asn1c_type_name(arg, terminal, TNF_SAFE)); |
| 1585 | OUT("asn_SPC_%s_specs_%d;\n", MKID(expr), expr->_type_unique_index); |
| 1586 | } |
| 1587 | } |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1588 | OUT("asn_struct_free_f %s_free;\n", p); |
| 1589 | OUT("asn_struct_print_f %s_print;\n", p); |
| 1590 | OUT("asn_constr_check_f %s_constraint;\n", p); |
| 1591 | OUT("ber_type_decoder_f %s_decode_ber;\n", p); |
| 1592 | OUT("der_type_encoder_f %s_encode_der;\n", p); |
| 1593 | OUT("xer_type_decoder_f %s_decode_xer;\n", p); |
| 1594 | OUT("xer_type_encoder_f %s_encode_xer;\n", p); |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 1595 | if(arg->flags & A1C_GEN_OER) { |
| 1596 | OUT("oer_type_decoder_f %s_decode_oer;\n", p); |
| 1597 | OUT("oer_type_encoder_f %s_encode_oer;\n", p); |
| 1598 | } |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 1599 | if(arg->flags & A1C_GEN_PER) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1600 | OUT("per_type_decoder_f %s_decode_uper;\n", p); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 1601 | OUT("per_type_encoder_f %s_encode_uper;\n", p); |
| 1602 | } |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1603 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1604 | |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 1605 | REDIR(saved_target); |
Lev Walkin | 3dcaafa | 2004-08-11 05:21:32 +0000 | [diff] [blame] | 1606 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1607 | return 0; |
| 1608 | } |
| 1609 | |
| 1610 | int |
| 1611 | asn1c_lang_C_type_EXTENSIBLE(arg_t *arg) { |
| 1612 | |
| 1613 | OUT("/*\n"); |
| 1614 | OUT(" * This type is extensible,\n"); |
| 1615 | OUT(" * possible extensions are below.\n"); |
| 1616 | OUT(" */\n"); |
| 1617 | |
| 1618 | return 0; |
| 1619 | } |
| 1620 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1621 | static int |
| 1622 | compute_extensions_start(asn1p_expr_t *expr) { |
Lev Walkin | 3563148 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 1623 | asn1p_expr_t *v; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1624 | int eidx = 0; |
Lev Walkin | 3563148 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 1625 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1626 | if(v->expr_type == A1TC_EXTENSIBLE) |
| 1627 | return eidx; |
| 1628 | eidx++; |
Lev Walkin | 3563148 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 1629 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1630 | return -1; |
Lev Walkin | 3563148 | 2004-07-01 00:52:50 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1633 | static int |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1634 | _print_tag(arg_t *arg, struct asn1p_type_tag_s *tag) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1635 | |
| 1636 | OUT("("); |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 1637 | switch(tag->tag_class) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1638 | case TC_UNIVERSAL: OUT("ASN_TAG_CLASS_UNIVERSAL"); break; |
| 1639 | case TC_APPLICATION: OUT("ASN_TAG_CLASS_APPLICATION"); break; |
| 1640 | case TC_CONTEXT_SPECIFIC: OUT("ASN_TAG_CLASS_CONTEXT"); break; |
| 1641 | case TC_PRIVATE: OUT("ASN_TAG_CLASS_PRIVATE"); break; |
| 1642 | case TC_NOCLASS: |
| 1643 | break; |
| 1644 | } |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 1645 | OUT(" | (%s << 2))", asn1p_itoa(tag->tag_value)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1646 | |
| 1647 | return 0; |
| 1648 | } |
| 1649 | |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1650 | |
| 1651 | static int |
| 1652 | _tag2el_cmp(const void *ap, const void *bp) { |
| 1653 | const tag2el_t *a = ap; |
| 1654 | const tag2el_t *b = bp; |
| 1655 | const struct asn1p_type_tag_s *ta = &a->el_tag; |
| 1656 | const struct asn1p_type_tag_s *tb = &b->el_tag; |
| 1657 | |
| 1658 | if(ta->tag_class == tb->tag_class) { |
| 1659 | if(ta->tag_value == tb->tag_value) { |
| 1660 | /* |
| 1661 | * Sort by their respective positions. |
| 1662 | */ |
| 1663 | if(a->el_no < b->el_no) |
| 1664 | return -1; |
| 1665 | else if(a->el_no > b->el_no) |
| 1666 | return 1; |
| 1667 | return 0; |
| 1668 | } else if(ta->tag_value < tb->tag_value) |
| 1669 | return -1; |
| 1670 | else |
| 1671 | return 1; |
| 1672 | } else if(ta->tag_class < tb->tag_class) { |
| 1673 | return -1; |
| 1674 | } else { |
| 1675 | return 1; |
| 1676 | } |
| 1677 | } |
| 1678 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1679 | /* |
| 1680 | * For constructed types, number of external tags may be greater than |
| 1681 | * number of elements in the type because of CHOICE type. |
| 1682 | * T ::= SET { -- Three possible tags: |
| 1683 | * a INTEGER, -- One tag is here... |
| 1684 | * b Choice1 -- ... and two more tags are there. |
| 1685 | * } |
| 1686 | * Choice1 ::= CHOICE { |
| 1687 | * s1 IA5String, |
| 1688 | * s2 ObjectDescriptor |
| 1689 | * } |
| 1690 | */ |
| 1691 | static int |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1692 | _fill_tag2el_map(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1693 | asn1p_expr_t *expr = arg->expr; |
| 1694 | arg_t tmparg = *arg; |
| 1695 | asn1p_expr_t *v; |
| 1696 | int element = 0; |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1697 | int original_count = *count; |
| 1698 | int sort_until = -1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1699 | |
| 1700 | TQ_FOR(v, &(expr->members), next) { |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1701 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 1702 | /* |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1703 | * CXER mandates sorting |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1704 | * only for the root part. |
| 1705 | */ |
| 1706 | if(flags == FTE_CANONICAL_XER |
| 1707 | && sort_until == -1) |
| 1708 | sort_until = *count; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1709 | continue; |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1710 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1711 | |
| 1712 | tmparg.expr = v; |
| 1713 | |
| 1714 | if(_add_tag2el_member(&tmparg, tag2el, count, |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1715 | (el_no==-1)?element:el_no, flags)) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1716 | return -1; |
| 1717 | } |
| 1718 | |
| 1719 | element++; |
| 1720 | } |
| 1721 | |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1722 | |
| 1723 | if(flags == FTE_CANONICAL_XER) { |
| 1724 | if(sort_until == -1) sort_until = *count; |
| 1725 | qsort((*tag2el) + original_count, |
| 1726 | sort_until - original_count, |
| 1727 | sizeof(**tag2el), _tag2el_cmp); |
| 1728 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE |
| 1729 | && (sort_until - original_count) >= 1) { |
| 1730 | /* Only take in account the root component */ |
| 1731 | *count = original_count + 1; |
| 1732 | } |
| 1733 | } else { |
| 1734 | /* |
| 1735 | * Sort the map according to canonical order of their |
| 1736 | * tags and element numbers. |
| 1737 | */ |
| 1738 | qsort(*tag2el, *count, sizeof(**tag2el), _tag2el_cmp); |
| 1739 | } |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1740 | |
Lev Walkin | 38abe79 | 2004-06-14 13:09:45 +0000 | [diff] [blame] | 1741 | /* |
| 1742 | * Initialize .toff_{first|last} members. |
| 1743 | */ |
| 1744 | if(*count) { |
| 1745 | struct asn1p_type_tag_s *cur_tag = 0; |
| 1746 | tag2el_t *cur = *tag2el; |
| 1747 | tag2el_t *end = cur + *count; |
| 1748 | int occur, i; |
| 1749 | for(occur = 0; cur < end; cur++) { |
| 1750 | if(cur_tag == 0 |
| 1751 | || cur_tag->tag_value != cur->el_tag.tag_value |
| 1752 | || cur_tag->tag_class != cur->el_tag.tag_class) { |
| 1753 | cur_tag = &cur->el_tag; |
| 1754 | occur = 0; |
| 1755 | } else { |
| 1756 | occur++; |
| 1757 | } |
| 1758 | cur->toff_first = -occur; |
| 1759 | for(i = 0; i >= -occur; i--) |
| 1760 | cur[i].toff_last = -i; |
| 1761 | } |
| 1762 | } |
| 1763 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1764 | return 0; |
| 1765 | } |
| 1766 | |
| 1767 | static int |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1768 | _add_tag2el_member(arg_t *arg, tag2el_t **tag2el, int *count, int el_no, fte_e flags) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1769 | struct asn1p_type_tag_s tag; |
| 1770 | int ret; |
| 1771 | |
| 1772 | assert(el_no >= 0); |
| 1773 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1774 | ret = asn1f_fetch_outmost_tag(arg->asn, arg->expr->module, |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1775 | arg->expr, &tag, AFT_IMAGINARY_ANY); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1776 | if(ret == 0) { |
Lev Walkin | 201943e | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1777 | tag2el_t *te; |
| 1778 | int new_count = (*count) + 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1779 | void *p; |
Lev Walkin | 201943e | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1780 | |
Lev Walkin | ac7e229 | 2004-09-05 10:42:33 +0000 | [diff] [blame] | 1781 | if(tag.tag_value == -1) { |
| 1782 | /* |
| 1783 | * This is an untagged ANY type, |
| 1784 | * proceed without adding a tag |
| 1785 | */ |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
Lev Walkin | 201943e | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1789 | p = realloc(*tag2el, new_count * sizeof(tag2el_t)); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1790 | if(p) *tag2el = p; |
| 1791 | else return -1; |
| 1792 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 1793 | if(0) DEBUG("Found tag for %s: %ld", |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1794 | arg->expr->Identifier, |
| 1795 | (long)tag.tag_value); |
| 1796 | |
Lev Walkin | 201943e | 2004-09-04 04:42:29 +0000 | [diff] [blame] | 1797 | te = &((*tag2el)[*count]); |
| 1798 | te->el_tag = tag; |
| 1799 | te->el_no = el_no; |
| 1800 | te->from_expr = arg->expr; |
| 1801 | *count = new_count; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1802 | return 0; |
| 1803 | } |
| 1804 | |
| 1805 | DEBUG("Searching tag in complex expression %s:%x at line %d", |
| 1806 | arg->expr->Identifier, |
| 1807 | arg->expr->expr_type, |
| 1808 | arg->expr->_lineno); |
| 1809 | |
| 1810 | /* |
| 1811 | * Iterate over members of CHOICE type. |
| 1812 | */ |
| 1813 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE) { |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1814 | return _fill_tag2el_map(arg, tag2el, count, el_no, flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | if(arg->expr->expr_type == A1TC_REFERENCE) { |
| 1818 | arg_t tmp = *arg; |
| 1819 | asn1p_expr_t *expr; |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 1820 | expr = asn1f_lookup_symbol_ex(tmp.asn, tmp.expr, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1821 | arg->expr->reference); |
| 1822 | if(expr) { |
| 1823 | tmp.expr = expr; |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1824 | return _add_tag2el_member(&tmp, tag2el, count, el_no, flags); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1825 | } else { |
| 1826 | FATAL("Cannot dereference %s at line %d", |
| 1827 | arg->expr->Identifier, |
| 1828 | arg->expr->_lineno); |
| 1829 | return -1; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | DEBUG("No tag for %s at line %d", |
| 1834 | arg->expr->Identifier, |
| 1835 | arg->expr->_lineno); |
| 1836 | |
| 1837 | return -1; |
| 1838 | } |
| 1839 | |
| 1840 | static int |
Lev Walkin | 33d5d3c | 2004-10-03 09:13:30 +0000 | [diff] [blame] | 1841 | emit_tag2member_map(arg_t *arg, tag2el_t *tag2el, int tag2el_count, const char *opt_modifier) { |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1842 | asn1p_expr_t *expr = arg->expr; |
Lev Walkin | f3b29b1 | 2005-06-02 05:21:37 +0000 | [diff] [blame] | 1843 | int i; |
| 1844 | |
| 1845 | if(!tag2el_count) return 0; /* No top level tags */ |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1846 | |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 1847 | OUT("static const asn_TYPE_tag2member_t asn_MAP_%s_tag2el%s_%d[] = {\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1848 | MKID(expr), opt_modifier?opt_modifier:"", |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1849 | expr->_type_unique_index); |
Lev Walkin | f3b29b1 | 2005-06-02 05:21:37 +0000 | [diff] [blame] | 1850 | for(i = 0; i < tag2el_count; i++) { |
| 1851 | OUT(" { "); |
| 1852 | _print_tag(arg, &tag2el[i].el_tag); |
| 1853 | OUT(", "); |
| 1854 | OUT("%d, ", tag2el[i].el_no); |
| 1855 | OUT("%d, ", tag2el[i].toff_first); |
| 1856 | OUT("%d ", tag2el[i].toff_last); |
Lev Walkin | 4062b01 | 2013-10-11 14:29:38 -0700 | [diff] [blame] | 1857 | OUT("}%s /* %s", |
Lev Walkin | f3b29b1 | 2005-06-02 05:21:37 +0000 | [diff] [blame] | 1858 | (i + 1 < tag2el_count) ? "," : "", |
Lev Walkin | 4062b01 | 2013-10-11 14:29:38 -0700 | [diff] [blame] | 1859 | tag2el[i].from_expr->Identifier); |
| 1860 | if(arg->flags & A1C_LINE_REFS) |
| 1861 | OUT("at %d", tag2el[i].from_expr->_lineno); |
| 1862 | OUT(" */\n"); |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1863 | } |
| 1864 | OUT("};\n"); |
| 1865 | |
Lev Walkin | f3b29b1 | 2005-06-02 05:21:37 +0000 | [diff] [blame] | 1866 | return 0; |
Lev Walkin | fd171ef | 2004-06-06 07:20:17 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1869 | static enum tvm_compat |
| 1870 | emit_tags_vectors(arg_t *arg, asn1p_expr_t *expr, int *tags_count_r, int *all_tags_count_r) { |
| 1871 | struct asn1p_type_tag_s *tags = 0; /* Effective tags */ |
| 1872 | struct asn1p_type_tag_s *all_tags = 0; /* The full array */ |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1873 | int tags_count = 0; |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1874 | int all_tags_count = 0; |
| 1875 | enum tvm_compat tv_mode = _TVM_SAME; |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1876 | int i; |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1877 | |
Lev Walkin | 6d1220a | 2004-09-14 14:10:10 +0000 | [diff] [blame] | 1878 | /* Cleanup before proceeding. */ |
| 1879 | *tags_count_r = 0; |
| 1880 | *all_tags_count_r = 0; |
| 1881 | |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1882 | /* Fetch a chain of tags */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1883 | tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, &tags, 0); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 1884 | if(tags_count < 0) { |
| 1885 | DEBUG("fail to fetch tags for %s", expr->Identifier); |
Lev Walkin | 6d1220a | 2004-09-14 14:10:10 +0000 | [diff] [blame] | 1886 | return -1; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 1887 | } |
Lev Walkin | 6439972 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 1888 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1889 | /* Fetch a chain of tags */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1890 | all_tags_count = asn1f_fetch_tags(arg->asn, expr->module, expr, |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1891 | &all_tags, AFT_FULL_COLLECT); |
| 1892 | if(all_tags_count < 0) { |
Lev Walkin | d8b8364 | 2016-03-14 02:00:27 -0700 | [diff] [blame] | 1893 | free(tags); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 1894 | DEBUG("fail to fetch tags chain for %s", expr->Identifier); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1895 | return -1; |
Lev Walkin | 5a8219a | 2004-09-08 00:28:57 +0000 | [diff] [blame] | 1896 | } |
Lev Walkin | 906654e | 2004-09-10 15:49:15 +0000 | [diff] [blame] | 1897 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1898 | assert(tags_count <= all_tags_count); |
| 1899 | assert((tags_count?0:1) == (all_tags_count?0:1)); |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1900 | |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1901 | if(tags_count <= all_tags_count) { |
| 1902 | for(i = 0; i < tags_count; i++) { |
| 1903 | if(tags[i].tag_value != all_tags[i].tag_value |
| 1904 | || tags[i].tag_class != all_tags[i].tag_class) { |
| 1905 | tv_mode = _TVM_DIFFERENT; |
| 1906 | break; |
| 1907 | } |
| 1908 | } |
| 1909 | if(i == tags_count && tags_count < all_tags_count) |
| 1910 | tv_mode = _TVM_SUBSET; |
| 1911 | } else { |
| 1912 | tv_mode = _TVM_DIFFERENT; |
| 1913 | } |
| 1914 | |
| 1915 | #define EMIT_TAGS_TABLE(name, tags, tags_count) do { \ |
Wim Lewis | fb6344e | 2014-07-28 12:16:01 -0700 | [diff] [blame] | 1916 | OUT("static const ber_tlv_tag_t asn_DEF_%s%s_tags_%d[] = {\n",\ |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 1917 | MKID(expr), name, \ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1918 | expr->_type_unique_index); \ |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1919 | INDENT(+1); \ |
| 1920 | /* Print the array of collected tags */ \ |
| 1921 | for(i = 0; i < tags_count; i++) { \ |
| 1922 | if(i) OUT(",\n"); \ |
| 1923 | _print_tag(arg, &tags[i]); \ |
| 1924 | } \ |
| 1925 | OUT("\n"); \ |
| 1926 | INDENT(-1); \ |
| 1927 | OUT("};\n"); \ |
| 1928 | } while(0) |
| 1929 | |
| 1930 | if(tags_count) { |
| 1931 | if(tv_mode == _TVM_SUBSET) |
| 1932 | EMIT_TAGS_TABLE("", all_tags, all_tags_count); |
| 1933 | else |
| 1934 | EMIT_TAGS_TABLE("", tags, tags_count); |
| 1935 | } |
| 1936 | |
| 1937 | if(all_tags_count) { |
| 1938 | if(tv_mode == _TVM_DIFFERENT) |
| 1939 | EMIT_TAGS_TABLE("_all", all_tags, all_tags_count); |
| 1940 | } |
| 1941 | |
Lev Walkin | d8b8364 | 2016-03-14 02:00:27 -0700 | [diff] [blame] | 1942 | free(tags); |
| 1943 | free(all_tags); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 1944 | |
| 1945 | *tags_count_r = tags_count; |
| 1946 | *all_tags_count_r = all_tags_count; |
| 1947 | |
| 1948 | return tv_mode; |
Lev Walkin | 27ea380 | 2004-06-28 21:13:46 +0000 | [diff] [blame] | 1949 | } |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1950 | |
| 1951 | static int |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 1952 | expr_elements_count(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1953 | asn1p_expr_t *topmost_parent; |
| 1954 | asn1p_expr_t *v; |
| 1955 | int elements = 0; |
| 1956 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 1957 | topmost_parent = asn1f_find_terminal_type_ex(arg->asn, expr); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1958 | if(!topmost_parent) return 0; |
| 1959 | |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 1960 | if(!(topmost_parent->expr_type & ASN_CONSTR_MASK) |
Lev Walkin | 1b3a135 | 2016-01-10 13:15:02 -0800 | [diff] [blame] | 1961 | && !(topmost_parent->expr_type == ASN_BASIC_INTEGER) |
Lev Walkin | 0f26245 | 2016-01-10 13:33:08 -0800 | [diff] [blame] | 1962 | && !(topmost_parent->expr_type == ASN_BASIC_ENUMERATED) |
| 1963 | && !(topmost_parent->expr_type == ASN_BASIC_BIT_STRING)) |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 1964 | return 0; |
| 1965 | |
| 1966 | TQ_FOR(v, &(topmost_parent->members), next) { |
| 1967 | if(v->expr_type != A1TC_EXTENSIBLE) |
| 1968 | elements++; |
| 1969 | } |
| 1970 | |
| 1971 | return elements; |
| 1972 | } |
| 1973 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 1974 | static asn1p_expr_type_e |
| 1975 | expr_get_type(arg_t *arg, asn1p_expr_t *expr) { |
| 1976 | asn1p_expr_t *terminal; |
| 1977 | terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 1978 | if(terminal) return terminal->expr_type; |
| 1979 | return A1TC_INVALID; |
| 1980 | } |
| 1981 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 1982 | static asn1c_integer_t |
| 1983 | PER_FROM_alphabet_characters(asn1cnst_range_t *range) { |
| 1984 | asn1c_integer_t numchars = 0; |
| 1985 | if(range->el_count) { |
| 1986 | int i; |
| 1987 | for(i = 0; i < range->el_count; i++) |
| 1988 | numchars |
| 1989 | += PER_FROM_alphabet_characters(range->elements[i]); |
| 1990 | } else { |
| 1991 | assert(range->left.type == ARE_VALUE); |
| 1992 | assert(range->right.type == ARE_VALUE); |
| 1993 | numchars = 1 + (range->right.value - range->left.value); |
| 1994 | } |
| 1995 | return numchars; |
| 1996 | } |
| 1997 | |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 1998 | static void |
| 1999 | emit_single_member_OER_constraint_comment(arg_t *arg, asn1cnst_range_t *range, char *type) { |
| 2000 | |
| 2001 | /* |
| 2002 | * Print some courtesy debug information. |
| 2003 | */ |
| 2004 | if(range |
| 2005 | && (range->left.type == ARE_VALUE || range->right.type == ARE_VALUE)) { |
| 2006 | OUT("\t/* "); |
| 2007 | if(type) OUT("(%s", type); |
| 2008 | OUT("("); |
| 2009 | if(range->left.type == ARE_VALUE) |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2010 | OUT("%s", asn1p_itoa(range->left.value)); |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2011 | else |
| 2012 | OUT("MIN"); |
| 2013 | OUT(".."); |
| 2014 | if(range->right.type == ARE_VALUE) |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2015 | OUT("%s", asn1p_itoa(range->right.value)); |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2016 | else |
| 2017 | OUT("MAX"); |
| 2018 | if(range->extensible) OUT(",..."); |
| 2019 | if(type) OUT(")"); |
| 2020 | OUT(") */"); |
| 2021 | } |
| 2022 | } |
| 2023 | |
| 2024 | static int |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 2025 | emit_single_member_OER_constraint_value(arg_t *arg, asn1cnst_range_t *range) { |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2026 | if(!range) { |
| 2027 | /* oer_support.h: asn_oer_constraint_s */ |
| 2028 | OUT("{ 0, 0 }"); |
| 2029 | return 0; |
| 2030 | } |
| 2031 | |
| 2032 | if(range->incompatible || range->not_OER_visible) { |
| 2033 | OUT("{ 0, 0 }"); |
| 2034 | } else if(range->left.type == ARE_VALUE && |
| 2035 | range->right.type == ARE_VALUE) { |
| 2036 | asn1c_integer_t lb = range->left.value; |
| 2037 | asn1c_integer_t ub = range->right.value; |
| 2038 | unsigned width = 0; |
| 2039 | unsigned positive = 0; |
| 2040 | |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 2041 | |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2042 | if(lb >= 0) { |
| 2043 | /* X.969 08/2015 10.2(a) */ |
| 2044 | if(ub <= 255) { |
| 2045 | width = 1; |
| 2046 | } else if(ub <= 65535) { |
| 2047 | width = 2; |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 2048 | } else if((unsigned long long)ub <= 4294967295UL) { |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2049 | width = 4; |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 2050 | } else if((unsigned long long)ub <= 18446744073709551615ULL) { |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2051 | width = 8; |
| 2052 | } |
| 2053 | positive = 1; |
| 2054 | } else { |
| 2055 | positive = 0; |
| 2056 | /* X.969 08/2015 10.2(b) - no lower bound or negative lower bound */ |
| 2057 | if(lb >= -128 && ub <= 127) { |
| 2058 | width = 1; |
| 2059 | } else if(lb >= -32768 && ub <= 32767) { |
| 2060 | width = 2; |
| 2061 | } else if(lb >= -2147483648L && ub <= 2147483647L) { |
| 2062 | width = 4; |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 2063 | } else if(lb >= (-9223372036854775807LL-1) |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2064 | && ub <= 9223372036854775807LL) { |
| 2065 | width = 8; |
| 2066 | } |
| 2067 | } |
| 2068 | OUT("{ %u, %u }", width, positive); |
| 2069 | } else { |
| 2070 | OUT("{ 0, 0 }"); |
| 2071 | } |
| 2072 | |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
| 2076 | static int |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 2077 | emit_single_member_OER_constraint_size(arg_t *arg, asn1cnst_range_t *range) { |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2078 | if(!range) { |
| 2079 | /* oer_support.h: asn_oer_constraint_s */ |
| 2080 | OUT("-1"); |
| 2081 | return 0; |
| 2082 | } |
| 2083 | |
| 2084 | if(range->incompatible || range->not_OER_visible) { |
| 2085 | OUT("-1"); |
| 2086 | } else { |
| 2087 | if(range->left.type == ARE_VALUE && range->right.type == ARE_VALUE |
| 2088 | && range->left.value == range->right.value |
| 2089 | && range->left.value >= 0) { |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2090 | OUT("%s", asn1p_itoa(range->left.value)); |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2091 | } else { |
| 2092 | OUT("-1"); |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | return 0; |
| 2097 | } |
| 2098 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2099 | static int |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 2100 | emit_single_member_PER_constraint(arg_t *arg, asn1cnst_range_t *range, int alphabetsize, const char *type) { |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2101 | if(!range || range->incompatible || range->not_PER_visible) { |
| 2102 | OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2103 | return 0; |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2104 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2105 | |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2106 | if(range->left.type == ARE_VALUE) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2107 | if(range->right.type == ARE_VALUE) { |
| 2108 | asn1c_integer_t cover = 1; |
| 2109 | asn1c_integer_t r = 1 + range->right.value |
| 2110 | - range->left.value; |
Lev Walkin | c46b7cb | 2006-08-18 02:27:55 +0000 | [diff] [blame] | 2111 | size_t rbits; /* Value range bits */ |
| 2112 | ssize_t ebits; /* Value effective range bits */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2113 | |
| 2114 | if(range->empty_constraint) |
| 2115 | r = 0; |
| 2116 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2117 | if(alphabetsize) { |
| 2118 | /* X.691: 27.5.2 */ |
| 2119 | r = PER_FROM_alphabet_characters(range); |
| 2120 | } |
| 2121 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2122 | /* Compute real constraint */ |
| 2123 | for(rbits = 0; rbits < (8 * sizeof(r)); rbits++) { |
| 2124 | if(r <= cover) |
| 2125 | break; |
| 2126 | cover *= 2; /* Can't do shifting */ |
| 2127 | if(cover < 0) { |
| 2128 | FATAL("Constraint at line %d too wide " |
| 2129 | "for %d-bits integer type", |
| 2130 | arg->expr->_lineno, |
| 2131 | sizeof(r) * 8); |
| 2132 | rbits = sizeof(r); |
| 2133 | break; |
| 2134 | } |
| 2135 | } |
| 2136 | |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 2137 | if(alphabetsize) { |
| 2138 | ebits = rbits; |
| 2139 | } else { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2140 | /* X.691, #10.9.4.1 */ |
| 2141 | for(ebits = 0; ebits <= 16; ebits++) |
| 2142 | if(r <= 1 << ebits) break; |
| 2143 | if(ebits == 17 |
| 2144 | || range->right.value >= 65536) |
| 2145 | ebits = -1; |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 2146 | if(0) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2147 | /* X.691, #10.5.7.1 */ |
| 2148 | for(ebits = 0; ebits <= 8; ebits++) |
| 2149 | if(r <= 1 << ebits) break; |
| 2150 | if(ebits == 9) { |
| 2151 | if(r <= 65536) |
| 2152 | ebits = 16; |
| 2153 | else |
| 2154 | ebits = -1; |
| 2155 | } |
| 2156 | } |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 2157 | } |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2158 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2159 | OUT("{ APC_CONSTRAINED%s,%s% d, % d, ", |
| 2160 | range->extensible |
| 2161 | ? " | APC_EXTENSIBLE" : "", |
| 2162 | range->extensible ? " " : "\t", rbits, ebits); |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 2163 | |
| 2164 | if(alphabetsize) { |
| 2165 | asn1c_integer_t lv = range->left.value; |
| 2166 | asn1c_integer_t rv = range->right.value; |
| 2167 | int gcmt = 0; |
| 2168 | if(lv > 0x7fffffff) { lv = 0x7fffffff; gcmt++; } |
| 2169 | if(rv > 0x7fffffff) { rv = 0x7fffffff; gcmt++; } |
| 2170 | if(gcmt) { |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 2171 | OINTS(lv); OUT(", "); OINTS(rv); OUT(" }"); |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 2172 | goto pcmt; |
| 2173 | } |
| 2174 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2175 | } else { |
| 2176 | if(range->extensible) { |
| 2177 | OUT("{ APC_SEMI_CONSTRAINED | APC_EXTENSIBLE, " |
| 2178 | "-1, "); |
| 2179 | } else { |
| 2180 | OUT("{ APC_SEMI_CONSTRAINED,\t-1, -1, "); |
| 2181 | } |
| 2182 | } |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 2183 | OINTS(range->left.value); OUT(", "); |
| 2184 | OINTS(range->right.value); OUT(" }"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2185 | } else { |
| 2186 | OUT("{ APC_UNCONSTRAINED,\t-1, -1, 0, 0 }"); |
| 2187 | } |
| 2188 | |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 2189 | pcmt: |
| 2190 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2191 | /* |
| 2192 | * Print some courtesy debug information. |
| 2193 | */ |
| 2194 | if(range->left.type == ARE_VALUE |
| 2195 | || range->right.type == ARE_VALUE) { |
| 2196 | OUT("\t/* "); |
| 2197 | if(type) OUT("(%s", type); |
| 2198 | OUT("("); |
| 2199 | if(range->left.type == ARE_VALUE) |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2200 | OUT("%s", asn1p_itoa(range->left.value)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2201 | else |
| 2202 | OUT("MIN"); |
| 2203 | OUT(".."); |
| 2204 | if(range->right.type == ARE_VALUE) |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2205 | OUT("%s", asn1p_itoa(range->right.value)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2206 | else |
| 2207 | OUT("MAX"); |
| 2208 | if(range->extensible) OUT(",..."); |
| 2209 | if(type) OUT(")"); |
| 2210 | OUT(") */"); |
| 2211 | } |
| 2212 | |
| 2213 | return 0; |
| 2214 | } |
| 2215 | |
| 2216 | static int |
Lev Walkin | 98eabc1 | 2017-07-19 08:51:11 +0400 | [diff] [blame] | 2217 | emit_member_OER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx) { |
| 2218 | int save_target = arg->target->target; |
| 2219 | asn1cnst_range_t *range; |
| 2220 | asn1p_expr_type_e etype; |
| 2221 | |
| 2222 | etype = expr_get_type(arg, expr); |
| 2223 | |
| 2224 | if((arg->flags & A1C_GEN_OER) |
| 2225 | && (expr->constraints || etype == ASN_BASIC_ENUMERATED |
| 2226 | || etype == ASN_CONSTR_CHOICE)) { |
| 2227 | /* Fall through */ |
| 2228 | } else { |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
| 2232 | REDIR(OT_CTDEFS); |
| 2233 | |
| 2234 | OUT("static asn_oer_constraints_t " |
| 2235 | "asn_OER_%s_%s_constr_%d GCC_NOTUSED = {\n", |
| 2236 | pfx, MKID(expr), expr->_type_unique_index); |
| 2237 | |
| 2238 | INDENT(+1); |
| 2239 | |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2240 | /* .value{.width,.positive} */ |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 2241 | range = asn1constraint_compute_OER_range(expr->Identifier, etype, |
| 2242 | expr->combined_constraints, |
Lev Walkin | 98eabc1 | 2017-07-19 08:51:11 +0400 | [diff] [blame] | 2243 | ACT_EL_RANGE, 0, 0, 0); |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 2244 | if(emit_single_member_OER_constraint_value(arg, range)) { |
Lev Walkin | 98eabc1 | 2017-07-19 08:51:11 +0400 | [diff] [blame] | 2245 | return -1; |
| 2246 | } |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2247 | emit_single_member_OER_constraint_comment(arg, range, 0); |
Lev Walkin | 98eabc1 | 2017-07-19 08:51:11 +0400 | [diff] [blame] | 2248 | asn1constraint_range_free(range); |
| 2249 | |
| 2250 | OUT(",\n"); |
| 2251 | |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2252 | /* .size */ |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 2253 | range = asn1constraint_compute_OER_range(expr->Identifier, etype, |
| 2254 | expr->combined_constraints, |
Lev Walkin | 98eabc1 | 2017-07-19 08:51:11 +0400 | [diff] [blame] | 2255 | ACT_CT_SIZE, 0, 0, 0); |
Lev Walkin | b9e98cb | 2017-08-02 12:53:44 -0700 | [diff] [blame] | 2256 | if(emit_single_member_OER_constraint_size(arg, range)) { |
Lev Walkin | 98eabc1 | 2017-07-19 08:51:11 +0400 | [diff] [blame] | 2257 | return -1; |
| 2258 | } |
Lev Walkin | 4118ccf | 2017-08-02 10:37:31 -0700 | [diff] [blame] | 2259 | emit_single_member_OER_constraint_comment(arg, range, "SIZE"); |
Lev Walkin | 98eabc1 | 2017-07-19 08:51:11 +0400 | [diff] [blame] | 2260 | asn1constraint_range_free(range); |
| 2261 | |
| 2262 | INDENT(-1); |
| 2263 | |
| 2264 | OUT("};\n"); |
| 2265 | |
| 2266 | REDIR(save_target); |
| 2267 | |
| 2268 | return 0; |
| 2269 | } |
| 2270 | |
| 2271 | static int |
Lev Walkin | 4b5dae3 | 2006-10-09 12:27:44 +0000 | [diff] [blame] | 2272 | emit_member_PER_constraints(arg_t *arg, asn1p_expr_t *expr, const char *pfx) { |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2273 | int save_target = arg->target->target; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2274 | asn1cnst_range_t *range; |
| 2275 | asn1p_expr_type_e etype; |
| 2276 | |
Lev Walkin | 07aaa34 | 2006-10-09 12:52:15 +0000 | [diff] [blame] | 2277 | etype = expr_get_type(arg, expr); |
| 2278 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2279 | if((arg->flags & A1C_GEN_PER) |
| 2280 | && (expr->constraints |
Lev Walkin | 07aaa34 | 2006-10-09 12:52:15 +0000 | [diff] [blame] | 2281 | || etype == ASN_BASIC_ENUMERATED |
| 2282 | || etype == ASN_CONSTR_CHOICE) |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2283 | ) { |
| 2284 | /* Fall through */ |
| 2285 | } else { |
| 2286 | return 0; |
| 2287 | } |
| 2288 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2289 | if(expr->_type_referenced) { |
| 2290 | REDIR(OT_FUNC_DECLS); |
| 2291 | |
| 2292 | OUT("extern asn_per_constraints_t " |
| 2293 | "asn_PER_%s_%s_constr_%d;\n", |
| 2294 | pfx, MKID(expr), expr->_type_unique_index); |
| 2295 | } |
| 2296 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2297 | REDIR(OT_CTDEFS); |
| 2298 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2299 | if(!(expr->_type_referenced)) OUT("static "); |
| 2300 | OUT("asn_per_constraints_t " |
Lev Walkin | 1715b32 | 2013-03-28 04:02:13 -0700 | [diff] [blame] | 2301 | "asn_PER_%s_%s_constr_%d GCC_NOTUSED = {\n", |
Lev Walkin | 4b5dae3 | 2006-10-09 12:27:44 +0000 | [diff] [blame] | 2302 | pfx, MKID(expr), expr->_type_unique_index); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2303 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2304 | INDENT(+1); |
| 2305 | |
| 2306 | /* |
| 2307 | * ENUMERATED and CHOICE are special. |
| 2308 | */ |
| 2309 | if(etype == ASN_BASIC_ENUMERATED |
| 2310 | || etype == ASN_CONSTR_CHOICE) { |
| 2311 | asn1cnst_range_t tmprng; |
| 2312 | asn1p_expr_t *v; |
| 2313 | int extensible = 0; |
| 2314 | int eidx = -1; |
| 2315 | |
| 2316 | expr = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 2317 | assert(expr); |
| 2318 | |
| 2319 | TQ_FOR(v, &(expr->members), next) { |
| 2320 | if(v->expr_type == A1TC_EXTENSIBLE) { |
| 2321 | extensible++; |
| 2322 | break; |
| 2323 | } |
| 2324 | eidx++; |
| 2325 | } |
| 2326 | |
| 2327 | memset(&tmprng, 0, sizeof (tmprng)); |
| 2328 | tmprng.extensible = extensible; |
| 2329 | if(eidx < 0) tmprng.empty_constraint = 1; |
| 2330 | tmprng.left.type = ARE_VALUE; |
| 2331 | tmprng.left.value = 0; |
| 2332 | tmprng.right.type = ARE_VALUE; |
| 2333 | tmprng.right.value = eidx < 0 ? 0 : eidx; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2334 | if(emit_single_member_PER_constraint(arg, &tmprng, 0, 0)) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2335 | return -1; |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 2336 | } else if(etype & ASN_STRING_KM_MASK) { |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 2337 | range = asn1constraint_compute_PER_range(expr->Identifier, etype, |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 2338 | expr->combined_constraints, ACT_CT_FROM, |
| 2339 | 0, 0, 0); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2340 | DEBUG("Emitting FROM constraint for %s", expr->Identifier); |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 2341 | |
| 2342 | if((range->left.type == ARE_MIN && range->right.type == ARE_MAX) |
| 2343 | || range->not_PER_visible) { |
| 2344 | switch(etype) { |
| 2345 | case ASN_STRING_BMPString: |
| 2346 | range->left.type = ARE_VALUE; |
| 2347 | range->left.value = 0; |
| 2348 | range->right.type = ARE_VALUE; |
| 2349 | range->right.value = 65535; |
| 2350 | range->not_PER_visible = 0; |
| 2351 | range->extensible = 0; |
| 2352 | break; |
| 2353 | case ASN_STRING_UniversalString: |
| 2354 | OUT("{ APC_CONSTRAINED,\t32, 32," |
| 2355 | " 0, 2147483647 }" |
| 2356 | " /* special case 1 */\n"); |
| 2357 | goto avoid; |
| 2358 | default: |
| 2359 | break; |
| 2360 | } |
| 2361 | } |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2362 | if(emit_single_member_PER_constraint(arg, range, 1, 0)) |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 2363 | return -1; |
Lev Walkin | 3a4689a | 2006-11-24 11:20:27 +0000 | [diff] [blame] | 2364 | avoid: |
Lev Walkin | 729eb86 | 2006-09-21 01:50:13 +0000 | [diff] [blame] | 2365 | asn1constraint_range_free(range); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2366 | } else { |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 2367 | range = asn1constraint_compute_PER_range(expr->Identifier, etype, |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2368 | expr->combined_constraints, ACT_EL_RANGE, |
| 2369 | 0, 0, 0); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2370 | if(emit_single_member_PER_constraint(arg, range, 0, 0)) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2371 | return -1; |
| 2372 | asn1constraint_range_free(range); |
| 2373 | } |
| 2374 | OUT(",\n"); |
| 2375 | |
Lev Walkin | a28cbb9 | 2017-07-31 20:20:17 -0700 | [diff] [blame] | 2376 | range = asn1constraint_compute_PER_range(expr->Identifier, etype, |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2377 | expr->combined_constraints, ACT_CT_SIZE, 0, 0, 0); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2378 | if(emit_single_member_PER_constraint(arg, range, 0, "SIZE")) |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2379 | return -1; |
| 2380 | asn1constraint_range_free(range); |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2381 | OUT(",\n"); |
| 2382 | |
| 2383 | if((etype & ASN_STRING_KM_MASK) && (expr->_mark & TM_PERFROMCT)) { |
| 2384 | int old_target = arg->target->target; |
| 2385 | REDIR(OT_CODE); |
| 2386 | |
| 2387 | OUT("static int asn_PER_MAP_%s_%d_v2c(unsigned int value) {\n", |
| 2388 | MKID(expr), expr->_type_unique_index); |
| 2389 | OUT("\tif(value >= sizeof(permitted_alphabet_table_%d)/" |
| 2390 | "sizeof(permitted_alphabet_table_%d[0]))\n", |
| 2391 | expr->_type_unique_index, |
| 2392 | expr->_type_unique_index); |
| 2393 | OUT("\t\treturn -1;\n"); |
| 2394 | OUT("\treturn permitted_alphabet_table_%d[value] - 1;\n", |
| 2395 | expr->_type_unique_index); |
| 2396 | OUT("}\n"); |
| 2397 | |
| 2398 | OUT("static int asn_PER_MAP_%s_%d_c2v(unsigned int code) {\n", |
| 2399 | MKID(expr), expr->_type_unique_index); |
| 2400 | OUT("\tif(code >= sizeof(permitted_alphabet_code2value_%d)/" |
| 2401 | "sizeof(permitted_alphabet_code2value_%d[0]))\n", |
| 2402 | expr->_type_unique_index, |
| 2403 | expr->_type_unique_index); |
| 2404 | OUT("\t\treturn -1;\n"); |
| 2405 | OUT("\treturn permitted_alphabet_code2value_%d[code];\n", |
| 2406 | expr->_type_unique_index); |
| 2407 | OUT("}\n"); |
| 2408 | |
| 2409 | REDIR(old_target); |
| 2410 | |
| 2411 | OUT("asn_PER_MAP_%s_%d_v2c,\t/* Value to PER code map */\n", |
| 2412 | MKID(expr), expr->_type_unique_index); |
| 2413 | OUT("asn_PER_MAP_%s_%d_c2v\t/* PER code to value map */\n", |
| 2414 | MKID(expr), expr->_type_unique_index); |
| 2415 | } else if(etype & ASN_STRING_KM_MASK) { |
| 2416 | DEBUG("No PER value map necessary for %s", MKID(expr)); |
| 2417 | OUT("0, 0\t/* No PER character map necessary */\n"); |
| 2418 | } else { |
| 2419 | OUT("0, 0\t/* No PER value map */\n"); |
| 2420 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2421 | |
| 2422 | INDENT(-1); |
| 2423 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2424 | OUT("};\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2425 | |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2426 | REDIR(save_target); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2427 | |
| 2428 | return 0; |
| 2429 | } |
| 2430 | |
| 2431 | static int |
Lev Walkin | 8032f7a | 2007-06-27 01:54:57 +0000 | [diff] [blame] | 2432 | safe_string(const uint8_t *buf, int size) { |
| 2433 | const uint8_t *end = buf + size; |
| 2434 | for(; buf < end; buf++) { |
| 2435 | int ch = *buf; |
| 2436 | if((ch < 0x20 || ch > 0x7e) || ch == '"') |
| 2437 | return 0; |
| 2438 | } |
| 2439 | return 1; |
| 2440 | } |
| 2441 | |
| 2442 | static void |
| 2443 | emit_default_value(arg_t *arg, asn1p_value_t *v) { |
| 2444 | |
| 2445 | OUT("static uint8_t defv[] = "); |
| 2446 | assert(v->type == ATV_STRING); |
| 2447 | |
| 2448 | if(safe_string(v->value.string.buf, v->value.string.size)) { |
| 2449 | OUT("\"%s\";\n", v->value.string.buf); |
| 2450 | } else { |
| 2451 | uint8_t *b = v->value.string.buf; |
| 2452 | uint8_t *e = v->value.string.size + b; |
| 2453 | OUT("{ "); |
| 2454 | for(;b < e; b++) |
| 2455 | OUT("0x%02x, ", *b); |
| 2456 | OUT("0 };\n"); |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | static int |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2461 | try_inline_default(arg_t *arg, asn1p_expr_t *expr, int out) { |
| 2462 | int save_target = arg->target->target; |
| 2463 | asn1p_expr_type_e etype = expr_get_type(arg, expr); |
| 2464 | int fits_long = 0; |
| 2465 | |
| 2466 | switch(etype) { |
| 2467 | case ASN_BASIC_BOOLEAN: |
| 2468 | fits_long = 1; |
| 2469 | case ASN_BASIC_INTEGER: |
| 2470 | case ASN_BASIC_ENUMERATED: |
| 2471 | if(expr->marker.default_value == NULL |
| 2472 | || expr->marker.default_value->type != ATV_INTEGER) |
| 2473 | break; |
| 2474 | if(!fits_long) |
| 2475 | fits_long = asn1c_type_fits_long(arg, expr)!=FL_NOTFIT; |
| 2476 | if(fits_long && !expr->marker.default_value->value.v_integer) |
| 2477 | expr->marker.flags &= ~EM_INDIRECT; |
Lev Walkin | 8032f7a | 2007-06-27 01:54:57 +0000 | [diff] [blame] | 2478 | if(!out) { |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2479 | OUT("asn_DFL_%d_set_%s,", |
Lev Walkin | 8032f7a | 2007-06-27 01:54:57 +0000 | [diff] [blame] | 2480 | expr->_type_unique_index, |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2481 | asn1p_itoa(expr->marker.default_value->value.v_integer)); |
| 2482 | OUT("\t/* DEFAULT %s */\n", |
| 2483 | asn1p_itoa(expr->marker.default_value->value.v_integer)); |
Lev Walkin | 8032f7a | 2007-06-27 01:54:57 +0000 | [diff] [blame] | 2484 | return 1; |
| 2485 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2486 | REDIR(OT_STAT_DEFS); |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2487 | OUT("static int asn_DFL_%d_set_%s(int set_value, void **sptr) {\n", |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2488 | expr->_type_unique_index, |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2489 | asn1p_itoa(expr->marker.default_value->value.v_integer)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2490 | INDENT(+1); |
| 2491 | OUT("%s *st = *sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE)); |
| 2492 | OUT("\n"); |
| 2493 | OUT("if(!st) {\n"); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 2494 | OUT("\tif(!set_value) return -1;\t/* Not a default value */\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2495 | OUT("\tst = (*sptr = CALLOC(1, sizeof(*st)));\n"); |
| 2496 | OUT("\tif(!st) return -1;\n"); |
| 2497 | OUT("}\n"); |
| 2498 | OUT("\n"); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 2499 | OUT("if(set_value) {\n"); |
| 2500 | INDENT(+1); |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2501 | OUT("/* Install default value %s */\n", |
| 2502 | asn1p_itoa(expr->marker.default_value->value.v_integer)); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2503 | if(fits_long) { |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 2504 | OUT("*st = "); |
| 2505 | OINT(expr->marker.default_value->value.v_integer); |
| 2506 | OUT(";\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2507 | OUT("return 0;\n"); |
| 2508 | } else { |
Lev Walkin | 63b4126 | 2007-11-06 01:48:46 +0000 | [diff] [blame] | 2509 | OUT("return asn_long2INTEGER(st, "); |
| 2510 | OINT(expr->marker.default_value->value.v_integer); |
| 2511 | OUT(");\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2512 | } |
| 2513 | INDENT(-1); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 2514 | OUT("} else {\n"); |
| 2515 | INDENT(+1); |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2516 | OUT("/* Test default value %s */\n", |
| 2517 | asn1p_itoa(expr->marker.default_value->value.v_integer)); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 2518 | if(fits_long) { |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2519 | OUT("return (*st == %s);\n", |
| 2520 | asn1p_itoa(expr->marker.default_value->value.v_integer)); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 2521 | } else { |
| 2522 | OUT("long value;\n"); |
| 2523 | OUT("if(asn_INTEGER2long(st, &value))\n"); |
| 2524 | OUT("\treturn -1;\n"); |
Lev Walkin | da997b1 | 2017-08-04 01:38:41 -0700 | [diff] [blame] | 2525 | OUT("return (value == %s);\n", |
| 2526 | asn1p_itoa(expr->marker.default_value->value.v_integer)); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 2527 | } |
| 2528 | INDENT(-1); |
| 2529 | OUT("}\n"); |
| 2530 | INDENT(-1); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2531 | OUT("}\n"); |
| 2532 | REDIR(save_target); |
| 2533 | return 1; |
| 2534 | case ASN_BASIC_NULL: |
| 2535 | //expr->marker.flags &= ~EM_INDIRECT; |
| 2536 | return 0; |
| 2537 | default: |
Lev Walkin | 8032f7a | 2007-06-27 01:54:57 +0000 | [diff] [blame] | 2538 | if(etype & ASN_STRING_KM_MASK) { |
| 2539 | if(expr->marker.default_value == NULL |
| 2540 | || expr->marker.default_value->type != ATV_STRING) |
| 2541 | break; |
| 2542 | if(!out) { |
| 2543 | OUT("asn_DFL_%d_set,\t/* DEFAULT \"%s\" */\n", |
| 2544 | expr->_type_unique_index, |
| 2545 | expr->marker.default_value->value.string.buf); |
| 2546 | return 1; |
| 2547 | } |
| 2548 | REDIR(OT_STAT_DEFS); |
| 2549 | OUT("static int asn_DFL_%d_set(int set_value, void **sptr) {\n", expr->_type_unique_index); |
| 2550 | INDENT(+1); |
| 2551 | emit_default_value(arg, expr->marker.default_value); |
| 2552 | OUT("%s *st = *sptr;\n", asn1c_type_name(arg, expr, TNF_CTYPE)); |
| 2553 | OUT("\n"); |
| 2554 | OUT("if(!st) {\n"); |
| 2555 | OUT("\tif(!set_value) return -1;\t/* Not a default value */\n"); |
| 2556 | OUT("\tst = (*sptr = CALLOC(1, sizeof(*st)));\n"); |
| 2557 | OUT("\tif(!st) return -1;\n"); |
| 2558 | OUT("}\n"); |
| 2559 | OUT("\n"); |
| 2560 | OUT("if(set_value) {\n"); |
| 2561 | INDENT(+1); |
| 2562 | OUT("uint8_t *ptr = MALLOC(sizeof(defv));\n"); |
| 2563 | OUT("if(!ptr) return -1;\n"); |
| 2564 | OUT("memcpy(ptr, &defv, sizeof(defv));\n"); |
| 2565 | OUT("FREEMEM(st->buf);\n"); |
| 2566 | OUT("st->buf = ptr;\n"); |
| 2567 | OUT("st->size = sizeof(defv) - 1;\n"); |
| 2568 | OUT("return 0;\n"); |
| 2569 | INDENT(-1); |
| 2570 | OUT("} else {\n"); |
| 2571 | INDENT(+1); |
| 2572 | OUT("if(st->size != (sizeof(defv) - 1)\n"); |
| 2573 | OUT("|| memcmp(st->buf, &defv, sizeof(defv) - 1))\n"); |
| 2574 | OUT("\treturn 0;\n"); |
| 2575 | OUT("return 1;\n"); |
| 2576 | INDENT(-1); |
| 2577 | OUT("}\n"); OUT("\n"); |
| 2578 | INDENT(-1); |
| 2579 | OUT("}\n"); |
| 2580 | REDIR(save_target); |
| 2581 | return 1; |
| 2582 | } |
| 2583 | break; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2584 | } |
| 2585 | return 0; |
| 2586 | } |
| 2587 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2588 | static int |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2589 | emit_member_table(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2590 | int save_target; |
| 2591 | arg_t tmp_arg; |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2592 | struct asn1p_type_tag_s outmost_tag_s; |
| 2593 | struct asn1p_type_tag_s *outmost_tag; |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 2594 | int complex_contents; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2595 | char *p; |
| 2596 | |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2597 | if(asn1f_fetch_outmost_tag(arg->asn, |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2598 | expr->module, expr, &outmost_tag_s, |
| 2599 | AFT_IMAGINARY_ANY)) { |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2600 | outmost_tag = 0; |
| 2601 | } else { |
| 2602 | outmost_tag = &outmost_tag_s; |
| 2603 | } |
| 2604 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2605 | OUT("{ "); |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2606 | |
| 2607 | if(outmost_tag && outmost_tag->tag_value == -1) |
| 2608 | OUT("ATF_OPEN_TYPE | "); |
Lev Walkin | c828571 | 2005-03-04 22:18:20 +0000 | [diff] [blame] | 2609 | OUT("%s, ", |
| 2610 | (expr->marker.flags & EM_INDIRECT)?"ATF_POINTER":"ATF_NOFLAGS"); |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 2611 | if((expr->marker.flags & EM_OMITABLE) == EM_OMITABLE) { |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2612 | asn1p_expr_t *tv; |
| 2613 | int opts = 0; |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 2614 | for(tv = expr; |
| 2615 | tv && (tv->marker.flags & EM_OMITABLE) == EM_OMITABLE; |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2616 | tv = TQ_NEXT(tv, next), opts++) { |
| 2617 | if(tv->expr_type == A1TC_EXTENSIBLE) |
| 2618 | opts--; |
| 2619 | } |
| 2620 | OUT("%d, ", opts); |
| 2621 | } else { |
| 2622 | OUT("0, "); |
| 2623 | } |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 2624 | if(expr->_anonymous_type) { |
| 2625 | assert(arg->expr->expr_type == ASN_CONSTR_SET_OF |
| 2626 | || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF); |
| 2627 | OUT("0,\n"); |
| 2628 | } else { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2629 | OUT("offsetof(struct "); |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 2630 | out_name_chain(arg, ONC_avoid_keywords); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2631 | OUT(", "); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2632 | if(arg->expr->expr_type == ASN_CONSTR_CHOICE |
| 2633 | && (!UNNAMED_UNIONS)) OUT("choice."); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 2634 | OUT("%s),\n", MKID_safe(expr)); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2635 | } |
| 2636 | INDENT(+1); |
| 2637 | if(C99_MODE) OUT(".tag = "); |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2638 | if(outmost_tag) { |
| 2639 | if(outmost_tag->tag_value == -1) |
| 2640 | OUT("-1 /* Ambiguous tag (ANY?) */"); |
| 2641 | else |
| 2642 | _print_tag(arg, outmost_tag); |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 2643 | } else { |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2644 | OUT("-1 /* Ambiguous tag (CHOICE?) */"); |
Lev Walkin | c3e2940 | 2004-09-10 06:07:18 +0000 | [diff] [blame] | 2645 | } |
Lev Walkin | b918973 | 2004-09-10 09:37:12 +0000 | [diff] [blame] | 2646 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2647 | OUT(",\n"); |
| 2648 | if(C99_MODE) OUT(".tag_mode = "); |
Lev Walkin | 9ab21b8 | 2006-10-19 02:46:01 +0000 | [diff] [blame] | 2649 | if((!(expr->expr_type & ASN_CONSTR_MASK) |
| 2650 | || expr->expr_type == ASN_CONSTR_CHOICE) |
| 2651 | && expr->tag.tag_class) { |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2652 | if(expr->tag.tag_mode == TM_IMPLICIT) |
| 2653 | OUT("-1,\t/* IMPLICIT tag at current level */\n"); |
| 2654 | else |
| 2655 | OUT("+1,\t/* EXPLICIT tag at current level */\n"); |
| 2656 | } else { |
| 2657 | OUT("0,\n"); |
| 2658 | } |
Lev Walkin | e0b56e0 | 2005-02-25 12:10:27 +0000 | [diff] [blame] | 2659 | |
| 2660 | complex_contents = |
| 2661 | (expr->expr_type & ASN_CONSTR_MASK) |
| 2662 | || expr->expr_type == ASN_BASIC_ENUMERATED |
Lev Walkin | 8ecf9db | 2005-03-03 21:28:12 +0000 | [diff] [blame] | 2663 | || (0 /* -- prohibited by X.693:8.3.4 */ |
| 2664 | && expr->expr_type == ASN_BASIC_INTEGER |
Lev Walkin | 8bb57a2 | 2007-12-03 13:41:36 +0000 | [diff] [blame] | 2665 | && expr_elements_count(arg, expr)) |
| 2666 | || (expr->expr_type == ASN_BASIC_INTEGER |
| 2667 | && asn1c_type_fits_long(arg, expr) == FL_FITS_UNSIGN); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2668 | if(C99_MODE) OUT(".type = "); |
Lev Walkin | ae7bf3f | 2005-08-29 10:31:14 +0000 | [diff] [blame] | 2669 | OUT("&asn_DEF_"); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2670 | if(complex_contents) { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 2671 | OUT("%s", MKID(expr)); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2672 | if(!(arg->flags & A1C_ALL_DEFS_GLOBAL)) |
| 2673 | OUT("_%d", expr->_type_unique_index); |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 2674 | } else { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2675 | OUT("%s", asn1c_type_name(arg, expr, TNF_SAFE)); |
Lev Walkin | 08079b0 | 2004-08-22 03:25:24 +0000 | [diff] [blame] | 2676 | } |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2677 | OUT(",\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2678 | if(C99_MODE) OUT(".memb_constraints = "); |
| 2679 | if(expr->constraints) { |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2680 | if(arg->flags & A1C_NO_CONSTRAINTS) { |
| 2681 | OUT("0,\t/* No check because of -fno-constraints */\n"); |
| 2682 | } else { |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 2683 | char *id = MKID(expr); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2684 | if(expr->_anonymous_type |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 2685 | && !strcmp(expr->Identifier, "Member")) |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2686 | id = asn1c_type_name(arg, expr, TNF_SAFE); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2687 | OUT("memb_%s_constraint_%d,\n", id, |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2688 | arg->expr->_type_unique_index); |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2689 | } |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2690 | } else { |
Lev Walkin | b7bfd1a | 2005-01-17 12:16:58 +0000 | [diff] [blame] | 2691 | OUT("0,\t/* Defer constraints checking to the member type */\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2692 | } |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 2693 | if(C99_MODE) OUT(".oer_constraints = "); |
| 2694 | if(arg->flags & A1C_GEN_OER) { |
| 2695 | if(expr->constraints) { |
| 2696 | OUT("&asn_OER_memb_%s_constr_%d,\n", |
| 2697 | MKID(expr), |
| 2698 | expr->_type_unique_index); |
| 2699 | } else { |
| 2700 | OUT("0,\t/* No OER visible constraints */\n"); |
| 2701 | } |
| 2702 | } else { |
| 2703 | OUT("0,\t/* OER is not compiled, use -gen-OER */\n"); |
| 2704 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2705 | if(C99_MODE) OUT(".per_constraints = "); |
| 2706 | if(arg->flags & A1C_GEN_PER) { |
Lev Walkin | 07aaa34 | 2006-10-09 12:52:15 +0000 | [diff] [blame] | 2707 | if(expr->constraints) { |
Lev Walkin | 4b5dae3 | 2006-10-09 12:27:44 +0000 | [diff] [blame] | 2708 | OUT("&asn_PER_memb_%s_constr_%d,\n", |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 2709 | MKID(expr), |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2710 | expr->_type_unique_index); |
| 2711 | } else { |
| 2712 | OUT("0,\t/* No PER visible constraints */\n"); |
| 2713 | } |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 2714 | } else { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2715 | OUT("0,\t/* PER is not compiled, use -gen-PER */\n"); |
| 2716 | } |
| 2717 | if(C99_MODE) OUT(".default_value = "); |
| 2718 | if(try_inline_default(arg, expr, 0)) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2719 | } else { |
| 2720 | OUT("0,\n"); |
| 2721 | } |
| 2722 | if(C99_MODE) OUT(".name = "); |
| 2723 | if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member")) { |
| 2724 | OUT("\"\"\n"); |
| 2725 | } else { |
| 2726 | OUT("\"%s\"\n", expr->Identifier); |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 2727 | } |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2728 | OUT("},\n"); |
| 2729 | INDENT(-1); |
| 2730 | |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2731 | if(!expr->constraints || (arg->flags & A1C_NO_CONSTRAINTS)) |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2732 | return 0; |
| 2733 | |
| 2734 | save_target = arg->target->target; |
| 2735 | REDIR(OT_CODE); |
| 2736 | |
Lev Walkin | 152a91e | 2005-02-14 20:41:29 +0000 | [diff] [blame] | 2737 | if(expr->_anonymous_type && !strcmp(expr->Identifier, "Member")) |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2738 | p = asn1c_type_name(arg, expr, TNF_SAFE); |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 2739 | else |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 2740 | p = MKID(expr); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2741 | OUT("static int\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2742 | OUT("memb_%s_constraint_%d(asn_TYPE_descriptor_t *td, const void *sptr,\n", p, arg->expr->_type_unique_index); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2743 | INDENT(+1); |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 2744 | OUT("\t\tasn_app_constraint_failed_f *ctfailcb, void *app_key) {\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2745 | tmp_arg = *arg; |
| 2746 | tmp_arg.expr = expr; |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2747 | DEBUG("member constraint checking code for %s", p); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2748 | if(asn1c_emit_constraint_checking_code(&tmp_arg) == 1) { |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2749 | OUT("return td->check_constraints" |
Lev Walkin | 1eded35 | 2006-07-13 11:19:01 +0000 | [diff] [blame] | 2750 | "(td, sptr, ctfailcb, app_key);\n"); |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2751 | } |
| 2752 | INDENT(-1); |
| 2753 | OUT("}\n"); |
| 2754 | OUT("\n"); |
| 2755 | |
Lev Walkin | 98eabc1 | 2017-07-19 08:51:11 +0400 | [diff] [blame] | 2756 | if(emit_member_OER_constraints(arg, expr, "memb")) |
| 2757 | return -1; |
| 2758 | |
Lev Walkin | 4b5dae3 | 2006-10-09 12:27:44 +0000 | [diff] [blame] | 2759 | if(emit_member_PER_constraints(arg, expr, "memb")) |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2760 | return -1; |
| 2761 | |
Lev Walkin | 59004fa | 2004-08-20 13:37:01 +0000 | [diff] [blame] | 2762 | REDIR(save_target); |
| 2763 | |
| 2764 | return 0; |
| 2765 | } |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2766 | |
Lev Walkin | dc06f6b | 2004-10-20 15:50:55 +0000 | [diff] [blame] | 2767 | /* |
| 2768 | * Generate "asn_DEF_XXX" type definition. |
| 2769 | */ |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2770 | static int |
Lev Walkin | 8de2ab2 | 2004-09-26 13:11:31 +0000 | [diff] [blame] | 2771 | emit_type_DEF(arg_t *arg, asn1p_expr_t *expr, enum tvm_compat tv_mode, int tags_count, int all_tags_count, int elements_count, enum etd_spec spec) { |
Lev Walkin | 8c2ca0b | 2006-09-17 06:31:08 +0000 | [diff] [blame] | 2772 | asn1p_expr_t *terminal; |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2773 | int using_type_name = 0; |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 2774 | char *p = MKID(expr); |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2775 | char *p2 = (char *)0; |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2776 | |
Lev Walkin | 8c2ca0b | 2006-09-17 06:31:08 +0000 | [diff] [blame] | 2777 | terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 2778 | |
Lev Walkin | 01582a7 | 2017-07-26 18:47:08 -0700 | [diff] [blame] | 2779 | if(emit_member_OER_constraints(arg, expr, "type")) |
| 2780 | return -1; |
| 2781 | |
Lev Walkin | 4b5dae3 | 2006-10-09 12:27:44 +0000 | [diff] [blame] | 2782 | if(emit_member_PER_constraints(arg, expr, "type")) |
Lev Walkin | 725883b | 2006-10-09 12:07:58 +0000 | [diff] [blame] | 2783 | return -1; |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2784 | |
Lev Walkin | dd32b59 | 2004-09-06 08:07:29 +0000 | [diff] [blame] | 2785 | if(HIDE_INNER_DEFS) |
| 2786 | OUT("static /* Use -fall-defs-global to expose */\n"); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2787 | OUT("asn_TYPE_descriptor_t asn_DEF_%s", p); |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2788 | if(HIDE_INNER_DEFS) OUT("_%d", expr->_type_unique_index); |
| 2789 | OUT(" = {\n"); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2790 | INDENT(+1); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2791 | |
| 2792 | if(expr->_anonymous_type) { |
| 2793 | p = ASN_EXPR_TYPE2STR(expr->expr_type); |
| 2794 | OUT("\"%s\",\n", p?p:""); |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 2795 | OUT("\"%s\",\n", |
| 2796 | p ? asn1c_make_identifier(AMI_CHECK_RESERVED, |
| 2797 | 0, p, 0) : ""); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2798 | } else { |
| 2799 | OUT("\"%s\",\n", expr->Identifier); |
| 2800 | OUT("\"%s\",\n", expr->Identifier); |
| 2801 | } |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2802 | |
| 2803 | if(expr->expr_type & ASN_CONSTR_MASK) { |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2804 | using_type_name = 1; |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2805 | p = asn1c_type_name(arg, arg->expr, TNF_SAFE); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2806 | } else { |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2807 | if (expr->expr_type == A1TC_REFERENCE) { |
| 2808 | p2 = strdup(asn1c_type_name(arg, terminal, TNF_SAFE)); |
| 2809 | } else { |
| 2810 | p2 = strdup(asn1c_type_name(arg, expr, TNF_SAFE)); |
| 2811 | } |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 2812 | p = MKID(expr); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2813 | } |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2814 | if (!p2) |
| 2815 | p2 = strdup(p); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2816 | |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2817 | #define FUNCREF(foo) do { \ |
| 2818 | OUT("%s", p); \ |
| 2819 | if(HIDE_INNER_DEFS && !using_type_name) \ |
| 2820 | OUT("_%d", expr->_type_unique_index); \ |
| 2821 | OUT("_" #foo ",\n"); \ |
| 2822 | } while(0) |
| 2823 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2824 | #define FUNCREF2(foo) \ |
| 2825 | do { \ |
| 2826 | OUT("%s", p2); \ |
| 2827 | OUT("_" #foo ",\n"); \ |
| 2828 | } while(0) |
| 2829 | |
| 2830 | FUNCREF2(free); |
| 2831 | FUNCREF2(print); |
mattipee | e0b99ab | 2017-08-04 11:44:04 +0100 | [diff] [blame] | 2832 | if (arg->flags & A1C_NO_CONSTRAINTS) |
| 2833 | OUT("0,\t/* No check because of -fno-constraints */\n"); |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2834 | else |
mattipee | e0b99ab | 2017-08-04 11:44:04 +0100 | [diff] [blame] | 2835 | { |
| 2836 | if (!expr->combined_constraints) |
| 2837 | FUNCREF2(constraint); |
| 2838 | else |
| 2839 | FUNCREF(constraint); |
| 2840 | } |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2841 | FUNCREF2(decode_ber); |
| 2842 | FUNCREF2(encode_der); |
| 2843 | FUNCREF2(decode_xer); |
| 2844 | FUNCREF2(encode_xer); |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 2845 | |
| 2846 | if(arg->flags & A1C_GEN_OER) { |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2847 | FUNCREF2(decode_oer); |
| 2848 | FUNCREF2(encode_oer); |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 2849 | } else { |
| 2850 | OUT("0, 0,\t/* No OER support, " |
| 2851 | "use \"-gen-OER\" to enable */\n"); |
| 2852 | } |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 2853 | if(arg->flags & A1C_GEN_PER) { |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2854 | FUNCREF2(decode_uper); |
| 2855 | FUNCREF2(encode_uper); |
Lev Walkin | 1dc8529 | 2006-08-18 01:32:26 +0000 | [diff] [blame] | 2856 | } else { |
| 2857 | OUT("0, 0,\t/* No PER support, " |
| 2858 | "use \"-gen-PER\" to enable */\n"); |
| 2859 | } |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2860 | if (p2) free(p2); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2861 | |
Lev Walkin | 8c2ca0b | 2006-09-17 06:31:08 +0000 | [diff] [blame] | 2862 | if(!terminal || terminal->expr_type == ASN_CONSTR_CHOICE) { |
| 2863 | //if(expr->expr_type == ASN_CONSTR_CHOICE) { |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2864 | OUT("CHOICE_outmost_tag,\n"); |
| 2865 | } else { |
| 2866 | OUT("0,\t/* Use generic outmost tag fetcher */\n"); |
| 2867 | } |
| 2868 | |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 2869 | p = MKID(expr); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2870 | if(tags_count) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2871 | OUT("asn_DEF_%s_tags_%d,\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2872 | p, expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2873 | OUT("sizeof(asn_DEF_%s_tags_%d)\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2874 | p, expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2875 | OUT("\t/sizeof(asn_DEF_%s_tags_%d[0])", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2876 | p, expr->_type_unique_index); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2877 | if(tv_mode == _TVM_SUBSET |
| 2878 | && tags_count != all_tags_count) |
| 2879 | OUT(" - %d", all_tags_count - tags_count); |
| 2880 | OUT(", /* %d */\n", tags_count); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2881 | } else { |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2882 | OUT("0,\t/* No effective tags (pointer) */\n"); |
| 2883 | OUT("0,\t/* No effective tags (count) */\n"); |
| 2884 | } |
| 2885 | |
| 2886 | if(all_tags_count && tv_mode == _TVM_DIFFERENT) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2887 | OUT("asn_DEF_%s_all_tags_%d,\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2888 | p, expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2889 | OUT("sizeof(asn_DEF_%s_all_tags_%d)\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2890 | p, expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2891 | OUT("\t/sizeof(asn_DEF_%s_all_tags_%d[0]), /* %d */\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2892 | p, expr->_type_unique_index, all_tags_count); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2893 | } else if(all_tags_count) { |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2894 | OUT("asn_DEF_%s_tags_%d,\t/* Same as above */\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2895 | p, expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2896 | OUT("sizeof(asn_DEF_%s_tags_%d)\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2897 | p, expr->_type_unique_index); |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2898 | OUT("\t/sizeof(asn_DEF_%s_tags_%d[0]), /* %d */\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2899 | p, expr->_type_unique_index, all_tags_count); |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2900 | } else { |
| 2901 | OUT("0,\t/* No tags (pointer) */\n"); |
| 2902 | OUT("0,\t/* No tags (count) */\n"); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Lev Walkin | b33425f | 2017-07-14 14:59:52 +0400 | [diff] [blame] | 2905 | if(arg->flags & A1C_GEN_OER) { |
| 2906 | if(expr->constraints |
| 2907 | || expr->expr_type == ASN_BASIC_ENUMERATED |
| 2908 | || expr->expr_type == ASN_CONSTR_CHOICE) { |
| 2909 | OUT("&asn_OER_type_%s_constr_%d,\n", |
| 2910 | p, expr->_type_unique_index); |
| 2911 | } else { |
| 2912 | OUT("0,\t/* No OER visible constraints */\n"); |
| 2913 | } |
| 2914 | } else { |
| 2915 | OUT("0,\t/* No OER visible constraints */\n"); |
| 2916 | } |
| 2917 | |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2918 | if(arg->flags & A1C_GEN_PER) { |
| 2919 | if(expr->constraints |
| 2920 | || expr->expr_type == ASN_BASIC_ENUMERATED |
| 2921 | || expr->expr_type == ASN_CONSTR_CHOICE) { |
Lev Walkin | 4b5dae3 | 2006-10-09 12:27:44 +0000 | [diff] [blame] | 2922 | OUT("&asn_PER_type_%s_constr_%d,\n", |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2923 | p, expr->_type_unique_index); |
| 2924 | } else { |
| 2925 | OUT("0,\t/* No PER visible constraints */\n"); |
| 2926 | } |
| 2927 | } else { |
| 2928 | OUT("0,\t/* No PER visible constraints */\n"); |
| 2929 | } |
| 2930 | |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2931 | if(elements_count || |
| 2932 | ((expr->expr_type == A1TC_REFERENCE) && |
| 2933 | (terminal->expr_type & ASN_CONSTR_MASK) && |
| 2934 | expr_elements_count(arg, terminal))) { |
| 2935 | |
| 2936 | if (expr->expr_type == A1TC_REFERENCE) { |
| 2937 | OUT("asn_MBR_%s_%d,\n", MKID(terminal), terminal->_type_unique_index); |
| 2938 | |
| 2939 | if(terminal->expr_type == ASN_CONSTR_SEQUENCE_OF |
| 2940 | || terminal->expr_type == ASN_CONSTR_SET_OF) { |
| 2941 | OUT("%d,\t/* Single element */\n", |
| 2942 | expr_elements_count(arg, terminal)); |
| 2943 | assert(expr_elements_count(arg, terminal) == 1); |
| 2944 | } else { |
| 2945 | OUT("%d,\t/* Elements count */\n", |
| 2946 | expr_elements_count(arg, terminal)); |
| 2947 | } |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2948 | } else { |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2949 | OUT("asn_MBR_%s_%d,\n", p, expr->_type_unique_index); |
| 2950 | |
| 2951 | if(expr->expr_type == ASN_CONSTR_SEQUENCE_OF |
| 2952 | || expr->expr_type == ASN_CONSTR_SET_OF) { |
| 2953 | OUT("%d,\t/* Single element */\n", |
| 2954 | elements_count); |
| 2955 | assert(elements_count == 1); |
| 2956 | } else { |
| 2957 | OUT("%d,\t/* Elements count */\n", |
| 2958 | elements_count); |
| 2959 | } |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2960 | } |
| 2961 | } else { |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2962 | if(expr_elements_count(arg, expr)) |
| 2963 | OUT("0, 0,\t/* Defined elsewhere */\n"); |
| 2964 | else |
| 2965 | OUT("0, 0,\t/* No members */\n"); |
| 2966 | } |
| 2967 | |
| 2968 | switch(spec) { |
| 2969 | case ETD_NO_SPECIFICS: |
Bi-Ruei, Chiu | 1fa31c9 | 2016-05-16 13:50:09 +0800 | [diff] [blame] | 2970 | if ((expr->expr_type == A1TC_REFERENCE) && |
| 2971 | ((terminal->expr_type & ASN_CONSTR_MASK) || |
| 2972 | (terminal->expr_type == ASN_BASIC_ENUMERATED) || |
| 2973 | ((terminal->expr_type == ASN_BASIC_INTEGER) && |
| 2974 | (asn1c_type_fits_long(arg, terminal) == FL_FITS_UNSIGN)))) { |
| 2975 | OUT("&asn_SPC_%s_specs_%d\t/* Additional specs */\n", MKID(terminal), terminal->_type_unique_index); |
| 2976 | } else if ((expr->expr_type == ASN_TYPE_ANY) || |
| 2977 | (expr->expr_type == ASN_BASIC_BIT_STRING) || |
| 2978 | (expr->expr_type == ASN_STRING_BMPString) || |
| 2979 | (expr->expr_type == ASN_BASIC_OCTET_STRING) || |
| 2980 | (expr->expr_type == ASN_STRING_UniversalString)) { |
| 2981 | OUT("&asn_SPC_%s_specs\t/* Additional specs */\n", asn1c_type_name(arg, expr, TNF_SAFE)); |
| 2982 | } else if ((expr->expr_type == A1TC_REFERENCE) && |
| 2983 | ((terminal->expr_type == ASN_TYPE_ANY) || |
| 2984 | (terminal->expr_type == ASN_BASIC_BIT_STRING) || |
| 2985 | (terminal->expr_type == ASN_STRING_BMPString) || |
| 2986 | (terminal->expr_type == ASN_BASIC_OCTET_STRING) || |
| 2987 | (terminal->expr_type == ASN_STRING_UniversalString))) { |
| 2988 | OUT("&asn_SPC_%s_specs\t/* Additional specs */\n", asn1c_type_name(arg, terminal, TNF_SAFE)); |
| 2989 | } else { |
| 2990 | OUT("0\t/* No specifics */\n"); |
| 2991 | } |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2992 | break; |
| 2993 | case ETD_HAS_SPECIFICS: |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 2994 | OUT("&asn_SPC_%s_specs_%d\t/* Additional specs */\n", |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 2995 | p, expr->_type_unique_index); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2996 | } |
Lev Walkin | 188ed2c | 2004-09-13 08:31:01 +0000 | [diff] [blame] | 2997 | INDENT(-1); |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 2998 | OUT("};\n"); |
| 2999 | OUT("\n"); |
Lev Walkin | 9729878 | 2004-08-26 06:20:34 +0000 | [diff] [blame] | 3000 | |
| 3001 | return 0; |
Lev Walkin | 59964be | 2004-08-25 02:03:12 +0000 | [diff] [blame] | 3002 | } |
Lev Walkin | cc93b0f | 2004-09-10 09:18:20 +0000 | [diff] [blame] | 3003 | |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 3004 | static int |
| 3005 | expr_as_xmlvaluelist(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 3006 | /* |
| 3007 | * X.680, 25.5, Table 5 |
| 3008 | */ |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 3009 | switch(expr_get_type(arg, expr)) { |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 3010 | case ASN_BASIC_BOOLEAN: |
| 3011 | case ASN_BASIC_ENUMERATED: |
| 3012 | case ASN_BASIC_NULL: |
| 3013 | return 1; |
Lev Walkin | d1bfea6 | 2005-11-08 03:06:16 +0000 | [diff] [blame] | 3014 | case ASN_CONSTR_CHOICE: |
| 3015 | return 2; |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 3016 | default: |
| 3017 | return 0; |
| 3018 | } |
| 3019 | } |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 3020 | |
| 3021 | static int |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 3022 | out_name_chain(arg_t *arg, enum onc_flags onc_flags) { |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 3023 | asn1p_expr_t *expr = arg->expr; |
| 3024 | char *id; |
| 3025 | |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 3026 | if((arg->flags & A1C_COMPOUND_NAMES |
| 3027 | || onc_flags & ONC_force_compound_name) |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 3028 | && ((expr->expr_type & ASN_CONSTR_MASK) |
| 3029 | || expr->expr_type == ASN_BASIC_ENUMERATED |
Lev Walkin | b02a883 | 2005-08-13 23:51:47 +0000 | [diff] [blame] | 3030 | || ((expr->expr_type == ASN_BASIC_INTEGER |
| 3031 | || expr->expr_type == ASN_BASIC_BIT_STRING) |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 3032 | && expr_elements_count(arg, expr)) |
| 3033 | ) |
Bi-Ruei, Chiu | 94f0b64 | 2017-06-29 01:34:23 +0800 | [diff] [blame] | 3034 | && expr->parent_expr) { |
| 3035 | |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 3036 | arg_t tmparg = *arg; |
| 3037 | |
| 3038 | tmparg.expr = expr->parent_expr; |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 3039 | if(0) tmparg.flags &= ~A1C_COMPOUND_NAMES; |
| 3040 | |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 3041 | out_name_chain(&tmparg, onc_flags); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 3042 | |
Bi-Ruei, Chiu | 94f0b64 | 2017-06-29 01:34:23 +0800 | [diff] [blame] | 3043 | if(expr->parent_expr->Identifier) OUT("__"); /* a separator between id components */ |
Lev Walkin | 21d0000 | 2005-03-04 08:48:53 +0000 | [diff] [blame] | 3044 | |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 3045 | /* Fall through */ |
| 3046 | } |
| 3047 | |
Lev Walkin | 0f5d74c | 2005-08-14 15:03:31 +0000 | [diff] [blame] | 3048 | if(onc_flags & ONC_avoid_keywords) |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 3049 | id = MKID_safe(expr); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 3050 | else |
Lev Walkin | a00d6b3 | 2006-03-21 03:40:38 +0000 | [diff] [blame] | 3051 | id = MKID(expr); |
Lev Walkin | 801fabc | 2005-01-28 12:18:50 +0000 | [diff] [blame] | 3052 | OUT("%s", id); |
| 3053 | |
| 3054 | return 0; |
| 3055 | } |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 3056 | |
| 3057 | static int |
| 3058 | emit_include_dependencies(arg_t *arg) { |
| 3059 | asn1p_expr_t *expr = arg->expr; |
| 3060 | asn1p_expr_t *memb; |
| 3061 | |
| 3062 | /* Avoid recursive definitions. */ |
| 3063 | TQ_FOR(memb, &(expr->members), next) { |
| 3064 | expr_break_recursion(arg, memb); |
| 3065 | } |
| 3066 | |
| 3067 | TQ_FOR(memb, &(expr->members), next) { |
| 3068 | |
| 3069 | if(memb->marker.flags & (EM_INDIRECT | EM_UNRECURSE)) { |
| 3070 | if(terminal_structable(arg, memb)) { |
| 3071 | int saved_target = arg->target->target; |
Bi-Ruei, Chiu | 9b87e5b | 2016-06-06 00:23:16 +0800 | [diff] [blame] | 3072 | if(saved_target != OT_FWD_DECLS) { |
| 3073 | REDIR(OT_FWD_DECLS); |
| 3074 | OUT("%s;\n", |
| 3075 | asn1c_type_name(arg, memb, TNF_RSAFE)); |
| 3076 | } |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 3077 | REDIR(saved_target); |
| 3078 | } |
| 3079 | } |
| 3080 | |
| 3081 | if((!(memb->expr_type & ASN_CONSTR_MASK) |
| 3082 | && memb->expr_type > ASN_CONSTR_MASK) |
| 3083 | || memb->meta_type == AMT_TYPEREF) { |
| 3084 | if(memb->marker.flags & EM_UNRECURSE) { |
| 3085 | GEN_POSTINCLUDE(asn1c_type_name(arg, |
| 3086 | memb, TNF_INCLUDE)); |
| 3087 | } else { |
| 3088 | GEN_INCLUDE(asn1c_type_name(arg, |
| 3089 | memb, TNF_INCLUDE)); |
| 3090 | } |
| 3091 | } |
| 3092 | } |
| 3093 | |
| 3094 | return 0; |
| 3095 | } |
| 3096 | |
| 3097 | /* |
| 3098 | * Check if it is better to make this type indirectly accessed via |
| 3099 | * a pointer. |
| 3100 | * This may be the case for the following recursive definition: |
| 3101 | * Type ::= CHOICE { member Type }; |
| 3102 | */ |
| 3103 | static int |
| 3104 | expr_break_recursion(arg_t *arg, asn1p_expr_t *expr) { |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 3105 | int ret; |
| 3106 | |
| 3107 | if(expr->marker.flags & EM_UNRECURSE) |
| 3108 | return 1; /* Already broken */ |
| 3109 | |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 3110 | /* -findirect-choice compiles members of CHOICE as indirect pointers */ |
| 3111 | if((arg->flags & A1C_INDIRECT_CHOICE) |
| 3112 | && arg->expr->expr_type == ASN_CONSTR_CHOICE |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 3113 | && (expr_get_type(arg, expr) & ASN_CONSTR_MASK) |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 3114 | ) { |
| 3115 | /* Break cross-reference */ |
| 3116 | expr->marker.flags |= EM_INDIRECT | EM_UNRECURSE; |
| 3117 | return 1; |
| 3118 | } |
| 3119 | |
| 3120 | if((expr->marker.flags & EM_INDIRECT) |
| 3121 | || arg->expr->expr_type == ASN_CONSTR_SET_OF |
| 3122 | || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) { |
| 3123 | if(terminal_structable(arg, expr)) { |
| 3124 | expr->marker.flags |= EM_UNRECURSE; |
| 3125 | |
| 3126 | if(arg->expr->expr_type == ASN_CONSTR_SET_OF |
| 3127 | || arg->expr->expr_type == ASN_CONSTR_SEQUENCE_OF) { |
| 3128 | /* Don't put EM_INDIRECT even if recursion */ |
| 3129 | return 1; |
| 3130 | } |
| 3131 | |
| 3132 | /* Fall through */ |
| 3133 | } |
| 3134 | } |
| 3135 | |
| 3136 | /* Look for recursive back-references */ |
| 3137 | ret = expr_defined_recursively(arg, expr); |
| 3138 | switch(ret) { |
| 3139 | case 2: /* Explicitly break the recursion */ |
| 3140 | case 1: /* Use safer typing */ |
| 3141 | expr->marker.flags |= EM_INDIRECT; |
| 3142 | expr->marker.flags |= EM_UNRECURSE; |
| 3143 | break; |
| 3144 | } |
| 3145 | |
| 3146 | return 0; |
| 3147 | } |
| 3148 | |
| 3149 | /* |
| 3150 | * Check if the type can be represented using simple `struct TYPE`. |
| 3151 | */ |
| 3152 | static asn1p_expr_t * |
| 3153 | terminal_structable(arg_t *arg, asn1p_expr_t *expr) { |
| 3154 | asn1p_expr_t *terminal = asn1f_find_terminal_type_ex(arg->asn, expr); |
| 3155 | if(terminal |
| 3156 | && !terminal->parent_expr |
| 3157 | && (terminal->expr_type & ASN_CONSTR_MASK)) { |
| 3158 | return terminal; |
| 3159 | } |
| 3160 | return 0; |
| 3161 | } |
| 3162 | |
| 3163 | static int |
| 3164 | asn1c_recurse(arg_t *arg, asn1p_expr_t *expr, int (*callback)(arg_t *arg, void *key), void *key) { |
| 3165 | arg_t tmp = *arg; |
| 3166 | int maxret = 0; |
| 3167 | int ret; |
| 3168 | |
Bi-Ruei, Chiu | 80fd306 | 2017-05-07 21:00:51 +0800 | [diff] [blame] | 3169 | if(expr->_mark & TM_RECURSION) return 0; |
Lev Walkin | b85a813 | 2005-08-18 13:38:19 +0000 | [diff] [blame] | 3170 | expr->_mark |= TM_RECURSION; |
| 3171 | |
| 3172 | /* Invoke callback for every type going into recursion */ |
| 3173 | tmp.expr = expr; |
| 3174 | maxret = callback(&tmp, key); |
| 3175 | if(maxret <= 1) { |
| 3176 | /* |
| 3177 | * Recursively invoke myself and the callbacks. |
| 3178 | */ |
| 3179 | TQ_FOR(tmp.expr, &(expr->members), next) { |
| 3180 | ret = asn1c_recurse(&tmp, tmp.expr, callback, key); |
| 3181 | if(ret > maxret) |
| 3182 | maxret = ret; |
| 3183 | if(maxret > 1) break; |
| 3184 | } |
| 3185 | } |
| 3186 | |
| 3187 | expr->_mark &= ~TM_RECURSION; |
| 3188 | return maxret; |
| 3189 | } |
| 3190 | |
| 3191 | static int |
| 3192 | check_is_refer_to(arg_t *arg, void *key) { |
| 3193 | asn1p_expr_t *terminal = terminal_structable(arg, arg->expr); |
| 3194 | if(terminal == key) { |
| 3195 | if(arg->expr->marker.flags & EM_INDIRECT) |
| 3196 | return 1; /* This is almost safe indirection */ |
| 3197 | return 2; |
| 3198 | } else if(terminal) { |
| 3199 | /* This might be N-step circular loop. Dive into it. */ |
| 3200 | return asn1c_recurse(arg, terminal, check_is_refer_to, key); |
| 3201 | } |
| 3202 | return 0; |
| 3203 | } |
| 3204 | |
| 3205 | /* |
| 3206 | * Check if the possibly inner expression defined recursively. |
| 3207 | */ |
| 3208 | static int |
| 3209 | expr_defined_recursively(arg_t *arg, asn1p_expr_t *expr) { |
| 3210 | asn1p_expr_t *terminal; |
| 3211 | asn1p_expr_t *topmost; |
| 3212 | |
| 3213 | /* If expression is top-level, there's no way it can be recursive. */ |
| 3214 | if(expr->parent_expr == 0) return 0; |
| 3215 | if(expr->expr_type != A1TC_REFERENCE) |
| 3216 | return 0; /* Basic types are never recursive */ |
| 3217 | |
| 3218 | terminal = terminal_structable(arg, expr); |
| 3219 | if(!terminal) return 0; /* Terminal cannot be indirected */ |
| 3220 | |
| 3221 | /* Search for the parent container for the given expression */ |
| 3222 | topmost = expr; |
| 3223 | while(topmost->parent_expr) |
| 3224 | topmost = topmost->parent_expr; |
| 3225 | |
| 3226 | /* Look inside the terminal type if it mentions the parent expression */ |
| 3227 | return asn1c_recurse(arg, terminal, check_is_refer_to, topmost); |
| 3228 | } |
Lev Walkin | 59b176e | 2005-11-26 11:25:14 +0000 | [diff] [blame] | 3229 | |
| 3230 | struct canonical_map_element { |
| 3231 | int eidx; |
| 3232 | asn1p_expr_t *expr; |
| 3233 | }; |
| 3234 | static int compar_cameo(const void *ap, const void *bp); |
| 3235 | static arg_t *cameo_arg; |
| 3236 | static int * |
| 3237 | compute_canonical_members_order(arg_t *arg, int el_count) { |
| 3238 | struct canonical_map_element *cmap; |
| 3239 | int *rmap; |
| 3240 | asn1p_expr_t *v; |
| 3241 | int eidx = 0; |
| 3242 | int ext_start = -1; |
| 3243 | int nextmax = -1; |
| 3244 | int already_sorted = 1; |
| 3245 | |
| 3246 | cmap = calloc(el_count, sizeof *cmap); |
| 3247 | assert(cmap); |
| 3248 | |
| 3249 | TQ_FOR(v, &(arg->expr->members), next) { |
| 3250 | if(v->expr_type != A1TC_EXTENSIBLE) { |
| 3251 | cmap[eidx].eidx = eidx; |
| 3252 | cmap[eidx].expr = v; |
| 3253 | eidx++; |
| 3254 | } else if(ext_start == -1) |
| 3255 | ext_start = eidx; |
| 3256 | } |
| 3257 | |
| 3258 | cameo_arg = arg; |
| 3259 | if(ext_start == -1) { |
| 3260 | /* Sort the whole thing */ |
| 3261 | qsort(cmap, el_count, sizeof(*cmap), compar_cameo); |
| 3262 | } else { |
| 3263 | /* Sort root and extensions independently */ |
| 3264 | qsort(cmap, ext_start, sizeof(*cmap), compar_cameo); |
| 3265 | qsort(cmap + ext_start, el_count - ext_start, |
| 3266 | sizeof(*cmap), compar_cameo); |
| 3267 | } |
| 3268 | |
| 3269 | /* move data back to a simpler map */ |
| 3270 | rmap = calloc(el_count, sizeof *rmap); |
| 3271 | assert(rmap); |
| 3272 | for(eidx = 0; eidx < el_count; eidx++) { |
| 3273 | rmap[eidx] = cmap[eidx].eidx; |
| 3274 | if(rmap[eidx] <= nextmax) |
| 3275 | already_sorted = 0; |
| 3276 | else |
| 3277 | nextmax = rmap[eidx]; |
| 3278 | } |
| 3279 | free(cmap); |
| 3280 | |
| 3281 | if(already_sorted) { free(rmap); rmap = 0; } |
| 3282 | return rmap; |
| 3283 | } |
| 3284 | static int compar_cameo(const void *ap, const void *bp) { |
| 3285 | const struct canonical_map_element *a = (const void *)ap; |
| 3286 | const struct canonical_map_element *b = (const void *)bp; |
| 3287 | struct asn1p_type_tag_s atag, btag; |
| 3288 | arg_t *arg = cameo_arg; |
| 3289 | |
| 3290 | if(asn1f_fetch_outmost_tag(arg->asn, a->expr->module, a->expr, |
| 3291 | &atag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE)) |
| 3292 | return 1; |
| 3293 | |
| 3294 | if(asn1f_fetch_outmost_tag(arg->asn, b->expr->module, b->expr, |
| 3295 | &btag, AFT_IMAGINARY_ANY | AFT_CANON_CHOICE)) |
| 3296 | return -1; |
| 3297 | |
| 3298 | if(atag.tag_class < btag.tag_class) |
| 3299 | return -1; |
| 3300 | if(atag.tag_class > btag.tag_class) |
| 3301 | return 1; |
| 3302 | if(atag.tag_value < btag.tag_value) |
| 3303 | return -1; |
| 3304 | if(atag.tag_value > btag.tag_value) |
| 3305 | return 1; |
| 3306 | return 0; |
| 3307 | |
| 3308 | } |